isl_basic_map_offset: extract out isl_basic_map_var_offset
[isl.git] / isl_map.c
blob8b674b84a4abeebbaf5c8e0606318f7c2a8cc7e6
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 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1701 enum isl_dim_type type, unsigned first, unsigned n)
1703 unsigned dim;
1705 if (!bmap)
1706 return isl_stat_error;
1707 dim = isl_basic_map_dim(bmap, type);
1708 if (first + n > dim || first + n < first)
1709 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1710 "position or range out of bounds",
1711 return isl_stat_error);
1712 return isl_stat_ok;
1715 /* Insert an extra integer division, prescribed by "div", to "bmap"
1716 * at (integer division) position "pos".
1718 * The integer division is first added at the end and then moved
1719 * into the right position.
1721 __isl_give isl_basic_map *isl_basic_map_insert_div(
1722 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1724 int i, k;
1726 bmap = isl_basic_map_cow(bmap);
1727 if (!bmap || !div)
1728 return isl_basic_map_free(bmap);
1730 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1731 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1732 "unexpected size", return isl_basic_map_free(bmap));
1733 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1734 return isl_basic_map_free(bmap);
1736 bmap = isl_basic_map_extend_space(bmap,
1737 isl_basic_map_get_space(bmap), 1, 0, 2);
1738 k = isl_basic_map_alloc_div(bmap);
1739 if (k < 0)
1740 return isl_basic_map_free(bmap);
1741 isl_seq_cpy(bmap->div[k], div->el, div->size);
1742 isl_int_set_si(bmap->div[k][div->size], 0);
1744 for (i = k; i > pos; --i)
1745 isl_basic_map_swap_div(bmap, i, i - 1);
1747 return bmap;
1750 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1752 if (!bmap)
1753 return isl_stat_error;
1754 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1755 bmap->n_div -= n;
1756 return isl_stat_ok;
1759 static __isl_give isl_basic_map *add_constraints(
1760 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1761 unsigned i_pos, unsigned o_pos)
1763 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1764 isl_ctx *ctx;
1765 isl_space *space;
1766 struct isl_dim_map *dim_map;
1768 space = isl_basic_map_peek_space(bmap2);
1769 if (!bmap1 || !space)
1770 goto error;
1772 total = isl_basic_map_dim(bmap1, isl_dim_all);
1773 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1774 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1775 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1776 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1777 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1778 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1779 ctx = isl_basic_map_get_ctx(bmap1);
1780 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1781 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1782 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1783 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1784 isl_dim_map_div(dim_map, bmap2, total);
1786 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1787 error:
1788 isl_basic_map_free(bmap1);
1789 isl_basic_map_free(bmap2);
1790 return NULL;
1793 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1794 struct isl_basic_set *bset2, unsigned pos)
1796 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1797 bset_to_bmap(bset2), 0, pos));
1800 __isl_give isl_basic_map *isl_basic_map_extend_space(
1801 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1802 unsigned extra, unsigned n_eq, unsigned n_ineq)
1804 struct isl_basic_map *ext;
1805 unsigned flags;
1806 int dims_ok;
1808 if (!space)
1809 goto error;
1811 if (!base)
1812 goto error;
1814 dims_ok = isl_space_is_equal(base->dim, space) &&
1815 base->extra >= base->n_div + extra;
1817 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1818 room_for_ineq(base, n_ineq)) {
1819 isl_space_free(space);
1820 return base;
1823 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1824 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1825 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1826 extra += base->extra;
1827 n_eq += base->n_eq;
1828 n_ineq += base->n_ineq;
1830 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1831 space = NULL;
1832 if (!ext)
1833 goto error;
1835 if (dims_ok)
1836 ext->sample = isl_vec_copy(base->sample);
1837 flags = base->flags;
1838 ext = add_constraints(ext, base, 0, 0);
1839 if (ext) {
1840 ext->flags = flags;
1841 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1844 return ext;
1846 error:
1847 isl_space_free(space);
1848 isl_basic_map_free(base);
1849 return NULL;
1852 __isl_give isl_basic_set *isl_basic_set_extend_space(
1853 __isl_take isl_basic_set *base,
1854 __isl_take isl_space *dim, unsigned extra,
1855 unsigned n_eq, unsigned n_ineq)
1857 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1858 dim, extra, n_eq, n_ineq));
1861 struct isl_basic_map *isl_basic_map_extend_constraints(
1862 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1864 if (!base)
1865 return NULL;
1866 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1867 0, n_eq, n_ineq);
1870 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1871 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1872 unsigned n_eq, unsigned n_ineq)
1874 struct isl_basic_map *bmap;
1875 isl_space *dim;
1877 if (!base)
1878 return NULL;
1879 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1880 if (!dim)
1881 goto error;
1883 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1884 return bmap;
1885 error:
1886 isl_basic_map_free(base);
1887 return NULL;
1890 struct isl_basic_set *isl_basic_set_extend_constraints(
1891 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1893 isl_basic_map *bmap = bset_to_bmap(base);
1894 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1895 return bset_from_bmap(bmap);
1898 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1900 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1903 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1905 if (!bmap)
1906 return NULL;
1908 if (bmap->ref > 1) {
1909 bmap->ref--;
1910 bmap = isl_basic_map_dup(bmap);
1912 if (bmap) {
1913 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1914 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1916 return bmap;
1919 /* Clear all cached information in "map", either because it is about
1920 * to be modified or because it is being freed.
1921 * Always return the same pointer that is passed in.
1922 * This is needed for the use in isl_map_free.
1924 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1926 isl_basic_map_free(map->cached_simple_hull[0]);
1927 isl_basic_map_free(map->cached_simple_hull[1]);
1928 map->cached_simple_hull[0] = NULL;
1929 map->cached_simple_hull[1] = NULL;
1930 return map;
1933 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1935 return isl_map_cow(set);
1938 /* Return an isl_map that is equal to "map" and that has only
1939 * a single reference.
1941 * If the original input already has only one reference, then
1942 * simply return it, but clear all cached information, since
1943 * it may be rendered invalid by the operations that will be
1944 * performed on the result.
1946 * Otherwise, create a duplicate (without any cached information).
1948 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1950 if (!map)
1951 return NULL;
1953 if (map->ref == 1)
1954 return clear_caches(map);
1955 map->ref--;
1956 return isl_map_dup(map);
1959 static void swap_vars(struct isl_blk blk, isl_int *a,
1960 unsigned a_len, unsigned b_len)
1962 isl_seq_cpy(blk.data, a+a_len, b_len);
1963 isl_seq_cpy(blk.data+b_len, a, a_len);
1964 isl_seq_cpy(a, blk.data, b_len+a_len);
1967 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1968 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1970 int i;
1971 struct isl_blk blk;
1973 if (!bmap)
1974 goto error;
1976 isl_assert(bmap->ctx,
1977 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1979 if (n1 == 0 || n2 == 0)
1980 return bmap;
1982 bmap = isl_basic_map_cow(bmap);
1983 if (!bmap)
1984 return NULL;
1986 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1987 if (isl_blk_is_error(blk))
1988 goto error;
1990 for (i = 0; i < bmap->n_eq; ++i)
1991 swap_vars(blk,
1992 bmap->eq[i] + pos, n1, n2);
1994 for (i = 0; i < bmap->n_ineq; ++i)
1995 swap_vars(blk,
1996 bmap->ineq[i] + pos, n1, n2);
1998 for (i = 0; i < bmap->n_div; ++i)
1999 swap_vars(blk,
2000 bmap->div[i]+1 + pos, n1, n2);
2002 isl_blk_free(bmap->ctx, blk);
2004 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2005 bmap = isl_basic_map_gauss(bmap, NULL);
2006 return isl_basic_map_finalize(bmap);
2007 error:
2008 isl_basic_map_free(bmap);
2009 return NULL;
2012 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2013 __isl_take isl_basic_map *bmap)
2015 int i = 0;
2016 unsigned total;
2017 if (!bmap)
2018 goto error;
2019 total = isl_basic_map_total_dim(bmap);
2020 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2021 return isl_basic_map_free(bmap);
2022 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2023 if (bmap->n_eq > 0)
2024 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2025 else {
2026 i = isl_basic_map_alloc_equality(bmap);
2027 if (i < 0)
2028 goto error;
2030 isl_int_set_si(bmap->eq[i][0], 1);
2031 isl_seq_clr(bmap->eq[i]+1, total);
2032 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2033 isl_vec_free(bmap->sample);
2034 bmap->sample = NULL;
2035 return isl_basic_map_finalize(bmap);
2036 error:
2037 isl_basic_map_free(bmap);
2038 return NULL;
2041 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2042 __isl_take isl_basic_set *bset)
2044 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2047 __isl_give isl_basic_map *isl_basic_map_set_rational(
2048 __isl_take isl_basic_map *bmap)
2050 if (!bmap)
2051 return NULL;
2053 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2054 return bmap;
2056 bmap = isl_basic_map_cow(bmap);
2057 if (!bmap)
2058 return NULL;
2060 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2062 return isl_basic_map_finalize(bmap);
2065 __isl_give isl_basic_set *isl_basic_set_set_rational(
2066 __isl_take isl_basic_set *bset)
2068 return isl_basic_map_set_rational(bset);
2071 __isl_give isl_basic_set *isl_basic_set_set_integral(
2072 __isl_take isl_basic_set *bset)
2074 if (!bset)
2075 return NULL;
2077 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2078 return bset;
2080 bset = isl_basic_set_cow(bset);
2081 if (!bset)
2082 return NULL;
2084 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2086 return isl_basic_set_finalize(bset);
2089 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2091 int i;
2093 map = isl_map_cow(map);
2094 if (!map)
2095 return NULL;
2096 for (i = 0; i < map->n; ++i) {
2097 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2098 if (!map->p[i])
2099 goto error;
2101 return map;
2102 error:
2103 isl_map_free(map);
2104 return NULL;
2107 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2109 return isl_map_set_rational(set);
2112 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2113 * of "bmap").
2115 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2117 isl_int *t = bmap->div[a];
2118 bmap->div[a] = bmap->div[b];
2119 bmap->div[b] = t;
2122 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2123 * div definitions accordingly.
2125 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2127 int i;
2128 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2130 swap_div(bmap, a, b);
2132 for (i = 0; i < bmap->n_eq; ++i)
2133 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2135 for (i = 0; i < bmap->n_ineq; ++i)
2136 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2138 for (i = 0; i < bmap->n_div; ++i)
2139 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2140 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2143 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2145 isl_seq_cpy(c, c + n, rem);
2146 isl_seq_clr(c + rem, n);
2149 /* Drop n dimensions starting at first.
2151 * In principle, this frees up some extra variables as the number
2152 * of columns remains constant, but we would have to extend
2153 * the div array too as the number of rows in this array is assumed
2154 * to be equal to extra.
2156 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2157 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2159 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2162 /* Move "n" divs starting at "first" to the end of the list of divs.
2164 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2165 unsigned first, unsigned n)
2167 isl_int **div;
2168 int i;
2170 if (first + n == bmap->n_div)
2171 return bmap;
2173 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2174 if (!div)
2175 goto error;
2176 for (i = 0; i < n; ++i)
2177 div[i] = bmap->div[first + i];
2178 for (i = 0; i < bmap->n_div - first - n; ++i)
2179 bmap->div[first + i] = bmap->div[first + n + i];
2180 for (i = 0; i < n; ++i)
2181 bmap->div[bmap->n_div - n + i] = div[i];
2182 free(div);
2183 return bmap;
2184 error:
2185 isl_basic_map_free(bmap);
2186 return NULL;
2189 /* Check that there are "n" dimensions of type "type" starting at "first"
2190 * in "map".
2192 static isl_stat isl_map_check_range(__isl_keep isl_map *map,
2193 enum isl_dim_type type, unsigned first, unsigned n)
2195 if (!map)
2196 return isl_stat_error;
2197 if (first + n > isl_map_dim(map, type) || first + n < first)
2198 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2199 "position or range out of bounds",
2200 return isl_stat_error);
2201 return isl_stat_ok;
2204 /* Drop "n" dimensions of type "type" starting at "first".
2206 * In principle, this frees up some extra variables as the number
2207 * of columns remains constant, but we would have to extend
2208 * the div array too as the number of rows in this array is assumed
2209 * to be equal to extra.
2211 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2212 enum isl_dim_type type, unsigned first, unsigned n)
2214 int i;
2215 unsigned dim;
2216 unsigned offset;
2217 unsigned left;
2219 if (!bmap)
2220 goto error;
2222 dim = isl_basic_map_dim(bmap, type);
2223 isl_assert(bmap->ctx, first + n <= dim, goto error);
2225 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2226 return bmap;
2228 bmap = isl_basic_map_cow(bmap);
2229 if (!bmap)
2230 return NULL;
2232 offset = isl_basic_map_offset(bmap, type) + first;
2233 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2234 for (i = 0; i < bmap->n_eq; ++i)
2235 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2237 for (i = 0; i < bmap->n_ineq; ++i)
2238 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2240 for (i = 0; i < bmap->n_div; ++i)
2241 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2243 if (type == isl_dim_div) {
2244 bmap = move_divs_last(bmap, first, n);
2245 if (!bmap)
2246 goto error;
2247 if (isl_basic_map_free_div(bmap, n) < 0)
2248 return isl_basic_map_free(bmap);
2249 } else
2250 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2251 if (!bmap->dim)
2252 goto error;
2254 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2255 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2256 bmap = isl_basic_map_simplify(bmap);
2257 return isl_basic_map_finalize(bmap);
2258 error:
2259 isl_basic_map_free(bmap);
2260 return NULL;
2263 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2264 enum isl_dim_type type, unsigned first, unsigned n)
2266 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2267 type, first, n));
2270 /* No longer consider "map" to be normalized.
2272 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2274 if (!map)
2275 return NULL;
2276 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2277 return map;
2280 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2281 enum isl_dim_type type, unsigned first, unsigned n)
2283 int i;
2285 if (isl_map_check_range(map, type, first, n) < 0)
2286 return isl_map_free(map);
2288 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2289 return map;
2290 map = isl_map_cow(map);
2291 if (!map)
2292 goto error;
2293 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2294 if (!map->dim)
2295 goto error;
2297 for (i = 0; i < map->n; ++i) {
2298 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2299 if (!map->p[i])
2300 goto error;
2302 map = isl_map_unmark_normalized(map);
2304 return map;
2305 error:
2306 isl_map_free(map);
2307 return NULL;
2310 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2311 enum isl_dim_type type, unsigned first, unsigned n)
2313 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2317 * We don't cow, as the div is assumed to be redundant.
2319 __isl_give isl_basic_map *isl_basic_map_drop_div(
2320 __isl_take isl_basic_map *bmap, unsigned div)
2322 int i;
2323 unsigned pos;
2325 if (!bmap)
2326 goto error;
2328 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2330 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
2332 for (i = 0; i < bmap->n_eq; ++i)
2333 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2335 for (i = 0; i < bmap->n_ineq; ++i) {
2336 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
2337 isl_basic_map_drop_inequality(bmap, i);
2338 --i;
2339 continue;
2341 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2344 for (i = 0; i < bmap->n_div; ++i)
2345 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2347 if (div != bmap->n_div - 1) {
2348 int j;
2349 isl_int *t = bmap->div[div];
2351 for (j = div; j < bmap->n_div - 1; ++j)
2352 bmap->div[j] = bmap->div[j+1];
2354 bmap->div[bmap->n_div - 1] = t;
2356 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2357 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2358 if (isl_basic_map_free_div(bmap, 1) < 0)
2359 return isl_basic_map_free(bmap);
2361 return bmap;
2362 error:
2363 isl_basic_map_free(bmap);
2364 return NULL;
2367 /* Eliminate the specified n dimensions starting at first from the
2368 * constraints, without removing the dimensions from the space.
2369 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2371 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2372 enum isl_dim_type type, unsigned first, unsigned n)
2374 int i;
2376 if (n == 0)
2377 return map;
2379 if (isl_map_check_range(map, type, first, n) < 0)
2380 return isl_map_free(map);
2382 map = isl_map_cow(map);
2383 if (!map)
2384 return NULL;
2386 for (i = 0; i < map->n; ++i) {
2387 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2388 if (!map->p[i])
2389 goto error;
2391 return map;
2392 error:
2393 isl_map_free(map);
2394 return NULL;
2397 /* Eliminate the specified n dimensions starting at first from the
2398 * constraints, without removing the dimensions from the space.
2399 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2401 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2402 enum isl_dim_type type, unsigned first, unsigned n)
2404 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2407 /* Eliminate the specified n dimensions starting at first from the
2408 * constraints, without removing the dimensions from the space.
2409 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2411 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2412 unsigned first, unsigned n)
2414 return isl_set_eliminate(set, isl_dim_set, first, n);
2417 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2418 __isl_take isl_basic_map *bmap)
2420 if (!bmap)
2421 return NULL;
2422 bmap = isl_basic_map_eliminate_vars(bmap,
2423 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2424 if (!bmap)
2425 return NULL;
2426 bmap->n_div = 0;
2427 return isl_basic_map_finalize(bmap);
2430 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2431 __isl_take isl_basic_set *bset)
2433 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2436 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2438 int i;
2440 if (!map)
2441 return NULL;
2442 if (map->n == 0)
2443 return map;
2445 map = isl_map_cow(map);
2446 if (!map)
2447 return NULL;
2449 for (i = 0; i < map->n; ++i) {
2450 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2451 if (!map->p[i])
2452 goto error;
2454 return map;
2455 error:
2456 isl_map_free(map);
2457 return NULL;
2460 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2462 return isl_map_remove_divs(set);
2465 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2466 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2467 unsigned first, unsigned n)
2469 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2470 return isl_basic_map_free(bmap);
2471 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2472 return bmap;
2473 bmap = isl_basic_map_eliminate_vars(bmap,
2474 isl_basic_map_offset(bmap, type) - 1 + first, n);
2475 if (!bmap)
2476 return bmap;
2477 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2478 return bmap;
2479 bmap = isl_basic_map_drop(bmap, type, first, n);
2480 return bmap;
2483 /* Return true if the definition of the given div (recursively) involves
2484 * any of the given variables.
2486 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2487 unsigned first, unsigned n)
2489 int i;
2490 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2492 if (isl_int_is_zero(bmap->div[div][0]))
2493 return isl_bool_false;
2494 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2495 return isl_bool_true;
2497 for (i = bmap->n_div - 1; i >= 0; --i) {
2498 isl_bool involves;
2500 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2501 continue;
2502 involves = div_involves_vars(bmap, i, first, n);
2503 if (involves < 0 || involves)
2504 return involves;
2507 return isl_bool_false;
2510 /* Try and add a lower and/or upper bound on "div" to "bmap"
2511 * based on inequality "i".
2512 * "total" is the total number of variables (excluding the divs).
2513 * "v" is a temporary object that can be used during the calculations.
2514 * If "lb" is set, then a lower bound should be constructed.
2515 * If "ub" is set, then an upper bound should be constructed.
2517 * The calling function has already checked that the inequality does not
2518 * reference "div", but we still need to check that the inequality is
2519 * of the right form. We'll consider the case where we want to construct
2520 * a lower bound. The construction of upper bounds is similar.
2522 * Let "div" be of the form
2524 * q = floor((a + f(x))/d)
2526 * We essentially check if constraint "i" is of the form
2528 * b + f(x) >= 0
2530 * so that we can use it to derive a lower bound on "div".
2531 * However, we allow a slightly more general form
2533 * b + g(x) >= 0
2535 * with the condition that the coefficients of g(x) - f(x) are all
2536 * divisible by d.
2537 * Rewriting this constraint as
2539 * 0 >= -b - g(x)
2541 * adding a + f(x) to both sides and dividing by d, we obtain
2543 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2545 * Taking the floor on both sides, we obtain
2547 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2549 * or
2551 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2553 * In the case of an upper bound, we construct the constraint
2555 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2558 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2559 __isl_take isl_basic_map *bmap, int div, int i,
2560 unsigned total, isl_int v, int lb, int ub)
2562 int j;
2564 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2565 if (lb) {
2566 isl_int_sub(v, bmap->ineq[i][1 + j],
2567 bmap->div[div][1 + 1 + j]);
2568 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2570 if (ub) {
2571 isl_int_add(v, bmap->ineq[i][1 + j],
2572 bmap->div[div][1 + 1 + j]);
2573 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2576 if (!lb && !ub)
2577 return bmap;
2579 bmap = isl_basic_map_cow(bmap);
2580 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2581 if (lb) {
2582 int k = isl_basic_map_alloc_inequality(bmap);
2583 if (k < 0)
2584 goto error;
2585 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2586 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2587 bmap->div[div][1 + j]);
2588 isl_int_cdiv_q(bmap->ineq[k][j],
2589 bmap->ineq[k][j], bmap->div[div][0]);
2591 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2593 if (ub) {
2594 int k = isl_basic_map_alloc_inequality(bmap);
2595 if (k < 0)
2596 goto error;
2597 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2598 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2599 bmap->div[div][1 + j]);
2600 isl_int_fdiv_q(bmap->ineq[k][j],
2601 bmap->ineq[k][j], bmap->div[div][0]);
2603 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2606 return bmap;
2607 error:
2608 isl_basic_map_free(bmap);
2609 return NULL;
2612 /* This function is called right before "div" is eliminated from "bmap"
2613 * using Fourier-Motzkin.
2614 * Look through the constraints of "bmap" for constraints on the argument
2615 * of the integer division and use them to construct constraints on the
2616 * integer division itself. These constraints can then be combined
2617 * during the Fourier-Motzkin elimination.
2618 * Note that it is only useful to introduce lower bounds on "div"
2619 * if "bmap" already contains upper bounds on "div" as the newly
2620 * introduce lower bounds can then be combined with the pre-existing
2621 * upper bounds. Similarly for upper bounds.
2622 * We therefore first check if "bmap" contains any lower and/or upper bounds
2623 * on "div".
2625 * It is interesting to note that the introduction of these constraints
2626 * can indeed lead to more accurate results, even when compared to
2627 * deriving constraints on the argument of "div" from constraints on "div".
2628 * Consider, for example, the set
2630 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2632 * The second constraint can be rewritten as
2634 * 2 * [(-i-2j+3)/4] + k >= 0
2636 * from which we can derive
2638 * -i - 2j + 3 >= -2k
2640 * or
2642 * i + 2j <= 3 + 2k
2644 * Combined with the first constraint, we obtain
2646 * -3 <= 3 + 2k or k >= -3
2648 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2649 * the first constraint, we obtain
2651 * [(i + 2j)/4] >= [-3/4] = -1
2653 * Combining this constraint with the second constraint, we obtain
2655 * k >= -2
2657 static __isl_give isl_basic_map *insert_bounds_on_div(
2658 __isl_take isl_basic_map *bmap, int div)
2660 int i;
2661 int check_lb, check_ub;
2662 isl_int v;
2663 unsigned total;
2665 if (!bmap)
2666 return NULL;
2668 if (isl_int_is_zero(bmap->div[div][0]))
2669 return bmap;
2671 total = isl_space_dim(bmap->dim, isl_dim_all);
2673 check_lb = 0;
2674 check_ub = 0;
2675 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2676 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2677 if (s > 0)
2678 check_ub = 1;
2679 if (s < 0)
2680 check_lb = 1;
2683 if (!check_lb && !check_ub)
2684 return bmap;
2686 isl_int_init(v);
2688 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2689 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2690 continue;
2692 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2693 check_lb, check_ub);
2696 isl_int_clear(v);
2698 return bmap;
2701 /* Remove all divs (recursively) involving any of the given dimensions
2702 * in their definitions.
2704 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2705 __isl_take isl_basic_map *bmap,
2706 enum isl_dim_type type, unsigned first, unsigned n)
2708 int i;
2710 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2711 return isl_basic_map_free(bmap);
2712 first += isl_basic_map_offset(bmap, type);
2714 for (i = bmap->n_div - 1; i >= 0; --i) {
2715 isl_bool involves;
2717 involves = div_involves_vars(bmap, i, first, n);
2718 if (involves < 0)
2719 return isl_basic_map_free(bmap);
2720 if (!involves)
2721 continue;
2722 bmap = insert_bounds_on_div(bmap, i);
2723 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2724 if (!bmap)
2725 return NULL;
2726 i = bmap->n_div;
2729 return bmap;
2732 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2733 __isl_take isl_basic_set *bset,
2734 enum isl_dim_type type, unsigned first, unsigned n)
2736 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2739 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2740 enum isl_dim_type type, unsigned first, unsigned n)
2742 int i;
2744 if (!map)
2745 return NULL;
2746 if (map->n == 0)
2747 return map;
2749 map = isl_map_cow(map);
2750 if (!map)
2751 return NULL;
2753 for (i = 0; i < map->n; ++i) {
2754 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2755 type, first, n);
2756 if (!map->p[i])
2757 goto error;
2759 return map;
2760 error:
2761 isl_map_free(map);
2762 return NULL;
2765 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2766 enum isl_dim_type type, unsigned first, unsigned n)
2768 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2769 type, first, n));
2772 /* Does the description of "bmap" depend on the specified dimensions?
2773 * We also check whether the dimensions appear in any of the div definitions.
2774 * In principle there is no need for this check. If the dimensions appear
2775 * in a div definition, they also appear in the defining constraints of that
2776 * div.
2778 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2779 enum isl_dim_type type, unsigned first, unsigned n)
2781 int i;
2783 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2784 return isl_bool_error;
2786 first += isl_basic_map_offset(bmap, type);
2787 for (i = 0; i < bmap->n_eq; ++i)
2788 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2789 return isl_bool_true;
2790 for (i = 0; i < bmap->n_ineq; ++i)
2791 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2792 return isl_bool_true;
2793 for (i = 0; i < bmap->n_div; ++i) {
2794 if (isl_int_is_zero(bmap->div[i][0]))
2795 continue;
2796 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2797 return isl_bool_true;
2800 return isl_bool_false;
2803 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2804 enum isl_dim_type type, unsigned first, unsigned n)
2806 int i;
2808 if (isl_map_check_range(map, type, first, n) < 0)
2809 return isl_bool_error;
2811 for (i = 0; i < map->n; ++i) {
2812 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2813 type, first, n);
2814 if (involves < 0 || involves)
2815 return involves;
2818 return isl_bool_false;
2821 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2822 enum isl_dim_type type, unsigned first, unsigned n)
2824 return isl_basic_map_involves_dims(bset, type, first, n);
2827 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2828 enum isl_dim_type type, unsigned first, unsigned n)
2830 return isl_map_involves_dims(set, type, first, n);
2833 /* Drop all constraints in bmap that involve any of the dimensions
2834 * first to first+n-1.
2836 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2837 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2839 int i;
2841 if (n == 0)
2842 return bmap;
2844 bmap = isl_basic_map_cow(bmap);
2846 if (!bmap)
2847 return NULL;
2849 for (i = bmap->n_eq - 1; i >= 0; --i) {
2850 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2851 continue;
2852 isl_basic_map_drop_equality(bmap, i);
2855 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2856 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2857 continue;
2858 isl_basic_map_drop_inequality(bmap, i);
2861 bmap = isl_basic_map_add_known_div_constraints(bmap);
2862 return bmap;
2865 /* Drop all constraints in bset that involve any of the dimensions
2866 * first to first+n-1.
2868 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2869 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2871 return isl_basic_map_drop_constraints_involving(bset, first, n);
2874 /* Drop all constraints in bmap that do not involve any of the dimensions
2875 * first to first + n - 1 of the given type.
2877 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2878 __isl_take isl_basic_map *bmap,
2879 enum isl_dim_type type, unsigned first, unsigned n)
2881 int i;
2883 if (n == 0) {
2884 isl_space *space = isl_basic_map_get_space(bmap);
2885 isl_basic_map_free(bmap);
2886 return isl_basic_map_universe(space);
2888 bmap = isl_basic_map_cow(bmap);
2889 if (!bmap)
2890 return NULL;
2892 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2893 return isl_basic_map_free(bmap);
2895 first += isl_basic_map_offset(bmap, type) - 1;
2897 for (i = bmap->n_eq - 1; i >= 0; --i) {
2898 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2899 continue;
2900 isl_basic_map_drop_equality(bmap, i);
2903 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2904 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2905 continue;
2906 isl_basic_map_drop_inequality(bmap, i);
2909 bmap = isl_basic_map_add_known_div_constraints(bmap);
2910 return bmap;
2913 /* Drop all constraints in bset that do not involve any of the dimensions
2914 * first to first + n - 1 of the given type.
2916 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2917 __isl_take isl_basic_set *bset,
2918 enum isl_dim_type type, unsigned first, unsigned n)
2920 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2921 type, first, n);
2924 /* Drop all constraints in bmap that involve any of the dimensions
2925 * first to first + n - 1 of the given type.
2927 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2928 __isl_take isl_basic_map *bmap,
2929 enum isl_dim_type type, unsigned first, unsigned n)
2931 if (!bmap)
2932 return NULL;
2933 if (n == 0)
2934 return bmap;
2936 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2937 return isl_basic_map_free(bmap);
2939 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2940 first += isl_basic_map_offset(bmap, type) - 1;
2941 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2944 /* Drop all constraints in bset that involve any of the dimensions
2945 * first to first + n - 1 of the given type.
2947 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2948 __isl_take isl_basic_set *bset,
2949 enum isl_dim_type type, unsigned first, unsigned n)
2951 return isl_basic_map_drop_constraints_involving_dims(bset,
2952 type, first, n);
2955 /* Drop constraints from "map" by applying "drop" to each basic map.
2957 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2958 enum isl_dim_type type, unsigned first, unsigned n,
2959 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2960 enum isl_dim_type type, unsigned first, unsigned n))
2962 int i;
2964 if (isl_map_check_range(map, type, first, n) < 0)
2965 return isl_map_free(map);
2967 map = isl_map_cow(map);
2968 if (!map)
2969 return NULL;
2971 for (i = 0; i < map->n; ++i) {
2972 map->p[i] = drop(map->p[i], type, first, n);
2973 if (!map->p[i])
2974 return isl_map_free(map);
2977 if (map->n > 1)
2978 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2980 return map;
2983 /* Drop all constraints in map that involve any of the dimensions
2984 * first to first + n - 1 of the given type.
2986 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2987 __isl_take isl_map *map,
2988 enum isl_dim_type type, unsigned first, unsigned n)
2990 if (n == 0)
2991 return map;
2992 return drop_constraints(map, type, first, n,
2993 &isl_basic_map_drop_constraints_involving_dims);
2996 /* Drop all constraints in "map" that do not involve any of the dimensions
2997 * first to first + n - 1 of the given type.
2999 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3000 __isl_take isl_map *map,
3001 enum isl_dim_type type, unsigned first, unsigned n)
3003 if (n == 0) {
3004 isl_space *space = isl_map_get_space(map);
3005 isl_map_free(map);
3006 return isl_map_universe(space);
3008 return drop_constraints(map, type, first, n,
3009 &isl_basic_map_drop_constraints_not_involving_dims);
3012 /* Drop all constraints in set that involve any of the dimensions
3013 * first to first + n - 1 of the given type.
3015 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3016 __isl_take isl_set *set,
3017 enum isl_dim_type type, unsigned first, unsigned n)
3019 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3022 /* Drop all constraints in "set" that do not involve any of the dimensions
3023 * first to first + n - 1 of the given type.
3025 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3026 __isl_take isl_set *set,
3027 enum isl_dim_type type, unsigned first, unsigned n)
3029 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3032 /* Does local variable "div" of "bmap" have a complete explicit representation?
3033 * Having a complete explicit representation requires not only
3034 * an explicit representation, but also that all local variables
3035 * that appear in this explicit representation in turn have
3036 * a complete explicit representation.
3038 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3040 int i;
3041 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3042 isl_bool marked;
3044 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3045 if (marked < 0 || marked)
3046 return isl_bool_not(marked);
3048 for (i = bmap->n_div - 1; i >= 0; --i) {
3049 isl_bool known;
3051 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3052 continue;
3053 known = isl_basic_map_div_is_known(bmap, i);
3054 if (known < 0 || !known)
3055 return known;
3058 return isl_bool_true;
3061 /* Remove all divs that are unknown or defined in terms of unknown divs.
3063 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3064 __isl_take isl_basic_map *bmap)
3066 int i;
3068 if (!bmap)
3069 return NULL;
3071 for (i = bmap->n_div - 1; i >= 0; --i) {
3072 if (isl_basic_map_div_is_known(bmap, i))
3073 continue;
3074 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3075 if (!bmap)
3076 return NULL;
3077 i = bmap->n_div;
3080 return bmap;
3083 /* Remove all divs that are unknown or defined in terms of unknown divs.
3085 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3086 __isl_take isl_basic_set *bset)
3088 return isl_basic_map_remove_unknown_divs(bset);
3091 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3093 int i;
3095 if (!map)
3096 return NULL;
3097 if (map->n == 0)
3098 return map;
3100 map = isl_map_cow(map);
3101 if (!map)
3102 return NULL;
3104 for (i = 0; i < map->n; ++i) {
3105 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3106 if (!map->p[i])
3107 goto error;
3109 return map;
3110 error:
3111 isl_map_free(map);
3112 return NULL;
3115 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3117 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3120 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3121 __isl_take isl_basic_set *bset,
3122 enum isl_dim_type type, unsigned first, unsigned n)
3124 isl_basic_map *bmap = bset_to_bmap(bset);
3125 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3126 return bset_from_bmap(bmap);
3129 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3130 enum isl_dim_type type, unsigned first, unsigned n)
3132 int i;
3134 if (n == 0)
3135 return map;
3137 map = isl_map_cow(map);
3138 if (isl_map_check_range(map, type, first, n) < 0)
3139 return isl_map_free(map);
3141 for (i = 0; i < map->n; ++i) {
3142 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3143 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3144 if (!map->p[i])
3145 goto error;
3147 map = isl_map_drop(map, type, first, n);
3148 return map;
3149 error:
3150 isl_map_free(map);
3151 return NULL;
3154 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3155 enum isl_dim_type type, unsigned first, unsigned n)
3157 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3158 type, first, n));
3161 /* Project out n inputs starting at first using Fourier-Motzkin */
3162 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3163 unsigned first, unsigned n)
3165 return isl_map_remove_dims(map, isl_dim_in, first, n);
3168 static void dump_term(struct isl_basic_map *bmap,
3169 isl_int c, int pos, FILE *out)
3171 const char *name;
3172 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3173 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3174 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3175 if (!pos)
3176 isl_int_print(out, c, 0);
3177 else {
3178 if (!isl_int_is_one(c))
3179 isl_int_print(out, c, 0);
3180 if (pos < 1 + nparam) {
3181 name = isl_space_get_dim_name(bmap->dim,
3182 isl_dim_param, pos - 1);
3183 if (name)
3184 fprintf(out, "%s", name);
3185 else
3186 fprintf(out, "p%d", pos - 1);
3187 } else if (pos < 1 + nparam + in)
3188 fprintf(out, "i%d", pos - 1 - nparam);
3189 else if (pos < 1 + nparam + dim)
3190 fprintf(out, "o%d", pos - 1 - nparam - in);
3191 else
3192 fprintf(out, "e%d", pos - 1 - nparam - dim);
3196 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3197 int sign, FILE *out)
3199 int i;
3200 int first;
3201 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3202 isl_int v;
3204 isl_int_init(v);
3205 for (i = 0, first = 1; i < len; ++i) {
3206 if (isl_int_sgn(c[i]) * sign <= 0)
3207 continue;
3208 if (!first)
3209 fprintf(out, " + ");
3210 first = 0;
3211 isl_int_abs(v, c[i]);
3212 dump_term(bmap, v, i, out);
3214 isl_int_clear(v);
3215 if (first)
3216 fprintf(out, "0");
3219 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3220 const char *op, FILE *out, int indent)
3222 int i;
3224 fprintf(out, "%*s", indent, "");
3226 dump_constraint_sign(bmap, c, 1, out);
3227 fprintf(out, " %s ", op);
3228 dump_constraint_sign(bmap, c, -1, out);
3230 fprintf(out, "\n");
3232 for (i = bmap->n_div; i < bmap->extra; ++i) {
3233 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3234 continue;
3235 fprintf(out, "%*s", indent, "");
3236 fprintf(out, "ERROR: unused div coefficient not zero\n");
3237 abort();
3241 static void dump_constraints(struct isl_basic_map *bmap,
3242 isl_int **c, unsigned n,
3243 const char *op, FILE *out, int indent)
3245 int i;
3247 for (i = 0; i < n; ++i)
3248 dump_constraint(bmap, c[i], op, out, indent);
3251 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3253 int j;
3254 int first = 1;
3255 unsigned total = isl_basic_map_total_dim(bmap);
3257 for (j = 0; j < 1 + total; ++j) {
3258 if (isl_int_is_zero(exp[j]))
3259 continue;
3260 if (!first && isl_int_is_pos(exp[j]))
3261 fprintf(out, "+");
3262 dump_term(bmap, exp[j], j, out);
3263 first = 0;
3267 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3269 int i;
3271 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3272 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3274 for (i = 0; i < bmap->n_div; ++i) {
3275 fprintf(out, "%*s", indent, "");
3276 fprintf(out, "e%d = [(", i);
3277 dump_affine(bmap, bmap->div[i]+1, out);
3278 fprintf(out, ")/");
3279 isl_int_print(out, bmap->div[i][0], 0);
3280 fprintf(out, "]\n");
3284 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3285 FILE *out, int indent)
3287 if (!bset) {
3288 fprintf(out, "null basic set\n");
3289 return;
3292 fprintf(out, "%*s", indent, "");
3293 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3294 bset->ref, bset->dim->nparam, bset->dim->n_out,
3295 bset->extra, bset->flags);
3296 dump(bset_to_bmap(bset), out, indent);
3299 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3300 FILE *out, int indent)
3302 if (!bmap) {
3303 fprintf(out, "null basic map\n");
3304 return;
3307 fprintf(out, "%*s", indent, "");
3308 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3309 "flags: %x, n_name: %d\n",
3310 bmap->ref,
3311 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3312 bmap->extra, bmap->flags, bmap->dim->n_id);
3313 dump(bmap, out, indent);
3316 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3318 unsigned total;
3319 if (!bmap)
3320 return -1;
3321 total = isl_basic_map_total_dim(bmap);
3322 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3323 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3324 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3325 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3326 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3327 return 0;
3330 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3331 unsigned flags)
3333 if (isl_space_check_is_set(space) < 0)
3334 goto error;
3335 return isl_map_alloc_space(space, n, flags);
3336 error:
3337 isl_space_free(space);
3338 return NULL;
3341 /* Make sure "map" has room for at least "n" more basic maps.
3343 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3345 int i;
3346 struct isl_map *grown = NULL;
3348 if (!map)
3349 return NULL;
3350 isl_assert(map->ctx, n >= 0, goto error);
3351 if (map->n + n <= map->size)
3352 return map;
3353 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3354 if (!grown)
3355 goto error;
3356 for (i = 0; i < map->n; ++i) {
3357 grown->p[i] = isl_basic_map_copy(map->p[i]);
3358 if (!grown->p[i])
3359 goto error;
3360 grown->n++;
3362 isl_map_free(map);
3363 return grown;
3364 error:
3365 isl_map_free(grown);
3366 isl_map_free(map);
3367 return NULL;
3370 /* Make sure "set" has room for at least "n" more basic sets.
3372 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3374 return set_from_map(isl_map_grow(set_to_map(set), n));
3377 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3379 return isl_map_from_basic_map(bset);
3382 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3384 struct isl_map *map;
3386 if (!bmap)
3387 return NULL;
3389 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3390 return isl_map_add_basic_map(map, bmap);
3393 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3394 __isl_take isl_basic_set *bset)
3396 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3397 bset_to_bmap(bset)));
3400 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3402 return isl_map_free(set);
3405 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3407 int i;
3409 if (!set) {
3410 fprintf(out, "null set\n");
3411 return;
3414 fprintf(out, "%*s", indent, "");
3415 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3416 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3417 set->flags);
3418 for (i = 0; i < set->n; ++i) {
3419 fprintf(out, "%*s", indent, "");
3420 fprintf(out, "basic set %d:\n", i);
3421 isl_basic_set_print_internal(set->p[i], out, indent+4);
3425 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3427 int i;
3429 if (!map) {
3430 fprintf(out, "null map\n");
3431 return;
3434 fprintf(out, "%*s", indent, "");
3435 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3436 "flags: %x, n_name: %d\n",
3437 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3438 map->dim->n_out, map->flags, map->dim->n_id);
3439 for (i = 0; i < map->n; ++i) {
3440 fprintf(out, "%*s", indent, "");
3441 fprintf(out, "basic map %d:\n", i);
3442 isl_basic_map_print_internal(map->p[i], out, indent+4);
3446 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3447 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3449 struct isl_basic_map *bmap_domain;
3451 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3452 goto error;
3454 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3455 isl_assert(bset->ctx,
3456 isl_basic_map_compatible_domain(bmap, bset), goto error);
3458 bmap = isl_basic_map_cow(bmap);
3459 if (!bmap)
3460 goto error;
3461 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3462 bset->n_div, bset->n_eq, bset->n_ineq);
3463 bmap_domain = isl_basic_map_from_domain(bset);
3464 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3466 bmap = isl_basic_map_simplify(bmap);
3467 return isl_basic_map_finalize(bmap);
3468 error:
3469 isl_basic_map_free(bmap);
3470 isl_basic_set_free(bset);
3471 return NULL;
3474 /* Check that the space of "bset" is the same as that of the range of "bmap".
3476 static isl_stat isl_basic_map_check_compatible_range(
3477 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3479 isl_bool ok;
3481 ok = isl_basic_map_compatible_range(bmap, bset);
3482 if (ok < 0)
3483 return isl_stat_error;
3484 if (!ok)
3485 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3486 "incompatible spaces", return isl_stat_error);
3488 return isl_stat_ok;
3491 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3492 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3494 struct isl_basic_map *bmap_range;
3496 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3497 goto error;
3499 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3500 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3501 goto error;
3503 if (isl_basic_set_plain_is_universe(bset)) {
3504 isl_basic_set_free(bset);
3505 return bmap;
3508 bmap = isl_basic_map_cow(bmap);
3509 if (!bmap)
3510 goto error;
3511 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3512 bset->n_div, bset->n_eq, bset->n_ineq);
3513 bmap_range = bset_to_bmap(bset);
3514 bmap = add_constraints(bmap, bmap_range, 0, 0);
3516 bmap = isl_basic_map_simplify(bmap);
3517 return isl_basic_map_finalize(bmap);
3518 error:
3519 isl_basic_map_free(bmap);
3520 isl_basic_set_free(bset);
3521 return NULL;
3524 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3525 __isl_keep isl_vec *vec)
3527 int i;
3528 unsigned total;
3529 isl_int s;
3531 if (!bmap || !vec)
3532 return isl_bool_error;
3534 total = 1 + isl_basic_map_total_dim(bmap);
3535 if (total != vec->size)
3536 return isl_bool_false;
3538 isl_int_init(s);
3540 for (i = 0; i < bmap->n_eq; ++i) {
3541 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3542 if (!isl_int_is_zero(s)) {
3543 isl_int_clear(s);
3544 return isl_bool_false;
3548 for (i = 0; i < bmap->n_ineq; ++i) {
3549 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3550 if (isl_int_is_neg(s)) {
3551 isl_int_clear(s);
3552 return isl_bool_false;
3556 isl_int_clear(s);
3558 return isl_bool_true;
3561 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3562 __isl_keep isl_vec *vec)
3564 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3567 __isl_give isl_basic_map *isl_basic_map_intersect(
3568 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3570 struct isl_vec *sample = NULL;
3572 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3573 goto error;
3574 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3575 isl_space_dim(bmap1->dim, isl_dim_param) &&
3576 isl_space_dim(bmap2->dim, isl_dim_all) !=
3577 isl_space_dim(bmap2->dim, isl_dim_param))
3578 return isl_basic_map_intersect(bmap2, bmap1);
3580 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3581 isl_space_dim(bmap2->dim, isl_dim_param))
3582 isl_assert(bmap1->ctx,
3583 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3585 if (isl_basic_map_plain_is_empty(bmap1)) {
3586 isl_basic_map_free(bmap2);
3587 return bmap1;
3589 if (isl_basic_map_plain_is_empty(bmap2)) {
3590 isl_basic_map_free(bmap1);
3591 return bmap2;
3594 if (bmap1->sample &&
3595 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3596 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3597 sample = isl_vec_copy(bmap1->sample);
3598 else if (bmap2->sample &&
3599 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3600 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3601 sample = isl_vec_copy(bmap2->sample);
3603 bmap1 = isl_basic_map_cow(bmap1);
3604 if (!bmap1)
3605 goto error;
3606 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3607 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3608 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3610 if (!bmap1)
3611 isl_vec_free(sample);
3612 else if (sample) {
3613 isl_vec_free(bmap1->sample);
3614 bmap1->sample = sample;
3617 bmap1 = isl_basic_map_simplify(bmap1);
3618 return isl_basic_map_finalize(bmap1);
3619 error:
3620 if (sample)
3621 isl_vec_free(sample);
3622 isl_basic_map_free(bmap1);
3623 isl_basic_map_free(bmap2);
3624 return NULL;
3627 struct isl_basic_set *isl_basic_set_intersect(
3628 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3630 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3631 bset_to_bmap(bset2)));
3634 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3635 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3637 return isl_basic_set_intersect(bset1, bset2);
3640 /* Special case of isl_map_intersect, where both map1 and map2
3641 * are convex, without any divs and such that either map1 or map2
3642 * contains a single constraint. This constraint is then simply
3643 * added to the other map.
3645 static __isl_give isl_map *map_intersect_add_constraint(
3646 __isl_take isl_map *map1, __isl_take isl_map *map2)
3648 isl_assert(map1->ctx, map1->n == 1, goto error);
3649 isl_assert(map2->ctx, map1->n == 1, goto error);
3650 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3651 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3653 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3654 return isl_map_intersect(map2, map1);
3656 map1 = isl_map_cow(map1);
3657 if (!map1)
3658 goto error;
3659 if (isl_map_plain_is_empty(map1)) {
3660 isl_map_free(map2);
3661 return map1;
3663 if (map2->p[0]->n_eq == 1)
3664 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3665 else
3666 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3667 map2->p[0]->ineq[0]);
3669 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3670 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3671 if (!map1->p[0])
3672 goto error;
3674 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3675 isl_basic_map_free(map1->p[0]);
3676 map1->n = 0;
3679 isl_map_free(map2);
3681 map1 = isl_map_unmark_normalized(map1);
3682 return map1;
3683 error:
3684 isl_map_free(map1);
3685 isl_map_free(map2);
3686 return NULL;
3689 /* map2 may be either a parameter domain or a map living in the same
3690 * space as map1.
3692 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3693 __isl_take isl_map *map2)
3695 unsigned flags = 0;
3696 isl_bool equal;
3697 isl_map *result;
3698 int i, j;
3700 if (!map1 || !map2)
3701 goto error;
3703 if ((isl_map_plain_is_empty(map1) ||
3704 isl_map_plain_is_universe(map2)) &&
3705 isl_space_is_equal(map1->dim, map2->dim)) {
3706 isl_map_free(map2);
3707 return map1;
3709 if ((isl_map_plain_is_empty(map2) ||
3710 isl_map_plain_is_universe(map1)) &&
3711 isl_space_is_equal(map1->dim, map2->dim)) {
3712 isl_map_free(map1);
3713 return map2;
3716 if (map1->n == 1 && map2->n == 1 &&
3717 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3718 isl_space_is_equal(map1->dim, map2->dim) &&
3719 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3720 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3721 return map_intersect_add_constraint(map1, map2);
3723 equal = isl_map_plain_is_equal(map1, map2);
3724 if (equal < 0)
3725 goto error;
3726 if (equal) {
3727 isl_map_free(map2);
3728 return map1;
3731 if (isl_space_dim(map2->dim, isl_dim_all) !=
3732 isl_space_dim(map2->dim, isl_dim_param))
3733 isl_assert(map1->ctx,
3734 isl_space_is_equal(map1->dim, map2->dim), goto error);
3736 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3737 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3738 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3740 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3741 map1->n * map2->n, flags);
3742 if (!result)
3743 goto error;
3744 for (i = 0; i < map1->n; ++i)
3745 for (j = 0; j < map2->n; ++j) {
3746 struct isl_basic_map *part;
3747 part = isl_basic_map_intersect(
3748 isl_basic_map_copy(map1->p[i]),
3749 isl_basic_map_copy(map2->p[j]));
3750 if (isl_basic_map_is_empty(part) < 0)
3751 part = isl_basic_map_free(part);
3752 result = isl_map_add_basic_map(result, part);
3753 if (!result)
3754 goto error;
3756 isl_map_free(map1);
3757 isl_map_free(map2);
3758 return result;
3759 error:
3760 isl_map_free(map1);
3761 isl_map_free(map2);
3762 return NULL;
3765 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3766 __isl_take isl_map *map2)
3768 if (!map1 || !map2)
3769 goto error;
3770 if (!isl_space_is_equal(map1->dim, map2->dim))
3771 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3772 "spaces don't match", goto error);
3773 return map_intersect_internal(map1, map2);
3774 error:
3775 isl_map_free(map1);
3776 isl_map_free(map2);
3777 return NULL;
3780 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3781 __isl_take isl_map *map2)
3783 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3786 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3788 return set_from_map(isl_map_intersect(set_to_map(set1),
3789 set_to_map(set2)));
3792 /* map_intersect_internal accepts intersections
3793 * with parameter domains, so we can just call that function.
3795 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3796 __isl_take isl_set *params)
3798 return map_intersect_internal(map, params);
3801 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3802 __isl_take isl_map *map2)
3804 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3807 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3808 __isl_take isl_set *params)
3810 return isl_map_intersect_params(set, params);
3813 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3815 isl_space *space;
3816 unsigned pos, n1, n2;
3818 if (!bmap)
3819 return NULL;
3820 bmap = isl_basic_map_cow(bmap);
3821 if (!bmap)
3822 return NULL;
3823 space = isl_space_reverse(isl_space_copy(bmap->dim));
3824 pos = isl_basic_map_offset(bmap, isl_dim_in);
3825 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3826 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3827 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3828 return isl_basic_map_reset_space(bmap, space);
3831 static __isl_give isl_basic_map *basic_map_space_reset(
3832 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3834 isl_space *space;
3836 if (!bmap)
3837 return NULL;
3838 if (!isl_space_is_named_or_nested(bmap->dim, type))
3839 return bmap;
3841 space = isl_basic_map_get_space(bmap);
3842 space = isl_space_reset(space, type);
3843 bmap = isl_basic_map_reset_space(bmap, space);
3844 return bmap;
3847 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3848 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3849 unsigned pos, unsigned n)
3851 isl_bool rational;
3852 isl_space *res_space;
3853 struct isl_basic_map *res;
3854 struct isl_dim_map *dim_map;
3855 unsigned total, off;
3856 enum isl_dim_type t;
3858 if (n == 0)
3859 return basic_map_space_reset(bmap, type);
3861 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3862 type, pos, n);
3863 if (!res_space)
3864 return isl_basic_map_free(bmap);
3866 total = isl_basic_map_total_dim(bmap) + n;
3867 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3868 off = 0;
3869 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3870 if (t != type) {
3871 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3872 } else {
3873 unsigned size = isl_basic_map_dim(bmap, t);
3874 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3875 0, pos, off);
3876 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3877 pos, size - pos, off + pos + n);
3879 off += isl_space_dim(res_space, t);
3881 isl_dim_map_div(dim_map, bmap, off);
3883 res = isl_basic_map_alloc_space(res_space,
3884 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3885 rational = isl_basic_map_is_rational(bmap);
3886 if (rational < 0)
3887 res = isl_basic_map_free(res);
3888 if (rational)
3889 res = isl_basic_map_set_rational(res);
3890 if (isl_basic_map_plain_is_empty(bmap)) {
3891 isl_basic_map_free(bmap);
3892 free(dim_map);
3893 return isl_basic_map_set_to_empty(res);
3895 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3896 return isl_basic_map_finalize(res);
3899 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3900 __isl_take isl_basic_set *bset,
3901 enum isl_dim_type type, unsigned pos, unsigned n)
3903 return isl_basic_map_insert_dims(bset, type, pos, n);
3906 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3907 enum isl_dim_type type, unsigned n)
3909 if (!bmap)
3910 return NULL;
3911 return isl_basic_map_insert_dims(bmap, type,
3912 isl_basic_map_dim(bmap, type), n);
3915 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3916 enum isl_dim_type type, unsigned n)
3918 if (!bset)
3919 return NULL;
3920 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3921 return isl_basic_map_add_dims(bset, type, n);
3922 error:
3923 isl_basic_set_free(bset);
3924 return NULL;
3927 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3928 enum isl_dim_type type)
3930 isl_space *space;
3932 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3933 return map;
3935 space = isl_map_get_space(map);
3936 space = isl_space_reset(space, type);
3937 map = isl_map_reset_space(map, space);
3938 return map;
3941 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3942 enum isl_dim_type type, unsigned pos, unsigned n)
3944 int i;
3946 if (n == 0)
3947 return map_space_reset(map, type);
3949 map = isl_map_cow(map);
3950 if (!map)
3951 return NULL;
3953 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3954 if (!map->dim)
3955 goto error;
3957 for (i = 0; i < map->n; ++i) {
3958 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3959 if (!map->p[i])
3960 goto error;
3963 return map;
3964 error:
3965 isl_map_free(map);
3966 return NULL;
3969 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3970 enum isl_dim_type type, unsigned pos, unsigned n)
3972 return isl_map_insert_dims(set, type, pos, n);
3975 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3976 enum isl_dim_type type, unsigned n)
3978 if (!map)
3979 return NULL;
3980 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3983 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3984 enum isl_dim_type type, unsigned n)
3986 if (!set)
3987 return NULL;
3988 isl_assert(set->ctx, type != isl_dim_in, goto error);
3989 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3990 error:
3991 isl_set_free(set);
3992 return NULL;
3995 __isl_give isl_basic_map *isl_basic_map_move_dims(
3996 __isl_take isl_basic_map *bmap,
3997 enum isl_dim_type dst_type, unsigned dst_pos,
3998 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4000 struct isl_dim_map *dim_map;
4001 struct isl_basic_map *res;
4002 enum isl_dim_type t;
4003 unsigned total, off;
4005 if (!bmap)
4006 return NULL;
4007 if (n == 0) {
4008 bmap = isl_basic_map_reset(bmap, src_type);
4009 bmap = isl_basic_map_reset(bmap, dst_type);
4010 return bmap;
4013 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4014 return isl_basic_map_free(bmap);
4016 if (dst_type == src_type && dst_pos == src_pos)
4017 return bmap;
4019 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4021 if (pos(bmap->dim, dst_type) + dst_pos ==
4022 pos(bmap->dim, src_type) + src_pos +
4023 ((src_type < dst_type) ? n : 0)) {
4024 bmap = isl_basic_map_cow(bmap);
4025 if (!bmap)
4026 return NULL;
4028 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4029 src_type, src_pos, n);
4030 if (!bmap->dim)
4031 goto error;
4033 bmap = isl_basic_map_finalize(bmap);
4035 return bmap;
4038 total = isl_basic_map_total_dim(bmap);
4039 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4041 off = 0;
4042 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4043 unsigned size = isl_space_dim(bmap->dim, t);
4044 if (t == dst_type) {
4045 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4046 0, dst_pos, off);
4047 off += dst_pos;
4048 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4049 src_pos, n, off);
4050 off += n;
4051 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4052 dst_pos, size - dst_pos, off);
4053 off += size - dst_pos;
4054 } else if (t == src_type) {
4055 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4056 0, src_pos, off);
4057 off += src_pos;
4058 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4059 src_pos + n, size - src_pos - n, off);
4060 off += size - src_pos - n;
4061 } else {
4062 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4063 off += size;
4066 isl_dim_map_div(dim_map, bmap, off);
4068 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4069 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4070 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4071 if (!bmap)
4072 goto error;
4074 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4075 src_type, src_pos, n);
4076 if (!bmap->dim)
4077 goto error;
4079 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4080 bmap = isl_basic_map_gauss(bmap, NULL);
4081 bmap = isl_basic_map_finalize(bmap);
4083 return bmap;
4084 error:
4085 isl_basic_map_free(bmap);
4086 return NULL;
4089 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4090 enum isl_dim_type dst_type, unsigned dst_pos,
4091 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4093 isl_basic_map *bmap = bset_to_bmap(bset);
4094 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4095 src_type, src_pos, n);
4096 return bset_from_bmap(bmap);
4099 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4100 enum isl_dim_type dst_type, unsigned dst_pos,
4101 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4103 if (!set)
4104 return NULL;
4105 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4106 return set_from_map(isl_map_move_dims(set_to_map(set),
4107 dst_type, dst_pos, src_type, src_pos, n));
4108 error:
4109 isl_set_free(set);
4110 return NULL;
4113 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4114 enum isl_dim_type dst_type, unsigned dst_pos,
4115 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4117 int i;
4119 if (n == 0) {
4120 map = isl_map_reset(map, src_type);
4121 map = isl_map_reset(map, dst_type);
4122 return map;
4125 if (isl_map_check_range(map, src_type, src_pos, n))
4126 return isl_map_free(map);
4128 if (dst_type == src_type && dst_pos == src_pos)
4129 return map;
4131 isl_assert(map->ctx, dst_type != src_type, goto error);
4133 map = isl_map_cow(map);
4134 if (!map)
4135 return NULL;
4137 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4138 if (!map->dim)
4139 goto error;
4141 for (i = 0; i < map->n; ++i) {
4142 map->p[i] = isl_basic_map_move_dims(map->p[i],
4143 dst_type, dst_pos,
4144 src_type, src_pos, n);
4145 if (!map->p[i])
4146 goto error;
4149 return map;
4150 error:
4151 isl_map_free(map);
4152 return NULL;
4155 /* Move the specified dimensions to the last columns right before
4156 * the divs. Don't change the dimension specification of bmap.
4157 * That's the responsibility of the caller.
4159 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4160 enum isl_dim_type type, unsigned first, unsigned n)
4162 struct isl_dim_map *dim_map;
4163 struct isl_basic_map *res;
4164 enum isl_dim_type t;
4165 unsigned total, off;
4167 if (!bmap)
4168 return NULL;
4169 if (pos(bmap->dim, type) + first + n ==
4170 1 + isl_space_dim(bmap->dim, isl_dim_all))
4171 return bmap;
4173 total = isl_basic_map_total_dim(bmap);
4174 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4176 off = 0;
4177 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4178 unsigned size = isl_space_dim(bmap->dim, t);
4179 if (t == type) {
4180 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4181 0, first, off);
4182 off += first;
4183 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4184 first, n, total - bmap->n_div - n);
4185 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4186 first + n, size - (first + n), off);
4187 off += size - (first + n);
4188 } else {
4189 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4190 off += size;
4193 isl_dim_map_div(dim_map, bmap, off + n);
4195 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4196 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4197 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4198 return res;
4201 /* Insert "n" rows in the divs of "bmap".
4203 * The number of columns is not changed, which means that the last
4204 * dimensions of "bmap" are being reintepreted as the new divs.
4205 * The space of "bmap" is not adjusted, however, which means
4206 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4207 * from the space of "bmap" is the responsibility of the caller.
4209 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4210 int n)
4212 int i;
4213 size_t row_size;
4214 isl_int **new_div;
4215 isl_int *old;
4217 bmap = isl_basic_map_cow(bmap);
4218 if (!bmap)
4219 return NULL;
4221 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4222 old = bmap->block2.data;
4223 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4224 (bmap->extra + n) * (1 + row_size));
4225 if (!bmap->block2.data)
4226 return isl_basic_map_free(bmap);
4227 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4228 if (!new_div)
4229 return isl_basic_map_free(bmap);
4230 for (i = 0; i < n; ++i) {
4231 new_div[i] = bmap->block2.data +
4232 (bmap->extra + i) * (1 + row_size);
4233 isl_seq_clr(new_div[i], 1 + row_size);
4235 for (i = 0; i < bmap->extra; ++i)
4236 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4237 free(bmap->div);
4238 bmap->div = new_div;
4239 bmap->n_div += n;
4240 bmap->extra += n;
4242 return bmap;
4245 /* Drop constraints from "bmap" that only involve the variables
4246 * of "type" in the range [first, first + n] that are not related
4247 * to any of the variables outside that interval.
4248 * These constraints cannot influence the values for the variables
4249 * outside the interval, except in case they cause "bmap" to be empty.
4250 * Only drop the constraints if "bmap" is known to be non-empty.
4252 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4253 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4254 unsigned first, unsigned n)
4256 int i;
4257 int *groups;
4258 unsigned dim, n_div;
4259 isl_bool non_empty;
4261 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4262 if (non_empty < 0)
4263 return isl_basic_map_free(bmap);
4264 if (!non_empty)
4265 return bmap;
4267 dim = isl_basic_map_dim(bmap, isl_dim_all);
4268 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4269 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4270 if (!groups)
4271 return isl_basic_map_free(bmap);
4272 first += isl_basic_map_offset(bmap, type) - 1;
4273 for (i = 0; i < first; ++i)
4274 groups[i] = -1;
4275 for (i = first + n; i < dim - n_div; ++i)
4276 groups[i] = -1;
4278 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4280 return bmap;
4283 /* Turn the n dimensions of type type, starting at first
4284 * into existentially quantified variables.
4286 * If a subset of the projected out variables are unrelated
4287 * to any of the variables that remain, then the constraints
4288 * involving this subset are simply dropped first.
4290 __isl_give isl_basic_map *isl_basic_map_project_out(
4291 __isl_take isl_basic_map *bmap,
4292 enum isl_dim_type type, unsigned first, unsigned n)
4294 isl_bool empty;
4296 if (n == 0)
4297 return basic_map_space_reset(bmap, type);
4298 if (type == isl_dim_div)
4299 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4300 "cannot project out existentially quantified variables",
4301 return isl_basic_map_free(bmap));
4303 empty = isl_basic_map_plain_is_empty(bmap);
4304 if (empty < 0)
4305 return isl_basic_map_free(bmap);
4306 if (empty)
4307 bmap = isl_basic_map_set_to_empty(bmap);
4309 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4310 if (!bmap)
4311 return NULL;
4313 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4314 return isl_basic_map_remove_dims(bmap, type, first, n);
4316 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4317 return isl_basic_map_free(bmap);
4319 bmap = move_last(bmap, type, first, n);
4320 bmap = isl_basic_map_cow(bmap);
4321 bmap = insert_div_rows(bmap, n);
4322 if (!bmap)
4323 return NULL;
4325 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4326 if (!bmap->dim)
4327 goto error;
4328 bmap = isl_basic_map_simplify(bmap);
4329 bmap = isl_basic_map_drop_redundant_divs(bmap);
4330 return isl_basic_map_finalize(bmap);
4331 error:
4332 isl_basic_map_free(bmap);
4333 return NULL;
4336 /* Turn the n dimensions of type type, starting at first
4337 * into existentially quantified variables.
4339 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4340 enum isl_dim_type type, unsigned first, unsigned n)
4342 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4343 type, first, n));
4346 /* Turn the n dimensions of type type, starting at first
4347 * into existentially quantified variables.
4349 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4350 enum isl_dim_type type, unsigned first, unsigned n)
4352 int i;
4354 if (n == 0)
4355 return map_space_reset(map, type);
4357 if (isl_map_check_range(map, type, first, n) < 0)
4358 return isl_map_free(map);
4360 map = isl_map_cow(map);
4361 if (!map)
4362 return NULL;
4364 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4365 if (!map->dim)
4366 goto error;
4368 for (i = 0; i < map->n; ++i) {
4369 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4370 if (!map->p[i])
4371 goto error;
4374 return map;
4375 error:
4376 isl_map_free(map);
4377 return NULL;
4380 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4381 * into existentially quantified variables.
4383 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4384 enum isl_dim_type type, unsigned first, unsigned n)
4386 unsigned dim;
4388 if (isl_map_check_range(map, type, first, n) < 0)
4389 return isl_map_free(map);
4390 dim = isl_map_dim(map, type);
4391 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4392 map = isl_map_project_out(map, type, 0, first);
4393 return map;
4396 /* Turn the n dimensions of type type, starting at first
4397 * into existentially quantified variables.
4399 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4400 enum isl_dim_type type, unsigned first, unsigned n)
4402 return set_from_map(isl_map_project_out(set_to_map(set),
4403 type, first, n));
4406 /* Return a map that projects the elements in "set" onto their
4407 * "n" set dimensions starting at "first".
4408 * "type" should be equal to isl_dim_set.
4410 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4411 enum isl_dim_type type, unsigned first, unsigned n)
4413 int i;
4414 int dim;
4415 isl_map *map;
4417 if (!set)
4418 return NULL;
4419 if (type != isl_dim_set)
4420 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4421 "only set dimensions can be projected out", goto error);
4422 dim = isl_set_dim(set, isl_dim_set);
4423 if (first + n > dim || first + n < first)
4424 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4425 "index out of bounds", goto error);
4427 map = isl_map_from_domain(set);
4428 map = isl_map_add_dims(map, isl_dim_out, n);
4429 for (i = 0; i < n; ++i)
4430 map = isl_map_equate(map, isl_dim_in, first + i,
4431 isl_dim_out, i);
4432 return map;
4433 error:
4434 isl_set_free(set);
4435 return NULL;
4438 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4439 unsigned n)
4441 int i, j;
4443 for (i = 0; i < n; ++i) {
4444 j = isl_basic_map_alloc_div(bmap);
4445 if (j < 0)
4446 goto error;
4447 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4449 return bmap;
4450 error:
4451 isl_basic_map_free(bmap);
4452 return NULL;
4455 struct isl_basic_map *isl_basic_map_apply_range(
4456 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4458 isl_space *space_result = NULL;
4459 struct isl_basic_map *bmap;
4460 unsigned n_in, n_out, n, nparam, total, pos;
4461 struct isl_dim_map *dim_map1, *dim_map2;
4463 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4464 goto error;
4465 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4466 bmap2->dim, isl_dim_in))
4467 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4468 "spaces don't match", goto error);
4470 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4471 isl_space_copy(bmap2->dim));
4473 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4474 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4475 n = isl_basic_map_dim(bmap1, isl_dim_out);
4476 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4478 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4479 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4480 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4481 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4482 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4483 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4484 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4485 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4486 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4487 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4488 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4490 bmap = isl_basic_map_alloc_space(space_result,
4491 bmap1->n_div + bmap2->n_div + n,
4492 bmap1->n_eq + bmap2->n_eq,
4493 bmap1->n_ineq + bmap2->n_ineq);
4494 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4495 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4496 bmap = add_divs(bmap, n);
4497 bmap = isl_basic_map_simplify(bmap);
4498 bmap = isl_basic_map_drop_redundant_divs(bmap);
4499 return isl_basic_map_finalize(bmap);
4500 error:
4501 isl_basic_map_free(bmap1);
4502 isl_basic_map_free(bmap2);
4503 return NULL;
4506 struct isl_basic_set *isl_basic_set_apply(
4507 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4509 if (!bset || !bmap)
4510 goto error;
4512 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4513 goto error);
4515 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4516 bmap));
4517 error:
4518 isl_basic_set_free(bset);
4519 isl_basic_map_free(bmap);
4520 return NULL;
4523 struct isl_basic_map *isl_basic_map_apply_domain(
4524 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4526 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4527 goto error;
4528 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4529 bmap2->dim, isl_dim_in))
4530 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4531 "spaces don't match", goto error);
4533 bmap1 = isl_basic_map_reverse(bmap1);
4534 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4535 return isl_basic_map_reverse(bmap1);
4536 error:
4537 isl_basic_map_free(bmap1);
4538 isl_basic_map_free(bmap2);
4539 return NULL;
4542 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4543 * A \cap B -> f(A) + f(B)
4545 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4546 __isl_take isl_basic_map *bmap2)
4548 unsigned n_in, n_out, nparam, total, pos;
4549 struct isl_basic_map *bmap = NULL;
4550 struct isl_dim_map *dim_map1, *dim_map2;
4551 int i;
4553 if (!bmap1 || !bmap2)
4554 goto error;
4556 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4557 goto error);
4559 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4560 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4561 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4563 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4564 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4565 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4566 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4567 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4568 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4569 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4570 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4571 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4572 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4573 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4575 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4576 bmap1->n_div + bmap2->n_div + 2 * n_out,
4577 bmap1->n_eq + bmap2->n_eq + n_out,
4578 bmap1->n_ineq + bmap2->n_ineq);
4579 for (i = 0; i < n_out; ++i) {
4580 int j = isl_basic_map_alloc_equality(bmap);
4581 if (j < 0)
4582 goto error;
4583 isl_seq_clr(bmap->eq[j], 1+total);
4584 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4585 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4586 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4588 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4589 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4590 bmap = add_divs(bmap, 2 * n_out);
4592 bmap = isl_basic_map_simplify(bmap);
4593 return isl_basic_map_finalize(bmap);
4594 error:
4595 isl_basic_map_free(bmap);
4596 isl_basic_map_free(bmap1);
4597 isl_basic_map_free(bmap2);
4598 return NULL;
4601 /* Given two maps A -> f(A) and B -> g(B), construct a map
4602 * A \cap B -> f(A) + f(B)
4604 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4605 __isl_take isl_map *map2)
4607 struct isl_map *result;
4608 int i, j;
4610 if (!map1 || !map2)
4611 goto error;
4613 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4615 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4616 map1->n * map2->n, 0);
4617 if (!result)
4618 goto error;
4619 for (i = 0; i < map1->n; ++i)
4620 for (j = 0; j < map2->n; ++j) {
4621 struct isl_basic_map *part;
4622 part = isl_basic_map_sum(
4623 isl_basic_map_copy(map1->p[i]),
4624 isl_basic_map_copy(map2->p[j]));
4625 if (isl_basic_map_is_empty(part))
4626 isl_basic_map_free(part);
4627 else
4628 result = isl_map_add_basic_map(result, part);
4629 if (!result)
4630 goto error;
4632 isl_map_free(map1);
4633 isl_map_free(map2);
4634 return result;
4635 error:
4636 isl_map_free(map1);
4637 isl_map_free(map2);
4638 return NULL;
4641 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4642 __isl_take isl_set *set2)
4644 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4647 /* Given a basic map A -> f(A), construct A -> -f(A).
4649 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4651 int i, j;
4652 unsigned off, n;
4654 bmap = isl_basic_map_cow(bmap);
4655 if (!bmap)
4656 return NULL;
4658 n = isl_basic_map_dim(bmap, isl_dim_out);
4659 off = isl_basic_map_offset(bmap, isl_dim_out);
4660 for (i = 0; i < bmap->n_eq; ++i)
4661 for (j = 0; j < n; ++j)
4662 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4663 for (i = 0; i < bmap->n_ineq; ++i)
4664 for (j = 0; j < n; ++j)
4665 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4666 for (i = 0; i < bmap->n_div; ++i)
4667 for (j = 0; j < n; ++j)
4668 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4669 bmap = isl_basic_map_gauss(bmap, NULL);
4670 return isl_basic_map_finalize(bmap);
4673 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4675 return isl_basic_map_neg(bset);
4678 /* Given a map A -> f(A), construct A -> -f(A).
4680 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4682 int i;
4684 map = isl_map_cow(map);
4685 if (!map)
4686 return NULL;
4688 for (i = 0; i < map->n; ++i) {
4689 map->p[i] = isl_basic_map_neg(map->p[i]);
4690 if (!map->p[i])
4691 goto error;
4694 return map;
4695 error:
4696 isl_map_free(map);
4697 return NULL;
4700 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4702 return set_from_map(isl_map_neg(set_to_map(set)));
4705 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4706 * A -> floor(f(A)/d).
4708 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4709 isl_int d)
4711 unsigned n_in, n_out, nparam, total, pos;
4712 struct isl_basic_map *result = NULL;
4713 struct isl_dim_map *dim_map;
4714 int i;
4716 if (!bmap)
4717 return NULL;
4719 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4720 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4721 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4723 total = nparam + n_in + n_out + bmap->n_div + n_out;
4724 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4725 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4726 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4727 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4728 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4730 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4731 bmap->n_div + n_out,
4732 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4733 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4734 result = add_divs(result, n_out);
4735 for (i = 0; i < n_out; ++i) {
4736 int j;
4737 j = isl_basic_map_alloc_inequality(result);
4738 if (j < 0)
4739 goto error;
4740 isl_seq_clr(result->ineq[j], 1+total);
4741 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4742 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4743 j = isl_basic_map_alloc_inequality(result);
4744 if (j < 0)
4745 goto error;
4746 isl_seq_clr(result->ineq[j], 1+total);
4747 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4748 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4749 isl_int_sub_ui(result->ineq[j][0], d, 1);
4752 result = isl_basic_map_simplify(result);
4753 return isl_basic_map_finalize(result);
4754 error:
4755 isl_basic_map_free(result);
4756 return NULL;
4759 /* Given a map A -> f(A) and an integer d, construct a map
4760 * A -> floor(f(A)/d).
4762 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4764 int i;
4766 map = isl_map_cow(map);
4767 if (!map)
4768 return NULL;
4770 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4771 for (i = 0; i < map->n; ++i) {
4772 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4773 if (!map->p[i])
4774 goto error;
4776 map = isl_map_unmark_normalized(map);
4778 return map;
4779 error:
4780 isl_map_free(map);
4781 return NULL;
4784 /* Given a map A -> f(A) and an integer d, construct a map
4785 * A -> floor(f(A)/d).
4787 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4788 __isl_take isl_val *d)
4790 if (!map || !d)
4791 goto error;
4792 if (!isl_val_is_int(d))
4793 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4794 "expecting integer denominator", goto error);
4795 map = isl_map_floordiv(map, d->n);
4796 isl_val_free(d);
4797 return map;
4798 error:
4799 isl_map_free(map);
4800 isl_val_free(d);
4801 return NULL;
4804 static __isl_give isl_basic_map *var_equal(__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_equality(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->eq[i], 1 + isl_basic_map_total_dim(bmap));
4817 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4818 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4819 return isl_basic_map_finalize(bmap);
4820 error:
4821 isl_basic_map_free(bmap);
4822 return NULL;
4825 /* Add a constraint to "bmap" expressing i_pos < o_pos
4827 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4828 unsigned pos)
4830 int i;
4831 unsigned nparam;
4832 unsigned n_in;
4834 i = isl_basic_map_alloc_inequality(bmap);
4835 if (i < 0)
4836 goto error;
4837 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4838 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4839 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4840 isl_int_set_si(bmap->ineq[i][0], -1);
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_less_or_equal(
4852 __isl_take isl_basic_map *bmap, 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][1+nparam+pos], -1);
4865 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4866 return isl_basic_map_finalize(bmap);
4867 error:
4868 isl_basic_map_free(bmap);
4869 return NULL;
4872 /* Add a constraint to "bmap" expressing i_pos > o_pos
4874 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4875 unsigned pos)
4877 int i;
4878 unsigned nparam;
4879 unsigned n_in;
4881 i = isl_basic_map_alloc_inequality(bmap);
4882 if (i < 0)
4883 goto error;
4884 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4885 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4886 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4887 isl_int_set_si(bmap->ineq[i][0], -1);
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 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4898 static __isl_give isl_basic_map *var_more_or_equal(
4899 __isl_take isl_basic_map *bmap, unsigned pos)
4901 int i;
4902 unsigned nparam;
4903 unsigned n_in;
4905 i = isl_basic_map_alloc_inequality(bmap);
4906 if (i < 0)
4907 goto error;
4908 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4909 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4910 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4911 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4912 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4913 return isl_basic_map_finalize(bmap);
4914 error:
4915 isl_basic_map_free(bmap);
4916 return NULL;
4919 __isl_give isl_basic_map *isl_basic_map_equal(
4920 __isl_take isl_space *space, unsigned n_equal)
4922 int i;
4923 struct isl_basic_map *bmap;
4924 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4925 if (!bmap)
4926 return NULL;
4927 for (i = 0; i < n_equal && bmap; ++i)
4928 bmap = var_equal(bmap, i);
4929 return isl_basic_map_finalize(bmap);
4932 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4934 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4935 unsigned pos)
4937 int i;
4938 struct isl_basic_map *bmap;
4939 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4940 if (!bmap)
4941 return NULL;
4942 for (i = 0; i < pos && bmap; ++i)
4943 bmap = var_equal(bmap, i);
4944 if (bmap)
4945 bmap = var_less(bmap, pos);
4946 return isl_basic_map_finalize(bmap);
4949 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4951 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4952 __isl_take isl_space *space, unsigned pos)
4954 int i;
4955 isl_basic_map *bmap;
4957 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4958 for (i = 0; i < pos; ++i)
4959 bmap = var_equal(bmap, i);
4960 bmap = var_less_or_equal(bmap, pos);
4961 return isl_basic_map_finalize(bmap);
4964 /* Return a relation on "space" expressing i_pos > o_pos
4966 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4967 unsigned pos)
4969 int i;
4970 struct isl_basic_map *bmap;
4971 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4972 if (!bmap)
4973 return NULL;
4974 for (i = 0; i < pos && bmap; ++i)
4975 bmap = var_equal(bmap, i);
4976 if (bmap)
4977 bmap = var_more(bmap, pos);
4978 return isl_basic_map_finalize(bmap);
4981 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4983 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4984 __isl_take isl_space *space, unsigned pos)
4986 int i;
4987 isl_basic_map *bmap;
4989 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4990 for (i = 0; i < pos; ++i)
4991 bmap = var_equal(bmap, i);
4992 bmap = var_more_or_equal(bmap, pos);
4993 return isl_basic_map_finalize(bmap);
4996 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4997 unsigned n, int equal)
4999 struct isl_map *map;
5000 int i;
5002 if (n == 0 && equal)
5003 return isl_map_universe(space);
5005 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5007 for (i = 0; i + 1 < n; ++i)
5008 map = isl_map_add_basic_map(map,
5009 isl_basic_map_less_at(isl_space_copy(space), i));
5010 if (n > 0) {
5011 if (equal)
5012 map = isl_map_add_basic_map(map,
5013 isl_basic_map_less_or_equal_at(space, n - 1));
5014 else
5015 map = isl_map_add_basic_map(map,
5016 isl_basic_map_less_at(space, n - 1));
5017 } else
5018 isl_space_free(space);
5020 return map;
5023 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5025 if (!space)
5026 return NULL;
5027 return map_lex_lte_first(space, space->n_out, equal);
5030 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5032 return map_lex_lte_first(dim, n, 0);
5035 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5037 return map_lex_lte_first(dim, n, 1);
5040 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5042 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5045 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5047 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5050 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5051 unsigned n, int equal)
5053 struct isl_map *map;
5054 int i;
5056 if (n == 0 && equal)
5057 return isl_map_universe(space);
5059 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5061 for (i = 0; i + 1 < n; ++i)
5062 map = isl_map_add_basic_map(map,
5063 isl_basic_map_more_at(isl_space_copy(space), i));
5064 if (n > 0) {
5065 if (equal)
5066 map = isl_map_add_basic_map(map,
5067 isl_basic_map_more_or_equal_at(space, n - 1));
5068 else
5069 map = isl_map_add_basic_map(map,
5070 isl_basic_map_more_at(space, n - 1));
5071 } else
5072 isl_space_free(space);
5074 return map;
5077 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5079 if (!space)
5080 return NULL;
5081 return map_lex_gte_first(space, space->n_out, equal);
5084 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5086 return map_lex_gte_first(dim, n, 0);
5089 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5091 return map_lex_gte_first(dim, n, 1);
5094 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5096 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5099 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5101 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5104 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5105 __isl_take isl_set *set2)
5107 isl_map *map;
5108 map = isl_map_lex_le(isl_set_get_space(set1));
5109 map = isl_map_intersect_domain(map, set1);
5110 map = isl_map_intersect_range(map, set2);
5111 return map;
5114 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5115 __isl_take isl_set *set2)
5117 isl_map *map;
5118 map = isl_map_lex_lt(isl_set_get_space(set1));
5119 map = isl_map_intersect_domain(map, set1);
5120 map = isl_map_intersect_range(map, set2);
5121 return map;
5124 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5125 __isl_take isl_set *set2)
5127 isl_map *map;
5128 map = isl_map_lex_ge(isl_set_get_space(set1));
5129 map = isl_map_intersect_domain(map, set1);
5130 map = isl_map_intersect_range(map, set2);
5131 return map;
5134 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5135 __isl_take isl_set *set2)
5137 isl_map *map;
5138 map = isl_map_lex_gt(isl_set_get_space(set1));
5139 map = isl_map_intersect_domain(map, set1);
5140 map = isl_map_intersect_range(map, set2);
5141 return map;
5144 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5145 __isl_take isl_map *map2)
5147 isl_map *map;
5148 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5149 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5150 map = isl_map_apply_range(map, isl_map_reverse(map2));
5151 return map;
5154 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5155 __isl_take isl_map *map2)
5157 isl_map *map;
5158 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5159 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5160 map = isl_map_apply_range(map, isl_map_reverse(map2));
5161 return map;
5164 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5165 __isl_take isl_map *map2)
5167 isl_map *map;
5168 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5169 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5170 map = isl_map_apply_range(map, isl_map_reverse(map2));
5171 return map;
5174 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5175 __isl_take isl_map *map2)
5177 isl_map *map;
5178 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5179 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5180 map = isl_map_apply_range(map, isl_map_reverse(map2));
5181 return map;
5184 /* For a div d = floor(f/m), add the constraint
5186 * f - m d >= 0
5188 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5189 unsigned pos, isl_int *div)
5191 int i;
5192 unsigned total = isl_basic_map_total_dim(bmap);
5194 i = isl_basic_map_alloc_inequality(bmap);
5195 if (i < 0)
5196 return isl_stat_error;
5197 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5198 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5200 return isl_stat_ok;
5203 /* For a div d = floor(f/m), add the constraint
5205 * -(f-(m-1)) + m d >= 0
5207 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5208 unsigned pos, isl_int *div)
5210 int i;
5211 unsigned total = isl_basic_map_total_dim(bmap);
5213 i = isl_basic_map_alloc_inequality(bmap);
5214 if (i < 0)
5215 return isl_stat_error;
5216 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5217 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5218 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5219 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5221 return isl_stat_ok;
5224 /* For a div d = floor(f/m), add the constraints
5226 * f - m d >= 0
5227 * -(f-(m-1)) + m d >= 0
5229 * Note that the second constraint is the negation of
5231 * f - m d >= m
5233 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5234 unsigned pos, isl_int *div)
5236 if (add_upper_div_constraint(bmap, pos, div) < 0)
5237 return -1;
5238 if (add_lower_div_constraint(bmap, pos, div) < 0)
5239 return -1;
5240 return 0;
5243 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5244 unsigned pos, isl_int *div)
5246 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5247 pos, div);
5250 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map *bmap,
5251 unsigned div)
5253 unsigned total = isl_basic_map_total_dim(bmap);
5254 unsigned div_pos = total - bmap->n_div + div;
5256 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5257 bmap->div[div]);
5260 /* For each known div d = floor(f/m), add the constraints
5262 * f - m d >= 0
5263 * -(f-(m-1)) + m d >= 0
5265 * Remove duplicate constraints in case of some these div constraints
5266 * already appear in "bmap".
5268 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5269 __isl_take isl_basic_map *bmap)
5271 unsigned n_div;
5273 if (!bmap)
5274 return NULL;
5275 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5276 if (n_div == 0)
5277 return bmap;
5279 bmap = add_known_div_constraints(bmap);
5280 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5281 bmap = isl_basic_map_finalize(bmap);
5282 return bmap;
5285 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5287 * In particular, if this div is of the form d = floor(f/m),
5288 * then add the constraint
5290 * f - m d >= 0
5292 * if sign < 0 or the constraint
5294 * -(f-(m-1)) + m d >= 0
5296 * if sign > 0.
5298 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5299 unsigned div, int sign)
5301 unsigned total;
5302 unsigned div_pos;
5304 if (!bmap)
5305 return -1;
5307 total = isl_basic_map_total_dim(bmap);
5308 div_pos = total - bmap->n_div + div;
5310 if (sign < 0)
5311 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5312 else
5313 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5316 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5317 __isl_take isl_basic_map *bmap)
5319 if (!bmap)
5320 goto error;
5321 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5322 bmap->n_div == 0 &&
5323 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5324 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5325 return bset_from_bmap(bmap);
5326 bmap = isl_basic_map_cow(bmap);
5327 if (!bmap)
5328 goto error;
5329 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5330 if (!bmap->dim)
5331 goto error;
5332 bmap->extra -= bmap->n_div;
5333 bmap->n_div = 0;
5334 bmap = isl_basic_map_finalize(bmap);
5335 return bset_from_bmap(bmap);
5336 error:
5337 isl_basic_map_free(bmap);
5338 return NULL;
5341 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5342 __isl_take isl_basic_set *bset)
5344 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5347 /* Replace each element in "list" by the result of applying
5348 * isl_basic_map_underlying_set to the element.
5350 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5351 __isl_take isl_basic_map_list *list)
5353 int i, n;
5355 if (!list)
5356 return NULL;
5358 n = isl_basic_map_list_n_basic_map(list);
5359 for (i = 0; i < n; ++i) {
5360 isl_basic_map *bmap;
5361 isl_basic_set *bset;
5363 bmap = isl_basic_map_list_get_basic_map(list, i);
5364 bset = isl_basic_set_underlying_set(bmap);
5365 list = isl_basic_set_list_set_basic_set(list, i, bset);
5368 return list;
5371 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5372 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5374 struct isl_basic_map *bmap;
5375 struct isl_ctx *ctx;
5376 unsigned total;
5377 int i;
5379 if (!bset || !like)
5380 goto error;
5381 ctx = bset->ctx;
5382 isl_assert(ctx, bset->n_div == 0, goto error);
5383 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5384 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5385 goto error);
5386 if (like->n_div == 0) {
5387 isl_space *space = isl_basic_map_get_space(like);
5388 isl_basic_map_free(like);
5389 return isl_basic_map_reset_space(bset, space);
5391 bset = isl_basic_set_cow(bset);
5392 if (!bset)
5393 goto error;
5394 total = bset->dim->n_out + bset->extra;
5395 bmap = bset_to_bmap(bset);
5396 isl_space_free(bmap->dim);
5397 bmap->dim = isl_space_copy(like->dim);
5398 if (!bmap->dim)
5399 goto error;
5400 bmap->n_div = like->n_div;
5401 bmap->extra += like->n_div;
5402 if (bmap->extra) {
5403 unsigned ltotal;
5404 isl_int **div;
5405 ltotal = total - bmap->extra + like->extra;
5406 if (ltotal > total)
5407 ltotal = total;
5408 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5409 bmap->extra * (1 + 1 + total));
5410 if (isl_blk_is_error(bmap->block2))
5411 goto error;
5412 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5413 if (!div)
5414 goto error;
5415 bmap->div = div;
5416 for (i = 0; i < bmap->extra; ++i)
5417 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5418 for (i = 0; i < like->n_div; ++i) {
5419 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5420 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5422 bmap = isl_basic_map_add_known_div_constraints(bmap);
5424 isl_basic_map_free(like);
5425 bmap = isl_basic_map_simplify(bmap);
5426 bmap = isl_basic_map_finalize(bmap);
5427 return bmap;
5428 error:
5429 isl_basic_map_free(like);
5430 isl_basic_set_free(bset);
5431 return NULL;
5434 struct isl_basic_set *isl_basic_set_from_underlying_set(
5435 struct isl_basic_set *bset, struct isl_basic_set *like)
5437 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5438 bset_to_bmap(like)));
5441 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5443 int i;
5445 map = isl_map_cow(map);
5446 if (!map)
5447 return NULL;
5448 map->dim = isl_space_cow(map->dim);
5449 if (!map->dim)
5450 goto error;
5452 for (i = 1; i < map->n; ++i)
5453 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5454 goto error);
5455 for (i = 0; i < map->n; ++i) {
5456 map->p[i] = bset_to_bmap(
5457 isl_basic_map_underlying_set(map->p[i]));
5458 if (!map->p[i])
5459 goto error;
5461 if (map->n == 0)
5462 map->dim = isl_space_underlying(map->dim, 0);
5463 else {
5464 isl_space_free(map->dim);
5465 map->dim = isl_space_copy(map->p[0]->dim);
5467 if (!map->dim)
5468 goto error;
5469 return set_from_map(map);
5470 error:
5471 isl_map_free(map);
5472 return NULL;
5475 /* Replace the space of "bmap" by "space".
5477 * If the space of "bmap" is identical to "space" (including the identifiers
5478 * of the input and output dimensions), then simply return the original input.
5480 __isl_give isl_basic_map *isl_basic_map_reset_space(
5481 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5483 isl_bool equal;
5484 isl_space *bmap_space;
5486 bmap_space = isl_basic_map_peek_space(bmap);
5487 equal = isl_space_is_equal(bmap_space, space);
5488 if (equal >= 0 && equal)
5489 equal = isl_space_has_equal_ids(bmap_space, space);
5490 if (equal < 0)
5491 goto error;
5492 if (equal) {
5493 isl_space_free(space);
5494 return bmap;
5496 bmap = isl_basic_map_cow(bmap);
5497 if (!bmap || !space)
5498 goto error;
5500 isl_space_free(bmap->dim);
5501 bmap->dim = space;
5503 bmap = isl_basic_map_finalize(bmap);
5505 return bmap;
5506 error:
5507 isl_basic_map_free(bmap);
5508 isl_space_free(space);
5509 return NULL;
5512 __isl_give isl_basic_set *isl_basic_set_reset_space(
5513 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5515 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5516 dim));
5519 /* Check that the total dimensions of "map" and "space" are the same.
5521 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5522 __isl_keep isl_space *space)
5524 unsigned dim1, dim2;
5526 if (!map || !space)
5527 return isl_stat_error;
5528 dim1 = isl_map_dim(map, isl_dim_all);
5529 dim2 = isl_space_dim(space, isl_dim_all);
5530 if (dim1 == dim2)
5531 return isl_stat_ok;
5532 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5533 "total dimensions do not match", return isl_stat_error);
5536 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5537 __isl_take isl_space *space)
5539 int i;
5541 map = isl_map_cow(map);
5542 if (!map || !space)
5543 goto error;
5545 for (i = 0; i < map->n; ++i) {
5546 map->p[i] = isl_basic_map_reset_space(map->p[i],
5547 isl_space_copy(space));
5548 if (!map->p[i])
5549 goto error;
5551 isl_space_free(map->dim);
5552 map->dim = space;
5554 return map;
5555 error:
5556 isl_map_free(map);
5557 isl_space_free(space);
5558 return NULL;
5561 /* Replace the space of "map" by "space", without modifying
5562 * the dimension of "map".
5564 * If the space of "map" is identical to "space" (including the identifiers
5565 * of the input and output dimensions), then simply return the original input.
5567 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5568 __isl_take isl_space *space)
5570 isl_bool equal;
5571 isl_space *map_space;
5573 map_space = isl_map_peek_space(map);
5574 equal = isl_space_is_equal(map_space, space);
5575 if (equal >= 0 && equal)
5576 equal = isl_space_has_equal_ids(map_space, space);
5577 if (equal < 0)
5578 goto error;
5579 if (equal) {
5580 isl_space_free(space);
5581 return map;
5583 if (check_map_space_equal_total_dim(map, space) < 0)
5584 goto error;
5585 return isl_map_reset_space(map, space);
5586 error:
5587 isl_map_free(map);
5588 isl_space_free(space);
5589 return NULL;
5592 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5593 __isl_take isl_space *dim)
5595 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5598 /* Compute the parameter domain of the given basic set.
5600 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5602 isl_bool is_params;
5603 isl_space *space;
5604 unsigned n;
5606 is_params = isl_basic_set_is_params(bset);
5607 if (is_params < 0)
5608 return isl_basic_set_free(bset);
5609 if (is_params)
5610 return bset;
5612 n = isl_basic_set_dim(bset, isl_dim_set);
5613 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5614 space = isl_basic_set_get_space(bset);
5615 space = isl_space_params(space);
5616 bset = isl_basic_set_reset_space(bset, space);
5617 return bset;
5620 /* Construct a zero-dimensional basic set with the given parameter domain.
5622 __isl_give isl_basic_set *isl_basic_set_from_params(
5623 __isl_take isl_basic_set *bset)
5625 isl_space *space;
5626 space = isl_basic_set_get_space(bset);
5627 space = isl_space_set_from_params(space);
5628 bset = isl_basic_set_reset_space(bset, space);
5629 return bset;
5632 /* Compute the parameter domain of the given set.
5634 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5636 isl_space *space;
5637 unsigned n;
5639 if (isl_set_is_params(set))
5640 return set;
5642 n = isl_set_dim(set, isl_dim_set);
5643 set = isl_set_project_out(set, isl_dim_set, 0, n);
5644 space = isl_set_get_space(set);
5645 space = isl_space_params(space);
5646 set = isl_set_reset_space(set, space);
5647 return set;
5650 /* Construct a zero-dimensional set with the given parameter domain.
5652 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5654 isl_space *space;
5655 space = isl_set_get_space(set);
5656 space = isl_space_set_from_params(space);
5657 set = isl_set_reset_space(set, space);
5658 return set;
5661 /* Compute the parameter domain of the given map.
5663 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5665 isl_space *space;
5666 unsigned n;
5668 n = isl_map_dim(map, isl_dim_in);
5669 map = isl_map_project_out(map, isl_dim_in, 0, n);
5670 n = isl_map_dim(map, isl_dim_out);
5671 map = isl_map_project_out(map, isl_dim_out, 0, n);
5672 space = isl_map_get_space(map);
5673 space = isl_space_params(space);
5674 map = isl_map_reset_space(map, space);
5675 return map;
5678 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5680 isl_space *space;
5681 unsigned n_out;
5683 if (!bmap)
5684 return NULL;
5685 space = isl_space_domain(isl_basic_map_get_space(bmap));
5687 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5688 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5690 return isl_basic_map_reset_space(bmap, space);
5693 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5695 if (!bmap)
5696 return isl_bool_error;
5697 return isl_space_may_be_set(bmap->dim);
5700 /* Is this basic map actually a set?
5701 * Users should never call this function. Outside of isl,
5702 * the type should indicate whether something is a set or a map.
5704 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5706 if (!bmap)
5707 return isl_bool_error;
5708 return isl_space_is_set(bmap->dim);
5711 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5713 isl_bool is_set;
5715 is_set = isl_basic_map_is_set(bmap);
5716 if (is_set < 0)
5717 goto error;
5718 if (is_set)
5719 return bmap;
5720 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5721 error:
5722 isl_basic_map_free(bmap);
5723 return NULL;
5726 __isl_give isl_basic_map *isl_basic_map_domain_map(
5727 __isl_take isl_basic_map *bmap)
5729 int i;
5730 isl_space *space;
5731 isl_basic_map *domain;
5732 int nparam, n_in, n_out;
5734 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5735 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5736 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5738 space = isl_basic_map_get_space(bmap);
5739 space = isl_space_from_range(isl_space_domain(space));
5740 domain = isl_basic_map_universe(space);
5742 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5743 bmap = isl_basic_map_apply_range(bmap, domain);
5744 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5746 for (i = 0; i < n_in; ++i)
5747 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5748 isl_dim_out, i);
5750 bmap = isl_basic_map_gauss(bmap, NULL);
5751 return isl_basic_map_finalize(bmap);
5754 __isl_give isl_basic_map *isl_basic_map_range_map(
5755 __isl_take isl_basic_map *bmap)
5757 int i;
5758 isl_space *space;
5759 isl_basic_map *range;
5760 int nparam, n_in, n_out;
5762 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5763 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5764 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5766 space = isl_basic_map_get_space(bmap);
5767 space = isl_space_from_range(isl_space_range(space));
5768 range = isl_basic_map_universe(space);
5770 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5771 bmap = isl_basic_map_apply_range(bmap, range);
5772 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5774 for (i = 0; i < n_out; ++i)
5775 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5776 isl_dim_out, i);
5778 bmap = isl_basic_map_gauss(bmap, NULL);
5779 return isl_basic_map_finalize(bmap);
5782 int isl_map_may_be_set(__isl_keep isl_map *map)
5784 if (!map)
5785 return -1;
5786 return isl_space_may_be_set(map->dim);
5789 /* Is this map actually a set?
5790 * Users should never call this function. Outside of isl,
5791 * the type should indicate whether something is a set or a map.
5793 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5795 if (!map)
5796 return isl_bool_error;
5797 return isl_space_is_set(map->dim);
5800 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5802 int i;
5803 isl_bool is_set;
5804 struct isl_set *set;
5806 is_set = isl_map_is_set(map);
5807 if (is_set < 0)
5808 goto error;
5809 if (is_set)
5810 return set_from_map(map);
5812 map = isl_map_cow(map);
5813 if (!map)
5814 goto error;
5816 set = set_from_map(map);
5817 set->dim = isl_space_range(set->dim);
5818 if (!set->dim)
5819 goto error;
5820 for (i = 0; i < map->n; ++i) {
5821 set->p[i] = isl_basic_map_range(map->p[i]);
5822 if (!set->p[i])
5823 goto error;
5825 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5826 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5827 return set;
5828 error:
5829 isl_map_free(map);
5830 return NULL;
5833 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5835 int i;
5837 map = isl_map_cow(map);
5838 if (!map)
5839 return NULL;
5841 map->dim = isl_space_domain_map(map->dim);
5842 if (!map->dim)
5843 goto error;
5844 for (i = 0; i < map->n; ++i) {
5845 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5846 if (!map->p[i])
5847 goto error;
5849 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5850 map = isl_map_unmark_normalized(map);
5851 return map;
5852 error:
5853 isl_map_free(map);
5854 return NULL;
5857 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5859 int i;
5860 isl_space *range_dim;
5862 map = isl_map_cow(map);
5863 if (!map)
5864 return NULL;
5866 range_dim = isl_space_range(isl_map_get_space(map));
5867 range_dim = isl_space_from_range(range_dim);
5868 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5869 map->dim = isl_space_join(map->dim, range_dim);
5870 if (!map->dim)
5871 goto error;
5872 for (i = 0; i < map->n; ++i) {
5873 map->p[i] = isl_basic_map_range_map(map->p[i]);
5874 if (!map->p[i])
5875 goto error;
5877 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5878 map = isl_map_unmark_normalized(map);
5879 return map;
5880 error:
5881 isl_map_free(map);
5882 return NULL;
5885 /* Given a wrapped map of the form A[B -> C],
5886 * return the map A[B -> C] -> B.
5888 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5890 isl_id *id;
5891 isl_map *map;
5893 if (!set)
5894 return NULL;
5895 if (!isl_set_has_tuple_id(set))
5896 return isl_map_domain_map(isl_set_unwrap(set));
5898 id = isl_set_get_tuple_id(set);
5899 map = isl_map_domain_map(isl_set_unwrap(set));
5900 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5902 return map;
5905 __isl_give isl_basic_map *isl_basic_map_from_domain(
5906 __isl_take isl_basic_set *bset)
5908 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5911 __isl_give isl_basic_map *isl_basic_map_from_range(
5912 __isl_take isl_basic_set *bset)
5914 isl_space *space;
5915 space = isl_basic_set_get_space(bset);
5916 space = isl_space_from_range(space);
5917 bset = isl_basic_set_reset_space(bset, space);
5918 return bset_to_bmap(bset);
5921 /* Create a relation with the given set as range.
5922 * The domain of the created relation is a zero-dimensional
5923 * flat anonymous space.
5925 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5927 isl_space *space;
5928 space = isl_set_get_space(set);
5929 space = isl_space_from_range(space);
5930 set = isl_set_reset_space(set, space);
5931 return set_to_map(set);
5934 /* Create a relation with the given set as domain.
5935 * The range of the created relation is a zero-dimensional
5936 * flat anonymous space.
5938 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5940 return isl_map_reverse(isl_map_from_range(set));
5943 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5944 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5946 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5949 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5950 __isl_take isl_set *range)
5952 return isl_map_apply_range(isl_map_reverse(domain), range);
5955 /* Return a newly allocated isl_map with given space and flags and
5956 * room for "n" basic maps.
5957 * Make sure that all cached information is cleared.
5959 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5960 unsigned flags)
5962 struct isl_map *map;
5964 if (!space)
5965 return NULL;
5966 if (n < 0)
5967 isl_die(space->ctx, isl_error_internal,
5968 "negative number of basic maps", goto error);
5969 map = isl_calloc(space->ctx, struct isl_map,
5970 sizeof(struct isl_map) +
5971 (n - 1) * sizeof(struct isl_basic_map *));
5972 if (!map)
5973 goto error;
5975 map->ctx = space->ctx;
5976 isl_ctx_ref(map->ctx);
5977 map->ref = 1;
5978 map->size = n;
5979 map->n = 0;
5980 map->dim = space;
5981 map->flags = flags;
5982 return map;
5983 error:
5984 isl_space_free(space);
5985 return NULL;
5988 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5990 struct isl_basic_map *bmap;
5991 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5992 bmap = isl_basic_map_set_to_empty(bmap);
5993 return bmap;
5996 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5998 struct isl_basic_set *bset;
5999 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6000 bset = isl_basic_set_set_to_empty(bset);
6001 return bset;
6004 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6006 struct isl_basic_map *bmap;
6007 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6008 bmap = isl_basic_map_finalize(bmap);
6009 return bmap;
6012 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6014 struct isl_basic_set *bset;
6015 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6016 bset = isl_basic_set_finalize(bset);
6017 return bset;
6020 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6021 __isl_take isl_space *space)
6023 int i;
6024 unsigned total = isl_space_dim(space, isl_dim_all);
6025 isl_basic_map *bmap;
6027 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6028 for (i = 0; i < total; ++i) {
6029 int k = isl_basic_map_alloc_inequality(bmap);
6030 if (k < 0)
6031 goto error;
6032 isl_seq_clr(bmap->ineq[k], 1 + total);
6033 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6035 return bmap;
6036 error:
6037 isl_basic_map_free(bmap);
6038 return NULL;
6041 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6042 __isl_take isl_space *space)
6044 return isl_basic_map_nat_universe(space);
6047 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6049 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6052 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6054 return isl_map_nat_universe(dim);
6057 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6059 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6062 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6064 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6067 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6069 struct isl_map *map;
6070 if (!space)
6071 return NULL;
6072 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6073 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6074 return map;
6077 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6079 struct isl_set *set;
6080 if (!space)
6081 return NULL;
6082 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6083 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6084 return set;
6087 struct isl_map *isl_map_dup(struct isl_map *map)
6089 int i;
6090 struct isl_map *dup;
6092 if (!map)
6093 return NULL;
6094 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6095 for (i = 0; i < map->n; ++i)
6096 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6097 return dup;
6100 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6101 __isl_take isl_basic_map *bmap)
6103 if (!bmap || !map)
6104 goto error;
6105 if (isl_basic_map_plain_is_empty(bmap)) {
6106 isl_basic_map_free(bmap);
6107 return map;
6109 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6110 isl_assert(map->ctx, map->n < map->size, goto error);
6111 map->p[map->n] = bmap;
6112 map->n++;
6113 map = isl_map_unmark_normalized(map);
6114 return map;
6115 error:
6116 if (map)
6117 isl_map_free(map);
6118 if (bmap)
6119 isl_basic_map_free(bmap);
6120 return NULL;
6123 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6125 int i;
6127 if (!map)
6128 return NULL;
6130 if (--map->ref > 0)
6131 return NULL;
6133 clear_caches(map);
6134 isl_ctx_deref(map->ctx);
6135 for (i = 0; i < map->n; ++i)
6136 isl_basic_map_free(map->p[i]);
6137 isl_space_free(map->dim);
6138 free(map);
6140 return NULL;
6143 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6144 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6146 int j;
6148 bmap = isl_basic_map_cow(bmap);
6149 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6150 j = isl_basic_map_alloc_equality(bmap);
6151 if (j < 0)
6152 goto error;
6153 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6154 isl_int_set_si(bmap->eq[j][pos], -1);
6155 isl_int_set_si(bmap->eq[j][0], value);
6156 bmap = isl_basic_map_simplify(bmap);
6157 return isl_basic_map_finalize(bmap);
6158 error:
6159 isl_basic_map_free(bmap);
6160 return NULL;
6163 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6164 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6166 int j;
6168 bmap = isl_basic_map_cow(bmap);
6169 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6170 j = isl_basic_map_alloc_equality(bmap);
6171 if (j < 0)
6172 goto error;
6173 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6174 isl_int_set_si(bmap->eq[j][pos], -1);
6175 isl_int_set(bmap->eq[j][0], value);
6176 bmap = isl_basic_map_simplify(bmap);
6177 return isl_basic_map_finalize(bmap);
6178 error:
6179 isl_basic_map_free(bmap);
6180 return NULL;
6183 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6184 enum isl_dim_type type, unsigned pos, int value)
6186 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6187 return isl_basic_map_free(bmap);
6188 return isl_basic_map_fix_pos_si(bmap,
6189 isl_basic_map_offset(bmap, type) + pos, value);
6192 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6193 enum isl_dim_type type, unsigned pos, isl_int value)
6195 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6196 return isl_basic_map_free(bmap);
6197 return isl_basic_map_fix_pos(bmap,
6198 isl_basic_map_offset(bmap, type) + pos, value);
6201 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6202 * to be equal to "v".
6204 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6205 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6207 if (!bmap || !v)
6208 goto error;
6209 if (!isl_val_is_int(v))
6210 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6211 "expecting integer value", goto error);
6212 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6213 goto error;
6214 pos += isl_basic_map_offset(bmap, type);
6215 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6216 isl_val_free(v);
6217 return bmap;
6218 error:
6219 isl_basic_map_free(bmap);
6220 isl_val_free(v);
6221 return NULL;
6224 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6225 * to be equal to "v".
6227 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6228 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6230 return isl_basic_map_fix_val(bset, type, pos, v);
6233 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6234 enum isl_dim_type type, unsigned pos, int value)
6236 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6237 type, pos, value));
6240 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6241 enum isl_dim_type type, unsigned pos, isl_int value)
6243 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6244 type, pos, value));
6247 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6248 unsigned input, int value)
6250 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6253 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6254 unsigned dim, int value)
6256 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6257 isl_dim_set, dim, value));
6260 /* Remove the basic map at position "i" from "map" if this basic map
6261 * is (obviously) empty.
6263 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6265 isl_bool empty;
6267 if (!map)
6268 return NULL;
6270 empty = isl_basic_map_plain_is_empty(map->p[i]);
6271 if (empty < 0)
6272 return isl_map_free(map);
6273 if (!empty)
6274 return map;
6276 isl_basic_map_free(map->p[i]);
6277 map->n--;
6278 if (i != map->n) {
6279 map->p[i] = map->p[map->n];
6280 map = isl_map_unmark_normalized(map);
6284 return map;
6287 /* Perform "fn" on each basic map of "map", where we may not be holding
6288 * the only reference to "map".
6289 * In particular, "fn" should be a semantics preserving operation
6290 * that we want to apply to all copies of "map". We therefore need
6291 * to be careful not to modify "map" in a way that breaks "map"
6292 * in case anything goes wrong.
6294 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6295 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6297 struct isl_basic_map *bmap;
6298 int i;
6300 if (!map)
6301 return NULL;
6303 for (i = map->n - 1; i >= 0; --i) {
6304 bmap = isl_basic_map_copy(map->p[i]);
6305 bmap = fn(bmap);
6306 if (!bmap)
6307 goto error;
6308 isl_basic_map_free(map->p[i]);
6309 map->p[i] = bmap;
6310 map = remove_if_empty(map, i);
6311 if (!map)
6312 return NULL;
6315 return map;
6316 error:
6317 isl_map_free(map);
6318 return NULL;
6321 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6322 enum isl_dim_type type, unsigned pos, int value)
6324 int i;
6326 map = isl_map_cow(map);
6327 if (!map)
6328 return NULL;
6330 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6331 for (i = map->n - 1; i >= 0; --i) {
6332 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6333 map = remove_if_empty(map, i);
6334 if (!map)
6335 return NULL;
6337 map = isl_map_unmark_normalized(map);
6338 return map;
6339 error:
6340 isl_map_free(map);
6341 return NULL;
6344 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6345 enum isl_dim_type type, unsigned pos, int value)
6347 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6350 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6351 enum isl_dim_type type, unsigned pos, isl_int value)
6353 int i;
6355 map = isl_map_cow(map);
6356 if (!map)
6357 return NULL;
6359 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6360 for (i = 0; i < map->n; ++i) {
6361 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6362 if (!map->p[i])
6363 goto error;
6365 map = isl_map_unmark_normalized(map);
6366 return map;
6367 error:
6368 isl_map_free(map);
6369 return NULL;
6372 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6373 enum isl_dim_type type, unsigned pos, isl_int value)
6375 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6378 /* Fix the value of the variable at position "pos" of type "type" of "map"
6379 * to be equal to "v".
6381 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6382 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6384 int i;
6386 map = isl_map_cow(map);
6387 if (!map || !v)
6388 goto error;
6390 if (!isl_val_is_int(v))
6391 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6392 "expecting integer value", goto error);
6393 if (pos >= isl_map_dim(map, type))
6394 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6395 "index out of bounds", goto error);
6396 for (i = map->n - 1; i >= 0; --i) {
6397 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6398 isl_val_copy(v));
6399 map = remove_if_empty(map, i);
6400 if (!map)
6401 goto error;
6403 map = isl_map_unmark_normalized(map);
6404 isl_val_free(v);
6405 return map;
6406 error:
6407 isl_map_free(map);
6408 isl_val_free(v);
6409 return NULL;
6412 /* Fix the value of the variable at position "pos" of type "type" of "set"
6413 * to be equal to "v".
6415 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6416 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6418 return isl_map_fix_val(set, type, pos, v);
6421 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6422 unsigned input, int value)
6424 return isl_map_fix_si(map, isl_dim_in, input, value);
6427 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6429 return set_from_map(isl_map_fix_si(set_to_map(set),
6430 isl_dim_set, dim, value));
6433 static __isl_give isl_basic_map *basic_map_bound_si(
6434 __isl_take isl_basic_map *bmap,
6435 enum isl_dim_type type, unsigned pos, int value, int upper)
6437 int j;
6439 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6440 return isl_basic_map_free(bmap);
6441 pos += isl_basic_map_offset(bmap, type);
6442 bmap = isl_basic_map_cow(bmap);
6443 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6444 j = isl_basic_map_alloc_inequality(bmap);
6445 if (j < 0)
6446 goto error;
6447 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6448 if (upper) {
6449 isl_int_set_si(bmap->ineq[j][pos], -1);
6450 isl_int_set_si(bmap->ineq[j][0], value);
6451 } else {
6452 isl_int_set_si(bmap->ineq[j][pos], 1);
6453 isl_int_set_si(bmap->ineq[j][0], -value);
6455 bmap = isl_basic_map_simplify(bmap);
6456 return isl_basic_map_finalize(bmap);
6457 error:
6458 isl_basic_map_free(bmap);
6459 return NULL;
6462 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6463 __isl_take isl_basic_map *bmap,
6464 enum isl_dim_type type, unsigned pos, int value)
6466 return basic_map_bound_si(bmap, type, pos, value, 0);
6469 /* Constrain the values of the given dimension to be no greater than "value".
6471 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6472 __isl_take isl_basic_map *bmap,
6473 enum isl_dim_type type, unsigned pos, int value)
6475 return basic_map_bound_si(bmap, type, pos, value, 1);
6478 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6479 enum isl_dim_type type, unsigned pos, int value, int upper)
6481 int i;
6483 map = isl_map_cow(map);
6484 if (!map)
6485 return NULL;
6487 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6488 for (i = 0; i < map->n; ++i) {
6489 map->p[i] = basic_map_bound_si(map->p[i],
6490 type, pos, value, upper);
6491 if (!map->p[i])
6492 goto error;
6494 map = isl_map_unmark_normalized(map);
6495 return map;
6496 error:
6497 isl_map_free(map);
6498 return NULL;
6501 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6502 enum isl_dim_type type, unsigned pos, int value)
6504 return map_bound_si(map, type, pos, value, 0);
6507 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6508 enum isl_dim_type type, unsigned pos, int value)
6510 return map_bound_si(map, type, pos, value, 1);
6513 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6514 enum isl_dim_type type, unsigned pos, int value)
6516 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6517 type, pos, value));
6520 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6521 enum isl_dim_type type, unsigned pos, int value)
6523 return isl_map_upper_bound_si(set, type, pos, value);
6526 /* Bound the given variable of "bmap" from below (or above is "upper"
6527 * is set) to "value".
6529 static __isl_give isl_basic_map *basic_map_bound(
6530 __isl_take isl_basic_map *bmap,
6531 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6533 int j;
6535 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6536 return isl_basic_map_free(bmap);
6537 pos += isl_basic_map_offset(bmap, type);
6538 bmap = isl_basic_map_cow(bmap);
6539 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6540 j = isl_basic_map_alloc_inequality(bmap);
6541 if (j < 0)
6542 goto error;
6543 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6544 if (upper) {
6545 isl_int_set_si(bmap->ineq[j][pos], -1);
6546 isl_int_set(bmap->ineq[j][0], value);
6547 } else {
6548 isl_int_set_si(bmap->ineq[j][pos], 1);
6549 isl_int_neg(bmap->ineq[j][0], value);
6551 bmap = isl_basic_map_simplify(bmap);
6552 return isl_basic_map_finalize(bmap);
6553 error:
6554 isl_basic_map_free(bmap);
6555 return NULL;
6558 /* Bound the given variable of "map" from below (or above is "upper"
6559 * is set) to "value".
6561 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6562 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6564 int i;
6566 map = isl_map_cow(map);
6567 if (!map)
6568 return NULL;
6570 if (pos >= isl_map_dim(map, type))
6571 isl_die(map->ctx, isl_error_invalid,
6572 "index out of bounds", goto error);
6573 for (i = map->n - 1; i >= 0; --i) {
6574 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6575 map = remove_if_empty(map, i);
6576 if (!map)
6577 return NULL;
6579 map = isl_map_unmark_normalized(map);
6580 return map;
6581 error:
6582 isl_map_free(map);
6583 return NULL;
6586 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6587 enum isl_dim_type type, unsigned pos, isl_int value)
6589 return map_bound(map, type, pos, value, 0);
6592 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6593 enum isl_dim_type type, unsigned pos, isl_int value)
6595 return map_bound(map, type, pos, value, 1);
6598 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6599 enum isl_dim_type type, unsigned pos, isl_int value)
6601 return isl_map_lower_bound(set, type, pos, value);
6604 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6605 enum isl_dim_type type, unsigned pos, isl_int value)
6607 return isl_map_upper_bound(set, type, pos, value);
6610 /* Force the values of the variable at position "pos" of type "type" of "set"
6611 * to be no smaller than "value".
6613 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6614 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6616 if (!value)
6617 goto error;
6618 if (!isl_val_is_int(value))
6619 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6620 "expecting integer value", goto error);
6621 set = isl_set_lower_bound(set, type, pos, value->n);
6622 isl_val_free(value);
6623 return set;
6624 error:
6625 isl_val_free(value);
6626 isl_set_free(set);
6627 return NULL;
6630 /* Force the values of the variable at position "pos" of type "type" of "set"
6631 * to be no greater than "value".
6633 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6634 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6636 if (!value)
6637 goto error;
6638 if (!isl_val_is_int(value))
6639 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6640 "expecting integer value", goto error);
6641 set = isl_set_upper_bound(set, type, pos, value->n);
6642 isl_val_free(value);
6643 return set;
6644 error:
6645 isl_val_free(value);
6646 isl_set_free(set);
6647 return NULL;
6650 /* Bound the given variable of "bset" from below (or above is "upper"
6651 * is set) to "value".
6653 static __isl_give isl_basic_set *isl_basic_set_bound(
6654 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6655 isl_int value, int upper)
6657 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6658 type, pos, value, upper));
6661 /* Bound the given variable of "bset" from below (or above is "upper"
6662 * is set) to "value".
6664 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6665 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6666 __isl_take isl_val *value, int upper)
6668 if (!value)
6669 goto error;
6670 if (!isl_val_is_int(value))
6671 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6672 "expecting integer value", goto error);
6673 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6674 isl_val_free(value);
6675 return bset;
6676 error:
6677 isl_val_free(value);
6678 isl_basic_set_free(bset);
6679 return NULL;
6682 /* Bound the given variable of "bset" from below to "value".
6684 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6685 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6686 __isl_take isl_val *value)
6688 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6691 /* Bound the given variable of "bset" from above to "value".
6693 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6694 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6695 __isl_take isl_val *value)
6697 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6700 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6702 int i;
6704 map = isl_map_cow(map);
6705 if (!map)
6706 return NULL;
6708 map->dim = isl_space_reverse(map->dim);
6709 if (!map->dim)
6710 goto error;
6711 for (i = 0; i < map->n; ++i) {
6712 map->p[i] = isl_basic_map_reverse(map->p[i]);
6713 if (!map->p[i])
6714 goto error;
6716 map = isl_map_unmark_normalized(map);
6717 return map;
6718 error:
6719 isl_map_free(map);
6720 return NULL;
6723 #undef TYPE
6724 #define TYPE isl_pw_multi_aff
6725 #undef SUFFIX
6726 #define SUFFIX _pw_multi_aff
6727 #undef EMPTY
6728 #define EMPTY isl_pw_multi_aff_empty
6729 #undef ADD
6730 #define ADD isl_pw_multi_aff_union_add
6731 #include "isl_map_lexopt_templ.c"
6733 /* Given a map "map", compute the lexicographically minimal
6734 * (or maximal) image element for each domain element in dom,
6735 * in the form of an isl_pw_multi_aff.
6736 * If "empty" is not NULL, then set *empty to those elements in dom that
6737 * do not have an image element.
6738 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6739 * should be computed over the domain of "map". "empty" is also NULL
6740 * in this case.
6742 * We first compute the lexicographically minimal or maximal element
6743 * in the first basic map. This results in a partial solution "res"
6744 * and a subset "todo" of dom that still need to be handled.
6745 * We then consider each of the remaining maps in "map" and successively
6746 * update both "res" and "todo".
6747 * If "empty" is NULL, then the todo sets are not needed and therefore
6748 * also not computed.
6750 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6751 __isl_take isl_map *map, __isl_take isl_set *dom,
6752 __isl_give isl_set **empty, unsigned flags)
6754 int i;
6755 int full;
6756 isl_pw_multi_aff *res;
6757 isl_set *todo;
6759 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6760 if (!map || (!full && !dom))
6761 goto error;
6763 if (isl_map_plain_is_empty(map)) {
6764 if (empty)
6765 *empty = dom;
6766 else
6767 isl_set_free(dom);
6768 return isl_pw_multi_aff_from_map(map);
6771 res = basic_map_partial_lexopt_pw_multi_aff(
6772 isl_basic_map_copy(map->p[0]),
6773 isl_set_copy(dom), empty, flags);
6775 if (empty)
6776 todo = *empty;
6777 for (i = 1; i < map->n; ++i) {
6778 isl_pw_multi_aff *res_i;
6780 res_i = basic_map_partial_lexopt_pw_multi_aff(
6781 isl_basic_map_copy(map->p[i]),
6782 isl_set_copy(dom), empty, flags);
6784 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6785 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6786 else
6787 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6789 if (empty)
6790 todo = isl_set_intersect(todo, *empty);
6793 isl_set_free(dom);
6794 isl_map_free(map);
6796 if (empty)
6797 *empty = todo;
6799 return res;
6800 error:
6801 if (empty)
6802 *empty = NULL;
6803 isl_set_free(dom);
6804 isl_map_free(map);
6805 return NULL;
6808 #undef TYPE
6809 #define TYPE isl_map
6810 #undef SUFFIX
6811 #define SUFFIX
6812 #undef EMPTY
6813 #define EMPTY isl_map_empty
6814 #undef ADD
6815 #define ADD isl_map_union_disjoint
6816 #include "isl_map_lexopt_templ.c"
6818 /* Given a map "map", compute the lexicographically minimal
6819 * (or maximal) image element for each domain element in "dom",
6820 * in the form of an isl_map.
6821 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6822 * do not have an image element.
6823 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6824 * should be computed over the domain of "map". "empty" is also NULL
6825 * in this case.
6827 * If the input consists of more than one disjunct, then first
6828 * compute the desired result in the form of an isl_pw_multi_aff and
6829 * then convert that into an isl_map.
6831 * This function used to have an explicit implementation in terms
6832 * of isl_maps, but it would continually intersect the domains of
6833 * partial results with the complement of the domain of the next
6834 * partial solution, potentially leading to an explosion in the number
6835 * of disjuncts if there are several disjuncts in the input.
6836 * An even earlier implementation of this function would look for
6837 * better results in the domain of the partial result and for extra
6838 * results in the complement of this domain, which would lead to
6839 * even more splintering.
6841 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6842 __isl_take isl_map *map, __isl_take isl_set *dom,
6843 __isl_give isl_set **empty, unsigned flags)
6845 int full;
6846 struct isl_map *res;
6847 isl_pw_multi_aff *pma;
6849 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6850 if (!map || (!full && !dom))
6851 goto error;
6853 if (isl_map_plain_is_empty(map)) {
6854 if (empty)
6855 *empty = dom;
6856 else
6857 isl_set_free(dom);
6858 return map;
6861 if (map->n == 1) {
6862 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6863 dom, empty, flags);
6864 isl_map_free(map);
6865 return res;
6868 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6869 flags);
6870 return isl_map_from_pw_multi_aff(pma);
6871 error:
6872 if (empty)
6873 *empty = NULL;
6874 isl_set_free(dom);
6875 isl_map_free(map);
6876 return NULL;
6879 __isl_give isl_map *isl_map_partial_lexmax(
6880 __isl_take isl_map *map, __isl_take isl_set *dom,
6881 __isl_give isl_set **empty)
6883 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6886 __isl_give isl_map *isl_map_partial_lexmin(
6887 __isl_take isl_map *map, __isl_take isl_set *dom,
6888 __isl_give isl_set **empty)
6890 return isl_map_partial_lexopt(map, dom, empty, 0);
6893 __isl_give isl_set *isl_set_partial_lexmin(
6894 __isl_take isl_set *set, __isl_take isl_set *dom,
6895 __isl_give isl_set **empty)
6897 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6898 dom, empty));
6901 __isl_give isl_set *isl_set_partial_lexmax(
6902 __isl_take isl_set *set, __isl_take isl_set *dom,
6903 __isl_give isl_set **empty)
6905 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6906 dom, empty));
6909 /* Compute the lexicographic minimum (or maximum if "flags" includes
6910 * ISL_OPT_MAX) of "bset" over its parametric domain.
6912 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6913 unsigned flags)
6915 return isl_basic_map_lexopt(bset, flags);
6918 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6920 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6923 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6925 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6928 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6930 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6933 /* Compute the lexicographic minimum of "bset" over its parametric domain
6934 * for the purpose of quantifier elimination.
6935 * That is, find an explicit representation for all the existentially
6936 * quantified variables in "bset" by computing their lexicographic
6937 * minimum.
6939 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6940 __isl_take isl_basic_set *bset)
6942 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6945 /* Given a basic map with one output dimension, compute the minimum or
6946 * maximum of that dimension as an isl_pw_aff.
6948 * Compute the optimum as a lexicographic optimum over the single
6949 * output dimension and extract the single isl_pw_aff from the result.
6951 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6952 int max)
6954 isl_pw_multi_aff *pma;
6955 isl_pw_aff *pwaff;
6957 bmap = isl_basic_map_copy(bmap);
6958 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6959 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6960 isl_pw_multi_aff_free(pma);
6962 return pwaff;
6965 /* Compute the minimum or maximum of the given output dimension
6966 * as a function of the parameters and the input dimensions,
6967 * but independently of the other output dimensions.
6969 * We first project out the other output dimension and then compute
6970 * the "lexicographic" maximum in each basic map, combining the results
6971 * using isl_pw_aff_union_max.
6973 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6974 int max)
6976 int i;
6977 isl_pw_aff *pwaff;
6978 unsigned n_out;
6980 n_out = isl_map_dim(map, isl_dim_out);
6981 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6982 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6983 if (!map)
6984 return NULL;
6986 if (map->n == 0) {
6987 isl_space *space = isl_map_get_space(map);
6988 isl_map_free(map);
6989 return isl_pw_aff_empty(space);
6992 pwaff = basic_map_dim_opt(map->p[0], max);
6993 for (i = 1; i < map->n; ++i) {
6994 isl_pw_aff *pwaff_i;
6996 pwaff_i = basic_map_dim_opt(map->p[i], max);
6997 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7000 isl_map_free(map);
7002 return pwaff;
7005 /* Compute the minimum of the given output dimension as a function of the
7006 * parameters and input dimensions, but independently of
7007 * the other output dimensions.
7009 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7011 return map_dim_opt(map, pos, 0);
7014 /* Compute the maximum of the given output dimension as a function of the
7015 * parameters and input dimensions, but independently of
7016 * the other output dimensions.
7018 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7020 return map_dim_opt(map, pos, 1);
7023 /* Compute the minimum or maximum of the given set dimension
7024 * as a function of the parameters,
7025 * but independently of the other set dimensions.
7027 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7028 int max)
7030 return map_dim_opt(set, pos, max);
7033 /* Compute the maximum of the given set dimension as a function of the
7034 * parameters, but independently of the other set dimensions.
7036 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7038 return set_dim_opt(set, pos, 1);
7041 /* Compute the minimum of the given set dimension as a function of the
7042 * parameters, but independently of the other set dimensions.
7044 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7046 return set_dim_opt(set, pos, 0);
7049 /* Apply a preimage specified by "mat" on the parameters of "bset".
7050 * bset is assumed to have only parameters and divs.
7052 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7053 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7055 unsigned nparam;
7057 if (!bset || !mat)
7058 goto error;
7060 bset->dim = isl_space_cow(bset->dim);
7061 if (!bset->dim)
7062 goto error;
7064 nparam = isl_basic_set_dim(bset, isl_dim_param);
7066 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7068 bset->dim->nparam = 0;
7069 bset->dim->n_out = nparam;
7070 bset = isl_basic_set_preimage(bset, mat);
7071 if (bset) {
7072 bset->dim->nparam = bset->dim->n_out;
7073 bset->dim->n_out = 0;
7075 return bset;
7076 error:
7077 isl_mat_free(mat);
7078 isl_basic_set_free(bset);
7079 return NULL;
7082 /* Apply a preimage specified by "mat" on the parameters of "set".
7083 * set is assumed to have only parameters and divs.
7085 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7086 __isl_take isl_mat *mat)
7088 isl_space *space;
7089 unsigned nparam;
7091 if (!set || !mat)
7092 goto error;
7094 nparam = isl_set_dim(set, isl_dim_param);
7096 if (mat->n_row != 1 + nparam)
7097 isl_die(isl_set_get_ctx(set), isl_error_internal,
7098 "unexpected number of rows", goto error);
7100 space = isl_set_get_space(set);
7101 space = isl_space_move_dims(space, isl_dim_set, 0,
7102 isl_dim_param, 0, nparam);
7103 set = isl_set_reset_space(set, space);
7104 set = isl_set_preimage(set, mat);
7105 nparam = isl_set_dim(set, isl_dim_out);
7106 space = isl_set_get_space(set);
7107 space = isl_space_move_dims(space, isl_dim_param, 0,
7108 isl_dim_out, 0, nparam);
7109 set = isl_set_reset_space(set, space);
7110 return set;
7111 error:
7112 isl_mat_free(mat);
7113 isl_set_free(set);
7114 return NULL;
7117 /* Intersect the basic set "bset" with the affine space specified by the
7118 * equalities in "eq".
7120 static __isl_give isl_basic_set *basic_set_append_equalities(
7121 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7123 int i, k;
7124 unsigned len;
7126 if (!bset || !eq)
7127 goto error;
7129 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7130 eq->n_row, 0);
7131 if (!bset)
7132 goto error;
7134 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7135 for (i = 0; i < eq->n_row; ++i) {
7136 k = isl_basic_set_alloc_equality(bset);
7137 if (k < 0)
7138 goto error;
7139 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7140 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7142 isl_mat_free(eq);
7144 bset = isl_basic_set_gauss(bset, NULL);
7145 bset = isl_basic_set_finalize(bset);
7147 return bset;
7148 error:
7149 isl_mat_free(eq);
7150 isl_basic_set_free(bset);
7151 return NULL;
7154 /* Intersect the set "set" with the affine space specified by the
7155 * equalities in "eq".
7157 static struct isl_set *set_append_equalities(struct isl_set *set,
7158 struct isl_mat *eq)
7160 int i;
7162 if (!set || !eq)
7163 goto error;
7165 for (i = 0; i < set->n; ++i) {
7166 set->p[i] = basic_set_append_equalities(set->p[i],
7167 isl_mat_copy(eq));
7168 if (!set->p[i])
7169 goto error;
7171 isl_mat_free(eq);
7172 return set;
7173 error:
7174 isl_mat_free(eq);
7175 isl_set_free(set);
7176 return NULL;
7179 /* Given a basic set "bset" that only involves parameters and existentially
7180 * quantified variables, return the index of the first equality
7181 * that only involves parameters. If there is no such equality then
7182 * return bset->n_eq.
7184 * This function assumes that isl_basic_set_gauss has been called on "bset".
7186 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7188 int i, j;
7189 unsigned nparam, n_div;
7191 if (!bset)
7192 return -1;
7194 nparam = isl_basic_set_dim(bset, isl_dim_param);
7195 n_div = isl_basic_set_dim(bset, isl_dim_div);
7197 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7198 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7199 ++i;
7202 return i;
7205 /* Compute an explicit representation for the existentially quantified
7206 * variables in "bset" by computing the "minimal value" of the set
7207 * variables. Since there are no set variables, the computation of
7208 * the minimal value essentially computes an explicit representation
7209 * of the non-empty part(s) of "bset".
7211 * The input only involves parameters and existentially quantified variables.
7212 * All equalities among parameters have been removed.
7214 * Since the existentially quantified variables in the result are in general
7215 * going to be different from those in the input, we first replace
7216 * them by the minimal number of variables based on their equalities.
7217 * This should simplify the parametric integer programming.
7219 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7221 isl_morph *morph1, *morph2;
7222 isl_set *set;
7223 unsigned n;
7225 if (!bset)
7226 return NULL;
7227 if (bset->n_eq == 0)
7228 return isl_basic_set_lexmin_compute_divs(bset);
7230 morph1 = isl_basic_set_parameter_compression(bset);
7231 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7232 bset = isl_basic_set_lift(bset);
7233 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7234 bset = isl_morph_basic_set(morph2, bset);
7235 n = isl_basic_set_dim(bset, isl_dim_set);
7236 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7238 set = isl_basic_set_lexmin_compute_divs(bset);
7240 set = isl_morph_set(isl_morph_inverse(morph1), set);
7242 return set;
7245 /* Project the given basic set onto its parameter domain, possibly introducing
7246 * new, explicit, existential variables in the constraints.
7247 * The input has parameters and (possibly implicit) existential variables.
7248 * The output has the same parameters, but only
7249 * explicit existentially quantified variables.
7251 * The actual projection is performed by pip, but pip doesn't seem
7252 * to like equalities very much, so we first remove the equalities
7253 * among the parameters by performing a variable compression on
7254 * the parameters. Afterward, an inverse transformation is performed
7255 * and the equalities among the parameters are inserted back in.
7257 * The variable compression on the parameters may uncover additional
7258 * equalities that were only implicit before. We therefore check
7259 * if there are any new parameter equalities in the result and
7260 * if so recurse. The removal of parameter equalities is required
7261 * for the parameter compression performed by base_compute_divs.
7263 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7265 int i;
7266 struct isl_mat *eq;
7267 struct isl_mat *T, *T2;
7268 struct isl_set *set;
7269 unsigned nparam;
7271 bset = isl_basic_set_cow(bset);
7272 if (!bset)
7273 return NULL;
7275 if (bset->n_eq == 0)
7276 return base_compute_divs(bset);
7278 bset = isl_basic_set_gauss(bset, NULL);
7279 if (!bset)
7280 return NULL;
7281 if (isl_basic_set_plain_is_empty(bset))
7282 return isl_set_from_basic_set(bset);
7284 i = first_parameter_equality(bset);
7285 if (i == bset->n_eq)
7286 return base_compute_divs(bset);
7288 nparam = isl_basic_set_dim(bset, isl_dim_param);
7289 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7290 0, 1 + nparam);
7291 eq = isl_mat_cow(eq);
7292 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7293 if (T && T->n_col == 0) {
7294 isl_mat_free(T);
7295 isl_mat_free(T2);
7296 isl_mat_free(eq);
7297 bset = isl_basic_set_set_to_empty(bset);
7298 return isl_set_from_basic_set(bset);
7300 bset = basic_set_parameter_preimage(bset, T);
7302 i = first_parameter_equality(bset);
7303 if (!bset)
7304 set = NULL;
7305 else if (i == bset->n_eq)
7306 set = base_compute_divs(bset);
7307 else
7308 set = parameter_compute_divs(bset);
7309 set = set_parameter_preimage(set, T2);
7310 set = set_append_equalities(set, eq);
7311 return set;
7314 /* Insert the divs from "ls" before those of "bmap".
7316 * The number of columns is not changed, which means that the last
7317 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7318 * The caller is responsible for removing the same number of dimensions
7319 * from the space of "bmap".
7321 static __isl_give isl_basic_map *insert_divs_from_local_space(
7322 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7324 int i;
7325 int n_div;
7326 int old_n_div;
7328 n_div = isl_local_space_dim(ls, isl_dim_div);
7329 if (n_div == 0)
7330 return bmap;
7332 old_n_div = bmap->n_div;
7333 bmap = insert_div_rows(bmap, n_div);
7334 if (!bmap)
7335 return NULL;
7337 for (i = 0; i < n_div; ++i) {
7338 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7339 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7342 return bmap;
7345 /* Replace the space of "bmap" by the space and divs of "ls".
7347 * If "ls" has any divs, then we simplify the result since we may
7348 * have discovered some additional equalities that could simplify
7349 * the div expressions.
7351 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7352 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7354 int n_div;
7356 bmap = isl_basic_map_cow(bmap);
7357 if (!bmap || !ls)
7358 goto error;
7360 n_div = isl_local_space_dim(ls, isl_dim_div);
7361 bmap = insert_divs_from_local_space(bmap, ls);
7362 if (!bmap)
7363 goto error;
7365 isl_space_free(bmap->dim);
7366 bmap->dim = isl_local_space_get_space(ls);
7367 if (!bmap->dim)
7368 goto error;
7370 isl_local_space_free(ls);
7371 if (n_div > 0)
7372 bmap = isl_basic_map_simplify(bmap);
7373 bmap = isl_basic_map_finalize(bmap);
7374 return bmap;
7375 error:
7376 isl_basic_map_free(bmap);
7377 isl_local_space_free(ls);
7378 return NULL;
7381 /* Replace the space of "map" by the space and divs of "ls".
7383 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7384 __isl_take isl_local_space *ls)
7386 int i;
7388 map = isl_map_cow(map);
7389 if (!map || !ls)
7390 goto error;
7392 for (i = 0; i < map->n; ++i) {
7393 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7394 isl_local_space_copy(ls));
7395 if (!map->p[i])
7396 goto error;
7398 isl_space_free(map->dim);
7399 map->dim = isl_local_space_get_space(ls);
7400 if (!map->dim)
7401 goto error;
7403 isl_local_space_free(ls);
7404 return map;
7405 error:
7406 isl_local_space_free(ls);
7407 isl_map_free(map);
7408 return NULL;
7411 /* Compute an explicit representation for the existentially
7412 * quantified variables for which do not know any explicit representation yet.
7414 * We first sort the existentially quantified variables so that the
7415 * existentially quantified variables for which we already have an explicit
7416 * representation are placed before those for which we do not.
7417 * The input dimensions, the output dimensions and the existentially
7418 * quantified variables for which we already have an explicit
7419 * representation are then turned into parameters.
7420 * compute_divs returns a map with the same parameters and
7421 * no input or output dimensions and the dimension specification
7422 * is reset to that of the input, including the existentially quantified
7423 * variables for which we already had an explicit representation.
7425 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7427 struct isl_basic_set *bset;
7428 struct isl_set *set;
7429 struct isl_map *map;
7430 isl_space *space;
7431 isl_local_space *ls;
7432 unsigned nparam;
7433 unsigned n_in;
7434 unsigned n_out;
7435 int n_known;
7436 int i;
7438 bmap = isl_basic_map_sort_divs(bmap);
7439 bmap = isl_basic_map_cow(bmap);
7440 if (!bmap)
7441 return NULL;
7443 n_known = isl_basic_map_first_unknown_div(bmap);
7444 if (n_known < 0)
7445 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7447 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7448 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7449 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7450 space = isl_space_set_alloc(bmap->ctx,
7451 nparam + n_in + n_out + n_known, 0);
7452 if (!space)
7453 goto error;
7455 ls = isl_basic_map_get_local_space(bmap);
7456 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7457 n_known, bmap->n_div - n_known);
7458 if (n_known > 0) {
7459 for (i = n_known; i < bmap->n_div; ++i)
7460 swap_div(bmap, i - n_known, i);
7461 bmap->n_div -= n_known;
7462 bmap->extra -= n_known;
7464 bmap = isl_basic_map_reset_space(bmap, space);
7465 bset = bset_from_bmap(bmap);
7467 set = parameter_compute_divs(bset);
7468 map = set_to_map(set);
7469 map = replace_space_by_local_space(map, ls);
7471 return map;
7472 error:
7473 isl_basic_map_free(bmap);
7474 return NULL;
7477 /* Remove the explicit representation of local variable "div",
7478 * if there is any.
7480 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7481 __isl_take isl_basic_map *bmap, int div)
7483 isl_bool unknown;
7485 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7486 if (unknown < 0)
7487 return isl_basic_map_free(bmap);
7488 if (unknown)
7489 return bmap;
7491 bmap = isl_basic_map_cow(bmap);
7492 if (!bmap)
7493 return NULL;
7494 isl_int_set_si(bmap->div[div][0], 0);
7495 return bmap;
7498 /* Is local variable "div" of "bmap" marked as not having an explicit
7499 * representation?
7500 * Note that even if "div" is not marked in this way and therefore
7501 * has an explicit representation, this representation may still
7502 * depend (indirectly) on other local variables that do not
7503 * have an explicit representation.
7505 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7506 int div)
7508 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7509 return isl_bool_error;
7510 return isl_int_is_zero(bmap->div[div][0]);
7513 /* Return the position of the first local variable that does not
7514 * have an explicit representation.
7515 * Return the total number of local variables if they all have
7516 * an explicit representation.
7517 * Return -1 on error.
7519 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7521 int i;
7523 if (!bmap)
7524 return -1;
7526 for (i = 0; i < bmap->n_div; ++i) {
7527 if (!isl_basic_map_div_is_known(bmap, i))
7528 return i;
7530 return bmap->n_div;
7533 /* Return the position of the first local variable that does not
7534 * have an explicit representation.
7535 * Return the total number of local variables if they all have
7536 * an explicit representation.
7537 * Return -1 on error.
7539 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7541 return isl_basic_map_first_unknown_div(bset);
7544 /* Does "bmap" have an explicit representation for all local variables?
7546 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7548 int first, n;
7550 n = isl_basic_map_dim(bmap, isl_dim_div);
7551 first = isl_basic_map_first_unknown_div(bmap);
7552 if (first < 0)
7553 return isl_bool_error;
7554 return first == n;
7557 /* Do all basic maps in "map" have an explicit representation
7558 * for all local variables?
7560 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7562 int i;
7564 if (!map)
7565 return isl_bool_error;
7567 for (i = 0; i < map->n; ++i) {
7568 int known = isl_basic_map_divs_known(map->p[i]);
7569 if (known <= 0)
7570 return known;
7573 return isl_bool_true;
7576 /* If bmap contains any unknown divs, then compute explicit
7577 * expressions for them. However, this computation may be
7578 * quite expensive, so first try to remove divs that aren't
7579 * strictly needed.
7581 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7583 int known;
7584 struct isl_map *map;
7586 known = isl_basic_map_divs_known(bmap);
7587 if (known < 0)
7588 goto error;
7589 if (known)
7590 return isl_map_from_basic_map(bmap);
7592 bmap = isl_basic_map_drop_redundant_divs(bmap);
7594 known = isl_basic_map_divs_known(bmap);
7595 if (known < 0)
7596 goto error;
7597 if (known)
7598 return isl_map_from_basic_map(bmap);
7600 map = compute_divs(bmap);
7601 return map;
7602 error:
7603 isl_basic_map_free(bmap);
7604 return NULL;
7607 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7609 int i;
7610 int known;
7611 struct isl_map *res;
7613 if (!map)
7614 return NULL;
7615 if (map->n == 0)
7616 return map;
7618 known = isl_map_divs_known(map);
7619 if (known < 0) {
7620 isl_map_free(map);
7621 return NULL;
7623 if (known)
7624 return map;
7626 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7627 for (i = 1 ; i < map->n; ++i) {
7628 struct isl_map *r2;
7629 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7630 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7631 res = isl_map_union_disjoint(res, r2);
7632 else
7633 res = isl_map_union(res, r2);
7635 isl_map_free(map);
7637 return res;
7640 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7642 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7645 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7647 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7650 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7652 int i;
7653 struct isl_set *set;
7655 if (!map)
7656 goto error;
7658 map = isl_map_cow(map);
7659 if (!map)
7660 return NULL;
7662 set = set_from_map(map);
7663 set->dim = isl_space_domain(set->dim);
7664 if (!set->dim)
7665 goto error;
7666 for (i = 0; i < map->n; ++i) {
7667 set->p[i] = isl_basic_map_domain(map->p[i]);
7668 if (!set->p[i])
7669 goto error;
7671 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7672 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7673 return set;
7674 error:
7675 isl_map_free(map);
7676 return NULL;
7679 /* Return the union of "map1" and "map2", where we assume for now that
7680 * "map1" and "map2" are disjoint. Note that the basic maps inside
7681 * "map1" or "map2" may not be disjoint from each other.
7682 * Also note that this function is also called from isl_map_union,
7683 * which takes care of handling the situation where "map1" and "map2"
7684 * may not be disjoint.
7686 * If one of the inputs is empty, we can simply return the other input.
7687 * Similarly, if one of the inputs is universal, then it is equal to the union.
7689 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7690 __isl_take isl_map *map2)
7692 int i;
7693 unsigned flags = 0;
7694 struct isl_map *map = NULL;
7695 int is_universe;
7697 if (!map1 || !map2)
7698 goto error;
7700 if (!isl_space_is_equal(map1->dim, map2->dim))
7701 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7702 "spaces don't match", goto error);
7704 if (map1->n == 0) {
7705 isl_map_free(map1);
7706 return map2;
7708 if (map2->n == 0) {
7709 isl_map_free(map2);
7710 return map1;
7713 is_universe = isl_map_plain_is_universe(map1);
7714 if (is_universe < 0)
7715 goto error;
7716 if (is_universe) {
7717 isl_map_free(map2);
7718 return map1;
7721 is_universe = isl_map_plain_is_universe(map2);
7722 if (is_universe < 0)
7723 goto error;
7724 if (is_universe) {
7725 isl_map_free(map1);
7726 return map2;
7729 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7730 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7731 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7733 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7734 map1->n + map2->n, flags);
7735 if (!map)
7736 goto error;
7737 for (i = 0; i < map1->n; ++i) {
7738 map = isl_map_add_basic_map(map,
7739 isl_basic_map_copy(map1->p[i]));
7740 if (!map)
7741 goto error;
7743 for (i = 0; i < map2->n; ++i) {
7744 map = isl_map_add_basic_map(map,
7745 isl_basic_map_copy(map2->p[i]));
7746 if (!map)
7747 goto error;
7749 isl_map_free(map1);
7750 isl_map_free(map2);
7751 return map;
7752 error:
7753 isl_map_free(map);
7754 isl_map_free(map1);
7755 isl_map_free(map2);
7756 return NULL;
7759 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7760 * guaranteed to be disjoint by the caller.
7762 * Note that this functions is called from within isl_map_make_disjoint,
7763 * so we have to be careful not to touch the constraints of the inputs
7764 * in any way.
7766 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7767 __isl_take isl_map *map2)
7769 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7772 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7773 * not be disjoint. The parameters are assumed to have been aligned.
7775 * We currently simply call map_union_disjoint, the internal operation
7776 * of which does not really depend on the inputs being disjoint.
7777 * If the result contains more than one basic map, then we clear
7778 * the disjoint flag since the result may contain basic maps from
7779 * both inputs and these are not guaranteed to be disjoint.
7781 * As a special case, if "map1" and "map2" are obviously equal,
7782 * then we simply return "map1".
7784 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7785 __isl_take isl_map *map2)
7787 int equal;
7789 if (!map1 || !map2)
7790 goto error;
7792 equal = isl_map_plain_is_equal(map1, map2);
7793 if (equal < 0)
7794 goto error;
7795 if (equal) {
7796 isl_map_free(map2);
7797 return map1;
7800 map1 = map_union_disjoint(map1, map2);
7801 if (!map1)
7802 return NULL;
7803 if (map1->n > 1)
7804 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7805 return map1;
7806 error:
7807 isl_map_free(map1);
7808 isl_map_free(map2);
7809 return NULL;
7812 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7813 * not be disjoint.
7815 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7816 __isl_take isl_map *map2)
7818 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7821 __isl_give isl_set *isl_set_union_disjoint(
7822 __isl_take isl_set *set1, __isl_take isl_set *set2)
7824 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7825 set_to_map(set2)));
7828 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7830 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7833 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7834 * the results.
7836 * "map" and "set" are assumed to be compatible and non-NULL.
7838 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7839 __isl_take isl_set *set,
7840 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7841 __isl_take isl_basic_set *bset))
7843 unsigned flags = 0;
7844 struct isl_map *result;
7845 int i, j;
7847 if (isl_set_plain_is_universe(set)) {
7848 isl_set_free(set);
7849 return map;
7852 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7853 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7854 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7856 result = isl_map_alloc_space(isl_space_copy(map->dim),
7857 map->n * set->n, flags);
7858 for (i = 0; result && i < map->n; ++i)
7859 for (j = 0; j < set->n; ++j) {
7860 result = isl_map_add_basic_map(result,
7861 fn(isl_basic_map_copy(map->p[i]),
7862 isl_basic_set_copy(set->p[j])));
7863 if (!result)
7864 break;
7867 isl_map_free(map);
7868 isl_set_free(set);
7869 return result;
7872 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7873 __isl_take isl_set *set)
7875 isl_bool ok;
7877 ok = isl_map_compatible_range(map, set);
7878 if (ok < 0)
7879 goto error;
7880 if (!ok)
7881 isl_die(set->ctx, isl_error_invalid,
7882 "incompatible spaces", goto error);
7884 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7885 error:
7886 isl_map_free(map);
7887 isl_set_free(set);
7888 return NULL;
7891 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7892 __isl_take isl_set *set)
7894 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7897 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7898 __isl_take isl_set *set)
7900 isl_bool ok;
7902 ok = isl_map_compatible_domain(map, set);
7903 if (ok < 0)
7904 goto error;
7905 if (!ok)
7906 isl_die(set->ctx, isl_error_invalid,
7907 "incompatible spaces", goto error);
7909 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7910 error:
7911 isl_map_free(map);
7912 isl_set_free(set);
7913 return NULL;
7916 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7917 __isl_take isl_set *set)
7919 return isl_map_align_params_map_map_and(map, set,
7920 &map_intersect_domain);
7923 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7924 * in the space B -> C, return the intersection.
7925 * The parameters are assumed to have been aligned.
7927 * The map "factor" is first extended to a map living in the space
7928 * [A -> B] -> C and then a regular intersection is computed.
7930 static __isl_give isl_map *map_intersect_domain_factor_range(
7931 __isl_take isl_map *map, __isl_take isl_map *factor)
7933 isl_space *space;
7934 isl_map *ext_factor;
7936 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7937 ext_factor = isl_map_universe(space);
7938 ext_factor = isl_map_domain_product(ext_factor, factor);
7939 return map_intersect(map, ext_factor);
7942 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7943 * in the space B -> C, return the intersection.
7945 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7946 __isl_take isl_map *map, __isl_take isl_map *factor)
7948 return isl_map_align_params_map_map_and(map, factor,
7949 &map_intersect_domain_factor_range);
7952 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7953 * in the space A -> C, return the intersection.
7955 * The map "factor" is first extended to a map living in the space
7956 * A -> [B -> C] and then a regular intersection is computed.
7958 static __isl_give isl_map *map_intersect_range_factor_range(
7959 __isl_take isl_map *map, __isl_take isl_map *factor)
7961 isl_space *space;
7962 isl_map *ext_factor;
7964 space = isl_space_range_factor_domain(isl_map_get_space(map));
7965 ext_factor = isl_map_universe(space);
7966 ext_factor = isl_map_range_product(ext_factor, factor);
7967 return isl_map_intersect(map, ext_factor);
7970 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7971 * in the space A -> C, return the intersection.
7973 __isl_give isl_map *isl_map_intersect_range_factor_range(
7974 __isl_take isl_map *map, __isl_take isl_map *factor)
7976 return isl_map_align_params_map_map_and(map, factor,
7977 &map_intersect_range_factor_range);
7980 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7981 __isl_take isl_map *map2)
7983 if (!map1 || !map2)
7984 goto error;
7985 map1 = isl_map_reverse(map1);
7986 map1 = isl_map_apply_range(map1, map2);
7987 return isl_map_reverse(map1);
7988 error:
7989 isl_map_free(map1);
7990 isl_map_free(map2);
7991 return NULL;
7994 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7995 __isl_take isl_map *map2)
7997 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
8000 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
8001 __isl_take isl_map *map2)
8003 isl_space *space;
8004 struct isl_map *result;
8005 int i, j;
8007 if (!map1 || !map2)
8008 goto error;
8010 space = isl_space_join(isl_space_copy(map1->dim),
8011 isl_space_copy(map2->dim));
8013 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8014 if (!result)
8015 goto error;
8016 for (i = 0; i < map1->n; ++i)
8017 for (j = 0; j < map2->n; ++j) {
8018 result = isl_map_add_basic_map(result,
8019 isl_basic_map_apply_range(
8020 isl_basic_map_copy(map1->p[i]),
8021 isl_basic_map_copy(map2->p[j])));
8022 if (!result)
8023 goto error;
8025 isl_map_free(map1);
8026 isl_map_free(map2);
8027 if (result && result->n <= 1)
8028 ISL_F_SET(result, ISL_MAP_DISJOINT);
8029 return result;
8030 error:
8031 isl_map_free(map1);
8032 isl_map_free(map2);
8033 return NULL;
8036 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8037 __isl_take isl_map *map2)
8039 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8043 * returns range - domain
8045 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8047 isl_space *target_space;
8048 struct isl_basic_set *bset;
8049 unsigned dim;
8050 unsigned nparam;
8051 int i;
8053 if (!bmap)
8054 goto error;
8055 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8056 bmap->dim, isl_dim_out),
8057 goto error);
8058 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8059 dim = isl_basic_map_dim(bmap, isl_dim_in);
8060 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8061 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8062 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8063 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8064 for (i = 0; i < dim; ++i) {
8065 int j = isl_basic_map_alloc_equality(bmap);
8066 if (j < 0) {
8067 bmap = isl_basic_map_free(bmap);
8068 break;
8070 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8071 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8072 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8073 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8075 bset = isl_basic_map_domain(bmap);
8076 bset = isl_basic_set_reset_space(bset, target_space);
8077 return bset;
8078 error:
8079 isl_basic_map_free(bmap);
8080 return NULL;
8084 * returns range - domain
8086 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8088 int i;
8089 isl_space *dim;
8090 struct isl_set *result;
8092 if (!map)
8093 return NULL;
8095 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8096 map->dim, isl_dim_out),
8097 goto error);
8098 dim = isl_map_get_space(map);
8099 dim = isl_space_domain(dim);
8100 result = isl_set_alloc_space(dim, map->n, 0);
8101 if (!result)
8102 goto error;
8103 for (i = 0; i < map->n; ++i)
8104 result = isl_set_add_basic_set(result,
8105 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8106 isl_map_free(map);
8107 return result;
8108 error:
8109 isl_map_free(map);
8110 return NULL;
8114 * returns [domain -> range] -> range - domain
8116 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8117 __isl_take isl_basic_map *bmap)
8119 int i, k;
8120 isl_space *space;
8121 isl_basic_map *domain;
8122 int nparam, n;
8123 unsigned total;
8125 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8126 bmap->dim, isl_dim_out))
8127 isl_die(bmap->ctx, isl_error_invalid,
8128 "domain and range don't match", goto error);
8130 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8131 n = isl_basic_map_dim(bmap, isl_dim_in);
8133 space = isl_basic_map_get_space(bmap);
8134 space = isl_space_from_range(isl_space_domain(space));
8135 domain = isl_basic_map_universe(space);
8137 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8138 bmap = isl_basic_map_apply_range(bmap, domain);
8139 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8141 total = isl_basic_map_total_dim(bmap);
8143 for (i = 0; i < n; ++i) {
8144 k = isl_basic_map_alloc_equality(bmap);
8145 if (k < 0)
8146 goto error;
8147 isl_seq_clr(bmap->eq[k], 1 + total);
8148 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8149 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8150 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8153 bmap = isl_basic_map_gauss(bmap, NULL);
8154 return isl_basic_map_finalize(bmap);
8155 error:
8156 isl_basic_map_free(bmap);
8157 return NULL;
8161 * returns [domain -> range] -> range - domain
8163 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8165 int i;
8166 isl_space *domain_space;
8168 if (!map)
8169 return NULL;
8171 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8172 map->dim, isl_dim_out))
8173 isl_die(map->ctx, isl_error_invalid,
8174 "domain and range don't match", goto error);
8176 map = isl_map_cow(map);
8177 if (!map)
8178 return NULL;
8180 domain_space = isl_space_domain(isl_map_get_space(map));
8181 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8182 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8183 if (!map->dim)
8184 goto error;
8185 for (i = 0; i < map->n; ++i) {
8186 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8187 if (!map->p[i])
8188 goto error;
8190 map = isl_map_unmark_normalized(map);
8191 return map;
8192 error:
8193 isl_map_free(map);
8194 return NULL;
8197 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8199 unsigned n_in, n_out;
8201 if (!space)
8202 return NULL;
8203 n_in = isl_space_dim(space, isl_dim_in);
8204 n_out = isl_space_dim(space, isl_dim_out);
8205 if (n_in != n_out)
8206 isl_die(space->ctx, isl_error_invalid,
8207 "number of input and output dimensions needs to be "
8208 "the same", goto error);
8209 return isl_basic_map_equal(space, n_in);
8210 error:
8211 isl_space_free(space);
8212 return NULL;
8215 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8217 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8220 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8222 isl_space *dim = isl_set_get_space(set);
8223 isl_map *id;
8224 id = isl_map_identity(isl_space_map_from_set(dim));
8225 return isl_map_intersect_range(id, set);
8228 /* Construct a basic set with all set dimensions having only non-negative
8229 * values.
8231 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8232 __isl_take isl_space *space)
8234 int i;
8235 unsigned nparam;
8236 unsigned dim;
8237 struct isl_basic_set *bset;
8239 if (!space)
8240 return NULL;
8241 nparam = space->nparam;
8242 dim = space->n_out;
8243 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8244 if (!bset)
8245 return NULL;
8246 for (i = 0; i < dim; ++i) {
8247 int k = isl_basic_set_alloc_inequality(bset);
8248 if (k < 0)
8249 goto error;
8250 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8251 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8253 return bset;
8254 error:
8255 isl_basic_set_free(bset);
8256 return NULL;
8259 /* Construct the half-space x_pos >= 0.
8261 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8262 int pos)
8264 int k;
8265 isl_basic_set *nonneg;
8267 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8268 k = isl_basic_set_alloc_inequality(nonneg);
8269 if (k < 0)
8270 goto error;
8271 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8272 isl_int_set_si(nonneg->ineq[k][pos], 1);
8274 return isl_basic_set_finalize(nonneg);
8275 error:
8276 isl_basic_set_free(nonneg);
8277 return NULL;
8280 /* Construct the half-space x_pos <= -1.
8282 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8283 int pos)
8285 int k;
8286 isl_basic_set *neg;
8288 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8289 k = isl_basic_set_alloc_inequality(neg);
8290 if (k < 0)
8291 goto error;
8292 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8293 isl_int_set_si(neg->ineq[k][0], -1);
8294 isl_int_set_si(neg->ineq[k][pos], -1);
8296 return isl_basic_set_finalize(neg);
8297 error:
8298 isl_basic_set_free(neg);
8299 return NULL;
8302 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8303 enum isl_dim_type type, unsigned first, unsigned n)
8305 int i;
8306 unsigned offset;
8307 isl_basic_set *nonneg;
8308 isl_basic_set *neg;
8310 if (!set)
8311 return NULL;
8312 if (n == 0)
8313 return set;
8315 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8317 offset = pos(set->dim, type);
8318 for (i = 0; i < n; ++i) {
8319 nonneg = nonneg_halfspace(isl_set_get_space(set),
8320 offset + first + i);
8321 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8323 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8326 return set;
8327 error:
8328 isl_set_free(set);
8329 return NULL;
8332 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8333 int len,
8334 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8335 void *user)
8337 isl_set *half;
8339 if (!set)
8340 return isl_stat_error;
8341 if (isl_set_plain_is_empty(set)) {
8342 isl_set_free(set);
8343 return isl_stat_ok;
8345 if (first == len)
8346 return fn(set, signs, user);
8348 signs[first] = 1;
8349 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8350 1 + first));
8351 half = isl_set_intersect(half, isl_set_copy(set));
8352 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8353 goto error;
8355 signs[first] = -1;
8356 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8357 1 + first));
8358 half = isl_set_intersect(half, set);
8359 return foreach_orthant(half, signs, first + 1, len, fn, user);
8360 error:
8361 isl_set_free(set);
8362 return isl_stat_error;
8365 /* Call "fn" on the intersections of "set" with each of the orthants
8366 * (except for obviously empty intersections). The orthant is identified
8367 * by the signs array, with each entry having value 1 or -1 according
8368 * to the sign of the corresponding variable.
8370 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8371 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8372 void *user)
8374 unsigned nparam;
8375 unsigned nvar;
8376 int *signs;
8377 isl_stat r;
8379 if (!set)
8380 return isl_stat_error;
8381 if (isl_set_plain_is_empty(set))
8382 return isl_stat_ok;
8384 nparam = isl_set_dim(set, isl_dim_param);
8385 nvar = isl_set_dim(set, isl_dim_set);
8387 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8389 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8390 fn, user);
8392 free(signs);
8394 return r;
8397 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8399 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8402 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8403 __isl_keep isl_basic_map *bmap2)
8405 isl_bool is_subset;
8406 struct isl_map *map1;
8407 struct isl_map *map2;
8409 if (!bmap1 || !bmap2)
8410 return isl_bool_error;
8412 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8413 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8415 is_subset = isl_map_is_subset(map1, map2);
8417 isl_map_free(map1);
8418 isl_map_free(map2);
8420 return is_subset;
8423 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8424 __isl_keep isl_basic_set *bset2)
8426 return isl_basic_map_is_subset(bset1, bset2);
8429 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8430 __isl_keep isl_basic_map *bmap2)
8432 isl_bool is_subset;
8434 if (!bmap1 || !bmap2)
8435 return isl_bool_error;
8436 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8437 if (is_subset != isl_bool_true)
8438 return is_subset;
8439 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8440 return is_subset;
8443 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8444 __isl_keep isl_basic_set *bset2)
8446 return isl_basic_map_is_equal(
8447 bset_to_bmap(bset1), bset_to_bmap(bset2));
8450 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8452 int i;
8453 int is_empty;
8455 if (!map)
8456 return isl_bool_error;
8457 for (i = 0; i < map->n; ++i) {
8458 is_empty = isl_basic_map_is_empty(map->p[i]);
8459 if (is_empty < 0)
8460 return isl_bool_error;
8461 if (!is_empty)
8462 return isl_bool_false;
8464 return isl_bool_true;
8467 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8469 return map ? map->n == 0 : isl_bool_error;
8472 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8474 return set ? set->n == 0 : isl_bool_error;
8477 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8479 return isl_map_is_empty(set_to_map(set));
8482 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8483 __isl_keep isl_map *map2)
8485 if (!map1 || !map2)
8486 return isl_bool_error;
8488 return isl_space_is_equal(map1->dim, map2->dim);
8491 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8492 __isl_keep isl_set *set2)
8494 if (!set1 || !set2)
8495 return isl_bool_error;
8497 return isl_space_is_equal(set1->dim, set2->dim);
8500 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8502 isl_bool is_subset;
8504 if (!map1 || !map2)
8505 return isl_bool_error;
8506 is_subset = isl_map_is_subset(map1, map2);
8507 if (is_subset != isl_bool_true)
8508 return is_subset;
8509 is_subset = isl_map_is_subset(map2, map1);
8510 return is_subset;
8513 /* Is "map1" equal to "map2"?
8515 * First check if they are obviously equal.
8516 * If not, then perform a more detailed analysis.
8518 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8520 isl_bool equal;
8522 equal = isl_map_plain_is_equal(map1, map2);
8523 if (equal < 0 || equal)
8524 return equal;
8525 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8528 isl_bool isl_basic_map_is_strict_subset(
8529 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8531 isl_bool is_subset;
8533 if (!bmap1 || !bmap2)
8534 return isl_bool_error;
8535 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8536 if (is_subset != isl_bool_true)
8537 return is_subset;
8538 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8539 return isl_bool_not(is_subset);
8542 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8543 __isl_keep isl_map *map2)
8545 isl_bool is_subset;
8547 if (!map1 || !map2)
8548 return isl_bool_error;
8549 is_subset = isl_map_is_subset(map1, map2);
8550 if (is_subset != isl_bool_true)
8551 return is_subset;
8552 is_subset = isl_map_is_subset(map2, map1);
8553 return isl_bool_not(is_subset);
8556 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8557 __isl_keep isl_set *set2)
8559 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8562 /* Is "bmap" obviously equal to the universe with the same space?
8564 * That is, does it not have any constraints?
8566 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8568 if (!bmap)
8569 return isl_bool_error;
8570 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8573 /* Is "bset" obviously equal to the universe with the same space?
8575 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8577 return isl_basic_map_plain_is_universe(bset);
8580 /* If "c" does not involve any existentially quantified variables,
8581 * then set *univ to false and abort
8583 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8585 isl_bool *univ = user;
8586 unsigned n;
8588 n = isl_constraint_dim(c, isl_dim_div);
8589 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8590 isl_constraint_free(c);
8591 if (*univ < 0 || !*univ)
8592 return isl_stat_error;
8593 return isl_stat_ok;
8596 /* Is "bmap" equal to the universe with the same space?
8598 * First check if it is obviously equal to the universe.
8599 * If not and if there are any constraints not involving
8600 * existentially quantified variables, then it is certainly
8601 * not equal to the universe.
8602 * Otherwise, check if the universe is a subset of "bmap".
8604 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8606 isl_bool univ;
8607 isl_basic_map *test;
8609 univ = isl_basic_map_plain_is_universe(bmap);
8610 if (univ < 0 || univ)
8611 return univ;
8612 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8613 return isl_bool_false;
8614 univ = isl_bool_true;
8615 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8616 univ)
8617 return isl_bool_error;
8618 if (univ < 0 || !univ)
8619 return univ;
8620 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8621 univ = isl_basic_map_is_subset(test, bmap);
8622 isl_basic_map_free(test);
8623 return univ;
8626 /* Is "bset" equal to the universe with the same space?
8628 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8630 return isl_basic_map_is_universe(bset);
8633 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8635 int i;
8637 if (!map)
8638 return isl_bool_error;
8640 for (i = 0; i < map->n; ++i) {
8641 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8642 if (r < 0 || r)
8643 return r;
8646 return isl_bool_false;
8649 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8651 return isl_map_plain_is_universe(set_to_map(set));
8654 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8656 struct isl_basic_set *bset = NULL;
8657 struct isl_vec *sample = NULL;
8658 isl_bool empty, non_empty;
8660 if (!bmap)
8661 return isl_bool_error;
8663 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8664 return isl_bool_true;
8666 if (isl_basic_map_plain_is_universe(bmap))
8667 return isl_bool_false;
8669 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8670 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8671 copy = isl_basic_map_remove_redundancies(copy);
8672 empty = isl_basic_map_plain_is_empty(copy);
8673 isl_basic_map_free(copy);
8674 return empty;
8677 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8678 if (non_empty < 0)
8679 return isl_bool_error;
8680 if (non_empty)
8681 return isl_bool_false;
8682 isl_vec_free(bmap->sample);
8683 bmap->sample = NULL;
8684 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8685 if (!bset)
8686 return isl_bool_error;
8687 sample = isl_basic_set_sample_vec(bset);
8688 if (!sample)
8689 return isl_bool_error;
8690 empty = sample->size == 0;
8691 isl_vec_free(bmap->sample);
8692 bmap->sample = sample;
8693 if (empty)
8694 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8696 return empty;
8699 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8701 if (!bmap)
8702 return isl_bool_error;
8703 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8706 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8708 if (!bset)
8709 return isl_bool_error;
8710 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8713 /* Is "bmap" known to be non-empty?
8715 * That is, is the cached sample still valid?
8717 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8719 unsigned total;
8721 if (!bmap)
8722 return isl_bool_error;
8723 if (!bmap->sample)
8724 return isl_bool_false;
8725 total = 1 + isl_basic_map_total_dim(bmap);
8726 if (bmap->sample->size != total)
8727 return isl_bool_false;
8728 return isl_basic_map_contains(bmap, bmap->sample);
8731 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8733 return isl_basic_map_is_empty(bset_to_bmap(bset));
8736 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8737 __isl_take isl_basic_map *bmap2)
8739 struct isl_map *map;
8740 if (!bmap1 || !bmap2)
8741 goto error;
8743 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8745 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8746 if (!map)
8747 goto error;
8748 map = isl_map_add_basic_map(map, bmap1);
8749 map = isl_map_add_basic_map(map, bmap2);
8750 return map;
8751 error:
8752 isl_basic_map_free(bmap1);
8753 isl_basic_map_free(bmap2);
8754 return NULL;
8757 struct isl_set *isl_basic_set_union(
8758 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8760 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8761 bset_to_bmap(bset2)));
8764 /* Order divs such that any div only depends on previous divs */
8765 __isl_give isl_basic_map *isl_basic_map_order_divs(
8766 __isl_take isl_basic_map *bmap)
8768 int i;
8769 unsigned off;
8771 if (!bmap)
8772 return NULL;
8774 off = isl_space_dim(bmap->dim, isl_dim_all);
8776 for (i = 0; i < bmap->n_div; ++i) {
8777 int pos;
8778 if (isl_int_is_zero(bmap->div[i][0]))
8779 continue;
8780 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8781 bmap->n_div-i);
8782 if (pos == -1)
8783 continue;
8784 if (pos == 0)
8785 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8786 "integer division depends on itself",
8787 return isl_basic_map_free(bmap));
8788 isl_basic_map_swap_div(bmap, i, i + pos);
8789 --i;
8791 return bmap;
8794 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8796 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8799 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8801 int i;
8803 if (!map)
8804 return 0;
8806 for (i = 0; i < map->n; ++i) {
8807 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8808 if (!map->p[i])
8809 goto error;
8812 return map;
8813 error:
8814 isl_map_free(map);
8815 return NULL;
8818 /* Sort the local variables of "bset".
8820 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8821 __isl_take isl_basic_set *bset)
8823 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8826 /* Apply the expansion computed by isl_merge_divs.
8827 * The expansion itself is given by "exp" while the resulting
8828 * list of divs is given by "div".
8830 * Move the integer divisions of "bmap" into the right position
8831 * according to "exp" and then introduce the additional integer
8832 * divisions, adding div constraints.
8833 * The moving should be done first to avoid moving coefficients
8834 * in the definitions of the extra integer divisions.
8836 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8837 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8839 int i, j;
8840 int n_div;
8842 bmap = isl_basic_map_cow(bmap);
8843 if (!bmap || !div)
8844 goto error;
8846 if (div->n_row < bmap->n_div)
8847 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8848 "not an expansion", goto error);
8850 n_div = bmap->n_div;
8851 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8852 div->n_row - n_div, 0,
8853 2 * (div->n_row - n_div));
8855 for (i = n_div; i < div->n_row; ++i)
8856 if (isl_basic_map_alloc_div(bmap) < 0)
8857 goto error;
8859 for (j = n_div - 1; j >= 0; --j) {
8860 if (exp[j] == j)
8861 break;
8862 isl_basic_map_swap_div(bmap, j, exp[j]);
8864 j = 0;
8865 for (i = 0; i < div->n_row; ++i) {
8866 if (j < n_div && exp[j] == i) {
8867 j++;
8868 } else {
8869 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8870 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8871 continue;
8872 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8873 goto error;
8877 isl_mat_free(div);
8878 return bmap;
8879 error:
8880 isl_basic_map_free(bmap);
8881 isl_mat_free(div);
8882 return NULL;
8885 /* Apply the expansion computed by isl_merge_divs.
8886 * The expansion itself is given by "exp" while the resulting
8887 * list of divs is given by "div".
8889 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8890 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8892 return isl_basic_map_expand_divs(bset, div, exp);
8895 /* Look for a div in dst that corresponds to the div "div" in src.
8896 * The divs before "div" in src and dst are assumed to be the same.
8898 * Returns -1 if no corresponding div was found and the position
8899 * of the corresponding div in dst otherwise.
8901 static int find_div(__isl_keep isl_basic_map *dst,
8902 __isl_keep isl_basic_map *src, unsigned div)
8904 int i;
8906 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8908 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8909 for (i = div; i < dst->n_div; ++i)
8910 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8911 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8912 dst->n_div - div) == -1)
8913 return i;
8914 return -1;
8917 /* Align the divs of "dst" to those of "src", adding divs from "src"
8918 * if needed. That is, make sure that the first src->n_div divs
8919 * of the result are equal to those of src.
8921 * The result is not finalized as by design it will have redundant
8922 * divs if any divs from "src" were copied.
8924 __isl_give isl_basic_map *isl_basic_map_align_divs(
8925 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8927 int i;
8928 isl_bool known;
8929 int extended;
8930 unsigned total;
8932 if (!dst || !src)
8933 return isl_basic_map_free(dst);
8935 if (src->n_div == 0)
8936 return dst;
8938 known = isl_basic_map_divs_known(src);
8939 if (known < 0)
8940 return isl_basic_map_free(dst);
8941 if (!known)
8942 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8943 "some src divs are unknown",
8944 return isl_basic_map_free(dst));
8946 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8947 if (!src)
8948 return isl_basic_map_free(dst);
8950 extended = 0;
8951 total = isl_space_dim(src->dim, isl_dim_all);
8952 for (i = 0; i < src->n_div; ++i) {
8953 int j = find_div(dst, src, i);
8954 if (j < 0) {
8955 if (!extended) {
8956 int extra = src->n_div - i;
8957 dst = isl_basic_map_cow(dst);
8958 if (!dst)
8959 goto error;
8960 dst = isl_basic_map_extend_space(dst,
8961 isl_space_copy(dst->dim),
8962 extra, 0, 2 * extra);
8963 extended = 1;
8965 j = isl_basic_map_alloc_div(dst);
8966 if (j < 0)
8967 goto error;
8968 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8969 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8970 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8971 goto error;
8973 if (j != i)
8974 isl_basic_map_swap_div(dst, i, j);
8976 isl_basic_map_free(src);
8977 return dst;
8978 error:
8979 isl_basic_map_free(src);
8980 isl_basic_map_free(dst);
8981 return NULL;
8984 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8986 int i;
8988 if (!map)
8989 return NULL;
8990 if (map->n == 0)
8991 return map;
8992 map = isl_map_compute_divs(map);
8993 map = isl_map_cow(map);
8994 if (!map)
8995 return NULL;
8997 for (i = 1; i < map->n; ++i)
8998 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8999 for (i = 1; i < map->n; ++i) {
9000 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9001 if (!map->p[i])
9002 return isl_map_free(map);
9005 map = isl_map_unmark_normalized(map);
9006 return map;
9009 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9011 return isl_map_align_divs_internal(map);
9014 struct isl_set *isl_set_align_divs(struct isl_set *set)
9016 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9019 /* Align the divs of the basic maps in "map" to those
9020 * of the basic maps in "list", as well as to the other basic maps in "map".
9021 * The elements in "list" are assumed to have known divs.
9023 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9024 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9026 int i, n;
9028 map = isl_map_compute_divs(map);
9029 map = isl_map_cow(map);
9030 if (!map || !list)
9031 return isl_map_free(map);
9032 if (map->n == 0)
9033 return map;
9035 n = isl_basic_map_list_n_basic_map(list);
9036 for (i = 0; i < n; ++i) {
9037 isl_basic_map *bmap;
9039 bmap = isl_basic_map_list_get_basic_map(list, i);
9040 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9041 isl_basic_map_free(bmap);
9043 if (!map->p[0])
9044 return isl_map_free(map);
9046 return isl_map_align_divs_internal(map);
9049 /* Align the divs of each element of "list" to those of "bmap".
9050 * Both "bmap" and the elements of "list" are assumed to have known divs.
9052 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9053 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9055 int i, n;
9057 if (!list || !bmap)
9058 return isl_basic_map_list_free(list);
9060 n = isl_basic_map_list_n_basic_map(list);
9061 for (i = 0; i < n; ++i) {
9062 isl_basic_map *bmap_i;
9064 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9065 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9066 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9069 return list;
9072 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9073 __isl_take isl_map *map)
9075 isl_bool ok;
9077 ok = isl_map_compatible_domain(map, set);
9078 if (ok < 0)
9079 goto error;
9080 if (!ok)
9081 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9082 "incompatible spaces", goto error);
9083 map = isl_map_intersect_domain(map, set);
9084 set = isl_map_range(map);
9085 return set;
9086 error:
9087 isl_set_free(set);
9088 isl_map_free(map);
9089 return NULL;
9092 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9093 __isl_take isl_map *map)
9095 return isl_map_align_params_map_map_and(set, map, &set_apply);
9098 /* There is no need to cow as removing empty parts doesn't change
9099 * the meaning of the set.
9101 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9103 int i;
9105 if (!map)
9106 return NULL;
9108 for (i = map->n - 1; i >= 0; --i)
9109 map = remove_if_empty(map, i);
9111 return map;
9114 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9116 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9119 /* Create a binary relation that maps the shared initial "pos" dimensions
9120 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9122 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9123 __isl_keep isl_basic_set *bset2, int pos)
9125 isl_basic_map *bmap1;
9126 isl_basic_map *bmap2;
9128 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9129 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9130 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9131 isl_dim_out, 0, pos);
9132 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9133 isl_dim_out, 0, pos);
9134 return isl_basic_map_range_product(bmap1, bmap2);
9137 /* Given two basic sets bset1 and bset2, compute the maximal difference
9138 * between the values of dimension pos in bset1 and those in bset2
9139 * for any common value of the parameters and dimensions preceding pos.
9141 static enum isl_lp_result basic_set_maximal_difference_at(
9142 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9143 int pos, isl_int *opt)
9145 isl_basic_map *bmap1;
9146 struct isl_ctx *ctx;
9147 struct isl_vec *obj;
9148 unsigned total;
9149 unsigned nparam;
9150 unsigned dim1;
9151 enum isl_lp_result res;
9153 if (!bset1 || !bset2)
9154 return isl_lp_error;
9156 nparam = isl_basic_set_n_param(bset1);
9157 dim1 = isl_basic_set_n_dim(bset1);
9159 bmap1 = join_initial(bset1, bset2, pos);
9160 if (!bmap1)
9161 return isl_lp_error;
9163 total = isl_basic_map_total_dim(bmap1);
9164 ctx = bmap1->ctx;
9165 obj = isl_vec_alloc(ctx, 1 + total);
9166 if (!obj)
9167 goto error;
9168 isl_seq_clr(obj->block.data, 1 + total);
9169 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9170 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9171 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9172 opt, NULL, NULL);
9173 isl_basic_map_free(bmap1);
9174 isl_vec_free(obj);
9175 return res;
9176 error:
9177 isl_basic_map_free(bmap1);
9178 return isl_lp_error;
9181 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9182 * for any common value of the parameters and dimensions preceding pos
9183 * in both basic sets, the values of dimension pos in bset1 are
9184 * smaller or larger than those in bset2.
9186 * Returns
9187 * 1 if bset1 follows bset2
9188 * -1 if bset1 precedes bset2
9189 * 0 if bset1 and bset2 are incomparable
9190 * -2 if some error occurred.
9192 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9193 __isl_keep isl_basic_set *bset2, int pos)
9195 isl_int opt;
9196 enum isl_lp_result res;
9197 int cmp;
9199 isl_int_init(opt);
9201 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9203 if (res == isl_lp_empty)
9204 cmp = 0;
9205 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9206 res == isl_lp_unbounded)
9207 cmp = 1;
9208 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9209 cmp = -1;
9210 else
9211 cmp = -2;
9213 isl_int_clear(opt);
9214 return cmp;
9217 /* Given two basic sets bset1 and bset2, check whether
9218 * for any common value of the parameters and dimensions preceding pos
9219 * there is a value of dimension pos in bset1 that is larger
9220 * than a value of the same dimension in bset2.
9222 * Return
9223 * 1 if there exists such a pair
9224 * 0 if there is no such pair, but there is a pair of equal values
9225 * -1 otherwise
9226 * -2 if some error occurred.
9228 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9229 __isl_keep isl_basic_set *bset2, int pos)
9231 isl_bool empty;
9232 isl_basic_map *bmap;
9233 unsigned dim1;
9235 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9236 bmap = join_initial(bset1, bset2, pos);
9237 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9238 isl_dim_out, dim1 - pos);
9239 empty = isl_basic_map_is_empty(bmap);
9240 if (empty < 0)
9241 goto error;
9242 if (empty) {
9243 isl_basic_map_free(bmap);
9244 return -1;
9246 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9247 isl_dim_out, dim1 - pos);
9248 empty = isl_basic_map_is_empty(bmap);
9249 if (empty < 0)
9250 goto error;
9251 isl_basic_map_free(bmap);
9252 if (empty)
9253 return 0;
9254 return 1;
9255 error:
9256 isl_basic_map_free(bmap);
9257 return -2;
9260 /* Given two sets set1 and set2, check whether
9261 * for any common value of the parameters and dimensions preceding pos
9262 * there is a value of dimension pos in set1 that is larger
9263 * than a value of the same dimension in set2.
9265 * Return
9266 * 1 if there exists such a pair
9267 * 0 if there is no such pair, but there is a pair of equal values
9268 * -1 otherwise
9269 * -2 if some error occurred.
9271 int isl_set_follows_at(__isl_keep isl_set *set1,
9272 __isl_keep isl_set *set2, int pos)
9274 int i, j;
9275 int follows = -1;
9277 if (!set1 || !set2)
9278 return -2;
9280 for (i = 0; i < set1->n; ++i)
9281 for (j = 0; j < set2->n; ++j) {
9282 int f;
9283 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9284 if (f == 1 || f == -2)
9285 return f;
9286 if (f > follows)
9287 follows = f;
9290 return follows;
9293 static isl_bool isl_basic_map_plain_has_fixed_var(
9294 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9296 int i;
9297 int d;
9298 unsigned total;
9300 if (!bmap)
9301 return isl_bool_error;
9302 total = isl_basic_map_total_dim(bmap);
9303 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9304 for (; d+1 > pos; --d)
9305 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9306 break;
9307 if (d != pos)
9308 continue;
9309 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9310 return isl_bool_false;
9311 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9312 return isl_bool_false;
9313 if (!isl_int_is_one(bmap->eq[i][1+d]))
9314 return isl_bool_false;
9315 if (val)
9316 isl_int_neg(*val, bmap->eq[i][0]);
9317 return isl_bool_true;
9319 return isl_bool_false;
9322 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9323 unsigned pos, isl_int *val)
9325 int i;
9326 isl_int v;
9327 isl_int tmp;
9328 isl_bool fixed;
9330 if (!map)
9331 return isl_bool_error;
9332 if (map->n == 0)
9333 return isl_bool_false;
9334 if (map->n == 1)
9335 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9336 isl_int_init(v);
9337 isl_int_init(tmp);
9338 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9339 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9340 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9341 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9342 fixed = isl_bool_false;
9344 if (val)
9345 isl_int_set(*val, v);
9346 isl_int_clear(tmp);
9347 isl_int_clear(v);
9348 return fixed;
9351 static isl_bool isl_basic_set_plain_has_fixed_var(
9352 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9354 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9355 pos, val);
9358 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9359 enum isl_dim_type type, unsigned pos, isl_int *val)
9361 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9362 return isl_bool_error;
9363 return isl_basic_map_plain_has_fixed_var(bmap,
9364 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9367 /* If "bmap" obviously lies on a hyperplane where the given dimension
9368 * has a fixed value, then return that value.
9369 * Otherwise return NaN.
9371 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9372 __isl_keep isl_basic_map *bmap,
9373 enum isl_dim_type type, unsigned pos)
9375 isl_ctx *ctx;
9376 isl_val *v;
9377 isl_bool fixed;
9379 if (!bmap)
9380 return NULL;
9381 ctx = isl_basic_map_get_ctx(bmap);
9382 v = isl_val_alloc(ctx);
9383 if (!v)
9384 return NULL;
9385 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9386 if (fixed < 0)
9387 return isl_val_free(v);
9388 if (fixed) {
9389 isl_int_set_si(v->d, 1);
9390 return v;
9392 isl_val_free(v);
9393 return isl_val_nan(ctx);
9396 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9397 enum isl_dim_type type, unsigned pos, isl_int *val)
9399 if (pos >= isl_map_dim(map, type))
9400 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9401 "position out of bounds", return isl_bool_error);
9402 return isl_map_plain_has_fixed_var(map,
9403 map_offset(map, type) - 1 + pos, val);
9406 /* If "map" obviously lies on a hyperplane where the given dimension
9407 * has a fixed value, then return that value.
9408 * Otherwise return NaN.
9410 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9411 enum isl_dim_type type, unsigned pos)
9413 isl_ctx *ctx;
9414 isl_val *v;
9415 isl_bool fixed;
9417 if (!map)
9418 return NULL;
9419 ctx = isl_map_get_ctx(map);
9420 v = isl_val_alloc(ctx);
9421 if (!v)
9422 return NULL;
9423 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9424 if (fixed < 0)
9425 return isl_val_free(v);
9426 if (fixed) {
9427 isl_int_set_si(v->d, 1);
9428 return v;
9430 isl_val_free(v);
9431 return isl_val_nan(ctx);
9434 /* If "set" obviously lies on a hyperplane where the given dimension
9435 * has a fixed value, then return that value.
9436 * Otherwise return NaN.
9438 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9439 enum isl_dim_type type, unsigned pos)
9441 return isl_map_plain_get_val_if_fixed(set, type, pos);
9444 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9445 * then return this fixed value in *val.
9447 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9448 unsigned dim, isl_int *val)
9450 return isl_basic_set_plain_has_fixed_var(bset,
9451 isl_basic_set_n_param(bset) + dim, val);
9454 /* Return -1 if the constraint "c1" should be sorted before "c2"
9455 * and 1 if it should be sorted after "c2".
9456 * Return 0 if the two constraints are the same (up to the constant term).
9458 * In particular, if a constraint involves later variables than another
9459 * then it is sorted after this other constraint.
9460 * uset_gist depends on constraints without existentially quantified
9461 * variables sorting first.
9463 * For constraints that have the same latest variable, those
9464 * with the same coefficient for this latest variable (first in absolute value
9465 * and then in actual value) are grouped together.
9466 * This is useful for detecting pairs of constraints that can
9467 * be chained in their printed representation.
9469 * Finally, within a group, constraints are sorted according to
9470 * their coefficients (excluding the constant term).
9472 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9474 isl_int **c1 = (isl_int **) p1;
9475 isl_int **c2 = (isl_int **) p2;
9476 int l1, l2;
9477 unsigned size = *(unsigned *) arg;
9478 int cmp;
9480 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9481 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9483 if (l1 != l2)
9484 return l1 - l2;
9486 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9487 if (cmp != 0)
9488 return cmp;
9489 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9490 if (cmp != 0)
9491 return -cmp;
9493 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9496 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9497 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9498 * and 0 if the two constraints are the same (up to the constant term).
9500 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9501 isl_int *c1, isl_int *c2)
9503 unsigned total;
9505 if (!bmap)
9506 return -2;
9507 total = isl_basic_map_total_dim(bmap);
9508 return sort_constraint_cmp(&c1, &c2, &total);
9511 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9512 __isl_take isl_basic_map *bmap)
9514 unsigned total;
9516 if (!bmap)
9517 return NULL;
9518 if (bmap->n_ineq == 0)
9519 return bmap;
9520 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9521 return bmap;
9522 total = isl_basic_map_total_dim(bmap);
9523 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9524 &sort_constraint_cmp, &total) < 0)
9525 return isl_basic_map_free(bmap);
9526 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9527 return bmap;
9530 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9531 __isl_take isl_basic_set *bset)
9533 isl_basic_map *bmap = bset_to_bmap(bset);
9534 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9537 __isl_give isl_basic_map *isl_basic_map_normalize(
9538 __isl_take isl_basic_map *bmap)
9540 bmap = isl_basic_map_remove_redundancies(bmap);
9541 bmap = isl_basic_map_sort_constraints(bmap);
9542 return bmap;
9544 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9545 __isl_keep isl_basic_map *bmap2)
9547 int i, cmp;
9548 unsigned total;
9549 isl_space *space1, *space2;
9551 if (!bmap1 || !bmap2)
9552 return -1;
9554 if (bmap1 == bmap2)
9555 return 0;
9556 space1 = isl_basic_map_peek_space(bmap1);
9557 space2 = isl_basic_map_peek_space(bmap2);
9558 cmp = isl_space_cmp(space1, space2);
9559 if (cmp)
9560 return cmp;
9561 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9562 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9563 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9564 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9565 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9566 return 0;
9567 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9568 return 1;
9569 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9570 return -1;
9571 if (bmap1->n_eq != bmap2->n_eq)
9572 return bmap1->n_eq - bmap2->n_eq;
9573 if (bmap1->n_ineq != bmap2->n_ineq)
9574 return bmap1->n_ineq - bmap2->n_ineq;
9575 if (bmap1->n_div != bmap2->n_div)
9576 return bmap1->n_div - bmap2->n_div;
9577 total = isl_basic_map_total_dim(bmap1);
9578 for (i = 0; i < bmap1->n_eq; ++i) {
9579 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9580 if (cmp)
9581 return cmp;
9583 for (i = 0; i < bmap1->n_ineq; ++i) {
9584 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9585 if (cmp)
9586 return cmp;
9588 for (i = 0; i < bmap1->n_div; ++i) {
9589 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9590 if (cmp)
9591 return cmp;
9593 return 0;
9596 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9597 __isl_keep isl_basic_set *bset2)
9599 return isl_basic_map_plain_cmp(bset1, bset2);
9602 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9604 int i, cmp;
9606 if (set1 == set2)
9607 return 0;
9608 if (set1->n != set2->n)
9609 return set1->n - set2->n;
9611 for (i = 0; i < set1->n; ++i) {
9612 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9613 if (cmp)
9614 return cmp;
9617 return 0;
9620 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9621 __isl_keep isl_basic_map *bmap2)
9623 if (!bmap1 || !bmap2)
9624 return isl_bool_error;
9625 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9628 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9629 __isl_keep isl_basic_set *bset2)
9631 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9632 bset_to_bmap(bset2));
9635 static int qsort_bmap_cmp(const void *p1, const void *p2)
9637 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9638 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9640 return isl_basic_map_plain_cmp(bmap1, bmap2);
9643 /* Sort the basic maps of "map" and remove duplicate basic maps.
9645 * While removing basic maps, we make sure that the basic maps remain
9646 * sorted because isl_map_normalize expects the basic maps of the result
9647 * to be sorted.
9649 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9651 int i, j;
9653 map = isl_map_remove_empty_parts(map);
9654 if (!map)
9655 return NULL;
9656 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9657 for (i = map->n - 1; i >= 1; --i) {
9658 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9659 continue;
9660 isl_basic_map_free(map->p[i-1]);
9661 for (j = i; j < map->n; ++j)
9662 map->p[j - 1] = map->p[j];
9663 map->n--;
9666 return map;
9669 /* Remove obvious duplicates among the basic maps of "map".
9671 * Unlike isl_map_normalize, this function does not remove redundant
9672 * constraints and only removes duplicates that have exactly the same
9673 * constraints in the input. It does sort the constraints and
9674 * the basic maps to ease the detection of duplicates.
9676 * If "map" has already been normalized or if the basic maps are
9677 * disjoint, then there can be no duplicates.
9679 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9681 int i;
9682 isl_basic_map *bmap;
9684 if (!map)
9685 return NULL;
9686 if (map->n <= 1)
9687 return map;
9688 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9689 return map;
9690 for (i = 0; i < map->n; ++i) {
9691 bmap = isl_basic_map_copy(map->p[i]);
9692 bmap = isl_basic_map_sort_constraints(bmap);
9693 if (!bmap)
9694 return isl_map_free(map);
9695 isl_basic_map_free(map->p[i]);
9696 map->p[i] = bmap;
9699 map = sort_and_remove_duplicates(map);
9700 return map;
9703 /* We normalize in place, but if anything goes wrong we need
9704 * to return NULL, so we need to make sure we don't change the
9705 * meaning of any possible other copies of map.
9707 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9709 int i;
9710 struct isl_basic_map *bmap;
9712 if (!map)
9713 return NULL;
9714 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9715 return map;
9716 for (i = 0; i < map->n; ++i) {
9717 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9718 if (!bmap)
9719 goto error;
9720 isl_basic_map_free(map->p[i]);
9721 map->p[i] = bmap;
9724 map = sort_and_remove_duplicates(map);
9725 if (map)
9726 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9727 return map;
9728 error:
9729 isl_map_free(map);
9730 return NULL;
9733 struct isl_set *isl_set_normalize(struct isl_set *set)
9735 return set_from_map(isl_map_normalize(set_to_map(set)));
9738 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9739 __isl_keep isl_map *map2)
9741 int i;
9742 isl_bool equal;
9744 if (!map1 || !map2)
9745 return isl_bool_error;
9747 if (map1 == map2)
9748 return isl_bool_true;
9749 if (!isl_space_is_equal(map1->dim, map2->dim))
9750 return isl_bool_false;
9752 map1 = isl_map_copy(map1);
9753 map2 = isl_map_copy(map2);
9754 map1 = isl_map_normalize(map1);
9755 map2 = isl_map_normalize(map2);
9756 if (!map1 || !map2)
9757 goto error;
9758 equal = map1->n == map2->n;
9759 for (i = 0; equal && i < map1->n; ++i) {
9760 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9761 if (equal < 0)
9762 goto error;
9764 isl_map_free(map1);
9765 isl_map_free(map2);
9766 return equal;
9767 error:
9768 isl_map_free(map1);
9769 isl_map_free(map2);
9770 return isl_bool_error;
9773 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9774 __isl_keep isl_set *set2)
9776 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9779 /* Return the basic maps in "map" as a list.
9781 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9782 __isl_keep isl_map *map)
9784 int i;
9785 isl_ctx *ctx;
9786 isl_basic_map_list *list;
9788 if (!map)
9789 return NULL;
9790 ctx = isl_map_get_ctx(map);
9791 list = isl_basic_map_list_alloc(ctx, map->n);
9793 for (i = 0; i < map->n; ++i) {
9794 isl_basic_map *bmap;
9796 bmap = isl_basic_map_copy(map->p[i]);
9797 list = isl_basic_map_list_add(list, bmap);
9800 return list;
9803 /* Return the intersection of the elements in the non-empty list "list".
9804 * All elements are assumed to live in the same space.
9806 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9807 __isl_take isl_basic_map_list *list)
9809 int i, n;
9810 isl_basic_map *bmap;
9812 if (!list)
9813 return NULL;
9814 n = isl_basic_map_list_n_basic_map(list);
9815 if (n < 1)
9816 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9817 "expecting non-empty list", goto error);
9819 bmap = isl_basic_map_list_get_basic_map(list, 0);
9820 for (i = 1; i < n; ++i) {
9821 isl_basic_map *bmap_i;
9823 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9824 bmap = isl_basic_map_intersect(bmap, bmap_i);
9827 isl_basic_map_list_free(list);
9828 return bmap;
9829 error:
9830 isl_basic_map_list_free(list);
9831 return NULL;
9834 /* Return the intersection of the elements in the non-empty list "list".
9835 * All elements are assumed to live in the same space.
9837 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9838 __isl_take isl_basic_set_list *list)
9840 return isl_basic_map_list_intersect(list);
9843 /* Return the union of the elements of "list".
9844 * The list is required to have at least one element.
9846 __isl_give isl_set *isl_basic_set_list_union(
9847 __isl_take isl_basic_set_list *list)
9849 int i, n;
9850 isl_space *space;
9851 isl_basic_set *bset;
9852 isl_set *set;
9854 if (!list)
9855 return NULL;
9856 n = isl_basic_set_list_n_basic_set(list);
9857 if (n < 1)
9858 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9859 "expecting non-empty list", goto error);
9861 bset = isl_basic_set_list_get_basic_set(list, 0);
9862 space = isl_basic_set_get_space(bset);
9863 isl_basic_set_free(bset);
9865 set = isl_set_alloc_space(space, n, 0);
9866 for (i = 0; i < n; ++i) {
9867 bset = isl_basic_set_list_get_basic_set(list, i);
9868 set = isl_set_add_basic_set(set, bset);
9871 isl_basic_set_list_free(list);
9872 return set;
9873 error:
9874 isl_basic_set_list_free(list);
9875 return NULL;
9878 /* Return the union of the elements in the non-empty list "list".
9879 * All elements are assumed to live in the same space.
9881 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9883 int i, n;
9884 isl_set *set;
9886 if (!list)
9887 return NULL;
9888 n = isl_set_list_n_set(list);
9889 if (n < 1)
9890 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9891 "expecting non-empty list", goto error);
9893 set = isl_set_list_get_set(list, 0);
9894 for (i = 1; i < n; ++i) {
9895 isl_set *set_i;
9897 set_i = isl_set_list_get_set(list, i);
9898 set = isl_set_union(set, set_i);
9901 isl_set_list_free(list);
9902 return set;
9903 error:
9904 isl_set_list_free(list);
9905 return NULL;
9908 __isl_give isl_basic_map *isl_basic_map_product(
9909 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9911 isl_space *space_result = NULL;
9912 struct isl_basic_map *bmap;
9913 unsigned in1, in2, out1, out2, nparam, total, pos;
9914 struct isl_dim_map *dim_map1, *dim_map2;
9916 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9917 goto error;
9918 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9919 isl_space_copy(bmap2->dim));
9921 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9922 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9923 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9924 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9925 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9927 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9928 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9929 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9930 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9931 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9932 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9933 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9934 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9935 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9936 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9937 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9939 bmap = isl_basic_map_alloc_space(space_result,
9940 bmap1->n_div + bmap2->n_div,
9941 bmap1->n_eq + bmap2->n_eq,
9942 bmap1->n_ineq + bmap2->n_ineq);
9943 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9944 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9945 bmap = isl_basic_map_simplify(bmap);
9946 return isl_basic_map_finalize(bmap);
9947 error:
9948 isl_basic_map_free(bmap1);
9949 isl_basic_map_free(bmap2);
9950 return NULL;
9953 __isl_give isl_basic_map *isl_basic_map_flat_product(
9954 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9956 isl_basic_map *prod;
9958 prod = isl_basic_map_product(bmap1, bmap2);
9959 prod = isl_basic_map_flatten(prod);
9960 return prod;
9963 __isl_give isl_basic_set *isl_basic_set_flat_product(
9964 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9966 return isl_basic_map_flat_range_product(bset1, bset2);
9969 __isl_give isl_basic_map *isl_basic_map_domain_product(
9970 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9972 isl_space *space_result = NULL;
9973 isl_basic_map *bmap;
9974 unsigned in1, in2, out, nparam, total, pos;
9975 struct isl_dim_map *dim_map1, *dim_map2;
9977 if (!bmap1 || !bmap2)
9978 goto error;
9980 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9981 isl_space_copy(bmap2->dim));
9983 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9984 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9985 out = isl_basic_map_dim(bmap1, isl_dim_out);
9986 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9988 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9989 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9990 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9991 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9992 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9993 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9994 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9995 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9996 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9997 isl_dim_map_div(dim_map1, bmap1, pos += out);
9998 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10000 bmap = isl_basic_map_alloc_space(space_result,
10001 bmap1->n_div + bmap2->n_div,
10002 bmap1->n_eq + bmap2->n_eq,
10003 bmap1->n_ineq + bmap2->n_ineq);
10004 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10005 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10006 bmap = isl_basic_map_simplify(bmap);
10007 return isl_basic_map_finalize(bmap);
10008 error:
10009 isl_basic_map_free(bmap1);
10010 isl_basic_map_free(bmap2);
10011 return NULL;
10014 __isl_give isl_basic_map *isl_basic_map_range_product(
10015 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10017 isl_bool rational;
10018 isl_space *space_result = NULL;
10019 isl_basic_map *bmap;
10020 unsigned in, out1, out2, nparam, total, pos;
10021 struct isl_dim_map *dim_map1, *dim_map2;
10023 rational = isl_basic_map_is_rational(bmap1);
10024 if (rational >= 0 && rational)
10025 rational = isl_basic_map_is_rational(bmap2);
10026 if (!bmap1 || !bmap2 || rational < 0)
10027 goto error;
10029 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10030 goto error;
10032 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10033 isl_space_copy(bmap2->dim));
10035 in = isl_basic_map_dim(bmap1, isl_dim_in);
10036 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10037 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10038 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10040 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10041 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10042 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10043 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10044 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10045 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10046 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10047 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10048 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10049 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10050 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10052 bmap = isl_basic_map_alloc_space(space_result,
10053 bmap1->n_div + bmap2->n_div,
10054 bmap1->n_eq + bmap2->n_eq,
10055 bmap1->n_ineq + bmap2->n_ineq);
10056 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10057 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10058 if (rational)
10059 bmap = isl_basic_map_set_rational(bmap);
10060 bmap = isl_basic_map_simplify(bmap);
10061 return isl_basic_map_finalize(bmap);
10062 error:
10063 isl_basic_map_free(bmap1);
10064 isl_basic_map_free(bmap2);
10065 return NULL;
10068 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10069 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10071 isl_basic_map *prod;
10073 prod = isl_basic_map_range_product(bmap1, bmap2);
10074 prod = isl_basic_map_flatten_range(prod);
10075 return prod;
10078 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10079 * and collect the results.
10080 * The result live in the space obtained by calling "space_product"
10081 * on the spaces of "map1" and "map2".
10082 * If "remove_duplicates" is set then the result may contain duplicates
10083 * (even if the inputs do not) and so we try and remove the obvious
10084 * duplicates.
10086 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10087 __isl_take isl_map *map2,
10088 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10089 __isl_take isl_space *right),
10090 __isl_give isl_basic_map *(*basic_map_product)(
10091 __isl_take isl_basic_map *left,
10092 __isl_take isl_basic_map *right),
10093 int remove_duplicates)
10095 unsigned flags = 0;
10096 struct isl_map *result;
10097 int i, j;
10098 isl_bool m;
10100 m = isl_map_has_equal_params(map1, map2);
10101 if (m < 0)
10102 goto error;
10103 if (!m)
10104 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10105 "parameters don't match", goto error);
10107 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10108 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10109 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10111 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10112 isl_space_copy(map2->dim)),
10113 map1->n * map2->n, flags);
10114 if (!result)
10115 goto error;
10116 for (i = 0; i < map1->n; ++i)
10117 for (j = 0; j < map2->n; ++j) {
10118 struct isl_basic_map *part;
10119 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10120 isl_basic_map_copy(map2->p[j]));
10121 if (isl_basic_map_is_empty(part))
10122 isl_basic_map_free(part);
10123 else
10124 result = isl_map_add_basic_map(result, part);
10125 if (!result)
10126 goto error;
10128 if (remove_duplicates)
10129 result = isl_map_remove_obvious_duplicates(result);
10130 isl_map_free(map1);
10131 isl_map_free(map2);
10132 return result;
10133 error:
10134 isl_map_free(map1);
10135 isl_map_free(map2);
10136 return NULL;
10139 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10141 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10142 __isl_take isl_map *map2)
10144 return map_product(map1, map2, &isl_space_product,
10145 &isl_basic_map_product, 0);
10148 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10149 __isl_take isl_map *map2)
10151 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10154 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10156 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10157 __isl_take isl_map *map2)
10159 isl_map *prod;
10161 prod = isl_map_product(map1, map2);
10162 prod = isl_map_flatten(prod);
10163 return prod;
10166 /* Given two set A and B, construct its Cartesian product A x B.
10168 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10170 return isl_map_range_product(set1, set2);
10173 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10174 __isl_take isl_set *set2)
10176 return isl_map_flat_range_product(set1, set2);
10179 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10181 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10182 __isl_take isl_map *map2)
10184 return map_product(map1, map2, &isl_space_domain_product,
10185 &isl_basic_map_domain_product, 1);
10188 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10190 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10191 __isl_take isl_map *map2)
10193 return map_product(map1, map2, &isl_space_range_product,
10194 &isl_basic_map_range_product, 1);
10197 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10198 __isl_take isl_map *map2)
10200 return isl_map_align_params_map_map_and(map1, map2,
10201 &map_domain_product_aligned);
10204 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10205 __isl_take isl_map *map2)
10207 return isl_map_align_params_map_map_and(map1, map2,
10208 &map_range_product_aligned);
10211 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10213 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10215 isl_space *space;
10216 int total1, keep1, total2, keep2;
10218 if (!map)
10219 return NULL;
10220 if (!isl_space_domain_is_wrapping(map->dim) ||
10221 !isl_space_range_is_wrapping(map->dim))
10222 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10223 "not a product", return isl_map_free(map));
10225 space = isl_map_get_space(map);
10226 total1 = isl_space_dim(space, isl_dim_in);
10227 total2 = isl_space_dim(space, isl_dim_out);
10228 space = isl_space_factor_domain(space);
10229 keep1 = isl_space_dim(space, isl_dim_in);
10230 keep2 = isl_space_dim(space, isl_dim_out);
10231 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10232 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10233 map = isl_map_reset_space(map, space);
10235 return map;
10238 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10240 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10242 isl_space *space;
10243 int total1, keep1, total2, keep2;
10245 if (!map)
10246 return NULL;
10247 if (!isl_space_domain_is_wrapping(map->dim) ||
10248 !isl_space_range_is_wrapping(map->dim))
10249 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10250 "not a product", return isl_map_free(map));
10252 space = isl_map_get_space(map);
10253 total1 = isl_space_dim(space, isl_dim_in);
10254 total2 = isl_space_dim(space, isl_dim_out);
10255 space = isl_space_factor_range(space);
10256 keep1 = isl_space_dim(space, isl_dim_in);
10257 keep2 = isl_space_dim(space, isl_dim_out);
10258 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10259 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10260 map = isl_map_reset_space(map, space);
10262 return map;
10265 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10267 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10269 isl_space *space;
10270 int total, keep;
10272 if (!map)
10273 return NULL;
10274 if (!isl_space_domain_is_wrapping(map->dim))
10275 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10276 "domain is not a product", return isl_map_free(map));
10278 space = isl_map_get_space(map);
10279 total = isl_space_dim(space, isl_dim_in);
10280 space = isl_space_domain_factor_domain(space);
10281 keep = isl_space_dim(space, isl_dim_in);
10282 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10283 map = isl_map_reset_space(map, space);
10285 return map;
10288 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10290 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10292 isl_space *space;
10293 int total, keep;
10295 if (!map)
10296 return NULL;
10297 if (!isl_space_domain_is_wrapping(map->dim))
10298 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10299 "domain is not a product", return isl_map_free(map));
10301 space = isl_map_get_space(map);
10302 total = isl_space_dim(space, isl_dim_in);
10303 space = isl_space_domain_factor_range(space);
10304 keep = isl_space_dim(space, isl_dim_in);
10305 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10306 map = isl_map_reset_space(map, space);
10308 return map;
10311 /* Given a map A -> [B -> C], extract the map A -> B.
10313 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10315 isl_space *space;
10316 int total, keep;
10318 if (!map)
10319 return NULL;
10320 if (!isl_space_range_is_wrapping(map->dim))
10321 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10322 "range is not a product", return isl_map_free(map));
10324 space = isl_map_get_space(map);
10325 total = isl_space_dim(space, isl_dim_out);
10326 space = isl_space_range_factor_domain(space);
10327 keep = isl_space_dim(space, isl_dim_out);
10328 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10329 map = isl_map_reset_space(map, space);
10331 return map;
10334 /* Given a map A -> [B -> C], extract the map A -> C.
10336 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10338 isl_space *space;
10339 int total, keep;
10341 if (!map)
10342 return NULL;
10343 if (!isl_space_range_is_wrapping(map->dim))
10344 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10345 "range is not a product", return isl_map_free(map));
10347 space = isl_map_get_space(map);
10348 total = isl_space_dim(space, isl_dim_out);
10349 space = isl_space_range_factor_range(space);
10350 keep = isl_space_dim(space, isl_dim_out);
10351 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10352 map = isl_map_reset_space(map, space);
10354 return map;
10357 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10359 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10360 __isl_take isl_map *map2)
10362 isl_map *prod;
10364 prod = isl_map_domain_product(map1, map2);
10365 prod = isl_map_flatten_domain(prod);
10366 return prod;
10369 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10371 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10372 __isl_take isl_map *map2)
10374 isl_map *prod;
10376 prod = isl_map_range_product(map1, map2);
10377 prod = isl_map_flatten_range(prod);
10378 return prod;
10381 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10383 int i;
10384 uint32_t hash = isl_hash_init();
10385 unsigned total;
10387 if (!bmap)
10388 return 0;
10389 bmap = isl_basic_map_copy(bmap);
10390 bmap = isl_basic_map_normalize(bmap);
10391 if (!bmap)
10392 return 0;
10393 total = isl_basic_map_total_dim(bmap);
10394 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10395 for (i = 0; i < bmap->n_eq; ++i) {
10396 uint32_t c_hash;
10397 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10398 isl_hash_hash(hash, c_hash);
10400 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10401 for (i = 0; i < bmap->n_ineq; ++i) {
10402 uint32_t c_hash;
10403 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10404 isl_hash_hash(hash, c_hash);
10406 isl_hash_byte(hash, bmap->n_div & 0xFF);
10407 for (i = 0; i < bmap->n_div; ++i) {
10408 uint32_t c_hash;
10409 if (isl_int_is_zero(bmap->div[i][0]))
10410 continue;
10411 isl_hash_byte(hash, i & 0xFF);
10412 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10413 isl_hash_hash(hash, c_hash);
10415 isl_basic_map_free(bmap);
10416 return hash;
10419 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10421 return isl_basic_map_get_hash(bset_to_bmap(bset));
10424 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10426 int i;
10427 uint32_t hash;
10429 if (!map)
10430 return 0;
10431 map = isl_map_copy(map);
10432 map = isl_map_normalize(map);
10433 if (!map)
10434 return 0;
10436 hash = isl_hash_init();
10437 for (i = 0; i < map->n; ++i) {
10438 uint32_t bmap_hash;
10439 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10440 isl_hash_hash(hash, bmap_hash);
10443 isl_map_free(map);
10445 return hash;
10448 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10450 return isl_map_get_hash(set_to_map(set));
10453 /* Return the number of basic maps in the (current) representation of "map".
10455 int isl_map_n_basic_map(__isl_keep isl_map *map)
10457 return map ? map->n : 0;
10460 int isl_set_n_basic_set(__isl_keep isl_set *set)
10462 return set ? set->n : 0;
10465 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10466 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10468 int i;
10470 if (!map)
10471 return isl_stat_error;
10473 for (i = 0; i < map->n; ++i)
10474 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10475 return isl_stat_error;
10477 return isl_stat_ok;
10480 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10481 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10483 int i;
10485 if (!set)
10486 return isl_stat_error;
10488 for (i = 0; i < set->n; ++i)
10489 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10490 return isl_stat_error;
10492 return isl_stat_ok;
10495 /* Return a list of basic sets, the union of which is equal to "set".
10497 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10498 __isl_keep isl_set *set)
10500 int i;
10501 isl_basic_set_list *list;
10503 if (!set)
10504 return NULL;
10506 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10507 for (i = 0; i < set->n; ++i) {
10508 isl_basic_set *bset;
10510 bset = isl_basic_set_copy(set->p[i]);
10511 list = isl_basic_set_list_add(list, bset);
10514 return list;
10517 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10519 isl_space *space;
10521 if (!bset)
10522 return NULL;
10524 bset = isl_basic_set_cow(bset);
10525 if (!bset)
10526 return NULL;
10528 space = isl_basic_set_get_space(bset);
10529 space = isl_space_lift(space, bset->n_div);
10530 if (!space)
10531 goto error;
10532 isl_space_free(bset->dim);
10533 bset->dim = space;
10534 bset->extra -= bset->n_div;
10535 bset->n_div = 0;
10537 bset = isl_basic_set_finalize(bset);
10539 return bset;
10540 error:
10541 isl_basic_set_free(bset);
10542 return NULL;
10545 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10547 int i;
10548 isl_space *space;
10549 unsigned n_div;
10551 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10553 if (!set)
10554 return NULL;
10556 set = isl_set_cow(set);
10557 if (!set)
10558 return NULL;
10560 n_div = set->p[0]->n_div;
10561 space = isl_set_get_space(set);
10562 space = isl_space_lift(space, n_div);
10563 if (!space)
10564 goto error;
10565 isl_space_free(set->dim);
10566 set->dim = space;
10568 for (i = 0; i < set->n; ++i) {
10569 set->p[i] = isl_basic_set_lift(set->p[i]);
10570 if (!set->p[i])
10571 goto error;
10574 return set;
10575 error:
10576 isl_set_free(set);
10577 return NULL;
10580 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10582 unsigned dim;
10583 int size = 0;
10585 if (!bset)
10586 return -1;
10588 dim = isl_basic_set_total_dim(bset);
10589 size += bset->n_eq * (1 + dim);
10590 size += bset->n_ineq * (1 + dim);
10591 size += bset->n_div * (2 + dim);
10593 return size;
10596 int isl_set_size(__isl_keep isl_set *set)
10598 int i;
10599 int size = 0;
10601 if (!set)
10602 return -1;
10604 for (i = 0; i < set->n; ++i)
10605 size += isl_basic_set_size(set->p[i]);
10607 return size;
10610 /* Check if there is any lower bound (if lower == 0) and/or upper
10611 * bound (if upper == 0) on the specified dim.
10613 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10614 enum isl_dim_type type, unsigned pos, int lower, int upper)
10616 int i;
10618 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10619 return isl_bool_error;
10621 pos += isl_basic_map_offset(bmap, type);
10623 for (i = 0; i < bmap->n_div; ++i) {
10624 if (isl_int_is_zero(bmap->div[i][0]))
10625 continue;
10626 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10627 return isl_bool_true;
10630 for (i = 0; i < bmap->n_eq; ++i)
10631 if (!isl_int_is_zero(bmap->eq[i][pos]))
10632 return isl_bool_true;
10634 for (i = 0; i < bmap->n_ineq; ++i) {
10635 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10636 if (sgn > 0)
10637 lower = 1;
10638 if (sgn < 0)
10639 upper = 1;
10642 return lower && upper;
10645 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10646 enum isl_dim_type type, unsigned pos)
10648 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10651 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10652 enum isl_dim_type type, unsigned pos)
10654 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10657 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10658 enum isl_dim_type type, unsigned pos)
10660 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10663 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10664 enum isl_dim_type type, unsigned pos)
10666 int i;
10668 if (!map)
10669 return isl_bool_error;
10671 for (i = 0; i < map->n; ++i) {
10672 isl_bool bounded;
10673 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10674 if (bounded < 0 || !bounded)
10675 return bounded;
10678 return isl_bool_true;
10681 /* Return true if the specified dim is involved in both an upper bound
10682 * and a lower bound.
10684 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10685 enum isl_dim_type type, unsigned pos)
10687 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10690 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10692 static isl_bool has_any_bound(__isl_keep isl_map *map,
10693 enum isl_dim_type type, unsigned pos,
10694 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10695 enum isl_dim_type type, unsigned pos))
10697 int i;
10699 if (!map)
10700 return isl_bool_error;
10702 for (i = 0; i < map->n; ++i) {
10703 isl_bool bounded;
10704 bounded = fn(map->p[i], type, pos);
10705 if (bounded < 0 || bounded)
10706 return bounded;
10709 return isl_bool_false;
10712 /* Return 1 if the specified dim is involved in any lower bound.
10714 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10715 enum isl_dim_type type, unsigned pos)
10717 return has_any_bound(set, type, pos,
10718 &isl_basic_map_dim_has_lower_bound);
10721 /* Return 1 if the specified dim is involved in any upper bound.
10723 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10724 enum isl_dim_type type, unsigned pos)
10726 return has_any_bound(set, type, pos,
10727 &isl_basic_map_dim_has_upper_bound);
10730 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10732 static isl_bool has_bound(__isl_keep isl_map *map,
10733 enum isl_dim_type type, unsigned pos,
10734 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10735 enum isl_dim_type type, unsigned pos))
10737 int i;
10739 if (!map)
10740 return isl_bool_error;
10742 for (i = 0; i < map->n; ++i) {
10743 isl_bool bounded;
10744 bounded = fn(map->p[i], type, pos);
10745 if (bounded < 0 || !bounded)
10746 return bounded;
10749 return isl_bool_true;
10752 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10754 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10755 enum isl_dim_type type, unsigned pos)
10757 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10760 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10762 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10763 enum isl_dim_type type, unsigned pos)
10765 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10768 /* For each of the "n" variables starting at "first", determine
10769 * the sign of the variable and put the results in the first "n"
10770 * elements of the array "signs".
10771 * Sign
10772 * 1 means that the variable is non-negative
10773 * -1 means that the variable is non-positive
10774 * 0 means the variable attains both positive and negative values.
10776 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10777 unsigned first, unsigned n, int *signs)
10779 isl_vec *bound = NULL;
10780 struct isl_tab *tab = NULL;
10781 struct isl_tab_undo *snap;
10782 int i;
10784 if (!bset || !signs)
10785 return isl_stat_error;
10787 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10788 tab = isl_tab_from_basic_set(bset, 0);
10789 if (!bound || !tab)
10790 goto error;
10792 isl_seq_clr(bound->el, bound->size);
10793 isl_int_set_si(bound->el[0], -1);
10795 snap = isl_tab_snap(tab);
10796 for (i = 0; i < n; ++i) {
10797 int empty;
10799 isl_int_set_si(bound->el[1 + first + i], -1);
10800 if (isl_tab_add_ineq(tab, bound->el) < 0)
10801 goto error;
10802 empty = tab->empty;
10803 isl_int_set_si(bound->el[1 + first + i], 0);
10804 if (isl_tab_rollback(tab, snap) < 0)
10805 goto error;
10807 if (empty) {
10808 signs[i] = 1;
10809 continue;
10812 isl_int_set_si(bound->el[1 + first + i], 1);
10813 if (isl_tab_add_ineq(tab, bound->el) < 0)
10814 goto error;
10815 empty = tab->empty;
10816 isl_int_set_si(bound->el[1 + first + i], 0);
10817 if (isl_tab_rollback(tab, snap) < 0)
10818 goto error;
10820 signs[i] = empty ? -1 : 0;
10823 isl_tab_free(tab);
10824 isl_vec_free(bound);
10825 return isl_stat_ok;
10826 error:
10827 isl_tab_free(tab);
10828 isl_vec_free(bound);
10829 return isl_stat_error;
10832 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10833 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10835 if (!bset || !signs)
10836 return isl_stat_error;
10837 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10838 return isl_stat_error);
10840 first += pos(bset->dim, type) - 1;
10841 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10844 /* Is it possible for the integer division "div" to depend (possibly
10845 * indirectly) on any output dimensions?
10847 * If the div is undefined, then we conservatively assume that it
10848 * may depend on them.
10849 * Otherwise, we check if it actually depends on them or on any integer
10850 * divisions that may depend on them.
10852 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10854 int i;
10855 unsigned n_out, o_out;
10856 unsigned n_div, o_div;
10858 if (isl_int_is_zero(bmap->div[div][0]))
10859 return isl_bool_true;
10861 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10862 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10864 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10865 return isl_bool_true;
10867 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10868 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10870 for (i = 0; i < n_div; ++i) {
10871 isl_bool may_involve;
10873 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10874 continue;
10875 may_involve = div_may_involve_output(bmap, i);
10876 if (may_involve < 0 || may_involve)
10877 return may_involve;
10880 return isl_bool_false;
10883 /* Return the first integer division of "bmap" in the range
10884 * [first, first + n[ that may depend on any output dimensions and
10885 * that has a non-zero coefficient in "c" (where the first coefficient
10886 * in "c" corresponds to integer division "first").
10888 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10889 isl_int *c, int first, int n)
10891 int k;
10893 if (!bmap)
10894 return -1;
10896 for (k = first; k < first + n; ++k) {
10897 isl_bool may_involve;
10899 if (isl_int_is_zero(c[k]))
10900 continue;
10901 may_involve = div_may_involve_output(bmap, k);
10902 if (may_involve < 0)
10903 return -1;
10904 if (may_involve)
10905 return k;
10908 return first + n;
10911 /* Look for a pair of inequality constraints in "bmap" of the form
10913 * -l + i >= 0 or i >= l
10914 * and
10915 * n + l - i >= 0 or i <= l + n
10917 * with n < "m" and i the output dimension at position "pos".
10918 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10919 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10920 * and earlier output dimensions, as well as integer divisions that do
10921 * not involve any of the output dimensions.
10923 * Return the index of the first inequality constraint or bmap->n_ineq
10924 * if no such pair can be found.
10926 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10927 int pos, isl_int m)
10929 int i, j;
10930 isl_ctx *ctx;
10931 unsigned total;
10932 unsigned n_div, o_div;
10933 unsigned n_out, o_out;
10934 int less;
10936 if (!bmap)
10937 return -1;
10939 ctx = isl_basic_map_get_ctx(bmap);
10940 total = isl_basic_map_total_dim(bmap);
10941 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10942 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10943 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10944 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10945 for (i = 0; i < bmap->n_ineq; ++i) {
10946 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10947 continue;
10948 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10949 n_out - (pos + 1)) != -1)
10950 continue;
10951 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10952 0, n_div) < n_div)
10953 continue;
10954 for (j = i + 1; j < bmap->n_ineq; ++j) {
10955 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10956 ctx->one))
10957 continue;
10958 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10959 bmap->ineq[j] + 1, total))
10960 continue;
10961 break;
10963 if (j >= bmap->n_ineq)
10964 continue;
10965 isl_int_add(bmap->ineq[i][0],
10966 bmap->ineq[i][0], bmap->ineq[j][0]);
10967 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10968 isl_int_sub(bmap->ineq[i][0],
10969 bmap->ineq[i][0], bmap->ineq[j][0]);
10970 if (!less)
10971 continue;
10972 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10973 return i;
10974 else
10975 return j;
10978 return bmap->n_ineq;
10981 /* Return the index of the equality of "bmap" that defines
10982 * the output dimension "pos" in terms of earlier dimensions.
10983 * The equality may also involve integer divisions, as long
10984 * as those integer divisions are defined in terms of
10985 * parameters or input dimensions.
10986 * In this case, *div is set to the number of integer divisions and
10987 * *ineq is set to the number of inequality constraints (provided
10988 * div and ineq are not NULL).
10990 * The equality may also involve a single integer division involving
10991 * the output dimensions (typically only output dimension "pos") as
10992 * long as the coefficient of output dimension "pos" is 1 or -1 and
10993 * there is a pair of constraints i >= l and i <= l + n, with i referring
10994 * to output dimension "pos", l an expression involving only earlier
10995 * dimensions and n smaller than the coefficient of the integer division
10996 * in the equality. In this case, the output dimension can be defined
10997 * in terms of a modulo expression that does not involve the integer division.
10998 * *div is then set to this single integer division and
10999 * *ineq is set to the index of constraint i >= l.
11001 * Return bmap->n_eq if there is no such equality.
11002 * Return -1 on error.
11004 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11005 int pos, int *div, int *ineq)
11007 int j, k, l;
11008 unsigned n_out, o_out;
11009 unsigned n_div, o_div;
11011 if (!bmap)
11012 return -1;
11014 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11015 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11016 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11017 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11019 if (ineq)
11020 *ineq = bmap->n_ineq;
11021 if (div)
11022 *div = n_div;
11023 for (j = 0; j < bmap->n_eq; ++j) {
11024 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11025 continue;
11026 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11027 n_out - (pos + 1)) != -1)
11028 continue;
11029 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11030 0, n_div);
11031 if (k >= n_div)
11032 return j;
11033 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11034 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11035 continue;
11036 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11037 k + 1, n_div - (k+1)) < n_div)
11038 continue;
11039 l = find_modulo_constraint_pair(bmap, pos,
11040 bmap->eq[j][o_div + k]);
11041 if (l < 0)
11042 return -1;
11043 if (l >= bmap->n_ineq)
11044 continue;
11045 if (div)
11046 *div = k;
11047 if (ineq)
11048 *ineq = l;
11049 return j;
11052 return bmap->n_eq;
11055 /* Check if the given basic map is obviously single-valued.
11056 * In particular, for each output dimension, check that there is
11057 * an equality that defines the output dimension in terms of
11058 * earlier dimensions.
11060 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11062 int i;
11063 unsigned n_out;
11065 if (!bmap)
11066 return isl_bool_error;
11068 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11070 for (i = 0; i < n_out; ++i) {
11071 int eq;
11073 eq = isl_basic_map_output_defining_equality(bmap, i,
11074 NULL, NULL);
11075 if (eq < 0)
11076 return isl_bool_error;
11077 if (eq >= bmap->n_eq)
11078 return isl_bool_false;
11081 return isl_bool_true;
11084 /* Check if the given basic map is single-valued.
11085 * We simply compute
11087 * M \circ M^-1
11089 * and check if the result is a subset of the identity mapping.
11091 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11093 isl_space *space;
11094 isl_basic_map *test;
11095 isl_basic_map *id;
11096 isl_bool sv;
11098 sv = isl_basic_map_plain_is_single_valued(bmap);
11099 if (sv < 0 || sv)
11100 return sv;
11102 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11103 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11105 space = isl_basic_map_get_space(bmap);
11106 space = isl_space_map_from_set(isl_space_range(space));
11107 id = isl_basic_map_identity(space);
11109 sv = isl_basic_map_is_subset(test, id);
11111 isl_basic_map_free(test);
11112 isl_basic_map_free(id);
11114 return sv;
11117 /* Check if the given map is obviously single-valued.
11119 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11121 if (!map)
11122 return isl_bool_error;
11123 if (map->n == 0)
11124 return isl_bool_true;
11125 if (map->n >= 2)
11126 return isl_bool_false;
11128 return isl_basic_map_plain_is_single_valued(map->p[0]);
11131 /* Check if the given map is single-valued.
11132 * We simply compute
11134 * M \circ M^-1
11136 * and check if the result is a subset of the identity mapping.
11138 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11140 isl_space *dim;
11141 isl_map *test;
11142 isl_map *id;
11143 isl_bool sv;
11145 sv = isl_map_plain_is_single_valued(map);
11146 if (sv < 0 || sv)
11147 return sv;
11149 test = isl_map_reverse(isl_map_copy(map));
11150 test = isl_map_apply_range(test, isl_map_copy(map));
11152 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11153 id = isl_map_identity(dim);
11155 sv = isl_map_is_subset(test, id);
11157 isl_map_free(test);
11158 isl_map_free(id);
11160 return sv;
11163 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11165 isl_bool in;
11167 map = isl_map_copy(map);
11168 map = isl_map_reverse(map);
11169 in = isl_map_is_single_valued(map);
11170 isl_map_free(map);
11172 return in;
11175 /* Check if the given map is obviously injective.
11177 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11179 isl_bool in;
11181 map = isl_map_copy(map);
11182 map = isl_map_reverse(map);
11183 in = isl_map_plain_is_single_valued(map);
11184 isl_map_free(map);
11186 return in;
11189 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11191 isl_bool sv;
11193 sv = isl_map_is_single_valued(map);
11194 if (sv < 0 || !sv)
11195 return sv;
11197 return isl_map_is_injective(map);
11200 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11202 return isl_map_is_single_valued(set_to_map(set));
11205 /* Does "map" only map elements to themselves?
11207 * If the domain and range spaces are different, then "map"
11208 * is considered not to be an identity relation, even if it is empty.
11209 * Otherwise, construct the maximal identity relation and
11210 * check whether "map" is a subset of this relation.
11212 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11214 isl_space *space;
11215 isl_map *id;
11216 isl_bool equal, is_identity;
11218 space = isl_map_get_space(map);
11219 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11220 isl_space_free(space);
11221 if (equal < 0 || !equal)
11222 return equal;
11224 id = isl_map_identity(isl_map_get_space(map));
11225 is_identity = isl_map_is_subset(map, id);
11226 isl_map_free(id);
11228 return is_identity;
11231 int isl_map_is_translation(__isl_keep isl_map *map)
11233 int ok;
11234 isl_set *delta;
11236 delta = isl_map_deltas(isl_map_copy(map));
11237 ok = isl_set_is_singleton(delta);
11238 isl_set_free(delta);
11240 return ok;
11243 static int unique(isl_int *p, unsigned pos, unsigned len)
11245 if (isl_seq_first_non_zero(p, pos) != -1)
11246 return 0;
11247 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11248 return 0;
11249 return 1;
11252 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11254 int i, j;
11255 unsigned nvar;
11256 unsigned ovar;
11258 if (!bset)
11259 return isl_bool_error;
11261 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11262 return isl_bool_false;
11264 nvar = isl_basic_set_dim(bset, isl_dim_set);
11265 ovar = isl_space_offset(bset->dim, isl_dim_set);
11266 for (j = 0; j < nvar; ++j) {
11267 int lower = 0, upper = 0;
11268 for (i = 0; i < bset->n_eq; ++i) {
11269 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11270 continue;
11271 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11272 return isl_bool_false;
11273 break;
11275 if (i < bset->n_eq)
11276 continue;
11277 for (i = 0; i < bset->n_ineq; ++i) {
11278 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11279 continue;
11280 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11281 return isl_bool_false;
11282 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11283 lower = 1;
11284 else
11285 upper = 1;
11287 if (!lower || !upper)
11288 return isl_bool_false;
11291 return isl_bool_true;
11294 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11296 if (!set)
11297 return isl_bool_error;
11298 if (set->n != 1)
11299 return isl_bool_false;
11301 return isl_basic_set_is_box(set->p[0]);
11304 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11306 if (!bset)
11307 return isl_bool_error;
11309 return isl_space_is_wrapping(bset->dim);
11312 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11314 if (!set)
11315 return isl_bool_error;
11317 return isl_space_is_wrapping(set->dim);
11320 /* Modify the space of "map" through a call to "change".
11321 * If "can_change" is set (not NULL), then first call it to check
11322 * if the modification is allowed, printing the error message "cannot_change"
11323 * if it is not.
11325 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11326 isl_bool (*can_change)(__isl_keep isl_map *map),
11327 const char *cannot_change,
11328 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11330 isl_bool ok;
11331 isl_space *space;
11333 if (!map)
11334 return NULL;
11336 ok = can_change ? can_change(map) : isl_bool_true;
11337 if (ok < 0)
11338 return isl_map_free(map);
11339 if (!ok)
11340 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11341 return isl_map_free(map));
11343 space = change(isl_map_get_space(map));
11344 map = isl_map_reset_space(map, space);
11346 return map;
11349 /* Is the domain of "map" a wrapped relation?
11351 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11353 if (!map)
11354 return isl_bool_error;
11356 return isl_space_domain_is_wrapping(map->dim);
11359 /* Does "map" have a wrapped relation in both domain and range?
11361 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11363 return isl_space_is_product(isl_map_peek_space(map));
11366 /* Is the range of "map" a wrapped relation?
11368 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11370 if (!map)
11371 return isl_bool_error;
11373 return isl_space_range_is_wrapping(map->dim);
11376 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11378 bmap = isl_basic_map_cow(bmap);
11379 if (!bmap)
11380 return NULL;
11382 bmap->dim = isl_space_wrap(bmap->dim);
11383 if (!bmap->dim)
11384 goto error;
11386 bmap = isl_basic_map_finalize(bmap);
11388 return bset_from_bmap(bmap);
11389 error:
11390 isl_basic_map_free(bmap);
11391 return NULL;
11394 /* Given a map A -> B, return the set (A -> B).
11396 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11398 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11401 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11403 bset = isl_basic_set_cow(bset);
11404 if (!bset)
11405 return NULL;
11407 bset->dim = isl_space_unwrap(bset->dim);
11408 if (!bset->dim)
11409 goto error;
11411 bset = isl_basic_set_finalize(bset);
11413 return bset_to_bmap(bset);
11414 error:
11415 isl_basic_set_free(bset);
11416 return NULL;
11419 /* Given a set (A -> B), return the map A -> B.
11420 * Error out if "set" is not of the form (A -> B).
11422 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11424 return isl_map_change_space(set, &isl_set_is_wrapping,
11425 "not a wrapping set", &isl_space_unwrap);
11428 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11429 enum isl_dim_type type)
11431 if (!bmap)
11432 return NULL;
11434 if (!isl_space_is_named_or_nested(bmap->dim, type))
11435 return bmap;
11437 bmap = isl_basic_map_cow(bmap);
11438 if (!bmap)
11439 return NULL;
11441 bmap->dim = isl_space_reset(bmap->dim, type);
11442 if (!bmap->dim)
11443 goto error;
11445 bmap = isl_basic_map_finalize(bmap);
11447 return bmap;
11448 error:
11449 isl_basic_map_free(bmap);
11450 return NULL;
11453 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11454 enum isl_dim_type type)
11456 int i;
11458 if (!map)
11459 return NULL;
11461 if (!isl_space_is_named_or_nested(map->dim, type))
11462 return map;
11464 map = isl_map_cow(map);
11465 if (!map)
11466 return NULL;
11468 for (i = 0; i < map->n; ++i) {
11469 map->p[i] = isl_basic_map_reset(map->p[i], type);
11470 if (!map->p[i])
11471 goto error;
11473 map->dim = isl_space_reset(map->dim, type);
11474 if (!map->dim)
11475 goto error;
11477 return map;
11478 error:
11479 isl_map_free(map);
11480 return NULL;
11483 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11485 if (!bmap)
11486 return NULL;
11488 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11489 return bmap;
11491 bmap = isl_basic_map_cow(bmap);
11492 if (!bmap)
11493 return NULL;
11495 bmap->dim = isl_space_flatten(bmap->dim);
11496 if (!bmap->dim)
11497 goto error;
11499 bmap = isl_basic_map_finalize(bmap);
11501 return bmap;
11502 error:
11503 isl_basic_map_free(bmap);
11504 return NULL;
11507 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11509 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11512 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11513 __isl_take isl_basic_map *bmap)
11515 if (!bmap)
11516 return NULL;
11518 if (!bmap->dim->nested[0])
11519 return bmap;
11521 bmap = isl_basic_map_cow(bmap);
11522 if (!bmap)
11523 return NULL;
11525 bmap->dim = isl_space_flatten_domain(bmap->dim);
11526 if (!bmap->dim)
11527 goto error;
11529 bmap = isl_basic_map_finalize(bmap);
11531 return bmap;
11532 error:
11533 isl_basic_map_free(bmap);
11534 return NULL;
11537 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11538 __isl_take isl_basic_map *bmap)
11540 if (!bmap)
11541 return NULL;
11543 if (!bmap->dim->nested[1])
11544 return bmap;
11546 bmap = isl_basic_map_cow(bmap);
11547 if (!bmap)
11548 return NULL;
11550 bmap->dim = isl_space_flatten_range(bmap->dim);
11551 if (!bmap->dim)
11552 goto error;
11554 bmap = isl_basic_map_finalize(bmap);
11556 return bmap;
11557 error:
11558 isl_basic_map_free(bmap);
11559 return NULL;
11562 /* Remove any internal structure from the spaces of domain and range of "map".
11564 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11566 if (!map)
11567 return NULL;
11569 if (!map->dim->nested[0] && !map->dim->nested[1])
11570 return map;
11572 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11575 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11577 return set_from_map(isl_map_flatten(set_to_map(set)));
11580 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11582 isl_space *space, *flat_space;
11583 isl_map *map;
11585 space = isl_set_get_space(set);
11586 flat_space = isl_space_flatten(isl_space_copy(space));
11587 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11588 flat_space));
11589 map = isl_map_intersect_domain(map, set);
11591 return map;
11594 /* Remove any internal structure from the space of the domain of "map".
11596 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11598 if (!map)
11599 return NULL;
11601 if (!map->dim->nested[0])
11602 return map;
11604 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11607 /* Remove any internal structure from the space of the range of "map".
11609 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11611 if (!map)
11612 return NULL;
11614 if (!map->dim->nested[1])
11615 return map;
11617 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11620 /* Reorder the dimensions of "bmap" according to the given dim_map
11621 * and set the dimension specification to "space" and
11622 * perform Gaussian elimination on the result.
11624 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11625 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11627 isl_basic_map *res;
11628 unsigned flags;
11629 unsigned n_div;
11631 if (!bmap || !space || !dim_map)
11632 goto error;
11634 flags = bmap->flags;
11635 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11636 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11637 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11638 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11639 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11640 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11641 if (res)
11642 res->flags = flags;
11643 res = isl_basic_map_gauss(res, NULL);
11644 res = isl_basic_map_finalize(res);
11645 return res;
11646 error:
11647 free(dim_map);
11648 isl_basic_map_free(bmap);
11649 isl_space_free(space);
11650 return NULL;
11653 /* Reorder the dimensions of "map" according to given reordering.
11655 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11656 __isl_take isl_reordering *r)
11658 int i;
11659 struct isl_dim_map *dim_map;
11661 map = isl_map_cow(map);
11662 dim_map = isl_dim_map_from_reordering(r);
11663 if (!map || !r || !dim_map)
11664 goto error;
11666 for (i = 0; i < map->n; ++i) {
11667 struct isl_dim_map *dim_map_i;
11668 isl_space *space;
11670 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11672 space = isl_reordering_get_space(r);
11673 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11675 if (!map->p[i])
11676 goto error;
11679 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11680 map = isl_map_unmark_normalized(map);
11682 isl_reordering_free(r);
11683 free(dim_map);
11684 return map;
11685 error:
11686 free(dim_map);
11687 isl_map_free(map);
11688 isl_reordering_free(r);
11689 return NULL;
11692 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11693 __isl_take isl_reordering *r)
11695 return set_from_map(isl_map_realign(set_to_map(set), r));
11698 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11699 __isl_take isl_space *model)
11701 isl_ctx *ctx;
11702 isl_bool aligned;
11704 if (!map || !model)
11705 goto error;
11707 ctx = isl_space_get_ctx(model);
11708 if (!isl_space_has_named_params(model))
11709 isl_die(ctx, isl_error_invalid,
11710 "model has unnamed parameters", goto error);
11711 if (isl_map_check_named_params(map) < 0)
11712 goto error;
11713 aligned = isl_map_space_has_equal_params(map, model);
11714 if (aligned < 0)
11715 goto error;
11716 if (!aligned) {
11717 isl_reordering *exp;
11719 exp = isl_parameter_alignment_reordering(map->dim, model);
11720 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11721 map = isl_map_realign(map, exp);
11724 isl_space_free(model);
11725 return map;
11726 error:
11727 isl_space_free(model);
11728 isl_map_free(map);
11729 return NULL;
11732 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11733 __isl_take isl_space *model)
11735 return isl_map_align_params(set, model);
11738 /* Align the parameters of "bmap" to those of "model", introducing
11739 * additional parameters if needed.
11741 __isl_give isl_basic_map *isl_basic_map_align_params(
11742 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11744 isl_ctx *ctx;
11745 isl_bool equal_params;
11747 if (!bmap || !model)
11748 goto error;
11750 ctx = isl_space_get_ctx(model);
11751 if (!isl_space_has_named_params(model))
11752 isl_die(ctx, isl_error_invalid,
11753 "model has unnamed parameters", goto error);
11754 if (isl_basic_map_check_named_params(bmap) < 0)
11755 goto error;
11756 equal_params = isl_space_has_equal_params(bmap->dim, model);
11757 if (equal_params < 0)
11758 goto error;
11759 if (!equal_params) {
11760 isl_reordering *exp;
11761 struct isl_dim_map *dim_map;
11763 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11764 exp = isl_reordering_extend_space(exp,
11765 isl_basic_map_get_space(bmap));
11766 dim_map = isl_dim_map_from_reordering(exp);
11767 bmap = isl_basic_map_realign(bmap,
11768 isl_reordering_get_space(exp),
11769 isl_dim_map_extend(dim_map, bmap));
11770 isl_reordering_free(exp);
11771 free(dim_map);
11774 isl_space_free(model);
11775 return bmap;
11776 error:
11777 isl_space_free(model);
11778 isl_basic_map_free(bmap);
11779 return NULL;
11782 /* Do "bset" and "space" have the same parameters?
11784 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11785 __isl_keep isl_space *space)
11787 isl_space *bset_space;
11789 bset_space = isl_basic_set_peek_space(bset);
11790 return isl_space_has_equal_params(bset_space, space);
11793 /* Do "map" and "space" have the same parameters?
11795 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11796 __isl_keep isl_space *space)
11798 isl_space *map_space;
11800 map_space = isl_map_peek_space(map);
11801 return isl_space_has_equal_params(map_space, space);
11804 /* Do "set" and "space" have the same parameters?
11806 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11807 __isl_keep isl_space *space)
11809 return isl_map_space_has_equal_params(set_to_map(set), space);
11812 /* Align the parameters of "bset" to those of "model", introducing
11813 * additional parameters if needed.
11815 __isl_give isl_basic_set *isl_basic_set_align_params(
11816 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11818 return isl_basic_map_align_params(bset, model);
11821 /* Drop all parameters not referenced by "map".
11823 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11825 int i;
11827 if (isl_map_check_named_params(map) < 0)
11828 return isl_map_free(map);
11830 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11831 isl_bool involves;
11833 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11834 if (involves < 0)
11835 return isl_map_free(map);
11836 if (!involves)
11837 map = isl_map_project_out(map, isl_dim_param, i, 1);
11840 return map;
11843 /* Drop all parameters not referenced by "set".
11845 __isl_give isl_set *isl_set_drop_unused_params(
11846 __isl_take isl_set *set)
11848 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11851 /* Drop all parameters not referenced by "bmap".
11853 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11854 __isl_take isl_basic_map *bmap)
11856 int i;
11858 if (isl_basic_map_check_named_params(bmap) < 0)
11859 return isl_basic_map_free(bmap);
11861 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11862 isl_bool involves;
11864 involves = isl_basic_map_involves_dims(bmap,
11865 isl_dim_param, i, 1);
11866 if (involves < 0)
11867 return isl_basic_map_free(bmap);
11868 if (!involves)
11869 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11872 return bmap;
11875 /* Drop all parameters not referenced by "bset".
11877 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11878 __isl_take isl_basic_set *bset)
11880 return bset_from_bmap(isl_basic_map_drop_unused_params(
11881 bset_to_bmap(bset)));
11884 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11885 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11886 enum isl_dim_type c2, enum isl_dim_type c3,
11887 enum isl_dim_type c4, enum isl_dim_type c5)
11889 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11890 struct isl_mat *mat;
11891 int i, j, k;
11892 int pos;
11894 if (!bmap)
11895 return NULL;
11896 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11897 isl_basic_map_total_dim(bmap) + 1);
11898 if (!mat)
11899 return NULL;
11900 for (i = 0; i < bmap->n_eq; ++i)
11901 for (j = 0, pos = 0; j < 5; ++j) {
11902 int off = isl_basic_map_offset(bmap, c[j]);
11903 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11904 isl_int_set(mat->row[i][pos],
11905 bmap->eq[i][off + k]);
11906 ++pos;
11910 return mat;
11913 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11914 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11915 enum isl_dim_type c2, enum isl_dim_type c3,
11916 enum isl_dim_type c4, enum isl_dim_type c5)
11918 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11919 struct isl_mat *mat;
11920 int i, j, k;
11921 int pos;
11923 if (!bmap)
11924 return NULL;
11925 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11926 isl_basic_map_total_dim(bmap) + 1);
11927 if (!mat)
11928 return NULL;
11929 for (i = 0; i < bmap->n_ineq; ++i)
11930 for (j = 0, pos = 0; j < 5; ++j) {
11931 int off = isl_basic_map_offset(bmap, c[j]);
11932 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11933 isl_int_set(mat->row[i][pos],
11934 bmap->ineq[i][off + k]);
11935 ++pos;
11939 return mat;
11942 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11943 __isl_take isl_space *space,
11944 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11945 enum isl_dim_type c2, enum isl_dim_type c3,
11946 enum isl_dim_type c4, enum isl_dim_type c5)
11948 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11949 isl_basic_map *bmap = NULL;
11950 unsigned total;
11951 unsigned extra;
11952 int i, j, k, l;
11953 int pos;
11955 if (!space || !eq || !ineq)
11956 goto error;
11958 if (eq->n_col != ineq->n_col)
11959 isl_die(space->ctx, isl_error_invalid,
11960 "equalities and inequalities matrices should have "
11961 "same number of columns", goto error);
11963 total = 1 + isl_space_dim(space, isl_dim_all);
11965 if (eq->n_col < total)
11966 isl_die(space->ctx, isl_error_invalid,
11967 "number of columns too small", goto error);
11969 extra = eq->n_col - total;
11971 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11972 eq->n_row, ineq->n_row);
11973 if (!bmap)
11974 goto error;
11975 for (i = 0; i < extra; ++i) {
11976 k = isl_basic_map_alloc_div(bmap);
11977 if (k < 0)
11978 goto error;
11979 isl_int_set_si(bmap->div[k][0], 0);
11981 for (i = 0; i < eq->n_row; ++i) {
11982 l = isl_basic_map_alloc_equality(bmap);
11983 if (l < 0)
11984 goto error;
11985 for (j = 0, pos = 0; j < 5; ++j) {
11986 int off = isl_basic_map_offset(bmap, c[j]);
11987 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11988 isl_int_set(bmap->eq[l][off + k],
11989 eq->row[i][pos]);
11990 ++pos;
11994 for (i = 0; i < ineq->n_row; ++i) {
11995 l = isl_basic_map_alloc_inequality(bmap);
11996 if (l < 0)
11997 goto error;
11998 for (j = 0, pos = 0; j < 5; ++j) {
11999 int off = isl_basic_map_offset(bmap, c[j]);
12000 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
12001 isl_int_set(bmap->ineq[l][off + k],
12002 ineq->row[i][pos]);
12003 ++pos;
12008 isl_space_free(space);
12009 isl_mat_free(eq);
12010 isl_mat_free(ineq);
12012 bmap = isl_basic_map_simplify(bmap);
12013 return isl_basic_map_finalize(bmap);
12014 error:
12015 isl_space_free(space);
12016 isl_mat_free(eq);
12017 isl_mat_free(ineq);
12018 isl_basic_map_free(bmap);
12019 return NULL;
12022 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12023 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12024 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12026 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12027 c1, c2, c3, c4, isl_dim_in);
12030 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12031 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12032 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12034 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12035 c1, c2, c3, c4, isl_dim_in);
12038 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12039 __isl_take isl_space *dim,
12040 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12041 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12043 isl_basic_map *bmap;
12044 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12045 c1, c2, c3, c4, isl_dim_in);
12046 return bset_from_bmap(bmap);
12049 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12051 if (!bmap)
12052 return isl_bool_error;
12054 return isl_space_can_zip(bmap->dim);
12057 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12059 if (!map)
12060 return isl_bool_error;
12062 return isl_space_can_zip(map->dim);
12065 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12066 * (A -> C) -> (B -> D).
12068 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12070 unsigned pos;
12071 unsigned n1;
12072 unsigned n2;
12074 if (!bmap)
12075 return NULL;
12077 if (!isl_basic_map_can_zip(bmap))
12078 isl_die(bmap->ctx, isl_error_invalid,
12079 "basic map cannot be zipped", goto error);
12080 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12081 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12082 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12083 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12084 bmap = isl_basic_map_cow(bmap);
12085 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12086 if (!bmap)
12087 return NULL;
12088 bmap->dim = isl_space_zip(bmap->dim);
12089 if (!bmap->dim)
12090 goto error;
12091 bmap = isl_basic_map_mark_final(bmap);
12092 return bmap;
12093 error:
12094 isl_basic_map_free(bmap);
12095 return NULL;
12098 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12099 * (A -> C) -> (B -> D).
12101 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12103 int i;
12105 if (!map)
12106 return NULL;
12108 if (!isl_map_can_zip(map))
12109 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12110 goto error);
12112 map = isl_map_cow(map);
12113 if (!map)
12114 return NULL;
12116 for (i = 0; i < map->n; ++i) {
12117 map->p[i] = isl_basic_map_zip(map->p[i]);
12118 if (!map->p[i])
12119 goto error;
12122 map->dim = isl_space_zip(map->dim);
12123 if (!map->dim)
12124 goto error;
12126 return map;
12127 error:
12128 isl_map_free(map);
12129 return NULL;
12132 /* Can we apply isl_basic_map_curry to "bmap"?
12133 * That is, does it have a nested relation in its domain?
12135 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12137 if (!bmap)
12138 return isl_bool_error;
12140 return isl_space_can_curry(bmap->dim);
12143 /* Can we apply isl_map_curry to "map"?
12144 * That is, does it have a nested relation in its domain?
12146 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12148 if (!map)
12149 return isl_bool_error;
12151 return isl_space_can_curry(map->dim);
12154 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12155 * A -> (B -> C).
12157 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12160 if (!bmap)
12161 return NULL;
12163 if (!isl_basic_map_can_curry(bmap))
12164 isl_die(bmap->ctx, isl_error_invalid,
12165 "basic map cannot be curried", goto error);
12166 bmap = isl_basic_map_cow(bmap);
12167 if (!bmap)
12168 return NULL;
12169 bmap->dim = isl_space_curry(bmap->dim);
12170 if (!bmap->dim)
12171 goto error;
12172 bmap = isl_basic_map_mark_final(bmap);
12173 return bmap;
12174 error:
12175 isl_basic_map_free(bmap);
12176 return NULL;
12179 /* Given a map (A -> B) -> C, return the corresponding map
12180 * A -> (B -> C).
12182 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12184 return isl_map_change_space(map, &isl_map_can_curry,
12185 "map cannot be curried", &isl_space_curry);
12188 /* Can isl_map_range_curry be applied to "map"?
12189 * That is, does it have a nested relation in its range,
12190 * the domain of which is itself a nested relation?
12192 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12194 if (!map)
12195 return isl_bool_error;
12197 return isl_space_can_range_curry(map->dim);
12200 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12201 * A -> (B -> (C -> D)).
12203 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12205 return isl_map_change_space(map, &isl_map_can_range_curry,
12206 "map range cannot be curried",
12207 &isl_space_range_curry);
12210 /* Can we apply isl_basic_map_uncurry to "bmap"?
12211 * That is, does it have a nested relation in its domain?
12213 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12215 if (!bmap)
12216 return isl_bool_error;
12218 return isl_space_can_uncurry(bmap->dim);
12221 /* Can we apply isl_map_uncurry to "map"?
12222 * That is, does it have a nested relation in its domain?
12224 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12226 if (!map)
12227 return isl_bool_error;
12229 return isl_space_can_uncurry(map->dim);
12232 /* Given a basic map A -> (B -> C), return the corresponding basic map
12233 * (A -> B) -> C.
12235 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12238 if (!bmap)
12239 return NULL;
12241 if (!isl_basic_map_can_uncurry(bmap))
12242 isl_die(bmap->ctx, isl_error_invalid,
12243 "basic map cannot be uncurried",
12244 return isl_basic_map_free(bmap));
12245 bmap = isl_basic_map_cow(bmap);
12246 if (!bmap)
12247 return NULL;
12248 bmap->dim = isl_space_uncurry(bmap->dim);
12249 if (!bmap->dim)
12250 return isl_basic_map_free(bmap);
12251 bmap = isl_basic_map_mark_final(bmap);
12252 return bmap;
12255 /* Given a map A -> (B -> C), return the corresponding map
12256 * (A -> B) -> C.
12258 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12260 return isl_map_change_space(map, &isl_map_can_uncurry,
12261 "map cannot be uncurried", &isl_space_uncurry);
12264 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12265 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12267 return isl_map_equate(set, type1, pos1, type2, pos2);
12270 /* Construct a basic map where the given dimensions are equal to each other.
12272 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12273 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12275 isl_basic_map *bmap = NULL;
12276 int i;
12278 if (!space)
12279 return NULL;
12281 if (pos1 >= isl_space_dim(space, type1))
12282 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12283 "index out of bounds", goto error);
12284 if (pos2 >= isl_space_dim(space, type2))
12285 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12286 "index out of bounds", goto error);
12288 if (type1 == type2 && pos1 == pos2)
12289 return isl_basic_map_universe(space);
12291 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12292 i = isl_basic_map_alloc_equality(bmap);
12293 if (i < 0)
12294 goto error;
12295 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12296 pos1 += isl_basic_map_offset(bmap, type1);
12297 pos2 += isl_basic_map_offset(bmap, type2);
12298 isl_int_set_si(bmap->eq[i][pos1], -1);
12299 isl_int_set_si(bmap->eq[i][pos2], 1);
12300 bmap = isl_basic_map_finalize(bmap);
12301 isl_space_free(space);
12302 return bmap;
12303 error:
12304 isl_space_free(space);
12305 isl_basic_map_free(bmap);
12306 return NULL;
12309 /* Add a constraint imposing that the given two dimensions are equal.
12311 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12312 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12314 isl_basic_map *eq;
12316 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12318 bmap = isl_basic_map_intersect(bmap, eq);
12320 return bmap;
12323 /* Add a constraint imposing that the given two dimensions are equal.
12325 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12326 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12328 isl_basic_map *bmap;
12330 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12332 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12334 return map;
12337 /* Add a constraint imposing that the given two dimensions have opposite values.
12339 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12340 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12342 isl_basic_map *bmap = NULL;
12343 int i;
12345 if (!map)
12346 return NULL;
12348 if (pos1 >= isl_map_dim(map, type1))
12349 isl_die(map->ctx, isl_error_invalid,
12350 "index out of bounds", goto error);
12351 if (pos2 >= isl_map_dim(map, type2))
12352 isl_die(map->ctx, isl_error_invalid,
12353 "index out of bounds", goto error);
12355 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12356 i = isl_basic_map_alloc_equality(bmap);
12357 if (i < 0)
12358 goto error;
12359 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12360 pos1 += isl_basic_map_offset(bmap, type1);
12361 pos2 += isl_basic_map_offset(bmap, type2);
12362 isl_int_set_si(bmap->eq[i][pos1], 1);
12363 isl_int_set_si(bmap->eq[i][pos2], 1);
12364 bmap = isl_basic_map_finalize(bmap);
12366 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12368 return map;
12369 error:
12370 isl_basic_map_free(bmap);
12371 isl_map_free(map);
12372 return NULL;
12375 /* Construct a constraint imposing that the value of the first dimension is
12376 * greater than or equal to that of the second.
12378 static __isl_give isl_constraint *constraint_order_ge(
12379 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12380 enum isl_dim_type type2, int pos2)
12382 isl_constraint *c;
12384 if (!space)
12385 return NULL;
12387 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12389 if (pos1 >= isl_constraint_dim(c, type1))
12390 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12391 "index out of bounds", return isl_constraint_free(c));
12392 if (pos2 >= isl_constraint_dim(c, type2))
12393 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12394 "index out of bounds", return isl_constraint_free(c));
12396 if (type1 == type2 && pos1 == pos2)
12397 return c;
12399 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12400 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12402 return c;
12405 /* Add a constraint imposing that the value of the first dimension is
12406 * greater than or equal to that of the second.
12408 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12409 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12411 isl_constraint *c;
12412 isl_space *space;
12414 if (type1 == type2 && pos1 == pos2)
12415 return bmap;
12416 space = isl_basic_map_get_space(bmap);
12417 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12418 bmap = isl_basic_map_add_constraint(bmap, c);
12420 return bmap;
12423 /* Add a constraint imposing that the value of the first dimension is
12424 * greater than or equal to that of the second.
12426 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12427 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12429 isl_constraint *c;
12430 isl_space *space;
12432 if (type1 == type2 && pos1 == pos2)
12433 return map;
12434 space = isl_map_get_space(map);
12435 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12436 map = isl_map_add_constraint(map, c);
12438 return map;
12441 /* Add a constraint imposing that the value of the first dimension is
12442 * less than or equal to that of the second.
12444 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12445 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12447 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12450 /* Construct a basic map where the value of the first dimension is
12451 * greater than that of the second.
12453 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12454 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12456 isl_basic_map *bmap = NULL;
12457 int i;
12459 if (!space)
12460 return NULL;
12462 if (pos1 >= isl_space_dim(space, type1))
12463 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12464 "index out of bounds", goto error);
12465 if (pos2 >= isl_space_dim(space, type2))
12466 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12467 "index out of bounds", goto error);
12469 if (type1 == type2 && pos1 == pos2)
12470 return isl_basic_map_empty(space);
12472 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12473 i = isl_basic_map_alloc_inequality(bmap);
12474 if (i < 0)
12475 return isl_basic_map_free(bmap);
12476 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12477 pos1 += isl_basic_map_offset(bmap, type1);
12478 pos2 += isl_basic_map_offset(bmap, type2);
12479 isl_int_set_si(bmap->ineq[i][pos1], 1);
12480 isl_int_set_si(bmap->ineq[i][pos2], -1);
12481 isl_int_set_si(bmap->ineq[i][0], -1);
12482 bmap = isl_basic_map_finalize(bmap);
12484 return bmap;
12485 error:
12486 isl_space_free(space);
12487 isl_basic_map_free(bmap);
12488 return NULL;
12491 /* Add a constraint imposing that the value of the first dimension is
12492 * greater than that of the second.
12494 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12495 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12497 isl_basic_map *gt;
12499 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12501 bmap = isl_basic_map_intersect(bmap, gt);
12503 return bmap;
12506 /* Add a constraint imposing that the value of the first dimension is
12507 * greater than that of the second.
12509 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12510 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12512 isl_basic_map *bmap;
12514 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12516 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12518 return map;
12521 /* Add a constraint imposing that the value of the first dimension is
12522 * smaller than that of the second.
12524 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12525 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12527 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12530 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12531 int pos)
12533 isl_aff *div;
12534 isl_local_space *ls;
12536 if (!bmap)
12537 return NULL;
12539 if (!isl_basic_map_divs_known(bmap))
12540 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12541 "some divs are unknown", return NULL);
12543 ls = isl_basic_map_get_local_space(bmap);
12544 div = isl_local_space_get_div(ls, pos);
12545 isl_local_space_free(ls);
12547 return div;
12550 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12551 int pos)
12553 return isl_basic_map_get_div(bset, pos);
12556 /* Plug in "subs" for dimension "type", "pos" of "bset".
12558 * Let i be the dimension to replace and let "subs" be of the form
12560 * f/d
12562 * Any integer division with a non-zero coefficient for i,
12564 * floor((a i + g)/m)
12566 * is replaced by
12568 * floor((a f + d g)/(m d))
12570 * Constraints of the form
12572 * a i + g
12574 * are replaced by
12576 * a f + d g
12578 * We currently require that "subs" is an integral expression.
12579 * Handling rational expressions may require us to add stride constraints
12580 * as we do in isl_basic_set_preimage_multi_aff.
12582 __isl_give isl_basic_set *isl_basic_set_substitute(
12583 __isl_take isl_basic_set *bset,
12584 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12586 int i;
12587 isl_int v;
12588 isl_ctx *ctx;
12590 if (bset && isl_basic_set_plain_is_empty(bset))
12591 return bset;
12593 bset = isl_basic_set_cow(bset);
12594 if (!bset || !subs)
12595 goto error;
12597 ctx = isl_basic_set_get_ctx(bset);
12598 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12599 isl_die(ctx, isl_error_invalid,
12600 "spaces don't match", goto error);
12601 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12602 isl_die(ctx, isl_error_unsupported,
12603 "cannot handle divs yet", goto error);
12604 if (!isl_int_is_one(subs->v->el[0]))
12605 isl_die(ctx, isl_error_invalid,
12606 "can only substitute integer expressions", goto error);
12608 pos += isl_basic_set_offset(bset, type);
12610 isl_int_init(v);
12612 for (i = 0; i < bset->n_eq; ++i) {
12613 if (isl_int_is_zero(bset->eq[i][pos]))
12614 continue;
12615 isl_int_set(v, bset->eq[i][pos]);
12616 isl_int_set_si(bset->eq[i][pos], 0);
12617 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12618 v, subs->v->el + 1, subs->v->size - 1);
12621 for (i = 0; i < bset->n_ineq; ++i) {
12622 if (isl_int_is_zero(bset->ineq[i][pos]))
12623 continue;
12624 isl_int_set(v, bset->ineq[i][pos]);
12625 isl_int_set_si(bset->ineq[i][pos], 0);
12626 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12627 v, subs->v->el + 1, subs->v->size - 1);
12630 for (i = 0; i < bset->n_div; ++i) {
12631 if (isl_int_is_zero(bset->div[i][1 + pos]))
12632 continue;
12633 isl_int_set(v, bset->div[i][1 + pos]);
12634 isl_int_set_si(bset->div[i][1 + pos], 0);
12635 isl_seq_combine(bset->div[i] + 1,
12636 subs->v->el[0], bset->div[i] + 1,
12637 v, subs->v->el + 1, subs->v->size - 1);
12638 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12641 isl_int_clear(v);
12643 bset = isl_basic_set_simplify(bset);
12644 return isl_basic_set_finalize(bset);
12645 error:
12646 isl_basic_set_free(bset);
12647 return NULL;
12650 /* Plug in "subs" for dimension "type", "pos" of "set".
12652 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12653 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12655 int i;
12657 if (set && isl_set_plain_is_empty(set))
12658 return set;
12660 set = isl_set_cow(set);
12661 if (!set || !subs)
12662 goto error;
12664 for (i = set->n - 1; i >= 0; --i) {
12665 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12666 set = set_from_map(remove_if_empty(set_to_map(set), i));
12667 if (!set)
12668 return NULL;
12671 return set;
12672 error:
12673 isl_set_free(set);
12674 return NULL;
12677 /* Check if the range of "ma" is compatible with the domain or range
12678 * (depending on "type") of "bmap".
12680 static isl_stat check_basic_map_compatible_range_multi_aff(
12681 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12682 __isl_keep isl_multi_aff *ma)
12684 isl_bool m;
12685 isl_space *ma_space;
12687 ma_space = isl_multi_aff_get_space(ma);
12689 m = isl_space_has_equal_params(bmap->dim, ma_space);
12690 if (m < 0)
12691 goto error;
12692 if (!m)
12693 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12694 "parameters don't match", goto error);
12695 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12696 if (m < 0)
12697 goto error;
12698 if (!m)
12699 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12700 "spaces don't match", goto error);
12702 isl_space_free(ma_space);
12703 return isl_stat_ok;
12704 error:
12705 isl_space_free(ma_space);
12706 return isl_stat_error;
12709 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12710 * coefficients before the transformed range of dimensions,
12711 * the "n_after" coefficients after the transformed range of dimensions
12712 * and the coefficients of the other divs in "bmap".
12714 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12715 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12717 int i;
12718 int n_param;
12719 int n_set;
12720 isl_local_space *ls;
12722 if (n_div == 0)
12723 return 0;
12725 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12726 if (!ls)
12727 return -1;
12729 n_param = isl_local_space_dim(ls, isl_dim_param);
12730 n_set = isl_local_space_dim(ls, isl_dim_set);
12731 for (i = 0; i < n_div; ++i) {
12732 int o_bmap = 0, o_ls = 0;
12734 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12735 o_bmap += 1 + 1 + n_param;
12736 o_ls += 1 + 1 + n_param;
12737 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12738 o_bmap += n_before;
12739 isl_seq_cpy(bmap->div[i] + o_bmap,
12740 ls->div->row[i] + o_ls, n_set);
12741 o_bmap += n_set;
12742 o_ls += n_set;
12743 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12744 o_bmap += n_after;
12745 isl_seq_cpy(bmap->div[i] + o_bmap,
12746 ls->div->row[i] + o_ls, n_div);
12747 o_bmap += n_div;
12748 o_ls += n_div;
12749 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12750 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12751 goto error;
12754 isl_local_space_free(ls);
12755 return 0;
12756 error:
12757 isl_local_space_free(ls);
12758 return -1;
12761 /* How many stride constraints does "ma" enforce?
12762 * That is, how many of the affine expressions have a denominator
12763 * different from one?
12765 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12767 int i;
12768 int strides = 0;
12770 for (i = 0; i < ma->n; ++i)
12771 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12772 strides++;
12774 return strides;
12777 /* For each affine expression in ma of the form
12779 * x_i = (f_i y + h_i)/m_i
12781 * with m_i different from one, add a constraint to "bmap"
12782 * of the form
12784 * f_i y + h_i = m_i alpha_i
12786 * with alpha_i an additional existentially quantified variable.
12788 * The input variables of "ma" correspond to a subset of the variables
12789 * of "bmap". There are "n_before" variables in "bmap" before this
12790 * subset and "n_after" variables after this subset.
12791 * The integer divisions of the affine expressions in "ma" are assumed
12792 * to have been aligned. There are "n_div_ma" of them and
12793 * they appear first in "bmap", straight after the "n_after" variables.
12795 static __isl_give isl_basic_map *add_ma_strides(
12796 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12797 int n_before, int n_after, int n_div_ma)
12799 int i, k;
12800 int div;
12801 int total;
12802 int n_param;
12803 int n_in;
12805 total = isl_basic_map_total_dim(bmap);
12806 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12807 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12808 for (i = 0; i < ma->n; ++i) {
12809 int o_bmap = 0, o_ma = 1;
12811 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12812 continue;
12813 div = isl_basic_map_alloc_div(bmap);
12814 k = isl_basic_map_alloc_equality(bmap);
12815 if (div < 0 || k < 0)
12816 goto error;
12817 isl_int_set_si(bmap->div[div][0], 0);
12818 isl_seq_cpy(bmap->eq[k] + o_bmap,
12819 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12820 o_bmap += 1 + n_param;
12821 o_ma += 1 + n_param;
12822 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12823 o_bmap += n_before;
12824 isl_seq_cpy(bmap->eq[k] + o_bmap,
12825 ma->u.p[i]->v->el + o_ma, n_in);
12826 o_bmap += n_in;
12827 o_ma += n_in;
12828 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12829 o_bmap += n_after;
12830 isl_seq_cpy(bmap->eq[k] + o_bmap,
12831 ma->u.p[i]->v->el + o_ma, n_div_ma);
12832 o_bmap += n_div_ma;
12833 o_ma += n_div_ma;
12834 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12835 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12836 total++;
12839 return bmap;
12840 error:
12841 isl_basic_map_free(bmap);
12842 return NULL;
12845 /* Replace the domain or range space (depending on "type) of "space" by "set".
12847 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12848 enum isl_dim_type type, __isl_take isl_space *set)
12850 if (type == isl_dim_in) {
12851 space = isl_space_range(space);
12852 space = isl_space_map_from_domain_and_range(set, space);
12853 } else {
12854 space = isl_space_domain(space);
12855 space = isl_space_map_from_domain_and_range(space, set);
12858 return space;
12861 /* Compute the preimage of the domain or range (depending on "type")
12862 * of "bmap" under the function represented by "ma".
12863 * In other words, plug in "ma" in the domain or range of "bmap".
12864 * The result is a basic map that lives in the same space as "bmap"
12865 * except that the domain or range has been replaced by
12866 * the domain space of "ma".
12868 * If bmap is represented by
12870 * A(p) + S u + B x + T v + C(divs) >= 0,
12872 * where u and x are input and output dimensions if type == isl_dim_out
12873 * while x and v are input and output dimensions if type == isl_dim_in,
12874 * and ma is represented by
12876 * x = D(p) + F(y) + G(divs')
12878 * then the result is
12880 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12882 * The divs in the input set are similarly adjusted.
12883 * In particular
12885 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12887 * becomes
12889 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12890 * B_i G(divs') + c_i(divs))/n_i)
12892 * If bmap is not a rational map and if F(y) involves any denominators
12894 * x_i = (f_i y + h_i)/m_i
12896 * then additional constraints are added to ensure that we only
12897 * map back integer points. That is we enforce
12899 * f_i y + h_i = m_i alpha_i
12901 * with alpha_i an additional existentially quantified variable.
12903 * We first copy over the divs from "ma".
12904 * Then we add the modified constraints and divs from "bmap".
12905 * Finally, we add the stride constraints, if needed.
12907 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12908 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12909 __isl_take isl_multi_aff *ma)
12911 int i, k;
12912 isl_space *space;
12913 isl_basic_map *res = NULL;
12914 int n_before, n_after, n_div_bmap, n_div_ma;
12915 isl_int f, c1, c2, g;
12916 isl_bool rational;
12917 int strides;
12919 isl_int_init(f);
12920 isl_int_init(c1);
12921 isl_int_init(c2);
12922 isl_int_init(g);
12924 ma = isl_multi_aff_align_divs(ma);
12925 if (!bmap || !ma)
12926 goto error;
12927 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12928 goto error;
12930 if (type == isl_dim_in) {
12931 n_before = 0;
12932 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12933 } else {
12934 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12935 n_after = 0;
12937 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12938 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12940 space = isl_multi_aff_get_domain_space(ma);
12941 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12942 rational = isl_basic_map_is_rational(bmap);
12943 strides = rational ? 0 : multi_aff_strides(ma);
12944 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12945 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12946 if (rational)
12947 res = isl_basic_map_set_rational(res);
12949 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12950 if (isl_basic_map_alloc_div(res) < 0)
12951 goto error;
12953 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12954 goto error;
12956 for (i = 0; i < bmap->n_eq; ++i) {
12957 k = isl_basic_map_alloc_equality(res);
12958 if (k < 0)
12959 goto error;
12960 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12961 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12964 for (i = 0; i < bmap->n_ineq; ++i) {
12965 k = isl_basic_map_alloc_inequality(res);
12966 if (k < 0)
12967 goto error;
12968 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12969 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12972 for (i = 0; i < bmap->n_div; ++i) {
12973 if (isl_int_is_zero(bmap->div[i][0])) {
12974 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12975 continue;
12977 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12978 n_before, n_after, n_div_ma, n_div_bmap,
12979 f, c1, c2, g, 1);
12982 if (strides)
12983 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12985 isl_int_clear(f);
12986 isl_int_clear(c1);
12987 isl_int_clear(c2);
12988 isl_int_clear(g);
12989 isl_basic_map_free(bmap);
12990 isl_multi_aff_free(ma);
12991 res = isl_basic_map_simplify(res);
12992 return isl_basic_map_finalize(res);
12993 error:
12994 isl_int_clear(f);
12995 isl_int_clear(c1);
12996 isl_int_clear(c2);
12997 isl_int_clear(g);
12998 isl_basic_map_free(bmap);
12999 isl_multi_aff_free(ma);
13000 isl_basic_map_free(res);
13001 return NULL;
13004 /* Compute the preimage of "bset" under the function represented by "ma".
13005 * In other words, plug in "ma" in "bset". The result is a basic set
13006 * that lives in the domain space of "ma".
13008 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13009 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13011 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13014 /* Compute the preimage of the domain of "bmap" under the function
13015 * represented by "ma".
13016 * In other words, plug in "ma" in the domain of "bmap".
13017 * The result is a basic map that lives in the same space as "bmap"
13018 * except that the domain has been replaced by the domain space of "ma".
13020 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13021 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13023 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13026 /* Compute the preimage of the range of "bmap" under the function
13027 * represented by "ma".
13028 * In other words, plug in "ma" in the range of "bmap".
13029 * The result is a basic map that lives in the same space as "bmap"
13030 * except that the range has been replaced by the domain space of "ma".
13032 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13033 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13035 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13038 /* Check if the range of "ma" is compatible with the domain or range
13039 * (depending on "type") of "map".
13040 * Return isl_stat_error if anything is wrong.
13042 static isl_stat check_map_compatible_range_multi_aff(
13043 __isl_keep isl_map *map, enum isl_dim_type type,
13044 __isl_keep isl_multi_aff *ma)
13046 isl_bool m;
13047 isl_space *ma_space;
13049 ma_space = isl_multi_aff_get_space(ma);
13050 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13051 isl_space_free(ma_space);
13052 if (m < 0)
13053 return isl_stat_error;
13054 if (!m)
13055 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13056 "spaces don't match", return isl_stat_error);
13057 return isl_stat_ok;
13060 /* Compute the preimage of the domain or range (depending on "type")
13061 * of "map" under the function represented by "ma".
13062 * In other words, plug in "ma" in the domain or range of "map".
13063 * The result is a map that lives in the same space as "map"
13064 * except that the domain or range has been replaced by
13065 * the domain space of "ma".
13067 * The parameters are assumed to have been aligned.
13069 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13070 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13072 int i;
13073 isl_space *space;
13075 map = isl_map_cow(map);
13076 ma = isl_multi_aff_align_divs(ma);
13077 if (!map || !ma)
13078 goto error;
13079 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13080 goto error;
13082 for (i = 0; i < map->n; ++i) {
13083 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13084 isl_multi_aff_copy(ma));
13085 if (!map->p[i])
13086 goto error;
13089 space = isl_multi_aff_get_domain_space(ma);
13090 space = isl_space_set(isl_map_get_space(map), type, space);
13092 isl_space_free(map->dim);
13093 map->dim = space;
13094 if (!map->dim)
13095 goto error;
13097 isl_multi_aff_free(ma);
13098 if (map->n > 1)
13099 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13100 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13101 return map;
13102 error:
13103 isl_multi_aff_free(ma);
13104 isl_map_free(map);
13105 return NULL;
13108 /* Compute the preimage of the domain or range (depending on "type")
13109 * of "map" under the function represented by "ma".
13110 * In other words, plug in "ma" in the domain or range of "map".
13111 * The result is a map that lives in the same space as "map"
13112 * except that the domain or range has been replaced by
13113 * the domain space of "ma".
13115 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13116 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13118 isl_bool aligned;
13120 if (!map || !ma)
13121 goto error;
13123 aligned = isl_map_space_has_equal_params(map, ma->space);
13124 if (aligned < 0)
13125 goto error;
13126 if (aligned)
13127 return map_preimage_multi_aff(map, type, ma);
13129 if (isl_map_check_named_params(map) < 0)
13130 goto error;
13131 if (!isl_space_has_named_params(ma->space))
13132 isl_die(map->ctx, isl_error_invalid,
13133 "unaligned unnamed parameters", goto error);
13134 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13135 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13137 return map_preimage_multi_aff(map, type, ma);
13138 error:
13139 isl_multi_aff_free(ma);
13140 return isl_map_free(map);
13143 /* Compute the preimage of "set" under the function represented by "ma".
13144 * In other words, plug in "ma" in "set". The result is a set
13145 * that lives in the domain space of "ma".
13147 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13148 __isl_take isl_multi_aff *ma)
13150 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13153 /* Compute the preimage of the domain of "map" under the function
13154 * represented by "ma".
13155 * In other words, plug in "ma" in the domain of "map".
13156 * The result is a map that lives in the same space as "map"
13157 * except that the domain has been replaced by the domain space of "ma".
13159 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13160 __isl_take isl_multi_aff *ma)
13162 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13165 /* Compute the preimage of the range of "map" under the function
13166 * represented by "ma".
13167 * In other words, plug in "ma" in the range of "map".
13168 * The result is a map that lives in the same space as "map"
13169 * except that the range has been replaced by the domain space of "ma".
13171 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13172 __isl_take isl_multi_aff *ma)
13174 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13177 /* Compute the preimage of "map" under the function represented by "pma".
13178 * In other words, plug in "pma" in the domain or range of "map".
13179 * The result is a map that lives in the same space as "map",
13180 * except that the space of type "type" has been replaced by
13181 * the domain space of "pma".
13183 * The parameters of "map" and "pma" are assumed to have been aligned.
13185 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13186 __isl_take isl_map *map, enum isl_dim_type type,
13187 __isl_take isl_pw_multi_aff *pma)
13189 int i;
13190 isl_map *res;
13192 if (!pma)
13193 goto error;
13195 if (pma->n == 0) {
13196 isl_pw_multi_aff_free(pma);
13197 res = isl_map_empty(isl_map_get_space(map));
13198 isl_map_free(map);
13199 return res;
13202 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13203 isl_multi_aff_copy(pma->p[0].maff));
13204 if (type == isl_dim_in)
13205 res = isl_map_intersect_domain(res,
13206 isl_map_copy(pma->p[0].set));
13207 else
13208 res = isl_map_intersect_range(res,
13209 isl_map_copy(pma->p[0].set));
13211 for (i = 1; i < pma->n; ++i) {
13212 isl_map *res_i;
13214 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13215 isl_multi_aff_copy(pma->p[i].maff));
13216 if (type == isl_dim_in)
13217 res_i = isl_map_intersect_domain(res_i,
13218 isl_map_copy(pma->p[i].set));
13219 else
13220 res_i = isl_map_intersect_range(res_i,
13221 isl_map_copy(pma->p[i].set));
13222 res = isl_map_union(res, res_i);
13225 isl_pw_multi_aff_free(pma);
13226 isl_map_free(map);
13227 return res;
13228 error:
13229 isl_pw_multi_aff_free(pma);
13230 isl_map_free(map);
13231 return NULL;
13234 /* Compute the preimage of "map" under the function represented by "pma".
13235 * In other words, plug in "pma" in the domain or range of "map".
13236 * The result is a map that lives in the same space as "map",
13237 * except that the space of type "type" has been replaced by
13238 * the domain space of "pma".
13240 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13241 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13243 isl_bool aligned;
13245 if (!map || !pma)
13246 goto error;
13248 aligned = isl_map_space_has_equal_params(map, pma->dim);
13249 if (aligned < 0)
13250 goto error;
13251 if (aligned)
13252 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13254 if (isl_map_check_named_params(map) < 0)
13255 goto error;
13256 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13257 goto error;
13258 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13259 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13261 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13262 error:
13263 isl_pw_multi_aff_free(pma);
13264 return isl_map_free(map);
13267 /* Compute the preimage of "set" under the function represented by "pma".
13268 * In other words, plug in "pma" in "set". The result is a set
13269 * that lives in the domain space of "pma".
13271 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13272 __isl_take isl_pw_multi_aff *pma)
13274 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13277 /* Compute the preimage of the domain of "map" under the function
13278 * represented by "pma".
13279 * In other words, plug in "pma" in the domain of "map".
13280 * The result is a map that lives in the same space as "map",
13281 * except that domain space has been replaced by the domain space of "pma".
13283 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13284 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13286 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13289 /* Compute the preimage of the range of "map" under the function
13290 * represented by "pma".
13291 * In other words, plug in "pma" in the range of "map".
13292 * The result is a map that lives in the same space as "map",
13293 * except that range space has been replaced by the domain space of "pma".
13295 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13296 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13298 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13301 /* Compute the preimage of "map" under the function represented by "mpa".
13302 * In other words, plug in "mpa" in the domain or range of "map".
13303 * The result is a map that lives in the same space as "map",
13304 * except that the space of type "type" has been replaced by
13305 * the domain space of "mpa".
13307 * If the map does not involve any constraints that refer to the
13308 * dimensions of the substituted space, then the only possible
13309 * effect of "mpa" on the map is to map the space to a different space.
13310 * We create a separate isl_multi_aff to effectuate this change
13311 * in order to avoid spurious splitting of the map along the pieces
13312 * of "mpa".
13313 * If "mpa" has a non-trivial explicit domain, however,
13314 * then the full substitution should be performed.
13316 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13317 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13319 int n;
13320 isl_bool full;
13321 isl_pw_multi_aff *pma;
13323 if (!map || !mpa)
13324 goto error;
13326 n = isl_map_dim(map, type);
13327 full = isl_map_involves_dims(map, type, 0, n);
13328 if (full >= 0 && !full)
13329 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13330 if (full < 0)
13331 goto error;
13332 if (!full) {
13333 isl_space *space;
13334 isl_multi_aff *ma;
13336 space = isl_multi_pw_aff_get_space(mpa);
13337 isl_multi_pw_aff_free(mpa);
13338 ma = isl_multi_aff_zero(space);
13339 return isl_map_preimage_multi_aff(map, type, ma);
13342 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13343 return isl_map_preimage_pw_multi_aff(map, type, pma);
13344 error:
13345 isl_map_free(map);
13346 isl_multi_pw_aff_free(mpa);
13347 return NULL;
13350 /* Compute the preimage of "map" under the function represented by "mpa".
13351 * In other words, plug in "mpa" in the domain "map".
13352 * The result is a map that lives in the same space as "map",
13353 * except that domain space has been replaced by the domain space of "mpa".
13355 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13356 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13358 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13361 /* Compute the preimage of "set" by the function represented by "mpa".
13362 * In other words, plug in "mpa" in "set".
13364 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13365 __isl_take isl_multi_pw_aff *mpa)
13367 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13370 /* Return a copy of the equality constraints of "bset" as a matrix.
13372 __isl_give isl_mat *isl_basic_set_extract_equalities(
13373 __isl_keep isl_basic_set *bset)
13375 isl_ctx *ctx;
13376 unsigned total;
13378 if (!bset)
13379 return NULL;
13381 ctx = isl_basic_set_get_ctx(bset);
13382 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13383 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13386 /* Are the "n" "coefficients" starting at "first" of the integer division
13387 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13388 * to each other?
13389 * The "coefficient" at position 0 is the denominator.
13390 * The "coefficient" at position 1 is the constant term.
13392 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13393 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13394 unsigned first, unsigned n)
13396 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13397 return isl_bool_error;
13398 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13399 return isl_bool_error;
13400 return isl_seq_eq(bmap1->div[pos1] + first,
13401 bmap2->div[pos2] + first, n);
13404 /* Are the integer division expressions at position "pos1" in "bmap1" and
13405 * "pos2" in "bmap2" equal to each other, except that the constant terms
13406 * are different?
13408 isl_bool isl_basic_map_equal_div_expr_except_constant(
13409 __isl_keep isl_basic_map *bmap1, int pos1,
13410 __isl_keep isl_basic_map *bmap2, int pos2)
13412 isl_bool equal;
13413 unsigned total;
13415 if (!bmap1 || !bmap2)
13416 return isl_bool_error;
13417 total = isl_basic_map_total_dim(bmap1);
13418 if (total != isl_basic_map_total_dim(bmap2))
13419 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13420 "incomparable div expressions", return isl_bool_error);
13421 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13422 0, 1);
13423 if (equal < 0 || !equal)
13424 return equal;
13425 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13426 1, 1);
13427 if (equal < 0 || equal)
13428 return isl_bool_not(equal);
13429 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13430 2, total);
13433 /* Replace the numerator of the constant term of the integer division
13434 * expression at position "div" in "bmap" by "value".
13435 * The caller guarantees that this does not change the meaning
13436 * of the input.
13438 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13439 __isl_take isl_basic_map *bmap, int div, int value)
13441 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13442 return isl_basic_map_free(bmap);
13444 isl_int_set_si(bmap->div[div][1], value);
13446 return bmap;
13449 /* Is the point "inner" internal to inequality constraint "ineq"
13450 * of "bset"?
13451 * The point is considered to be internal to the inequality constraint,
13452 * if it strictly lies on the positive side of the inequality constraint,
13453 * or if it lies on the constraint and the constraint is lexico-positive.
13455 static isl_bool is_internal(__isl_keep isl_vec *inner,
13456 __isl_keep isl_basic_set *bset, int ineq)
13458 isl_ctx *ctx;
13459 int pos;
13460 unsigned total;
13462 if (!inner || !bset)
13463 return isl_bool_error;
13465 ctx = isl_basic_set_get_ctx(bset);
13466 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13467 &ctx->normalize_gcd);
13468 if (!isl_int_is_zero(ctx->normalize_gcd))
13469 return isl_int_is_nonneg(ctx->normalize_gcd);
13471 total = isl_basic_set_dim(bset, isl_dim_all);
13472 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13473 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13476 /* Tighten the inequality constraints of "bset" that are outward with respect
13477 * to the point "vec".
13478 * That is, tighten the constraints that are not satisfied by "vec".
13480 * "vec" is a point internal to some superset S of "bset" that is used
13481 * to make the subsets of S disjoint, by tightening one half of the constraints
13482 * that separate two subsets. In particular, the constraints of S
13483 * are all satisfied by "vec" and should not be tightened.
13484 * Of the internal constraints, those that have "vec" on the outside
13485 * are tightened. The shared facet is included in the adjacent subset
13486 * with the opposite constraint.
13487 * For constraints that saturate "vec", this criterion cannot be used
13488 * to determine which of the two sides should be tightened.
13489 * Instead, the sign of the first non-zero coefficient is used
13490 * to make this choice. Note that this second criterion is never used
13491 * on the constraints of S since "vec" is interior to "S".
13493 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13494 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13496 int j;
13498 bset = isl_basic_set_cow(bset);
13499 if (!bset)
13500 return NULL;
13501 for (j = 0; j < bset->n_ineq; ++j) {
13502 isl_bool internal;
13504 internal = is_internal(vec, bset, j);
13505 if (internal < 0)
13506 return isl_basic_set_free(bset);
13507 if (internal)
13508 continue;
13509 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13512 return bset;
13515 /* Replace the variables x of type "type" starting at "first" in "bmap"
13516 * by x' with x = M x' with M the matrix trans.
13517 * That is, replace the corresponding coefficients c by c M.
13519 * The transformation matrix should be a square matrix.
13521 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13522 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13523 __isl_take isl_mat *trans)
13525 unsigned pos;
13527 bmap = isl_basic_map_cow(bmap);
13528 if (!bmap || !trans)
13529 goto error;
13531 if (trans->n_row != trans->n_col)
13532 isl_die(trans->ctx, isl_error_invalid,
13533 "expecting square transformation matrix", goto error);
13534 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
13535 isl_die(trans->ctx, isl_error_invalid,
13536 "oversized transformation matrix", goto error);
13538 pos = isl_basic_map_offset(bmap, type) + first;
13540 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13541 isl_mat_copy(trans)) < 0)
13542 goto error;
13543 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13544 isl_mat_copy(trans)) < 0)
13545 goto error;
13546 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13547 isl_mat_copy(trans)) < 0)
13548 goto error;
13550 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13551 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13553 isl_mat_free(trans);
13554 return bmap;
13555 error:
13556 isl_mat_free(trans);
13557 isl_basic_map_free(bmap);
13558 return NULL;
13561 /* Replace the variables x of type "type" starting at "first" in "bset"
13562 * by x' with x = M x' with M the matrix trans.
13563 * That is, replace the corresponding coefficients c by c M.
13565 * The transformation matrix should be a square matrix.
13567 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13568 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13569 __isl_take isl_mat *trans)
13571 return isl_basic_map_transform_dims(bset, type, first, trans);