isl_inequality_negate: return modified result
[isl.git] / isl_map.c
blob40f8fcc1cb35a7da10019e286fd6ad880012e888
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/id.h>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl/map.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
36 #include <isl_sort.h>
37 #include "isl_tab.h"
38 #include <isl/vec.h>
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
48 #include <bset_to_bmap.c>
49 #include <bset_from_bmap.c>
50 #include <set_to_map.c>
51 #include <set_from_map.c>
53 /* Treat "bset" as a basic map.
54 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
55 * this function performs a redundant cast.
57 static __isl_keep const isl_basic_map *const_bset_to_bmap(
58 __isl_keep const isl_basic_set *bset)
60 return (const isl_basic_map *) bset;
63 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
65 switch (type) {
66 case isl_dim_param: return dim->nparam;
67 case isl_dim_in: return dim->n_in;
68 case isl_dim_out: return dim->n_out;
69 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
70 default: return 0;
74 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
76 switch (type) {
77 case isl_dim_param: return 1;
78 case isl_dim_in: return 1 + dim->nparam;
79 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
80 default: return 0;
84 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
85 enum isl_dim_type type)
87 if (!bmap)
88 return 0;
89 switch (type) {
90 case isl_dim_cst: return 1;
91 case isl_dim_param:
92 case isl_dim_in:
93 case isl_dim_out: return isl_space_dim(bmap->dim, type);
94 case isl_dim_div: return bmap->n_div;
95 case isl_dim_all: return isl_basic_map_total_dim(bmap);
96 default: return 0;
100 /* Return the space of "map".
102 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
104 return map ? map->dim : NULL;
107 /* Return the space of "set".
109 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
111 return isl_map_peek_space(set_to_map(set));
114 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
116 return map ? n(map->dim, type) : 0;
119 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
121 return set ? n(set->dim, type) : 0;
124 /* Return the position of the variables of the given type
125 * within the sequence of variables of "bmap".
127 int isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
128 enum isl_dim_type type)
130 isl_space *space;
132 space = isl_basic_map_peek_space(bmap);
133 if (!space)
134 return -1;
136 switch (type) {
137 case isl_dim_param:
138 case isl_dim_in:
139 case isl_dim_out: return isl_space_offset(space, type);
140 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
141 case isl_dim_cst:
142 default:
143 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
144 "invalid dimension type", return -1);
148 /* Return the position of the coefficients of the variables of the given type
149 * within the sequence of coefficients of "bmap".
151 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
152 enum isl_dim_type type)
154 switch (type) {
155 case isl_dim_cst: return 0;
156 case isl_dim_param:
157 case isl_dim_in:
158 case isl_dim_out:
159 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
160 default: return 0;
164 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
165 enum isl_dim_type type)
167 return isl_basic_map_offset(bset, type);
170 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
172 return pos(map->dim, type);
175 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
176 enum isl_dim_type type)
178 return isl_basic_map_dim(bset, type);
181 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
183 return isl_basic_set_dim(bset, isl_dim_set);
186 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
188 return isl_basic_set_dim(bset, isl_dim_param);
191 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
193 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
196 unsigned isl_set_n_dim(__isl_keep isl_set *set)
198 return isl_set_dim(set, isl_dim_set);
201 unsigned isl_set_n_param(__isl_keep isl_set *set)
203 return isl_set_dim(set, isl_dim_param);
206 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
208 return bmap ? bmap->dim->n_in : 0;
211 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
213 return bmap ? bmap->dim->n_out : 0;
216 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
218 return bmap ? bmap->dim->nparam : 0;
221 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
223 return bmap ? bmap->n_div : 0;
226 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
228 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
231 unsigned isl_map_n_in(__isl_keep const isl_map *map)
233 return map ? map->dim->n_in : 0;
236 unsigned isl_map_n_out(__isl_keep const isl_map *map)
238 return map ? map->dim->n_out : 0;
241 unsigned isl_map_n_param(__isl_keep const isl_map *map)
243 return map ? map->dim->nparam : 0;
246 /* Return the number of equality constraints in the description of "bmap".
247 * Return -1 on error.
249 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
251 if (!bmap)
252 return -1;
253 return bmap->n_eq;
256 /* Return the number of equality constraints in the description of "bset".
257 * Return -1 on error.
259 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
261 return isl_basic_map_n_equality(bset_to_bmap(bset));
264 /* Return the number of inequality constraints in the description of "bmap".
265 * Return -1 on error.
267 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
269 if (!bmap)
270 return -1;
271 return bmap->n_ineq;
274 /* Return the number of inequality constraints in the description of "bset".
275 * Return -1 on error.
277 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
279 return isl_basic_map_n_inequality(bset_to_bmap(bset));
282 /* Do "bmap1" and "bmap2" have the same parameters?
284 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
285 __isl_keep isl_basic_map *bmap2)
287 isl_space *space1, *space2;
289 space1 = isl_basic_map_peek_space(bmap1);
290 space2 = isl_basic_map_peek_space(bmap2);
291 return isl_space_has_equal_params(space1, space2);
294 /* Do "map1" and "map2" have the same parameters?
296 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
297 __isl_keep isl_map *map2)
299 isl_space *space1, *space2;
301 space1 = isl_map_peek_space(map1);
302 space2 = isl_map_peek_space(map2);
303 return isl_space_has_equal_params(space1, space2);
306 /* Do "map" and "set" have the same parameters?
308 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
309 __isl_keep isl_set *set)
311 return isl_map_has_equal_params(map, set_to_map(set));
314 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
315 __isl_keep isl_set *set)
317 isl_bool m;
318 if (!map || !set)
319 return isl_bool_error;
320 m = isl_map_has_equal_params(map, set_to_map(set));
321 if (m < 0 || !m)
322 return m;
323 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
324 set->dim, isl_dim_set);
327 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
328 __isl_keep isl_basic_set *bset)
330 isl_bool m;
331 if (!bmap || !bset)
332 return isl_bool_error;
333 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
334 if (m < 0 || !m)
335 return m;
336 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
337 bset->dim, isl_dim_set);
340 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
341 __isl_keep isl_set *set)
343 isl_bool m;
344 if (!map || !set)
345 return isl_bool_error;
346 m = isl_map_has_equal_params(map, set_to_map(set));
347 if (m < 0 || !m)
348 return m;
349 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
350 set->dim, isl_dim_set);
353 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
354 __isl_keep isl_basic_set *bset)
356 isl_bool m;
357 if (!bmap || !bset)
358 return isl_bool_error;
359 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
360 if (m < 0 || !m)
361 return m;
362 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
363 bset->dim, isl_dim_set);
366 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
368 return bmap ? bmap->ctx : NULL;
371 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
373 return bset ? bset->ctx : NULL;
376 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
378 return map ? map->ctx : NULL;
381 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
383 return set ? set->ctx : NULL;
386 /* Return the space of "bmap".
388 __isl_keep isl_space *isl_basic_map_peek_space(
389 __isl_keep const isl_basic_map *bmap)
391 return bmap ? bmap->dim : NULL;
394 /* Return the space of "bset".
396 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
398 return isl_basic_map_peek_space(bset_to_bmap(bset));
401 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
403 return isl_space_copy(isl_basic_map_peek_space(bmap));
406 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
408 return isl_basic_map_get_space(bset_to_bmap(bset));
411 /* Extract the divs in "bmap" as a matrix.
413 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
415 int i;
416 isl_ctx *ctx;
417 isl_mat *div;
418 unsigned total;
419 unsigned cols;
421 if (!bmap)
422 return NULL;
424 ctx = isl_basic_map_get_ctx(bmap);
425 total = isl_space_dim(bmap->dim, isl_dim_all);
426 cols = 1 + 1 + total + bmap->n_div;
427 div = isl_mat_alloc(ctx, bmap->n_div, cols);
428 if (!div)
429 return NULL;
431 for (i = 0; i < bmap->n_div; ++i)
432 isl_seq_cpy(div->row[i], bmap->div[i], cols);
434 return div;
437 /* Extract the divs in "bset" as a matrix.
439 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
441 return isl_basic_map_get_divs(bset);
444 __isl_give isl_local_space *isl_basic_map_get_local_space(
445 __isl_keep isl_basic_map *bmap)
447 isl_mat *div;
449 if (!bmap)
450 return NULL;
452 div = isl_basic_map_get_divs(bmap);
453 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
456 __isl_give isl_local_space *isl_basic_set_get_local_space(
457 __isl_keep isl_basic_set *bset)
459 return isl_basic_map_get_local_space(bset);
462 /* For each known div d = floor(f/m), add the constraints
464 * f - m d >= 0
465 * -(f-(m-1)) + m d >= 0
467 * Do not finalize the result.
469 static __isl_give isl_basic_map *add_known_div_constraints(
470 __isl_take isl_basic_map *bmap)
472 int i;
473 unsigned n_div;
475 if (!bmap)
476 return NULL;
477 n_div = isl_basic_map_dim(bmap, isl_dim_div);
478 if (n_div == 0)
479 return bmap;
480 bmap = isl_basic_map_cow(bmap);
481 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
482 if (!bmap)
483 return NULL;
484 for (i = 0; i < n_div; ++i) {
485 if (isl_int_is_zero(bmap->div[i][0]))
486 continue;
487 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
488 return isl_basic_map_free(bmap);
491 return bmap;
494 __isl_give isl_basic_map *isl_basic_map_from_local_space(
495 __isl_take isl_local_space *ls)
497 int i;
498 int n_div;
499 isl_basic_map *bmap;
501 if (!ls)
502 return NULL;
504 n_div = isl_local_space_dim(ls, isl_dim_div);
505 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
506 n_div, 0, 2 * n_div);
508 for (i = 0; i < n_div; ++i)
509 if (isl_basic_map_alloc_div(bmap) < 0)
510 goto error;
512 for (i = 0; i < n_div; ++i)
513 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
514 bmap = add_known_div_constraints(bmap);
516 isl_local_space_free(ls);
517 return bmap;
518 error:
519 isl_local_space_free(ls);
520 isl_basic_map_free(bmap);
521 return NULL;
524 __isl_give isl_basic_set *isl_basic_set_from_local_space(
525 __isl_take isl_local_space *ls)
527 return isl_basic_map_from_local_space(ls);
530 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
532 return isl_space_copy(isl_map_peek_space(map));
535 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
537 if (!set)
538 return NULL;
539 return isl_space_copy(set->dim);
542 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
543 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
545 bmap = isl_basic_map_cow(bmap);
546 if (!bmap)
547 return NULL;
548 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
549 if (!bmap->dim)
550 goto error;
551 bmap = isl_basic_map_finalize(bmap);
552 return bmap;
553 error:
554 isl_basic_map_free(bmap);
555 return NULL;
558 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
559 __isl_take isl_basic_set *bset, const char *s)
561 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
564 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
565 enum isl_dim_type type)
567 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
570 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
571 enum isl_dim_type type, const char *s)
573 int i;
575 map = isl_map_cow(map);
576 if (!map)
577 return NULL;
579 map->dim = isl_space_set_tuple_name(map->dim, type, s);
580 if (!map->dim)
581 goto error;
583 for (i = 0; i < map->n; ++i) {
584 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
585 if (!map->p[i])
586 goto error;
589 return map;
590 error:
591 isl_map_free(map);
592 return NULL;
595 /* Replace the identifier of the tuple of type "type" by "id".
597 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
598 __isl_take isl_basic_map *bmap,
599 enum isl_dim_type type, __isl_take isl_id *id)
601 bmap = isl_basic_map_cow(bmap);
602 if (!bmap)
603 goto error;
604 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
605 if (!bmap->dim)
606 return isl_basic_map_free(bmap);
607 bmap = isl_basic_map_finalize(bmap);
608 return bmap;
609 error:
610 isl_id_free(id);
611 return NULL;
614 /* Replace the identifier of the tuple by "id".
616 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
617 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
619 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
622 /* Does the input or output tuple have a name?
624 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
626 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
629 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
630 enum isl_dim_type type)
632 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
635 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
636 const char *s)
638 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
639 isl_dim_set, s));
642 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
643 enum isl_dim_type type, __isl_take isl_id *id)
645 map = isl_map_cow(map);
646 if (!map)
647 goto error;
649 map->dim = isl_space_set_tuple_id(map->dim, type, id);
651 return isl_map_reset_space(map, isl_space_copy(map->dim));
652 error:
653 isl_id_free(id);
654 return NULL;
657 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
658 __isl_take isl_id *id)
660 return isl_map_set_tuple_id(set, isl_dim_set, id);
663 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
664 enum isl_dim_type type)
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
670 map->dim = isl_space_reset_tuple_id(map->dim, type);
672 return isl_map_reset_space(map, isl_space_copy(map->dim));
675 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
677 return isl_map_reset_tuple_id(set, isl_dim_set);
680 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
682 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
685 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
686 enum isl_dim_type type)
688 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
691 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
693 return isl_map_has_tuple_id(set, isl_dim_set);
696 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
698 return isl_map_get_tuple_id(set, isl_dim_set);
701 /* Does the set tuple have a name?
703 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
705 if (!set)
706 return isl_bool_error;
707 return isl_space_has_tuple_name(set->dim, isl_dim_set);
711 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
713 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
716 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
718 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
721 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
722 enum isl_dim_type type, unsigned pos)
724 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
727 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
728 enum isl_dim_type type, unsigned pos)
730 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
733 /* Does the given dimension have a name?
735 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
736 enum isl_dim_type type, unsigned pos)
738 if (!map)
739 return isl_bool_error;
740 return isl_space_has_dim_name(map->dim, type, pos);
743 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
744 enum isl_dim_type type, unsigned pos)
746 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
749 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
750 enum isl_dim_type type, unsigned pos)
752 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
755 /* Does the given dimension have a name?
757 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
758 enum isl_dim_type type, unsigned pos)
760 if (!set)
761 return isl_bool_error;
762 return isl_space_has_dim_name(set->dim, type, pos);
765 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
766 __isl_take isl_basic_map *bmap,
767 enum isl_dim_type type, unsigned pos, const char *s)
769 bmap = isl_basic_map_cow(bmap);
770 if (!bmap)
771 return NULL;
772 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
773 if (!bmap->dim)
774 goto error;
775 return isl_basic_map_finalize(bmap);
776 error:
777 isl_basic_map_free(bmap);
778 return NULL;
781 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
782 enum isl_dim_type type, unsigned pos, const char *s)
784 int i;
786 map = isl_map_cow(map);
787 if (!map)
788 return NULL;
790 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
791 if (!map->dim)
792 goto error;
794 for (i = 0; i < map->n; ++i) {
795 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
796 if (!map->p[i])
797 goto error;
800 return map;
801 error:
802 isl_map_free(map);
803 return NULL;
806 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
807 __isl_take isl_basic_set *bset,
808 enum isl_dim_type type, unsigned pos, const char *s)
810 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
811 type, pos, s));
814 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
815 enum isl_dim_type type, unsigned pos, const char *s)
817 return set_from_map(isl_map_set_dim_name(set_to_map(set),
818 type, pos, s));
821 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
822 enum isl_dim_type type, unsigned pos)
824 if (!bmap)
825 return isl_bool_error;
826 return isl_space_has_dim_id(bmap->dim, type, pos);
829 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
830 enum isl_dim_type type, unsigned pos)
832 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
835 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
836 enum isl_dim_type type, unsigned pos)
838 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
841 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
842 enum isl_dim_type type, unsigned pos)
844 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
847 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
848 enum isl_dim_type type, unsigned pos)
850 return isl_map_has_dim_id(set, type, pos);
853 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
854 enum isl_dim_type type, unsigned pos)
856 return isl_map_get_dim_id(set, type, pos);
859 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
860 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
862 map = isl_map_cow(map);
863 if (!map)
864 goto error;
866 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
868 return isl_map_reset_space(map, isl_space_copy(map->dim));
869 error:
870 isl_id_free(id);
871 return NULL;
874 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
875 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
877 return isl_map_set_dim_id(set, type, pos, id);
880 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
881 __isl_keep isl_id *id)
883 if (!map)
884 return -1;
885 return isl_space_find_dim_by_id(map->dim, type, id);
888 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
889 __isl_keep isl_id *id)
891 return isl_map_find_dim_by_id(set, type, id);
894 /* Return the position of the dimension of the given type and name
895 * in "bmap".
896 * Return -1 if no such dimension can be found.
898 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
899 enum isl_dim_type type, const char *name)
901 if (!bmap)
902 return -1;
903 return isl_space_find_dim_by_name(bmap->dim, type, name);
906 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
907 const char *name)
909 if (!map)
910 return -1;
911 return isl_space_find_dim_by_name(map->dim, type, name);
914 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
915 const char *name)
917 return isl_map_find_dim_by_name(set, type, name);
920 /* Check whether equality i of bset is a pure stride constraint
921 * on a single dimension, i.e., of the form
923 * v = k e
925 * with k a constant and e an existentially quantified variable.
927 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
929 unsigned nparam;
930 unsigned d;
931 unsigned n_div;
932 int pos1;
933 int pos2;
935 if (!bset)
936 return isl_bool_error;
938 if (!isl_int_is_zero(bset->eq[i][0]))
939 return isl_bool_false;
941 nparam = isl_basic_set_dim(bset, isl_dim_param);
942 d = isl_basic_set_dim(bset, isl_dim_set);
943 n_div = isl_basic_set_dim(bset, isl_dim_div);
945 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
946 return isl_bool_false;
947 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
948 if (pos1 == -1)
949 return isl_bool_false;
950 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
951 d - pos1 - 1) != -1)
952 return isl_bool_false;
954 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
955 if (pos2 == -1)
956 return isl_bool_false;
957 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
958 n_div - pos2 - 1) != -1)
959 return isl_bool_false;
960 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
961 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
962 return isl_bool_false;
964 return isl_bool_true;
967 /* Reset the user pointer on all identifiers of parameters and tuples
968 * of the space of "map".
970 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
972 isl_space *space;
974 space = isl_map_get_space(map);
975 space = isl_space_reset_user(space);
976 map = isl_map_reset_space(map, space);
978 return map;
981 /* Reset the user pointer on all identifiers of parameters and tuples
982 * of the space of "set".
984 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
986 return isl_map_reset_user(set);
989 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
991 if (!bmap)
992 return isl_bool_error;
993 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
996 /* Has "map" been marked as a rational map?
997 * In particular, have all basic maps in "map" been marked this way?
998 * An empty map is not considered to be rational.
999 * Maps where only some of the basic maps are marked rational
1000 * are not allowed.
1002 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1004 int i;
1005 isl_bool rational;
1007 if (!map)
1008 return isl_bool_error;
1009 if (map->n == 0)
1010 return isl_bool_false;
1011 rational = isl_basic_map_is_rational(map->p[0]);
1012 if (rational < 0)
1013 return rational;
1014 for (i = 1; i < map->n; ++i) {
1015 isl_bool rational_i;
1017 rational_i = isl_basic_map_is_rational(map->p[i]);
1018 if (rational_i < 0)
1019 return rational_i;
1020 if (rational != rational_i)
1021 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1022 "mixed rational and integer basic maps "
1023 "not supported", return isl_bool_error);
1026 return rational;
1029 /* Has "set" been marked as a rational set?
1030 * In particular, have all basic set in "set" been marked this way?
1031 * An empty set is not considered to be rational.
1032 * Sets where only some of the basic sets are marked rational
1033 * are not allowed.
1035 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1037 return isl_map_is_rational(set);
1040 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1042 return isl_basic_map_is_rational(bset);
1045 /* Does "bmap" contain any rational points?
1047 * If "bmap" has an equality for each dimension, equating the dimension
1048 * to an integer constant, then it has no rational points, even if it
1049 * is marked as rational.
1051 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1053 isl_bool has_rational = isl_bool_true;
1054 unsigned total;
1056 if (!bmap)
1057 return isl_bool_error;
1058 if (isl_basic_map_plain_is_empty(bmap))
1059 return isl_bool_false;
1060 if (!isl_basic_map_is_rational(bmap))
1061 return isl_bool_false;
1062 bmap = isl_basic_map_copy(bmap);
1063 bmap = isl_basic_map_implicit_equalities(bmap);
1064 if (!bmap)
1065 return isl_bool_error;
1066 total = isl_basic_map_total_dim(bmap);
1067 if (bmap->n_eq == total) {
1068 int i, j;
1069 for (i = 0; i < bmap->n_eq; ++i) {
1070 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1071 if (j < 0)
1072 break;
1073 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1074 !isl_int_is_negone(bmap->eq[i][1 + j]))
1075 break;
1076 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1077 total - j - 1);
1078 if (j >= 0)
1079 break;
1081 if (i == bmap->n_eq)
1082 has_rational = isl_bool_false;
1084 isl_basic_map_free(bmap);
1086 return has_rational;
1089 /* Does "map" contain any rational points?
1091 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1093 int i;
1094 isl_bool has_rational;
1096 if (!map)
1097 return isl_bool_error;
1098 for (i = 0; i < map->n; ++i) {
1099 has_rational = isl_basic_map_has_rational(map->p[i]);
1100 if (has_rational < 0 || has_rational)
1101 return has_rational;
1103 return isl_bool_false;
1106 /* Does "set" contain any rational points?
1108 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1110 return isl_map_has_rational(set);
1113 /* Is this basic set a parameter domain?
1115 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1117 if (!bset)
1118 return isl_bool_error;
1119 return isl_space_is_params(bset->dim);
1122 /* Is this set a parameter domain?
1124 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1126 if (!set)
1127 return isl_bool_error;
1128 return isl_space_is_params(set->dim);
1131 /* Is this map actually a parameter domain?
1132 * Users should never call this function. Outside of isl,
1133 * a map can never be a parameter domain.
1135 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1137 if (!map)
1138 return isl_bool_error;
1139 return isl_space_is_params(map->dim);
1142 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1143 __isl_take isl_basic_map *bmap, unsigned extra,
1144 unsigned n_eq, unsigned n_ineq)
1146 int i;
1147 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1149 bmap->ctx = ctx;
1150 isl_ctx_ref(ctx);
1152 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1153 if (isl_blk_is_error(bmap->block))
1154 goto error;
1156 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1157 if ((n_ineq + n_eq) && !bmap->ineq)
1158 goto error;
1160 if (extra == 0) {
1161 bmap->block2 = isl_blk_empty();
1162 bmap->div = NULL;
1163 } else {
1164 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1165 if (isl_blk_is_error(bmap->block2))
1166 goto error;
1168 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1169 if (!bmap->div)
1170 goto error;
1173 for (i = 0; i < n_ineq + n_eq; ++i)
1174 bmap->ineq[i] = bmap->block.data + i * row_size;
1176 for (i = 0; i < extra; ++i)
1177 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1179 bmap->ref = 1;
1180 bmap->flags = 0;
1181 bmap->c_size = n_eq + n_ineq;
1182 bmap->eq = bmap->ineq + n_ineq;
1183 bmap->extra = extra;
1184 bmap->n_eq = 0;
1185 bmap->n_ineq = 0;
1186 bmap->n_div = 0;
1187 bmap->sample = NULL;
1189 return bmap;
1190 error:
1191 isl_basic_map_free(bmap);
1192 return NULL;
1195 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1196 unsigned nparam, unsigned dim, unsigned extra,
1197 unsigned n_eq, unsigned n_ineq)
1199 struct isl_basic_map *bmap;
1200 isl_space *space;
1202 space = isl_space_set_alloc(ctx, nparam, dim);
1203 if (!space)
1204 return NULL;
1206 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1207 return bset_from_bmap(bmap);
1210 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1211 unsigned extra, unsigned n_eq, unsigned n_ineq)
1213 struct isl_basic_map *bmap;
1214 if (!dim)
1215 return NULL;
1216 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1217 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1218 return bset_from_bmap(bmap);
1219 error:
1220 isl_space_free(dim);
1221 return NULL;
1224 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1225 unsigned extra, unsigned n_eq, unsigned n_ineq)
1227 struct isl_basic_map *bmap;
1229 if (!space)
1230 return NULL;
1231 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1232 if (!bmap)
1233 goto error;
1234 bmap->dim = space;
1236 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1237 error:
1238 isl_space_free(space);
1239 return NULL;
1242 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1243 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1244 unsigned n_eq, unsigned n_ineq)
1246 struct isl_basic_map *bmap;
1247 isl_space *dim;
1249 dim = isl_space_alloc(ctx, nparam, in, out);
1250 if (!dim)
1251 return NULL;
1253 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1254 return bmap;
1257 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1258 __isl_keep isl_basic_map *src)
1260 int i;
1261 unsigned total = isl_basic_map_total_dim(src);
1263 if (!dst)
1264 return NULL;
1266 for (i = 0; i < src->n_eq; ++i) {
1267 int j = isl_basic_map_alloc_equality(dst);
1268 if (j < 0)
1269 return isl_basic_map_free(dst);
1270 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1273 for (i = 0; i < src->n_ineq; ++i) {
1274 int j = isl_basic_map_alloc_inequality(dst);
1275 if (j < 0)
1276 return isl_basic_map_free(dst);
1277 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1280 for (i = 0; i < src->n_div; ++i) {
1281 int j = isl_basic_map_alloc_div(dst);
1282 if (j < 0)
1283 return isl_basic_map_free(dst);
1284 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1286 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1287 return dst;
1290 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1292 struct isl_basic_map *dup;
1294 if (!bmap)
1295 return NULL;
1296 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1297 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1298 dup = dup_constraints(dup, bmap);
1299 if (!dup)
1300 return NULL;
1301 dup->flags = bmap->flags;
1302 dup->sample = isl_vec_copy(bmap->sample);
1303 return dup;
1306 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1308 struct isl_basic_map *dup;
1310 dup = isl_basic_map_dup(bset_to_bmap(bset));
1311 return bset_from_bmap(dup);
1314 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1316 if (!bset)
1317 return NULL;
1319 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1320 bset->ref++;
1321 return bset;
1323 return isl_basic_set_dup(bset);
1326 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1328 if (!set)
1329 return NULL;
1331 set->ref++;
1332 return set;
1335 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1337 if (!bmap)
1338 return NULL;
1340 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1341 bmap->ref++;
1342 return bmap;
1344 bmap = isl_basic_map_dup(bmap);
1345 if (bmap)
1346 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1347 return bmap;
1350 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1352 if (!map)
1353 return NULL;
1355 map->ref++;
1356 return map;
1359 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1361 if (!bmap)
1362 return NULL;
1364 if (--bmap->ref > 0)
1365 return NULL;
1367 isl_ctx_deref(bmap->ctx);
1368 free(bmap->div);
1369 isl_blk_free(bmap->ctx, bmap->block2);
1370 free(bmap->ineq);
1371 isl_blk_free(bmap->ctx, bmap->block);
1372 isl_vec_free(bmap->sample);
1373 isl_space_free(bmap->dim);
1374 free(bmap);
1376 return NULL;
1379 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1381 return isl_basic_map_free(bset_to_bmap(bset));
1384 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1386 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1389 /* Check that "bset" does not involve any parameters.
1391 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1393 if (!bset)
1394 return isl_stat_error;
1395 if (isl_basic_set_dim(bset, isl_dim_param) != 0)
1396 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1397 "basic set should not have any parameters",
1398 return isl_stat_error);
1399 return isl_stat_ok;
1402 /* Check that "bset" does not involve any local variables.
1404 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1406 if (!bset)
1407 return isl_stat_error;
1408 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
1409 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1410 "basic set should not have any local variables",
1411 return isl_stat_error);
1412 return isl_stat_ok;
1415 /* Check that "map" has only named parameters, reporting an error
1416 * if it does not.
1418 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1420 return isl_space_check_named_params(isl_map_peek_space(map));
1423 /* Check that "bmap" has only named parameters, reporting an error
1424 * if it does not.
1426 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1428 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1431 /* Check that "bmap1" and "bmap2" have the same parameters,
1432 * reporting an error if they do not.
1434 static isl_stat isl_basic_map_check_equal_params(
1435 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1437 isl_bool match;
1439 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1440 if (match < 0)
1441 return isl_stat_error;
1442 if (!match)
1443 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1444 "parameters don't match", return isl_stat_error);
1445 return isl_stat_ok;
1448 __isl_give isl_map *isl_map_align_params_map_map_and(
1449 __isl_take isl_map *map1, __isl_take isl_map *map2,
1450 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1451 __isl_take isl_map *map2))
1453 if (!map1 || !map2)
1454 goto error;
1455 if (isl_map_has_equal_params(map1, map2))
1456 return fn(map1, map2);
1457 if (isl_map_check_named_params(map1) < 0)
1458 goto error;
1459 if (isl_map_check_named_params(map2) < 0)
1460 goto error;
1461 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1462 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1463 return fn(map1, map2);
1464 error:
1465 isl_map_free(map1);
1466 isl_map_free(map2);
1467 return NULL;
1470 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1471 __isl_keep isl_map *map2,
1472 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1474 isl_bool r;
1476 if (!map1 || !map2)
1477 return isl_bool_error;
1478 if (isl_map_has_equal_params(map1, map2))
1479 return fn(map1, map2);
1480 if (isl_map_check_named_params(map1) < 0)
1481 return isl_bool_error;
1482 if (isl_map_check_named_params(map2) < 0)
1483 return isl_bool_error;
1484 map1 = isl_map_copy(map1);
1485 map2 = isl_map_copy(map2);
1486 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1487 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1488 r = fn(map1, map2);
1489 isl_map_free(map1);
1490 isl_map_free(map2);
1491 return r;
1494 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1496 struct isl_ctx *ctx;
1497 if (!bmap)
1498 return -1;
1499 ctx = bmap->ctx;
1500 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1501 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1502 return -1);
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1505 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1506 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1507 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1508 isl_int *t;
1509 int j = isl_basic_map_alloc_inequality(bmap);
1510 if (j < 0)
1511 return -1;
1512 t = bmap->ineq[j];
1513 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1514 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1515 bmap->eq[-1] = t;
1516 bmap->n_eq++;
1517 bmap->n_ineq--;
1518 bmap->eq--;
1519 return 0;
1521 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1522 bmap->extra - bmap->n_div);
1523 return bmap->n_eq++;
1526 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1528 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1531 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1533 if (!bmap)
1534 return -1;
1535 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1536 bmap->n_eq -= n;
1537 return 0;
1540 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1542 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1545 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1547 isl_int *t;
1548 if (!bmap)
1549 return -1;
1550 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1552 if (pos != bmap->n_eq - 1) {
1553 t = bmap->eq[pos];
1554 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1555 bmap->eq[bmap->n_eq - 1] = t;
1557 bmap->n_eq--;
1558 return 0;
1561 /* Turn inequality "pos" of "bmap" into an equality.
1563 * In particular, we move the inequality in front of the equalities
1564 * and move the last inequality in the position of the moved inequality.
1565 * Note that isl_tab_make_equalities_explicit depends on this particular
1566 * change in the ordering of the constraints.
1568 void isl_basic_map_inequality_to_equality(
1569 struct isl_basic_map *bmap, unsigned pos)
1571 isl_int *t;
1573 t = bmap->ineq[pos];
1574 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1575 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1576 bmap->eq[-1] = t;
1577 bmap->n_eq++;
1578 bmap->n_ineq--;
1579 bmap->eq--;
1580 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1581 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1582 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1583 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1586 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1588 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1591 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1593 struct isl_ctx *ctx;
1594 if (!bmap)
1595 return -1;
1596 ctx = bmap->ctx;
1597 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1598 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1599 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1600 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1601 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1602 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1603 1 + isl_basic_map_total_dim(bmap),
1604 bmap->extra - bmap->n_div);
1605 return bmap->n_ineq++;
1608 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1610 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1613 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1615 if (!bmap)
1616 return -1;
1617 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1618 bmap->n_ineq -= n;
1619 return 0;
1622 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1624 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1627 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1629 isl_int *t;
1630 if (!bmap)
1631 return -1;
1632 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1634 if (pos != bmap->n_ineq - 1) {
1635 t = bmap->ineq[pos];
1636 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1637 bmap->ineq[bmap->n_ineq - 1] = t;
1638 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1640 bmap->n_ineq--;
1641 return 0;
1644 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1646 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1649 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1650 isl_int *eq)
1652 isl_bool empty;
1653 int k;
1655 empty = isl_basic_map_plain_is_empty(bmap);
1656 if (empty < 0)
1657 return isl_basic_map_free(bmap);
1658 if (empty)
1659 return bmap;
1661 bmap = isl_basic_map_cow(bmap);
1662 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1663 if (!bmap)
1664 return NULL;
1665 k = isl_basic_map_alloc_equality(bmap);
1666 if (k < 0)
1667 goto error;
1668 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1669 return bmap;
1670 error:
1671 isl_basic_map_free(bmap);
1672 return NULL;
1675 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1676 isl_int *eq)
1678 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1681 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1682 isl_int *ineq)
1684 int k;
1686 bmap = isl_basic_map_cow(bmap);
1687 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1688 if (!bmap)
1689 return NULL;
1690 k = isl_basic_map_alloc_inequality(bmap);
1691 if (k < 0)
1692 goto error;
1693 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1694 return bmap;
1695 error:
1696 isl_basic_map_free(bmap);
1697 return NULL;
1700 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1701 isl_int *ineq)
1703 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1706 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1708 if (!bmap)
1709 return -1;
1710 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1711 isl_seq_clr(bmap->div[bmap->n_div] +
1712 1 + 1 + isl_basic_map_total_dim(bmap),
1713 bmap->extra - bmap->n_div);
1714 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1715 return bmap->n_div++;
1718 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1720 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1723 #undef TYPE
1724 #define TYPE isl_basic_map
1725 #include "check_type_range_templ.c"
1727 /* Check that there are "n" dimensions of type "type" starting at "first"
1728 * in "bset".
1730 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1731 enum isl_dim_type type, unsigned first, unsigned n)
1733 return isl_basic_map_check_range(bset_to_bmap(bset),
1734 type, first, n);
1737 /* Insert an extra integer division, prescribed by "div", to "bmap"
1738 * at (integer division) position "pos".
1740 * The integer division is first added at the end and then moved
1741 * into the right position.
1743 __isl_give isl_basic_map *isl_basic_map_insert_div(
1744 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1746 int i, k;
1748 bmap = isl_basic_map_cow(bmap);
1749 if (!bmap || !div)
1750 return isl_basic_map_free(bmap);
1752 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1753 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1754 "unexpected size", return isl_basic_map_free(bmap));
1755 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1756 return isl_basic_map_free(bmap);
1758 bmap = isl_basic_map_extend_space(bmap,
1759 isl_basic_map_get_space(bmap), 1, 0, 2);
1760 k = isl_basic_map_alloc_div(bmap);
1761 if (k < 0)
1762 return isl_basic_map_free(bmap);
1763 isl_seq_cpy(bmap->div[k], div->el, div->size);
1764 isl_int_set_si(bmap->div[k][div->size], 0);
1766 for (i = k; i > pos; --i)
1767 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1769 return bmap;
1772 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1774 if (!bmap)
1775 return isl_stat_error;
1776 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1777 bmap->n_div -= n;
1778 return isl_stat_ok;
1781 static __isl_give isl_basic_map *add_constraints(
1782 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1783 unsigned i_pos, unsigned o_pos)
1785 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1786 isl_ctx *ctx;
1787 isl_space *space;
1788 struct isl_dim_map *dim_map;
1790 space = isl_basic_map_peek_space(bmap2);
1791 if (!bmap1 || !space)
1792 goto error;
1794 total = isl_basic_map_dim(bmap1, isl_dim_all);
1795 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1796 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1797 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1798 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1799 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1800 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1801 ctx = isl_basic_map_get_ctx(bmap1);
1802 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1803 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1804 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1805 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1806 isl_dim_map_div(dim_map, bmap2, total);
1808 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1809 error:
1810 isl_basic_map_free(bmap1);
1811 isl_basic_map_free(bmap2);
1812 return NULL;
1815 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1816 struct isl_basic_set *bset2, unsigned pos)
1818 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1819 bset_to_bmap(bset2), 0, pos));
1822 __isl_give isl_basic_map *isl_basic_map_extend_space(
1823 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1824 unsigned extra, unsigned n_eq, unsigned n_ineq)
1826 struct isl_basic_map *ext;
1827 unsigned flags;
1828 int dims_ok;
1830 if (!space)
1831 goto error;
1833 if (!base)
1834 goto error;
1836 dims_ok = isl_space_is_equal(base->dim, space) &&
1837 base->extra >= base->n_div + extra;
1839 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1840 room_for_ineq(base, n_ineq)) {
1841 isl_space_free(space);
1842 return base;
1845 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1846 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1847 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1848 extra += base->extra;
1849 n_eq += base->n_eq;
1850 n_ineq += base->n_ineq;
1852 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1853 space = NULL;
1854 if (!ext)
1855 goto error;
1857 if (dims_ok)
1858 ext->sample = isl_vec_copy(base->sample);
1859 flags = base->flags;
1860 ext = add_constraints(ext, base, 0, 0);
1861 if (ext) {
1862 ext->flags = flags;
1863 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1866 return ext;
1868 error:
1869 isl_space_free(space);
1870 isl_basic_map_free(base);
1871 return NULL;
1874 __isl_give isl_basic_set *isl_basic_set_extend_space(
1875 __isl_take isl_basic_set *base,
1876 __isl_take isl_space *dim, unsigned extra,
1877 unsigned n_eq, unsigned n_ineq)
1879 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1880 dim, extra, n_eq, n_ineq));
1883 struct isl_basic_map *isl_basic_map_extend_constraints(
1884 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1886 if (!base)
1887 return NULL;
1888 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1889 0, n_eq, n_ineq);
1892 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1893 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1894 unsigned n_eq, unsigned n_ineq)
1896 struct isl_basic_map *bmap;
1897 isl_space *dim;
1899 if (!base)
1900 return NULL;
1901 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1902 if (!dim)
1903 goto error;
1905 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1906 return bmap;
1907 error:
1908 isl_basic_map_free(base);
1909 return NULL;
1912 struct isl_basic_set *isl_basic_set_extend_constraints(
1913 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1915 isl_basic_map *bmap = bset_to_bmap(base);
1916 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1917 return bset_from_bmap(bmap);
1920 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1922 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1925 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1927 if (!bmap)
1928 return NULL;
1930 if (bmap->ref > 1) {
1931 bmap->ref--;
1932 bmap = isl_basic_map_dup(bmap);
1934 if (bmap) {
1935 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1936 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1938 return bmap;
1941 /* Clear all cached information in "map", either because it is about
1942 * to be modified or because it is being freed.
1943 * Always return the same pointer that is passed in.
1944 * This is needed for the use in isl_map_free.
1946 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1948 isl_basic_map_free(map->cached_simple_hull[0]);
1949 isl_basic_map_free(map->cached_simple_hull[1]);
1950 map->cached_simple_hull[0] = NULL;
1951 map->cached_simple_hull[1] = NULL;
1952 return map;
1955 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1957 return isl_map_cow(set);
1960 /* Return an isl_map that is equal to "map" and that has only
1961 * a single reference.
1963 * If the original input already has only one reference, then
1964 * simply return it, but clear all cached information, since
1965 * it may be rendered invalid by the operations that will be
1966 * performed on the result.
1968 * Otherwise, create a duplicate (without any cached information).
1970 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1972 if (!map)
1973 return NULL;
1975 if (map->ref == 1)
1976 return clear_caches(map);
1977 map->ref--;
1978 return isl_map_dup(map);
1981 static void swap_vars(struct isl_blk blk, isl_int *a,
1982 unsigned a_len, unsigned b_len)
1984 isl_seq_cpy(blk.data, a+a_len, b_len);
1985 isl_seq_cpy(blk.data+b_len, a, a_len);
1986 isl_seq_cpy(a, blk.data, b_len+a_len);
1989 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1990 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1992 int i;
1993 struct isl_blk blk;
1995 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1996 goto error;
1998 if (n1 == 0 || n2 == 0)
1999 return bmap;
2001 bmap = isl_basic_map_cow(bmap);
2002 if (!bmap)
2003 return NULL;
2005 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2006 if (isl_blk_is_error(blk))
2007 goto error;
2009 for (i = 0; i < bmap->n_eq; ++i)
2010 swap_vars(blk,
2011 bmap->eq[i] + pos, n1, n2);
2013 for (i = 0; i < bmap->n_ineq; ++i)
2014 swap_vars(blk,
2015 bmap->ineq[i] + pos, n1, n2);
2017 for (i = 0; i < bmap->n_div; ++i)
2018 swap_vars(blk,
2019 bmap->div[i]+1 + pos, n1, n2);
2021 isl_blk_free(bmap->ctx, blk);
2023 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2024 bmap = isl_basic_map_gauss(bmap, NULL);
2025 return isl_basic_map_finalize(bmap);
2026 error:
2027 isl_basic_map_free(bmap);
2028 return NULL;
2031 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2032 __isl_take isl_basic_map *bmap)
2034 int i = 0;
2035 unsigned total;
2036 if (!bmap)
2037 goto error;
2038 total = isl_basic_map_total_dim(bmap);
2039 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2040 return isl_basic_map_free(bmap);
2041 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2042 if (bmap->n_eq > 0)
2043 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2044 else {
2045 i = isl_basic_map_alloc_equality(bmap);
2046 if (i < 0)
2047 goto error;
2049 isl_int_set_si(bmap->eq[i][0], 1);
2050 isl_seq_clr(bmap->eq[i]+1, total);
2051 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2052 isl_vec_free(bmap->sample);
2053 bmap->sample = NULL;
2054 return isl_basic_map_finalize(bmap);
2055 error:
2056 isl_basic_map_free(bmap);
2057 return NULL;
2060 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2061 __isl_take isl_basic_set *bset)
2063 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2066 __isl_give isl_basic_map *isl_basic_map_set_rational(
2067 __isl_take isl_basic_map *bmap)
2069 if (!bmap)
2070 return NULL;
2072 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2073 return bmap;
2075 bmap = isl_basic_map_cow(bmap);
2076 if (!bmap)
2077 return NULL;
2079 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2081 return isl_basic_map_finalize(bmap);
2084 __isl_give isl_basic_set *isl_basic_set_set_rational(
2085 __isl_take isl_basic_set *bset)
2087 return isl_basic_map_set_rational(bset);
2090 __isl_give isl_basic_set *isl_basic_set_set_integral(
2091 __isl_take isl_basic_set *bset)
2093 if (!bset)
2094 return NULL;
2096 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2097 return bset;
2099 bset = isl_basic_set_cow(bset);
2100 if (!bset)
2101 return NULL;
2103 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2105 return isl_basic_set_finalize(bset);
2108 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2110 int i;
2112 map = isl_map_cow(map);
2113 if (!map)
2114 return NULL;
2115 for (i = 0; i < map->n; ++i) {
2116 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2117 if (!map->p[i])
2118 goto error;
2120 return map;
2121 error:
2122 isl_map_free(map);
2123 return NULL;
2126 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2128 return isl_map_set_rational(set);
2131 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2132 * of "bmap").
2134 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2136 isl_int *t = bmap->div[a];
2137 bmap->div[a] = bmap->div[b];
2138 bmap->div[b] = t;
2141 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2142 * div definitions accordingly.
2144 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2145 int a, int b)
2147 int i;
2148 unsigned off;
2150 if (!bmap)
2151 return NULL;
2153 off = isl_space_dim(bmap->dim, isl_dim_all);
2154 swap_div(bmap, a, b);
2156 for (i = 0; i < bmap->n_eq; ++i)
2157 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2159 for (i = 0; i < bmap->n_ineq; ++i)
2160 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2162 for (i = 0; i < bmap->n_div; ++i)
2163 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2164 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2166 return bmap;
2169 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2171 isl_seq_cpy(c, c + n, rem);
2172 isl_seq_clr(c + rem, n);
2175 /* Drop n dimensions starting at first.
2177 * In principle, this frees up some extra variables as the number
2178 * of columns remains constant, but we would have to extend
2179 * the div array too as the number of rows in this array is assumed
2180 * to be equal to extra.
2182 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2183 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2185 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2188 /* Move "n" divs starting at "first" to the end of the list of divs.
2190 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2191 unsigned first, unsigned n)
2193 isl_int **div;
2194 int i;
2196 if (first + n == bmap->n_div)
2197 return bmap;
2199 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2200 if (!div)
2201 goto error;
2202 for (i = 0; i < n; ++i)
2203 div[i] = bmap->div[first + i];
2204 for (i = 0; i < bmap->n_div - first - n; ++i)
2205 bmap->div[first + i] = bmap->div[first + n + i];
2206 for (i = 0; i < n; ++i)
2207 bmap->div[bmap->n_div - n + i] = div[i];
2208 free(div);
2209 return bmap;
2210 error:
2211 isl_basic_map_free(bmap);
2212 return NULL;
2215 #undef TYPE
2216 #define TYPE isl_map
2217 static
2218 #include "check_type_range_templ.c"
2220 /* Check that there are "n" dimensions of type "type" starting at "first"
2221 * in "set".
2223 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2224 enum isl_dim_type type, unsigned first, unsigned n)
2226 return isl_map_check_range(set_to_map(set), type, first, n);
2229 /* Drop "n" dimensions of type "type" starting at "first".
2230 * Perform the core computation, without cowing or
2231 * simplifying and finalizing the result.
2233 * In principle, this frees up some extra variables as the number
2234 * of columns remains constant, but we would have to extend
2235 * the div array too as the number of rows in this array is assumed
2236 * to be equal to extra.
2238 __isl_give isl_basic_map *isl_basic_map_drop_core(
2239 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2240 unsigned first, unsigned n)
2242 int i;
2243 unsigned offset;
2244 unsigned left;
2246 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2247 return isl_basic_map_free(bmap);
2249 offset = isl_basic_map_offset(bmap, type) + first;
2250 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2251 for (i = 0; i < bmap->n_eq; ++i)
2252 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2254 for (i = 0; i < bmap->n_ineq; ++i)
2255 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2257 for (i = 0; i < bmap->n_div; ++i)
2258 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2260 if (type == isl_dim_div) {
2261 bmap = move_divs_last(bmap, first, n);
2262 if (!bmap)
2263 return NULL;
2264 if (isl_basic_map_free_div(bmap, n) < 0)
2265 return isl_basic_map_free(bmap);
2266 } else
2267 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2268 if (!bmap->dim)
2269 return isl_basic_map_free(bmap);
2271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2272 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2273 return bmap;
2276 /* Drop "n" dimensions of type "type" starting at "first".
2278 * In principle, this frees up some extra variables as the number
2279 * of columns remains constant, but we would have to extend
2280 * the div array too as the number of rows in this array is assumed
2281 * to be equal to extra.
2283 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2284 enum isl_dim_type type, unsigned first, unsigned n)
2286 if (!bmap)
2287 return NULL;
2288 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2289 return bmap;
2291 bmap = isl_basic_map_cow(bmap);
2292 if (!bmap)
2293 return NULL;
2295 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2297 bmap = isl_basic_map_simplify(bmap);
2298 return isl_basic_map_finalize(bmap);
2301 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2302 enum isl_dim_type type, unsigned first, unsigned n)
2304 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2305 type, first, n));
2308 /* No longer consider "map" to be normalized.
2310 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2312 if (!map)
2313 return NULL;
2314 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2315 return map;
2318 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2319 enum isl_dim_type type, unsigned first, unsigned n)
2321 int i;
2323 if (isl_map_check_range(map, type, first, n) < 0)
2324 return isl_map_free(map);
2326 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2327 return map;
2328 map = isl_map_cow(map);
2329 if (!map)
2330 goto error;
2331 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2332 if (!map->dim)
2333 goto error;
2335 for (i = 0; i < map->n; ++i) {
2336 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2337 if (!map->p[i])
2338 goto error;
2340 map = isl_map_unmark_normalized(map);
2342 return map;
2343 error:
2344 isl_map_free(map);
2345 return NULL;
2348 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2349 enum isl_dim_type type, unsigned first, unsigned n)
2351 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2354 /* Drop the integer division at position "div", which is assumed
2355 * not to appear in any of the constraints or
2356 * in any of the other integer divisions.
2358 * Since the integer division is redundant, there is no need to cow.
2360 __isl_give isl_basic_map *isl_basic_map_drop_div(
2361 __isl_take isl_basic_map *bmap, unsigned div)
2363 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2366 /* Eliminate the specified n dimensions starting at first from the
2367 * constraints, without removing the dimensions from the space.
2368 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2370 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2371 enum isl_dim_type type, unsigned first, unsigned n)
2373 int i;
2375 if (n == 0)
2376 return map;
2378 if (isl_map_check_range(map, type, first, n) < 0)
2379 return isl_map_free(map);
2381 map = isl_map_cow(map);
2382 if (!map)
2383 return NULL;
2385 for (i = 0; i < map->n; ++i) {
2386 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2387 if (!map->p[i])
2388 goto error;
2390 return map;
2391 error:
2392 isl_map_free(map);
2393 return NULL;
2396 /* Eliminate the specified n dimensions starting at first from the
2397 * constraints, without removing the dimensions from the space.
2398 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2400 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2401 enum isl_dim_type type, unsigned first, unsigned n)
2403 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2406 /* Eliminate the specified n dimensions starting at first from the
2407 * constraints, without removing the dimensions from the space.
2408 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2410 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2411 unsigned first, unsigned n)
2413 return isl_set_eliminate(set, isl_dim_set, first, n);
2416 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2417 __isl_take isl_basic_map *bmap)
2419 if (!bmap)
2420 return NULL;
2421 bmap = isl_basic_map_eliminate_vars(bmap,
2422 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2423 if (!bmap)
2424 return NULL;
2425 bmap->n_div = 0;
2426 return isl_basic_map_finalize(bmap);
2429 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2430 __isl_take isl_basic_set *bset)
2432 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2435 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2437 int i;
2439 if (!map)
2440 return NULL;
2441 if (map->n == 0)
2442 return map;
2444 map = isl_map_cow(map);
2445 if (!map)
2446 return NULL;
2448 for (i = 0; i < map->n; ++i) {
2449 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2450 if (!map->p[i])
2451 goto error;
2453 return map;
2454 error:
2455 isl_map_free(map);
2456 return NULL;
2459 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2461 return isl_map_remove_divs(set);
2464 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2465 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2466 unsigned first, unsigned n)
2468 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2469 return isl_basic_map_free(bmap);
2470 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2471 return bmap;
2472 bmap = isl_basic_map_eliminate_vars(bmap,
2473 isl_basic_map_offset(bmap, type) - 1 + first, n);
2474 if (!bmap)
2475 return bmap;
2476 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2477 return bmap;
2478 bmap = isl_basic_map_drop(bmap, type, first, n);
2479 return bmap;
2482 /* Return true if the definition of the given div (recursively) involves
2483 * any of the given variables.
2485 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2486 unsigned first, unsigned n)
2488 int i;
2489 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2491 if (isl_int_is_zero(bmap->div[div][0]))
2492 return isl_bool_false;
2493 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2494 return isl_bool_true;
2496 for (i = bmap->n_div - 1; i >= 0; --i) {
2497 isl_bool involves;
2499 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2500 continue;
2501 involves = div_involves_vars(bmap, i, first, n);
2502 if (involves < 0 || involves)
2503 return involves;
2506 return isl_bool_false;
2509 /* Try and add a lower and/or upper bound on "div" to "bmap"
2510 * based on inequality "i".
2511 * "total" is the total number of variables (excluding the divs).
2512 * "v" is a temporary object that can be used during the calculations.
2513 * If "lb" is set, then a lower bound should be constructed.
2514 * If "ub" is set, then an upper bound should be constructed.
2516 * The calling function has already checked that the inequality does not
2517 * reference "div", but we still need to check that the inequality is
2518 * of the right form. We'll consider the case where we want to construct
2519 * a lower bound. The construction of upper bounds is similar.
2521 * Let "div" be of the form
2523 * q = floor((a + f(x))/d)
2525 * We essentially check if constraint "i" is of the form
2527 * b + f(x) >= 0
2529 * so that we can use it to derive a lower bound on "div".
2530 * However, we allow a slightly more general form
2532 * b + g(x) >= 0
2534 * with the condition that the coefficients of g(x) - f(x) are all
2535 * divisible by d.
2536 * Rewriting this constraint as
2538 * 0 >= -b - g(x)
2540 * adding a + f(x) to both sides and dividing by d, we obtain
2542 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2544 * Taking the floor on both sides, we obtain
2546 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2548 * or
2550 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2552 * In the case of an upper bound, we construct the constraint
2554 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2557 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2558 __isl_take isl_basic_map *bmap, int div, int i,
2559 unsigned total, isl_int v, int lb, int ub)
2561 int j;
2563 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2564 if (lb) {
2565 isl_int_sub(v, bmap->ineq[i][1 + j],
2566 bmap->div[div][1 + 1 + j]);
2567 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2569 if (ub) {
2570 isl_int_add(v, bmap->ineq[i][1 + j],
2571 bmap->div[div][1 + 1 + j]);
2572 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2575 if (!lb && !ub)
2576 return bmap;
2578 bmap = isl_basic_map_cow(bmap);
2579 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2580 if (lb) {
2581 int k = isl_basic_map_alloc_inequality(bmap);
2582 if (k < 0)
2583 goto error;
2584 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2585 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2586 bmap->div[div][1 + j]);
2587 isl_int_cdiv_q(bmap->ineq[k][j],
2588 bmap->ineq[k][j], bmap->div[div][0]);
2590 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2592 if (ub) {
2593 int k = isl_basic_map_alloc_inequality(bmap);
2594 if (k < 0)
2595 goto error;
2596 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2597 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2598 bmap->div[div][1 + j]);
2599 isl_int_fdiv_q(bmap->ineq[k][j],
2600 bmap->ineq[k][j], bmap->div[div][0]);
2602 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2605 return bmap;
2606 error:
2607 isl_basic_map_free(bmap);
2608 return NULL;
2611 /* This function is called right before "div" is eliminated from "bmap"
2612 * using Fourier-Motzkin.
2613 * Look through the constraints of "bmap" for constraints on the argument
2614 * of the integer division and use them to construct constraints on the
2615 * integer division itself. These constraints can then be combined
2616 * during the Fourier-Motzkin elimination.
2617 * Note that it is only useful to introduce lower bounds on "div"
2618 * if "bmap" already contains upper bounds on "div" as the newly
2619 * introduce lower bounds can then be combined with the pre-existing
2620 * upper bounds. Similarly for upper bounds.
2621 * We therefore first check if "bmap" contains any lower and/or upper bounds
2622 * on "div".
2624 * It is interesting to note that the introduction of these constraints
2625 * can indeed lead to more accurate results, even when compared to
2626 * deriving constraints on the argument of "div" from constraints on "div".
2627 * Consider, for example, the set
2629 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2631 * The second constraint can be rewritten as
2633 * 2 * [(-i-2j+3)/4] + k >= 0
2635 * from which we can derive
2637 * -i - 2j + 3 >= -2k
2639 * or
2641 * i + 2j <= 3 + 2k
2643 * Combined with the first constraint, we obtain
2645 * -3 <= 3 + 2k or k >= -3
2647 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2648 * the first constraint, we obtain
2650 * [(i + 2j)/4] >= [-3/4] = -1
2652 * Combining this constraint with the second constraint, we obtain
2654 * k >= -2
2656 static __isl_give isl_basic_map *insert_bounds_on_div(
2657 __isl_take isl_basic_map *bmap, int div)
2659 int i;
2660 int check_lb, check_ub;
2661 isl_int v;
2662 unsigned total;
2664 if (!bmap)
2665 return NULL;
2667 if (isl_int_is_zero(bmap->div[div][0]))
2668 return bmap;
2670 total = isl_space_dim(bmap->dim, isl_dim_all);
2672 check_lb = 0;
2673 check_ub = 0;
2674 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2675 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2676 if (s > 0)
2677 check_ub = 1;
2678 if (s < 0)
2679 check_lb = 1;
2682 if (!check_lb && !check_ub)
2683 return bmap;
2685 isl_int_init(v);
2687 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2688 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2689 continue;
2691 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2692 check_lb, check_ub);
2695 isl_int_clear(v);
2697 return bmap;
2700 /* Remove all divs (recursively) involving any of the given dimensions
2701 * in their definitions.
2703 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2704 __isl_take isl_basic_map *bmap,
2705 enum isl_dim_type type, unsigned first, unsigned n)
2707 int i;
2709 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2710 return isl_basic_map_free(bmap);
2711 first += isl_basic_map_offset(bmap, type);
2713 for (i = bmap->n_div - 1; i >= 0; --i) {
2714 isl_bool involves;
2716 involves = div_involves_vars(bmap, i, first, n);
2717 if (involves < 0)
2718 return isl_basic_map_free(bmap);
2719 if (!involves)
2720 continue;
2721 bmap = insert_bounds_on_div(bmap, i);
2722 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2723 if (!bmap)
2724 return NULL;
2725 i = bmap->n_div;
2728 return bmap;
2731 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2732 __isl_take isl_basic_set *bset,
2733 enum isl_dim_type type, unsigned first, unsigned n)
2735 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2738 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2739 enum isl_dim_type type, unsigned first, unsigned n)
2741 int i;
2743 if (!map)
2744 return NULL;
2745 if (map->n == 0)
2746 return map;
2748 map = isl_map_cow(map);
2749 if (!map)
2750 return NULL;
2752 for (i = 0; i < map->n; ++i) {
2753 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2754 type, first, n);
2755 if (!map->p[i])
2756 goto error;
2758 return map;
2759 error:
2760 isl_map_free(map);
2761 return NULL;
2764 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2765 enum isl_dim_type type, unsigned first, unsigned n)
2767 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2768 type, first, n));
2771 /* Does the description of "bmap" depend on the specified dimensions?
2772 * We also check whether the dimensions appear in any of the div definitions.
2773 * In principle there is no need for this check. If the dimensions appear
2774 * in a div definition, they also appear in the defining constraints of that
2775 * div.
2777 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2778 enum isl_dim_type type, unsigned first, unsigned n)
2780 int i;
2782 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2783 return isl_bool_error;
2785 first += isl_basic_map_offset(bmap, type);
2786 for (i = 0; i < bmap->n_eq; ++i)
2787 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2788 return isl_bool_true;
2789 for (i = 0; i < bmap->n_ineq; ++i)
2790 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2791 return isl_bool_true;
2792 for (i = 0; i < bmap->n_div; ++i) {
2793 if (isl_int_is_zero(bmap->div[i][0]))
2794 continue;
2795 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2796 return isl_bool_true;
2799 return isl_bool_false;
2802 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2803 enum isl_dim_type type, unsigned first, unsigned n)
2805 int i;
2807 if (isl_map_check_range(map, type, first, n) < 0)
2808 return isl_bool_error;
2810 for (i = 0; i < map->n; ++i) {
2811 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2812 type, first, n);
2813 if (involves < 0 || involves)
2814 return involves;
2817 return isl_bool_false;
2820 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2821 enum isl_dim_type type, unsigned first, unsigned n)
2823 return isl_basic_map_involves_dims(bset, type, first, n);
2826 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2827 enum isl_dim_type type, unsigned first, unsigned n)
2829 return isl_map_involves_dims(set, type, first, n);
2832 /* Drop all constraints in bmap that involve any of the dimensions
2833 * first to first+n-1.
2834 * This function only performs the actual removal of constraints.
2836 * This function should not call finalize since it is used by
2837 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2839 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2840 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2842 int i;
2844 if (n == 0)
2845 return bmap;
2847 bmap = isl_basic_map_cow(bmap);
2849 if (!bmap)
2850 return NULL;
2852 for (i = bmap->n_eq - 1; i >= 0; --i) {
2853 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2854 continue;
2855 isl_basic_map_drop_equality(bmap, i);
2858 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2859 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2860 continue;
2861 isl_basic_map_drop_inequality(bmap, i);
2864 return bmap;
2867 /* Drop all constraints in bset that involve any of the dimensions
2868 * first to first+n-1.
2869 * This function only performs the actual removal of constraints.
2871 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2872 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2874 return isl_basic_map_drop_constraints_involving(bset, first, n);
2877 /* Drop all constraints in bmap that do not involve any of the dimensions
2878 * first to first + n - 1 of the given type.
2880 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2881 __isl_take isl_basic_map *bmap,
2882 enum isl_dim_type type, unsigned first, unsigned n)
2884 int i;
2886 if (n == 0) {
2887 isl_space *space = isl_basic_map_get_space(bmap);
2888 isl_basic_map_free(bmap);
2889 return isl_basic_map_universe(space);
2891 bmap = isl_basic_map_cow(bmap);
2892 if (!bmap)
2893 return NULL;
2895 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2896 return isl_basic_map_free(bmap);
2898 first += isl_basic_map_offset(bmap, type) - 1;
2900 for (i = bmap->n_eq - 1; i >= 0; --i) {
2901 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2902 continue;
2903 isl_basic_map_drop_equality(bmap, i);
2906 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2907 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2908 continue;
2909 isl_basic_map_drop_inequality(bmap, i);
2912 bmap = isl_basic_map_add_known_div_constraints(bmap);
2913 return bmap;
2916 /* Drop all constraints in bset that do not involve any of the dimensions
2917 * first to first + n - 1 of the given type.
2919 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2920 __isl_take isl_basic_set *bset,
2921 enum isl_dim_type type, unsigned first, unsigned n)
2923 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2924 type, first, n);
2927 /* Drop all constraints in bmap that involve any of the dimensions
2928 * first to first + n - 1 of the given type.
2930 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2931 __isl_take isl_basic_map *bmap,
2932 enum isl_dim_type type, unsigned first, unsigned n)
2934 if (!bmap)
2935 return NULL;
2936 if (n == 0)
2937 return bmap;
2939 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2940 return isl_basic_map_free(bmap);
2942 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2943 first += isl_basic_map_offset(bmap, type) - 1;
2944 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2945 bmap = isl_basic_map_add_known_div_constraints(bmap);
2946 return bmap;
2949 /* Drop all constraints in bset that involve any of the dimensions
2950 * first to first + n - 1 of the given type.
2952 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2953 __isl_take isl_basic_set *bset,
2954 enum isl_dim_type type, unsigned first, unsigned n)
2956 return isl_basic_map_drop_constraints_involving_dims(bset,
2957 type, first, n);
2960 /* Drop constraints from "map" by applying "drop" to each basic map.
2962 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2963 enum isl_dim_type type, unsigned first, unsigned n,
2964 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2965 enum isl_dim_type type, unsigned first, unsigned n))
2967 int i;
2969 if (isl_map_check_range(map, type, first, n) < 0)
2970 return isl_map_free(map);
2972 map = isl_map_cow(map);
2973 if (!map)
2974 return NULL;
2976 for (i = 0; i < map->n; ++i) {
2977 map->p[i] = drop(map->p[i], type, first, n);
2978 if (!map->p[i])
2979 return isl_map_free(map);
2982 if (map->n > 1)
2983 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2985 return map;
2988 /* Drop all constraints in map that involve any of the dimensions
2989 * first to first + n - 1 of the given type.
2991 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2992 __isl_take isl_map *map,
2993 enum isl_dim_type type, unsigned first, unsigned n)
2995 if (n == 0)
2996 return map;
2997 return drop_constraints(map, type, first, n,
2998 &isl_basic_map_drop_constraints_involving_dims);
3001 /* Drop all constraints in "map" that do not involve any of the dimensions
3002 * first to first + n - 1 of the given type.
3004 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3005 __isl_take isl_map *map,
3006 enum isl_dim_type type, unsigned first, unsigned n)
3008 if (n == 0) {
3009 isl_space *space = isl_map_get_space(map);
3010 isl_map_free(map);
3011 return isl_map_universe(space);
3013 return drop_constraints(map, type, first, n,
3014 &isl_basic_map_drop_constraints_not_involving_dims);
3017 /* Drop all constraints in set that involve any of the dimensions
3018 * first to first + n - 1 of the given type.
3020 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3021 __isl_take isl_set *set,
3022 enum isl_dim_type type, unsigned first, unsigned n)
3024 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3027 /* Drop all constraints in "set" that do not involve any of the dimensions
3028 * first to first + n - 1 of the given type.
3030 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3031 __isl_take isl_set *set,
3032 enum isl_dim_type type, unsigned first, unsigned n)
3034 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3037 /* Does local variable "div" of "bmap" have a complete explicit representation?
3038 * Having a complete explicit representation requires not only
3039 * an explicit representation, but also that all local variables
3040 * that appear in this explicit representation in turn have
3041 * a complete explicit representation.
3043 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3045 int i;
3046 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3047 isl_bool marked;
3049 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3050 if (marked < 0 || marked)
3051 return isl_bool_not(marked);
3053 for (i = bmap->n_div - 1; i >= 0; --i) {
3054 isl_bool known;
3056 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3057 continue;
3058 known = isl_basic_map_div_is_known(bmap, i);
3059 if (known < 0 || !known)
3060 return known;
3063 return isl_bool_true;
3066 /* Remove all divs that are unknown or defined in terms of unknown divs.
3068 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3069 __isl_take isl_basic_map *bmap)
3071 int i;
3073 if (!bmap)
3074 return NULL;
3076 for (i = bmap->n_div - 1; i >= 0; --i) {
3077 if (isl_basic_map_div_is_known(bmap, i))
3078 continue;
3079 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3080 if (!bmap)
3081 return NULL;
3082 i = bmap->n_div;
3085 return bmap;
3088 /* Remove all divs that are unknown or defined in terms of unknown divs.
3090 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3091 __isl_take isl_basic_set *bset)
3093 return isl_basic_map_remove_unknown_divs(bset);
3096 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3098 int i;
3100 if (!map)
3101 return NULL;
3102 if (map->n == 0)
3103 return map;
3105 map = isl_map_cow(map);
3106 if (!map)
3107 return NULL;
3109 for (i = 0; i < map->n; ++i) {
3110 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3111 if (!map->p[i])
3112 goto error;
3114 return map;
3115 error:
3116 isl_map_free(map);
3117 return NULL;
3120 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3122 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3125 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3126 __isl_take isl_basic_set *bset,
3127 enum isl_dim_type type, unsigned first, unsigned n)
3129 isl_basic_map *bmap = bset_to_bmap(bset);
3130 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3131 return bset_from_bmap(bmap);
3134 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3135 enum isl_dim_type type, unsigned first, unsigned n)
3137 int i;
3139 if (n == 0)
3140 return map;
3142 map = isl_map_cow(map);
3143 if (isl_map_check_range(map, type, first, n) < 0)
3144 return isl_map_free(map);
3146 for (i = 0; i < map->n; ++i) {
3147 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3148 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3149 if (!map->p[i])
3150 goto error;
3152 map = isl_map_drop(map, type, first, n);
3153 return map;
3154 error:
3155 isl_map_free(map);
3156 return NULL;
3159 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3160 enum isl_dim_type type, unsigned first, unsigned n)
3162 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3163 type, first, n));
3166 /* Project out n inputs starting at first using Fourier-Motzkin */
3167 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3168 unsigned first, unsigned n)
3170 return isl_map_remove_dims(map, isl_dim_in, first, n);
3173 static void dump_term(struct isl_basic_map *bmap,
3174 isl_int c, int pos, FILE *out)
3176 const char *name;
3177 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3178 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3179 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3180 if (!pos)
3181 isl_int_print(out, c, 0);
3182 else {
3183 if (!isl_int_is_one(c))
3184 isl_int_print(out, c, 0);
3185 if (pos < 1 + nparam) {
3186 name = isl_space_get_dim_name(bmap->dim,
3187 isl_dim_param, pos - 1);
3188 if (name)
3189 fprintf(out, "%s", name);
3190 else
3191 fprintf(out, "p%d", pos - 1);
3192 } else if (pos < 1 + nparam + in)
3193 fprintf(out, "i%d", pos - 1 - nparam);
3194 else if (pos < 1 + nparam + dim)
3195 fprintf(out, "o%d", pos - 1 - nparam - in);
3196 else
3197 fprintf(out, "e%d", pos - 1 - nparam - dim);
3201 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3202 int sign, FILE *out)
3204 int i;
3205 int first;
3206 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3207 isl_int v;
3209 isl_int_init(v);
3210 for (i = 0, first = 1; i < len; ++i) {
3211 if (isl_int_sgn(c[i]) * sign <= 0)
3212 continue;
3213 if (!first)
3214 fprintf(out, " + ");
3215 first = 0;
3216 isl_int_abs(v, c[i]);
3217 dump_term(bmap, v, i, out);
3219 isl_int_clear(v);
3220 if (first)
3221 fprintf(out, "0");
3224 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3225 const char *op, FILE *out, int indent)
3227 int i;
3229 fprintf(out, "%*s", indent, "");
3231 dump_constraint_sign(bmap, c, 1, out);
3232 fprintf(out, " %s ", op);
3233 dump_constraint_sign(bmap, c, -1, out);
3235 fprintf(out, "\n");
3237 for (i = bmap->n_div; i < bmap->extra; ++i) {
3238 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3239 continue;
3240 fprintf(out, "%*s", indent, "");
3241 fprintf(out, "ERROR: unused div coefficient not zero\n");
3242 abort();
3246 static void dump_constraints(struct isl_basic_map *bmap,
3247 isl_int **c, unsigned n,
3248 const char *op, FILE *out, int indent)
3250 int i;
3252 for (i = 0; i < n; ++i)
3253 dump_constraint(bmap, c[i], op, out, indent);
3256 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3258 int j;
3259 int first = 1;
3260 unsigned total = isl_basic_map_total_dim(bmap);
3262 for (j = 0; j < 1 + total; ++j) {
3263 if (isl_int_is_zero(exp[j]))
3264 continue;
3265 if (!first && isl_int_is_pos(exp[j]))
3266 fprintf(out, "+");
3267 dump_term(bmap, exp[j], j, out);
3268 first = 0;
3272 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3274 int i;
3276 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3277 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3279 for (i = 0; i < bmap->n_div; ++i) {
3280 fprintf(out, "%*s", indent, "");
3281 fprintf(out, "e%d = [(", i);
3282 dump_affine(bmap, bmap->div[i]+1, out);
3283 fprintf(out, ")/");
3284 isl_int_print(out, bmap->div[i][0], 0);
3285 fprintf(out, "]\n");
3289 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3290 FILE *out, int indent)
3292 if (!bset) {
3293 fprintf(out, "null basic set\n");
3294 return;
3297 fprintf(out, "%*s", indent, "");
3298 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3299 bset->ref, bset->dim->nparam, bset->dim->n_out,
3300 bset->extra, bset->flags);
3301 dump(bset_to_bmap(bset), out, indent);
3304 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3305 FILE *out, int indent)
3307 if (!bmap) {
3308 fprintf(out, "null basic map\n");
3309 return;
3312 fprintf(out, "%*s", indent, "");
3313 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3314 "flags: %x, n_name: %d\n",
3315 bmap->ref,
3316 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3317 bmap->extra, bmap->flags, bmap->dim->n_id);
3318 dump(bmap, out, indent);
3321 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3322 unsigned pos)
3324 unsigned total;
3326 if (!bmap)
3327 return NULL;
3328 total = isl_basic_map_total_dim(bmap);
3329 if (pos >= bmap->n_ineq)
3330 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3331 "invalid position", return isl_basic_map_free(bmap));
3332 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3333 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3334 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3335 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3336 return bmap;
3339 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3340 unsigned flags)
3342 if (isl_space_check_is_set(space) < 0)
3343 goto error;
3344 return isl_map_alloc_space(space, n, flags);
3345 error:
3346 isl_space_free(space);
3347 return NULL;
3350 /* Make sure "map" has room for at least "n" more basic maps.
3352 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3354 int i;
3355 struct isl_map *grown = NULL;
3357 if (!map)
3358 return NULL;
3359 isl_assert(map->ctx, n >= 0, goto error);
3360 if (map->n + n <= map->size)
3361 return map;
3362 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3363 if (!grown)
3364 goto error;
3365 for (i = 0; i < map->n; ++i) {
3366 grown->p[i] = isl_basic_map_copy(map->p[i]);
3367 if (!grown->p[i])
3368 goto error;
3369 grown->n++;
3371 isl_map_free(map);
3372 return grown;
3373 error:
3374 isl_map_free(grown);
3375 isl_map_free(map);
3376 return NULL;
3379 /* Make sure "set" has room for at least "n" more basic sets.
3381 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3383 return set_from_map(isl_map_grow(set_to_map(set), n));
3386 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3388 return isl_map_from_basic_map(bset);
3391 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3393 struct isl_map *map;
3395 if (!bmap)
3396 return NULL;
3398 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3399 return isl_map_add_basic_map(map, bmap);
3402 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3403 __isl_take isl_basic_set *bset)
3405 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3406 bset_to_bmap(bset)));
3409 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3411 return isl_map_free(set);
3414 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3416 int i;
3418 if (!set) {
3419 fprintf(out, "null set\n");
3420 return;
3423 fprintf(out, "%*s", indent, "");
3424 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3425 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3426 set->flags);
3427 for (i = 0; i < set->n; ++i) {
3428 fprintf(out, "%*s", indent, "");
3429 fprintf(out, "basic set %d:\n", i);
3430 isl_basic_set_print_internal(set->p[i], out, indent+4);
3434 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3436 int i;
3438 if (!map) {
3439 fprintf(out, "null map\n");
3440 return;
3443 fprintf(out, "%*s", indent, "");
3444 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3445 "flags: %x, n_name: %d\n",
3446 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3447 map->dim->n_out, map->flags, map->dim->n_id);
3448 for (i = 0; i < map->n; ++i) {
3449 fprintf(out, "%*s", indent, "");
3450 fprintf(out, "basic map %d:\n", i);
3451 isl_basic_map_print_internal(map->p[i], out, indent+4);
3455 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3456 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3458 struct isl_basic_map *bmap_domain;
3460 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3461 goto error;
3463 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3464 isl_assert(bset->ctx,
3465 isl_basic_map_compatible_domain(bmap, bset), goto error);
3467 bmap = isl_basic_map_cow(bmap);
3468 if (!bmap)
3469 goto error;
3470 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3471 bset->n_div, bset->n_eq, bset->n_ineq);
3472 bmap_domain = isl_basic_map_from_domain(bset);
3473 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3475 bmap = isl_basic_map_simplify(bmap);
3476 return isl_basic_map_finalize(bmap);
3477 error:
3478 isl_basic_map_free(bmap);
3479 isl_basic_set_free(bset);
3480 return NULL;
3483 /* Check that the space of "bset" is the same as that of the range of "bmap".
3485 static isl_stat isl_basic_map_check_compatible_range(
3486 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3488 isl_bool ok;
3490 ok = isl_basic_map_compatible_range(bmap, bset);
3491 if (ok < 0)
3492 return isl_stat_error;
3493 if (!ok)
3494 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3495 "incompatible spaces", return isl_stat_error);
3497 return isl_stat_ok;
3500 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3501 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3503 struct isl_basic_map *bmap_range;
3505 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3506 goto error;
3508 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3509 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3510 goto error;
3512 if (isl_basic_set_plain_is_universe(bset)) {
3513 isl_basic_set_free(bset);
3514 return bmap;
3517 bmap = isl_basic_map_cow(bmap);
3518 if (!bmap)
3519 goto error;
3520 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3521 bset->n_div, bset->n_eq, bset->n_ineq);
3522 bmap_range = bset_to_bmap(bset);
3523 bmap = add_constraints(bmap, bmap_range, 0, 0);
3525 bmap = isl_basic_map_simplify(bmap);
3526 return isl_basic_map_finalize(bmap);
3527 error:
3528 isl_basic_map_free(bmap);
3529 isl_basic_set_free(bset);
3530 return NULL;
3533 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3534 __isl_keep isl_vec *vec)
3536 int i;
3537 unsigned total;
3538 isl_int s;
3540 if (!bmap || !vec)
3541 return isl_bool_error;
3543 total = 1 + isl_basic_map_total_dim(bmap);
3544 if (total != vec->size)
3545 return isl_bool_false;
3547 isl_int_init(s);
3549 for (i = 0; i < bmap->n_eq; ++i) {
3550 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3551 if (!isl_int_is_zero(s)) {
3552 isl_int_clear(s);
3553 return isl_bool_false;
3557 for (i = 0; i < bmap->n_ineq; ++i) {
3558 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3559 if (isl_int_is_neg(s)) {
3560 isl_int_clear(s);
3561 return isl_bool_false;
3565 isl_int_clear(s);
3567 return isl_bool_true;
3570 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3571 __isl_keep isl_vec *vec)
3573 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3576 __isl_give isl_basic_map *isl_basic_map_intersect(
3577 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3579 struct isl_vec *sample = NULL;
3581 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3582 goto error;
3583 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3584 isl_space_dim(bmap1->dim, isl_dim_param) &&
3585 isl_space_dim(bmap2->dim, isl_dim_all) !=
3586 isl_space_dim(bmap2->dim, isl_dim_param))
3587 return isl_basic_map_intersect(bmap2, bmap1);
3589 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3590 isl_space_dim(bmap2->dim, isl_dim_param))
3591 isl_assert(bmap1->ctx,
3592 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3594 if (isl_basic_map_plain_is_empty(bmap1)) {
3595 isl_basic_map_free(bmap2);
3596 return bmap1;
3598 if (isl_basic_map_plain_is_empty(bmap2)) {
3599 isl_basic_map_free(bmap1);
3600 return bmap2;
3603 if (bmap1->sample &&
3604 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3605 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3606 sample = isl_vec_copy(bmap1->sample);
3607 else if (bmap2->sample &&
3608 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3609 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3610 sample = isl_vec_copy(bmap2->sample);
3612 bmap1 = isl_basic_map_cow(bmap1);
3613 if (!bmap1)
3614 goto error;
3615 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3616 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3617 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3619 if (!bmap1)
3620 isl_vec_free(sample);
3621 else if (sample) {
3622 isl_vec_free(bmap1->sample);
3623 bmap1->sample = sample;
3626 bmap1 = isl_basic_map_simplify(bmap1);
3627 return isl_basic_map_finalize(bmap1);
3628 error:
3629 if (sample)
3630 isl_vec_free(sample);
3631 isl_basic_map_free(bmap1);
3632 isl_basic_map_free(bmap2);
3633 return NULL;
3636 struct isl_basic_set *isl_basic_set_intersect(
3637 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3639 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3640 bset_to_bmap(bset2)));
3643 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3644 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3646 return isl_basic_set_intersect(bset1, bset2);
3649 /* Special case of isl_map_intersect, where both map1 and map2
3650 * are convex, without any divs and such that either map1 or map2
3651 * contains a single constraint. This constraint is then simply
3652 * added to the other map.
3654 static __isl_give isl_map *map_intersect_add_constraint(
3655 __isl_take isl_map *map1, __isl_take isl_map *map2)
3657 isl_assert(map1->ctx, map1->n == 1, goto error);
3658 isl_assert(map2->ctx, map1->n == 1, goto error);
3659 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3660 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3662 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3663 return isl_map_intersect(map2, map1);
3665 map1 = isl_map_cow(map1);
3666 if (!map1)
3667 goto error;
3668 if (isl_map_plain_is_empty(map1)) {
3669 isl_map_free(map2);
3670 return map1;
3672 if (map2->p[0]->n_eq == 1)
3673 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3674 else
3675 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3676 map2->p[0]->ineq[0]);
3678 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3679 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3680 if (!map1->p[0])
3681 goto error;
3683 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3684 isl_basic_map_free(map1->p[0]);
3685 map1->n = 0;
3688 isl_map_free(map2);
3690 map1 = isl_map_unmark_normalized(map1);
3691 return map1;
3692 error:
3693 isl_map_free(map1);
3694 isl_map_free(map2);
3695 return NULL;
3698 /* map2 may be either a parameter domain or a map living in the same
3699 * space as map1.
3701 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3702 __isl_take isl_map *map2)
3704 unsigned flags = 0;
3705 isl_bool equal;
3706 isl_map *result;
3707 int i, j;
3709 if (!map1 || !map2)
3710 goto error;
3712 if ((isl_map_plain_is_empty(map1) ||
3713 isl_map_plain_is_universe(map2)) &&
3714 isl_space_is_equal(map1->dim, map2->dim)) {
3715 isl_map_free(map2);
3716 return map1;
3718 if ((isl_map_plain_is_empty(map2) ||
3719 isl_map_plain_is_universe(map1)) &&
3720 isl_space_is_equal(map1->dim, map2->dim)) {
3721 isl_map_free(map1);
3722 return map2;
3725 if (map1->n == 1 && map2->n == 1 &&
3726 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3727 isl_space_is_equal(map1->dim, map2->dim) &&
3728 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3729 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3730 return map_intersect_add_constraint(map1, map2);
3732 equal = isl_map_plain_is_equal(map1, map2);
3733 if (equal < 0)
3734 goto error;
3735 if (equal) {
3736 isl_map_free(map2);
3737 return map1;
3740 if (isl_space_dim(map2->dim, isl_dim_all) !=
3741 isl_space_dim(map2->dim, isl_dim_param))
3742 isl_assert(map1->ctx,
3743 isl_space_is_equal(map1->dim, map2->dim), goto error);
3745 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3746 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3747 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3749 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3750 map1->n * map2->n, flags);
3751 if (!result)
3752 goto error;
3753 for (i = 0; i < map1->n; ++i)
3754 for (j = 0; j < map2->n; ++j) {
3755 struct isl_basic_map *part;
3756 part = isl_basic_map_intersect(
3757 isl_basic_map_copy(map1->p[i]),
3758 isl_basic_map_copy(map2->p[j]));
3759 if (isl_basic_map_is_empty(part) < 0)
3760 part = isl_basic_map_free(part);
3761 result = isl_map_add_basic_map(result, part);
3762 if (!result)
3763 goto error;
3765 isl_map_free(map1);
3766 isl_map_free(map2);
3767 return result;
3768 error:
3769 isl_map_free(map1);
3770 isl_map_free(map2);
3771 return NULL;
3774 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3775 __isl_take isl_map *map2)
3777 if (!map1 || !map2)
3778 goto error;
3779 if (!isl_space_is_equal(map1->dim, map2->dim))
3780 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3781 "spaces don't match", goto error);
3782 return map_intersect_internal(map1, map2);
3783 error:
3784 isl_map_free(map1);
3785 isl_map_free(map2);
3786 return NULL;
3789 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3790 __isl_take isl_map *map2)
3792 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3795 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3797 return set_from_map(isl_map_intersect(set_to_map(set1),
3798 set_to_map(set2)));
3801 /* map_intersect_internal accepts intersections
3802 * with parameter domains, so we can just call that function.
3804 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3805 __isl_take isl_set *params)
3807 return map_intersect_internal(map, params);
3810 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3811 __isl_take isl_map *map2)
3813 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3816 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3817 __isl_take isl_set *params)
3819 return isl_map_intersect_params(set, params);
3822 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3824 isl_space *space;
3825 unsigned pos, n1, n2;
3827 if (!bmap)
3828 return NULL;
3829 bmap = isl_basic_map_cow(bmap);
3830 if (!bmap)
3831 return NULL;
3832 space = isl_space_reverse(isl_space_copy(bmap->dim));
3833 pos = isl_basic_map_offset(bmap, isl_dim_in);
3834 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3835 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3836 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3837 return isl_basic_map_reset_space(bmap, space);
3840 static __isl_give isl_basic_map *basic_map_space_reset(
3841 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3843 isl_space *space;
3845 if (!bmap)
3846 return NULL;
3847 if (!isl_space_is_named_or_nested(bmap->dim, type))
3848 return bmap;
3850 space = isl_basic_map_get_space(bmap);
3851 space = isl_space_reset(space, type);
3852 bmap = isl_basic_map_reset_space(bmap, space);
3853 return bmap;
3856 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3857 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3858 unsigned pos, unsigned n)
3860 isl_bool rational, is_empty;
3861 isl_space *res_space;
3862 struct isl_basic_map *res;
3863 struct isl_dim_map *dim_map;
3864 unsigned total, off;
3865 enum isl_dim_type t;
3867 if (n == 0)
3868 return basic_map_space_reset(bmap, type);
3870 is_empty = isl_basic_map_plain_is_empty(bmap);
3871 if (is_empty < 0)
3872 return isl_basic_map_free(bmap);
3873 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3874 type, pos, n);
3875 if (!res_space)
3876 return isl_basic_map_free(bmap);
3877 if (is_empty) {
3878 isl_basic_map_free(bmap);
3879 return isl_basic_map_empty(res_space);
3882 total = isl_basic_map_total_dim(bmap) + n;
3883 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3884 off = 0;
3885 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3886 if (t != type) {
3887 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3888 } else {
3889 unsigned size = isl_basic_map_dim(bmap, t);
3890 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3891 0, pos, off);
3892 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3893 pos, size - pos, off + pos + n);
3895 off += isl_space_dim(res_space, t);
3897 isl_dim_map_div(dim_map, bmap, off);
3899 res = isl_basic_map_alloc_space(res_space,
3900 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3901 rational = isl_basic_map_is_rational(bmap);
3902 if (rational < 0)
3903 res = isl_basic_map_free(res);
3904 if (rational)
3905 res = isl_basic_map_set_rational(res);
3906 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3907 return isl_basic_map_finalize(res);
3910 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3911 __isl_take isl_basic_set *bset,
3912 enum isl_dim_type type, unsigned pos, unsigned n)
3914 return isl_basic_map_insert_dims(bset, type, pos, n);
3917 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3918 enum isl_dim_type type, unsigned n)
3920 if (!bmap)
3921 return NULL;
3922 return isl_basic_map_insert_dims(bmap, type,
3923 isl_basic_map_dim(bmap, type), n);
3926 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3927 enum isl_dim_type type, unsigned n)
3929 if (!bset)
3930 return NULL;
3931 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3932 return isl_basic_map_add_dims(bset, type, n);
3933 error:
3934 isl_basic_set_free(bset);
3935 return NULL;
3938 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3939 enum isl_dim_type type)
3941 isl_space *space;
3943 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3944 return map;
3946 space = isl_map_get_space(map);
3947 space = isl_space_reset(space, type);
3948 map = isl_map_reset_space(map, space);
3949 return map;
3952 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3953 enum isl_dim_type type, unsigned pos, unsigned n)
3955 int i;
3957 if (n == 0)
3958 return map_space_reset(map, type);
3960 map = isl_map_cow(map);
3961 if (!map)
3962 return NULL;
3964 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3965 if (!map->dim)
3966 goto error;
3968 for (i = 0; i < map->n; ++i) {
3969 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3970 if (!map->p[i])
3971 goto error;
3974 return map;
3975 error:
3976 isl_map_free(map);
3977 return NULL;
3980 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3981 enum isl_dim_type type, unsigned pos, unsigned n)
3983 return isl_map_insert_dims(set, type, pos, n);
3986 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3987 enum isl_dim_type type, unsigned n)
3989 if (!map)
3990 return NULL;
3991 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3994 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3995 enum isl_dim_type type, unsigned n)
3997 if (!set)
3998 return NULL;
3999 isl_assert(set->ctx, type != isl_dim_in, goto error);
4000 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4001 error:
4002 isl_set_free(set);
4003 return NULL;
4006 __isl_give isl_basic_map *isl_basic_map_move_dims(
4007 __isl_take isl_basic_map *bmap,
4008 enum isl_dim_type dst_type, unsigned dst_pos,
4009 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4011 struct isl_dim_map *dim_map;
4012 struct isl_basic_map *res;
4013 enum isl_dim_type t;
4014 unsigned total, off;
4016 if (!bmap)
4017 return NULL;
4018 if (n == 0) {
4019 bmap = isl_basic_map_reset(bmap, src_type);
4020 bmap = isl_basic_map_reset(bmap, dst_type);
4021 return bmap;
4024 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4025 return isl_basic_map_free(bmap);
4027 if (dst_type == src_type && dst_pos == src_pos)
4028 return bmap;
4030 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4032 if (pos(bmap->dim, dst_type) + dst_pos ==
4033 pos(bmap->dim, src_type) + src_pos +
4034 ((src_type < dst_type) ? n : 0)) {
4035 bmap = isl_basic_map_cow(bmap);
4036 if (!bmap)
4037 return NULL;
4039 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4040 src_type, src_pos, n);
4041 if (!bmap->dim)
4042 goto error;
4044 bmap = isl_basic_map_finalize(bmap);
4046 return bmap;
4049 total = isl_basic_map_total_dim(bmap);
4050 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4052 off = 0;
4053 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4054 unsigned size = isl_space_dim(bmap->dim, t);
4055 if (t == dst_type) {
4056 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4057 0, dst_pos, off);
4058 off += dst_pos;
4059 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4060 src_pos, n, off);
4061 off += n;
4062 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4063 dst_pos, size - dst_pos, off);
4064 off += size - dst_pos;
4065 } else if (t == src_type) {
4066 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4067 0, src_pos, off);
4068 off += src_pos;
4069 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4070 src_pos + n, size - src_pos - n, off);
4071 off += size - src_pos - n;
4072 } else {
4073 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4074 off += size;
4077 isl_dim_map_div(dim_map, bmap, off);
4079 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4080 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4081 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4082 if (!bmap)
4083 goto error;
4085 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4086 src_type, src_pos, n);
4087 if (!bmap->dim)
4088 goto error;
4090 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4091 bmap = isl_basic_map_gauss(bmap, NULL);
4092 bmap = isl_basic_map_finalize(bmap);
4094 return bmap;
4095 error:
4096 isl_basic_map_free(bmap);
4097 return NULL;
4100 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4101 enum isl_dim_type dst_type, unsigned dst_pos,
4102 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4104 isl_basic_map *bmap = bset_to_bmap(bset);
4105 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4106 src_type, src_pos, n);
4107 return bset_from_bmap(bmap);
4110 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4111 enum isl_dim_type dst_type, unsigned dst_pos,
4112 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4114 if (!set)
4115 return NULL;
4116 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4117 return set_from_map(isl_map_move_dims(set_to_map(set),
4118 dst_type, dst_pos, src_type, src_pos, n));
4119 error:
4120 isl_set_free(set);
4121 return NULL;
4124 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4125 enum isl_dim_type dst_type, unsigned dst_pos,
4126 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4128 int i;
4130 if (n == 0) {
4131 map = isl_map_reset(map, src_type);
4132 map = isl_map_reset(map, dst_type);
4133 return map;
4136 if (isl_map_check_range(map, src_type, src_pos, n))
4137 return isl_map_free(map);
4139 if (dst_type == src_type && dst_pos == src_pos)
4140 return map;
4142 isl_assert(map->ctx, dst_type != src_type, goto error);
4144 map = isl_map_cow(map);
4145 if (!map)
4146 return NULL;
4148 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4149 if (!map->dim)
4150 goto error;
4152 for (i = 0; i < map->n; ++i) {
4153 map->p[i] = isl_basic_map_move_dims(map->p[i],
4154 dst_type, dst_pos,
4155 src_type, src_pos, n);
4156 if (!map->p[i])
4157 goto error;
4160 return map;
4161 error:
4162 isl_map_free(map);
4163 return NULL;
4166 /* Move the specified dimensions to the last columns right before
4167 * the divs. Don't change the dimension specification of bmap.
4168 * That's the responsibility of the caller.
4170 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4171 enum isl_dim_type type, unsigned first, unsigned n)
4173 struct isl_dim_map *dim_map;
4174 struct isl_basic_map *res;
4175 enum isl_dim_type t;
4176 unsigned total, off;
4178 if (!bmap)
4179 return NULL;
4180 if (pos(bmap->dim, type) + first + n ==
4181 1 + isl_space_dim(bmap->dim, isl_dim_all))
4182 return bmap;
4184 total = isl_basic_map_total_dim(bmap);
4185 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4187 off = 0;
4188 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4189 unsigned size = isl_space_dim(bmap->dim, t);
4190 if (t == type) {
4191 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4192 0, first, off);
4193 off += first;
4194 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4195 first, n, total - bmap->n_div - n);
4196 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4197 first + n, size - (first + n), off);
4198 off += size - (first + n);
4199 } else {
4200 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4201 off += size;
4204 isl_dim_map_div(dim_map, bmap, off + n);
4206 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4207 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4208 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4209 return res;
4212 /* Insert "n" rows in the divs of "bmap".
4214 * The number of columns is not changed, which means that the last
4215 * dimensions of "bmap" are being reintepreted as the new divs.
4216 * The space of "bmap" is not adjusted, however, which means
4217 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4218 * from the space of "bmap" is the responsibility of the caller.
4220 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4221 int n)
4223 int i;
4224 size_t row_size;
4225 isl_int **new_div;
4226 isl_int *old;
4228 bmap = isl_basic_map_cow(bmap);
4229 if (!bmap)
4230 return NULL;
4232 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4233 old = bmap->block2.data;
4234 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4235 (bmap->extra + n) * (1 + row_size));
4236 if (!bmap->block2.data)
4237 return isl_basic_map_free(bmap);
4238 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4239 if (!new_div)
4240 return isl_basic_map_free(bmap);
4241 for (i = 0; i < n; ++i) {
4242 new_div[i] = bmap->block2.data +
4243 (bmap->extra + i) * (1 + row_size);
4244 isl_seq_clr(new_div[i], 1 + row_size);
4246 for (i = 0; i < bmap->extra; ++i)
4247 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4248 free(bmap->div);
4249 bmap->div = new_div;
4250 bmap->n_div += n;
4251 bmap->extra += n;
4253 return bmap;
4256 /* Drop constraints from "bmap" that only involve the variables
4257 * of "type" in the range [first, first + n] that are not related
4258 * to any of the variables outside that interval.
4259 * These constraints cannot influence the values for the variables
4260 * outside the interval, except in case they cause "bmap" to be empty.
4261 * Only drop the constraints if "bmap" is known to be non-empty.
4263 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4264 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4265 unsigned first, unsigned n)
4267 int i;
4268 int *groups;
4269 unsigned dim, n_div;
4270 isl_bool non_empty;
4272 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4273 if (non_empty < 0)
4274 return isl_basic_map_free(bmap);
4275 if (!non_empty)
4276 return bmap;
4278 dim = isl_basic_map_dim(bmap, isl_dim_all);
4279 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4280 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4281 if (!groups)
4282 return isl_basic_map_free(bmap);
4283 first += isl_basic_map_offset(bmap, type) - 1;
4284 for (i = 0; i < first; ++i)
4285 groups[i] = -1;
4286 for (i = first + n; i < dim - n_div; ++i)
4287 groups[i] = -1;
4289 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4291 return bmap;
4294 /* Turn the n dimensions of type type, starting at first
4295 * into existentially quantified variables.
4297 * If a subset of the projected out variables are unrelated
4298 * to any of the variables that remain, then the constraints
4299 * involving this subset are simply dropped first.
4301 __isl_give isl_basic_map *isl_basic_map_project_out(
4302 __isl_take isl_basic_map *bmap,
4303 enum isl_dim_type type, unsigned first, unsigned n)
4305 isl_bool empty;
4307 if (n == 0)
4308 return basic_map_space_reset(bmap, type);
4309 if (type == isl_dim_div)
4310 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4311 "cannot project out existentially quantified variables",
4312 return isl_basic_map_free(bmap));
4314 empty = isl_basic_map_plain_is_empty(bmap);
4315 if (empty < 0)
4316 return isl_basic_map_free(bmap);
4317 if (empty)
4318 bmap = isl_basic_map_set_to_empty(bmap);
4320 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4321 if (!bmap)
4322 return NULL;
4324 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4325 return isl_basic_map_remove_dims(bmap, type, first, n);
4327 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4328 return isl_basic_map_free(bmap);
4330 bmap = move_last(bmap, type, first, n);
4331 bmap = isl_basic_map_cow(bmap);
4332 bmap = insert_div_rows(bmap, n);
4333 if (!bmap)
4334 return NULL;
4336 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4337 if (!bmap->dim)
4338 goto error;
4339 bmap = isl_basic_map_simplify(bmap);
4340 bmap = isl_basic_map_drop_redundant_divs(bmap);
4341 return isl_basic_map_finalize(bmap);
4342 error:
4343 isl_basic_map_free(bmap);
4344 return NULL;
4347 /* Turn the n dimensions of type type, starting at first
4348 * into existentially quantified variables.
4350 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4351 enum isl_dim_type type, unsigned first, unsigned n)
4353 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4354 type, first, n));
4357 /* Turn the n dimensions of type type, starting at first
4358 * into existentially quantified variables.
4360 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4361 enum isl_dim_type type, unsigned first, unsigned n)
4363 int i;
4365 if (n == 0)
4366 return map_space_reset(map, type);
4368 if (isl_map_check_range(map, type, first, n) < 0)
4369 return isl_map_free(map);
4371 map = isl_map_cow(map);
4372 if (!map)
4373 return NULL;
4375 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4376 if (!map->dim)
4377 goto error;
4379 for (i = 0; i < map->n; ++i) {
4380 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4381 if (!map->p[i])
4382 goto error;
4385 return map;
4386 error:
4387 isl_map_free(map);
4388 return NULL;
4391 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4392 * into existentially quantified variables.
4394 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4395 enum isl_dim_type type, unsigned first, unsigned n)
4397 unsigned dim;
4399 if (isl_map_check_range(map, type, first, n) < 0)
4400 return isl_map_free(map);
4401 dim = isl_map_dim(map, type);
4402 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4403 map = isl_map_project_out(map, type, 0, first);
4404 return map;
4407 /* Turn the n dimensions of type type, starting at first
4408 * into existentially quantified variables.
4410 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4411 enum isl_dim_type type, unsigned first, unsigned n)
4413 return set_from_map(isl_map_project_out(set_to_map(set),
4414 type, first, n));
4417 /* Return a map that projects the elements in "set" onto their
4418 * "n" set dimensions starting at "first".
4419 * "type" should be equal to isl_dim_set.
4421 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4422 enum isl_dim_type type, unsigned first, unsigned n)
4424 int i;
4425 isl_map *map;
4427 if (type != isl_dim_set)
4428 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4429 "only set dimensions can be projected out", goto error);
4430 if (isl_set_check_range(set, type, first, n) < 0)
4431 return isl_set_free(set);
4433 map = isl_map_from_domain(set);
4434 map = isl_map_add_dims(map, isl_dim_out, n);
4435 for (i = 0; i < n; ++i)
4436 map = isl_map_equate(map, isl_dim_in, first + i,
4437 isl_dim_out, i);
4438 return map;
4439 error:
4440 isl_set_free(set);
4441 return NULL;
4444 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4445 unsigned n)
4447 int i, j;
4449 for (i = 0; i < n; ++i) {
4450 j = isl_basic_map_alloc_div(bmap);
4451 if (j < 0)
4452 goto error;
4453 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4455 return bmap;
4456 error:
4457 isl_basic_map_free(bmap);
4458 return NULL;
4461 struct isl_basic_map *isl_basic_map_apply_range(
4462 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4464 isl_space *space_result = NULL;
4465 struct isl_basic_map *bmap;
4466 unsigned n_in, n_out, n, nparam, total, pos;
4467 struct isl_dim_map *dim_map1, *dim_map2;
4469 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4470 goto error;
4471 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4472 bmap2->dim, isl_dim_in))
4473 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4474 "spaces don't match", goto error);
4476 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4477 isl_space_copy(bmap2->dim));
4479 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4480 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4481 n = isl_basic_map_dim(bmap1, isl_dim_out);
4482 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4484 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4485 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4486 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4487 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4488 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4489 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4490 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4491 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4492 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4493 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4494 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4496 bmap = isl_basic_map_alloc_space(space_result,
4497 bmap1->n_div + bmap2->n_div + n,
4498 bmap1->n_eq + bmap2->n_eq,
4499 bmap1->n_ineq + bmap2->n_ineq);
4500 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4501 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4502 bmap = add_divs(bmap, n);
4503 bmap = isl_basic_map_simplify(bmap);
4504 bmap = isl_basic_map_drop_redundant_divs(bmap);
4505 return isl_basic_map_finalize(bmap);
4506 error:
4507 isl_basic_map_free(bmap1);
4508 isl_basic_map_free(bmap2);
4509 return NULL;
4512 struct isl_basic_set *isl_basic_set_apply(
4513 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4515 if (!bset || !bmap)
4516 goto error;
4518 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4519 goto error);
4521 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4522 bmap));
4523 error:
4524 isl_basic_set_free(bset);
4525 isl_basic_map_free(bmap);
4526 return NULL;
4529 struct isl_basic_map *isl_basic_map_apply_domain(
4530 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4532 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4533 goto error;
4534 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4535 bmap2->dim, isl_dim_in))
4536 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4537 "spaces don't match", goto error);
4539 bmap1 = isl_basic_map_reverse(bmap1);
4540 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4541 return isl_basic_map_reverse(bmap1);
4542 error:
4543 isl_basic_map_free(bmap1);
4544 isl_basic_map_free(bmap2);
4545 return NULL;
4548 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4549 * A \cap B -> f(A) + f(B)
4551 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4552 __isl_take isl_basic_map *bmap2)
4554 unsigned n_in, n_out, nparam, total, pos;
4555 struct isl_basic_map *bmap = NULL;
4556 struct isl_dim_map *dim_map1, *dim_map2;
4557 int i;
4559 if (!bmap1 || !bmap2)
4560 goto error;
4562 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4563 goto error);
4565 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4566 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4567 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4569 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4570 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4571 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4572 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4573 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4574 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4575 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4576 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4577 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4578 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4579 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4581 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4582 bmap1->n_div + bmap2->n_div + 2 * n_out,
4583 bmap1->n_eq + bmap2->n_eq + n_out,
4584 bmap1->n_ineq + bmap2->n_ineq);
4585 for (i = 0; i < n_out; ++i) {
4586 int j = isl_basic_map_alloc_equality(bmap);
4587 if (j < 0)
4588 goto error;
4589 isl_seq_clr(bmap->eq[j], 1+total);
4590 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4591 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4592 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4594 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4595 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4596 bmap = add_divs(bmap, 2 * n_out);
4598 bmap = isl_basic_map_simplify(bmap);
4599 return isl_basic_map_finalize(bmap);
4600 error:
4601 isl_basic_map_free(bmap);
4602 isl_basic_map_free(bmap1);
4603 isl_basic_map_free(bmap2);
4604 return NULL;
4607 /* Given two maps A -> f(A) and B -> g(B), construct a map
4608 * A \cap B -> f(A) + f(B)
4610 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4611 __isl_take isl_map *map2)
4613 struct isl_map *result;
4614 int i, j;
4616 if (!map1 || !map2)
4617 goto error;
4619 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4621 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4622 map1->n * map2->n, 0);
4623 if (!result)
4624 goto error;
4625 for (i = 0; i < map1->n; ++i)
4626 for (j = 0; j < map2->n; ++j) {
4627 struct isl_basic_map *part;
4628 part = isl_basic_map_sum(
4629 isl_basic_map_copy(map1->p[i]),
4630 isl_basic_map_copy(map2->p[j]));
4631 if (isl_basic_map_is_empty(part))
4632 isl_basic_map_free(part);
4633 else
4634 result = isl_map_add_basic_map(result, part);
4635 if (!result)
4636 goto error;
4638 isl_map_free(map1);
4639 isl_map_free(map2);
4640 return result;
4641 error:
4642 isl_map_free(map1);
4643 isl_map_free(map2);
4644 return NULL;
4647 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4648 __isl_take isl_set *set2)
4650 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4653 /* Given a basic map A -> f(A), construct A -> -f(A).
4655 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4657 int i, j;
4658 unsigned off, n;
4660 bmap = isl_basic_map_cow(bmap);
4661 if (!bmap)
4662 return NULL;
4664 n = isl_basic_map_dim(bmap, isl_dim_out);
4665 off = isl_basic_map_offset(bmap, isl_dim_out);
4666 for (i = 0; i < bmap->n_eq; ++i)
4667 for (j = 0; j < n; ++j)
4668 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4669 for (i = 0; i < bmap->n_ineq; ++i)
4670 for (j = 0; j < n; ++j)
4671 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4672 for (i = 0; i < bmap->n_div; ++i)
4673 for (j = 0; j < n; ++j)
4674 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4675 bmap = isl_basic_map_gauss(bmap, NULL);
4676 return isl_basic_map_finalize(bmap);
4679 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4681 return isl_basic_map_neg(bset);
4684 /* Given a map A -> f(A), construct A -> -f(A).
4686 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4688 int i;
4690 map = isl_map_cow(map);
4691 if (!map)
4692 return NULL;
4694 for (i = 0; i < map->n; ++i) {
4695 map->p[i] = isl_basic_map_neg(map->p[i]);
4696 if (!map->p[i])
4697 goto error;
4700 return map;
4701 error:
4702 isl_map_free(map);
4703 return NULL;
4706 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4708 return set_from_map(isl_map_neg(set_to_map(set)));
4711 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4712 * A -> floor(f(A)/d).
4714 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4715 isl_int d)
4717 unsigned n_in, n_out, nparam, total, pos;
4718 struct isl_basic_map *result = NULL;
4719 struct isl_dim_map *dim_map;
4720 int i;
4722 if (!bmap)
4723 return NULL;
4725 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4726 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4727 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4729 total = nparam + n_in + n_out + bmap->n_div + n_out;
4730 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4731 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4732 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4733 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4734 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4736 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4737 bmap->n_div + n_out,
4738 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4739 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4740 result = add_divs(result, n_out);
4741 for (i = 0; i < n_out; ++i) {
4742 int j;
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_neg(result->ineq[j][1+nparam+n_in+i], d);
4748 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4749 j = isl_basic_map_alloc_inequality(result);
4750 if (j < 0)
4751 goto error;
4752 isl_seq_clr(result->ineq[j], 1+total);
4753 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4754 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4755 isl_int_sub_ui(result->ineq[j][0], d, 1);
4758 result = isl_basic_map_simplify(result);
4759 return isl_basic_map_finalize(result);
4760 error:
4761 isl_basic_map_free(result);
4762 return NULL;
4765 /* Given a map A -> f(A) and an integer d, construct a map
4766 * A -> floor(f(A)/d).
4768 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4770 int i;
4772 map = isl_map_cow(map);
4773 if (!map)
4774 return NULL;
4776 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4777 for (i = 0; i < map->n; ++i) {
4778 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4779 if (!map->p[i])
4780 goto error;
4782 map = isl_map_unmark_normalized(map);
4784 return map;
4785 error:
4786 isl_map_free(map);
4787 return NULL;
4790 /* Given a map A -> f(A) and an integer d, construct a map
4791 * A -> floor(f(A)/d).
4793 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4794 __isl_take isl_val *d)
4796 if (!map || !d)
4797 goto error;
4798 if (!isl_val_is_int(d))
4799 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4800 "expecting integer denominator", goto error);
4801 map = isl_map_floordiv(map, d->n);
4802 isl_val_free(d);
4803 return map;
4804 error:
4805 isl_map_free(map);
4806 isl_val_free(d);
4807 return NULL;
4810 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4811 unsigned pos)
4813 int i;
4814 unsigned nparam;
4815 unsigned n_in;
4817 i = isl_basic_map_alloc_equality(bmap);
4818 if (i < 0)
4819 goto error;
4820 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4821 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4822 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4823 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4824 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4825 return isl_basic_map_finalize(bmap);
4826 error:
4827 isl_basic_map_free(bmap);
4828 return NULL;
4831 /* Add a constraint to "bmap" expressing i_pos < o_pos
4833 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4834 unsigned pos)
4836 int i;
4837 unsigned nparam;
4838 unsigned n_in;
4840 i = isl_basic_map_alloc_inequality(bmap);
4841 if (i < 0)
4842 goto error;
4843 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4844 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4845 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4846 isl_int_set_si(bmap->ineq[i][0], -1);
4847 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4848 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4849 return isl_basic_map_finalize(bmap);
4850 error:
4851 isl_basic_map_free(bmap);
4852 return NULL;
4855 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4857 static __isl_give isl_basic_map *var_less_or_equal(
4858 __isl_take isl_basic_map *bmap, unsigned pos)
4860 int i;
4861 unsigned nparam;
4862 unsigned n_in;
4864 i = isl_basic_map_alloc_inequality(bmap);
4865 if (i < 0)
4866 goto error;
4867 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4868 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4869 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4870 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4871 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4872 return isl_basic_map_finalize(bmap);
4873 error:
4874 isl_basic_map_free(bmap);
4875 return NULL;
4878 /* Add a constraint to "bmap" expressing i_pos > o_pos
4880 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4881 unsigned pos)
4883 int i;
4884 unsigned nparam;
4885 unsigned n_in;
4887 i = isl_basic_map_alloc_inequality(bmap);
4888 if (i < 0)
4889 goto error;
4890 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4891 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4892 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4893 isl_int_set_si(bmap->ineq[i][0], -1);
4894 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4895 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4896 return isl_basic_map_finalize(bmap);
4897 error:
4898 isl_basic_map_free(bmap);
4899 return NULL;
4902 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4904 static __isl_give isl_basic_map *var_more_or_equal(
4905 __isl_take isl_basic_map *bmap, unsigned pos)
4907 int i;
4908 unsigned nparam;
4909 unsigned n_in;
4911 i = isl_basic_map_alloc_inequality(bmap);
4912 if (i < 0)
4913 goto error;
4914 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4915 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4916 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4917 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4918 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4919 return isl_basic_map_finalize(bmap);
4920 error:
4921 isl_basic_map_free(bmap);
4922 return NULL;
4925 __isl_give isl_basic_map *isl_basic_map_equal(
4926 __isl_take isl_space *space, unsigned n_equal)
4928 int i;
4929 struct isl_basic_map *bmap;
4930 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4931 if (!bmap)
4932 return NULL;
4933 for (i = 0; i < n_equal && bmap; ++i)
4934 bmap = var_equal(bmap, i);
4935 return isl_basic_map_finalize(bmap);
4938 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4940 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4941 unsigned pos)
4943 int i;
4944 struct isl_basic_map *bmap;
4945 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4946 if (!bmap)
4947 return NULL;
4948 for (i = 0; i < pos && bmap; ++i)
4949 bmap = var_equal(bmap, i);
4950 if (bmap)
4951 bmap = var_less(bmap, pos);
4952 return isl_basic_map_finalize(bmap);
4955 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4957 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4958 __isl_take isl_space *space, unsigned pos)
4960 int i;
4961 isl_basic_map *bmap;
4963 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4964 for (i = 0; i < pos; ++i)
4965 bmap = var_equal(bmap, i);
4966 bmap = var_less_or_equal(bmap, pos);
4967 return isl_basic_map_finalize(bmap);
4970 /* Return a relation on "space" expressing i_pos > o_pos
4972 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4973 unsigned pos)
4975 int i;
4976 struct isl_basic_map *bmap;
4977 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4978 if (!bmap)
4979 return NULL;
4980 for (i = 0; i < pos && bmap; ++i)
4981 bmap = var_equal(bmap, i);
4982 if (bmap)
4983 bmap = var_more(bmap, pos);
4984 return isl_basic_map_finalize(bmap);
4987 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4989 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4990 __isl_take isl_space *space, unsigned pos)
4992 int i;
4993 isl_basic_map *bmap;
4995 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4996 for (i = 0; i < pos; ++i)
4997 bmap = var_equal(bmap, i);
4998 bmap = var_more_or_equal(bmap, pos);
4999 return isl_basic_map_finalize(bmap);
5002 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5003 unsigned n, int equal)
5005 struct isl_map *map;
5006 int i;
5008 if (n == 0 && equal)
5009 return isl_map_universe(space);
5011 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5013 for (i = 0; i + 1 < n; ++i)
5014 map = isl_map_add_basic_map(map,
5015 isl_basic_map_less_at(isl_space_copy(space), i));
5016 if (n > 0) {
5017 if (equal)
5018 map = isl_map_add_basic_map(map,
5019 isl_basic_map_less_or_equal_at(space, n - 1));
5020 else
5021 map = isl_map_add_basic_map(map,
5022 isl_basic_map_less_at(space, n - 1));
5023 } else
5024 isl_space_free(space);
5026 return map;
5029 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5031 if (!space)
5032 return NULL;
5033 return map_lex_lte_first(space, space->n_out, equal);
5036 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5038 return map_lex_lte_first(dim, n, 0);
5041 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5043 return map_lex_lte_first(dim, n, 1);
5046 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5048 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5051 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5053 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5056 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5057 unsigned n, int equal)
5059 struct isl_map *map;
5060 int i;
5062 if (n == 0 && equal)
5063 return isl_map_universe(space);
5065 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5067 for (i = 0; i + 1 < n; ++i)
5068 map = isl_map_add_basic_map(map,
5069 isl_basic_map_more_at(isl_space_copy(space), i));
5070 if (n > 0) {
5071 if (equal)
5072 map = isl_map_add_basic_map(map,
5073 isl_basic_map_more_or_equal_at(space, n - 1));
5074 else
5075 map = isl_map_add_basic_map(map,
5076 isl_basic_map_more_at(space, n - 1));
5077 } else
5078 isl_space_free(space);
5080 return map;
5083 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5085 if (!space)
5086 return NULL;
5087 return map_lex_gte_first(space, space->n_out, equal);
5090 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5092 return map_lex_gte_first(dim, n, 0);
5095 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5097 return map_lex_gte_first(dim, n, 1);
5100 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5102 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5105 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5107 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5110 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5111 __isl_take isl_set *set2)
5113 isl_map *map;
5114 map = isl_map_lex_le(isl_set_get_space(set1));
5115 map = isl_map_intersect_domain(map, set1);
5116 map = isl_map_intersect_range(map, set2);
5117 return map;
5120 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5121 __isl_take isl_set *set2)
5123 isl_map *map;
5124 map = isl_map_lex_lt(isl_set_get_space(set1));
5125 map = isl_map_intersect_domain(map, set1);
5126 map = isl_map_intersect_range(map, set2);
5127 return map;
5130 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5131 __isl_take isl_set *set2)
5133 isl_map *map;
5134 map = isl_map_lex_ge(isl_set_get_space(set1));
5135 map = isl_map_intersect_domain(map, set1);
5136 map = isl_map_intersect_range(map, set2);
5137 return map;
5140 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5141 __isl_take isl_set *set2)
5143 isl_map *map;
5144 map = isl_map_lex_gt(isl_set_get_space(set1));
5145 map = isl_map_intersect_domain(map, set1);
5146 map = isl_map_intersect_range(map, set2);
5147 return map;
5150 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5151 __isl_take isl_map *map2)
5153 isl_map *map;
5154 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5155 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5156 map = isl_map_apply_range(map, isl_map_reverse(map2));
5157 return map;
5160 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5161 __isl_take isl_map *map2)
5163 isl_map *map;
5164 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5165 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5166 map = isl_map_apply_range(map, isl_map_reverse(map2));
5167 return map;
5170 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5171 __isl_take isl_map *map2)
5173 isl_map *map;
5174 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5175 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5176 map = isl_map_apply_range(map, isl_map_reverse(map2));
5177 return map;
5180 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5181 __isl_take isl_map *map2)
5183 isl_map *map;
5184 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5185 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5186 map = isl_map_apply_range(map, isl_map_reverse(map2));
5187 return map;
5190 /* For a div d = floor(f/m), add the constraint
5192 * f - m d >= 0
5194 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5195 unsigned pos, isl_int *div)
5197 int i;
5198 unsigned total = isl_basic_map_total_dim(bmap);
5200 i = isl_basic_map_alloc_inequality(bmap);
5201 if (i < 0)
5202 return isl_stat_error;
5203 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5204 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5206 return isl_stat_ok;
5209 /* For a div d = floor(f/m), add the constraint
5211 * -(f-(m-1)) + m d >= 0
5213 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5214 unsigned pos, isl_int *div)
5216 int i;
5217 unsigned total = isl_basic_map_total_dim(bmap);
5219 i = isl_basic_map_alloc_inequality(bmap);
5220 if (i < 0)
5221 return isl_stat_error;
5222 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5223 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5224 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5225 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5227 return isl_stat_ok;
5230 /* For a div d = floor(f/m), add the constraints
5232 * f - m d >= 0
5233 * -(f-(m-1)) + m d >= 0
5235 * Note that the second constraint is the negation of
5237 * f - m d >= m
5239 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5240 unsigned pos, isl_int *div)
5242 if (add_upper_div_constraint(bmap, pos, div) < 0)
5243 return -1;
5244 if (add_lower_div_constraint(bmap, pos, div) < 0)
5245 return -1;
5246 return 0;
5249 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5250 unsigned pos, isl_int *div)
5252 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5253 pos, div);
5256 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map *bmap,
5257 unsigned div)
5259 unsigned total = isl_basic_map_total_dim(bmap);
5260 unsigned div_pos = total - bmap->n_div + div;
5262 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5263 bmap->div[div]);
5266 /* For each known div d = floor(f/m), add the constraints
5268 * f - m d >= 0
5269 * -(f-(m-1)) + m d >= 0
5271 * Remove duplicate constraints in case of some these div constraints
5272 * already appear in "bmap".
5274 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5275 __isl_take isl_basic_map *bmap)
5277 unsigned n_div;
5279 if (!bmap)
5280 return NULL;
5281 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5282 if (n_div == 0)
5283 return bmap;
5285 bmap = add_known_div_constraints(bmap);
5286 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5287 bmap = isl_basic_map_finalize(bmap);
5288 return bmap;
5291 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5293 * In particular, if this div is of the form d = floor(f/m),
5294 * then add the constraint
5296 * f - m d >= 0
5298 * if sign < 0 or the constraint
5300 * -(f-(m-1)) + m d >= 0
5302 * if sign > 0.
5304 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5305 unsigned div, int sign)
5307 unsigned total;
5308 unsigned div_pos;
5310 if (!bmap)
5311 return -1;
5313 total = isl_basic_map_total_dim(bmap);
5314 div_pos = total - bmap->n_div + div;
5316 if (sign < 0)
5317 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5318 else
5319 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5322 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5323 __isl_take isl_basic_map *bmap)
5325 if (!bmap)
5326 goto error;
5327 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5328 bmap->n_div == 0 &&
5329 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5330 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5331 return bset_from_bmap(bmap);
5332 bmap = isl_basic_map_cow(bmap);
5333 if (!bmap)
5334 goto error;
5335 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5336 if (!bmap->dim)
5337 goto error;
5338 bmap->extra -= bmap->n_div;
5339 bmap->n_div = 0;
5340 bmap = isl_basic_map_finalize(bmap);
5341 return bset_from_bmap(bmap);
5342 error:
5343 isl_basic_map_free(bmap);
5344 return NULL;
5347 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5348 __isl_take isl_basic_set *bset)
5350 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5353 /* Replace each element in "list" by the result of applying
5354 * isl_basic_map_underlying_set to the element.
5356 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5357 __isl_take isl_basic_map_list *list)
5359 int i, n;
5361 if (!list)
5362 return NULL;
5364 n = isl_basic_map_list_n_basic_map(list);
5365 for (i = 0; i < n; ++i) {
5366 isl_basic_map *bmap;
5367 isl_basic_set *bset;
5369 bmap = isl_basic_map_list_get_basic_map(list, i);
5370 bset = isl_basic_set_underlying_set(bmap);
5371 list = isl_basic_set_list_set_basic_set(list, i, bset);
5374 return list;
5377 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5378 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5380 struct isl_basic_map *bmap;
5381 struct isl_ctx *ctx;
5382 unsigned total;
5383 int i;
5385 if (!bset || !like)
5386 goto error;
5387 ctx = bset->ctx;
5388 if (isl_basic_set_check_no_params(bset) < 0 ||
5389 isl_basic_set_check_no_locals(bset) < 0)
5390 goto error;
5391 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5392 goto error);
5393 if (like->n_div == 0) {
5394 isl_space *space = isl_basic_map_get_space(like);
5395 isl_basic_map_free(like);
5396 return isl_basic_map_reset_space(bset, space);
5398 bset = isl_basic_set_cow(bset);
5399 if (!bset)
5400 goto error;
5401 total = bset->dim->n_out + bset->extra;
5402 bmap = bset_to_bmap(bset);
5403 isl_space_free(bmap->dim);
5404 bmap->dim = isl_space_copy(like->dim);
5405 if (!bmap->dim)
5406 goto error;
5407 bmap->n_div = like->n_div;
5408 bmap->extra += like->n_div;
5409 if (bmap->extra) {
5410 unsigned ltotal;
5411 isl_int **div;
5412 ltotal = total - bmap->extra + like->extra;
5413 if (ltotal > total)
5414 ltotal = total;
5415 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5416 bmap->extra * (1 + 1 + total));
5417 if (isl_blk_is_error(bmap->block2))
5418 goto error;
5419 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5420 if (!div)
5421 goto error;
5422 bmap->div = div;
5423 for (i = 0; i < bmap->extra; ++i)
5424 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5425 for (i = 0; i < like->n_div; ++i) {
5426 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5427 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5429 bmap = isl_basic_map_add_known_div_constraints(bmap);
5431 isl_basic_map_free(like);
5432 bmap = isl_basic_map_simplify(bmap);
5433 bmap = isl_basic_map_finalize(bmap);
5434 return bmap;
5435 error:
5436 isl_basic_map_free(like);
5437 isl_basic_set_free(bset);
5438 return NULL;
5441 struct isl_basic_set *isl_basic_set_from_underlying_set(
5442 struct isl_basic_set *bset, struct isl_basic_set *like)
5444 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5445 bset_to_bmap(like)));
5448 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5450 int i;
5452 map = isl_map_cow(map);
5453 if (!map)
5454 return NULL;
5455 map->dim = isl_space_cow(map->dim);
5456 if (!map->dim)
5457 goto error;
5459 for (i = 1; i < map->n; ++i)
5460 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5461 goto error);
5462 for (i = 0; i < map->n; ++i) {
5463 map->p[i] = bset_to_bmap(
5464 isl_basic_map_underlying_set(map->p[i]));
5465 if (!map->p[i])
5466 goto error;
5468 if (map->n == 0)
5469 map->dim = isl_space_underlying(map->dim, 0);
5470 else {
5471 isl_space_free(map->dim);
5472 map->dim = isl_space_copy(map->p[0]->dim);
5474 if (!map->dim)
5475 goto error;
5476 return set_from_map(map);
5477 error:
5478 isl_map_free(map);
5479 return NULL;
5482 /* Replace the space of "bmap" by "space".
5484 * If the space of "bmap" is identical to "space" (including the identifiers
5485 * of the input and output dimensions), then simply return the original input.
5487 __isl_give isl_basic_map *isl_basic_map_reset_space(
5488 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5490 isl_bool equal;
5491 isl_space *bmap_space;
5493 bmap_space = isl_basic_map_peek_space(bmap);
5494 equal = isl_space_is_equal(bmap_space, space);
5495 if (equal >= 0 && equal)
5496 equal = isl_space_has_equal_ids(bmap_space, space);
5497 if (equal < 0)
5498 goto error;
5499 if (equal) {
5500 isl_space_free(space);
5501 return bmap;
5503 bmap = isl_basic_map_cow(bmap);
5504 if (!bmap || !space)
5505 goto error;
5507 isl_space_free(bmap->dim);
5508 bmap->dim = space;
5510 bmap = isl_basic_map_finalize(bmap);
5512 return bmap;
5513 error:
5514 isl_basic_map_free(bmap);
5515 isl_space_free(space);
5516 return NULL;
5519 __isl_give isl_basic_set *isl_basic_set_reset_space(
5520 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5522 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5523 dim));
5526 /* Check that the total dimensions of "map" and "space" are the same.
5528 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5529 __isl_keep isl_space *space)
5531 unsigned dim1, dim2;
5533 if (!map || !space)
5534 return isl_stat_error;
5535 dim1 = isl_map_dim(map, isl_dim_all);
5536 dim2 = isl_space_dim(space, isl_dim_all);
5537 if (dim1 == dim2)
5538 return isl_stat_ok;
5539 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5540 "total dimensions do not match", return isl_stat_error);
5543 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5544 __isl_take isl_space *space)
5546 int i;
5548 map = isl_map_cow(map);
5549 if (!map || !space)
5550 goto error;
5552 for (i = 0; i < map->n; ++i) {
5553 map->p[i] = isl_basic_map_reset_space(map->p[i],
5554 isl_space_copy(space));
5555 if (!map->p[i])
5556 goto error;
5558 isl_space_free(map->dim);
5559 map->dim = space;
5561 return map;
5562 error:
5563 isl_map_free(map);
5564 isl_space_free(space);
5565 return NULL;
5568 /* Replace the space of "map" by "space", without modifying
5569 * the dimension of "map".
5571 * If the space of "map" is identical to "space" (including the identifiers
5572 * of the input and output dimensions), then simply return the original input.
5574 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5575 __isl_take isl_space *space)
5577 isl_bool equal;
5578 isl_space *map_space;
5580 map_space = isl_map_peek_space(map);
5581 equal = isl_space_is_equal(map_space, space);
5582 if (equal >= 0 && equal)
5583 equal = isl_space_has_equal_ids(map_space, space);
5584 if (equal < 0)
5585 goto error;
5586 if (equal) {
5587 isl_space_free(space);
5588 return map;
5590 if (check_map_space_equal_total_dim(map, space) < 0)
5591 goto error;
5592 return isl_map_reset_space(map, space);
5593 error:
5594 isl_map_free(map);
5595 isl_space_free(space);
5596 return NULL;
5599 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5600 __isl_take isl_space *dim)
5602 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5605 /* Compute the parameter domain of the given basic set.
5607 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5609 isl_bool is_params;
5610 isl_space *space;
5611 unsigned n;
5613 is_params = isl_basic_set_is_params(bset);
5614 if (is_params < 0)
5615 return isl_basic_set_free(bset);
5616 if (is_params)
5617 return bset;
5619 n = isl_basic_set_dim(bset, isl_dim_set);
5620 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5621 space = isl_basic_set_get_space(bset);
5622 space = isl_space_params(space);
5623 bset = isl_basic_set_reset_space(bset, space);
5624 return bset;
5627 /* Construct a zero-dimensional basic set with the given parameter domain.
5629 __isl_give isl_basic_set *isl_basic_set_from_params(
5630 __isl_take isl_basic_set *bset)
5632 isl_space *space;
5633 space = isl_basic_set_get_space(bset);
5634 space = isl_space_set_from_params(space);
5635 bset = isl_basic_set_reset_space(bset, space);
5636 return bset;
5639 /* Compute the parameter domain of the given set.
5641 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5643 isl_space *space;
5644 unsigned n;
5646 if (isl_set_is_params(set))
5647 return set;
5649 n = isl_set_dim(set, isl_dim_set);
5650 set = isl_set_project_out(set, isl_dim_set, 0, n);
5651 space = isl_set_get_space(set);
5652 space = isl_space_params(space);
5653 set = isl_set_reset_space(set, space);
5654 return set;
5657 /* Construct a zero-dimensional set with the given parameter domain.
5659 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5661 isl_space *space;
5662 space = isl_set_get_space(set);
5663 space = isl_space_set_from_params(space);
5664 set = isl_set_reset_space(set, space);
5665 return set;
5668 /* Compute the parameter domain of the given map.
5670 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5672 isl_space *space;
5673 unsigned n;
5675 n = isl_map_dim(map, isl_dim_in);
5676 map = isl_map_project_out(map, isl_dim_in, 0, n);
5677 n = isl_map_dim(map, isl_dim_out);
5678 map = isl_map_project_out(map, isl_dim_out, 0, n);
5679 space = isl_map_get_space(map);
5680 space = isl_space_params(space);
5681 map = isl_map_reset_space(map, space);
5682 return map;
5685 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5687 isl_space *space;
5688 unsigned n_out;
5690 if (!bmap)
5691 return NULL;
5692 space = isl_space_domain(isl_basic_map_get_space(bmap));
5694 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5695 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5697 return isl_basic_map_reset_space(bmap, space);
5700 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5702 if (!bmap)
5703 return isl_bool_error;
5704 return isl_space_may_be_set(bmap->dim);
5707 /* Is this basic map actually a set?
5708 * Users should never call this function. Outside of isl,
5709 * the type should indicate whether something is a set or a map.
5711 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5713 if (!bmap)
5714 return isl_bool_error;
5715 return isl_space_is_set(bmap->dim);
5718 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5720 isl_bool is_set;
5722 is_set = isl_basic_map_is_set(bmap);
5723 if (is_set < 0)
5724 goto error;
5725 if (is_set)
5726 return bmap;
5727 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5728 error:
5729 isl_basic_map_free(bmap);
5730 return NULL;
5733 __isl_give isl_basic_map *isl_basic_map_domain_map(
5734 __isl_take isl_basic_map *bmap)
5736 int i;
5737 isl_space *space;
5738 isl_basic_map *domain;
5739 int nparam, n_in, n_out;
5741 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5742 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5743 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5745 space = isl_basic_map_get_space(bmap);
5746 space = isl_space_from_range(isl_space_domain(space));
5747 domain = isl_basic_map_universe(space);
5749 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5750 bmap = isl_basic_map_apply_range(bmap, domain);
5751 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5753 for (i = 0; i < n_in; ++i)
5754 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5755 isl_dim_out, i);
5757 bmap = isl_basic_map_gauss(bmap, NULL);
5758 return isl_basic_map_finalize(bmap);
5761 __isl_give isl_basic_map *isl_basic_map_range_map(
5762 __isl_take isl_basic_map *bmap)
5764 int i;
5765 isl_space *space;
5766 isl_basic_map *range;
5767 int nparam, n_in, n_out;
5769 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5770 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5771 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5773 space = isl_basic_map_get_space(bmap);
5774 space = isl_space_from_range(isl_space_range(space));
5775 range = isl_basic_map_universe(space);
5777 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5778 bmap = isl_basic_map_apply_range(bmap, range);
5779 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5781 for (i = 0; i < n_out; ++i)
5782 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5783 isl_dim_out, i);
5785 bmap = isl_basic_map_gauss(bmap, NULL);
5786 return isl_basic_map_finalize(bmap);
5789 int isl_map_may_be_set(__isl_keep isl_map *map)
5791 if (!map)
5792 return -1;
5793 return isl_space_may_be_set(map->dim);
5796 /* Is this map actually a set?
5797 * Users should never call this function. Outside of isl,
5798 * the type should indicate whether something is a set or a map.
5800 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5802 if (!map)
5803 return isl_bool_error;
5804 return isl_space_is_set(map->dim);
5807 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5809 int i;
5810 isl_bool is_set;
5811 struct isl_set *set;
5813 is_set = isl_map_is_set(map);
5814 if (is_set < 0)
5815 goto error;
5816 if (is_set)
5817 return set_from_map(map);
5819 map = isl_map_cow(map);
5820 if (!map)
5821 goto error;
5823 set = set_from_map(map);
5824 set->dim = isl_space_range(set->dim);
5825 if (!set->dim)
5826 goto error;
5827 for (i = 0; i < map->n; ++i) {
5828 set->p[i] = isl_basic_map_range(map->p[i]);
5829 if (!set->p[i])
5830 goto error;
5832 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5833 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5834 return set;
5835 error:
5836 isl_map_free(map);
5837 return NULL;
5840 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5842 int i;
5844 map = isl_map_cow(map);
5845 if (!map)
5846 return NULL;
5848 map->dim = isl_space_domain_map(map->dim);
5849 if (!map->dim)
5850 goto error;
5851 for (i = 0; i < map->n; ++i) {
5852 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5853 if (!map->p[i])
5854 goto error;
5856 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5857 map = isl_map_unmark_normalized(map);
5858 return map;
5859 error:
5860 isl_map_free(map);
5861 return NULL;
5864 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5866 int i;
5867 isl_space *range_dim;
5869 map = isl_map_cow(map);
5870 if (!map)
5871 return NULL;
5873 range_dim = isl_space_range(isl_map_get_space(map));
5874 range_dim = isl_space_from_range(range_dim);
5875 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5876 map->dim = isl_space_join(map->dim, range_dim);
5877 if (!map->dim)
5878 goto error;
5879 for (i = 0; i < map->n; ++i) {
5880 map->p[i] = isl_basic_map_range_map(map->p[i]);
5881 if (!map->p[i])
5882 goto error;
5884 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5885 map = isl_map_unmark_normalized(map);
5886 return map;
5887 error:
5888 isl_map_free(map);
5889 return NULL;
5892 /* Given a wrapped map of the form A[B -> C],
5893 * return the map A[B -> C] -> B.
5895 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5897 isl_id *id;
5898 isl_map *map;
5900 if (!set)
5901 return NULL;
5902 if (!isl_set_has_tuple_id(set))
5903 return isl_map_domain_map(isl_set_unwrap(set));
5905 id = isl_set_get_tuple_id(set);
5906 map = isl_map_domain_map(isl_set_unwrap(set));
5907 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5909 return map;
5912 __isl_give isl_basic_map *isl_basic_map_from_domain(
5913 __isl_take isl_basic_set *bset)
5915 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5918 __isl_give isl_basic_map *isl_basic_map_from_range(
5919 __isl_take isl_basic_set *bset)
5921 isl_space *space;
5922 space = isl_basic_set_get_space(bset);
5923 space = isl_space_from_range(space);
5924 bset = isl_basic_set_reset_space(bset, space);
5925 return bset_to_bmap(bset);
5928 /* Create a relation with the given set as range.
5929 * The domain of the created relation is a zero-dimensional
5930 * flat anonymous space.
5932 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5934 isl_space *space;
5935 space = isl_set_get_space(set);
5936 space = isl_space_from_range(space);
5937 set = isl_set_reset_space(set, space);
5938 return set_to_map(set);
5941 /* Create a relation with the given set as domain.
5942 * The range of the created relation is a zero-dimensional
5943 * flat anonymous space.
5945 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5947 return isl_map_reverse(isl_map_from_range(set));
5950 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5951 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5953 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5956 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5957 __isl_take isl_set *range)
5959 return isl_map_apply_range(isl_map_reverse(domain), range);
5962 /* Return a newly allocated isl_map with given space and flags and
5963 * room for "n" basic maps.
5964 * Make sure that all cached information is cleared.
5966 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5967 unsigned flags)
5969 struct isl_map *map;
5971 if (!space)
5972 return NULL;
5973 if (n < 0)
5974 isl_die(space->ctx, isl_error_internal,
5975 "negative number of basic maps", goto error);
5976 map = isl_calloc(space->ctx, struct isl_map,
5977 sizeof(struct isl_map) +
5978 (n - 1) * sizeof(struct isl_basic_map *));
5979 if (!map)
5980 goto error;
5982 map->ctx = space->ctx;
5983 isl_ctx_ref(map->ctx);
5984 map->ref = 1;
5985 map->size = n;
5986 map->n = 0;
5987 map->dim = space;
5988 map->flags = flags;
5989 return map;
5990 error:
5991 isl_space_free(space);
5992 return NULL;
5995 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5997 struct isl_basic_map *bmap;
5998 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5999 bmap = isl_basic_map_set_to_empty(bmap);
6000 return bmap;
6003 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6005 struct isl_basic_set *bset;
6006 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6007 bset = isl_basic_set_set_to_empty(bset);
6008 return bset;
6011 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6013 struct isl_basic_map *bmap;
6014 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6015 bmap = isl_basic_map_finalize(bmap);
6016 return bmap;
6019 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6021 struct isl_basic_set *bset;
6022 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6023 bset = isl_basic_set_finalize(bset);
6024 return bset;
6027 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6028 __isl_take isl_space *space)
6030 int i;
6031 unsigned total = isl_space_dim(space, isl_dim_all);
6032 isl_basic_map *bmap;
6034 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6035 for (i = 0; i < total; ++i) {
6036 int k = isl_basic_map_alloc_inequality(bmap);
6037 if (k < 0)
6038 goto error;
6039 isl_seq_clr(bmap->ineq[k], 1 + total);
6040 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6042 return bmap;
6043 error:
6044 isl_basic_map_free(bmap);
6045 return NULL;
6048 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6049 __isl_take isl_space *space)
6051 return isl_basic_map_nat_universe(space);
6054 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6056 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6059 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6061 return isl_map_nat_universe(dim);
6064 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6066 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6069 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6071 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6074 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6076 struct isl_map *map;
6077 if (!space)
6078 return NULL;
6079 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6080 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6081 return map;
6084 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6086 struct isl_set *set;
6087 if (!space)
6088 return NULL;
6089 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6090 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6091 return set;
6094 struct isl_map *isl_map_dup(struct isl_map *map)
6096 int i;
6097 struct isl_map *dup;
6099 if (!map)
6100 return NULL;
6101 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6102 for (i = 0; i < map->n; ++i)
6103 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6104 return dup;
6107 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6108 __isl_take isl_basic_map *bmap)
6110 if (!bmap || !map)
6111 goto error;
6112 if (isl_basic_map_plain_is_empty(bmap)) {
6113 isl_basic_map_free(bmap);
6114 return map;
6116 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6117 isl_assert(map->ctx, map->n < map->size, goto error);
6118 map->p[map->n] = bmap;
6119 map->n++;
6120 map = isl_map_unmark_normalized(map);
6121 return map;
6122 error:
6123 if (map)
6124 isl_map_free(map);
6125 if (bmap)
6126 isl_basic_map_free(bmap);
6127 return NULL;
6130 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6132 int i;
6134 if (!map)
6135 return NULL;
6137 if (--map->ref > 0)
6138 return NULL;
6140 clear_caches(map);
6141 isl_ctx_deref(map->ctx);
6142 for (i = 0; i < map->n; ++i)
6143 isl_basic_map_free(map->p[i]);
6144 isl_space_free(map->dim);
6145 free(map);
6147 return NULL;
6150 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6151 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6153 int j;
6155 bmap = isl_basic_map_cow(bmap);
6156 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6157 j = isl_basic_map_alloc_equality(bmap);
6158 if (j < 0)
6159 goto error;
6160 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6161 isl_int_set_si(bmap->eq[j][pos], -1);
6162 isl_int_set_si(bmap->eq[j][0], value);
6163 bmap = isl_basic_map_simplify(bmap);
6164 return isl_basic_map_finalize(bmap);
6165 error:
6166 isl_basic_map_free(bmap);
6167 return NULL;
6170 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6171 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6173 int j;
6175 bmap = isl_basic_map_cow(bmap);
6176 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6177 j = isl_basic_map_alloc_equality(bmap);
6178 if (j < 0)
6179 goto error;
6180 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6181 isl_int_set_si(bmap->eq[j][pos], -1);
6182 isl_int_set(bmap->eq[j][0], value);
6183 bmap = isl_basic_map_simplify(bmap);
6184 return isl_basic_map_finalize(bmap);
6185 error:
6186 isl_basic_map_free(bmap);
6187 return NULL;
6190 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6191 enum isl_dim_type type, unsigned pos, int value)
6193 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6194 return isl_basic_map_free(bmap);
6195 return isl_basic_map_fix_pos_si(bmap,
6196 isl_basic_map_offset(bmap, type) + pos, value);
6199 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6200 enum isl_dim_type type, unsigned pos, isl_int value)
6202 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6203 return isl_basic_map_free(bmap);
6204 return isl_basic_map_fix_pos(bmap,
6205 isl_basic_map_offset(bmap, type) + pos, value);
6208 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6209 * to be equal to "v".
6211 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6212 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6214 if (!bmap || !v)
6215 goto error;
6216 if (!isl_val_is_int(v))
6217 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6218 "expecting integer value", goto error);
6219 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6220 goto error;
6221 pos += isl_basic_map_offset(bmap, type);
6222 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6223 isl_val_free(v);
6224 return bmap;
6225 error:
6226 isl_basic_map_free(bmap);
6227 isl_val_free(v);
6228 return NULL;
6231 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6232 * to be equal to "v".
6234 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6235 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6237 return isl_basic_map_fix_val(bset, type, pos, v);
6240 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6241 enum isl_dim_type type, unsigned pos, int value)
6243 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6244 type, pos, value));
6247 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6248 enum isl_dim_type type, unsigned pos, isl_int value)
6250 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6251 type, pos, value));
6254 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6255 unsigned input, int value)
6257 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6260 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6261 unsigned dim, int value)
6263 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6264 isl_dim_set, dim, value));
6267 /* Remove the basic map at position "i" from "map" if this basic map
6268 * is (obviously) empty.
6270 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6272 isl_bool empty;
6274 if (!map)
6275 return NULL;
6277 empty = isl_basic_map_plain_is_empty(map->p[i]);
6278 if (empty < 0)
6279 return isl_map_free(map);
6280 if (!empty)
6281 return map;
6283 isl_basic_map_free(map->p[i]);
6284 map->n--;
6285 if (i != map->n) {
6286 map->p[i] = map->p[map->n];
6287 map = isl_map_unmark_normalized(map);
6291 return map;
6294 /* Perform "fn" on each basic map of "map", where we may not be holding
6295 * the only reference to "map".
6296 * In particular, "fn" should be a semantics preserving operation
6297 * that we want to apply to all copies of "map". We therefore need
6298 * to be careful not to modify "map" in a way that breaks "map"
6299 * in case anything goes wrong.
6301 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6302 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6304 struct isl_basic_map *bmap;
6305 int i;
6307 if (!map)
6308 return NULL;
6310 for (i = map->n - 1; i >= 0; --i) {
6311 bmap = isl_basic_map_copy(map->p[i]);
6312 bmap = fn(bmap);
6313 if (!bmap)
6314 goto error;
6315 isl_basic_map_free(map->p[i]);
6316 map->p[i] = bmap;
6317 map = remove_if_empty(map, i);
6318 if (!map)
6319 return NULL;
6322 return map;
6323 error:
6324 isl_map_free(map);
6325 return NULL;
6328 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6329 enum isl_dim_type type, unsigned pos, int value)
6331 int i;
6333 map = isl_map_cow(map);
6334 if (isl_map_check_range(map, type, pos, 1) < 0)
6335 return isl_map_free(map);
6336 for (i = map->n - 1; i >= 0; --i) {
6337 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6338 map = remove_if_empty(map, i);
6339 if (!map)
6340 return NULL;
6342 map = isl_map_unmark_normalized(map);
6343 return map;
6346 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6347 enum isl_dim_type type, unsigned pos, int value)
6349 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6352 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6353 enum isl_dim_type type, unsigned pos, isl_int value)
6355 int i;
6357 map = isl_map_cow(map);
6358 if (isl_map_check_range(map, type, pos, 1) < 0)
6359 return isl_map_free(map);
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 (isl_map_check_range(map, type, pos, 1) < 0)
6394 goto error;
6395 for (i = map->n - 1; i >= 0; --i) {
6396 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6397 isl_val_copy(v));
6398 map = remove_if_empty(map, i);
6399 if (!map)
6400 goto error;
6402 map = isl_map_unmark_normalized(map);
6403 isl_val_free(v);
6404 return map;
6405 error:
6406 isl_map_free(map);
6407 isl_val_free(v);
6408 return NULL;
6411 /* Fix the value of the variable at position "pos" of type "type" of "set"
6412 * to be equal to "v".
6414 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6415 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6417 return isl_map_fix_val(set, type, pos, v);
6420 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6421 unsigned input, int value)
6423 return isl_map_fix_si(map, isl_dim_in, input, value);
6426 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6428 return set_from_map(isl_map_fix_si(set_to_map(set),
6429 isl_dim_set, dim, value));
6432 static __isl_give isl_basic_map *basic_map_bound_si(
6433 __isl_take isl_basic_map *bmap,
6434 enum isl_dim_type type, unsigned pos, int value, int upper)
6436 int j;
6438 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6439 return isl_basic_map_free(bmap);
6440 pos += isl_basic_map_offset(bmap, type);
6441 bmap = isl_basic_map_cow(bmap);
6442 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6443 j = isl_basic_map_alloc_inequality(bmap);
6444 if (j < 0)
6445 goto error;
6446 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6447 if (upper) {
6448 isl_int_set_si(bmap->ineq[j][pos], -1);
6449 isl_int_set_si(bmap->ineq[j][0], value);
6450 } else {
6451 isl_int_set_si(bmap->ineq[j][pos], 1);
6452 isl_int_set_si(bmap->ineq[j][0], -value);
6454 bmap = isl_basic_map_simplify(bmap);
6455 return isl_basic_map_finalize(bmap);
6456 error:
6457 isl_basic_map_free(bmap);
6458 return NULL;
6461 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6462 __isl_take isl_basic_map *bmap,
6463 enum isl_dim_type type, unsigned pos, int value)
6465 return basic_map_bound_si(bmap, type, pos, value, 0);
6468 /* Constrain the values of the given dimension to be no greater than "value".
6470 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6471 __isl_take isl_basic_map *bmap,
6472 enum isl_dim_type type, unsigned pos, int value)
6474 return basic_map_bound_si(bmap, type, pos, value, 1);
6477 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6478 enum isl_dim_type type, unsigned pos, int value, int upper)
6480 int i;
6482 map = isl_map_cow(map);
6483 if (isl_map_check_range(map, type, pos, 1) < 0)
6484 return isl_map_free(map);
6485 for (i = 0; i < map->n; ++i) {
6486 map->p[i] = basic_map_bound_si(map->p[i],
6487 type, pos, value, upper);
6488 if (!map->p[i])
6489 goto error;
6491 map = isl_map_unmark_normalized(map);
6492 return map;
6493 error:
6494 isl_map_free(map);
6495 return NULL;
6498 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6499 enum isl_dim_type type, unsigned pos, int value)
6501 return map_bound_si(map, type, pos, value, 0);
6504 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6505 enum isl_dim_type type, unsigned pos, int value)
6507 return map_bound_si(map, type, pos, value, 1);
6510 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6511 enum isl_dim_type type, unsigned pos, int value)
6513 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6514 type, pos, value));
6517 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6518 enum isl_dim_type type, unsigned pos, int value)
6520 return isl_map_upper_bound_si(set, type, pos, value);
6523 /* Bound the given variable of "bmap" from below (or above is "upper"
6524 * is set) to "value".
6526 static __isl_give isl_basic_map *basic_map_bound(
6527 __isl_take isl_basic_map *bmap,
6528 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6530 int j;
6532 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6533 return isl_basic_map_free(bmap);
6534 pos += isl_basic_map_offset(bmap, type);
6535 bmap = isl_basic_map_cow(bmap);
6536 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6537 j = isl_basic_map_alloc_inequality(bmap);
6538 if (j < 0)
6539 goto error;
6540 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6541 if (upper) {
6542 isl_int_set_si(bmap->ineq[j][pos], -1);
6543 isl_int_set(bmap->ineq[j][0], value);
6544 } else {
6545 isl_int_set_si(bmap->ineq[j][pos], 1);
6546 isl_int_neg(bmap->ineq[j][0], value);
6548 bmap = isl_basic_map_simplify(bmap);
6549 return isl_basic_map_finalize(bmap);
6550 error:
6551 isl_basic_map_free(bmap);
6552 return NULL;
6555 /* Bound the given variable of "map" from below (or above is "upper"
6556 * is set) to "value".
6558 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6559 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6561 int i;
6563 map = isl_map_cow(map);
6564 if (isl_map_check_range(map, type, pos, 1) < 0)
6565 return isl_map_free(map);
6566 for (i = map->n - 1; i >= 0; --i) {
6567 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6568 map = remove_if_empty(map, i);
6569 if (!map)
6570 return NULL;
6572 map = isl_map_unmark_normalized(map);
6573 return map;
6576 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6577 enum isl_dim_type type, unsigned pos, isl_int value)
6579 return map_bound(map, type, pos, value, 0);
6582 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6583 enum isl_dim_type type, unsigned pos, isl_int value)
6585 return map_bound(map, type, pos, value, 1);
6588 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6589 enum isl_dim_type type, unsigned pos, isl_int value)
6591 return isl_map_lower_bound(set, type, pos, value);
6594 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6595 enum isl_dim_type type, unsigned pos, isl_int value)
6597 return isl_map_upper_bound(set, type, pos, value);
6600 /* Force the values of the variable at position "pos" of type "type" of "set"
6601 * to be no smaller than "value".
6603 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6604 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6606 if (!value)
6607 goto error;
6608 if (!isl_val_is_int(value))
6609 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6610 "expecting integer value", goto error);
6611 set = isl_set_lower_bound(set, type, pos, value->n);
6612 isl_val_free(value);
6613 return set;
6614 error:
6615 isl_val_free(value);
6616 isl_set_free(set);
6617 return NULL;
6620 /* Force the values of the variable at position "pos" of type "type" of "set"
6621 * to be no greater than "value".
6623 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6624 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6626 if (!value)
6627 goto error;
6628 if (!isl_val_is_int(value))
6629 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6630 "expecting integer value", goto error);
6631 set = isl_set_upper_bound(set, type, pos, value->n);
6632 isl_val_free(value);
6633 return set;
6634 error:
6635 isl_val_free(value);
6636 isl_set_free(set);
6637 return NULL;
6640 /* Bound the given variable of "bset" from below (or above is "upper"
6641 * is set) to "value".
6643 static __isl_give isl_basic_set *isl_basic_set_bound(
6644 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6645 isl_int value, int upper)
6647 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6648 type, pos, value, upper));
6651 /* Bound the given variable of "bset" from below (or above is "upper"
6652 * is set) to "value".
6654 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6655 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6656 __isl_take isl_val *value, int upper)
6658 if (!value)
6659 goto error;
6660 if (!isl_val_is_int(value))
6661 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6662 "expecting integer value", goto error);
6663 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6664 isl_val_free(value);
6665 return bset;
6666 error:
6667 isl_val_free(value);
6668 isl_basic_set_free(bset);
6669 return NULL;
6672 /* Bound the given variable of "bset" from below to "value".
6674 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6675 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6676 __isl_take isl_val *value)
6678 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6681 /* Bound the given variable of "bset" from above to "value".
6683 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6684 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6685 __isl_take isl_val *value)
6687 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6690 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6692 int i;
6694 map = isl_map_cow(map);
6695 if (!map)
6696 return NULL;
6698 map->dim = isl_space_reverse(map->dim);
6699 if (!map->dim)
6700 goto error;
6701 for (i = 0; i < map->n; ++i) {
6702 map->p[i] = isl_basic_map_reverse(map->p[i]);
6703 if (!map->p[i])
6704 goto error;
6706 map = isl_map_unmark_normalized(map);
6707 return map;
6708 error:
6709 isl_map_free(map);
6710 return NULL;
6713 #undef TYPE
6714 #define TYPE isl_pw_multi_aff
6715 #undef SUFFIX
6716 #define SUFFIX _pw_multi_aff
6717 #undef EMPTY
6718 #define EMPTY isl_pw_multi_aff_empty
6719 #undef ADD
6720 #define ADD isl_pw_multi_aff_union_add
6721 #include "isl_map_lexopt_templ.c"
6723 /* Given a map "map", compute the lexicographically minimal
6724 * (or maximal) image element for each domain element in dom,
6725 * in the form of an isl_pw_multi_aff.
6726 * If "empty" is not NULL, then set *empty to those elements in dom that
6727 * do not have an image element.
6728 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6729 * should be computed over the domain of "map". "empty" is also NULL
6730 * in this case.
6732 * We first compute the lexicographically minimal or maximal element
6733 * in the first basic map. This results in a partial solution "res"
6734 * and a subset "todo" of dom that still need to be handled.
6735 * We then consider each of the remaining maps in "map" and successively
6736 * update both "res" and "todo".
6737 * If "empty" is NULL, then the todo sets are not needed and therefore
6738 * also not computed.
6740 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6741 __isl_take isl_map *map, __isl_take isl_set *dom,
6742 __isl_give isl_set **empty, unsigned flags)
6744 int i;
6745 int full;
6746 isl_pw_multi_aff *res;
6747 isl_set *todo;
6749 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6750 if (!map || (!full && !dom))
6751 goto error;
6753 if (isl_map_plain_is_empty(map)) {
6754 if (empty)
6755 *empty = dom;
6756 else
6757 isl_set_free(dom);
6758 return isl_pw_multi_aff_from_map(map);
6761 res = basic_map_partial_lexopt_pw_multi_aff(
6762 isl_basic_map_copy(map->p[0]),
6763 isl_set_copy(dom), empty, flags);
6765 if (empty)
6766 todo = *empty;
6767 for (i = 1; i < map->n; ++i) {
6768 isl_pw_multi_aff *res_i;
6770 res_i = basic_map_partial_lexopt_pw_multi_aff(
6771 isl_basic_map_copy(map->p[i]),
6772 isl_set_copy(dom), empty, flags);
6774 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6775 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6776 else
6777 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6779 if (empty)
6780 todo = isl_set_intersect(todo, *empty);
6783 isl_set_free(dom);
6784 isl_map_free(map);
6786 if (empty)
6787 *empty = todo;
6789 return res;
6790 error:
6791 if (empty)
6792 *empty = NULL;
6793 isl_set_free(dom);
6794 isl_map_free(map);
6795 return NULL;
6798 #undef TYPE
6799 #define TYPE isl_map
6800 #undef SUFFIX
6801 #define SUFFIX
6802 #undef EMPTY
6803 #define EMPTY isl_map_empty
6804 #undef ADD
6805 #define ADD isl_map_union_disjoint
6806 #include "isl_map_lexopt_templ.c"
6808 /* Given a map "map", compute the lexicographically minimal
6809 * (or maximal) image element for each domain element in "dom",
6810 * in the form of an isl_map.
6811 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6812 * do not have an image element.
6813 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6814 * should be computed over the domain of "map". "empty" is also NULL
6815 * in this case.
6817 * If the input consists of more than one disjunct, then first
6818 * compute the desired result in the form of an isl_pw_multi_aff and
6819 * then convert that into an isl_map.
6821 * This function used to have an explicit implementation in terms
6822 * of isl_maps, but it would continually intersect the domains of
6823 * partial results with the complement of the domain of the next
6824 * partial solution, potentially leading to an explosion in the number
6825 * of disjuncts if there are several disjuncts in the input.
6826 * An even earlier implementation of this function would look for
6827 * better results in the domain of the partial result and for extra
6828 * results in the complement of this domain, which would lead to
6829 * even more splintering.
6831 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6832 __isl_take isl_map *map, __isl_take isl_set *dom,
6833 __isl_give isl_set **empty, unsigned flags)
6835 int full;
6836 struct isl_map *res;
6837 isl_pw_multi_aff *pma;
6839 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6840 if (!map || (!full && !dom))
6841 goto error;
6843 if (isl_map_plain_is_empty(map)) {
6844 if (empty)
6845 *empty = dom;
6846 else
6847 isl_set_free(dom);
6848 return map;
6851 if (map->n == 1) {
6852 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6853 dom, empty, flags);
6854 isl_map_free(map);
6855 return res;
6858 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6859 flags);
6860 return isl_map_from_pw_multi_aff(pma);
6861 error:
6862 if (empty)
6863 *empty = NULL;
6864 isl_set_free(dom);
6865 isl_map_free(map);
6866 return NULL;
6869 __isl_give isl_map *isl_map_partial_lexmax(
6870 __isl_take isl_map *map, __isl_take isl_set *dom,
6871 __isl_give isl_set **empty)
6873 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6876 __isl_give isl_map *isl_map_partial_lexmin(
6877 __isl_take isl_map *map, __isl_take isl_set *dom,
6878 __isl_give isl_set **empty)
6880 return isl_map_partial_lexopt(map, dom, empty, 0);
6883 __isl_give isl_set *isl_set_partial_lexmin(
6884 __isl_take isl_set *set, __isl_take isl_set *dom,
6885 __isl_give isl_set **empty)
6887 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6888 dom, empty));
6891 __isl_give isl_set *isl_set_partial_lexmax(
6892 __isl_take isl_set *set, __isl_take isl_set *dom,
6893 __isl_give isl_set **empty)
6895 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6896 dom, empty));
6899 /* Compute the lexicographic minimum (or maximum if "flags" includes
6900 * ISL_OPT_MAX) of "bset" over its parametric domain.
6902 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6903 unsigned flags)
6905 return isl_basic_map_lexopt(bset, flags);
6908 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6910 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6913 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6915 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6918 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6920 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6923 /* Compute the lexicographic minimum of "bset" over its parametric domain
6924 * for the purpose of quantifier elimination.
6925 * That is, find an explicit representation for all the existentially
6926 * quantified variables in "bset" by computing their lexicographic
6927 * minimum.
6929 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6930 __isl_take isl_basic_set *bset)
6932 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6935 /* Given a basic map with one output dimension, compute the minimum or
6936 * maximum of that dimension as an isl_pw_aff.
6938 * Compute the optimum as a lexicographic optimum over the single
6939 * output dimension and extract the single isl_pw_aff from the result.
6941 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6942 int max)
6944 isl_pw_multi_aff *pma;
6945 isl_pw_aff *pwaff;
6947 bmap = isl_basic_map_copy(bmap);
6948 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6949 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6950 isl_pw_multi_aff_free(pma);
6952 return pwaff;
6955 /* Compute the minimum or maximum of the given output dimension
6956 * as a function of the parameters and the input dimensions,
6957 * but independently of the other output dimensions.
6959 * We first project out the other output dimension and then compute
6960 * the "lexicographic" maximum in each basic map, combining the results
6961 * using isl_pw_aff_union_max.
6963 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6964 int max)
6966 int i;
6967 isl_pw_aff *pwaff;
6968 unsigned n_out;
6970 n_out = isl_map_dim(map, isl_dim_out);
6971 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6972 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6973 if (!map)
6974 return NULL;
6976 if (map->n == 0) {
6977 isl_space *space = isl_map_get_space(map);
6978 isl_map_free(map);
6979 return isl_pw_aff_empty(space);
6982 pwaff = basic_map_dim_opt(map->p[0], max);
6983 for (i = 1; i < map->n; ++i) {
6984 isl_pw_aff *pwaff_i;
6986 pwaff_i = basic_map_dim_opt(map->p[i], max);
6987 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6990 isl_map_free(map);
6992 return pwaff;
6995 /* Compute the minimum of the given output dimension as a function of the
6996 * parameters and input dimensions, but independently of
6997 * the other output dimensions.
6999 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7001 return map_dim_opt(map, pos, 0);
7004 /* Compute the maximum of the given output dimension as a function of the
7005 * parameters and input dimensions, but independently of
7006 * the other output dimensions.
7008 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7010 return map_dim_opt(map, pos, 1);
7013 /* Compute the minimum or maximum of the given set dimension
7014 * as a function of the parameters,
7015 * but independently of the other set dimensions.
7017 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7018 int max)
7020 return map_dim_opt(set, pos, max);
7023 /* Compute the maximum of the given set dimension as a function of the
7024 * parameters, but independently of the other set dimensions.
7026 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7028 return set_dim_opt(set, pos, 1);
7031 /* Compute the minimum of the given set dimension as a function of the
7032 * parameters, but independently of the other set dimensions.
7034 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7036 return set_dim_opt(set, pos, 0);
7039 /* Apply a preimage specified by "mat" on the parameters of "bset".
7040 * bset is assumed to have only parameters and divs.
7042 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7043 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7045 unsigned nparam;
7047 if (!bset || !mat)
7048 goto error;
7050 bset->dim = isl_space_cow(bset->dim);
7051 if (!bset->dim)
7052 goto error;
7054 nparam = isl_basic_set_dim(bset, isl_dim_param);
7056 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7058 bset->dim->nparam = 0;
7059 bset->dim->n_out = nparam;
7060 bset = isl_basic_set_preimage(bset, mat);
7061 if (bset) {
7062 bset->dim->nparam = bset->dim->n_out;
7063 bset->dim->n_out = 0;
7065 return bset;
7066 error:
7067 isl_mat_free(mat);
7068 isl_basic_set_free(bset);
7069 return NULL;
7072 /* Apply a preimage specified by "mat" on the parameters of "set".
7073 * set is assumed to have only parameters and divs.
7075 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7076 __isl_take isl_mat *mat)
7078 isl_space *space;
7079 unsigned nparam;
7081 if (!set || !mat)
7082 goto error;
7084 nparam = isl_set_dim(set, isl_dim_param);
7086 if (mat->n_row != 1 + nparam)
7087 isl_die(isl_set_get_ctx(set), isl_error_internal,
7088 "unexpected number of rows", goto error);
7090 space = isl_set_get_space(set);
7091 space = isl_space_move_dims(space, isl_dim_set, 0,
7092 isl_dim_param, 0, nparam);
7093 set = isl_set_reset_space(set, space);
7094 set = isl_set_preimage(set, mat);
7095 nparam = isl_set_dim(set, isl_dim_out);
7096 space = isl_set_get_space(set);
7097 space = isl_space_move_dims(space, isl_dim_param, 0,
7098 isl_dim_out, 0, nparam);
7099 set = isl_set_reset_space(set, space);
7100 return set;
7101 error:
7102 isl_mat_free(mat);
7103 isl_set_free(set);
7104 return NULL;
7107 /* Intersect the basic set "bset" with the affine space specified by the
7108 * equalities in "eq".
7110 static __isl_give isl_basic_set *basic_set_append_equalities(
7111 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7113 int i, k;
7114 unsigned len;
7116 if (!bset || !eq)
7117 goto error;
7119 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7120 eq->n_row, 0);
7121 if (!bset)
7122 goto error;
7124 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7125 for (i = 0; i < eq->n_row; ++i) {
7126 k = isl_basic_set_alloc_equality(bset);
7127 if (k < 0)
7128 goto error;
7129 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7130 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7132 isl_mat_free(eq);
7134 bset = isl_basic_set_gauss(bset, NULL);
7135 bset = isl_basic_set_finalize(bset);
7137 return bset;
7138 error:
7139 isl_mat_free(eq);
7140 isl_basic_set_free(bset);
7141 return NULL;
7144 /* Intersect the set "set" with the affine space specified by the
7145 * equalities in "eq".
7147 static struct isl_set *set_append_equalities(struct isl_set *set,
7148 struct isl_mat *eq)
7150 int i;
7152 if (!set || !eq)
7153 goto error;
7155 for (i = 0; i < set->n; ++i) {
7156 set->p[i] = basic_set_append_equalities(set->p[i],
7157 isl_mat_copy(eq));
7158 if (!set->p[i])
7159 goto error;
7161 isl_mat_free(eq);
7162 return set;
7163 error:
7164 isl_mat_free(eq);
7165 isl_set_free(set);
7166 return NULL;
7169 /* Given a basic set "bset" that only involves parameters and existentially
7170 * quantified variables, return the index of the first equality
7171 * that only involves parameters. If there is no such equality then
7172 * return bset->n_eq.
7174 * This function assumes that isl_basic_set_gauss has been called on "bset".
7176 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7178 int i, j;
7179 unsigned nparam, n_div;
7181 if (!bset)
7182 return -1;
7184 nparam = isl_basic_set_dim(bset, isl_dim_param);
7185 n_div = isl_basic_set_dim(bset, isl_dim_div);
7187 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7188 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7189 ++i;
7192 return i;
7195 /* Compute an explicit representation for the existentially quantified
7196 * variables in "bset" by computing the "minimal value" of the set
7197 * variables. Since there are no set variables, the computation of
7198 * the minimal value essentially computes an explicit representation
7199 * of the non-empty part(s) of "bset".
7201 * The input only involves parameters and existentially quantified variables.
7202 * All equalities among parameters have been removed.
7204 * Since the existentially quantified variables in the result are in general
7205 * going to be different from those in the input, we first replace
7206 * them by the minimal number of variables based on their equalities.
7207 * This should simplify the parametric integer programming.
7209 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7211 isl_morph *morph1, *morph2;
7212 isl_set *set;
7213 unsigned n;
7215 if (!bset)
7216 return NULL;
7217 if (bset->n_eq == 0)
7218 return isl_basic_set_lexmin_compute_divs(bset);
7220 morph1 = isl_basic_set_parameter_compression(bset);
7221 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7222 bset = isl_basic_set_lift(bset);
7223 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7224 bset = isl_morph_basic_set(morph2, bset);
7225 n = isl_basic_set_dim(bset, isl_dim_set);
7226 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7228 set = isl_basic_set_lexmin_compute_divs(bset);
7230 set = isl_morph_set(isl_morph_inverse(morph1), set);
7232 return set;
7235 /* Project the given basic set onto its parameter domain, possibly introducing
7236 * new, explicit, existential variables in the constraints.
7237 * The input has parameters and (possibly implicit) existential variables.
7238 * The output has the same parameters, but only
7239 * explicit existentially quantified variables.
7241 * The actual projection is performed by pip, but pip doesn't seem
7242 * to like equalities very much, so we first remove the equalities
7243 * among the parameters by performing a variable compression on
7244 * the parameters. Afterward, an inverse transformation is performed
7245 * and the equalities among the parameters are inserted back in.
7247 * The variable compression on the parameters may uncover additional
7248 * equalities that were only implicit before. We therefore check
7249 * if there are any new parameter equalities in the result and
7250 * if so recurse. The removal of parameter equalities is required
7251 * for the parameter compression performed by base_compute_divs.
7253 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7255 int i;
7256 struct isl_mat *eq;
7257 struct isl_mat *T, *T2;
7258 struct isl_set *set;
7259 unsigned nparam;
7261 bset = isl_basic_set_cow(bset);
7262 if (!bset)
7263 return NULL;
7265 if (bset->n_eq == 0)
7266 return base_compute_divs(bset);
7268 bset = isl_basic_set_gauss(bset, NULL);
7269 if (!bset)
7270 return NULL;
7271 if (isl_basic_set_plain_is_empty(bset))
7272 return isl_set_from_basic_set(bset);
7274 i = first_parameter_equality(bset);
7275 if (i == bset->n_eq)
7276 return base_compute_divs(bset);
7278 nparam = isl_basic_set_dim(bset, isl_dim_param);
7279 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7280 0, 1 + nparam);
7281 eq = isl_mat_cow(eq);
7282 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7283 if (T && T->n_col == 0) {
7284 isl_mat_free(T);
7285 isl_mat_free(T2);
7286 isl_mat_free(eq);
7287 bset = isl_basic_set_set_to_empty(bset);
7288 return isl_set_from_basic_set(bset);
7290 bset = basic_set_parameter_preimage(bset, T);
7292 i = first_parameter_equality(bset);
7293 if (!bset)
7294 set = NULL;
7295 else if (i == bset->n_eq)
7296 set = base_compute_divs(bset);
7297 else
7298 set = parameter_compute_divs(bset);
7299 set = set_parameter_preimage(set, T2);
7300 set = set_append_equalities(set, eq);
7301 return set;
7304 /* Insert the divs from "ls" before those of "bmap".
7306 * The number of columns is not changed, which means that the last
7307 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7308 * The caller is responsible for removing the same number of dimensions
7309 * from the space of "bmap".
7311 static __isl_give isl_basic_map *insert_divs_from_local_space(
7312 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7314 int i;
7315 int n_div;
7316 int old_n_div;
7318 n_div = isl_local_space_dim(ls, isl_dim_div);
7319 if (n_div == 0)
7320 return bmap;
7322 old_n_div = bmap->n_div;
7323 bmap = insert_div_rows(bmap, n_div);
7324 if (!bmap)
7325 return NULL;
7327 for (i = 0; i < n_div; ++i) {
7328 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7329 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7332 return bmap;
7335 /* Replace the space of "bmap" by the space and divs of "ls".
7337 * If "ls" has any divs, then we simplify the result since we may
7338 * have discovered some additional equalities that could simplify
7339 * the div expressions.
7341 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7342 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7344 int n_div;
7346 bmap = isl_basic_map_cow(bmap);
7347 if (!bmap || !ls)
7348 goto error;
7350 n_div = isl_local_space_dim(ls, isl_dim_div);
7351 bmap = insert_divs_from_local_space(bmap, ls);
7352 if (!bmap)
7353 goto error;
7355 isl_space_free(bmap->dim);
7356 bmap->dim = isl_local_space_get_space(ls);
7357 if (!bmap->dim)
7358 goto error;
7360 isl_local_space_free(ls);
7361 if (n_div > 0)
7362 bmap = isl_basic_map_simplify(bmap);
7363 bmap = isl_basic_map_finalize(bmap);
7364 return bmap;
7365 error:
7366 isl_basic_map_free(bmap);
7367 isl_local_space_free(ls);
7368 return NULL;
7371 /* Replace the space of "map" by the space and divs of "ls".
7373 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7374 __isl_take isl_local_space *ls)
7376 int i;
7378 map = isl_map_cow(map);
7379 if (!map || !ls)
7380 goto error;
7382 for (i = 0; i < map->n; ++i) {
7383 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7384 isl_local_space_copy(ls));
7385 if (!map->p[i])
7386 goto error;
7388 isl_space_free(map->dim);
7389 map->dim = isl_local_space_get_space(ls);
7390 if (!map->dim)
7391 goto error;
7393 isl_local_space_free(ls);
7394 return map;
7395 error:
7396 isl_local_space_free(ls);
7397 isl_map_free(map);
7398 return NULL;
7401 /* Compute an explicit representation for the existentially
7402 * quantified variables for which do not know any explicit representation yet.
7404 * We first sort the existentially quantified variables so that the
7405 * existentially quantified variables for which we already have an explicit
7406 * representation are placed before those for which we do not.
7407 * The input dimensions, the output dimensions and the existentially
7408 * quantified variables for which we already have an explicit
7409 * representation are then turned into parameters.
7410 * compute_divs returns a map with the same parameters and
7411 * no input or output dimensions and the dimension specification
7412 * is reset to that of the input, including the existentially quantified
7413 * variables for which we already had an explicit representation.
7415 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7417 struct isl_basic_set *bset;
7418 struct isl_set *set;
7419 struct isl_map *map;
7420 isl_space *space;
7421 isl_local_space *ls;
7422 unsigned nparam;
7423 unsigned n_in;
7424 unsigned n_out;
7425 int n_known;
7426 int i;
7428 bmap = isl_basic_map_sort_divs(bmap);
7429 bmap = isl_basic_map_cow(bmap);
7430 if (!bmap)
7431 return NULL;
7433 n_known = isl_basic_map_first_unknown_div(bmap);
7434 if (n_known < 0)
7435 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7437 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7438 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7439 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7440 space = isl_space_set_alloc(bmap->ctx,
7441 nparam + n_in + n_out + n_known, 0);
7442 if (!space)
7443 goto error;
7445 ls = isl_basic_map_get_local_space(bmap);
7446 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7447 n_known, bmap->n_div - n_known);
7448 if (n_known > 0) {
7449 for (i = n_known; i < bmap->n_div; ++i)
7450 swap_div(bmap, i - n_known, i);
7451 bmap->n_div -= n_known;
7452 bmap->extra -= n_known;
7454 bmap = isl_basic_map_reset_space(bmap, space);
7455 bset = bset_from_bmap(bmap);
7457 set = parameter_compute_divs(bset);
7458 map = set_to_map(set);
7459 map = replace_space_by_local_space(map, ls);
7461 return map;
7462 error:
7463 isl_basic_map_free(bmap);
7464 return NULL;
7467 /* Remove the explicit representation of local variable "div",
7468 * if there is any.
7470 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7471 __isl_take isl_basic_map *bmap, int div)
7473 isl_bool unknown;
7475 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7476 if (unknown < 0)
7477 return isl_basic_map_free(bmap);
7478 if (unknown)
7479 return bmap;
7481 bmap = isl_basic_map_cow(bmap);
7482 if (!bmap)
7483 return NULL;
7484 isl_int_set_si(bmap->div[div][0], 0);
7485 return bmap;
7488 /* Is local variable "div" of "bmap" marked as not having an explicit
7489 * representation?
7490 * Note that even if "div" is not marked in this way and therefore
7491 * has an explicit representation, this representation may still
7492 * depend (indirectly) on other local variables that do not
7493 * have an explicit representation.
7495 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7496 int div)
7498 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7499 return isl_bool_error;
7500 return isl_int_is_zero(bmap->div[div][0]);
7503 /* Return the position of the first local variable that does not
7504 * have an explicit representation.
7505 * Return the total number of local variables if they all have
7506 * an explicit representation.
7507 * Return -1 on error.
7509 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7511 int i;
7513 if (!bmap)
7514 return -1;
7516 for (i = 0; i < bmap->n_div; ++i) {
7517 if (!isl_basic_map_div_is_known(bmap, i))
7518 return i;
7520 return bmap->n_div;
7523 /* Return the position of the first local variable that does not
7524 * have an explicit representation.
7525 * Return the total number of local variables if they all have
7526 * an explicit representation.
7527 * Return -1 on error.
7529 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7531 return isl_basic_map_first_unknown_div(bset);
7534 /* Does "bmap" have an explicit representation for all local variables?
7536 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7538 int first, n;
7540 n = isl_basic_map_dim(bmap, isl_dim_div);
7541 first = isl_basic_map_first_unknown_div(bmap);
7542 if (first < 0)
7543 return isl_bool_error;
7544 return first == n;
7547 /* Do all basic maps in "map" have an explicit representation
7548 * for all local variables?
7550 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7552 int i;
7554 if (!map)
7555 return isl_bool_error;
7557 for (i = 0; i < map->n; ++i) {
7558 int known = isl_basic_map_divs_known(map->p[i]);
7559 if (known <= 0)
7560 return known;
7563 return isl_bool_true;
7566 /* If bmap contains any unknown divs, then compute explicit
7567 * expressions for them. However, this computation may be
7568 * quite expensive, so first try to remove divs that aren't
7569 * strictly needed.
7571 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7573 int known;
7574 struct isl_map *map;
7576 known = isl_basic_map_divs_known(bmap);
7577 if (known < 0)
7578 goto error;
7579 if (known)
7580 return isl_map_from_basic_map(bmap);
7582 bmap = isl_basic_map_drop_redundant_divs(bmap);
7584 known = isl_basic_map_divs_known(bmap);
7585 if (known < 0)
7586 goto error;
7587 if (known)
7588 return isl_map_from_basic_map(bmap);
7590 map = compute_divs(bmap);
7591 return map;
7592 error:
7593 isl_basic_map_free(bmap);
7594 return NULL;
7597 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7599 int i;
7600 int known;
7601 struct isl_map *res;
7603 if (!map)
7604 return NULL;
7605 if (map->n == 0)
7606 return map;
7608 known = isl_map_divs_known(map);
7609 if (known < 0) {
7610 isl_map_free(map);
7611 return NULL;
7613 if (known)
7614 return map;
7616 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7617 for (i = 1 ; i < map->n; ++i) {
7618 struct isl_map *r2;
7619 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7620 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7621 res = isl_map_union_disjoint(res, r2);
7622 else
7623 res = isl_map_union(res, r2);
7625 isl_map_free(map);
7627 return res;
7630 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7632 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7635 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7637 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7640 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7642 int i;
7643 struct isl_set *set;
7645 if (!map)
7646 goto error;
7648 map = isl_map_cow(map);
7649 if (!map)
7650 return NULL;
7652 set = set_from_map(map);
7653 set->dim = isl_space_domain(set->dim);
7654 if (!set->dim)
7655 goto error;
7656 for (i = 0; i < map->n; ++i) {
7657 set->p[i] = isl_basic_map_domain(map->p[i]);
7658 if (!set->p[i])
7659 goto error;
7661 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7662 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7663 return set;
7664 error:
7665 isl_map_free(map);
7666 return NULL;
7669 /* Return the union of "map1" and "map2", where we assume for now that
7670 * "map1" and "map2" are disjoint. Note that the basic maps inside
7671 * "map1" or "map2" may not be disjoint from each other.
7672 * Also note that this function is also called from isl_map_union,
7673 * which takes care of handling the situation where "map1" and "map2"
7674 * may not be disjoint.
7676 * If one of the inputs is empty, we can simply return the other input.
7677 * Similarly, if one of the inputs is universal, then it is equal to the union.
7679 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7680 __isl_take isl_map *map2)
7682 int i;
7683 unsigned flags = 0;
7684 struct isl_map *map = NULL;
7685 int is_universe;
7687 if (!map1 || !map2)
7688 goto error;
7690 if (!isl_space_is_equal(map1->dim, map2->dim))
7691 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7692 "spaces don't match", goto error);
7694 if (map1->n == 0) {
7695 isl_map_free(map1);
7696 return map2;
7698 if (map2->n == 0) {
7699 isl_map_free(map2);
7700 return map1;
7703 is_universe = isl_map_plain_is_universe(map1);
7704 if (is_universe < 0)
7705 goto error;
7706 if (is_universe) {
7707 isl_map_free(map2);
7708 return map1;
7711 is_universe = isl_map_plain_is_universe(map2);
7712 if (is_universe < 0)
7713 goto error;
7714 if (is_universe) {
7715 isl_map_free(map1);
7716 return map2;
7719 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7720 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7721 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7723 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7724 map1->n + map2->n, flags);
7725 if (!map)
7726 goto error;
7727 for (i = 0; i < map1->n; ++i) {
7728 map = isl_map_add_basic_map(map,
7729 isl_basic_map_copy(map1->p[i]));
7730 if (!map)
7731 goto error;
7733 for (i = 0; i < map2->n; ++i) {
7734 map = isl_map_add_basic_map(map,
7735 isl_basic_map_copy(map2->p[i]));
7736 if (!map)
7737 goto error;
7739 isl_map_free(map1);
7740 isl_map_free(map2);
7741 return map;
7742 error:
7743 isl_map_free(map);
7744 isl_map_free(map1);
7745 isl_map_free(map2);
7746 return NULL;
7749 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7750 * guaranteed to be disjoint by the caller.
7752 * Note that this functions is called from within isl_map_make_disjoint,
7753 * so we have to be careful not to touch the constraints of the inputs
7754 * in any way.
7756 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7757 __isl_take isl_map *map2)
7759 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7762 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7763 * not be disjoint. The parameters are assumed to have been aligned.
7765 * We currently simply call map_union_disjoint, the internal operation
7766 * of which does not really depend on the inputs being disjoint.
7767 * If the result contains more than one basic map, then we clear
7768 * the disjoint flag since the result may contain basic maps from
7769 * both inputs and these are not guaranteed to be disjoint.
7771 * As a special case, if "map1" and "map2" are obviously equal,
7772 * then we simply return "map1".
7774 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7775 __isl_take isl_map *map2)
7777 int equal;
7779 if (!map1 || !map2)
7780 goto error;
7782 equal = isl_map_plain_is_equal(map1, map2);
7783 if (equal < 0)
7784 goto error;
7785 if (equal) {
7786 isl_map_free(map2);
7787 return map1;
7790 map1 = map_union_disjoint(map1, map2);
7791 if (!map1)
7792 return NULL;
7793 if (map1->n > 1)
7794 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7795 return map1;
7796 error:
7797 isl_map_free(map1);
7798 isl_map_free(map2);
7799 return NULL;
7802 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7803 * not be disjoint.
7805 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7806 __isl_take isl_map *map2)
7808 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7811 __isl_give isl_set *isl_set_union_disjoint(
7812 __isl_take isl_set *set1, __isl_take isl_set *set2)
7814 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7815 set_to_map(set2)));
7818 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7820 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7823 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7824 * the results.
7826 * "map" and "set" are assumed to be compatible and non-NULL.
7828 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7829 __isl_take isl_set *set,
7830 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7831 __isl_take isl_basic_set *bset))
7833 unsigned flags = 0;
7834 struct isl_map *result;
7835 int i, j;
7837 if (isl_set_plain_is_universe(set)) {
7838 isl_set_free(set);
7839 return map;
7842 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7843 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7844 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7846 result = isl_map_alloc_space(isl_space_copy(map->dim),
7847 map->n * set->n, flags);
7848 for (i = 0; result && i < map->n; ++i)
7849 for (j = 0; j < set->n; ++j) {
7850 result = isl_map_add_basic_map(result,
7851 fn(isl_basic_map_copy(map->p[i]),
7852 isl_basic_set_copy(set->p[j])));
7853 if (!result)
7854 break;
7857 isl_map_free(map);
7858 isl_set_free(set);
7859 return result;
7862 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7863 __isl_take isl_set *set)
7865 isl_bool ok;
7867 ok = isl_map_compatible_range(map, set);
7868 if (ok < 0)
7869 goto error;
7870 if (!ok)
7871 isl_die(set->ctx, isl_error_invalid,
7872 "incompatible spaces", goto error);
7874 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7875 error:
7876 isl_map_free(map);
7877 isl_set_free(set);
7878 return NULL;
7881 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7882 __isl_take isl_set *set)
7884 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7887 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7888 __isl_take isl_set *set)
7890 isl_bool ok;
7892 ok = isl_map_compatible_domain(map, set);
7893 if (ok < 0)
7894 goto error;
7895 if (!ok)
7896 isl_die(set->ctx, isl_error_invalid,
7897 "incompatible spaces", goto error);
7899 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7900 error:
7901 isl_map_free(map);
7902 isl_set_free(set);
7903 return NULL;
7906 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7907 __isl_take isl_set *set)
7909 return isl_map_align_params_map_map_and(map, set,
7910 &map_intersect_domain);
7913 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7914 * in the space B -> C, return the intersection.
7915 * The parameters are assumed to have been aligned.
7917 * The map "factor" is first extended to a map living in the space
7918 * [A -> B] -> C and then a regular intersection is computed.
7920 static __isl_give isl_map *map_intersect_domain_factor_range(
7921 __isl_take isl_map *map, __isl_take isl_map *factor)
7923 isl_space *space;
7924 isl_map *ext_factor;
7926 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7927 ext_factor = isl_map_universe(space);
7928 ext_factor = isl_map_domain_product(ext_factor, factor);
7929 return map_intersect(map, ext_factor);
7932 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7933 * in the space B -> C, return the intersection.
7935 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7936 __isl_take isl_map *map, __isl_take isl_map *factor)
7938 return isl_map_align_params_map_map_and(map, factor,
7939 &map_intersect_domain_factor_range);
7942 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7943 * in the space A -> C, return the intersection.
7945 * The map "factor" is first extended to a map living in the space
7946 * A -> [B -> C] and then a regular intersection is computed.
7948 static __isl_give isl_map *map_intersect_range_factor_range(
7949 __isl_take isl_map *map, __isl_take isl_map *factor)
7951 isl_space *space;
7952 isl_map *ext_factor;
7954 space = isl_space_range_factor_domain(isl_map_get_space(map));
7955 ext_factor = isl_map_universe(space);
7956 ext_factor = isl_map_range_product(ext_factor, factor);
7957 return isl_map_intersect(map, ext_factor);
7960 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7961 * in the space A -> C, return the intersection.
7963 __isl_give isl_map *isl_map_intersect_range_factor_range(
7964 __isl_take isl_map *map, __isl_take isl_map *factor)
7966 return isl_map_align_params_map_map_and(map, factor,
7967 &map_intersect_range_factor_range);
7970 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7971 __isl_take isl_map *map2)
7973 if (!map1 || !map2)
7974 goto error;
7975 map1 = isl_map_reverse(map1);
7976 map1 = isl_map_apply_range(map1, map2);
7977 return isl_map_reverse(map1);
7978 error:
7979 isl_map_free(map1);
7980 isl_map_free(map2);
7981 return NULL;
7984 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7985 __isl_take isl_map *map2)
7987 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7990 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7991 __isl_take isl_map *map2)
7993 isl_space *space;
7994 struct isl_map *result;
7995 int i, j;
7997 if (!map1 || !map2)
7998 goto error;
8000 space = isl_space_join(isl_space_copy(map1->dim),
8001 isl_space_copy(map2->dim));
8003 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8004 if (!result)
8005 goto error;
8006 for (i = 0; i < map1->n; ++i)
8007 for (j = 0; j < map2->n; ++j) {
8008 result = isl_map_add_basic_map(result,
8009 isl_basic_map_apply_range(
8010 isl_basic_map_copy(map1->p[i]),
8011 isl_basic_map_copy(map2->p[j])));
8012 if (!result)
8013 goto error;
8015 isl_map_free(map1);
8016 isl_map_free(map2);
8017 if (result && result->n <= 1)
8018 ISL_F_SET(result, ISL_MAP_DISJOINT);
8019 return result;
8020 error:
8021 isl_map_free(map1);
8022 isl_map_free(map2);
8023 return NULL;
8026 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8027 __isl_take isl_map *map2)
8029 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8033 * returns range - domain
8035 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8037 isl_space *target_space;
8038 struct isl_basic_set *bset;
8039 unsigned dim;
8040 unsigned nparam;
8041 int i;
8043 if (!bmap)
8044 goto error;
8045 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8046 bmap->dim, isl_dim_out),
8047 goto error);
8048 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8049 dim = isl_basic_map_dim(bmap, isl_dim_in);
8050 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8051 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8052 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8053 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8054 for (i = 0; i < dim; ++i) {
8055 int j = isl_basic_map_alloc_equality(bmap);
8056 if (j < 0) {
8057 bmap = isl_basic_map_free(bmap);
8058 break;
8060 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8061 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8062 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8063 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8065 bset = isl_basic_map_domain(bmap);
8066 bset = isl_basic_set_reset_space(bset, target_space);
8067 return bset;
8068 error:
8069 isl_basic_map_free(bmap);
8070 return NULL;
8073 /* Check that domain and range of "map" are the same.
8075 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
8077 isl_space *space;
8078 isl_bool equal;
8080 space = isl_map_peek_space(map);
8081 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
8082 if (equal < 0)
8083 return isl_stat_error;
8084 if (!equal)
8085 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8086 "domain and range don't match", return isl_stat_error);
8087 return isl_stat_ok;
8091 * returns range - domain
8093 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8095 int i;
8096 isl_space *dim;
8097 struct isl_set *result;
8099 if (!map)
8100 return NULL;
8102 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8103 map->dim, isl_dim_out),
8104 goto error);
8105 dim = isl_map_get_space(map);
8106 dim = isl_space_domain(dim);
8107 result = isl_set_alloc_space(dim, map->n, 0);
8108 if (!result)
8109 goto error;
8110 for (i = 0; i < map->n; ++i)
8111 result = isl_set_add_basic_set(result,
8112 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8113 isl_map_free(map);
8114 return result;
8115 error:
8116 isl_map_free(map);
8117 return NULL;
8121 * returns [domain -> range] -> range - domain
8123 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8124 __isl_take isl_basic_map *bmap)
8126 int i, k;
8127 isl_space *space;
8128 isl_basic_map *domain;
8129 int nparam, n;
8130 unsigned total;
8132 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8133 bmap->dim, isl_dim_out))
8134 isl_die(bmap->ctx, isl_error_invalid,
8135 "domain and range don't match", goto error);
8137 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8138 n = isl_basic_map_dim(bmap, isl_dim_in);
8140 space = isl_basic_map_get_space(bmap);
8141 space = isl_space_from_range(isl_space_domain(space));
8142 domain = isl_basic_map_universe(space);
8144 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8145 bmap = isl_basic_map_apply_range(bmap, domain);
8146 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8148 total = isl_basic_map_total_dim(bmap);
8150 for (i = 0; i < n; ++i) {
8151 k = isl_basic_map_alloc_equality(bmap);
8152 if (k < 0)
8153 goto error;
8154 isl_seq_clr(bmap->eq[k], 1 + total);
8155 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8156 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8157 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8160 bmap = isl_basic_map_gauss(bmap, NULL);
8161 return isl_basic_map_finalize(bmap);
8162 error:
8163 isl_basic_map_free(bmap);
8164 return NULL;
8168 * returns [domain -> range] -> range - domain
8170 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8172 int i;
8173 isl_space *domain_space;
8175 if (isl_map_check_equal_tuples(map) < 0)
8176 return isl_map_free(map);
8178 map = isl_map_cow(map);
8179 if (!map)
8180 return NULL;
8182 domain_space = isl_space_domain(isl_map_get_space(map));
8183 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8184 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8185 if (!map->dim)
8186 goto error;
8187 for (i = 0; i < map->n; ++i) {
8188 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8189 if (!map->p[i])
8190 goto error;
8192 map = isl_map_unmark_normalized(map);
8193 return map;
8194 error:
8195 isl_map_free(map);
8196 return NULL;
8199 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8201 unsigned n_in, n_out;
8203 if (!space)
8204 return NULL;
8205 n_in = isl_space_dim(space, isl_dim_in);
8206 n_out = isl_space_dim(space, isl_dim_out);
8207 if (n_in != n_out)
8208 isl_die(space->ctx, isl_error_invalid,
8209 "number of input and output dimensions needs to be "
8210 "the same", goto error);
8211 return isl_basic_map_equal(space, n_in);
8212 error:
8213 isl_space_free(space);
8214 return NULL;
8217 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8219 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8222 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8224 isl_space *dim = isl_set_get_space(set);
8225 isl_map *id;
8226 id = isl_map_identity(isl_space_map_from_set(dim));
8227 return isl_map_intersect_range(id, set);
8230 /* Construct a basic set with all set dimensions having only non-negative
8231 * values.
8233 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8234 __isl_take isl_space *space)
8236 int i;
8237 unsigned nparam;
8238 unsigned dim;
8239 struct isl_basic_set *bset;
8241 if (!space)
8242 return NULL;
8243 nparam = space->nparam;
8244 dim = space->n_out;
8245 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8246 if (!bset)
8247 return NULL;
8248 for (i = 0; i < dim; ++i) {
8249 int k = isl_basic_set_alloc_inequality(bset);
8250 if (k < 0)
8251 goto error;
8252 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8253 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8255 return bset;
8256 error:
8257 isl_basic_set_free(bset);
8258 return NULL;
8261 /* Construct the half-space x_pos >= 0.
8263 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8264 int pos)
8266 int k;
8267 isl_basic_set *nonneg;
8269 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8270 k = isl_basic_set_alloc_inequality(nonneg);
8271 if (k < 0)
8272 goto error;
8273 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8274 isl_int_set_si(nonneg->ineq[k][pos], 1);
8276 return isl_basic_set_finalize(nonneg);
8277 error:
8278 isl_basic_set_free(nonneg);
8279 return NULL;
8282 /* Construct the half-space x_pos <= -1.
8284 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8285 int pos)
8287 int k;
8288 isl_basic_set *neg;
8290 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8291 k = isl_basic_set_alloc_inequality(neg);
8292 if (k < 0)
8293 goto error;
8294 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8295 isl_int_set_si(neg->ineq[k][0], -1);
8296 isl_int_set_si(neg->ineq[k][pos], -1);
8298 return isl_basic_set_finalize(neg);
8299 error:
8300 isl_basic_set_free(neg);
8301 return NULL;
8304 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8305 enum isl_dim_type type, unsigned first, unsigned n)
8307 int i;
8308 unsigned offset;
8309 isl_basic_set *nonneg;
8310 isl_basic_set *neg;
8312 if (n == 0)
8313 return set;
8315 if (isl_set_check_range(set, type, first, n) < 0)
8316 return isl_set_free(set);
8318 offset = pos(set->dim, type);
8319 for (i = 0; i < n; ++i) {
8320 nonneg = nonneg_halfspace(isl_set_get_space(set),
8321 offset + first + i);
8322 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8324 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8327 return set;
8330 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8331 int len,
8332 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8333 void *user)
8335 isl_set *half;
8337 if (!set)
8338 return isl_stat_error;
8339 if (isl_set_plain_is_empty(set)) {
8340 isl_set_free(set);
8341 return isl_stat_ok;
8343 if (first == len)
8344 return fn(set, signs, user);
8346 signs[first] = 1;
8347 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8348 1 + first));
8349 half = isl_set_intersect(half, isl_set_copy(set));
8350 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8351 goto error;
8353 signs[first] = -1;
8354 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8355 1 + first));
8356 half = isl_set_intersect(half, set);
8357 return foreach_orthant(half, signs, first + 1, len, fn, user);
8358 error:
8359 isl_set_free(set);
8360 return isl_stat_error;
8363 /* Call "fn" on the intersections of "set" with each of the orthants
8364 * (except for obviously empty intersections). The orthant is identified
8365 * by the signs array, with each entry having value 1 or -1 according
8366 * to the sign of the corresponding variable.
8368 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8369 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8370 void *user)
8372 unsigned nparam;
8373 unsigned nvar;
8374 int *signs;
8375 isl_stat r;
8377 if (!set)
8378 return isl_stat_error;
8379 if (isl_set_plain_is_empty(set))
8380 return isl_stat_ok;
8382 nparam = isl_set_dim(set, isl_dim_param);
8383 nvar = isl_set_dim(set, isl_dim_set);
8385 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8387 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8388 fn, user);
8390 free(signs);
8392 return r;
8395 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8397 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8400 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8401 __isl_keep isl_basic_map *bmap2)
8403 isl_bool is_subset;
8404 struct isl_map *map1;
8405 struct isl_map *map2;
8407 if (!bmap1 || !bmap2)
8408 return isl_bool_error;
8410 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8411 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8413 is_subset = isl_map_is_subset(map1, map2);
8415 isl_map_free(map1);
8416 isl_map_free(map2);
8418 return is_subset;
8421 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8422 __isl_keep isl_basic_set *bset2)
8424 return isl_basic_map_is_subset(bset1, bset2);
8427 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8428 __isl_keep isl_basic_map *bmap2)
8430 isl_bool is_subset;
8432 if (!bmap1 || !bmap2)
8433 return isl_bool_error;
8434 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8435 if (is_subset != isl_bool_true)
8436 return is_subset;
8437 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8438 return is_subset;
8441 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8442 __isl_keep isl_basic_set *bset2)
8444 return isl_basic_map_is_equal(
8445 bset_to_bmap(bset1), bset_to_bmap(bset2));
8448 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8450 int i;
8451 int is_empty;
8453 if (!map)
8454 return isl_bool_error;
8455 for (i = 0; i < map->n; ++i) {
8456 is_empty = isl_basic_map_is_empty(map->p[i]);
8457 if (is_empty < 0)
8458 return isl_bool_error;
8459 if (!is_empty)
8460 return isl_bool_false;
8462 return isl_bool_true;
8465 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8467 return map ? map->n == 0 : isl_bool_error;
8470 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8472 return set ? set->n == 0 : isl_bool_error;
8475 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8477 return isl_map_is_empty(set_to_map(set));
8480 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8481 __isl_keep isl_map *map2)
8483 if (!map1 || !map2)
8484 return isl_bool_error;
8486 return isl_space_is_equal(map1->dim, map2->dim);
8489 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8490 __isl_keep isl_set *set2)
8492 if (!set1 || !set2)
8493 return isl_bool_error;
8495 return isl_space_is_equal(set1->dim, set2->dim);
8498 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8500 isl_bool is_subset;
8502 if (!map1 || !map2)
8503 return isl_bool_error;
8504 is_subset = isl_map_is_subset(map1, map2);
8505 if (is_subset != isl_bool_true)
8506 return is_subset;
8507 is_subset = isl_map_is_subset(map2, map1);
8508 return is_subset;
8511 /* Is "map1" equal to "map2"?
8513 * First check if they are obviously equal.
8514 * If not, then perform a more detailed analysis.
8516 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8518 isl_bool equal;
8520 equal = isl_map_plain_is_equal(map1, map2);
8521 if (equal < 0 || equal)
8522 return equal;
8523 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8526 isl_bool isl_basic_map_is_strict_subset(
8527 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8529 isl_bool is_subset;
8531 if (!bmap1 || !bmap2)
8532 return isl_bool_error;
8533 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8534 if (is_subset != isl_bool_true)
8535 return is_subset;
8536 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8537 return isl_bool_not(is_subset);
8540 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8541 __isl_keep isl_map *map2)
8543 isl_bool is_subset;
8545 if (!map1 || !map2)
8546 return isl_bool_error;
8547 is_subset = isl_map_is_subset(map1, map2);
8548 if (is_subset != isl_bool_true)
8549 return is_subset;
8550 is_subset = isl_map_is_subset(map2, map1);
8551 return isl_bool_not(is_subset);
8554 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8555 __isl_keep isl_set *set2)
8557 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8560 /* Is "bmap" obviously equal to the universe with the same space?
8562 * That is, does it not have any constraints?
8564 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8566 if (!bmap)
8567 return isl_bool_error;
8568 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8571 /* Is "bset" obviously equal to the universe with the same space?
8573 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8575 return isl_basic_map_plain_is_universe(bset);
8578 /* If "c" does not involve any existentially quantified variables,
8579 * then set *univ to false and abort
8581 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8583 isl_bool *univ = user;
8584 unsigned n;
8586 n = isl_constraint_dim(c, isl_dim_div);
8587 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8588 isl_constraint_free(c);
8589 if (*univ < 0 || !*univ)
8590 return isl_stat_error;
8591 return isl_stat_ok;
8594 /* Is "bmap" equal to the universe with the same space?
8596 * First check if it is obviously equal to the universe.
8597 * If not and if there are any constraints not involving
8598 * existentially quantified variables, then it is certainly
8599 * not equal to the universe.
8600 * Otherwise, check if the universe is a subset of "bmap".
8602 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8604 isl_bool univ;
8605 isl_basic_map *test;
8607 univ = isl_basic_map_plain_is_universe(bmap);
8608 if (univ < 0 || univ)
8609 return univ;
8610 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8611 return isl_bool_false;
8612 univ = isl_bool_true;
8613 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8614 univ)
8615 return isl_bool_error;
8616 if (univ < 0 || !univ)
8617 return univ;
8618 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8619 univ = isl_basic_map_is_subset(test, bmap);
8620 isl_basic_map_free(test);
8621 return univ;
8624 /* Is "bset" equal to the universe with the same space?
8626 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8628 return isl_basic_map_is_universe(bset);
8631 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8633 int i;
8635 if (!map)
8636 return isl_bool_error;
8638 for (i = 0; i < map->n; ++i) {
8639 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8640 if (r < 0 || r)
8641 return r;
8644 return isl_bool_false;
8647 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8649 return isl_map_plain_is_universe(set_to_map(set));
8652 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8654 struct isl_basic_set *bset = NULL;
8655 struct isl_vec *sample = NULL;
8656 isl_bool empty, non_empty;
8658 if (!bmap)
8659 return isl_bool_error;
8661 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8662 return isl_bool_true;
8664 if (isl_basic_map_plain_is_universe(bmap))
8665 return isl_bool_false;
8667 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8668 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8669 copy = isl_basic_map_remove_redundancies(copy);
8670 empty = isl_basic_map_plain_is_empty(copy);
8671 isl_basic_map_free(copy);
8672 return empty;
8675 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8676 if (non_empty < 0)
8677 return isl_bool_error;
8678 if (non_empty)
8679 return isl_bool_false;
8680 isl_vec_free(bmap->sample);
8681 bmap->sample = NULL;
8682 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8683 if (!bset)
8684 return isl_bool_error;
8685 sample = isl_basic_set_sample_vec(bset);
8686 if (!sample)
8687 return isl_bool_error;
8688 empty = sample->size == 0;
8689 isl_vec_free(bmap->sample);
8690 bmap->sample = sample;
8691 if (empty)
8692 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8694 return empty;
8697 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8699 if (!bmap)
8700 return isl_bool_error;
8701 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8704 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8706 if (!bset)
8707 return isl_bool_error;
8708 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8711 /* Is "bmap" known to be non-empty?
8713 * That is, is the cached sample still valid?
8715 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8717 unsigned total;
8719 if (!bmap)
8720 return isl_bool_error;
8721 if (!bmap->sample)
8722 return isl_bool_false;
8723 total = 1 + isl_basic_map_total_dim(bmap);
8724 if (bmap->sample->size != total)
8725 return isl_bool_false;
8726 return isl_basic_map_contains(bmap, bmap->sample);
8729 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8731 return isl_basic_map_is_empty(bset_to_bmap(bset));
8734 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8735 __isl_take isl_basic_map *bmap2)
8737 struct isl_map *map;
8738 if (!bmap1 || !bmap2)
8739 goto error;
8741 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8743 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8744 if (!map)
8745 goto error;
8746 map = isl_map_add_basic_map(map, bmap1);
8747 map = isl_map_add_basic_map(map, bmap2);
8748 return map;
8749 error:
8750 isl_basic_map_free(bmap1);
8751 isl_basic_map_free(bmap2);
8752 return NULL;
8755 struct isl_set *isl_basic_set_union(
8756 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8758 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8759 bset_to_bmap(bset2)));
8762 /* Order divs such that any div only depends on previous divs */
8763 __isl_give isl_basic_map *isl_basic_map_order_divs(
8764 __isl_take isl_basic_map *bmap)
8766 int i;
8767 unsigned off;
8769 if (!bmap)
8770 return NULL;
8772 off = isl_space_dim(bmap->dim, isl_dim_all);
8774 for (i = 0; i < bmap->n_div; ++i) {
8775 int pos;
8776 if (isl_int_is_zero(bmap->div[i][0]))
8777 continue;
8778 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8779 bmap->n_div-i);
8780 if (pos == -1)
8781 continue;
8782 if (pos == 0)
8783 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8784 "integer division depends on itself",
8785 return isl_basic_map_free(bmap));
8786 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8787 if (!bmap)
8788 return NULL;
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 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8863 if (!bmap)
8864 goto error;
8866 j = 0;
8867 for (i = 0; i < div->n_row; ++i) {
8868 if (j < n_div && exp[j] == i) {
8869 j++;
8870 } else {
8871 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8872 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8873 continue;
8874 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8875 goto error;
8879 isl_mat_free(div);
8880 return bmap;
8881 error:
8882 isl_basic_map_free(bmap);
8883 isl_mat_free(div);
8884 return NULL;
8887 /* Apply the expansion computed by isl_merge_divs.
8888 * The expansion itself is given by "exp" while the resulting
8889 * list of divs is given by "div".
8891 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8892 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8894 return isl_basic_map_expand_divs(bset, div, exp);
8897 /* Look for a div in dst that corresponds to the div "div" in src.
8898 * The divs before "div" in src and dst are assumed to be the same.
8900 * Returns -1 if no corresponding div was found and the position
8901 * of the corresponding div in dst otherwise.
8903 static int find_div(__isl_keep isl_basic_map *dst,
8904 __isl_keep isl_basic_map *src, unsigned div)
8906 int i;
8908 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8910 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8911 for (i = div; i < dst->n_div; ++i)
8912 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8913 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8914 dst->n_div - div) == -1)
8915 return i;
8916 return -1;
8919 /* Align the divs of "dst" to those of "src", adding divs from "src"
8920 * if needed. That is, make sure that the first src->n_div divs
8921 * of the result are equal to those of src.
8923 * The result is not finalized as by design it will have redundant
8924 * divs if any divs from "src" were copied.
8926 __isl_give isl_basic_map *isl_basic_map_align_divs(
8927 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8929 int i;
8930 isl_bool known;
8931 int extended;
8932 unsigned total;
8934 if (!dst || !src)
8935 return isl_basic_map_free(dst);
8937 if (src->n_div == 0)
8938 return dst;
8940 known = isl_basic_map_divs_known(src);
8941 if (known < 0)
8942 return isl_basic_map_free(dst);
8943 if (!known)
8944 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8945 "some src divs are unknown",
8946 return isl_basic_map_free(dst));
8948 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8949 if (!src)
8950 return isl_basic_map_free(dst);
8952 extended = 0;
8953 total = isl_space_dim(src->dim, isl_dim_all);
8954 for (i = 0; i < src->n_div; ++i) {
8955 int j = find_div(dst, src, i);
8956 if (j < 0) {
8957 if (!extended) {
8958 int extra = src->n_div - i;
8959 dst = isl_basic_map_cow(dst);
8960 if (!dst)
8961 goto error;
8962 dst = isl_basic_map_extend_space(dst,
8963 isl_space_copy(dst->dim),
8964 extra, 0, 2 * extra);
8965 extended = 1;
8967 j = isl_basic_map_alloc_div(dst);
8968 if (j < 0)
8969 goto error;
8970 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8971 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8972 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8973 goto error;
8975 if (j != i)
8976 dst = isl_basic_map_swap_div(dst, i, j);
8977 if (!dst)
8978 goto error;
8980 isl_basic_map_free(src);
8981 return dst;
8982 error:
8983 isl_basic_map_free(src);
8984 isl_basic_map_free(dst);
8985 return NULL;
8988 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8990 int i;
8992 if (!map)
8993 return NULL;
8994 if (map->n == 0)
8995 return map;
8996 map = isl_map_compute_divs(map);
8997 map = isl_map_cow(map);
8998 if (!map)
8999 return NULL;
9001 for (i = 1; i < map->n; ++i)
9002 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9003 for (i = 1; i < map->n; ++i) {
9004 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9005 if (!map->p[i])
9006 return isl_map_free(map);
9009 map = isl_map_unmark_normalized(map);
9010 return map;
9013 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9015 return isl_map_align_divs_internal(map);
9018 struct isl_set *isl_set_align_divs(struct isl_set *set)
9020 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9023 /* Align the divs of the basic maps in "map" to those
9024 * of the basic maps in "list", as well as to the other basic maps in "map".
9025 * The elements in "list" are assumed to have known divs.
9027 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9028 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9030 int i, n;
9032 map = isl_map_compute_divs(map);
9033 map = isl_map_cow(map);
9034 if (!map || !list)
9035 return isl_map_free(map);
9036 if (map->n == 0)
9037 return map;
9039 n = isl_basic_map_list_n_basic_map(list);
9040 for (i = 0; i < n; ++i) {
9041 isl_basic_map *bmap;
9043 bmap = isl_basic_map_list_get_basic_map(list, i);
9044 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9045 isl_basic_map_free(bmap);
9047 if (!map->p[0])
9048 return isl_map_free(map);
9050 return isl_map_align_divs_internal(map);
9053 /* Align the divs of each element of "list" to those of "bmap".
9054 * Both "bmap" and the elements of "list" are assumed to have known divs.
9056 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9057 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9059 int i, n;
9061 if (!list || !bmap)
9062 return isl_basic_map_list_free(list);
9064 n = isl_basic_map_list_n_basic_map(list);
9065 for (i = 0; i < n; ++i) {
9066 isl_basic_map *bmap_i;
9068 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9069 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9070 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9073 return list;
9076 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9077 __isl_take isl_map *map)
9079 isl_bool ok;
9081 ok = isl_map_compatible_domain(map, set);
9082 if (ok < 0)
9083 goto error;
9084 if (!ok)
9085 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9086 "incompatible spaces", goto error);
9087 map = isl_map_intersect_domain(map, set);
9088 set = isl_map_range(map);
9089 return set;
9090 error:
9091 isl_set_free(set);
9092 isl_map_free(map);
9093 return NULL;
9096 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9097 __isl_take isl_map *map)
9099 return isl_map_align_params_map_map_and(set, map, &set_apply);
9102 /* There is no need to cow as removing empty parts doesn't change
9103 * the meaning of the set.
9105 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9107 int i;
9109 if (!map)
9110 return NULL;
9112 for (i = map->n - 1; i >= 0; --i)
9113 map = remove_if_empty(map, i);
9115 return map;
9118 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9120 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9123 /* Create a binary relation that maps the shared initial "pos" dimensions
9124 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9126 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9127 __isl_keep isl_basic_set *bset2, int pos)
9129 isl_basic_map *bmap1;
9130 isl_basic_map *bmap2;
9132 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9133 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9134 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9135 isl_dim_out, 0, pos);
9136 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9137 isl_dim_out, 0, pos);
9138 return isl_basic_map_range_product(bmap1, bmap2);
9141 /* Given two basic sets bset1 and bset2, compute the maximal difference
9142 * between the values of dimension pos in bset1 and those in bset2
9143 * for any common value of the parameters and dimensions preceding pos.
9145 static enum isl_lp_result basic_set_maximal_difference_at(
9146 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9147 int pos, isl_int *opt)
9149 isl_basic_map *bmap1;
9150 struct isl_ctx *ctx;
9151 struct isl_vec *obj;
9152 unsigned total;
9153 unsigned nparam;
9154 unsigned dim1;
9155 enum isl_lp_result res;
9157 if (!bset1 || !bset2)
9158 return isl_lp_error;
9160 nparam = isl_basic_set_n_param(bset1);
9161 dim1 = isl_basic_set_n_dim(bset1);
9163 bmap1 = join_initial(bset1, bset2, pos);
9164 if (!bmap1)
9165 return isl_lp_error;
9167 total = isl_basic_map_total_dim(bmap1);
9168 ctx = bmap1->ctx;
9169 obj = isl_vec_alloc(ctx, 1 + total);
9170 if (!obj)
9171 goto error;
9172 isl_seq_clr(obj->block.data, 1 + total);
9173 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9174 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9175 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9176 opt, NULL, NULL);
9177 isl_basic_map_free(bmap1);
9178 isl_vec_free(obj);
9179 return res;
9180 error:
9181 isl_basic_map_free(bmap1);
9182 return isl_lp_error;
9185 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9186 * for any common value of the parameters and dimensions preceding pos
9187 * in both basic sets, the values of dimension pos in bset1 are
9188 * smaller or larger than those in bset2.
9190 * Returns
9191 * 1 if bset1 follows bset2
9192 * -1 if bset1 precedes bset2
9193 * 0 if bset1 and bset2 are incomparable
9194 * -2 if some error occurred.
9196 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9197 __isl_keep isl_basic_set *bset2, int pos)
9199 isl_int opt;
9200 enum isl_lp_result res;
9201 int cmp;
9203 isl_int_init(opt);
9205 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9207 if (res == isl_lp_empty)
9208 cmp = 0;
9209 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9210 res == isl_lp_unbounded)
9211 cmp = 1;
9212 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9213 cmp = -1;
9214 else
9215 cmp = -2;
9217 isl_int_clear(opt);
9218 return cmp;
9221 /* Given two basic sets bset1 and bset2, check whether
9222 * for any common value of the parameters and dimensions preceding pos
9223 * there is a value of dimension pos in bset1 that is larger
9224 * than a value of the same dimension in bset2.
9226 * Return
9227 * 1 if there exists such a pair
9228 * 0 if there is no such pair, but there is a pair of equal values
9229 * -1 otherwise
9230 * -2 if some error occurred.
9232 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9233 __isl_keep isl_basic_set *bset2, int pos)
9235 isl_bool empty;
9236 isl_basic_map *bmap;
9237 unsigned dim1;
9239 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9240 bmap = join_initial(bset1, bset2, pos);
9241 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9242 isl_dim_out, dim1 - pos);
9243 empty = isl_basic_map_is_empty(bmap);
9244 if (empty < 0)
9245 goto error;
9246 if (empty) {
9247 isl_basic_map_free(bmap);
9248 return -1;
9250 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9251 isl_dim_out, dim1 - pos);
9252 empty = isl_basic_map_is_empty(bmap);
9253 if (empty < 0)
9254 goto error;
9255 isl_basic_map_free(bmap);
9256 if (empty)
9257 return 0;
9258 return 1;
9259 error:
9260 isl_basic_map_free(bmap);
9261 return -2;
9264 /* Given two sets set1 and set2, check whether
9265 * for any common value of the parameters and dimensions preceding pos
9266 * there is a value of dimension pos in set1 that is larger
9267 * than a value of the same dimension in set2.
9269 * Return
9270 * 1 if there exists such a pair
9271 * 0 if there is no such pair, but there is a pair of equal values
9272 * -1 otherwise
9273 * -2 if some error occurred.
9275 int isl_set_follows_at(__isl_keep isl_set *set1,
9276 __isl_keep isl_set *set2, int pos)
9278 int i, j;
9279 int follows = -1;
9281 if (!set1 || !set2)
9282 return -2;
9284 for (i = 0; i < set1->n; ++i)
9285 for (j = 0; j < set2->n; ++j) {
9286 int f;
9287 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9288 if (f == 1 || f == -2)
9289 return f;
9290 if (f > follows)
9291 follows = f;
9294 return follows;
9297 static isl_bool isl_basic_map_plain_has_fixed_var(
9298 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9300 int i;
9301 int d;
9302 unsigned total;
9304 if (!bmap)
9305 return isl_bool_error;
9306 total = isl_basic_map_total_dim(bmap);
9307 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9308 for (; d+1 > pos; --d)
9309 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9310 break;
9311 if (d != pos)
9312 continue;
9313 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9314 return isl_bool_false;
9315 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9316 return isl_bool_false;
9317 if (!isl_int_is_one(bmap->eq[i][1+d]))
9318 return isl_bool_false;
9319 if (val)
9320 isl_int_neg(*val, bmap->eq[i][0]);
9321 return isl_bool_true;
9323 return isl_bool_false;
9326 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9327 unsigned pos, isl_int *val)
9329 int i;
9330 isl_int v;
9331 isl_int tmp;
9332 isl_bool fixed;
9334 if (!map)
9335 return isl_bool_error;
9336 if (map->n == 0)
9337 return isl_bool_false;
9338 if (map->n == 1)
9339 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9340 isl_int_init(v);
9341 isl_int_init(tmp);
9342 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9343 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9344 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9345 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9346 fixed = isl_bool_false;
9348 if (val)
9349 isl_int_set(*val, v);
9350 isl_int_clear(tmp);
9351 isl_int_clear(v);
9352 return fixed;
9355 static isl_bool isl_basic_set_plain_has_fixed_var(
9356 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9358 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9359 pos, val);
9362 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9363 enum isl_dim_type type, unsigned pos, isl_int *val)
9365 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9366 return isl_bool_error;
9367 return isl_basic_map_plain_has_fixed_var(bmap,
9368 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9371 /* If "bmap" obviously lies on a hyperplane where the given dimension
9372 * has a fixed value, then return that value.
9373 * Otherwise return NaN.
9375 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9376 __isl_keep isl_basic_map *bmap,
9377 enum isl_dim_type type, unsigned pos)
9379 isl_ctx *ctx;
9380 isl_val *v;
9381 isl_bool fixed;
9383 if (!bmap)
9384 return NULL;
9385 ctx = isl_basic_map_get_ctx(bmap);
9386 v = isl_val_alloc(ctx);
9387 if (!v)
9388 return NULL;
9389 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9390 if (fixed < 0)
9391 return isl_val_free(v);
9392 if (fixed) {
9393 isl_int_set_si(v->d, 1);
9394 return v;
9396 isl_val_free(v);
9397 return isl_val_nan(ctx);
9400 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9401 enum isl_dim_type type, unsigned pos, isl_int *val)
9403 if (isl_map_check_range(map, type, pos, 1) < 0)
9404 return isl_bool_error;
9405 return isl_map_plain_has_fixed_var(map,
9406 map_offset(map, type) - 1 + pos, val);
9409 /* If "map" obviously lies on a hyperplane where the given dimension
9410 * has a fixed value, then return that value.
9411 * Otherwise return NaN.
9413 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9414 enum isl_dim_type type, unsigned pos)
9416 isl_ctx *ctx;
9417 isl_val *v;
9418 isl_bool fixed;
9420 if (!map)
9421 return NULL;
9422 ctx = isl_map_get_ctx(map);
9423 v = isl_val_alloc(ctx);
9424 if (!v)
9425 return NULL;
9426 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9427 if (fixed < 0)
9428 return isl_val_free(v);
9429 if (fixed) {
9430 isl_int_set_si(v->d, 1);
9431 return v;
9433 isl_val_free(v);
9434 return isl_val_nan(ctx);
9437 /* If "set" obviously lies on a hyperplane where the given dimension
9438 * has a fixed value, then return that value.
9439 * Otherwise return NaN.
9441 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9442 enum isl_dim_type type, unsigned pos)
9444 return isl_map_plain_get_val_if_fixed(set, type, pos);
9447 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9448 * then return this fixed value in *val.
9450 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9451 unsigned dim, isl_int *val)
9453 return isl_basic_set_plain_has_fixed_var(bset,
9454 isl_basic_set_n_param(bset) + dim, val);
9457 /* Return -1 if the constraint "c1" should be sorted before "c2"
9458 * and 1 if it should be sorted after "c2".
9459 * Return 0 if the two constraints are the same (up to the constant term).
9461 * In particular, if a constraint involves later variables than another
9462 * then it is sorted after this other constraint.
9463 * uset_gist depends on constraints without existentially quantified
9464 * variables sorting first.
9466 * For constraints that have the same latest variable, those
9467 * with the same coefficient for this latest variable (first in absolute value
9468 * and then in actual value) are grouped together.
9469 * This is useful for detecting pairs of constraints that can
9470 * be chained in their printed representation.
9472 * Finally, within a group, constraints are sorted according to
9473 * their coefficients (excluding the constant term).
9475 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9477 isl_int **c1 = (isl_int **) p1;
9478 isl_int **c2 = (isl_int **) p2;
9479 int l1, l2;
9480 unsigned size = *(unsigned *) arg;
9481 int cmp;
9483 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9484 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9486 if (l1 != l2)
9487 return l1 - l2;
9489 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9490 if (cmp != 0)
9491 return cmp;
9492 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9493 if (cmp != 0)
9494 return -cmp;
9496 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9499 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9500 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9501 * and 0 if the two constraints are the same (up to the constant term).
9503 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9504 isl_int *c1, isl_int *c2)
9506 unsigned total;
9508 if (!bmap)
9509 return -2;
9510 total = isl_basic_map_total_dim(bmap);
9511 return sort_constraint_cmp(&c1, &c2, &total);
9514 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9515 __isl_take isl_basic_map *bmap)
9517 unsigned total;
9519 if (!bmap)
9520 return NULL;
9521 if (bmap->n_ineq == 0)
9522 return bmap;
9523 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9524 return bmap;
9525 total = isl_basic_map_total_dim(bmap);
9526 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9527 &sort_constraint_cmp, &total) < 0)
9528 return isl_basic_map_free(bmap);
9529 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9530 return bmap;
9533 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9534 __isl_take isl_basic_set *bset)
9536 isl_basic_map *bmap = bset_to_bmap(bset);
9537 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9540 __isl_give isl_basic_map *isl_basic_map_normalize(
9541 __isl_take isl_basic_map *bmap)
9543 bmap = isl_basic_map_remove_redundancies(bmap);
9544 bmap = isl_basic_map_sort_constraints(bmap);
9545 return bmap;
9547 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9548 __isl_keep isl_basic_map *bmap2)
9550 int i, cmp;
9551 unsigned total;
9552 isl_space *space1, *space2;
9554 if (!bmap1 || !bmap2)
9555 return -1;
9557 if (bmap1 == bmap2)
9558 return 0;
9559 space1 = isl_basic_map_peek_space(bmap1);
9560 space2 = isl_basic_map_peek_space(bmap2);
9561 cmp = isl_space_cmp(space1, space2);
9562 if (cmp)
9563 return cmp;
9564 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9565 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9566 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9567 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9568 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9569 return 0;
9570 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9571 return 1;
9572 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9573 return -1;
9574 if (bmap1->n_eq != bmap2->n_eq)
9575 return bmap1->n_eq - bmap2->n_eq;
9576 if (bmap1->n_ineq != bmap2->n_ineq)
9577 return bmap1->n_ineq - bmap2->n_ineq;
9578 if (bmap1->n_div != bmap2->n_div)
9579 return bmap1->n_div - bmap2->n_div;
9580 total = isl_basic_map_total_dim(bmap1);
9581 for (i = 0; i < bmap1->n_eq; ++i) {
9582 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9583 if (cmp)
9584 return cmp;
9586 for (i = 0; i < bmap1->n_ineq; ++i) {
9587 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9588 if (cmp)
9589 return cmp;
9591 for (i = 0; i < bmap1->n_div; ++i) {
9592 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9593 if (cmp)
9594 return cmp;
9596 return 0;
9599 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9600 __isl_keep isl_basic_set *bset2)
9602 return isl_basic_map_plain_cmp(bset1, bset2);
9605 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9607 int i, cmp;
9609 if (set1 == set2)
9610 return 0;
9611 if (set1->n != set2->n)
9612 return set1->n - set2->n;
9614 for (i = 0; i < set1->n; ++i) {
9615 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9616 if (cmp)
9617 return cmp;
9620 return 0;
9623 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9624 __isl_keep isl_basic_map *bmap2)
9626 if (!bmap1 || !bmap2)
9627 return isl_bool_error;
9628 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9631 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9632 __isl_keep isl_basic_set *bset2)
9634 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9635 bset_to_bmap(bset2));
9638 static int qsort_bmap_cmp(const void *p1, const void *p2)
9640 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9641 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9643 return isl_basic_map_plain_cmp(bmap1, bmap2);
9646 /* Sort the basic maps of "map" and remove duplicate basic maps.
9648 * While removing basic maps, we make sure that the basic maps remain
9649 * sorted because isl_map_normalize expects the basic maps of the result
9650 * to be sorted.
9652 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9654 int i, j;
9656 map = isl_map_remove_empty_parts(map);
9657 if (!map)
9658 return NULL;
9659 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9660 for (i = map->n - 1; i >= 1; --i) {
9661 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9662 continue;
9663 isl_basic_map_free(map->p[i-1]);
9664 for (j = i; j < map->n; ++j)
9665 map->p[j - 1] = map->p[j];
9666 map->n--;
9669 return map;
9672 /* Remove obvious duplicates among the basic maps of "map".
9674 * Unlike isl_map_normalize, this function does not remove redundant
9675 * constraints and only removes duplicates that have exactly the same
9676 * constraints in the input. It does sort the constraints and
9677 * the basic maps to ease the detection of duplicates.
9679 * If "map" has already been normalized or if the basic maps are
9680 * disjoint, then there can be no duplicates.
9682 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9684 int i;
9685 isl_basic_map *bmap;
9687 if (!map)
9688 return NULL;
9689 if (map->n <= 1)
9690 return map;
9691 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9692 return map;
9693 for (i = 0; i < map->n; ++i) {
9694 bmap = isl_basic_map_copy(map->p[i]);
9695 bmap = isl_basic_map_sort_constraints(bmap);
9696 if (!bmap)
9697 return isl_map_free(map);
9698 isl_basic_map_free(map->p[i]);
9699 map->p[i] = bmap;
9702 map = sort_and_remove_duplicates(map);
9703 return map;
9706 /* We normalize in place, but if anything goes wrong we need
9707 * to return NULL, so we need to make sure we don't change the
9708 * meaning of any possible other copies of map.
9710 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9712 int i;
9713 struct isl_basic_map *bmap;
9715 if (!map)
9716 return NULL;
9717 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9718 return map;
9719 for (i = 0; i < map->n; ++i) {
9720 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9721 if (!bmap)
9722 goto error;
9723 isl_basic_map_free(map->p[i]);
9724 map->p[i] = bmap;
9727 map = sort_and_remove_duplicates(map);
9728 if (map)
9729 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9730 return map;
9731 error:
9732 isl_map_free(map);
9733 return NULL;
9736 struct isl_set *isl_set_normalize(struct isl_set *set)
9738 return set_from_map(isl_map_normalize(set_to_map(set)));
9741 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9742 __isl_keep isl_map *map2)
9744 int i;
9745 isl_bool equal;
9747 if (!map1 || !map2)
9748 return isl_bool_error;
9750 if (map1 == map2)
9751 return isl_bool_true;
9752 if (!isl_space_is_equal(map1->dim, map2->dim))
9753 return isl_bool_false;
9755 map1 = isl_map_copy(map1);
9756 map2 = isl_map_copy(map2);
9757 map1 = isl_map_normalize(map1);
9758 map2 = isl_map_normalize(map2);
9759 if (!map1 || !map2)
9760 goto error;
9761 equal = map1->n == map2->n;
9762 for (i = 0; equal && i < map1->n; ++i) {
9763 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9764 if (equal < 0)
9765 goto error;
9767 isl_map_free(map1);
9768 isl_map_free(map2);
9769 return equal;
9770 error:
9771 isl_map_free(map1);
9772 isl_map_free(map2);
9773 return isl_bool_error;
9776 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9777 __isl_keep isl_set *set2)
9779 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9782 /* Return the basic maps in "map" as a list.
9784 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9785 __isl_keep isl_map *map)
9787 int i;
9788 isl_ctx *ctx;
9789 isl_basic_map_list *list;
9791 if (!map)
9792 return NULL;
9793 ctx = isl_map_get_ctx(map);
9794 list = isl_basic_map_list_alloc(ctx, map->n);
9796 for (i = 0; i < map->n; ++i) {
9797 isl_basic_map *bmap;
9799 bmap = isl_basic_map_copy(map->p[i]);
9800 list = isl_basic_map_list_add(list, bmap);
9803 return list;
9806 /* Return the intersection of the elements in the non-empty list "list".
9807 * All elements are assumed to live in the same space.
9809 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9810 __isl_take isl_basic_map_list *list)
9812 int i, n;
9813 isl_basic_map *bmap;
9815 if (!list)
9816 return NULL;
9817 n = isl_basic_map_list_n_basic_map(list);
9818 if (n < 1)
9819 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9820 "expecting non-empty list", goto error);
9822 bmap = isl_basic_map_list_get_basic_map(list, 0);
9823 for (i = 1; i < n; ++i) {
9824 isl_basic_map *bmap_i;
9826 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9827 bmap = isl_basic_map_intersect(bmap, bmap_i);
9830 isl_basic_map_list_free(list);
9831 return bmap;
9832 error:
9833 isl_basic_map_list_free(list);
9834 return NULL;
9837 /* Return the intersection of the elements in the non-empty list "list".
9838 * All elements are assumed to live in the same space.
9840 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9841 __isl_take isl_basic_set_list *list)
9843 return isl_basic_map_list_intersect(list);
9846 /* Return the union of the elements of "list".
9847 * The list is required to have at least one element.
9849 __isl_give isl_set *isl_basic_set_list_union(
9850 __isl_take isl_basic_set_list *list)
9852 int i, n;
9853 isl_space *space;
9854 isl_basic_set *bset;
9855 isl_set *set;
9857 if (!list)
9858 return NULL;
9859 n = isl_basic_set_list_n_basic_set(list);
9860 if (n < 1)
9861 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9862 "expecting non-empty list", goto error);
9864 bset = isl_basic_set_list_get_basic_set(list, 0);
9865 space = isl_basic_set_get_space(bset);
9866 isl_basic_set_free(bset);
9868 set = isl_set_alloc_space(space, n, 0);
9869 for (i = 0; i < n; ++i) {
9870 bset = isl_basic_set_list_get_basic_set(list, i);
9871 set = isl_set_add_basic_set(set, bset);
9874 isl_basic_set_list_free(list);
9875 return set;
9876 error:
9877 isl_basic_set_list_free(list);
9878 return NULL;
9881 /* Return the union of the elements in the non-empty list "list".
9882 * All elements are assumed to live in the same space.
9884 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9886 int i, n;
9887 isl_set *set;
9889 if (!list)
9890 return NULL;
9891 n = isl_set_list_n_set(list);
9892 if (n < 1)
9893 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9894 "expecting non-empty list", goto error);
9896 set = isl_set_list_get_set(list, 0);
9897 for (i = 1; i < n; ++i) {
9898 isl_set *set_i;
9900 set_i = isl_set_list_get_set(list, i);
9901 set = isl_set_union(set, set_i);
9904 isl_set_list_free(list);
9905 return set;
9906 error:
9907 isl_set_list_free(list);
9908 return NULL;
9911 __isl_give isl_basic_map *isl_basic_map_product(
9912 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9914 isl_space *space_result = NULL;
9915 struct isl_basic_map *bmap;
9916 unsigned in1, in2, out1, out2, nparam, total, pos;
9917 struct isl_dim_map *dim_map1, *dim_map2;
9919 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9920 goto error;
9921 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9922 isl_space_copy(bmap2->dim));
9924 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9925 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9926 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9927 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9928 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9930 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9931 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9932 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9933 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9934 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9935 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9936 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9937 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9938 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9939 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9940 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9942 bmap = isl_basic_map_alloc_space(space_result,
9943 bmap1->n_div + bmap2->n_div,
9944 bmap1->n_eq + bmap2->n_eq,
9945 bmap1->n_ineq + bmap2->n_ineq);
9946 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9947 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9948 bmap = isl_basic_map_simplify(bmap);
9949 return isl_basic_map_finalize(bmap);
9950 error:
9951 isl_basic_map_free(bmap1);
9952 isl_basic_map_free(bmap2);
9953 return NULL;
9956 __isl_give isl_basic_map *isl_basic_map_flat_product(
9957 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9959 isl_basic_map *prod;
9961 prod = isl_basic_map_product(bmap1, bmap2);
9962 prod = isl_basic_map_flatten(prod);
9963 return prod;
9966 __isl_give isl_basic_set *isl_basic_set_flat_product(
9967 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9969 return isl_basic_map_flat_range_product(bset1, bset2);
9972 __isl_give isl_basic_map *isl_basic_map_domain_product(
9973 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9975 isl_space *space_result = NULL;
9976 isl_basic_map *bmap;
9977 unsigned in1, in2, out, nparam, total, pos;
9978 struct isl_dim_map *dim_map1, *dim_map2;
9980 if (!bmap1 || !bmap2)
9981 goto error;
9983 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9984 isl_space_copy(bmap2->dim));
9986 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9987 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9988 out = isl_basic_map_dim(bmap1, isl_dim_out);
9989 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9991 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9992 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9993 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9994 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9995 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9996 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9997 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9998 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9999 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10000 isl_dim_map_div(dim_map1, bmap1, pos += out);
10001 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10003 bmap = isl_basic_map_alloc_space(space_result,
10004 bmap1->n_div + bmap2->n_div,
10005 bmap1->n_eq + bmap2->n_eq,
10006 bmap1->n_ineq + bmap2->n_ineq);
10007 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10008 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10009 bmap = isl_basic_map_simplify(bmap);
10010 return isl_basic_map_finalize(bmap);
10011 error:
10012 isl_basic_map_free(bmap1);
10013 isl_basic_map_free(bmap2);
10014 return NULL;
10017 __isl_give isl_basic_map *isl_basic_map_range_product(
10018 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10020 isl_bool rational;
10021 isl_space *space_result = NULL;
10022 isl_basic_map *bmap;
10023 unsigned in, out1, out2, nparam, total, pos;
10024 struct isl_dim_map *dim_map1, *dim_map2;
10026 rational = isl_basic_map_is_rational(bmap1);
10027 if (rational >= 0 && rational)
10028 rational = isl_basic_map_is_rational(bmap2);
10029 if (!bmap1 || !bmap2 || rational < 0)
10030 goto error;
10032 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10033 goto error;
10035 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10036 isl_space_copy(bmap2->dim));
10038 in = isl_basic_map_dim(bmap1, isl_dim_in);
10039 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10040 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10041 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10043 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10044 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10045 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10046 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10047 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10048 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10049 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10050 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10051 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10052 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10053 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10055 bmap = isl_basic_map_alloc_space(space_result,
10056 bmap1->n_div + bmap2->n_div,
10057 bmap1->n_eq + bmap2->n_eq,
10058 bmap1->n_ineq + bmap2->n_ineq);
10059 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10060 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10061 if (rational)
10062 bmap = isl_basic_map_set_rational(bmap);
10063 bmap = isl_basic_map_simplify(bmap);
10064 return isl_basic_map_finalize(bmap);
10065 error:
10066 isl_basic_map_free(bmap1);
10067 isl_basic_map_free(bmap2);
10068 return NULL;
10071 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10072 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10074 isl_basic_map *prod;
10076 prod = isl_basic_map_range_product(bmap1, bmap2);
10077 prod = isl_basic_map_flatten_range(prod);
10078 return prod;
10081 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10082 * and collect the results.
10083 * The result live in the space obtained by calling "space_product"
10084 * on the spaces of "map1" and "map2".
10085 * If "remove_duplicates" is set then the result may contain duplicates
10086 * (even if the inputs do not) and so we try and remove the obvious
10087 * duplicates.
10089 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10090 __isl_take isl_map *map2,
10091 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10092 __isl_take isl_space *right),
10093 __isl_give isl_basic_map *(*basic_map_product)(
10094 __isl_take isl_basic_map *left,
10095 __isl_take isl_basic_map *right),
10096 int remove_duplicates)
10098 unsigned flags = 0;
10099 struct isl_map *result;
10100 int i, j;
10101 isl_bool m;
10103 m = isl_map_has_equal_params(map1, map2);
10104 if (m < 0)
10105 goto error;
10106 if (!m)
10107 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10108 "parameters don't match", goto error);
10110 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10111 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10112 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10114 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10115 isl_space_copy(map2->dim)),
10116 map1->n * map2->n, flags);
10117 if (!result)
10118 goto error;
10119 for (i = 0; i < map1->n; ++i)
10120 for (j = 0; j < map2->n; ++j) {
10121 struct isl_basic_map *part;
10122 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10123 isl_basic_map_copy(map2->p[j]));
10124 if (isl_basic_map_is_empty(part))
10125 isl_basic_map_free(part);
10126 else
10127 result = isl_map_add_basic_map(result, part);
10128 if (!result)
10129 goto error;
10131 if (remove_duplicates)
10132 result = isl_map_remove_obvious_duplicates(result);
10133 isl_map_free(map1);
10134 isl_map_free(map2);
10135 return result;
10136 error:
10137 isl_map_free(map1);
10138 isl_map_free(map2);
10139 return NULL;
10142 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10144 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10145 __isl_take isl_map *map2)
10147 return map_product(map1, map2, &isl_space_product,
10148 &isl_basic_map_product, 0);
10151 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10152 __isl_take isl_map *map2)
10154 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10157 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10159 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10160 __isl_take isl_map *map2)
10162 isl_map *prod;
10164 prod = isl_map_product(map1, map2);
10165 prod = isl_map_flatten(prod);
10166 return prod;
10169 /* Given two set A and B, construct its Cartesian product A x B.
10171 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10173 return isl_map_range_product(set1, set2);
10176 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10177 __isl_take isl_set *set2)
10179 return isl_map_flat_range_product(set1, set2);
10182 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10184 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10185 __isl_take isl_map *map2)
10187 return map_product(map1, map2, &isl_space_domain_product,
10188 &isl_basic_map_domain_product, 1);
10191 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10193 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10194 __isl_take isl_map *map2)
10196 return map_product(map1, map2, &isl_space_range_product,
10197 &isl_basic_map_range_product, 1);
10200 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10201 __isl_take isl_map *map2)
10203 return isl_map_align_params_map_map_and(map1, map2,
10204 &map_domain_product_aligned);
10207 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10208 __isl_take isl_map *map2)
10210 return isl_map_align_params_map_map_and(map1, map2,
10211 &map_range_product_aligned);
10214 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10216 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10218 isl_space *space;
10219 int total1, keep1, total2, keep2;
10221 if (!map)
10222 return NULL;
10223 if (!isl_space_domain_is_wrapping(map->dim) ||
10224 !isl_space_range_is_wrapping(map->dim))
10225 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10226 "not a product", return isl_map_free(map));
10228 space = isl_map_get_space(map);
10229 total1 = isl_space_dim(space, isl_dim_in);
10230 total2 = isl_space_dim(space, isl_dim_out);
10231 space = isl_space_factor_domain(space);
10232 keep1 = isl_space_dim(space, isl_dim_in);
10233 keep2 = isl_space_dim(space, isl_dim_out);
10234 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10235 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10236 map = isl_map_reset_space(map, space);
10238 return map;
10241 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10243 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10245 isl_space *space;
10246 int total1, keep1, total2, keep2;
10248 if (!map)
10249 return NULL;
10250 if (!isl_space_domain_is_wrapping(map->dim) ||
10251 !isl_space_range_is_wrapping(map->dim))
10252 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10253 "not a product", return isl_map_free(map));
10255 space = isl_map_get_space(map);
10256 total1 = isl_space_dim(space, isl_dim_in);
10257 total2 = isl_space_dim(space, isl_dim_out);
10258 space = isl_space_factor_range(space);
10259 keep1 = isl_space_dim(space, isl_dim_in);
10260 keep2 = isl_space_dim(space, isl_dim_out);
10261 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10262 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10263 map = isl_map_reset_space(map, space);
10265 return map;
10268 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10270 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10272 isl_space *space;
10273 int total, keep;
10275 if (!map)
10276 return NULL;
10277 if (!isl_space_domain_is_wrapping(map->dim))
10278 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10279 "domain is not a product", return isl_map_free(map));
10281 space = isl_map_get_space(map);
10282 total = isl_space_dim(space, isl_dim_in);
10283 space = isl_space_domain_factor_domain(space);
10284 keep = isl_space_dim(space, isl_dim_in);
10285 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10286 map = isl_map_reset_space(map, space);
10288 return map;
10291 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10293 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10295 isl_space *space;
10296 int total, keep;
10298 if (!map)
10299 return NULL;
10300 if (!isl_space_domain_is_wrapping(map->dim))
10301 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10302 "domain is not a product", return isl_map_free(map));
10304 space = isl_map_get_space(map);
10305 total = isl_space_dim(space, isl_dim_in);
10306 space = isl_space_domain_factor_range(space);
10307 keep = isl_space_dim(space, isl_dim_in);
10308 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10309 map = isl_map_reset_space(map, space);
10311 return map;
10314 /* Given a map A -> [B -> C], extract the map A -> B.
10316 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10318 isl_space *space;
10319 int total, keep;
10321 if (!map)
10322 return NULL;
10323 if (!isl_space_range_is_wrapping(map->dim))
10324 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10325 "range is not a product", return isl_map_free(map));
10327 space = isl_map_get_space(map);
10328 total = isl_space_dim(space, isl_dim_out);
10329 space = isl_space_range_factor_domain(space);
10330 keep = isl_space_dim(space, isl_dim_out);
10331 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10332 map = isl_map_reset_space(map, space);
10334 return map;
10337 /* Given a map A -> [B -> C], extract the map A -> C.
10339 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10341 isl_space *space;
10342 int total, keep;
10344 if (!map)
10345 return NULL;
10346 if (!isl_space_range_is_wrapping(map->dim))
10347 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10348 "range is not a product", return isl_map_free(map));
10350 space = isl_map_get_space(map);
10351 total = isl_space_dim(space, isl_dim_out);
10352 space = isl_space_range_factor_range(space);
10353 keep = isl_space_dim(space, isl_dim_out);
10354 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10355 map = isl_map_reset_space(map, space);
10357 return map;
10360 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10362 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10363 __isl_take isl_map *map2)
10365 isl_map *prod;
10367 prod = isl_map_domain_product(map1, map2);
10368 prod = isl_map_flatten_domain(prod);
10369 return prod;
10372 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10374 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10375 __isl_take isl_map *map2)
10377 isl_map *prod;
10379 prod = isl_map_range_product(map1, map2);
10380 prod = isl_map_flatten_range(prod);
10381 return prod;
10384 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10386 int i;
10387 uint32_t hash = isl_hash_init();
10388 unsigned total;
10390 if (!bmap)
10391 return 0;
10392 bmap = isl_basic_map_copy(bmap);
10393 bmap = isl_basic_map_normalize(bmap);
10394 if (!bmap)
10395 return 0;
10396 total = isl_basic_map_total_dim(bmap);
10397 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10398 for (i = 0; i < bmap->n_eq; ++i) {
10399 uint32_t c_hash;
10400 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10401 isl_hash_hash(hash, c_hash);
10403 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10404 for (i = 0; i < bmap->n_ineq; ++i) {
10405 uint32_t c_hash;
10406 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10407 isl_hash_hash(hash, c_hash);
10409 isl_hash_byte(hash, bmap->n_div & 0xFF);
10410 for (i = 0; i < bmap->n_div; ++i) {
10411 uint32_t c_hash;
10412 if (isl_int_is_zero(bmap->div[i][0]))
10413 continue;
10414 isl_hash_byte(hash, i & 0xFF);
10415 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10416 isl_hash_hash(hash, c_hash);
10418 isl_basic_map_free(bmap);
10419 return hash;
10422 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10424 return isl_basic_map_get_hash(bset_to_bmap(bset));
10427 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10429 int i;
10430 uint32_t hash;
10432 if (!map)
10433 return 0;
10434 map = isl_map_copy(map);
10435 map = isl_map_normalize(map);
10436 if (!map)
10437 return 0;
10439 hash = isl_hash_init();
10440 for (i = 0; i < map->n; ++i) {
10441 uint32_t bmap_hash;
10442 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10443 isl_hash_hash(hash, bmap_hash);
10446 isl_map_free(map);
10448 return hash;
10451 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10453 return isl_map_get_hash(set_to_map(set));
10456 /* Return the number of basic maps in the (current) representation of "map".
10458 int isl_map_n_basic_map(__isl_keep isl_map *map)
10460 return map ? map->n : 0;
10463 int isl_set_n_basic_set(__isl_keep isl_set *set)
10465 return set ? set->n : 0;
10468 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10469 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10471 int i;
10473 if (!map)
10474 return isl_stat_error;
10476 for (i = 0; i < map->n; ++i)
10477 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10478 return isl_stat_error;
10480 return isl_stat_ok;
10483 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10484 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10486 int i;
10488 if (!set)
10489 return isl_stat_error;
10491 for (i = 0; i < set->n; ++i)
10492 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10493 return isl_stat_error;
10495 return isl_stat_ok;
10498 /* Return a list of basic sets, the union of which is equal to "set".
10500 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10501 __isl_keep isl_set *set)
10503 int i;
10504 isl_basic_set_list *list;
10506 if (!set)
10507 return NULL;
10509 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10510 for (i = 0; i < set->n; ++i) {
10511 isl_basic_set *bset;
10513 bset = isl_basic_set_copy(set->p[i]);
10514 list = isl_basic_set_list_add(list, bset);
10517 return list;
10520 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10522 isl_space *space;
10524 if (!bset)
10525 return NULL;
10527 bset = isl_basic_set_cow(bset);
10528 if (!bset)
10529 return NULL;
10531 space = isl_basic_set_get_space(bset);
10532 space = isl_space_lift(space, bset->n_div);
10533 if (!space)
10534 goto error;
10535 isl_space_free(bset->dim);
10536 bset->dim = space;
10537 bset->extra -= bset->n_div;
10538 bset->n_div = 0;
10540 bset = isl_basic_set_finalize(bset);
10542 return bset;
10543 error:
10544 isl_basic_set_free(bset);
10545 return NULL;
10548 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10550 int i;
10551 isl_space *space;
10552 unsigned n_div;
10554 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10556 if (!set)
10557 return NULL;
10559 set = isl_set_cow(set);
10560 if (!set)
10561 return NULL;
10563 n_div = set->p[0]->n_div;
10564 space = isl_set_get_space(set);
10565 space = isl_space_lift(space, n_div);
10566 if (!space)
10567 goto error;
10568 isl_space_free(set->dim);
10569 set->dim = space;
10571 for (i = 0; i < set->n; ++i) {
10572 set->p[i] = isl_basic_set_lift(set->p[i]);
10573 if (!set->p[i])
10574 goto error;
10577 return set;
10578 error:
10579 isl_set_free(set);
10580 return NULL;
10583 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10585 unsigned dim;
10586 int size = 0;
10588 if (!bset)
10589 return -1;
10591 dim = isl_basic_set_total_dim(bset);
10592 size += bset->n_eq * (1 + dim);
10593 size += bset->n_ineq * (1 + dim);
10594 size += bset->n_div * (2 + dim);
10596 return size;
10599 int isl_set_size(__isl_keep isl_set *set)
10601 int i;
10602 int size = 0;
10604 if (!set)
10605 return -1;
10607 for (i = 0; i < set->n; ++i)
10608 size += isl_basic_set_size(set->p[i]);
10610 return size;
10613 /* Check if there is any lower bound (if lower == 0) and/or upper
10614 * bound (if upper == 0) on the specified dim.
10616 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10617 enum isl_dim_type type, unsigned pos, int lower, int upper)
10619 int i;
10621 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10622 return isl_bool_error;
10624 pos += isl_basic_map_offset(bmap, type);
10626 for (i = 0; i < bmap->n_div; ++i) {
10627 if (isl_int_is_zero(bmap->div[i][0]))
10628 continue;
10629 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10630 return isl_bool_true;
10633 for (i = 0; i < bmap->n_eq; ++i)
10634 if (!isl_int_is_zero(bmap->eq[i][pos]))
10635 return isl_bool_true;
10637 for (i = 0; i < bmap->n_ineq; ++i) {
10638 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10639 if (sgn > 0)
10640 lower = 1;
10641 if (sgn < 0)
10642 upper = 1;
10645 return lower && upper;
10648 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10649 enum isl_dim_type type, unsigned pos)
10651 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10654 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10655 enum isl_dim_type type, unsigned pos)
10657 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10660 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10661 enum isl_dim_type type, unsigned pos)
10663 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10666 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10667 enum isl_dim_type type, unsigned pos)
10669 int i;
10671 if (!map)
10672 return isl_bool_error;
10674 for (i = 0; i < map->n; ++i) {
10675 isl_bool bounded;
10676 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10677 if (bounded < 0 || !bounded)
10678 return bounded;
10681 return isl_bool_true;
10684 /* Return true if the specified dim is involved in both an upper bound
10685 * and a lower bound.
10687 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10688 enum isl_dim_type type, unsigned pos)
10690 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10693 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10695 static isl_bool has_any_bound(__isl_keep isl_map *map,
10696 enum isl_dim_type type, unsigned pos,
10697 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10698 enum isl_dim_type type, unsigned pos))
10700 int i;
10702 if (!map)
10703 return isl_bool_error;
10705 for (i = 0; i < map->n; ++i) {
10706 isl_bool bounded;
10707 bounded = fn(map->p[i], type, pos);
10708 if (bounded < 0 || bounded)
10709 return bounded;
10712 return isl_bool_false;
10715 /* Return 1 if the specified dim is involved in any lower bound.
10717 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10718 enum isl_dim_type type, unsigned pos)
10720 return has_any_bound(set, type, pos,
10721 &isl_basic_map_dim_has_lower_bound);
10724 /* Return 1 if the specified dim is involved in any upper bound.
10726 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10727 enum isl_dim_type type, unsigned pos)
10729 return has_any_bound(set, type, pos,
10730 &isl_basic_map_dim_has_upper_bound);
10733 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10735 static isl_bool has_bound(__isl_keep isl_map *map,
10736 enum isl_dim_type type, unsigned pos,
10737 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10738 enum isl_dim_type type, unsigned pos))
10740 int i;
10742 if (!map)
10743 return isl_bool_error;
10745 for (i = 0; i < map->n; ++i) {
10746 isl_bool bounded;
10747 bounded = fn(map->p[i], type, pos);
10748 if (bounded < 0 || !bounded)
10749 return bounded;
10752 return isl_bool_true;
10755 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10757 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10758 enum isl_dim_type type, unsigned pos)
10760 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10763 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10765 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10766 enum isl_dim_type type, unsigned pos)
10768 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10771 /* For each of the "n" variables starting at "first", determine
10772 * the sign of the variable and put the results in the first "n"
10773 * elements of the array "signs".
10774 * Sign
10775 * 1 means that the variable is non-negative
10776 * -1 means that the variable is non-positive
10777 * 0 means the variable attains both positive and negative values.
10779 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10780 unsigned first, unsigned n, int *signs)
10782 isl_vec *bound = NULL;
10783 struct isl_tab *tab = NULL;
10784 struct isl_tab_undo *snap;
10785 int i;
10787 if (!bset || !signs)
10788 return isl_stat_error;
10790 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10791 tab = isl_tab_from_basic_set(bset, 0);
10792 if (!bound || !tab)
10793 goto error;
10795 isl_seq_clr(bound->el, bound->size);
10796 isl_int_set_si(bound->el[0], -1);
10798 snap = isl_tab_snap(tab);
10799 for (i = 0; i < n; ++i) {
10800 int empty;
10802 isl_int_set_si(bound->el[1 + first + i], -1);
10803 if (isl_tab_add_ineq(tab, bound->el) < 0)
10804 goto error;
10805 empty = tab->empty;
10806 isl_int_set_si(bound->el[1 + first + i], 0);
10807 if (isl_tab_rollback(tab, snap) < 0)
10808 goto error;
10810 if (empty) {
10811 signs[i] = 1;
10812 continue;
10815 isl_int_set_si(bound->el[1 + first + i], 1);
10816 if (isl_tab_add_ineq(tab, bound->el) < 0)
10817 goto error;
10818 empty = tab->empty;
10819 isl_int_set_si(bound->el[1 + first + i], 0);
10820 if (isl_tab_rollback(tab, snap) < 0)
10821 goto error;
10823 signs[i] = empty ? -1 : 0;
10826 isl_tab_free(tab);
10827 isl_vec_free(bound);
10828 return isl_stat_ok;
10829 error:
10830 isl_tab_free(tab);
10831 isl_vec_free(bound);
10832 return isl_stat_error;
10835 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10836 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10838 if (!bset || !signs)
10839 return isl_stat_error;
10840 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10841 return isl_stat_error;
10843 first += pos(bset->dim, type) - 1;
10844 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10847 /* Is it possible for the integer division "div" to depend (possibly
10848 * indirectly) on any output dimensions?
10850 * If the div is undefined, then we conservatively assume that it
10851 * may depend on them.
10852 * Otherwise, we check if it actually depends on them or on any integer
10853 * divisions that may depend on them.
10855 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10857 int i;
10858 unsigned n_out, o_out;
10859 unsigned n_div, o_div;
10861 if (isl_int_is_zero(bmap->div[div][0]))
10862 return isl_bool_true;
10864 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10865 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10867 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10868 return isl_bool_true;
10870 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10871 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10873 for (i = 0; i < n_div; ++i) {
10874 isl_bool may_involve;
10876 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10877 continue;
10878 may_involve = div_may_involve_output(bmap, i);
10879 if (may_involve < 0 || may_involve)
10880 return may_involve;
10883 return isl_bool_false;
10886 /* Return the first integer division of "bmap" in the range
10887 * [first, first + n[ that may depend on any output dimensions and
10888 * that has a non-zero coefficient in "c" (where the first coefficient
10889 * in "c" corresponds to integer division "first").
10891 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10892 isl_int *c, int first, int n)
10894 int k;
10896 if (!bmap)
10897 return -1;
10899 for (k = first; k < first + n; ++k) {
10900 isl_bool may_involve;
10902 if (isl_int_is_zero(c[k]))
10903 continue;
10904 may_involve = div_may_involve_output(bmap, k);
10905 if (may_involve < 0)
10906 return -1;
10907 if (may_involve)
10908 return k;
10911 return first + n;
10914 /* Look for a pair of inequality constraints in "bmap" of the form
10916 * -l + i >= 0 or i >= l
10917 * and
10918 * n + l - i >= 0 or i <= l + n
10920 * with n < "m" and i the output dimension at position "pos".
10921 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10922 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10923 * and earlier output dimensions, as well as integer divisions that do
10924 * not involve any of the output dimensions.
10926 * Return the index of the first inequality constraint or bmap->n_ineq
10927 * if no such pair can be found.
10929 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10930 int pos, isl_int m)
10932 int i, j;
10933 isl_ctx *ctx;
10934 unsigned total;
10935 unsigned n_div, o_div;
10936 unsigned n_out, o_out;
10937 int less;
10939 if (!bmap)
10940 return -1;
10942 ctx = isl_basic_map_get_ctx(bmap);
10943 total = isl_basic_map_total_dim(bmap);
10944 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10945 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10946 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10947 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10948 for (i = 0; i < bmap->n_ineq; ++i) {
10949 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10950 continue;
10951 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10952 n_out - (pos + 1)) != -1)
10953 continue;
10954 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10955 0, n_div) < n_div)
10956 continue;
10957 for (j = i + 1; j < bmap->n_ineq; ++j) {
10958 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10959 ctx->one))
10960 continue;
10961 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10962 bmap->ineq[j] + 1, total))
10963 continue;
10964 break;
10966 if (j >= bmap->n_ineq)
10967 continue;
10968 isl_int_add(bmap->ineq[i][0],
10969 bmap->ineq[i][0], bmap->ineq[j][0]);
10970 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10971 isl_int_sub(bmap->ineq[i][0],
10972 bmap->ineq[i][0], bmap->ineq[j][0]);
10973 if (!less)
10974 continue;
10975 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10976 return i;
10977 else
10978 return j;
10981 return bmap->n_ineq;
10984 /* Return the index of the equality of "bmap" that defines
10985 * the output dimension "pos" in terms of earlier dimensions.
10986 * The equality may also involve integer divisions, as long
10987 * as those integer divisions are defined in terms of
10988 * parameters or input dimensions.
10989 * In this case, *div is set to the number of integer divisions and
10990 * *ineq is set to the number of inequality constraints (provided
10991 * div and ineq are not NULL).
10993 * The equality may also involve a single integer division involving
10994 * the output dimensions (typically only output dimension "pos") as
10995 * long as the coefficient of output dimension "pos" is 1 or -1 and
10996 * there is a pair of constraints i >= l and i <= l + n, with i referring
10997 * to output dimension "pos", l an expression involving only earlier
10998 * dimensions and n smaller than the coefficient of the integer division
10999 * in the equality. In this case, the output dimension can be defined
11000 * in terms of a modulo expression that does not involve the integer division.
11001 * *div is then set to this single integer division and
11002 * *ineq is set to the index of constraint i >= l.
11004 * Return bmap->n_eq if there is no such equality.
11005 * Return -1 on error.
11007 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11008 int pos, int *div, int *ineq)
11010 int j, k, l;
11011 unsigned n_out, o_out;
11012 unsigned n_div, o_div;
11014 if (!bmap)
11015 return -1;
11017 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11018 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11019 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11020 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11022 if (ineq)
11023 *ineq = bmap->n_ineq;
11024 if (div)
11025 *div = n_div;
11026 for (j = 0; j < bmap->n_eq; ++j) {
11027 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11028 continue;
11029 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11030 n_out - (pos + 1)) != -1)
11031 continue;
11032 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11033 0, n_div);
11034 if (k >= n_div)
11035 return j;
11036 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11037 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11038 continue;
11039 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11040 k + 1, n_div - (k+1)) < n_div)
11041 continue;
11042 l = find_modulo_constraint_pair(bmap, pos,
11043 bmap->eq[j][o_div + k]);
11044 if (l < 0)
11045 return -1;
11046 if (l >= bmap->n_ineq)
11047 continue;
11048 if (div)
11049 *div = k;
11050 if (ineq)
11051 *ineq = l;
11052 return j;
11055 return bmap->n_eq;
11058 /* Check if the given basic map is obviously single-valued.
11059 * In particular, for each output dimension, check that there is
11060 * an equality that defines the output dimension in terms of
11061 * earlier dimensions.
11063 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11065 int i;
11066 unsigned n_out;
11068 if (!bmap)
11069 return isl_bool_error;
11071 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11073 for (i = 0; i < n_out; ++i) {
11074 int eq;
11076 eq = isl_basic_map_output_defining_equality(bmap, i,
11077 NULL, NULL);
11078 if (eq < 0)
11079 return isl_bool_error;
11080 if (eq >= bmap->n_eq)
11081 return isl_bool_false;
11084 return isl_bool_true;
11087 /* Check if the given basic map is single-valued.
11088 * We simply compute
11090 * M \circ M^-1
11092 * and check if the result is a subset of the identity mapping.
11094 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11096 isl_space *space;
11097 isl_basic_map *test;
11098 isl_basic_map *id;
11099 isl_bool sv;
11101 sv = isl_basic_map_plain_is_single_valued(bmap);
11102 if (sv < 0 || sv)
11103 return sv;
11105 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11106 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11108 space = isl_basic_map_get_space(bmap);
11109 space = isl_space_map_from_set(isl_space_range(space));
11110 id = isl_basic_map_identity(space);
11112 sv = isl_basic_map_is_subset(test, id);
11114 isl_basic_map_free(test);
11115 isl_basic_map_free(id);
11117 return sv;
11120 /* Check if the given map is obviously single-valued.
11122 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11124 if (!map)
11125 return isl_bool_error;
11126 if (map->n == 0)
11127 return isl_bool_true;
11128 if (map->n >= 2)
11129 return isl_bool_false;
11131 return isl_basic_map_plain_is_single_valued(map->p[0]);
11134 /* Check if the given map is single-valued.
11135 * We simply compute
11137 * M \circ M^-1
11139 * and check if the result is a subset of the identity mapping.
11141 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11143 isl_space *dim;
11144 isl_map *test;
11145 isl_map *id;
11146 isl_bool sv;
11148 sv = isl_map_plain_is_single_valued(map);
11149 if (sv < 0 || sv)
11150 return sv;
11152 test = isl_map_reverse(isl_map_copy(map));
11153 test = isl_map_apply_range(test, isl_map_copy(map));
11155 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11156 id = isl_map_identity(dim);
11158 sv = isl_map_is_subset(test, id);
11160 isl_map_free(test);
11161 isl_map_free(id);
11163 return sv;
11166 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11168 isl_bool in;
11170 map = isl_map_copy(map);
11171 map = isl_map_reverse(map);
11172 in = isl_map_is_single_valued(map);
11173 isl_map_free(map);
11175 return in;
11178 /* Check if the given map is obviously injective.
11180 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11182 isl_bool in;
11184 map = isl_map_copy(map);
11185 map = isl_map_reverse(map);
11186 in = isl_map_plain_is_single_valued(map);
11187 isl_map_free(map);
11189 return in;
11192 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11194 isl_bool sv;
11196 sv = isl_map_is_single_valued(map);
11197 if (sv < 0 || !sv)
11198 return sv;
11200 return isl_map_is_injective(map);
11203 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11205 return isl_map_is_single_valued(set_to_map(set));
11208 /* Does "map" only map elements to themselves?
11210 * If the domain and range spaces are different, then "map"
11211 * is considered not to be an identity relation, even if it is empty.
11212 * Otherwise, construct the maximal identity relation and
11213 * check whether "map" is a subset of this relation.
11215 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11217 isl_space *space;
11218 isl_map *id;
11219 isl_bool equal, is_identity;
11221 space = isl_map_get_space(map);
11222 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11223 isl_space_free(space);
11224 if (equal < 0 || !equal)
11225 return equal;
11227 id = isl_map_identity(isl_map_get_space(map));
11228 is_identity = isl_map_is_subset(map, id);
11229 isl_map_free(id);
11231 return is_identity;
11234 int isl_map_is_translation(__isl_keep isl_map *map)
11236 int ok;
11237 isl_set *delta;
11239 delta = isl_map_deltas(isl_map_copy(map));
11240 ok = isl_set_is_singleton(delta);
11241 isl_set_free(delta);
11243 return ok;
11246 static int unique(isl_int *p, unsigned pos, unsigned len)
11248 if (isl_seq_first_non_zero(p, pos) != -1)
11249 return 0;
11250 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11251 return 0;
11252 return 1;
11255 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11257 int i, j;
11258 unsigned nvar;
11259 unsigned ovar;
11261 if (!bset)
11262 return isl_bool_error;
11264 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11265 return isl_bool_false;
11267 nvar = isl_basic_set_dim(bset, isl_dim_set);
11268 ovar = isl_space_offset(bset->dim, isl_dim_set);
11269 for (j = 0; j < nvar; ++j) {
11270 int lower = 0, upper = 0;
11271 for (i = 0; i < bset->n_eq; ++i) {
11272 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11273 continue;
11274 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11275 return isl_bool_false;
11276 break;
11278 if (i < bset->n_eq)
11279 continue;
11280 for (i = 0; i < bset->n_ineq; ++i) {
11281 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11282 continue;
11283 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11284 return isl_bool_false;
11285 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11286 lower = 1;
11287 else
11288 upper = 1;
11290 if (!lower || !upper)
11291 return isl_bool_false;
11294 return isl_bool_true;
11297 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11299 if (!set)
11300 return isl_bool_error;
11301 if (set->n != 1)
11302 return isl_bool_false;
11304 return isl_basic_set_is_box(set->p[0]);
11307 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11309 if (!bset)
11310 return isl_bool_error;
11312 return isl_space_is_wrapping(bset->dim);
11315 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11317 if (!set)
11318 return isl_bool_error;
11320 return isl_space_is_wrapping(set->dim);
11323 /* Modify the space of "map" through a call to "change".
11324 * If "can_change" is set (not NULL), then first call it to check
11325 * if the modification is allowed, printing the error message "cannot_change"
11326 * if it is not.
11328 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11329 isl_bool (*can_change)(__isl_keep isl_map *map),
11330 const char *cannot_change,
11331 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11333 isl_bool ok;
11334 isl_space *space;
11336 if (!map)
11337 return NULL;
11339 ok = can_change ? can_change(map) : isl_bool_true;
11340 if (ok < 0)
11341 return isl_map_free(map);
11342 if (!ok)
11343 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11344 return isl_map_free(map));
11346 space = change(isl_map_get_space(map));
11347 map = isl_map_reset_space(map, space);
11349 return map;
11352 /* Is the domain of "map" a wrapped relation?
11354 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11356 if (!map)
11357 return isl_bool_error;
11359 return isl_space_domain_is_wrapping(map->dim);
11362 /* Does "map" have a wrapped relation in both domain and range?
11364 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11366 return isl_space_is_product(isl_map_peek_space(map));
11369 /* Is the range of "map" a wrapped relation?
11371 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11373 if (!map)
11374 return isl_bool_error;
11376 return isl_space_range_is_wrapping(map->dim);
11379 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11381 bmap = isl_basic_map_cow(bmap);
11382 if (!bmap)
11383 return NULL;
11385 bmap->dim = isl_space_wrap(bmap->dim);
11386 if (!bmap->dim)
11387 goto error;
11389 bmap = isl_basic_map_finalize(bmap);
11391 return bset_from_bmap(bmap);
11392 error:
11393 isl_basic_map_free(bmap);
11394 return NULL;
11397 /* Given a map A -> B, return the set (A -> B).
11399 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11401 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11404 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11406 bset = isl_basic_set_cow(bset);
11407 if (!bset)
11408 return NULL;
11410 bset->dim = isl_space_unwrap(bset->dim);
11411 if (!bset->dim)
11412 goto error;
11414 bset = isl_basic_set_finalize(bset);
11416 return bset_to_bmap(bset);
11417 error:
11418 isl_basic_set_free(bset);
11419 return NULL;
11422 /* Given a set (A -> B), return the map A -> B.
11423 * Error out if "set" is not of the form (A -> B).
11425 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11427 return isl_map_change_space(set, &isl_set_is_wrapping,
11428 "not a wrapping set", &isl_space_unwrap);
11431 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11432 enum isl_dim_type type)
11434 if (!bmap)
11435 return NULL;
11437 if (!isl_space_is_named_or_nested(bmap->dim, type))
11438 return bmap;
11440 bmap = isl_basic_map_cow(bmap);
11441 if (!bmap)
11442 return NULL;
11444 bmap->dim = isl_space_reset(bmap->dim, type);
11445 if (!bmap->dim)
11446 goto error;
11448 bmap = isl_basic_map_finalize(bmap);
11450 return bmap;
11451 error:
11452 isl_basic_map_free(bmap);
11453 return NULL;
11456 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11457 enum isl_dim_type type)
11459 int i;
11461 if (!map)
11462 return NULL;
11464 if (!isl_space_is_named_or_nested(map->dim, type))
11465 return map;
11467 map = isl_map_cow(map);
11468 if (!map)
11469 return NULL;
11471 for (i = 0; i < map->n; ++i) {
11472 map->p[i] = isl_basic_map_reset(map->p[i], type);
11473 if (!map->p[i])
11474 goto error;
11476 map->dim = isl_space_reset(map->dim, type);
11477 if (!map->dim)
11478 goto error;
11480 return map;
11481 error:
11482 isl_map_free(map);
11483 return NULL;
11486 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11488 if (!bmap)
11489 return NULL;
11491 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11492 return bmap;
11494 bmap = isl_basic_map_cow(bmap);
11495 if (!bmap)
11496 return NULL;
11498 bmap->dim = isl_space_flatten(bmap->dim);
11499 if (!bmap->dim)
11500 goto error;
11502 bmap = isl_basic_map_finalize(bmap);
11504 return bmap;
11505 error:
11506 isl_basic_map_free(bmap);
11507 return NULL;
11510 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11512 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11515 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11516 __isl_take isl_basic_map *bmap)
11518 if (!bmap)
11519 return NULL;
11521 if (!bmap->dim->nested[0])
11522 return bmap;
11524 bmap = isl_basic_map_cow(bmap);
11525 if (!bmap)
11526 return NULL;
11528 bmap->dim = isl_space_flatten_domain(bmap->dim);
11529 if (!bmap->dim)
11530 goto error;
11532 bmap = isl_basic_map_finalize(bmap);
11534 return bmap;
11535 error:
11536 isl_basic_map_free(bmap);
11537 return NULL;
11540 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11541 __isl_take isl_basic_map *bmap)
11543 if (!bmap)
11544 return NULL;
11546 if (!bmap->dim->nested[1])
11547 return bmap;
11549 bmap = isl_basic_map_cow(bmap);
11550 if (!bmap)
11551 return NULL;
11553 bmap->dim = isl_space_flatten_range(bmap->dim);
11554 if (!bmap->dim)
11555 goto error;
11557 bmap = isl_basic_map_finalize(bmap);
11559 return bmap;
11560 error:
11561 isl_basic_map_free(bmap);
11562 return NULL;
11565 /* Remove any internal structure from the spaces of domain and range of "map".
11567 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11569 if (!map)
11570 return NULL;
11572 if (!map->dim->nested[0] && !map->dim->nested[1])
11573 return map;
11575 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11578 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11580 return set_from_map(isl_map_flatten(set_to_map(set)));
11583 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11585 isl_space *space, *flat_space;
11586 isl_map *map;
11588 space = isl_set_get_space(set);
11589 flat_space = isl_space_flatten(isl_space_copy(space));
11590 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11591 flat_space));
11592 map = isl_map_intersect_domain(map, set);
11594 return map;
11597 /* Remove any internal structure from the space of the domain of "map".
11599 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11601 if (!map)
11602 return NULL;
11604 if (!map->dim->nested[0])
11605 return map;
11607 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11610 /* Remove any internal structure from the space of the range of "map".
11612 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11614 if (!map)
11615 return NULL;
11617 if (!map->dim->nested[1])
11618 return map;
11620 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11623 /* Reorder the dimensions of "bmap" according to the given dim_map
11624 * and set the dimension specification to "space" and
11625 * perform Gaussian elimination on the result.
11627 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11628 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11630 isl_basic_map *res;
11631 unsigned flags;
11632 unsigned n_div;
11634 if (!bmap || !space || !dim_map)
11635 goto error;
11637 flags = bmap->flags;
11638 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11639 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11640 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11641 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11642 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11643 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11644 if (res)
11645 res->flags = flags;
11646 res = isl_basic_map_gauss(res, NULL);
11647 res = isl_basic_map_finalize(res);
11648 return res;
11649 error:
11650 isl_dim_map_free(dim_map);
11651 isl_basic_map_free(bmap);
11652 isl_space_free(space);
11653 return NULL;
11656 /* Reorder the dimensions of "map" according to given reordering.
11658 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11659 __isl_take isl_reordering *r)
11661 int i;
11662 struct isl_dim_map *dim_map;
11664 map = isl_map_cow(map);
11665 dim_map = isl_dim_map_from_reordering(r);
11666 if (!map || !r || !dim_map)
11667 goto error;
11669 for (i = 0; i < map->n; ++i) {
11670 struct isl_dim_map *dim_map_i;
11671 isl_space *space;
11673 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11675 space = isl_reordering_get_space(r);
11676 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11678 if (!map->p[i])
11679 goto error;
11682 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11683 map = isl_map_unmark_normalized(map);
11685 isl_reordering_free(r);
11686 isl_dim_map_free(dim_map);
11687 return map;
11688 error:
11689 isl_dim_map_free(dim_map);
11690 isl_map_free(map);
11691 isl_reordering_free(r);
11692 return NULL;
11695 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11696 __isl_take isl_reordering *r)
11698 return set_from_map(isl_map_realign(set_to_map(set), r));
11701 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11702 __isl_take isl_space *model)
11704 isl_ctx *ctx;
11705 isl_bool aligned;
11707 if (!map || !model)
11708 goto error;
11710 ctx = isl_space_get_ctx(model);
11711 if (!isl_space_has_named_params(model))
11712 isl_die(ctx, isl_error_invalid,
11713 "model has unnamed parameters", goto error);
11714 if (isl_map_check_named_params(map) < 0)
11715 goto error;
11716 aligned = isl_map_space_has_equal_params(map, model);
11717 if (aligned < 0)
11718 goto error;
11719 if (!aligned) {
11720 isl_reordering *exp;
11722 exp = isl_parameter_alignment_reordering(map->dim, model);
11723 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11724 map = isl_map_realign(map, exp);
11727 isl_space_free(model);
11728 return map;
11729 error:
11730 isl_space_free(model);
11731 isl_map_free(map);
11732 return NULL;
11735 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11736 __isl_take isl_space *model)
11738 return isl_map_align_params(set, model);
11741 /* Align the parameters of "bmap" to those of "model", introducing
11742 * additional parameters if needed.
11744 __isl_give isl_basic_map *isl_basic_map_align_params(
11745 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11747 isl_ctx *ctx;
11748 isl_bool equal_params;
11750 if (!bmap || !model)
11751 goto error;
11753 ctx = isl_space_get_ctx(model);
11754 if (!isl_space_has_named_params(model))
11755 isl_die(ctx, isl_error_invalid,
11756 "model has unnamed parameters", goto error);
11757 if (isl_basic_map_check_named_params(bmap) < 0)
11758 goto error;
11759 equal_params = isl_space_has_equal_params(bmap->dim, model);
11760 if (equal_params < 0)
11761 goto error;
11762 if (!equal_params) {
11763 isl_reordering *exp;
11764 struct isl_dim_map *dim_map;
11766 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11767 exp = isl_reordering_extend_space(exp,
11768 isl_basic_map_get_space(bmap));
11769 dim_map = isl_dim_map_from_reordering(exp);
11770 bmap = isl_basic_map_realign(bmap,
11771 isl_reordering_get_space(exp),
11772 isl_dim_map_extend(dim_map, bmap));
11773 isl_reordering_free(exp);
11774 isl_dim_map_free(dim_map);
11777 isl_space_free(model);
11778 return bmap;
11779 error:
11780 isl_space_free(model);
11781 isl_basic_map_free(bmap);
11782 return NULL;
11785 /* Do "bset" and "space" have the same parameters?
11787 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11788 __isl_keep isl_space *space)
11790 isl_space *bset_space;
11792 bset_space = isl_basic_set_peek_space(bset);
11793 return isl_space_has_equal_params(bset_space, space);
11796 /* Do "map" and "space" have the same parameters?
11798 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11799 __isl_keep isl_space *space)
11801 isl_space *map_space;
11803 map_space = isl_map_peek_space(map);
11804 return isl_space_has_equal_params(map_space, space);
11807 /* Do "set" and "space" have the same parameters?
11809 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11810 __isl_keep isl_space *space)
11812 return isl_map_space_has_equal_params(set_to_map(set), space);
11815 /* Align the parameters of "bset" to those of "model", introducing
11816 * additional parameters if needed.
11818 __isl_give isl_basic_set *isl_basic_set_align_params(
11819 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11821 return isl_basic_map_align_params(bset, model);
11824 /* Drop all parameters not referenced by "map".
11826 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11828 int i;
11830 if (isl_map_check_named_params(map) < 0)
11831 return isl_map_free(map);
11833 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11834 isl_bool involves;
11836 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11837 if (involves < 0)
11838 return isl_map_free(map);
11839 if (!involves)
11840 map = isl_map_project_out(map, isl_dim_param, i, 1);
11843 return map;
11846 /* Drop all parameters not referenced by "set".
11848 __isl_give isl_set *isl_set_drop_unused_params(
11849 __isl_take isl_set *set)
11851 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11854 /* Drop all parameters not referenced by "bmap".
11856 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11857 __isl_take isl_basic_map *bmap)
11859 int i;
11861 if (isl_basic_map_check_named_params(bmap) < 0)
11862 return isl_basic_map_free(bmap);
11864 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11865 isl_bool involves;
11867 involves = isl_basic_map_involves_dims(bmap,
11868 isl_dim_param, i, 1);
11869 if (involves < 0)
11870 return isl_basic_map_free(bmap);
11871 if (!involves)
11872 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11875 return bmap;
11878 /* Drop all parameters not referenced by "bset".
11880 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11881 __isl_take isl_basic_set *bset)
11883 return bset_from_bmap(isl_basic_map_drop_unused_params(
11884 bset_to_bmap(bset)));
11887 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11888 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11889 enum isl_dim_type c2, enum isl_dim_type c3,
11890 enum isl_dim_type c4, enum isl_dim_type c5)
11892 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11893 struct isl_mat *mat;
11894 int i, j, k;
11895 int pos;
11897 if (!bmap)
11898 return NULL;
11899 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11900 isl_basic_map_total_dim(bmap) + 1);
11901 if (!mat)
11902 return NULL;
11903 for (i = 0; i < bmap->n_eq; ++i)
11904 for (j = 0, pos = 0; j < 5; ++j) {
11905 int off = isl_basic_map_offset(bmap, c[j]);
11906 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11907 isl_int_set(mat->row[i][pos],
11908 bmap->eq[i][off + k]);
11909 ++pos;
11913 return mat;
11916 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11917 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11918 enum isl_dim_type c2, enum isl_dim_type c3,
11919 enum isl_dim_type c4, enum isl_dim_type c5)
11921 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11922 struct isl_mat *mat;
11923 int i, j, k;
11924 int pos;
11926 if (!bmap)
11927 return NULL;
11928 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11929 isl_basic_map_total_dim(bmap) + 1);
11930 if (!mat)
11931 return NULL;
11932 for (i = 0; i < bmap->n_ineq; ++i)
11933 for (j = 0, pos = 0; j < 5; ++j) {
11934 int off = isl_basic_map_offset(bmap, c[j]);
11935 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11936 isl_int_set(mat->row[i][pos],
11937 bmap->ineq[i][off + k]);
11938 ++pos;
11942 return mat;
11945 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11946 __isl_take isl_space *space,
11947 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11948 enum isl_dim_type c2, enum isl_dim_type c3,
11949 enum isl_dim_type c4, enum isl_dim_type c5)
11951 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11952 isl_basic_map *bmap = NULL;
11953 unsigned total;
11954 unsigned extra;
11955 int i, j, k, l;
11956 int pos;
11958 if (!space || !eq || !ineq)
11959 goto error;
11961 if (eq->n_col != ineq->n_col)
11962 isl_die(space->ctx, isl_error_invalid,
11963 "equalities and inequalities matrices should have "
11964 "same number of columns", goto error);
11966 total = 1 + isl_space_dim(space, isl_dim_all);
11968 if (eq->n_col < total)
11969 isl_die(space->ctx, isl_error_invalid,
11970 "number of columns too small", goto error);
11972 extra = eq->n_col - total;
11974 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11975 eq->n_row, ineq->n_row);
11976 if (!bmap)
11977 goto error;
11978 for (i = 0; i < extra; ++i) {
11979 k = isl_basic_map_alloc_div(bmap);
11980 if (k < 0)
11981 goto error;
11982 isl_int_set_si(bmap->div[k][0], 0);
11984 for (i = 0; i < eq->n_row; ++i) {
11985 l = isl_basic_map_alloc_equality(bmap);
11986 if (l < 0)
11987 goto error;
11988 for (j = 0, pos = 0; j < 5; ++j) {
11989 int off = isl_basic_map_offset(bmap, c[j]);
11990 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11991 isl_int_set(bmap->eq[l][off + k],
11992 eq->row[i][pos]);
11993 ++pos;
11997 for (i = 0; i < ineq->n_row; ++i) {
11998 l = isl_basic_map_alloc_inequality(bmap);
11999 if (l < 0)
12000 goto error;
12001 for (j = 0, pos = 0; j < 5; ++j) {
12002 int off = isl_basic_map_offset(bmap, c[j]);
12003 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
12004 isl_int_set(bmap->ineq[l][off + k],
12005 ineq->row[i][pos]);
12006 ++pos;
12011 isl_space_free(space);
12012 isl_mat_free(eq);
12013 isl_mat_free(ineq);
12015 bmap = isl_basic_map_simplify(bmap);
12016 return isl_basic_map_finalize(bmap);
12017 error:
12018 isl_space_free(space);
12019 isl_mat_free(eq);
12020 isl_mat_free(ineq);
12021 isl_basic_map_free(bmap);
12022 return NULL;
12025 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12026 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12027 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12029 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12030 c1, c2, c3, c4, isl_dim_in);
12033 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12034 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12035 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12037 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12038 c1, c2, c3, c4, isl_dim_in);
12041 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12042 __isl_take isl_space *dim,
12043 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12044 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12046 isl_basic_map *bmap;
12047 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12048 c1, c2, c3, c4, isl_dim_in);
12049 return bset_from_bmap(bmap);
12052 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12054 if (!bmap)
12055 return isl_bool_error;
12057 return isl_space_can_zip(bmap->dim);
12060 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12062 if (!map)
12063 return isl_bool_error;
12065 return isl_space_can_zip(map->dim);
12068 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12069 * (A -> C) -> (B -> D).
12071 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12073 unsigned pos;
12074 unsigned n1;
12075 unsigned n2;
12077 if (!bmap)
12078 return NULL;
12080 if (!isl_basic_map_can_zip(bmap))
12081 isl_die(bmap->ctx, isl_error_invalid,
12082 "basic map cannot be zipped", goto error);
12083 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12084 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12085 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12086 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12087 bmap = isl_basic_map_cow(bmap);
12088 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12089 if (!bmap)
12090 return NULL;
12091 bmap->dim = isl_space_zip(bmap->dim);
12092 if (!bmap->dim)
12093 goto error;
12094 bmap = isl_basic_map_mark_final(bmap);
12095 return bmap;
12096 error:
12097 isl_basic_map_free(bmap);
12098 return NULL;
12101 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12102 * (A -> C) -> (B -> D).
12104 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12106 int i;
12108 if (!map)
12109 return NULL;
12111 if (!isl_map_can_zip(map))
12112 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12113 goto error);
12115 map = isl_map_cow(map);
12116 if (!map)
12117 return NULL;
12119 for (i = 0; i < map->n; ++i) {
12120 map->p[i] = isl_basic_map_zip(map->p[i]);
12121 if (!map->p[i])
12122 goto error;
12125 map->dim = isl_space_zip(map->dim);
12126 if (!map->dim)
12127 goto error;
12129 return map;
12130 error:
12131 isl_map_free(map);
12132 return NULL;
12135 /* Can we apply isl_basic_map_curry to "bmap"?
12136 * That is, does it have a nested relation in its domain?
12138 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12140 if (!bmap)
12141 return isl_bool_error;
12143 return isl_space_can_curry(bmap->dim);
12146 /* Can we apply isl_map_curry to "map"?
12147 * That is, does it have a nested relation in its domain?
12149 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12151 if (!map)
12152 return isl_bool_error;
12154 return isl_space_can_curry(map->dim);
12157 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12158 * A -> (B -> C).
12160 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12163 if (!bmap)
12164 return NULL;
12166 if (!isl_basic_map_can_curry(bmap))
12167 isl_die(bmap->ctx, isl_error_invalid,
12168 "basic map cannot be curried", goto error);
12169 bmap = isl_basic_map_cow(bmap);
12170 if (!bmap)
12171 return NULL;
12172 bmap->dim = isl_space_curry(bmap->dim);
12173 if (!bmap->dim)
12174 goto error;
12175 bmap = isl_basic_map_mark_final(bmap);
12176 return bmap;
12177 error:
12178 isl_basic_map_free(bmap);
12179 return NULL;
12182 /* Given a map (A -> B) -> C, return the corresponding map
12183 * A -> (B -> C).
12185 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12187 return isl_map_change_space(map, &isl_map_can_curry,
12188 "map cannot be curried", &isl_space_curry);
12191 /* Can isl_map_range_curry be applied to "map"?
12192 * That is, does it have a nested relation in its range,
12193 * the domain of which is itself a nested relation?
12195 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12197 if (!map)
12198 return isl_bool_error;
12200 return isl_space_can_range_curry(map->dim);
12203 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12204 * A -> (B -> (C -> D)).
12206 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12208 return isl_map_change_space(map, &isl_map_can_range_curry,
12209 "map range cannot be curried",
12210 &isl_space_range_curry);
12213 /* Can we apply isl_basic_map_uncurry to "bmap"?
12214 * That is, does it have a nested relation in its domain?
12216 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12218 if (!bmap)
12219 return isl_bool_error;
12221 return isl_space_can_uncurry(bmap->dim);
12224 /* Can we apply isl_map_uncurry to "map"?
12225 * That is, does it have a nested relation in its domain?
12227 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12229 if (!map)
12230 return isl_bool_error;
12232 return isl_space_can_uncurry(map->dim);
12235 /* Given a basic map A -> (B -> C), return the corresponding basic map
12236 * (A -> B) -> C.
12238 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12241 if (!bmap)
12242 return NULL;
12244 if (!isl_basic_map_can_uncurry(bmap))
12245 isl_die(bmap->ctx, isl_error_invalid,
12246 "basic map cannot be uncurried",
12247 return isl_basic_map_free(bmap));
12248 bmap = isl_basic_map_cow(bmap);
12249 if (!bmap)
12250 return NULL;
12251 bmap->dim = isl_space_uncurry(bmap->dim);
12252 if (!bmap->dim)
12253 return isl_basic_map_free(bmap);
12254 bmap = isl_basic_map_mark_final(bmap);
12255 return bmap;
12258 /* Given a map A -> (B -> C), return the corresponding map
12259 * (A -> B) -> C.
12261 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12263 return isl_map_change_space(map, &isl_map_can_uncurry,
12264 "map cannot be uncurried", &isl_space_uncurry);
12267 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12268 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12270 return isl_map_equate(set, type1, pos1, type2, pos2);
12273 /* Construct a basic map where the given dimensions are equal to each other.
12275 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12276 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12278 isl_basic_map *bmap = NULL;
12279 int i;
12281 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12282 isl_space_check_range(space, type2, pos2, 1) < 0)
12283 goto error;
12285 if (type1 == type2 && pos1 == pos2)
12286 return isl_basic_map_universe(space);
12288 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12289 i = isl_basic_map_alloc_equality(bmap);
12290 if (i < 0)
12291 goto error;
12292 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12293 pos1 += isl_basic_map_offset(bmap, type1);
12294 pos2 += isl_basic_map_offset(bmap, type2);
12295 isl_int_set_si(bmap->eq[i][pos1], -1);
12296 isl_int_set_si(bmap->eq[i][pos2], 1);
12297 bmap = isl_basic_map_finalize(bmap);
12298 isl_space_free(space);
12299 return bmap;
12300 error:
12301 isl_space_free(space);
12302 isl_basic_map_free(bmap);
12303 return NULL;
12306 /* Add a constraint imposing that the given two dimensions are equal.
12308 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12309 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12311 isl_basic_map *eq;
12313 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12315 bmap = isl_basic_map_intersect(bmap, eq);
12317 return bmap;
12320 /* Add a constraint imposing that the given two dimensions are equal.
12322 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12323 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12325 isl_basic_map *bmap;
12327 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12329 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12331 return map;
12334 /* Add a constraint imposing that the given two dimensions have opposite values.
12336 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12337 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12339 isl_basic_map *bmap = NULL;
12340 int i;
12342 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12343 return isl_map_free(map);
12344 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12345 return isl_map_free(map);
12347 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12348 i = isl_basic_map_alloc_equality(bmap);
12349 if (i < 0)
12350 goto error;
12351 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12352 pos1 += isl_basic_map_offset(bmap, type1);
12353 pos2 += isl_basic_map_offset(bmap, type2);
12354 isl_int_set_si(bmap->eq[i][pos1], 1);
12355 isl_int_set_si(bmap->eq[i][pos2], 1);
12356 bmap = isl_basic_map_finalize(bmap);
12358 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12360 return map;
12361 error:
12362 isl_basic_map_free(bmap);
12363 isl_map_free(map);
12364 return NULL;
12367 /* Construct a constraint imposing that the value of the first dimension is
12368 * greater than or equal to that of the second.
12370 static __isl_give isl_constraint *constraint_order_ge(
12371 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12372 enum isl_dim_type type2, int pos2)
12374 isl_constraint *c;
12376 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12377 isl_space_check_range(space, type2, pos2, 1) < 0)
12378 space = isl_space_free(space);
12379 if (!space)
12380 return NULL;
12382 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12384 if (type1 == type2 && pos1 == pos2)
12385 return c;
12387 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12388 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12390 return c;
12393 /* Add a constraint imposing that the value of the first dimension is
12394 * greater than or equal to that of the second.
12396 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12397 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12399 isl_constraint *c;
12400 isl_space *space;
12402 if (type1 == type2 && pos1 == pos2)
12403 return bmap;
12404 space = isl_basic_map_get_space(bmap);
12405 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12406 bmap = isl_basic_map_add_constraint(bmap, c);
12408 return bmap;
12411 /* Add a constraint imposing that the value of the first dimension is
12412 * greater than or equal to that of the second.
12414 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12415 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12417 isl_constraint *c;
12418 isl_space *space;
12420 if (type1 == type2 && pos1 == pos2)
12421 return map;
12422 space = isl_map_get_space(map);
12423 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12424 map = isl_map_add_constraint(map, c);
12426 return map;
12429 /* Add a constraint imposing that the value of the first dimension is
12430 * less than or equal to that of the second.
12432 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12433 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12435 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12438 /* Construct a basic map where the value of the first dimension is
12439 * greater than that of the second.
12441 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12442 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12444 isl_basic_map *bmap = NULL;
12445 int i;
12447 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12448 isl_space_check_range(space, type2, pos2, 1) < 0)
12449 goto error;
12451 if (type1 == type2 && pos1 == pos2)
12452 return isl_basic_map_empty(space);
12454 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12455 i = isl_basic_map_alloc_inequality(bmap);
12456 if (i < 0)
12457 return isl_basic_map_free(bmap);
12458 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12459 pos1 += isl_basic_map_offset(bmap, type1);
12460 pos2 += isl_basic_map_offset(bmap, type2);
12461 isl_int_set_si(bmap->ineq[i][pos1], 1);
12462 isl_int_set_si(bmap->ineq[i][pos2], -1);
12463 isl_int_set_si(bmap->ineq[i][0], -1);
12464 bmap = isl_basic_map_finalize(bmap);
12466 return bmap;
12467 error:
12468 isl_space_free(space);
12469 isl_basic_map_free(bmap);
12470 return NULL;
12473 /* Add a constraint imposing that the value of the first dimension is
12474 * greater than that of the second.
12476 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12477 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12479 isl_basic_map *gt;
12481 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12483 bmap = isl_basic_map_intersect(bmap, gt);
12485 return bmap;
12488 /* Add a constraint imposing that the value of the first dimension is
12489 * greater than that of the second.
12491 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12492 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12494 isl_basic_map *bmap;
12496 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12498 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12500 return map;
12503 /* Add a constraint imposing that the value of the first dimension is
12504 * smaller than that of the second.
12506 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12507 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12509 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12512 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12513 int pos)
12515 isl_aff *div;
12516 isl_local_space *ls;
12518 if (!bmap)
12519 return NULL;
12521 if (!isl_basic_map_divs_known(bmap))
12522 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12523 "some divs are unknown", return NULL);
12525 ls = isl_basic_map_get_local_space(bmap);
12526 div = isl_local_space_get_div(ls, pos);
12527 isl_local_space_free(ls);
12529 return div;
12532 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12533 int pos)
12535 return isl_basic_map_get_div(bset, pos);
12538 /* Plug in "subs" for dimension "type", "pos" of "bset".
12540 * Let i be the dimension to replace and let "subs" be of the form
12542 * f/d
12544 * Any integer division with a non-zero coefficient for i,
12546 * floor((a i + g)/m)
12548 * is replaced by
12550 * floor((a f + d g)/(m d))
12552 * Constraints of the form
12554 * a i + g
12556 * are replaced by
12558 * a f + d g
12560 * We currently require that "subs" is an integral expression.
12561 * Handling rational expressions may require us to add stride constraints
12562 * as we do in isl_basic_set_preimage_multi_aff.
12564 __isl_give isl_basic_set *isl_basic_set_substitute(
12565 __isl_take isl_basic_set *bset,
12566 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12568 int i;
12569 isl_int v;
12570 isl_ctx *ctx;
12572 if (bset && isl_basic_set_plain_is_empty(bset))
12573 return bset;
12575 bset = isl_basic_set_cow(bset);
12576 if (!bset || !subs)
12577 goto error;
12579 ctx = isl_basic_set_get_ctx(bset);
12580 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12581 isl_die(ctx, isl_error_invalid,
12582 "spaces don't match", goto error);
12583 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12584 isl_die(ctx, isl_error_unsupported,
12585 "cannot handle divs yet", goto error);
12586 if (!isl_int_is_one(subs->v->el[0]))
12587 isl_die(ctx, isl_error_invalid,
12588 "can only substitute integer expressions", goto error);
12590 pos += isl_basic_set_offset(bset, type);
12592 isl_int_init(v);
12594 for (i = 0; i < bset->n_eq; ++i) {
12595 if (isl_int_is_zero(bset->eq[i][pos]))
12596 continue;
12597 isl_int_set(v, bset->eq[i][pos]);
12598 isl_int_set_si(bset->eq[i][pos], 0);
12599 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12600 v, subs->v->el + 1, subs->v->size - 1);
12603 for (i = 0; i < bset->n_ineq; ++i) {
12604 if (isl_int_is_zero(bset->ineq[i][pos]))
12605 continue;
12606 isl_int_set(v, bset->ineq[i][pos]);
12607 isl_int_set_si(bset->ineq[i][pos], 0);
12608 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12609 v, subs->v->el + 1, subs->v->size - 1);
12612 for (i = 0; i < bset->n_div; ++i) {
12613 if (isl_int_is_zero(bset->div[i][1 + pos]))
12614 continue;
12615 isl_int_set(v, bset->div[i][1 + pos]);
12616 isl_int_set_si(bset->div[i][1 + pos], 0);
12617 isl_seq_combine(bset->div[i] + 1,
12618 subs->v->el[0], bset->div[i] + 1,
12619 v, subs->v->el + 1, subs->v->size - 1);
12620 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12623 isl_int_clear(v);
12625 bset = isl_basic_set_simplify(bset);
12626 return isl_basic_set_finalize(bset);
12627 error:
12628 isl_basic_set_free(bset);
12629 return NULL;
12632 /* Plug in "subs" for dimension "type", "pos" of "set".
12634 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12635 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12637 int i;
12639 if (set && isl_set_plain_is_empty(set))
12640 return set;
12642 set = isl_set_cow(set);
12643 if (!set || !subs)
12644 goto error;
12646 for (i = set->n - 1; i >= 0; --i) {
12647 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12648 set = set_from_map(remove_if_empty(set_to_map(set), i));
12649 if (!set)
12650 return NULL;
12653 return set;
12654 error:
12655 isl_set_free(set);
12656 return NULL;
12659 /* Check if the range of "ma" is compatible with the domain or range
12660 * (depending on "type") of "bmap".
12662 static isl_stat check_basic_map_compatible_range_multi_aff(
12663 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12664 __isl_keep isl_multi_aff *ma)
12666 isl_bool m;
12667 isl_space *ma_space;
12669 ma_space = isl_multi_aff_get_space(ma);
12671 m = isl_space_has_equal_params(bmap->dim, ma_space);
12672 if (m < 0)
12673 goto error;
12674 if (!m)
12675 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12676 "parameters don't match", goto error);
12677 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12678 if (m < 0)
12679 goto error;
12680 if (!m)
12681 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12682 "spaces don't match", goto error);
12684 isl_space_free(ma_space);
12685 return isl_stat_ok;
12686 error:
12687 isl_space_free(ma_space);
12688 return isl_stat_error;
12691 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12692 * coefficients before the transformed range of dimensions,
12693 * the "n_after" coefficients after the transformed range of dimensions
12694 * and the coefficients of the other divs in "bmap".
12696 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12697 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12699 int i;
12700 int n_param;
12701 int n_set;
12702 isl_local_space *ls;
12704 if (n_div == 0)
12705 return bmap;
12707 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12708 if (!ls)
12709 return isl_basic_map_free(bmap);
12711 n_param = isl_local_space_dim(ls, isl_dim_param);
12712 n_set = isl_local_space_dim(ls, isl_dim_set);
12713 for (i = 0; i < n_div; ++i) {
12714 int o_bmap = 0, o_ls = 0;
12716 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12717 o_bmap += 1 + 1 + n_param;
12718 o_ls += 1 + 1 + n_param;
12719 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12720 o_bmap += n_before;
12721 isl_seq_cpy(bmap->div[i] + o_bmap,
12722 ls->div->row[i] + o_ls, n_set);
12723 o_bmap += n_set;
12724 o_ls += n_set;
12725 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12726 o_bmap += n_after;
12727 isl_seq_cpy(bmap->div[i] + o_bmap,
12728 ls->div->row[i] + o_ls, n_div);
12729 o_bmap += n_div;
12730 o_ls += n_div;
12731 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12732 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12733 goto error;
12736 isl_local_space_free(ls);
12737 return bmap;
12738 error:
12739 isl_local_space_free(ls);
12740 return isl_basic_map_free(bmap);
12743 /* How many stride constraints does "ma" enforce?
12744 * That is, how many of the affine expressions have a denominator
12745 * different from one?
12747 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12749 int i;
12750 int strides = 0;
12752 for (i = 0; i < ma->n; ++i)
12753 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12754 strides++;
12756 return strides;
12759 /* For each affine expression in ma of the form
12761 * x_i = (f_i y + h_i)/m_i
12763 * with m_i different from one, add a constraint to "bmap"
12764 * of the form
12766 * f_i y + h_i = m_i alpha_i
12768 * with alpha_i an additional existentially quantified variable.
12770 * The input variables of "ma" correspond to a subset of the variables
12771 * of "bmap". There are "n_before" variables in "bmap" before this
12772 * subset and "n_after" variables after this subset.
12773 * The integer divisions of the affine expressions in "ma" are assumed
12774 * to have been aligned. There are "n_div_ma" of them and
12775 * they appear first in "bmap", straight after the "n_after" variables.
12777 static __isl_give isl_basic_map *add_ma_strides(
12778 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12779 int n_before, int n_after, int n_div_ma)
12781 int i, k;
12782 int div;
12783 int total;
12784 int n_param;
12785 int n_in;
12787 total = isl_basic_map_total_dim(bmap);
12788 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12789 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12790 for (i = 0; i < ma->n; ++i) {
12791 int o_bmap = 0, o_ma = 1;
12793 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12794 continue;
12795 div = isl_basic_map_alloc_div(bmap);
12796 k = isl_basic_map_alloc_equality(bmap);
12797 if (div < 0 || k < 0)
12798 goto error;
12799 isl_int_set_si(bmap->div[div][0], 0);
12800 isl_seq_cpy(bmap->eq[k] + o_bmap,
12801 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12802 o_bmap += 1 + n_param;
12803 o_ma += 1 + n_param;
12804 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12805 o_bmap += n_before;
12806 isl_seq_cpy(bmap->eq[k] + o_bmap,
12807 ma->u.p[i]->v->el + o_ma, n_in);
12808 o_bmap += n_in;
12809 o_ma += n_in;
12810 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12811 o_bmap += n_after;
12812 isl_seq_cpy(bmap->eq[k] + o_bmap,
12813 ma->u.p[i]->v->el + o_ma, n_div_ma);
12814 o_bmap += n_div_ma;
12815 o_ma += n_div_ma;
12816 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12817 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12818 total++;
12821 return bmap;
12822 error:
12823 isl_basic_map_free(bmap);
12824 return NULL;
12827 /* Replace the domain or range space (depending on "type) of "space" by "set".
12829 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12830 enum isl_dim_type type, __isl_take isl_space *set)
12832 if (type == isl_dim_in) {
12833 space = isl_space_range(space);
12834 space = isl_space_map_from_domain_and_range(set, space);
12835 } else {
12836 space = isl_space_domain(space);
12837 space = isl_space_map_from_domain_and_range(space, set);
12840 return space;
12843 /* Compute the preimage of the domain or range (depending on "type")
12844 * of "bmap" under the function represented by "ma".
12845 * In other words, plug in "ma" in the domain or range of "bmap".
12846 * The result is a basic map that lives in the same space as "bmap"
12847 * except that the domain or range has been replaced by
12848 * the domain space of "ma".
12850 * If bmap is represented by
12852 * A(p) + S u + B x + T v + C(divs) >= 0,
12854 * where u and x are input and output dimensions if type == isl_dim_out
12855 * while x and v are input and output dimensions if type == isl_dim_in,
12856 * and ma is represented by
12858 * x = D(p) + F(y) + G(divs')
12860 * then the result is
12862 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12864 * The divs in the input set are similarly adjusted.
12865 * In particular
12867 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12869 * becomes
12871 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12872 * B_i G(divs') + c_i(divs))/n_i)
12874 * If bmap is not a rational map and if F(y) involves any denominators
12876 * x_i = (f_i y + h_i)/m_i
12878 * then additional constraints are added to ensure that we only
12879 * map back integer points. That is we enforce
12881 * f_i y + h_i = m_i alpha_i
12883 * with alpha_i an additional existentially quantified variable.
12885 * We first copy over the divs from "ma".
12886 * Then we add the modified constraints and divs from "bmap".
12887 * Finally, we add the stride constraints, if needed.
12889 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12890 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12891 __isl_take isl_multi_aff *ma)
12893 int i, k;
12894 isl_space *space;
12895 isl_basic_map *res = NULL;
12896 int n_before, n_after, n_div_bmap, n_div_ma;
12897 isl_int f, c1, c2, g;
12898 isl_bool rational;
12899 int strides;
12901 isl_int_init(f);
12902 isl_int_init(c1);
12903 isl_int_init(c2);
12904 isl_int_init(g);
12906 ma = isl_multi_aff_align_divs(ma);
12907 if (!bmap || !ma)
12908 goto error;
12909 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12910 goto error;
12912 if (type == isl_dim_in) {
12913 n_before = 0;
12914 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12915 } else {
12916 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12917 n_after = 0;
12919 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12920 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12922 space = isl_multi_aff_get_domain_space(ma);
12923 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12924 rational = isl_basic_map_is_rational(bmap);
12925 strides = rational ? 0 : multi_aff_strides(ma);
12926 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12927 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12928 if (rational)
12929 res = isl_basic_map_set_rational(res);
12931 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12932 if (isl_basic_map_alloc_div(res) < 0)
12933 goto error;
12935 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
12936 if (!res)
12937 goto error;
12939 for (i = 0; i < bmap->n_eq; ++i) {
12940 k = isl_basic_map_alloc_equality(res);
12941 if (k < 0)
12942 goto error;
12943 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12944 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12947 for (i = 0; i < bmap->n_ineq; ++i) {
12948 k = isl_basic_map_alloc_inequality(res);
12949 if (k < 0)
12950 goto error;
12951 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12952 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12955 for (i = 0; i < bmap->n_div; ++i) {
12956 if (isl_int_is_zero(bmap->div[i][0])) {
12957 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12958 continue;
12960 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12961 n_before, n_after, n_div_ma, n_div_bmap,
12962 f, c1, c2, g, 1);
12965 if (strides)
12966 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12968 isl_int_clear(f);
12969 isl_int_clear(c1);
12970 isl_int_clear(c2);
12971 isl_int_clear(g);
12972 isl_basic_map_free(bmap);
12973 isl_multi_aff_free(ma);
12974 res = isl_basic_map_simplify(res);
12975 return isl_basic_map_finalize(res);
12976 error:
12977 isl_int_clear(f);
12978 isl_int_clear(c1);
12979 isl_int_clear(c2);
12980 isl_int_clear(g);
12981 isl_basic_map_free(bmap);
12982 isl_multi_aff_free(ma);
12983 isl_basic_map_free(res);
12984 return NULL;
12987 /* Compute the preimage of "bset" under the function represented by "ma".
12988 * In other words, plug in "ma" in "bset". The result is a basic set
12989 * that lives in the domain space of "ma".
12991 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12992 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12994 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12997 /* Compute the preimage of the domain of "bmap" under the function
12998 * represented by "ma".
12999 * In other words, plug in "ma" in the domain of "bmap".
13000 * The result is a basic map that lives in the same space as "bmap"
13001 * except that the domain has been replaced by the domain space of "ma".
13003 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13004 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13006 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13009 /* Compute the preimage of the range of "bmap" under the function
13010 * represented by "ma".
13011 * In other words, plug in "ma" in the range of "bmap".
13012 * The result is a basic map that lives in the same space as "bmap"
13013 * except that the range has been replaced by the domain space of "ma".
13015 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13016 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13018 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13021 /* Check if the range of "ma" is compatible with the domain or range
13022 * (depending on "type") of "map".
13023 * Return isl_stat_error if anything is wrong.
13025 static isl_stat check_map_compatible_range_multi_aff(
13026 __isl_keep isl_map *map, enum isl_dim_type type,
13027 __isl_keep isl_multi_aff *ma)
13029 isl_bool m;
13030 isl_space *ma_space;
13032 ma_space = isl_multi_aff_get_space(ma);
13033 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13034 isl_space_free(ma_space);
13035 if (m < 0)
13036 return isl_stat_error;
13037 if (!m)
13038 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13039 "spaces don't match", return isl_stat_error);
13040 return isl_stat_ok;
13043 /* Compute the preimage of the domain or range (depending on "type")
13044 * of "map" under the function represented by "ma".
13045 * In other words, plug in "ma" in the domain or range of "map".
13046 * The result is a map that lives in the same space as "map"
13047 * except that the domain or range has been replaced by
13048 * the domain space of "ma".
13050 * The parameters are assumed to have been aligned.
13052 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13053 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13055 int i;
13056 isl_space *space;
13058 map = isl_map_cow(map);
13059 ma = isl_multi_aff_align_divs(ma);
13060 if (!map || !ma)
13061 goto error;
13062 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13063 goto error;
13065 for (i = 0; i < map->n; ++i) {
13066 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13067 isl_multi_aff_copy(ma));
13068 if (!map->p[i])
13069 goto error;
13072 space = isl_multi_aff_get_domain_space(ma);
13073 space = isl_space_set(isl_map_get_space(map), type, space);
13075 isl_space_free(map->dim);
13076 map->dim = space;
13077 if (!map->dim)
13078 goto error;
13080 isl_multi_aff_free(ma);
13081 if (map->n > 1)
13082 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13083 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13084 return map;
13085 error:
13086 isl_multi_aff_free(ma);
13087 isl_map_free(map);
13088 return NULL;
13091 /* Compute the preimage of the domain or range (depending on "type")
13092 * of "map" under the function represented by "ma".
13093 * In other words, plug in "ma" in the domain or range of "map".
13094 * The result is a map that lives in the same space as "map"
13095 * except that the domain or range has been replaced by
13096 * the domain space of "ma".
13098 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13099 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13101 isl_bool aligned;
13103 if (!map || !ma)
13104 goto error;
13106 aligned = isl_map_space_has_equal_params(map, ma->space);
13107 if (aligned < 0)
13108 goto error;
13109 if (aligned)
13110 return map_preimage_multi_aff(map, type, ma);
13112 if (isl_map_check_named_params(map) < 0)
13113 goto error;
13114 if (!isl_space_has_named_params(ma->space))
13115 isl_die(map->ctx, isl_error_invalid,
13116 "unaligned unnamed parameters", goto error);
13117 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13118 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13120 return map_preimage_multi_aff(map, type, ma);
13121 error:
13122 isl_multi_aff_free(ma);
13123 return isl_map_free(map);
13126 /* Compute the preimage of "set" under the function represented by "ma".
13127 * In other words, plug in "ma" in "set". The result is a set
13128 * that lives in the domain space of "ma".
13130 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13131 __isl_take isl_multi_aff *ma)
13133 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13136 /* Compute the preimage of the domain of "map" under the function
13137 * represented by "ma".
13138 * In other words, plug in "ma" in the domain of "map".
13139 * The result is a map that lives in the same space as "map"
13140 * except that the domain has been replaced by the domain space of "ma".
13142 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13143 __isl_take isl_multi_aff *ma)
13145 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13148 /* Compute the preimage of the range of "map" under the function
13149 * represented by "ma".
13150 * In other words, plug in "ma" in the range of "map".
13151 * The result is a map that lives in the same space as "map"
13152 * except that the range has been replaced by the domain space of "ma".
13154 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13155 __isl_take isl_multi_aff *ma)
13157 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13160 /* Compute the preimage of "map" under the function represented by "pma".
13161 * In other words, plug in "pma" in the domain or range of "map".
13162 * The result is a map that lives in the same space as "map",
13163 * except that the space of type "type" has been replaced by
13164 * the domain space of "pma".
13166 * The parameters of "map" and "pma" are assumed to have been aligned.
13168 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13169 __isl_take isl_map *map, enum isl_dim_type type,
13170 __isl_take isl_pw_multi_aff *pma)
13172 int i;
13173 isl_map *res;
13175 if (!pma)
13176 goto error;
13178 if (pma->n == 0) {
13179 isl_pw_multi_aff_free(pma);
13180 res = isl_map_empty(isl_map_get_space(map));
13181 isl_map_free(map);
13182 return res;
13185 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13186 isl_multi_aff_copy(pma->p[0].maff));
13187 if (type == isl_dim_in)
13188 res = isl_map_intersect_domain(res,
13189 isl_map_copy(pma->p[0].set));
13190 else
13191 res = isl_map_intersect_range(res,
13192 isl_map_copy(pma->p[0].set));
13194 for (i = 1; i < pma->n; ++i) {
13195 isl_map *res_i;
13197 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13198 isl_multi_aff_copy(pma->p[i].maff));
13199 if (type == isl_dim_in)
13200 res_i = isl_map_intersect_domain(res_i,
13201 isl_map_copy(pma->p[i].set));
13202 else
13203 res_i = isl_map_intersect_range(res_i,
13204 isl_map_copy(pma->p[i].set));
13205 res = isl_map_union(res, res_i);
13208 isl_pw_multi_aff_free(pma);
13209 isl_map_free(map);
13210 return res;
13211 error:
13212 isl_pw_multi_aff_free(pma);
13213 isl_map_free(map);
13214 return NULL;
13217 /* Compute the preimage of "map" under the function represented by "pma".
13218 * In other words, plug in "pma" in the domain or range of "map".
13219 * The result is a map that lives in the same space as "map",
13220 * except that the space of type "type" has been replaced by
13221 * the domain space of "pma".
13223 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13224 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13226 isl_bool aligned;
13228 if (!map || !pma)
13229 goto error;
13231 aligned = isl_map_space_has_equal_params(map, pma->dim);
13232 if (aligned < 0)
13233 goto error;
13234 if (aligned)
13235 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13237 if (isl_map_check_named_params(map) < 0)
13238 goto error;
13239 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13240 goto error;
13241 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13242 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13244 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13245 error:
13246 isl_pw_multi_aff_free(pma);
13247 return isl_map_free(map);
13250 /* Compute the preimage of "set" under the function represented by "pma".
13251 * In other words, plug in "pma" in "set". The result is a set
13252 * that lives in the domain space of "pma".
13254 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13255 __isl_take isl_pw_multi_aff *pma)
13257 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13260 /* Compute the preimage of the domain of "map" under the function
13261 * represented by "pma".
13262 * In other words, plug in "pma" in the domain of "map".
13263 * The result is a map that lives in the same space as "map",
13264 * except that domain space has been replaced by the domain space of "pma".
13266 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13267 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13269 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13272 /* Compute the preimage of the range of "map" under the function
13273 * represented by "pma".
13274 * In other words, plug in "pma" in the range of "map".
13275 * The result is a map that lives in the same space as "map",
13276 * except that range space has been replaced by the domain space of "pma".
13278 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13279 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13281 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13284 /* Compute the preimage of "map" under the function represented by "mpa".
13285 * In other words, plug in "mpa" in the domain or range of "map".
13286 * The result is a map that lives in the same space as "map",
13287 * except that the space of type "type" has been replaced by
13288 * the domain space of "mpa".
13290 * If the map does not involve any constraints that refer to the
13291 * dimensions of the substituted space, then the only possible
13292 * effect of "mpa" on the map is to map the space to a different space.
13293 * We create a separate isl_multi_aff to effectuate this change
13294 * in order to avoid spurious splitting of the map along the pieces
13295 * of "mpa".
13296 * If "mpa" has a non-trivial explicit domain, however,
13297 * then the full substitution should be performed.
13299 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13300 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13302 int n;
13303 isl_bool full;
13304 isl_pw_multi_aff *pma;
13306 if (!map || !mpa)
13307 goto error;
13309 n = isl_map_dim(map, type);
13310 full = isl_map_involves_dims(map, type, 0, n);
13311 if (full >= 0 && !full)
13312 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13313 if (full < 0)
13314 goto error;
13315 if (!full) {
13316 isl_space *space;
13317 isl_multi_aff *ma;
13319 space = isl_multi_pw_aff_get_space(mpa);
13320 isl_multi_pw_aff_free(mpa);
13321 ma = isl_multi_aff_zero(space);
13322 return isl_map_preimage_multi_aff(map, type, ma);
13325 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13326 return isl_map_preimage_pw_multi_aff(map, type, pma);
13327 error:
13328 isl_map_free(map);
13329 isl_multi_pw_aff_free(mpa);
13330 return NULL;
13333 /* Compute the preimage of "map" under the function represented by "mpa".
13334 * In other words, plug in "mpa" in the domain "map".
13335 * The result is a map that lives in the same space as "map",
13336 * except that domain space has been replaced by the domain space of "mpa".
13338 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13339 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13341 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13344 /* Compute the preimage of "set" by the function represented by "mpa".
13345 * In other words, plug in "mpa" in "set".
13347 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13348 __isl_take isl_multi_pw_aff *mpa)
13350 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13353 /* Return a copy of the equality constraints of "bset" as a matrix.
13355 __isl_give isl_mat *isl_basic_set_extract_equalities(
13356 __isl_keep isl_basic_set *bset)
13358 isl_ctx *ctx;
13359 unsigned total;
13361 if (!bset)
13362 return NULL;
13364 ctx = isl_basic_set_get_ctx(bset);
13365 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13366 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13369 /* Are the "n" "coefficients" starting at "first" of the integer division
13370 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13371 * to each other?
13372 * The "coefficient" at position 0 is the denominator.
13373 * The "coefficient" at position 1 is the constant term.
13375 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13376 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13377 unsigned first, unsigned n)
13379 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13380 return isl_bool_error;
13381 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13382 return isl_bool_error;
13383 return isl_seq_eq(bmap1->div[pos1] + first,
13384 bmap2->div[pos2] + first, n);
13387 /* Are the integer division expressions at position "pos1" in "bmap1" and
13388 * "pos2" in "bmap2" equal to each other, except that the constant terms
13389 * are different?
13391 isl_bool isl_basic_map_equal_div_expr_except_constant(
13392 __isl_keep isl_basic_map *bmap1, int pos1,
13393 __isl_keep isl_basic_map *bmap2, int pos2)
13395 isl_bool equal;
13396 unsigned total;
13398 if (!bmap1 || !bmap2)
13399 return isl_bool_error;
13400 total = isl_basic_map_total_dim(bmap1);
13401 if (total != isl_basic_map_total_dim(bmap2))
13402 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13403 "incomparable div expressions", return isl_bool_error);
13404 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13405 0, 1);
13406 if (equal < 0 || !equal)
13407 return equal;
13408 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13409 1, 1);
13410 if (equal < 0 || equal)
13411 return isl_bool_not(equal);
13412 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13413 2, total);
13416 /* Replace the numerator of the constant term of the integer division
13417 * expression at position "div" in "bmap" by "value".
13418 * The caller guarantees that this does not change the meaning
13419 * of the input.
13421 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13422 __isl_take isl_basic_map *bmap, int div, int value)
13424 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13425 return isl_basic_map_free(bmap);
13427 isl_int_set_si(bmap->div[div][1], value);
13429 return bmap;
13432 /* Is the point "inner" internal to inequality constraint "ineq"
13433 * of "bset"?
13434 * The point is considered to be internal to the inequality constraint,
13435 * if it strictly lies on the positive side of the inequality constraint,
13436 * or if it lies on the constraint and the constraint is lexico-positive.
13438 static isl_bool is_internal(__isl_keep isl_vec *inner,
13439 __isl_keep isl_basic_set *bset, int ineq)
13441 isl_ctx *ctx;
13442 int pos;
13443 unsigned total;
13445 if (!inner || !bset)
13446 return isl_bool_error;
13448 ctx = isl_basic_set_get_ctx(bset);
13449 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13450 &ctx->normalize_gcd);
13451 if (!isl_int_is_zero(ctx->normalize_gcd))
13452 return isl_int_is_nonneg(ctx->normalize_gcd);
13454 total = isl_basic_set_dim(bset, isl_dim_all);
13455 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13456 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13459 /* Tighten the inequality constraints of "bset" that are outward with respect
13460 * to the point "vec".
13461 * That is, tighten the constraints that are not satisfied by "vec".
13463 * "vec" is a point internal to some superset S of "bset" that is used
13464 * to make the subsets of S disjoint, by tightening one half of the constraints
13465 * that separate two subsets. In particular, the constraints of S
13466 * are all satisfied by "vec" and should not be tightened.
13467 * Of the internal constraints, those that have "vec" on the outside
13468 * are tightened. The shared facet is included in the adjacent subset
13469 * with the opposite constraint.
13470 * For constraints that saturate "vec", this criterion cannot be used
13471 * to determine which of the two sides should be tightened.
13472 * Instead, the sign of the first non-zero coefficient is used
13473 * to make this choice. Note that this second criterion is never used
13474 * on the constraints of S since "vec" is interior to "S".
13476 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13477 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13479 int j;
13481 bset = isl_basic_set_cow(bset);
13482 if (!bset)
13483 return NULL;
13484 for (j = 0; j < bset->n_ineq; ++j) {
13485 isl_bool internal;
13487 internal = is_internal(vec, bset, j);
13488 if (internal < 0)
13489 return isl_basic_set_free(bset);
13490 if (internal)
13491 continue;
13492 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13495 return bset;
13498 /* Replace the variables x of type "type" starting at "first" in "bmap"
13499 * by x' with x = M x' with M the matrix trans.
13500 * That is, replace the corresponding coefficients c by c M.
13502 * The transformation matrix should be a square matrix.
13504 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13505 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13506 __isl_take isl_mat *trans)
13508 unsigned pos;
13510 bmap = isl_basic_map_cow(bmap);
13511 if (!bmap || !trans)
13512 goto error;
13514 if (trans->n_row != trans->n_col)
13515 isl_die(trans->ctx, isl_error_invalid,
13516 "expecting square transformation matrix", goto error);
13517 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13518 goto error;
13520 pos = isl_basic_map_offset(bmap, type) + first;
13522 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13523 isl_mat_copy(trans)) < 0)
13524 goto error;
13525 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13526 isl_mat_copy(trans)) < 0)
13527 goto error;
13528 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13529 isl_mat_copy(trans)) < 0)
13530 goto error;
13532 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13533 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13535 isl_mat_free(trans);
13536 return bmap;
13537 error:
13538 isl_mat_free(trans);
13539 isl_basic_map_free(bmap);
13540 return NULL;
13543 /* Replace the variables x of type "type" starting at "first" in "bset"
13544 * by x' with x = M x' with M the matrix trans.
13545 * That is, replace the corresponding coefficients c by c M.
13547 * The transformation matrix should be a square matrix.
13549 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13550 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13551 __isl_take isl_mat *trans)
13553 return isl_basic_map_transform_dims(bset, type, first, trans);