isl_mat_is_scaled_identity: return isl_bool
[isl.git] / isl_map.c
blob29c4ce1c2cd085f48080bdbdd9f4c22f0cc77f3f
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 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 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2146 int i;
2147 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2149 swap_div(bmap, a, b);
2151 for (i = 0; i < bmap->n_eq; ++i)
2152 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2154 for (i = 0; i < bmap->n_ineq; ++i)
2155 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2157 for (i = 0; i < bmap->n_div; ++i)
2158 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2159 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2162 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2164 isl_seq_cpy(c, c + n, rem);
2165 isl_seq_clr(c + rem, n);
2168 /* Drop n dimensions starting at first.
2170 * In principle, this frees up some extra variables as the number
2171 * of columns remains constant, but we would have to extend
2172 * the div array too as the number of rows in this array is assumed
2173 * to be equal to extra.
2175 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2176 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2178 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2181 /* Move "n" divs starting at "first" to the end of the list of divs.
2183 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2184 unsigned first, unsigned n)
2186 isl_int **div;
2187 int i;
2189 if (first + n == bmap->n_div)
2190 return bmap;
2192 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2193 if (!div)
2194 goto error;
2195 for (i = 0; i < n; ++i)
2196 div[i] = bmap->div[first + i];
2197 for (i = 0; i < bmap->n_div - first - n; ++i)
2198 bmap->div[first + i] = bmap->div[first + n + i];
2199 for (i = 0; i < n; ++i)
2200 bmap->div[bmap->n_div - n + i] = div[i];
2201 free(div);
2202 return bmap;
2203 error:
2204 isl_basic_map_free(bmap);
2205 return NULL;
2208 #undef TYPE
2209 #define TYPE isl_map
2210 static
2211 #include "check_type_range_templ.c"
2213 /* Check that there are "n" dimensions of type "type" starting at "first"
2214 * in "set".
2216 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2217 enum isl_dim_type type, unsigned first, unsigned n)
2219 return isl_map_check_range(set_to_map(set), type, first, n);
2222 /* Drop "n" dimensions of type "type" starting at "first".
2223 * Perform the core computation, without cowing or
2224 * simplifying and finalizing the result.
2226 * In principle, this frees up some extra variables as the number
2227 * of columns remains constant, but we would have to extend
2228 * the div array too as the number of rows in this array is assumed
2229 * to be equal to extra.
2231 __isl_give isl_basic_map *isl_basic_map_drop_core(
2232 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2233 unsigned first, unsigned n)
2235 int i;
2236 unsigned offset;
2237 unsigned left;
2239 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2240 return isl_basic_map_free(bmap);
2242 offset = isl_basic_map_offset(bmap, type) + first;
2243 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2244 for (i = 0; i < bmap->n_eq; ++i)
2245 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2247 for (i = 0; i < bmap->n_ineq; ++i)
2248 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2250 for (i = 0; i < bmap->n_div; ++i)
2251 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2253 if (type == isl_dim_div) {
2254 bmap = move_divs_last(bmap, first, n);
2255 if (!bmap)
2256 return NULL;
2257 if (isl_basic_map_free_div(bmap, n) < 0)
2258 return isl_basic_map_free(bmap);
2259 } else
2260 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2261 if (!bmap->dim)
2262 return isl_basic_map_free(bmap);
2264 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2265 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2266 return bmap;
2269 /* Drop "n" dimensions of type "type" starting at "first".
2271 * In principle, this frees up some extra variables as the number
2272 * of columns remains constant, but we would have to extend
2273 * the div array too as the number of rows in this array is assumed
2274 * to be equal to extra.
2276 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2277 enum isl_dim_type type, unsigned first, unsigned n)
2279 if (!bmap)
2280 return NULL;
2281 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2282 return bmap;
2284 bmap = isl_basic_map_cow(bmap);
2285 if (!bmap)
2286 return NULL;
2288 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2290 bmap = isl_basic_map_simplify(bmap);
2291 return isl_basic_map_finalize(bmap);
2294 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2295 enum isl_dim_type type, unsigned first, unsigned n)
2297 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2298 type, first, n));
2301 /* No longer consider "map" to be normalized.
2303 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2305 if (!map)
2306 return NULL;
2307 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2308 return map;
2311 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2312 enum isl_dim_type type, unsigned first, unsigned n)
2314 int i;
2316 if (isl_map_check_range(map, type, first, n) < 0)
2317 return isl_map_free(map);
2319 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2320 return map;
2321 map = isl_map_cow(map);
2322 if (!map)
2323 goto error;
2324 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2325 if (!map->dim)
2326 goto error;
2328 for (i = 0; i < map->n; ++i) {
2329 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2330 if (!map->p[i])
2331 goto error;
2333 map = isl_map_unmark_normalized(map);
2335 return map;
2336 error:
2337 isl_map_free(map);
2338 return NULL;
2341 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2342 enum isl_dim_type type, unsigned first, unsigned n)
2344 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2347 /* Drop the integer division at position "div", which is assumed
2348 * not to appear in any of the constraints or
2349 * in any of the other integer divisions.
2351 * Since the integer division is redundant, there is no need to cow.
2353 __isl_give isl_basic_map *isl_basic_map_drop_div(
2354 __isl_take isl_basic_map *bmap, unsigned div)
2356 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2359 /* Eliminate the specified n dimensions starting at first from the
2360 * constraints, without removing the dimensions from the space.
2361 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2363 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2364 enum isl_dim_type type, unsigned first, unsigned n)
2366 int i;
2368 if (n == 0)
2369 return map;
2371 if (isl_map_check_range(map, type, first, n) < 0)
2372 return isl_map_free(map);
2374 map = isl_map_cow(map);
2375 if (!map)
2376 return NULL;
2378 for (i = 0; i < map->n; ++i) {
2379 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2380 if (!map->p[i])
2381 goto error;
2383 return map;
2384 error:
2385 isl_map_free(map);
2386 return NULL;
2389 /* Eliminate the specified n dimensions starting at first from the
2390 * constraints, without removing the dimensions from the space.
2391 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2393 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2394 enum isl_dim_type type, unsigned first, unsigned n)
2396 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2399 /* Eliminate the specified n dimensions starting at first from the
2400 * constraints, without removing the dimensions from the space.
2401 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2403 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2404 unsigned first, unsigned n)
2406 return isl_set_eliminate(set, isl_dim_set, first, n);
2409 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2410 __isl_take isl_basic_map *bmap)
2412 if (!bmap)
2413 return NULL;
2414 bmap = isl_basic_map_eliminate_vars(bmap,
2415 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2416 if (!bmap)
2417 return NULL;
2418 bmap->n_div = 0;
2419 return isl_basic_map_finalize(bmap);
2422 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2423 __isl_take isl_basic_set *bset)
2425 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2428 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2430 int i;
2432 if (!map)
2433 return NULL;
2434 if (map->n == 0)
2435 return map;
2437 map = isl_map_cow(map);
2438 if (!map)
2439 return NULL;
2441 for (i = 0; i < map->n; ++i) {
2442 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2443 if (!map->p[i])
2444 goto error;
2446 return map;
2447 error:
2448 isl_map_free(map);
2449 return NULL;
2452 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2454 return isl_map_remove_divs(set);
2457 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2458 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2459 unsigned first, unsigned n)
2461 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2462 return isl_basic_map_free(bmap);
2463 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2464 return bmap;
2465 bmap = isl_basic_map_eliminate_vars(bmap,
2466 isl_basic_map_offset(bmap, type) - 1 + first, n);
2467 if (!bmap)
2468 return bmap;
2469 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2470 return bmap;
2471 bmap = isl_basic_map_drop(bmap, type, first, n);
2472 return bmap;
2475 /* Return true if the definition of the given div (recursively) involves
2476 * any of the given variables.
2478 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2479 unsigned first, unsigned n)
2481 int i;
2482 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2484 if (isl_int_is_zero(bmap->div[div][0]))
2485 return isl_bool_false;
2486 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2487 return isl_bool_true;
2489 for (i = bmap->n_div - 1; i >= 0; --i) {
2490 isl_bool involves;
2492 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2493 continue;
2494 involves = div_involves_vars(bmap, i, first, n);
2495 if (involves < 0 || involves)
2496 return involves;
2499 return isl_bool_false;
2502 /* Try and add a lower and/or upper bound on "div" to "bmap"
2503 * based on inequality "i".
2504 * "total" is the total number of variables (excluding the divs).
2505 * "v" is a temporary object that can be used during the calculations.
2506 * If "lb" is set, then a lower bound should be constructed.
2507 * If "ub" is set, then an upper bound should be constructed.
2509 * The calling function has already checked that the inequality does not
2510 * reference "div", but we still need to check that the inequality is
2511 * of the right form. We'll consider the case where we want to construct
2512 * a lower bound. The construction of upper bounds is similar.
2514 * Let "div" be of the form
2516 * q = floor((a + f(x))/d)
2518 * We essentially check if constraint "i" is of the form
2520 * b + f(x) >= 0
2522 * so that we can use it to derive a lower bound on "div".
2523 * However, we allow a slightly more general form
2525 * b + g(x) >= 0
2527 * with the condition that the coefficients of g(x) - f(x) are all
2528 * divisible by d.
2529 * Rewriting this constraint as
2531 * 0 >= -b - g(x)
2533 * adding a + f(x) to both sides and dividing by d, we obtain
2535 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2537 * Taking the floor on both sides, we obtain
2539 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2541 * or
2543 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2545 * In the case of an upper bound, we construct the constraint
2547 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2550 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2551 __isl_take isl_basic_map *bmap, int div, int i,
2552 unsigned total, isl_int v, int lb, int ub)
2554 int j;
2556 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2557 if (lb) {
2558 isl_int_sub(v, bmap->ineq[i][1 + j],
2559 bmap->div[div][1 + 1 + j]);
2560 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2562 if (ub) {
2563 isl_int_add(v, bmap->ineq[i][1 + j],
2564 bmap->div[div][1 + 1 + j]);
2565 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2568 if (!lb && !ub)
2569 return bmap;
2571 bmap = isl_basic_map_cow(bmap);
2572 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2573 if (lb) {
2574 int k = isl_basic_map_alloc_inequality(bmap);
2575 if (k < 0)
2576 goto error;
2577 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2578 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2579 bmap->div[div][1 + j]);
2580 isl_int_cdiv_q(bmap->ineq[k][j],
2581 bmap->ineq[k][j], bmap->div[div][0]);
2583 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2585 if (ub) {
2586 int k = isl_basic_map_alloc_inequality(bmap);
2587 if (k < 0)
2588 goto error;
2589 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2590 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2591 bmap->div[div][1 + j]);
2592 isl_int_fdiv_q(bmap->ineq[k][j],
2593 bmap->ineq[k][j], bmap->div[div][0]);
2595 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2598 return bmap;
2599 error:
2600 isl_basic_map_free(bmap);
2601 return NULL;
2604 /* This function is called right before "div" is eliminated from "bmap"
2605 * using Fourier-Motzkin.
2606 * Look through the constraints of "bmap" for constraints on the argument
2607 * of the integer division and use them to construct constraints on the
2608 * integer division itself. These constraints can then be combined
2609 * during the Fourier-Motzkin elimination.
2610 * Note that it is only useful to introduce lower bounds on "div"
2611 * if "bmap" already contains upper bounds on "div" as the newly
2612 * introduce lower bounds can then be combined with the pre-existing
2613 * upper bounds. Similarly for upper bounds.
2614 * We therefore first check if "bmap" contains any lower and/or upper bounds
2615 * on "div".
2617 * It is interesting to note that the introduction of these constraints
2618 * can indeed lead to more accurate results, even when compared to
2619 * deriving constraints on the argument of "div" from constraints on "div".
2620 * Consider, for example, the set
2622 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2624 * The second constraint can be rewritten as
2626 * 2 * [(-i-2j+3)/4] + k >= 0
2628 * from which we can derive
2630 * -i - 2j + 3 >= -2k
2632 * or
2634 * i + 2j <= 3 + 2k
2636 * Combined with the first constraint, we obtain
2638 * -3 <= 3 + 2k or k >= -3
2640 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2641 * the first constraint, we obtain
2643 * [(i + 2j)/4] >= [-3/4] = -1
2645 * Combining this constraint with the second constraint, we obtain
2647 * k >= -2
2649 static __isl_give isl_basic_map *insert_bounds_on_div(
2650 __isl_take isl_basic_map *bmap, int div)
2652 int i;
2653 int check_lb, check_ub;
2654 isl_int v;
2655 unsigned total;
2657 if (!bmap)
2658 return NULL;
2660 if (isl_int_is_zero(bmap->div[div][0]))
2661 return bmap;
2663 total = isl_space_dim(bmap->dim, isl_dim_all);
2665 check_lb = 0;
2666 check_ub = 0;
2667 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2668 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2669 if (s > 0)
2670 check_ub = 1;
2671 if (s < 0)
2672 check_lb = 1;
2675 if (!check_lb && !check_ub)
2676 return bmap;
2678 isl_int_init(v);
2680 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2681 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2682 continue;
2684 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2685 check_lb, check_ub);
2688 isl_int_clear(v);
2690 return bmap;
2693 /* Remove all divs (recursively) involving any of the given dimensions
2694 * in their definitions.
2696 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2697 __isl_take isl_basic_map *bmap,
2698 enum isl_dim_type type, unsigned first, unsigned n)
2700 int i;
2702 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2703 return isl_basic_map_free(bmap);
2704 first += isl_basic_map_offset(bmap, type);
2706 for (i = bmap->n_div - 1; i >= 0; --i) {
2707 isl_bool involves;
2709 involves = div_involves_vars(bmap, i, first, n);
2710 if (involves < 0)
2711 return isl_basic_map_free(bmap);
2712 if (!involves)
2713 continue;
2714 bmap = insert_bounds_on_div(bmap, i);
2715 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2716 if (!bmap)
2717 return NULL;
2718 i = bmap->n_div;
2721 return bmap;
2724 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2725 __isl_take isl_basic_set *bset,
2726 enum isl_dim_type type, unsigned first, unsigned n)
2728 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2731 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2732 enum isl_dim_type type, unsigned first, unsigned n)
2734 int i;
2736 if (!map)
2737 return NULL;
2738 if (map->n == 0)
2739 return map;
2741 map = isl_map_cow(map);
2742 if (!map)
2743 return NULL;
2745 for (i = 0; i < map->n; ++i) {
2746 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2747 type, first, n);
2748 if (!map->p[i])
2749 goto error;
2751 return map;
2752 error:
2753 isl_map_free(map);
2754 return NULL;
2757 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2758 enum isl_dim_type type, unsigned first, unsigned n)
2760 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2761 type, first, n));
2764 /* Does the description of "bmap" depend on the specified dimensions?
2765 * We also check whether the dimensions appear in any of the div definitions.
2766 * In principle there is no need for this check. If the dimensions appear
2767 * in a div definition, they also appear in the defining constraints of that
2768 * div.
2770 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2771 enum isl_dim_type type, unsigned first, unsigned n)
2773 int i;
2775 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2776 return isl_bool_error;
2778 first += isl_basic_map_offset(bmap, type);
2779 for (i = 0; i < bmap->n_eq; ++i)
2780 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2781 return isl_bool_true;
2782 for (i = 0; i < bmap->n_ineq; ++i)
2783 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2784 return isl_bool_true;
2785 for (i = 0; i < bmap->n_div; ++i) {
2786 if (isl_int_is_zero(bmap->div[i][0]))
2787 continue;
2788 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2789 return isl_bool_true;
2792 return isl_bool_false;
2795 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2796 enum isl_dim_type type, unsigned first, unsigned n)
2798 int i;
2800 if (isl_map_check_range(map, type, first, n) < 0)
2801 return isl_bool_error;
2803 for (i = 0; i < map->n; ++i) {
2804 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2805 type, first, n);
2806 if (involves < 0 || involves)
2807 return involves;
2810 return isl_bool_false;
2813 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2814 enum isl_dim_type type, unsigned first, unsigned n)
2816 return isl_basic_map_involves_dims(bset, type, first, n);
2819 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2820 enum isl_dim_type type, unsigned first, unsigned n)
2822 return isl_map_involves_dims(set, type, first, n);
2825 /* Drop all constraints in bmap that involve any of the dimensions
2826 * first to first+n-1.
2827 * This function only performs the actual removal of constraints.
2829 * This function should not call finalize since it is used by
2830 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2832 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2833 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2835 int i;
2837 if (n == 0)
2838 return bmap;
2840 bmap = isl_basic_map_cow(bmap);
2842 if (!bmap)
2843 return NULL;
2845 for (i = bmap->n_eq - 1; i >= 0; --i) {
2846 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2847 continue;
2848 isl_basic_map_drop_equality(bmap, i);
2851 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2852 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2853 continue;
2854 isl_basic_map_drop_inequality(bmap, i);
2857 return bmap;
2860 /* Drop all constraints in bset that involve any of the dimensions
2861 * first to first+n-1.
2862 * This function only performs the actual removal of constraints.
2864 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2865 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2867 return isl_basic_map_drop_constraints_involving(bset, first, n);
2870 /* Drop all constraints in bmap that do not involve any of the dimensions
2871 * first to first + n - 1 of the given type.
2873 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2874 __isl_take isl_basic_map *bmap,
2875 enum isl_dim_type type, unsigned first, unsigned n)
2877 int i;
2879 if (n == 0) {
2880 isl_space *space = isl_basic_map_get_space(bmap);
2881 isl_basic_map_free(bmap);
2882 return isl_basic_map_universe(space);
2884 bmap = isl_basic_map_cow(bmap);
2885 if (!bmap)
2886 return NULL;
2888 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2889 return isl_basic_map_free(bmap);
2891 first += isl_basic_map_offset(bmap, type) - 1;
2893 for (i = bmap->n_eq - 1; i >= 0; --i) {
2894 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2895 continue;
2896 isl_basic_map_drop_equality(bmap, i);
2899 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2900 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2901 continue;
2902 isl_basic_map_drop_inequality(bmap, i);
2905 bmap = isl_basic_map_add_known_div_constraints(bmap);
2906 return bmap;
2909 /* Drop all constraints in bset that do not involve any of the dimensions
2910 * first to first + n - 1 of the given type.
2912 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2913 __isl_take isl_basic_set *bset,
2914 enum isl_dim_type type, unsigned first, unsigned n)
2916 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2917 type, first, n);
2920 /* Drop all constraints in bmap that involve any of the dimensions
2921 * first to first + n - 1 of the given type.
2923 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2924 __isl_take isl_basic_map *bmap,
2925 enum isl_dim_type type, unsigned first, unsigned n)
2927 if (!bmap)
2928 return NULL;
2929 if (n == 0)
2930 return bmap;
2932 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2933 return isl_basic_map_free(bmap);
2935 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2936 first += isl_basic_map_offset(bmap, type) - 1;
2937 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2938 bmap = isl_basic_map_add_known_div_constraints(bmap);
2939 return bmap;
2942 /* Drop all constraints in bset that involve any of the dimensions
2943 * first to first + n - 1 of the given type.
2945 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2946 __isl_take isl_basic_set *bset,
2947 enum isl_dim_type type, unsigned first, unsigned n)
2949 return isl_basic_map_drop_constraints_involving_dims(bset,
2950 type, first, n);
2953 /* Drop constraints from "map" by applying "drop" to each basic map.
2955 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2956 enum isl_dim_type type, unsigned first, unsigned n,
2957 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2958 enum isl_dim_type type, unsigned first, unsigned n))
2960 int i;
2962 if (isl_map_check_range(map, type, first, n) < 0)
2963 return isl_map_free(map);
2965 map = isl_map_cow(map);
2966 if (!map)
2967 return NULL;
2969 for (i = 0; i < map->n; ++i) {
2970 map->p[i] = drop(map->p[i], type, first, n);
2971 if (!map->p[i])
2972 return isl_map_free(map);
2975 if (map->n > 1)
2976 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2978 return map;
2981 /* Drop all constraints in map that involve any of the dimensions
2982 * first to first + n - 1 of the given type.
2984 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2985 __isl_take isl_map *map,
2986 enum isl_dim_type type, unsigned first, unsigned n)
2988 if (n == 0)
2989 return map;
2990 return drop_constraints(map, type, first, n,
2991 &isl_basic_map_drop_constraints_involving_dims);
2994 /* Drop all constraints in "map" that do not involve any of the dimensions
2995 * first to first + n - 1 of the given type.
2997 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2998 __isl_take isl_map *map,
2999 enum isl_dim_type type, unsigned first, unsigned n)
3001 if (n == 0) {
3002 isl_space *space = isl_map_get_space(map);
3003 isl_map_free(map);
3004 return isl_map_universe(space);
3006 return drop_constraints(map, type, first, n,
3007 &isl_basic_map_drop_constraints_not_involving_dims);
3010 /* Drop all constraints in set that involve any of the dimensions
3011 * first to first + n - 1 of the given type.
3013 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3014 __isl_take isl_set *set,
3015 enum isl_dim_type type, unsigned first, unsigned n)
3017 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3020 /* Drop all constraints in "set" that do not involve any of the dimensions
3021 * first to first + n - 1 of the given type.
3023 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3024 __isl_take isl_set *set,
3025 enum isl_dim_type type, unsigned first, unsigned n)
3027 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3030 /* Does local variable "div" of "bmap" have a complete explicit representation?
3031 * Having a complete explicit representation requires not only
3032 * an explicit representation, but also that all local variables
3033 * that appear in this explicit representation in turn have
3034 * a complete explicit representation.
3036 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3038 int i;
3039 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3040 isl_bool marked;
3042 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3043 if (marked < 0 || marked)
3044 return isl_bool_not(marked);
3046 for (i = bmap->n_div - 1; i >= 0; --i) {
3047 isl_bool known;
3049 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3050 continue;
3051 known = isl_basic_map_div_is_known(bmap, i);
3052 if (known < 0 || !known)
3053 return known;
3056 return isl_bool_true;
3059 /* Remove all divs that are unknown or defined in terms of unknown divs.
3061 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3062 __isl_take isl_basic_map *bmap)
3064 int i;
3066 if (!bmap)
3067 return NULL;
3069 for (i = bmap->n_div - 1; i >= 0; --i) {
3070 if (isl_basic_map_div_is_known(bmap, i))
3071 continue;
3072 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3073 if (!bmap)
3074 return NULL;
3075 i = bmap->n_div;
3078 return bmap;
3081 /* Remove all divs that are unknown or defined in terms of unknown divs.
3083 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3084 __isl_take isl_basic_set *bset)
3086 return isl_basic_map_remove_unknown_divs(bset);
3089 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3091 int i;
3093 if (!map)
3094 return NULL;
3095 if (map->n == 0)
3096 return map;
3098 map = isl_map_cow(map);
3099 if (!map)
3100 return NULL;
3102 for (i = 0; i < map->n; ++i) {
3103 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3104 if (!map->p[i])
3105 goto error;
3107 return map;
3108 error:
3109 isl_map_free(map);
3110 return NULL;
3113 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3115 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3118 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3119 __isl_take isl_basic_set *bset,
3120 enum isl_dim_type type, unsigned first, unsigned n)
3122 isl_basic_map *bmap = bset_to_bmap(bset);
3123 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3124 return bset_from_bmap(bmap);
3127 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3128 enum isl_dim_type type, unsigned first, unsigned n)
3130 int i;
3132 if (n == 0)
3133 return map;
3135 map = isl_map_cow(map);
3136 if (isl_map_check_range(map, type, first, n) < 0)
3137 return isl_map_free(map);
3139 for (i = 0; i < map->n; ++i) {
3140 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3141 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3142 if (!map->p[i])
3143 goto error;
3145 map = isl_map_drop(map, type, first, n);
3146 return map;
3147 error:
3148 isl_map_free(map);
3149 return NULL;
3152 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3153 enum isl_dim_type type, unsigned first, unsigned n)
3155 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3156 type, first, n));
3159 /* Project out n inputs starting at first using Fourier-Motzkin */
3160 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3161 unsigned first, unsigned n)
3163 return isl_map_remove_dims(map, isl_dim_in, first, n);
3166 static void dump_term(struct isl_basic_map *bmap,
3167 isl_int c, int pos, FILE *out)
3169 const char *name;
3170 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3171 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3172 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3173 if (!pos)
3174 isl_int_print(out, c, 0);
3175 else {
3176 if (!isl_int_is_one(c))
3177 isl_int_print(out, c, 0);
3178 if (pos < 1 + nparam) {
3179 name = isl_space_get_dim_name(bmap->dim,
3180 isl_dim_param, pos - 1);
3181 if (name)
3182 fprintf(out, "%s", name);
3183 else
3184 fprintf(out, "p%d", pos - 1);
3185 } else if (pos < 1 + nparam + in)
3186 fprintf(out, "i%d", pos - 1 - nparam);
3187 else if (pos < 1 + nparam + dim)
3188 fprintf(out, "o%d", pos - 1 - nparam - in);
3189 else
3190 fprintf(out, "e%d", pos - 1 - nparam - dim);
3194 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3195 int sign, FILE *out)
3197 int i;
3198 int first;
3199 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3200 isl_int v;
3202 isl_int_init(v);
3203 for (i = 0, first = 1; i < len; ++i) {
3204 if (isl_int_sgn(c[i]) * sign <= 0)
3205 continue;
3206 if (!first)
3207 fprintf(out, " + ");
3208 first = 0;
3209 isl_int_abs(v, c[i]);
3210 dump_term(bmap, v, i, out);
3212 isl_int_clear(v);
3213 if (first)
3214 fprintf(out, "0");
3217 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3218 const char *op, FILE *out, int indent)
3220 int i;
3222 fprintf(out, "%*s", indent, "");
3224 dump_constraint_sign(bmap, c, 1, out);
3225 fprintf(out, " %s ", op);
3226 dump_constraint_sign(bmap, c, -1, out);
3228 fprintf(out, "\n");
3230 for (i = bmap->n_div; i < bmap->extra; ++i) {
3231 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3232 continue;
3233 fprintf(out, "%*s", indent, "");
3234 fprintf(out, "ERROR: unused div coefficient not zero\n");
3235 abort();
3239 static void dump_constraints(struct isl_basic_map *bmap,
3240 isl_int **c, unsigned n,
3241 const char *op, FILE *out, int indent)
3243 int i;
3245 for (i = 0; i < n; ++i)
3246 dump_constraint(bmap, c[i], op, out, indent);
3249 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3251 int j;
3252 int first = 1;
3253 unsigned total = isl_basic_map_total_dim(bmap);
3255 for (j = 0; j < 1 + total; ++j) {
3256 if (isl_int_is_zero(exp[j]))
3257 continue;
3258 if (!first && isl_int_is_pos(exp[j]))
3259 fprintf(out, "+");
3260 dump_term(bmap, exp[j], j, out);
3261 first = 0;
3265 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3267 int i;
3269 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3270 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3272 for (i = 0; i < bmap->n_div; ++i) {
3273 fprintf(out, "%*s", indent, "");
3274 fprintf(out, "e%d = [(", i);
3275 dump_affine(bmap, bmap->div[i]+1, out);
3276 fprintf(out, ")/");
3277 isl_int_print(out, bmap->div[i][0], 0);
3278 fprintf(out, "]\n");
3282 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3283 FILE *out, int indent)
3285 if (!bset) {
3286 fprintf(out, "null basic set\n");
3287 return;
3290 fprintf(out, "%*s", indent, "");
3291 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3292 bset->ref, bset->dim->nparam, bset->dim->n_out,
3293 bset->extra, bset->flags);
3294 dump(bset_to_bmap(bset), out, indent);
3297 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3298 FILE *out, int indent)
3300 if (!bmap) {
3301 fprintf(out, "null basic map\n");
3302 return;
3305 fprintf(out, "%*s", indent, "");
3306 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3307 "flags: %x, n_name: %d\n",
3308 bmap->ref,
3309 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3310 bmap->extra, bmap->flags, bmap->dim->n_id);
3311 dump(bmap, out, indent);
3314 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3316 unsigned total;
3317 if (!bmap)
3318 return -1;
3319 total = isl_basic_map_total_dim(bmap);
3320 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3321 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3322 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3323 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3324 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3325 return 0;
3328 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3329 unsigned flags)
3331 if (isl_space_check_is_set(space) < 0)
3332 goto error;
3333 return isl_map_alloc_space(space, n, flags);
3334 error:
3335 isl_space_free(space);
3336 return NULL;
3339 /* Make sure "map" has room for at least "n" more basic maps.
3341 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3343 int i;
3344 struct isl_map *grown = NULL;
3346 if (!map)
3347 return NULL;
3348 isl_assert(map->ctx, n >= 0, goto error);
3349 if (map->n + n <= map->size)
3350 return map;
3351 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3352 if (!grown)
3353 goto error;
3354 for (i = 0; i < map->n; ++i) {
3355 grown->p[i] = isl_basic_map_copy(map->p[i]);
3356 if (!grown->p[i])
3357 goto error;
3358 grown->n++;
3360 isl_map_free(map);
3361 return grown;
3362 error:
3363 isl_map_free(grown);
3364 isl_map_free(map);
3365 return NULL;
3368 /* Make sure "set" has room for at least "n" more basic sets.
3370 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3372 return set_from_map(isl_map_grow(set_to_map(set), n));
3375 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3377 return isl_map_from_basic_map(bset);
3380 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3382 struct isl_map *map;
3384 if (!bmap)
3385 return NULL;
3387 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3388 return isl_map_add_basic_map(map, bmap);
3391 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3392 __isl_take isl_basic_set *bset)
3394 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3395 bset_to_bmap(bset)));
3398 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3400 return isl_map_free(set);
3403 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3405 int i;
3407 if (!set) {
3408 fprintf(out, "null set\n");
3409 return;
3412 fprintf(out, "%*s", indent, "");
3413 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3414 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3415 set->flags);
3416 for (i = 0; i < set->n; ++i) {
3417 fprintf(out, "%*s", indent, "");
3418 fprintf(out, "basic set %d:\n", i);
3419 isl_basic_set_print_internal(set->p[i], out, indent+4);
3423 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3425 int i;
3427 if (!map) {
3428 fprintf(out, "null map\n");
3429 return;
3432 fprintf(out, "%*s", indent, "");
3433 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3434 "flags: %x, n_name: %d\n",
3435 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3436 map->dim->n_out, map->flags, map->dim->n_id);
3437 for (i = 0; i < map->n; ++i) {
3438 fprintf(out, "%*s", indent, "");
3439 fprintf(out, "basic map %d:\n", i);
3440 isl_basic_map_print_internal(map->p[i], out, indent+4);
3444 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3445 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3447 struct isl_basic_map *bmap_domain;
3449 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3450 goto error;
3452 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3453 isl_assert(bset->ctx,
3454 isl_basic_map_compatible_domain(bmap, bset), goto error);
3456 bmap = isl_basic_map_cow(bmap);
3457 if (!bmap)
3458 goto error;
3459 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3460 bset->n_div, bset->n_eq, bset->n_ineq);
3461 bmap_domain = isl_basic_map_from_domain(bset);
3462 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3464 bmap = isl_basic_map_simplify(bmap);
3465 return isl_basic_map_finalize(bmap);
3466 error:
3467 isl_basic_map_free(bmap);
3468 isl_basic_set_free(bset);
3469 return NULL;
3472 /* Check that the space of "bset" is the same as that of the range of "bmap".
3474 static isl_stat isl_basic_map_check_compatible_range(
3475 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3477 isl_bool ok;
3479 ok = isl_basic_map_compatible_range(bmap, bset);
3480 if (ok < 0)
3481 return isl_stat_error;
3482 if (!ok)
3483 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3484 "incompatible spaces", return isl_stat_error);
3486 return isl_stat_ok;
3489 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3490 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3492 struct isl_basic_map *bmap_range;
3494 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3495 goto error;
3497 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3498 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3499 goto error;
3501 if (isl_basic_set_plain_is_universe(bset)) {
3502 isl_basic_set_free(bset);
3503 return bmap;
3506 bmap = isl_basic_map_cow(bmap);
3507 if (!bmap)
3508 goto error;
3509 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3510 bset->n_div, bset->n_eq, bset->n_ineq);
3511 bmap_range = bset_to_bmap(bset);
3512 bmap = add_constraints(bmap, bmap_range, 0, 0);
3514 bmap = isl_basic_map_simplify(bmap);
3515 return isl_basic_map_finalize(bmap);
3516 error:
3517 isl_basic_map_free(bmap);
3518 isl_basic_set_free(bset);
3519 return NULL;
3522 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3523 __isl_keep isl_vec *vec)
3525 int i;
3526 unsigned total;
3527 isl_int s;
3529 if (!bmap || !vec)
3530 return isl_bool_error;
3532 total = 1 + isl_basic_map_total_dim(bmap);
3533 if (total != vec->size)
3534 return isl_bool_false;
3536 isl_int_init(s);
3538 for (i = 0; i < bmap->n_eq; ++i) {
3539 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3540 if (!isl_int_is_zero(s)) {
3541 isl_int_clear(s);
3542 return isl_bool_false;
3546 for (i = 0; i < bmap->n_ineq; ++i) {
3547 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3548 if (isl_int_is_neg(s)) {
3549 isl_int_clear(s);
3550 return isl_bool_false;
3554 isl_int_clear(s);
3556 return isl_bool_true;
3559 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3560 __isl_keep isl_vec *vec)
3562 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3565 __isl_give isl_basic_map *isl_basic_map_intersect(
3566 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3568 struct isl_vec *sample = NULL;
3570 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3571 goto error;
3572 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3573 isl_space_dim(bmap1->dim, isl_dim_param) &&
3574 isl_space_dim(bmap2->dim, isl_dim_all) !=
3575 isl_space_dim(bmap2->dim, isl_dim_param))
3576 return isl_basic_map_intersect(bmap2, bmap1);
3578 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3579 isl_space_dim(bmap2->dim, isl_dim_param))
3580 isl_assert(bmap1->ctx,
3581 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3583 if (isl_basic_map_plain_is_empty(bmap1)) {
3584 isl_basic_map_free(bmap2);
3585 return bmap1;
3587 if (isl_basic_map_plain_is_empty(bmap2)) {
3588 isl_basic_map_free(bmap1);
3589 return bmap2;
3592 if (bmap1->sample &&
3593 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3594 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3595 sample = isl_vec_copy(bmap1->sample);
3596 else if (bmap2->sample &&
3597 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3598 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3599 sample = isl_vec_copy(bmap2->sample);
3601 bmap1 = isl_basic_map_cow(bmap1);
3602 if (!bmap1)
3603 goto error;
3604 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3605 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3606 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3608 if (!bmap1)
3609 isl_vec_free(sample);
3610 else if (sample) {
3611 isl_vec_free(bmap1->sample);
3612 bmap1->sample = sample;
3615 bmap1 = isl_basic_map_simplify(bmap1);
3616 return isl_basic_map_finalize(bmap1);
3617 error:
3618 if (sample)
3619 isl_vec_free(sample);
3620 isl_basic_map_free(bmap1);
3621 isl_basic_map_free(bmap2);
3622 return NULL;
3625 struct isl_basic_set *isl_basic_set_intersect(
3626 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3628 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3629 bset_to_bmap(bset2)));
3632 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3633 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3635 return isl_basic_set_intersect(bset1, bset2);
3638 /* Special case of isl_map_intersect, where both map1 and map2
3639 * are convex, without any divs and such that either map1 or map2
3640 * contains a single constraint. This constraint is then simply
3641 * added to the other map.
3643 static __isl_give isl_map *map_intersect_add_constraint(
3644 __isl_take isl_map *map1, __isl_take isl_map *map2)
3646 isl_assert(map1->ctx, map1->n == 1, goto error);
3647 isl_assert(map2->ctx, map1->n == 1, goto error);
3648 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3649 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3651 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3652 return isl_map_intersect(map2, map1);
3654 map1 = isl_map_cow(map1);
3655 if (!map1)
3656 goto error;
3657 if (isl_map_plain_is_empty(map1)) {
3658 isl_map_free(map2);
3659 return map1;
3661 if (map2->p[0]->n_eq == 1)
3662 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3663 else
3664 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3665 map2->p[0]->ineq[0]);
3667 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3668 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3669 if (!map1->p[0])
3670 goto error;
3672 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3673 isl_basic_map_free(map1->p[0]);
3674 map1->n = 0;
3677 isl_map_free(map2);
3679 map1 = isl_map_unmark_normalized(map1);
3680 return map1;
3681 error:
3682 isl_map_free(map1);
3683 isl_map_free(map2);
3684 return NULL;
3687 /* map2 may be either a parameter domain or a map living in the same
3688 * space as map1.
3690 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3691 __isl_take isl_map *map2)
3693 unsigned flags = 0;
3694 isl_bool equal;
3695 isl_map *result;
3696 int i, j;
3698 if (!map1 || !map2)
3699 goto error;
3701 if ((isl_map_plain_is_empty(map1) ||
3702 isl_map_plain_is_universe(map2)) &&
3703 isl_space_is_equal(map1->dim, map2->dim)) {
3704 isl_map_free(map2);
3705 return map1;
3707 if ((isl_map_plain_is_empty(map2) ||
3708 isl_map_plain_is_universe(map1)) &&
3709 isl_space_is_equal(map1->dim, map2->dim)) {
3710 isl_map_free(map1);
3711 return map2;
3714 if (map1->n == 1 && map2->n == 1 &&
3715 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3716 isl_space_is_equal(map1->dim, map2->dim) &&
3717 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3718 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3719 return map_intersect_add_constraint(map1, map2);
3721 equal = isl_map_plain_is_equal(map1, map2);
3722 if (equal < 0)
3723 goto error;
3724 if (equal) {
3725 isl_map_free(map2);
3726 return map1;
3729 if (isl_space_dim(map2->dim, isl_dim_all) !=
3730 isl_space_dim(map2->dim, isl_dim_param))
3731 isl_assert(map1->ctx,
3732 isl_space_is_equal(map1->dim, map2->dim), goto error);
3734 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3735 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3736 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3738 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3739 map1->n * map2->n, flags);
3740 if (!result)
3741 goto error;
3742 for (i = 0; i < map1->n; ++i)
3743 for (j = 0; j < map2->n; ++j) {
3744 struct isl_basic_map *part;
3745 part = isl_basic_map_intersect(
3746 isl_basic_map_copy(map1->p[i]),
3747 isl_basic_map_copy(map2->p[j]));
3748 if (isl_basic_map_is_empty(part) < 0)
3749 part = isl_basic_map_free(part);
3750 result = isl_map_add_basic_map(result, part);
3751 if (!result)
3752 goto error;
3754 isl_map_free(map1);
3755 isl_map_free(map2);
3756 return result;
3757 error:
3758 isl_map_free(map1);
3759 isl_map_free(map2);
3760 return NULL;
3763 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3764 __isl_take isl_map *map2)
3766 if (!map1 || !map2)
3767 goto error;
3768 if (!isl_space_is_equal(map1->dim, map2->dim))
3769 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3770 "spaces don't match", goto error);
3771 return map_intersect_internal(map1, map2);
3772 error:
3773 isl_map_free(map1);
3774 isl_map_free(map2);
3775 return NULL;
3778 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3779 __isl_take isl_map *map2)
3781 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3784 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3786 return set_from_map(isl_map_intersect(set_to_map(set1),
3787 set_to_map(set2)));
3790 /* map_intersect_internal accepts intersections
3791 * with parameter domains, so we can just call that function.
3793 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3794 __isl_take isl_set *params)
3796 return map_intersect_internal(map, params);
3799 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3800 __isl_take isl_map *map2)
3802 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3805 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3806 __isl_take isl_set *params)
3808 return isl_map_intersect_params(set, params);
3811 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3813 isl_space *space;
3814 unsigned pos, n1, n2;
3816 if (!bmap)
3817 return NULL;
3818 bmap = isl_basic_map_cow(bmap);
3819 if (!bmap)
3820 return NULL;
3821 space = isl_space_reverse(isl_space_copy(bmap->dim));
3822 pos = isl_basic_map_offset(bmap, isl_dim_in);
3823 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3824 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3825 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3826 return isl_basic_map_reset_space(bmap, space);
3829 static __isl_give isl_basic_map *basic_map_space_reset(
3830 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3832 isl_space *space;
3834 if (!bmap)
3835 return NULL;
3836 if (!isl_space_is_named_or_nested(bmap->dim, type))
3837 return bmap;
3839 space = isl_basic_map_get_space(bmap);
3840 space = isl_space_reset(space, type);
3841 bmap = isl_basic_map_reset_space(bmap, space);
3842 return bmap;
3845 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3846 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3847 unsigned pos, unsigned n)
3849 isl_bool rational, is_empty;
3850 isl_space *res_space;
3851 struct isl_basic_map *res;
3852 struct isl_dim_map *dim_map;
3853 unsigned total, off;
3854 enum isl_dim_type t;
3856 if (n == 0)
3857 return basic_map_space_reset(bmap, type);
3859 is_empty = isl_basic_map_plain_is_empty(bmap);
3860 if (is_empty < 0)
3861 return isl_basic_map_free(bmap);
3862 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3863 type, pos, n);
3864 if (!res_space)
3865 return isl_basic_map_free(bmap);
3866 if (is_empty) {
3867 isl_basic_map_free(bmap);
3868 return isl_basic_map_empty(res_space);
3871 total = isl_basic_map_total_dim(bmap) + n;
3872 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3873 off = 0;
3874 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3875 if (t != type) {
3876 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3877 } else {
3878 unsigned size = isl_basic_map_dim(bmap, t);
3879 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3880 0, pos, off);
3881 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3882 pos, size - pos, off + pos + n);
3884 off += isl_space_dim(res_space, t);
3886 isl_dim_map_div(dim_map, bmap, off);
3888 res = isl_basic_map_alloc_space(res_space,
3889 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3890 rational = isl_basic_map_is_rational(bmap);
3891 if (rational < 0)
3892 res = isl_basic_map_free(res);
3893 if (rational)
3894 res = isl_basic_map_set_rational(res);
3895 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3896 return isl_basic_map_finalize(res);
3899 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3900 __isl_take isl_basic_set *bset,
3901 enum isl_dim_type type, unsigned pos, unsigned n)
3903 return isl_basic_map_insert_dims(bset, type, pos, n);
3906 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3907 enum isl_dim_type type, unsigned n)
3909 if (!bmap)
3910 return NULL;
3911 return isl_basic_map_insert_dims(bmap, type,
3912 isl_basic_map_dim(bmap, type), n);
3915 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3916 enum isl_dim_type type, unsigned n)
3918 if (!bset)
3919 return NULL;
3920 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3921 return isl_basic_map_add_dims(bset, type, n);
3922 error:
3923 isl_basic_set_free(bset);
3924 return NULL;
3927 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3928 enum isl_dim_type type)
3930 isl_space *space;
3932 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3933 return map;
3935 space = isl_map_get_space(map);
3936 space = isl_space_reset(space, type);
3937 map = isl_map_reset_space(map, space);
3938 return map;
3941 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3942 enum isl_dim_type type, unsigned pos, unsigned n)
3944 int i;
3946 if (n == 0)
3947 return map_space_reset(map, type);
3949 map = isl_map_cow(map);
3950 if (!map)
3951 return NULL;
3953 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3954 if (!map->dim)
3955 goto error;
3957 for (i = 0; i < map->n; ++i) {
3958 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3959 if (!map->p[i])
3960 goto error;
3963 return map;
3964 error:
3965 isl_map_free(map);
3966 return NULL;
3969 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3970 enum isl_dim_type type, unsigned pos, unsigned n)
3972 return isl_map_insert_dims(set, type, pos, n);
3975 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3976 enum isl_dim_type type, unsigned n)
3978 if (!map)
3979 return NULL;
3980 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3983 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3984 enum isl_dim_type type, unsigned n)
3986 if (!set)
3987 return NULL;
3988 isl_assert(set->ctx, type != isl_dim_in, goto error);
3989 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3990 error:
3991 isl_set_free(set);
3992 return NULL;
3995 __isl_give isl_basic_map *isl_basic_map_move_dims(
3996 __isl_take isl_basic_map *bmap,
3997 enum isl_dim_type dst_type, unsigned dst_pos,
3998 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4000 struct isl_dim_map *dim_map;
4001 struct isl_basic_map *res;
4002 enum isl_dim_type t;
4003 unsigned total, off;
4005 if (!bmap)
4006 return NULL;
4007 if (n == 0) {
4008 bmap = isl_basic_map_reset(bmap, src_type);
4009 bmap = isl_basic_map_reset(bmap, dst_type);
4010 return bmap;
4013 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4014 return isl_basic_map_free(bmap);
4016 if (dst_type == src_type && dst_pos == src_pos)
4017 return bmap;
4019 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4021 if (pos(bmap->dim, dst_type) + dst_pos ==
4022 pos(bmap->dim, src_type) + src_pos +
4023 ((src_type < dst_type) ? n : 0)) {
4024 bmap = isl_basic_map_cow(bmap);
4025 if (!bmap)
4026 return NULL;
4028 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4029 src_type, src_pos, n);
4030 if (!bmap->dim)
4031 goto error;
4033 bmap = isl_basic_map_finalize(bmap);
4035 return bmap;
4038 total = isl_basic_map_total_dim(bmap);
4039 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4041 off = 0;
4042 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4043 unsigned size = isl_space_dim(bmap->dim, t);
4044 if (t == dst_type) {
4045 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4046 0, dst_pos, off);
4047 off += dst_pos;
4048 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4049 src_pos, n, off);
4050 off += n;
4051 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4052 dst_pos, size - dst_pos, off);
4053 off += size - dst_pos;
4054 } else if (t == src_type) {
4055 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4056 0, src_pos, off);
4057 off += src_pos;
4058 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4059 src_pos + n, size - src_pos - n, off);
4060 off += size - src_pos - n;
4061 } else {
4062 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4063 off += size;
4066 isl_dim_map_div(dim_map, bmap, off);
4068 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4069 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4070 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4071 if (!bmap)
4072 goto error;
4074 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4075 src_type, src_pos, n);
4076 if (!bmap->dim)
4077 goto error;
4079 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4080 bmap = isl_basic_map_gauss(bmap, NULL);
4081 bmap = isl_basic_map_finalize(bmap);
4083 return bmap;
4084 error:
4085 isl_basic_map_free(bmap);
4086 return NULL;
4089 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4090 enum isl_dim_type dst_type, unsigned dst_pos,
4091 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4093 isl_basic_map *bmap = bset_to_bmap(bset);
4094 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4095 src_type, src_pos, n);
4096 return bset_from_bmap(bmap);
4099 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4100 enum isl_dim_type dst_type, unsigned dst_pos,
4101 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4103 if (!set)
4104 return NULL;
4105 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4106 return set_from_map(isl_map_move_dims(set_to_map(set),
4107 dst_type, dst_pos, src_type, src_pos, n));
4108 error:
4109 isl_set_free(set);
4110 return NULL;
4113 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4114 enum isl_dim_type dst_type, unsigned dst_pos,
4115 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4117 int i;
4119 if (n == 0) {
4120 map = isl_map_reset(map, src_type);
4121 map = isl_map_reset(map, dst_type);
4122 return map;
4125 if (isl_map_check_range(map, src_type, src_pos, n))
4126 return isl_map_free(map);
4128 if (dst_type == src_type && dst_pos == src_pos)
4129 return map;
4131 isl_assert(map->ctx, dst_type != src_type, goto error);
4133 map = isl_map_cow(map);
4134 if (!map)
4135 return NULL;
4137 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4138 if (!map->dim)
4139 goto error;
4141 for (i = 0; i < map->n; ++i) {
4142 map->p[i] = isl_basic_map_move_dims(map->p[i],
4143 dst_type, dst_pos,
4144 src_type, src_pos, n);
4145 if (!map->p[i])
4146 goto error;
4149 return map;
4150 error:
4151 isl_map_free(map);
4152 return NULL;
4155 /* Move the specified dimensions to the last columns right before
4156 * the divs. Don't change the dimension specification of bmap.
4157 * That's the responsibility of the caller.
4159 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4160 enum isl_dim_type type, unsigned first, unsigned n)
4162 struct isl_dim_map *dim_map;
4163 struct isl_basic_map *res;
4164 enum isl_dim_type t;
4165 unsigned total, off;
4167 if (!bmap)
4168 return NULL;
4169 if (pos(bmap->dim, type) + first + n ==
4170 1 + isl_space_dim(bmap->dim, isl_dim_all))
4171 return bmap;
4173 total = isl_basic_map_total_dim(bmap);
4174 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4176 off = 0;
4177 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4178 unsigned size = isl_space_dim(bmap->dim, t);
4179 if (t == type) {
4180 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4181 0, first, off);
4182 off += first;
4183 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4184 first, n, total - bmap->n_div - n);
4185 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4186 first + n, size - (first + n), off);
4187 off += size - (first + n);
4188 } else {
4189 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4190 off += size;
4193 isl_dim_map_div(dim_map, bmap, off + n);
4195 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4196 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4197 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4198 return res;
4201 /* Insert "n" rows in the divs of "bmap".
4203 * The number of columns is not changed, which means that the last
4204 * dimensions of "bmap" are being reintepreted as the new divs.
4205 * The space of "bmap" is not adjusted, however, which means
4206 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4207 * from the space of "bmap" is the responsibility of the caller.
4209 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4210 int n)
4212 int i;
4213 size_t row_size;
4214 isl_int **new_div;
4215 isl_int *old;
4217 bmap = isl_basic_map_cow(bmap);
4218 if (!bmap)
4219 return NULL;
4221 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4222 old = bmap->block2.data;
4223 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4224 (bmap->extra + n) * (1 + row_size));
4225 if (!bmap->block2.data)
4226 return isl_basic_map_free(bmap);
4227 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4228 if (!new_div)
4229 return isl_basic_map_free(bmap);
4230 for (i = 0; i < n; ++i) {
4231 new_div[i] = bmap->block2.data +
4232 (bmap->extra + i) * (1 + row_size);
4233 isl_seq_clr(new_div[i], 1 + row_size);
4235 for (i = 0; i < bmap->extra; ++i)
4236 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4237 free(bmap->div);
4238 bmap->div = new_div;
4239 bmap->n_div += n;
4240 bmap->extra += n;
4242 return bmap;
4245 /* Drop constraints from "bmap" that only involve the variables
4246 * of "type" in the range [first, first + n] that are not related
4247 * to any of the variables outside that interval.
4248 * These constraints cannot influence the values for the variables
4249 * outside the interval, except in case they cause "bmap" to be empty.
4250 * Only drop the constraints if "bmap" is known to be non-empty.
4252 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4253 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4254 unsigned first, unsigned n)
4256 int i;
4257 int *groups;
4258 unsigned dim, n_div;
4259 isl_bool non_empty;
4261 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4262 if (non_empty < 0)
4263 return isl_basic_map_free(bmap);
4264 if (!non_empty)
4265 return bmap;
4267 dim = isl_basic_map_dim(bmap, isl_dim_all);
4268 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4269 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4270 if (!groups)
4271 return isl_basic_map_free(bmap);
4272 first += isl_basic_map_offset(bmap, type) - 1;
4273 for (i = 0; i < first; ++i)
4274 groups[i] = -1;
4275 for (i = first + n; i < dim - n_div; ++i)
4276 groups[i] = -1;
4278 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4280 return bmap;
4283 /* Turn the n dimensions of type type, starting at first
4284 * into existentially quantified variables.
4286 * If a subset of the projected out variables are unrelated
4287 * to any of the variables that remain, then the constraints
4288 * involving this subset are simply dropped first.
4290 __isl_give isl_basic_map *isl_basic_map_project_out(
4291 __isl_take isl_basic_map *bmap,
4292 enum isl_dim_type type, unsigned first, unsigned n)
4294 isl_bool empty;
4296 if (n == 0)
4297 return basic_map_space_reset(bmap, type);
4298 if (type == isl_dim_div)
4299 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4300 "cannot project out existentially quantified variables",
4301 return isl_basic_map_free(bmap));
4303 empty = isl_basic_map_plain_is_empty(bmap);
4304 if (empty < 0)
4305 return isl_basic_map_free(bmap);
4306 if (empty)
4307 bmap = isl_basic_map_set_to_empty(bmap);
4309 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4310 if (!bmap)
4311 return NULL;
4313 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4314 return isl_basic_map_remove_dims(bmap, type, first, n);
4316 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4317 return isl_basic_map_free(bmap);
4319 bmap = move_last(bmap, type, first, n);
4320 bmap = isl_basic_map_cow(bmap);
4321 bmap = insert_div_rows(bmap, n);
4322 if (!bmap)
4323 return NULL;
4325 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4326 if (!bmap->dim)
4327 goto error;
4328 bmap = isl_basic_map_simplify(bmap);
4329 bmap = isl_basic_map_drop_redundant_divs(bmap);
4330 return isl_basic_map_finalize(bmap);
4331 error:
4332 isl_basic_map_free(bmap);
4333 return NULL;
4336 /* Turn the n dimensions of type type, starting at first
4337 * into existentially quantified variables.
4339 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4340 enum isl_dim_type type, unsigned first, unsigned n)
4342 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4343 type, first, n));
4346 /* Turn the n dimensions of type type, starting at first
4347 * into existentially quantified variables.
4349 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4350 enum isl_dim_type type, unsigned first, unsigned n)
4352 int i;
4354 if (n == 0)
4355 return map_space_reset(map, type);
4357 if (isl_map_check_range(map, type, first, n) < 0)
4358 return isl_map_free(map);
4360 map = isl_map_cow(map);
4361 if (!map)
4362 return NULL;
4364 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4365 if (!map->dim)
4366 goto error;
4368 for (i = 0; i < map->n; ++i) {
4369 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4370 if (!map->p[i])
4371 goto error;
4374 return map;
4375 error:
4376 isl_map_free(map);
4377 return NULL;
4380 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4381 * into existentially quantified variables.
4383 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4384 enum isl_dim_type type, unsigned first, unsigned n)
4386 unsigned dim;
4388 if (isl_map_check_range(map, type, first, n) < 0)
4389 return isl_map_free(map);
4390 dim = isl_map_dim(map, type);
4391 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4392 map = isl_map_project_out(map, type, 0, first);
4393 return map;
4396 /* Turn the n dimensions of type type, starting at first
4397 * into existentially quantified variables.
4399 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4400 enum isl_dim_type type, unsigned first, unsigned n)
4402 return set_from_map(isl_map_project_out(set_to_map(set),
4403 type, first, n));
4406 /* Return a map that projects the elements in "set" onto their
4407 * "n" set dimensions starting at "first".
4408 * "type" should be equal to isl_dim_set.
4410 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4411 enum isl_dim_type type, unsigned first, unsigned n)
4413 int i;
4414 isl_map *map;
4416 if (type != isl_dim_set)
4417 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4418 "only set dimensions can be projected out", goto error);
4419 if (isl_set_check_range(set, type, first, n) < 0)
4420 return isl_set_free(set);
4422 map = isl_map_from_domain(set);
4423 map = isl_map_add_dims(map, isl_dim_out, n);
4424 for (i = 0; i < n; ++i)
4425 map = isl_map_equate(map, isl_dim_in, first + i,
4426 isl_dim_out, i);
4427 return map;
4428 error:
4429 isl_set_free(set);
4430 return NULL;
4433 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4434 unsigned n)
4436 int i, j;
4438 for (i = 0; i < n; ++i) {
4439 j = isl_basic_map_alloc_div(bmap);
4440 if (j < 0)
4441 goto error;
4442 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4444 return bmap;
4445 error:
4446 isl_basic_map_free(bmap);
4447 return NULL;
4450 struct isl_basic_map *isl_basic_map_apply_range(
4451 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4453 isl_space *space_result = NULL;
4454 struct isl_basic_map *bmap;
4455 unsigned n_in, n_out, n, nparam, total, pos;
4456 struct isl_dim_map *dim_map1, *dim_map2;
4458 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4459 goto error;
4460 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4461 bmap2->dim, isl_dim_in))
4462 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4463 "spaces don't match", goto error);
4465 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4466 isl_space_copy(bmap2->dim));
4468 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4469 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4470 n = isl_basic_map_dim(bmap1, isl_dim_out);
4471 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4473 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4474 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4475 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4476 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4477 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4478 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4479 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4480 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4481 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4482 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4483 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4485 bmap = isl_basic_map_alloc_space(space_result,
4486 bmap1->n_div + bmap2->n_div + n,
4487 bmap1->n_eq + bmap2->n_eq,
4488 bmap1->n_ineq + bmap2->n_ineq);
4489 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4490 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4491 bmap = add_divs(bmap, n);
4492 bmap = isl_basic_map_simplify(bmap);
4493 bmap = isl_basic_map_drop_redundant_divs(bmap);
4494 return isl_basic_map_finalize(bmap);
4495 error:
4496 isl_basic_map_free(bmap1);
4497 isl_basic_map_free(bmap2);
4498 return NULL;
4501 struct isl_basic_set *isl_basic_set_apply(
4502 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4504 if (!bset || !bmap)
4505 goto error;
4507 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4508 goto error);
4510 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4511 bmap));
4512 error:
4513 isl_basic_set_free(bset);
4514 isl_basic_map_free(bmap);
4515 return NULL;
4518 struct isl_basic_map *isl_basic_map_apply_domain(
4519 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4521 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4522 goto error;
4523 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4524 bmap2->dim, isl_dim_in))
4525 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4526 "spaces don't match", goto error);
4528 bmap1 = isl_basic_map_reverse(bmap1);
4529 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4530 return isl_basic_map_reverse(bmap1);
4531 error:
4532 isl_basic_map_free(bmap1);
4533 isl_basic_map_free(bmap2);
4534 return NULL;
4537 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4538 * A \cap B -> f(A) + f(B)
4540 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4541 __isl_take isl_basic_map *bmap2)
4543 unsigned n_in, n_out, nparam, total, pos;
4544 struct isl_basic_map *bmap = NULL;
4545 struct isl_dim_map *dim_map1, *dim_map2;
4546 int i;
4548 if (!bmap1 || !bmap2)
4549 goto error;
4551 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4552 goto error);
4554 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4555 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4556 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4558 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4559 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4560 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4561 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4562 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4563 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4564 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4565 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4566 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4567 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4568 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4570 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4571 bmap1->n_div + bmap2->n_div + 2 * n_out,
4572 bmap1->n_eq + bmap2->n_eq + n_out,
4573 bmap1->n_ineq + bmap2->n_ineq);
4574 for (i = 0; i < n_out; ++i) {
4575 int j = isl_basic_map_alloc_equality(bmap);
4576 if (j < 0)
4577 goto error;
4578 isl_seq_clr(bmap->eq[j], 1+total);
4579 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4580 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4581 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4583 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4584 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4585 bmap = add_divs(bmap, 2 * n_out);
4587 bmap = isl_basic_map_simplify(bmap);
4588 return isl_basic_map_finalize(bmap);
4589 error:
4590 isl_basic_map_free(bmap);
4591 isl_basic_map_free(bmap1);
4592 isl_basic_map_free(bmap2);
4593 return NULL;
4596 /* Given two maps A -> f(A) and B -> g(B), construct a map
4597 * A \cap B -> f(A) + f(B)
4599 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4600 __isl_take isl_map *map2)
4602 struct isl_map *result;
4603 int i, j;
4605 if (!map1 || !map2)
4606 goto error;
4608 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4610 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4611 map1->n * map2->n, 0);
4612 if (!result)
4613 goto error;
4614 for (i = 0; i < map1->n; ++i)
4615 for (j = 0; j < map2->n; ++j) {
4616 struct isl_basic_map *part;
4617 part = isl_basic_map_sum(
4618 isl_basic_map_copy(map1->p[i]),
4619 isl_basic_map_copy(map2->p[j]));
4620 if (isl_basic_map_is_empty(part))
4621 isl_basic_map_free(part);
4622 else
4623 result = isl_map_add_basic_map(result, part);
4624 if (!result)
4625 goto error;
4627 isl_map_free(map1);
4628 isl_map_free(map2);
4629 return result;
4630 error:
4631 isl_map_free(map1);
4632 isl_map_free(map2);
4633 return NULL;
4636 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4637 __isl_take isl_set *set2)
4639 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4642 /* Given a basic map A -> f(A), construct A -> -f(A).
4644 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4646 int i, j;
4647 unsigned off, n;
4649 bmap = isl_basic_map_cow(bmap);
4650 if (!bmap)
4651 return NULL;
4653 n = isl_basic_map_dim(bmap, isl_dim_out);
4654 off = isl_basic_map_offset(bmap, isl_dim_out);
4655 for (i = 0; i < bmap->n_eq; ++i)
4656 for (j = 0; j < n; ++j)
4657 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4658 for (i = 0; i < bmap->n_ineq; ++i)
4659 for (j = 0; j < n; ++j)
4660 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4661 for (i = 0; i < bmap->n_div; ++i)
4662 for (j = 0; j < n; ++j)
4663 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4664 bmap = isl_basic_map_gauss(bmap, NULL);
4665 return isl_basic_map_finalize(bmap);
4668 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4670 return isl_basic_map_neg(bset);
4673 /* Given a map A -> f(A), construct A -> -f(A).
4675 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4677 int i;
4679 map = isl_map_cow(map);
4680 if (!map)
4681 return NULL;
4683 for (i = 0; i < map->n; ++i) {
4684 map->p[i] = isl_basic_map_neg(map->p[i]);
4685 if (!map->p[i])
4686 goto error;
4689 return map;
4690 error:
4691 isl_map_free(map);
4692 return NULL;
4695 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4697 return set_from_map(isl_map_neg(set_to_map(set)));
4700 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4701 * A -> floor(f(A)/d).
4703 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4704 isl_int d)
4706 unsigned n_in, n_out, nparam, total, pos;
4707 struct isl_basic_map *result = NULL;
4708 struct isl_dim_map *dim_map;
4709 int i;
4711 if (!bmap)
4712 return NULL;
4714 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4715 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4716 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4718 total = nparam + n_in + n_out + bmap->n_div + n_out;
4719 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4720 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4721 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4722 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4723 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4725 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4726 bmap->n_div + n_out,
4727 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4728 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4729 result = add_divs(result, n_out);
4730 for (i = 0; i < n_out; ++i) {
4731 int j;
4732 j = isl_basic_map_alloc_inequality(result);
4733 if (j < 0)
4734 goto error;
4735 isl_seq_clr(result->ineq[j], 1+total);
4736 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4737 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4738 j = isl_basic_map_alloc_inequality(result);
4739 if (j < 0)
4740 goto error;
4741 isl_seq_clr(result->ineq[j], 1+total);
4742 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4743 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4744 isl_int_sub_ui(result->ineq[j][0], d, 1);
4747 result = isl_basic_map_simplify(result);
4748 return isl_basic_map_finalize(result);
4749 error:
4750 isl_basic_map_free(result);
4751 return NULL;
4754 /* Given a map A -> f(A) and an integer d, construct a map
4755 * A -> floor(f(A)/d).
4757 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4759 int i;
4761 map = isl_map_cow(map);
4762 if (!map)
4763 return NULL;
4765 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4766 for (i = 0; i < map->n; ++i) {
4767 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4768 if (!map->p[i])
4769 goto error;
4771 map = isl_map_unmark_normalized(map);
4773 return map;
4774 error:
4775 isl_map_free(map);
4776 return NULL;
4779 /* Given a map A -> f(A) and an integer d, construct a map
4780 * A -> floor(f(A)/d).
4782 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4783 __isl_take isl_val *d)
4785 if (!map || !d)
4786 goto error;
4787 if (!isl_val_is_int(d))
4788 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4789 "expecting integer denominator", goto error);
4790 map = isl_map_floordiv(map, d->n);
4791 isl_val_free(d);
4792 return map;
4793 error:
4794 isl_map_free(map);
4795 isl_val_free(d);
4796 return NULL;
4799 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4800 unsigned pos)
4802 int i;
4803 unsigned nparam;
4804 unsigned n_in;
4806 i = isl_basic_map_alloc_equality(bmap);
4807 if (i < 0)
4808 goto error;
4809 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4810 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4811 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4812 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4813 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4814 return isl_basic_map_finalize(bmap);
4815 error:
4816 isl_basic_map_free(bmap);
4817 return NULL;
4820 /* Add a constraint to "bmap" expressing i_pos < o_pos
4822 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4823 unsigned pos)
4825 int i;
4826 unsigned nparam;
4827 unsigned n_in;
4829 i = isl_basic_map_alloc_inequality(bmap);
4830 if (i < 0)
4831 goto error;
4832 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4833 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4834 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4835 isl_int_set_si(bmap->ineq[i][0], -1);
4836 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4837 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4838 return isl_basic_map_finalize(bmap);
4839 error:
4840 isl_basic_map_free(bmap);
4841 return NULL;
4844 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4846 static __isl_give isl_basic_map *var_less_or_equal(
4847 __isl_take isl_basic_map *bmap, unsigned pos)
4849 int i;
4850 unsigned nparam;
4851 unsigned n_in;
4853 i = isl_basic_map_alloc_inequality(bmap);
4854 if (i < 0)
4855 goto error;
4856 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4857 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4858 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4859 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4860 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4861 return isl_basic_map_finalize(bmap);
4862 error:
4863 isl_basic_map_free(bmap);
4864 return NULL;
4867 /* Add a constraint to "bmap" expressing i_pos > o_pos
4869 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4870 unsigned pos)
4872 int i;
4873 unsigned nparam;
4874 unsigned n_in;
4876 i = isl_basic_map_alloc_inequality(bmap);
4877 if (i < 0)
4878 goto error;
4879 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4880 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4881 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4882 isl_int_set_si(bmap->ineq[i][0], -1);
4883 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4884 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4885 return isl_basic_map_finalize(bmap);
4886 error:
4887 isl_basic_map_free(bmap);
4888 return NULL;
4891 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4893 static __isl_give isl_basic_map *var_more_or_equal(
4894 __isl_take isl_basic_map *bmap, unsigned pos)
4896 int i;
4897 unsigned nparam;
4898 unsigned n_in;
4900 i = isl_basic_map_alloc_inequality(bmap);
4901 if (i < 0)
4902 goto error;
4903 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4904 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4905 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4906 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4907 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4908 return isl_basic_map_finalize(bmap);
4909 error:
4910 isl_basic_map_free(bmap);
4911 return NULL;
4914 __isl_give isl_basic_map *isl_basic_map_equal(
4915 __isl_take isl_space *space, unsigned n_equal)
4917 int i;
4918 struct isl_basic_map *bmap;
4919 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4920 if (!bmap)
4921 return NULL;
4922 for (i = 0; i < n_equal && bmap; ++i)
4923 bmap = var_equal(bmap, i);
4924 return isl_basic_map_finalize(bmap);
4927 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4929 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4930 unsigned pos)
4932 int i;
4933 struct isl_basic_map *bmap;
4934 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4935 if (!bmap)
4936 return NULL;
4937 for (i = 0; i < pos && bmap; ++i)
4938 bmap = var_equal(bmap, i);
4939 if (bmap)
4940 bmap = var_less(bmap, pos);
4941 return isl_basic_map_finalize(bmap);
4944 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4946 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4947 __isl_take isl_space *space, unsigned pos)
4949 int i;
4950 isl_basic_map *bmap;
4952 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4953 for (i = 0; i < pos; ++i)
4954 bmap = var_equal(bmap, i);
4955 bmap = var_less_or_equal(bmap, pos);
4956 return isl_basic_map_finalize(bmap);
4959 /* Return a relation on "space" expressing i_pos > o_pos
4961 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4962 unsigned pos)
4964 int i;
4965 struct isl_basic_map *bmap;
4966 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4967 if (!bmap)
4968 return NULL;
4969 for (i = 0; i < pos && bmap; ++i)
4970 bmap = var_equal(bmap, i);
4971 if (bmap)
4972 bmap = var_more(bmap, pos);
4973 return isl_basic_map_finalize(bmap);
4976 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4978 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4979 __isl_take isl_space *space, unsigned pos)
4981 int i;
4982 isl_basic_map *bmap;
4984 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4985 for (i = 0; i < pos; ++i)
4986 bmap = var_equal(bmap, i);
4987 bmap = var_more_or_equal(bmap, pos);
4988 return isl_basic_map_finalize(bmap);
4991 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4992 unsigned n, int equal)
4994 struct isl_map *map;
4995 int i;
4997 if (n == 0 && equal)
4998 return isl_map_universe(space);
5000 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5002 for (i = 0; i + 1 < n; ++i)
5003 map = isl_map_add_basic_map(map,
5004 isl_basic_map_less_at(isl_space_copy(space), i));
5005 if (n > 0) {
5006 if (equal)
5007 map = isl_map_add_basic_map(map,
5008 isl_basic_map_less_or_equal_at(space, n - 1));
5009 else
5010 map = isl_map_add_basic_map(map,
5011 isl_basic_map_less_at(space, n - 1));
5012 } else
5013 isl_space_free(space);
5015 return map;
5018 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5020 if (!space)
5021 return NULL;
5022 return map_lex_lte_first(space, space->n_out, equal);
5025 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5027 return map_lex_lte_first(dim, n, 0);
5030 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5032 return map_lex_lte_first(dim, n, 1);
5035 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5037 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5040 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5042 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5045 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5046 unsigned n, int equal)
5048 struct isl_map *map;
5049 int i;
5051 if (n == 0 && equal)
5052 return isl_map_universe(space);
5054 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5056 for (i = 0; i + 1 < n; ++i)
5057 map = isl_map_add_basic_map(map,
5058 isl_basic_map_more_at(isl_space_copy(space), i));
5059 if (n > 0) {
5060 if (equal)
5061 map = isl_map_add_basic_map(map,
5062 isl_basic_map_more_or_equal_at(space, n - 1));
5063 else
5064 map = isl_map_add_basic_map(map,
5065 isl_basic_map_more_at(space, n - 1));
5066 } else
5067 isl_space_free(space);
5069 return map;
5072 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5074 if (!space)
5075 return NULL;
5076 return map_lex_gte_first(space, space->n_out, equal);
5079 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5081 return map_lex_gte_first(dim, n, 0);
5084 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5086 return map_lex_gte_first(dim, n, 1);
5089 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5091 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5094 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5096 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5099 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5100 __isl_take isl_set *set2)
5102 isl_map *map;
5103 map = isl_map_lex_le(isl_set_get_space(set1));
5104 map = isl_map_intersect_domain(map, set1);
5105 map = isl_map_intersect_range(map, set2);
5106 return map;
5109 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5110 __isl_take isl_set *set2)
5112 isl_map *map;
5113 map = isl_map_lex_lt(isl_set_get_space(set1));
5114 map = isl_map_intersect_domain(map, set1);
5115 map = isl_map_intersect_range(map, set2);
5116 return map;
5119 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5120 __isl_take isl_set *set2)
5122 isl_map *map;
5123 map = isl_map_lex_ge(isl_set_get_space(set1));
5124 map = isl_map_intersect_domain(map, set1);
5125 map = isl_map_intersect_range(map, set2);
5126 return map;
5129 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5130 __isl_take isl_set *set2)
5132 isl_map *map;
5133 map = isl_map_lex_gt(isl_set_get_space(set1));
5134 map = isl_map_intersect_domain(map, set1);
5135 map = isl_map_intersect_range(map, set2);
5136 return map;
5139 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5140 __isl_take isl_map *map2)
5142 isl_map *map;
5143 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5144 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5145 map = isl_map_apply_range(map, isl_map_reverse(map2));
5146 return map;
5149 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5150 __isl_take isl_map *map2)
5152 isl_map *map;
5153 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5154 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5155 map = isl_map_apply_range(map, isl_map_reverse(map2));
5156 return map;
5159 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5160 __isl_take isl_map *map2)
5162 isl_map *map;
5163 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5164 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5165 map = isl_map_apply_range(map, isl_map_reverse(map2));
5166 return map;
5169 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5170 __isl_take isl_map *map2)
5172 isl_map *map;
5173 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5174 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5175 map = isl_map_apply_range(map, isl_map_reverse(map2));
5176 return map;
5179 /* For a div d = floor(f/m), add the constraint
5181 * f - m d >= 0
5183 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5184 unsigned pos, isl_int *div)
5186 int i;
5187 unsigned total = isl_basic_map_total_dim(bmap);
5189 i = isl_basic_map_alloc_inequality(bmap);
5190 if (i < 0)
5191 return isl_stat_error;
5192 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5193 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5195 return isl_stat_ok;
5198 /* For a div d = floor(f/m), add the constraint
5200 * -(f-(m-1)) + m d >= 0
5202 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5203 unsigned pos, isl_int *div)
5205 int i;
5206 unsigned total = isl_basic_map_total_dim(bmap);
5208 i = isl_basic_map_alloc_inequality(bmap);
5209 if (i < 0)
5210 return isl_stat_error;
5211 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5212 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5213 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5214 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5216 return isl_stat_ok;
5219 /* For a div d = floor(f/m), add the constraints
5221 * f - m d >= 0
5222 * -(f-(m-1)) + m d >= 0
5224 * Note that the second constraint is the negation of
5226 * f - m d >= m
5228 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5229 unsigned pos, isl_int *div)
5231 if (add_upper_div_constraint(bmap, pos, div) < 0)
5232 return -1;
5233 if (add_lower_div_constraint(bmap, pos, div) < 0)
5234 return -1;
5235 return 0;
5238 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5239 unsigned pos, isl_int *div)
5241 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5242 pos, div);
5245 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map *bmap,
5246 unsigned div)
5248 unsigned total = isl_basic_map_total_dim(bmap);
5249 unsigned div_pos = total - bmap->n_div + div;
5251 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5252 bmap->div[div]);
5255 /* For each known div d = floor(f/m), add the constraints
5257 * f - m d >= 0
5258 * -(f-(m-1)) + m d >= 0
5260 * Remove duplicate constraints in case of some these div constraints
5261 * already appear in "bmap".
5263 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5264 __isl_take isl_basic_map *bmap)
5266 unsigned n_div;
5268 if (!bmap)
5269 return NULL;
5270 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5271 if (n_div == 0)
5272 return bmap;
5274 bmap = add_known_div_constraints(bmap);
5275 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5276 bmap = isl_basic_map_finalize(bmap);
5277 return bmap;
5280 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5282 * In particular, if this div is of the form d = floor(f/m),
5283 * then add the constraint
5285 * f - m d >= 0
5287 * if sign < 0 or the constraint
5289 * -(f-(m-1)) + m d >= 0
5291 * if sign > 0.
5293 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5294 unsigned div, int sign)
5296 unsigned total;
5297 unsigned div_pos;
5299 if (!bmap)
5300 return -1;
5302 total = isl_basic_map_total_dim(bmap);
5303 div_pos = total - bmap->n_div + div;
5305 if (sign < 0)
5306 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5307 else
5308 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5311 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5312 __isl_take isl_basic_map *bmap)
5314 if (!bmap)
5315 goto error;
5316 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5317 bmap->n_div == 0 &&
5318 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5319 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5320 return bset_from_bmap(bmap);
5321 bmap = isl_basic_map_cow(bmap);
5322 if (!bmap)
5323 goto error;
5324 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5325 if (!bmap->dim)
5326 goto error;
5327 bmap->extra -= bmap->n_div;
5328 bmap->n_div = 0;
5329 bmap = isl_basic_map_finalize(bmap);
5330 return bset_from_bmap(bmap);
5331 error:
5332 isl_basic_map_free(bmap);
5333 return NULL;
5336 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5337 __isl_take isl_basic_set *bset)
5339 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5342 /* Replace each element in "list" by the result of applying
5343 * isl_basic_map_underlying_set to the element.
5345 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5346 __isl_take isl_basic_map_list *list)
5348 int i, n;
5350 if (!list)
5351 return NULL;
5353 n = isl_basic_map_list_n_basic_map(list);
5354 for (i = 0; i < n; ++i) {
5355 isl_basic_map *bmap;
5356 isl_basic_set *bset;
5358 bmap = isl_basic_map_list_get_basic_map(list, i);
5359 bset = isl_basic_set_underlying_set(bmap);
5360 list = isl_basic_set_list_set_basic_set(list, i, bset);
5363 return list;
5366 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5367 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5369 struct isl_basic_map *bmap;
5370 struct isl_ctx *ctx;
5371 unsigned total;
5372 int i;
5374 if (!bset || !like)
5375 goto error;
5376 ctx = bset->ctx;
5377 if (isl_basic_set_check_no_params(bset) < 0 ||
5378 isl_basic_set_check_no_locals(bset) < 0)
5379 goto error;
5380 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5381 goto error);
5382 if (like->n_div == 0) {
5383 isl_space *space = isl_basic_map_get_space(like);
5384 isl_basic_map_free(like);
5385 return isl_basic_map_reset_space(bset, space);
5387 bset = isl_basic_set_cow(bset);
5388 if (!bset)
5389 goto error;
5390 total = bset->dim->n_out + bset->extra;
5391 bmap = bset_to_bmap(bset);
5392 isl_space_free(bmap->dim);
5393 bmap->dim = isl_space_copy(like->dim);
5394 if (!bmap->dim)
5395 goto error;
5396 bmap->n_div = like->n_div;
5397 bmap->extra += like->n_div;
5398 if (bmap->extra) {
5399 unsigned ltotal;
5400 isl_int **div;
5401 ltotal = total - bmap->extra + like->extra;
5402 if (ltotal > total)
5403 ltotal = total;
5404 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5405 bmap->extra * (1 + 1 + total));
5406 if (isl_blk_is_error(bmap->block2))
5407 goto error;
5408 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5409 if (!div)
5410 goto error;
5411 bmap->div = div;
5412 for (i = 0; i < bmap->extra; ++i)
5413 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5414 for (i = 0; i < like->n_div; ++i) {
5415 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5416 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5418 bmap = isl_basic_map_add_known_div_constraints(bmap);
5420 isl_basic_map_free(like);
5421 bmap = isl_basic_map_simplify(bmap);
5422 bmap = isl_basic_map_finalize(bmap);
5423 return bmap;
5424 error:
5425 isl_basic_map_free(like);
5426 isl_basic_set_free(bset);
5427 return NULL;
5430 struct isl_basic_set *isl_basic_set_from_underlying_set(
5431 struct isl_basic_set *bset, struct isl_basic_set *like)
5433 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5434 bset_to_bmap(like)));
5437 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5439 int i;
5441 map = isl_map_cow(map);
5442 if (!map)
5443 return NULL;
5444 map->dim = isl_space_cow(map->dim);
5445 if (!map->dim)
5446 goto error;
5448 for (i = 1; i < map->n; ++i)
5449 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5450 goto error);
5451 for (i = 0; i < map->n; ++i) {
5452 map->p[i] = bset_to_bmap(
5453 isl_basic_map_underlying_set(map->p[i]));
5454 if (!map->p[i])
5455 goto error;
5457 if (map->n == 0)
5458 map->dim = isl_space_underlying(map->dim, 0);
5459 else {
5460 isl_space_free(map->dim);
5461 map->dim = isl_space_copy(map->p[0]->dim);
5463 if (!map->dim)
5464 goto error;
5465 return set_from_map(map);
5466 error:
5467 isl_map_free(map);
5468 return NULL;
5471 /* Replace the space of "bmap" by "space".
5473 * If the space of "bmap" is identical to "space" (including the identifiers
5474 * of the input and output dimensions), then simply return the original input.
5476 __isl_give isl_basic_map *isl_basic_map_reset_space(
5477 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5479 isl_bool equal;
5480 isl_space *bmap_space;
5482 bmap_space = isl_basic_map_peek_space(bmap);
5483 equal = isl_space_is_equal(bmap_space, space);
5484 if (equal >= 0 && equal)
5485 equal = isl_space_has_equal_ids(bmap_space, space);
5486 if (equal < 0)
5487 goto error;
5488 if (equal) {
5489 isl_space_free(space);
5490 return bmap;
5492 bmap = isl_basic_map_cow(bmap);
5493 if (!bmap || !space)
5494 goto error;
5496 isl_space_free(bmap->dim);
5497 bmap->dim = space;
5499 bmap = isl_basic_map_finalize(bmap);
5501 return bmap;
5502 error:
5503 isl_basic_map_free(bmap);
5504 isl_space_free(space);
5505 return NULL;
5508 __isl_give isl_basic_set *isl_basic_set_reset_space(
5509 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5511 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5512 dim));
5515 /* Check that the total dimensions of "map" and "space" are the same.
5517 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5518 __isl_keep isl_space *space)
5520 unsigned dim1, dim2;
5522 if (!map || !space)
5523 return isl_stat_error;
5524 dim1 = isl_map_dim(map, isl_dim_all);
5525 dim2 = isl_space_dim(space, isl_dim_all);
5526 if (dim1 == dim2)
5527 return isl_stat_ok;
5528 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5529 "total dimensions do not match", return isl_stat_error);
5532 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5533 __isl_take isl_space *space)
5535 int i;
5537 map = isl_map_cow(map);
5538 if (!map || !space)
5539 goto error;
5541 for (i = 0; i < map->n; ++i) {
5542 map->p[i] = isl_basic_map_reset_space(map->p[i],
5543 isl_space_copy(space));
5544 if (!map->p[i])
5545 goto error;
5547 isl_space_free(map->dim);
5548 map->dim = space;
5550 return map;
5551 error:
5552 isl_map_free(map);
5553 isl_space_free(space);
5554 return NULL;
5557 /* Replace the space of "map" by "space", without modifying
5558 * the dimension of "map".
5560 * If the space of "map" is identical to "space" (including the identifiers
5561 * of the input and output dimensions), then simply return the original input.
5563 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5564 __isl_take isl_space *space)
5566 isl_bool equal;
5567 isl_space *map_space;
5569 map_space = isl_map_peek_space(map);
5570 equal = isl_space_is_equal(map_space, space);
5571 if (equal >= 0 && equal)
5572 equal = isl_space_has_equal_ids(map_space, space);
5573 if (equal < 0)
5574 goto error;
5575 if (equal) {
5576 isl_space_free(space);
5577 return map;
5579 if (check_map_space_equal_total_dim(map, space) < 0)
5580 goto error;
5581 return isl_map_reset_space(map, space);
5582 error:
5583 isl_map_free(map);
5584 isl_space_free(space);
5585 return NULL;
5588 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5589 __isl_take isl_space *dim)
5591 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5594 /* Compute the parameter domain of the given basic set.
5596 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5598 isl_bool is_params;
5599 isl_space *space;
5600 unsigned n;
5602 is_params = isl_basic_set_is_params(bset);
5603 if (is_params < 0)
5604 return isl_basic_set_free(bset);
5605 if (is_params)
5606 return bset;
5608 n = isl_basic_set_dim(bset, isl_dim_set);
5609 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5610 space = isl_basic_set_get_space(bset);
5611 space = isl_space_params(space);
5612 bset = isl_basic_set_reset_space(bset, space);
5613 return bset;
5616 /* Construct a zero-dimensional basic set with the given parameter domain.
5618 __isl_give isl_basic_set *isl_basic_set_from_params(
5619 __isl_take isl_basic_set *bset)
5621 isl_space *space;
5622 space = isl_basic_set_get_space(bset);
5623 space = isl_space_set_from_params(space);
5624 bset = isl_basic_set_reset_space(bset, space);
5625 return bset;
5628 /* Compute the parameter domain of the given set.
5630 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5632 isl_space *space;
5633 unsigned n;
5635 if (isl_set_is_params(set))
5636 return set;
5638 n = isl_set_dim(set, isl_dim_set);
5639 set = isl_set_project_out(set, isl_dim_set, 0, n);
5640 space = isl_set_get_space(set);
5641 space = isl_space_params(space);
5642 set = isl_set_reset_space(set, space);
5643 return set;
5646 /* Construct a zero-dimensional set with the given parameter domain.
5648 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5650 isl_space *space;
5651 space = isl_set_get_space(set);
5652 space = isl_space_set_from_params(space);
5653 set = isl_set_reset_space(set, space);
5654 return set;
5657 /* Compute the parameter domain of the given map.
5659 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5661 isl_space *space;
5662 unsigned n;
5664 n = isl_map_dim(map, isl_dim_in);
5665 map = isl_map_project_out(map, isl_dim_in, 0, n);
5666 n = isl_map_dim(map, isl_dim_out);
5667 map = isl_map_project_out(map, isl_dim_out, 0, n);
5668 space = isl_map_get_space(map);
5669 space = isl_space_params(space);
5670 map = isl_map_reset_space(map, space);
5671 return map;
5674 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5676 isl_space *space;
5677 unsigned n_out;
5679 if (!bmap)
5680 return NULL;
5681 space = isl_space_domain(isl_basic_map_get_space(bmap));
5683 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5684 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5686 return isl_basic_map_reset_space(bmap, space);
5689 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5691 if (!bmap)
5692 return isl_bool_error;
5693 return isl_space_may_be_set(bmap->dim);
5696 /* Is this basic map actually a set?
5697 * Users should never call this function. Outside of isl,
5698 * the type should indicate whether something is a set or a map.
5700 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5702 if (!bmap)
5703 return isl_bool_error;
5704 return isl_space_is_set(bmap->dim);
5707 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5709 isl_bool is_set;
5711 is_set = isl_basic_map_is_set(bmap);
5712 if (is_set < 0)
5713 goto error;
5714 if (is_set)
5715 return bmap;
5716 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5717 error:
5718 isl_basic_map_free(bmap);
5719 return NULL;
5722 __isl_give isl_basic_map *isl_basic_map_domain_map(
5723 __isl_take isl_basic_map *bmap)
5725 int i;
5726 isl_space *space;
5727 isl_basic_map *domain;
5728 int nparam, n_in, n_out;
5730 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5731 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5732 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5734 space = isl_basic_map_get_space(bmap);
5735 space = isl_space_from_range(isl_space_domain(space));
5736 domain = isl_basic_map_universe(space);
5738 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5739 bmap = isl_basic_map_apply_range(bmap, domain);
5740 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5742 for (i = 0; i < n_in; ++i)
5743 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5744 isl_dim_out, i);
5746 bmap = isl_basic_map_gauss(bmap, NULL);
5747 return isl_basic_map_finalize(bmap);
5750 __isl_give isl_basic_map *isl_basic_map_range_map(
5751 __isl_take isl_basic_map *bmap)
5753 int i;
5754 isl_space *space;
5755 isl_basic_map *range;
5756 int nparam, n_in, n_out;
5758 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5759 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5760 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5762 space = isl_basic_map_get_space(bmap);
5763 space = isl_space_from_range(isl_space_range(space));
5764 range = isl_basic_map_universe(space);
5766 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5767 bmap = isl_basic_map_apply_range(bmap, range);
5768 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5770 for (i = 0; i < n_out; ++i)
5771 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5772 isl_dim_out, i);
5774 bmap = isl_basic_map_gauss(bmap, NULL);
5775 return isl_basic_map_finalize(bmap);
5778 int isl_map_may_be_set(__isl_keep isl_map *map)
5780 if (!map)
5781 return -1;
5782 return isl_space_may_be_set(map->dim);
5785 /* Is this map actually a set?
5786 * Users should never call this function. Outside of isl,
5787 * the type should indicate whether something is a set or a map.
5789 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5791 if (!map)
5792 return isl_bool_error;
5793 return isl_space_is_set(map->dim);
5796 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5798 int i;
5799 isl_bool is_set;
5800 struct isl_set *set;
5802 is_set = isl_map_is_set(map);
5803 if (is_set < 0)
5804 goto error;
5805 if (is_set)
5806 return set_from_map(map);
5808 map = isl_map_cow(map);
5809 if (!map)
5810 goto error;
5812 set = set_from_map(map);
5813 set->dim = isl_space_range(set->dim);
5814 if (!set->dim)
5815 goto error;
5816 for (i = 0; i < map->n; ++i) {
5817 set->p[i] = isl_basic_map_range(map->p[i]);
5818 if (!set->p[i])
5819 goto error;
5821 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5822 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5823 return set;
5824 error:
5825 isl_map_free(map);
5826 return NULL;
5829 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5831 int i;
5833 map = isl_map_cow(map);
5834 if (!map)
5835 return NULL;
5837 map->dim = isl_space_domain_map(map->dim);
5838 if (!map->dim)
5839 goto error;
5840 for (i = 0; i < map->n; ++i) {
5841 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5842 if (!map->p[i])
5843 goto error;
5845 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5846 map = isl_map_unmark_normalized(map);
5847 return map;
5848 error:
5849 isl_map_free(map);
5850 return NULL;
5853 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5855 int i;
5856 isl_space *range_dim;
5858 map = isl_map_cow(map);
5859 if (!map)
5860 return NULL;
5862 range_dim = isl_space_range(isl_map_get_space(map));
5863 range_dim = isl_space_from_range(range_dim);
5864 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5865 map->dim = isl_space_join(map->dim, range_dim);
5866 if (!map->dim)
5867 goto error;
5868 for (i = 0; i < map->n; ++i) {
5869 map->p[i] = isl_basic_map_range_map(map->p[i]);
5870 if (!map->p[i])
5871 goto error;
5873 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5874 map = isl_map_unmark_normalized(map);
5875 return map;
5876 error:
5877 isl_map_free(map);
5878 return NULL;
5881 /* Given a wrapped map of the form A[B -> C],
5882 * return the map A[B -> C] -> B.
5884 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5886 isl_id *id;
5887 isl_map *map;
5889 if (!set)
5890 return NULL;
5891 if (!isl_set_has_tuple_id(set))
5892 return isl_map_domain_map(isl_set_unwrap(set));
5894 id = isl_set_get_tuple_id(set);
5895 map = isl_map_domain_map(isl_set_unwrap(set));
5896 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5898 return map;
5901 __isl_give isl_basic_map *isl_basic_map_from_domain(
5902 __isl_take isl_basic_set *bset)
5904 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5907 __isl_give isl_basic_map *isl_basic_map_from_range(
5908 __isl_take isl_basic_set *bset)
5910 isl_space *space;
5911 space = isl_basic_set_get_space(bset);
5912 space = isl_space_from_range(space);
5913 bset = isl_basic_set_reset_space(bset, space);
5914 return bset_to_bmap(bset);
5917 /* Create a relation with the given set as range.
5918 * The domain of the created relation is a zero-dimensional
5919 * flat anonymous space.
5921 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5923 isl_space *space;
5924 space = isl_set_get_space(set);
5925 space = isl_space_from_range(space);
5926 set = isl_set_reset_space(set, space);
5927 return set_to_map(set);
5930 /* Create a relation with the given set as domain.
5931 * The range of the created relation is a zero-dimensional
5932 * flat anonymous space.
5934 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5936 return isl_map_reverse(isl_map_from_range(set));
5939 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5940 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5942 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5945 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5946 __isl_take isl_set *range)
5948 return isl_map_apply_range(isl_map_reverse(domain), range);
5951 /* Return a newly allocated isl_map with given space and flags and
5952 * room for "n" basic maps.
5953 * Make sure that all cached information is cleared.
5955 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5956 unsigned flags)
5958 struct isl_map *map;
5960 if (!space)
5961 return NULL;
5962 if (n < 0)
5963 isl_die(space->ctx, isl_error_internal,
5964 "negative number of basic maps", goto error);
5965 map = isl_calloc(space->ctx, struct isl_map,
5966 sizeof(struct isl_map) +
5967 (n - 1) * sizeof(struct isl_basic_map *));
5968 if (!map)
5969 goto error;
5971 map->ctx = space->ctx;
5972 isl_ctx_ref(map->ctx);
5973 map->ref = 1;
5974 map->size = n;
5975 map->n = 0;
5976 map->dim = space;
5977 map->flags = flags;
5978 return map;
5979 error:
5980 isl_space_free(space);
5981 return NULL;
5984 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5986 struct isl_basic_map *bmap;
5987 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5988 bmap = isl_basic_map_set_to_empty(bmap);
5989 return bmap;
5992 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5994 struct isl_basic_set *bset;
5995 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5996 bset = isl_basic_set_set_to_empty(bset);
5997 return bset;
6000 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6002 struct isl_basic_map *bmap;
6003 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6004 bmap = isl_basic_map_finalize(bmap);
6005 return bmap;
6008 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6010 struct isl_basic_set *bset;
6011 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6012 bset = isl_basic_set_finalize(bset);
6013 return bset;
6016 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6017 __isl_take isl_space *space)
6019 int i;
6020 unsigned total = isl_space_dim(space, isl_dim_all);
6021 isl_basic_map *bmap;
6023 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6024 for (i = 0; i < total; ++i) {
6025 int k = isl_basic_map_alloc_inequality(bmap);
6026 if (k < 0)
6027 goto error;
6028 isl_seq_clr(bmap->ineq[k], 1 + total);
6029 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6031 return bmap;
6032 error:
6033 isl_basic_map_free(bmap);
6034 return NULL;
6037 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6038 __isl_take isl_space *space)
6040 return isl_basic_map_nat_universe(space);
6043 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6045 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6048 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6050 return isl_map_nat_universe(dim);
6053 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6055 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6058 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6060 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6063 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6065 struct isl_map *map;
6066 if (!space)
6067 return NULL;
6068 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6069 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6070 return map;
6073 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6075 struct isl_set *set;
6076 if (!space)
6077 return NULL;
6078 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6079 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6080 return set;
6083 struct isl_map *isl_map_dup(struct isl_map *map)
6085 int i;
6086 struct isl_map *dup;
6088 if (!map)
6089 return NULL;
6090 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6091 for (i = 0; i < map->n; ++i)
6092 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6093 return dup;
6096 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6097 __isl_take isl_basic_map *bmap)
6099 if (!bmap || !map)
6100 goto error;
6101 if (isl_basic_map_plain_is_empty(bmap)) {
6102 isl_basic_map_free(bmap);
6103 return map;
6105 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6106 isl_assert(map->ctx, map->n < map->size, goto error);
6107 map->p[map->n] = bmap;
6108 map->n++;
6109 map = isl_map_unmark_normalized(map);
6110 return map;
6111 error:
6112 if (map)
6113 isl_map_free(map);
6114 if (bmap)
6115 isl_basic_map_free(bmap);
6116 return NULL;
6119 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6121 int i;
6123 if (!map)
6124 return NULL;
6126 if (--map->ref > 0)
6127 return NULL;
6129 clear_caches(map);
6130 isl_ctx_deref(map->ctx);
6131 for (i = 0; i < map->n; ++i)
6132 isl_basic_map_free(map->p[i]);
6133 isl_space_free(map->dim);
6134 free(map);
6136 return NULL;
6139 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6140 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6142 int j;
6144 bmap = isl_basic_map_cow(bmap);
6145 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6146 j = isl_basic_map_alloc_equality(bmap);
6147 if (j < 0)
6148 goto error;
6149 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6150 isl_int_set_si(bmap->eq[j][pos], -1);
6151 isl_int_set_si(bmap->eq[j][0], value);
6152 bmap = isl_basic_map_simplify(bmap);
6153 return isl_basic_map_finalize(bmap);
6154 error:
6155 isl_basic_map_free(bmap);
6156 return NULL;
6159 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6160 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6162 int j;
6164 bmap = isl_basic_map_cow(bmap);
6165 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6166 j = isl_basic_map_alloc_equality(bmap);
6167 if (j < 0)
6168 goto error;
6169 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6170 isl_int_set_si(bmap->eq[j][pos], -1);
6171 isl_int_set(bmap->eq[j][0], value);
6172 bmap = isl_basic_map_simplify(bmap);
6173 return isl_basic_map_finalize(bmap);
6174 error:
6175 isl_basic_map_free(bmap);
6176 return NULL;
6179 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6180 enum isl_dim_type type, unsigned pos, int value)
6182 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6183 return isl_basic_map_free(bmap);
6184 return isl_basic_map_fix_pos_si(bmap,
6185 isl_basic_map_offset(bmap, type) + pos, value);
6188 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6189 enum isl_dim_type type, unsigned pos, isl_int value)
6191 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6192 return isl_basic_map_free(bmap);
6193 return isl_basic_map_fix_pos(bmap,
6194 isl_basic_map_offset(bmap, type) + pos, value);
6197 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6198 * to be equal to "v".
6200 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6201 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6203 if (!bmap || !v)
6204 goto error;
6205 if (!isl_val_is_int(v))
6206 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6207 "expecting integer value", goto error);
6208 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6209 goto error;
6210 pos += isl_basic_map_offset(bmap, type);
6211 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6212 isl_val_free(v);
6213 return bmap;
6214 error:
6215 isl_basic_map_free(bmap);
6216 isl_val_free(v);
6217 return NULL;
6220 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6221 * to be equal to "v".
6223 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6224 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6226 return isl_basic_map_fix_val(bset, type, pos, v);
6229 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6230 enum isl_dim_type type, unsigned pos, int value)
6232 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6233 type, pos, value));
6236 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6237 enum isl_dim_type type, unsigned pos, isl_int value)
6239 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6240 type, pos, value));
6243 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6244 unsigned input, int value)
6246 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6249 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6250 unsigned dim, int value)
6252 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6253 isl_dim_set, dim, value));
6256 /* Remove the basic map at position "i" from "map" if this basic map
6257 * is (obviously) empty.
6259 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6261 isl_bool empty;
6263 if (!map)
6264 return NULL;
6266 empty = isl_basic_map_plain_is_empty(map->p[i]);
6267 if (empty < 0)
6268 return isl_map_free(map);
6269 if (!empty)
6270 return map;
6272 isl_basic_map_free(map->p[i]);
6273 map->n--;
6274 if (i != map->n) {
6275 map->p[i] = map->p[map->n];
6276 map = isl_map_unmark_normalized(map);
6280 return map;
6283 /* Perform "fn" on each basic map of "map", where we may not be holding
6284 * the only reference to "map".
6285 * In particular, "fn" should be a semantics preserving operation
6286 * that we want to apply to all copies of "map". We therefore need
6287 * to be careful not to modify "map" in a way that breaks "map"
6288 * in case anything goes wrong.
6290 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6291 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6293 struct isl_basic_map *bmap;
6294 int i;
6296 if (!map)
6297 return NULL;
6299 for (i = map->n - 1; i >= 0; --i) {
6300 bmap = isl_basic_map_copy(map->p[i]);
6301 bmap = fn(bmap);
6302 if (!bmap)
6303 goto error;
6304 isl_basic_map_free(map->p[i]);
6305 map->p[i] = bmap;
6306 map = remove_if_empty(map, i);
6307 if (!map)
6308 return NULL;
6311 return map;
6312 error:
6313 isl_map_free(map);
6314 return NULL;
6317 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6318 enum isl_dim_type type, unsigned pos, int value)
6320 int i;
6322 map = isl_map_cow(map);
6323 if (isl_map_check_range(map, type, pos, 1) < 0)
6324 return isl_map_free(map);
6325 for (i = map->n - 1; i >= 0; --i) {
6326 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6327 map = remove_if_empty(map, i);
6328 if (!map)
6329 return NULL;
6331 map = isl_map_unmark_normalized(map);
6332 return map;
6335 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6336 enum isl_dim_type type, unsigned pos, int value)
6338 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6341 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6342 enum isl_dim_type type, unsigned pos, isl_int value)
6344 int i;
6346 map = isl_map_cow(map);
6347 if (isl_map_check_range(map, type, pos, 1) < 0)
6348 return isl_map_free(map);
6349 for (i = 0; i < map->n; ++i) {
6350 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6351 if (!map->p[i])
6352 goto error;
6354 map = isl_map_unmark_normalized(map);
6355 return map;
6356 error:
6357 isl_map_free(map);
6358 return NULL;
6361 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6362 enum isl_dim_type type, unsigned pos, isl_int value)
6364 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6367 /* Fix the value of the variable at position "pos" of type "type" of "map"
6368 * to be equal to "v".
6370 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6371 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6373 int i;
6375 map = isl_map_cow(map);
6376 if (!map || !v)
6377 goto error;
6379 if (!isl_val_is_int(v))
6380 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6381 "expecting integer value", goto error);
6382 if (isl_map_check_range(map, type, pos, 1) < 0)
6383 goto error;
6384 for (i = map->n - 1; i >= 0; --i) {
6385 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6386 isl_val_copy(v));
6387 map = remove_if_empty(map, i);
6388 if (!map)
6389 goto error;
6391 map = isl_map_unmark_normalized(map);
6392 isl_val_free(v);
6393 return map;
6394 error:
6395 isl_map_free(map);
6396 isl_val_free(v);
6397 return NULL;
6400 /* Fix the value of the variable at position "pos" of type "type" of "set"
6401 * to be equal to "v".
6403 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6404 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6406 return isl_map_fix_val(set, type, pos, v);
6409 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6410 unsigned input, int value)
6412 return isl_map_fix_si(map, isl_dim_in, input, value);
6415 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6417 return set_from_map(isl_map_fix_si(set_to_map(set),
6418 isl_dim_set, dim, value));
6421 static __isl_give isl_basic_map *basic_map_bound_si(
6422 __isl_take isl_basic_map *bmap,
6423 enum isl_dim_type type, unsigned pos, int value, int upper)
6425 int j;
6427 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6428 return isl_basic_map_free(bmap);
6429 pos += isl_basic_map_offset(bmap, type);
6430 bmap = isl_basic_map_cow(bmap);
6431 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6432 j = isl_basic_map_alloc_inequality(bmap);
6433 if (j < 0)
6434 goto error;
6435 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6436 if (upper) {
6437 isl_int_set_si(bmap->ineq[j][pos], -1);
6438 isl_int_set_si(bmap->ineq[j][0], value);
6439 } else {
6440 isl_int_set_si(bmap->ineq[j][pos], 1);
6441 isl_int_set_si(bmap->ineq[j][0], -value);
6443 bmap = isl_basic_map_simplify(bmap);
6444 return isl_basic_map_finalize(bmap);
6445 error:
6446 isl_basic_map_free(bmap);
6447 return NULL;
6450 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6451 __isl_take isl_basic_map *bmap,
6452 enum isl_dim_type type, unsigned pos, int value)
6454 return basic_map_bound_si(bmap, type, pos, value, 0);
6457 /* Constrain the values of the given dimension to be no greater than "value".
6459 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6460 __isl_take isl_basic_map *bmap,
6461 enum isl_dim_type type, unsigned pos, int value)
6463 return basic_map_bound_si(bmap, type, pos, value, 1);
6466 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6467 enum isl_dim_type type, unsigned pos, int value, int upper)
6469 int i;
6471 map = isl_map_cow(map);
6472 if (isl_map_check_range(map, type, pos, 1) < 0)
6473 return isl_map_free(map);
6474 for (i = 0; i < map->n; ++i) {
6475 map->p[i] = basic_map_bound_si(map->p[i],
6476 type, pos, value, upper);
6477 if (!map->p[i])
6478 goto error;
6480 map = isl_map_unmark_normalized(map);
6481 return map;
6482 error:
6483 isl_map_free(map);
6484 return NULL;
6487 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6488 enum isl_dim_type type, unsigned pos, int value)
6490 return map_bound_si(map, type, pos, value, 0);
6493 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6494 enum isl_dim_type type, unsigned pos, int value)
6496 return map_bound_si(map, type, pos, value, 1);
6499 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6500 enum isl_dim_type type, unsigned pos, int value)
6502 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6503 type, pos, value));
6506 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6507 enum isl_dim_type type, unsigned pos, int value)
6509 return isl_map_upper_bound_si(set, type, pos, value);
6512 /* Bound the given variable of "bmap" from below (or above is "upper"
6513 * is set) to "value".
6515 static __isl_give isl_basic_map *basic_map_bound(
6516 __isl_take isl_basic_map *bmap,
6517 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6519 int j;
6521 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6522 return isl_basic_map_free(bmap);
6523 pos += isl_basic_map_offset(bmap, type);
6524 bmap = isl_basic_map_cow(bmap);
6525 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6526 j = isl_basic_map_alloc_inequality(bmap);
6527 if (j < 0)
6528 goto error;
6529 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6530 if (upper) {
6531 isl_int_set_si(bmap->ineq[j][pos], -1);
6532 isl_int_set(bmap->ineq[j][0], value);
6533 } else {
6534 isl_int_set_si(bmap->ineq[j][pos], 1);
6535 isl_int_neg(bmap->ineq[j][0], value);
6537 bmap = isl_basic_map_simplify(bmap);
6538 return isl_basic_map_finalize(bmap);
6539 error:
6540 isl_basic_map_free(bmap);
6541 return NULL;
6544 /* Bound the given variable of "map" from below (or above is "upper"
6545 * is set) to "value".
6547 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6548 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6550 int i;
6552 map = isl_map_cow(map);
6553 if (isl_map_check_range(map, type, pos, 1) < 0)
6554 return isl_map_free(map);
6555 for (i = map->n - 1; i >= 0; --i) {
6556 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6557 map = remove_if_empty(map, i);
6558 if (!map)
6559 return NULL;
6561 map = isl_map_unmark_normalized(map);
6562 return map;
6565 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6566 enum isl_dim_type type, unsigned pos, isl_int value)
6568 return map_bound(map, type, pos, value, 0);
6571 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6572 enum isl_dim_type type, unsigned pos, isl_int value)
6574 return map_bound(map, type, pos, value, 1);
6577 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6578 enum isl_dim_type type, unsigned pos, isl_int value)
6580 return isl_map_lower_bound(set, type, pos, value);
6583 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6584 enum isl_dim_type type, unsigned pos, isl_int value)
6586 return isl_map_upper_bound(set, type, pos, value);
6589 /* Force the values of the variable at position "pos" of type "type" of "set"
6590 * to be no smaller than "value".
6592 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6593 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6595 if (!value)
6596 goto error;
6597 if (!isl_val_is_int(value))
6598 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6599 "expecting integer value", goto error);
6600 set = isl_set_lower_bound(set, type, pos, value->n);
6601 isl_val_free(value);
6602 return set;
6603 error:
6604 isl_val_free(value);
6605 isl_set_free(set);
6606 return NULL;
6609 /* Force the values of the variable at position "pos" of type "type" of "set"
6610 * to be no greater than "value".
6612 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6613 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6615 if (!value)
6616 goto error;
6617 if (!isl_val_is_int(value))
6618 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6619 "expecting integer value", goto error);
6620 set = isl_set_upper_bound(set, type, pos, value->n);
6621 isl_val_free(value);
6622 return set;
6623 error:
6624 isl_val_free(value);
6625 isl_set_free(set);
6626 return NULL;
6629 /* Bound the given variable of "bset" from below (or above is "upper"
6630 * is set) to "value".
6632 static __isl_give isl_basic_set *isl_basic_set_bound(
6633 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6634 isl_int value, int upper)
6636 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6637 type, pos, value, upper));
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_val(
6644 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6645 __isl_take isl_val *value, int upper)
6647 if (!value)
6648 goto error;
6649 if (!isl_val_is_int(value))
6650 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6651 "expecting integer value", goto error);
6652 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6653 isl_val_free(value);
6654 return bset;
6655 error:
6656 isl_val_free(value);
6657 isl_basic_set_free(bset);
6658 return NULL;
6661 /* Bound the given variable of "bset" from below to "value".
6663 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6664 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6665 __isl_take isl_val *value)
6667 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6670 /* Bound the given variable of "bset" from above to "value".
6672 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6673 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6674 __isl_take isl_val *value)
6676 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6679 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6681 int i;
6683 map = isl_map_cow(map);
6684 if (!map)
6685 return NULL;
6687 map->dim = isl_space_reverse(map->dim);
6688 if (!map->dim)
6689 goto error;
6690 for (i = 0; i < map->n; ++i) {
6691 map->p[i] = isl_basic_map_reverse(map->p[i]);
6692 if (!map->p[i])
6693 goto error;
6695 map = isl_map_unmark_normalized(map);
6696 return map;
6697 error:
6698 isl_map_free(map);
6699 return NULL;
6702 #undef TYPE
6703 #define TYPE isl_pw_multi_aff
6704 #undef SUFFIX
6705 #define SUFFIX _pw_multi_aff
6706 #undef EMPTY
6707 #define EMPTY isl_pw_multi_aff_empty
6708 #undef ADD
6709 #define ADD isl_pw_multi_aff_union_add
6710 #include "isl_map_lexopt_templ.c"
6712 /* Given a map "map", compute the lexicographically minimal
6713 * (or maximal) image element for each domain element in dom,
6714 * in the form of an isl_pw_multi_aff.
6715 * If "empty" is not NULL, then set *empty to those elements in dom that
6716 * do not have an image element.
6717 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6718 * should be computed over the domain of "map". "empty" is also NULL
6719 * in this case.
6721 * We first compute the lexicographically minimal or maximal element
6722 * in the first basic map. This results in a partial solution "res"
6723 * and a subset "todo" of dom that still need to be handled.
6724 * We then consider each of the remaining maps in "map" and successively
6725 * update both "res" and "todo".
6726 * If "empty" is NULL, then the todo sets are not needed and therefore
6727 * also not computed.
6729 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6730 __isl_take isl_map *map, __isl_take isl_set *dom,
6731 __isl_give isl_set **empty, unsigned flags)
6733 int i;
6734 int full;
6735 isl_pw_multi_aff *res;
6736 isl_set *todo;
6738 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6739 if (!map || (!full && !dom))
6740 goto error;
6742 if (isl_map_plain_is_empty(map)) {
6743 if (empty)
6744 *empty = dom;
6745 else
6746 isl_set_free(dom);
6747 return isl_pw_multi_aff_from_map(map);
6750 res = basic_map_partial_lexopt_pw_multi_aff(
6751 isl_basic_map_copy(map->p[0]),
6752 isl_set_copy(dom), empty, flags);
6754 if (empty)
6755 todo = *empty;
6756 for (i = 1; i < map->n; ++i) {
6757 isl_pw_multi_aff *res_i;
6759 res_i = basic_map_partial_lexopt_pw_multi_aff(
6760 isl_basic_map_copy(map->p[i]),
6761 isl_set_copy(dom), empty, flags);
6763 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6764 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6765 else
6766 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6768 if (empty)
6769 todo = isl_set_intersect(todo, *empty);
6772 isl_set_free(dom);
6773 isl_map_free(map);
6775 if (empty)
6776 *empty = todo;
6778 return res;
6779 error:
6780 if (empty)
6781 *empty = NULL;
6782 isl_set_free(dom);
6783 isl_map_free(map);
6784 return NULL;
6787 #undef TYPE
6788 #define TYPE isl_map
6789 #undef SUFFIX
6790 #define SUFFIX
6791 #undef EMPTY
6792 #define EMPTY isl_map_empty
6793 #undef ADD
6794 #define ADD isl_map_union_disjoint
6795 #include "isl_map_lexopt_templ.c"
6797 /* Given a map "map", compute the lexicographically minimal
6798 * (or maximal) image element for each domain element in "dom",
6799 * in the form of an isl_map.
6800 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6801 * do not have an image element.
6802 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6803 * should be computed over the domain of "map". "empty" is also NULL
6804 * in this case.
6806 * If the input consists of more than one disjunct, then first
6807 * compute the desired result in the form of an isl_pw_multi_aff and
6808 * then convert that into an isl_map.
6810 * This function used to have an explicit implementation in terms
6811 * of isl_maps, but it would continually intersect the domains of
6812 * partial results with the complement of the domain of the next
6813 * partial solution, potentially leading to an explosion in the number
6814 * of disjuncts if there are several disjuncts in the input.
6815 * An even earlier implementation of this function would look for
6816 * better results in the domain of the partial result and for extra
6817 * results in the complement of this domain, which would lead to
6818 * even more splintering.
6820 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6821 __isl_take isl_map *map, __isl_take isl_set *dom,
6822 __isl_give isl_set **empty, unsigned flags)
6824 int full;
6825 struct isl_map *res;
6826 isl_pw_multi_aff *pma;
6828 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6829 if (!map || (!full && !dom))
6830 goto error;
6832 if (isl_map_plain_is_empty(map)) {
6833 if (empty)
6834 *empty = dom;
6835 else
6836 isl_set_free(dom);
6837 return map;
6840 if (map->n == 1) {
6841 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6842 dom, empty, flags);
6843 isl_map_free(map);
6844 return res;
6847 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6848 flags);
6849 return isl_map_from_pw_multi_aff(pma);
6850 error:
6851 if (empty)
6852 *empty = NULL;
6853 isl_set_free(dom);
6854 isl_map_free(map);
6855 return NULL;
6858 __isl_give isl_map *isl_map_partial_lexmax(
6859 __isl_take isl_map *map, __isl_take isl_set *dom,
6860 __isl_give isl_set **empty)
6862 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6865 __isl_give isl_map *isl_map_partial_lexmin(
6866 __isl_take isl_map *map, __isl_take isl_set *dom,
6867 __isl_give isl_set **empty)
6869 return isl_map_partial_lexopt(map, dom, empty, 0);
6872 __isl_give isl_set *isl_set_partial_lexmin(
6873 __isl_take isl_set *set, __isl_take isl_set *dom,
6874 __isl_give isl_set **empty)
6876 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6877 dom, empty));
6880 __isl_give isl_set *isl_set_partial_lexmax(
6881 __isl_take isl_set *set, __isl_take isl_set *dom,
6882 __isl_give isl_set **empty)
6884 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6885 dom, empty));
6888 /* Compute the lexicographic minimum (or maximum if "flags" includes
6889 * ISL_OPT_MAX) of "bset" over its parametric domain.
6891 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6892 unsigned flags)
6894 return isl_basic_map_lexopt(bset, flags);
6897 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6899 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6902 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6904 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6907 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6909 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6912 /* Compute the lexicographic minimum of "bset" over its parametric domain
6913 * for the purpose of quantifier elimination.
6914 * That is, find an explicit representation for all the existentially
6915 * quantified variables in "bset" by computing their lexicographic
6916 * minimum.
6918 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6919 __isl_take isl_basic_set *bset)
6921 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6924 /* Given a basic map with one output dimension, compute the minimum or
6925 * maximum of that dimension as an isl_pw_aff.
6927 * Compute the optimum as a lexicographic optimum over the single
6928 * output dimension and extract the single isl_pw_aff from the result.
6930 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6931 int max)
6933 isl_pw_multi_aff *pma;
6934 isl_pw_aff *pwaff;
6936 bmap = isl_basic_map_copy(bmap);
6937 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6938 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6939 isl_pw_multi_aff_free(pma);
6941 return pwaff;
6944 /* Compute the minimum or maximum of the given output dimension
6945 * as a function of the parameters and the input dimensions,
6946 * but independently of the other output dimensions.
6948 * We first project out the other output dimension and then compute
6949 * the "lexicographic" maximum in each basic map, combining the results
6950 * using isl_pw_aff_union_max.
6952 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6953 int max)
6955 int i;
6956 isl_pw_aff *pwaff;
6957 unsigned n_out;
6959 n_out = isl_map_dim(map, isl_dim_out);
6960 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6961 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6962 if (!map)
6963 return NULL;
6965 if (map->n == 0) {
6966 isl_space *space = isl_map_get_space(map);
6967 isl_map_free(map);
6968 return isl_pw_aff_empty(space);
6971 pwaff = basic_map_dim_opt(map->p[0], max);
6972 for (i = 1; i < map->n; ++i) {
6973 isl_pw_aff *pwaff_i;
6975 pwaff_i = basic_map_dim_opt(map->p[i], max);
6976 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6979 isl_map_free(map);
6981 return pwaff;
6984 /* Compute the minimum of the given output dimension as a function of the
6985 * parameters and input dimensions, but independently of
6986 * the other output dimensions.
6988 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6990 return map_dim_opt(map, pos, 0);
6993 /* Compute the maximum of the given output dimension as a function of the
6994 * parameters and input dimensions, but independently of
6995 * the other output dimensions.
6997 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6999 return map_dim_opt(map, pos, 1);
7002 /* Compute the minimum or maximum of the given set dimension
7003 * as a function of the parameters,
7004 * but independently of the other set dimensions.
7006 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7007 int max)
7009 return map_dim_opt(set, pos, max);
7012 /* Compute the maximum of the given set dimension as a function of the
7013 * parameters, but independently of the other set dimensions.
7015 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7017 return set_dim_opt(set, pos, 1);
7020 /* Compute the minimum of the given set dimension as a function of the
7021 * parameters, but independently of the other set dimensions.
7023 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7025 return set_dim_opt(set, pos, 0);
7028 /* Apply a preimage specified by "mat" on the parameters of "bset".
7029 * bset is assumed to have only parameters and divs.
7031 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7032 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7034 unsigned nparam;
7036 if (!bset || !mat)
7037 goto error;
7039 bset->dim = isl_space_cow(bset->dim);
7040 if (!bset->dim)
7041 goto error;
7043 nparam = isl_basic_set_dim(bset, isl_dim_param);
7045 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7047 bset->dim->nparam = 0;
7048 bset->dim->n_out = nparam;
7049 bset = isl_basic_set_preimage(bset, mat);
7050 if (bset) {
7051 bset->dim->nparam = bset->dim->n_out;
7052 bset->dim->n_out = 0;
7054 return bset;
7055 error:
7056 isl_mat_free(mat);
7057 isl_basic_set_free(bset);
7058 return NULL;
7061 /* Apply a preimage specified by "mat" on the parameters of "set".
7062 * set is assumed to have only parameters and divs.
7064 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7065 __isl_take isl_mat *mat)
7067 isl_space *space;
7068 unsigned nparam;
7070 if (!set || !mat)
7071 goto error;
7073 nparam = isl_set_dim(set, isl_dim_param);
7075 if (mat->n_row != 1 + nparam)
7076 isl_die(isl_set_get_ctx(set), isl_error_internal,
7077 "unexpected number of rows", goto error);
7079 space = isl_set_get_space(set);
7080 space = isl_space_move_dims(space, isl_dim_set, 0,
7081 isl_dim_param, 0, nparam);
7082 set = isl_set_reset_space(set, space);
7083 set = isl_set_preimage(set, mat);
7084 nparam = isl_set_dim(set, isl_dim_out);
7085 space = isl_set_get_space(set);
7086 space = isl_space_move_dims(space, isl_dim_param, 0,
7087 isl_dim_out, 0, nparam);
7088 set = isl_set_reset_space(set, space);
7089 return set;
7090 error:
7091 isl_mat_free(mat);
7092 isl_set_free(set);
7093 return NULL;
7096 /* Intersect the basic set "bset" with the affine space specified by the
7097 * equalities in "eq".
7099 static __isl_give isl_basic_set *basic_set_append_equalities(
7100 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7102 int i, k;
7103 unsigned len;
7105 if (!bset || !eq)
7106 goto error;
7108 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7109 eq->n_row, 0);
7110 if (!bset)
7111 goto error;
7113 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7114 for (i = 0; i < eq->n_row; ++i) {
7115 k = isl_basic_set_alloc_equality(bset);
7116 if (k < 0)
7117 goto error;
7118 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7119 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7121 isl_mat_free(eq);
7123 bset = isl_basic_set_gauss(bset, NULL);
7124 bset = isl_basic_set_finalize(bset);
7126 return bset;
7127 error:
7128 isl_mat_free(eq);
7129 isl_basic_set_free(bset);
7130 return NULL;
7133 /* Intersect the set "set" with the affine space specified by the
7134 * equalities in "eq".
7136 static struct isl_set *set_append_equalities(struct isl_set *set,
7137 struct isl_mat *eq)
7139 int i;
7141 if (!set || !eq)
7142 goto error;
7144 for (i = 0; i < set->n; ++i) {
7145 set->p[i] = basic_set_append_equalities(set->p[i],
7146 isl_mat_copy(eq));
7147 if (!set->p[i])
7148 goto error;
7150 isl_mat_free(eq);
7151 return set;
7152 error:
7153 isl_mat_free(eq);
7154 isl_set_free(set);
7155 return NULL;
7158 /* Given a basic set "bset" that only involves parameters and existentially
7159 * quantified variables, return the index of the first equality
7160 * that only involves parameters. If there is no such equality then
7161 * return bset->n_eq.
7163 * This function assumes that isl_basic_set_gauss has been called on "bset".
7165 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7167 int i, j;
7168 unsigned nparam, n_div;
7170 if (!bset)
7171 return -1;
7173 nparam = isl_basic_set_dim(bset, isl_dim_param);
7174 n_div = isl_basic_set_dim(bset, isl_dim_div);
7176 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7177 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7178 ++i;
7181 return i;
7184 /* Compute an explicit representation for the existentially quantified
7185 * variables in "bset" by computing the "minimal value" of the set
7186 * variables. Since there are no set variables, the computation of
7187 * the minimal value essentially computes an explicit representation
7188 * of the non-empty part(s) of "bset".
7190 * The input only involves parameters and existentially quantified variables.
7191 * All equalities among parameters have been removed.
7193 * Since the existentially quantified variables in the result are in general
7194 * going to be different from those in the input, we first replace
7195 * them by the minimal number of variables based on their equalities.
7196 * This should simplify the parametric integer programming.
7198 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7200 isl_morph *morph1, *morph2;
7201 isl_set *set;
7202 unsigned n;
7204 if (!bset)
7205 return NULL;
7206 if (bset->n_eq == 0)
7207 return isl_basic_set_lexmin_compute_divs(bset);
7209 morph1 = isl_basic_set_parameter_compression(bset);
7210 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7211 bset = isl_basic_set_lift(bset);
7212 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7213 bset = isl_morph_basic_set(morph2, bset);
7214 n = isl_basic_set_dim(bset, isl_dim_set);
7215 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7217 set = isl_basic_set_lexmin_compute_divs(bset);
7219 set = isl_morph_set(isl_morph_inverse(morph1), set);
7221 return set;
7224 /* Project the given basic set onto its parameter domain, possibly introducing
7225 * new, explicit, existential variables in the constraints.
7226 * The input has parameters and (possibly implicit) existential variables.
7227 * The output has the same parameters, but only
7228 * explicit existentially quantified variables.
7230 * The actual projection is performed by pip, but pip doesn't seem
7231 * to like equalities very much, so we first remove the equalities
7232 * among the parameters by performing a variable compression on
7233 * the parameters. Afterward, an inverse transformation is performed
7234 * and the equalities among the parameters are inserted back in.
7236 * The variable compression on the parameters may uncover additional
7237 * equalities that were only implicit before. We therefore check
7238 * if there are any new parameter equalities in the result and
7239 * if so recurse. The removal of parameter equalities is required
7240 * for the parameter compression performed by base_compute_divs.
7242 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7244 int i;
7245 struct isl_mat *eq;
7246 struct isl_mat *T, *T2;
7247 struct isl_set *set;
7248 unsigned nparam;
7250 bset = isl_basic_set_cow(bset);
7251 if (!bset)
7252 return NULL;
7254 if (bset->n_eq == 0)
7255 return base_compute_divs(bset);
7257 bset = isl_basic_set_gauss(bset, NULL);
7258 if (!bset)
7259 return NULL;
7260 if (isl_basic_set_plain_is_empty(bset))
7261 return isl_set_from_basic_set(bset);
7263 i = first_parameter_equality(bset);
7264 if (i == bset->n_eq)
7265 return base_compute_divs(bset);
7267 nparam = isl_basic_set_dim(bset, isl_dim_param);
7268 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7269 0, 1 + nparam);
7270 eq = isl_mat_cow(eq);
7271 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7272 if (T && T->n_col == 0) {
7273 isl_mat_free(T);
7274 isl_mat_free(T2);
7275 isl_mat_free(eq);
7276 bset = isl_basic_set_set_to_empty(bset);
7277 return isl_set_from_basic_set(bset);
7279 bset = basic_set_parameter_preimage(bset, T);
7281 i = first_parameter_equality(bset);
7282 if (!bset)
7283 set = NULL;
7284 else if (i == bset->n_eq)
7285 set = base_compute_divs(bset);
7286 else
7287 set = parameter_compute_divs(bset);
7288 set = set_parameter_preimage(set, T2);
7289 set = set_append_equalities(set, eq);
7290 return set;
7293 /* Insert the divs from "ls" before those of "bmap".
7295 * The number of columns is not changed, which means that the last
7296 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7297 * The caller is responsible for removing the same number of dimensions
7298 * from the space of "bmap".
7300 static __isl_give isl_basic_map *insert_divs_from_local_space(
7301 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7303 int i;
7304 int n_div;
7305 int old_n_div;
7307 n_div = isl_local_space_dim(ls, isl_dim_div);
7308 if (n_div == 0)
7309 return bmap;
7311 old_n_div = bmap->n_div;
7312 bmap = insert_div_rows(bmap, n_div);
7313 if (!bmap)
7314 return NULL;
7316 for (i = 0; i < n_div; ++i) {
7317 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7318 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7321 return bmap;
7324 /* Replace the space of "bmap" by the space and divs of "ls".
7326 * If "ls" has any divs, then we simplify the result since we may
7327 * have discovered some additional equalities that could simplify
7328 * the div expressions.
7330 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7331 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7333 int n_div;
7335 bmap = isl_basic_map_cow(bmap);
7336 if (!bmap || !ls)
7337 goto error;
7339 n_div = isl_local_space_dim(ls, isl_dim_div);
7340 bmap = insert_divs_from_local_space(bmap, ls);
7341 if (!bmap)
7342 goto error;
7344 isl_space_free(bmap->dim);
7345 bmap->dim = isl_local_space_get_space(ls);
7346 if (!bmap->dim)
7347 goto error;
7349 isl_local_space_free(ls);
7350 if (n_div > 0)
7351 bmap = isl_basic_map_simplify(bmap);
7352 bmap = isl_basic_map_finalize(bmap);
7353 return bmap;
7354 error:
7355 isl_basic_map_free(bmap);
7356 isl_local_space_free(ls);
7357 return NULL;
7360 /* Replace the space of "map" by the space and divs of "ls".
7362 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7363 __isl_take isl_local_space *ls)
7365 int i;
7367 map = isl_map_cow(map);
7368 if (!map || !ls)
7369 goto error;
7371 for (i = 0; i < map->n; ++i) {
7372 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7373 isl_local_space_copy(ls));
7374 if (!map->p[i])
7375 goto error;
7377 isl_space_free(map->dim);
7378 map->dim = isl_local_space_get_space(ls);
7379 if (!map->dim)
7380 goto error;
7382 isl_local_space_free(ls);
7383 return map;
7384 error:
7385 isl_local_space_free(ls);
7386 isl_map_free(map);
7387 return NULL;
7390 /* Compute an explicit representation for the existentially
7391 * quantified variables for which do not know any explicit representation yet.
7393 * We first sort the existentially quantified variables so that the
7394 * existentially quantified variables for which we already have an explicit
7395 * representation are placed before those for which we do not.
7396 * The input dimensions, the output dimensions and the existentially
7397 * quantified variables for which we already have an explicit
7398 * representation are then turned into parameters.
7399 * compute_divs returns a map with the same parameters and
7400 * no input or output dimensions and the dimension specification
7401 * is reset to that of the input, including the existentially quantified
7402 * variables for which we already had an explicit representation.
7404 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7406 struct isl_basic_set *bset;
7407 struct isl_set *set;
7408 struct isl_map *map;
7409 isl_space *space;
7410 isl_local_space *ls;
7411 unsigned nparam;
7412 unsigned n_in;
7413 unsigned n_out;
7414 int n_known;
7415 int i;
7417 bmap = isl_basic_map_sort_divs(bmap);
7418 bmap = isl_basic_map_cow(bmap);
7419 if (!bmap)
7420 return NULL;
7422 n_known = isl_basic_map_first_unknown_div(bmap);
7423 if (n_known < 0)
7424 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7426 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7427 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7428 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7429 space = isl_space_set_alloc(bmap->ctx,
7430 nparam + n_in + n_out + n_known, 0);
7431 if (!space)
7432 goto error;
7434 ls = isl_basic_map_get_local_space(bmap);
7435 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7436 n_known, bmap->n_div - n_known);
7437 if (n_known > 0) {
7438 for (i = n_known; i < bmap->n_div; ++i)
7439 swap_div(bmap, i - n_known, i);
7440 bmap->n_div -= n_known;
7441 bmap->extra -= n_known;
7443 bmap = isl_basic_map_reset_space(bmap, space);
7444 bset = bset_from_bmap(bmap);
7446 set = parameter_compute_divs(bset);
7447 map = set_to_map(set);
7448 map = replace_space_by_local_space(map, ls);
7450 return map;
7451 error:
7452 isl_basic_map_free(bmap);
7453 return NULL;
7456 /* Remove the explicit representation of local variable "div",
7457 * if there is any.
7459 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7460 __isl_take isl_basic_map *bmap, int div)
7462 isl_bool unknown;
7464 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7465 if (unknown < 0)
7466 return isl_basic_map_free(bmap);
7467 if (unknown)
7468 return bmap;
7470 bmap = isl_basic_map_cow(bmap);
7471 if (!bmap)
7472 return NULL;
7473 isl_int_set_si(bmap->div[div][0], 0);
7474 return bmap;
7477 /* Is local variable "div" of "bmap" marked as not having an explicit
7478 * representation?
7479 * Note that even if "div" is not marked in this way and therefore
7480 * has an explicit representation, this representation may still
7481 * depend (indirectly) on other local variables that do not
7482 * have an explicit representation.
7484 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7485 int div)
7487 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7488 return isl_bool_error;
7489 return isl_int_is_zero(bmap->div[div][0]);
7492 /* Return the position of the first local variable that does not
7493 * have an explicit representation.
7494 * Return the total number of local variables if they all have
7495 * an explicit representation.
7496 * Return -1 on error.
7498 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7500 int i;
7502 if (!bmap)
7503 return -1;
7505 for (i = 0; i < bmap->n_div; ++i) {
7506 if (!isl_basic_map_div_is_known(bmap, i))
7507 return i;
7509 return bmap->n_div;
7512 /* Return the position of the first local variable that does not
7513 * have an explicit representation.
7514 * Return the total number of local variables if they all have
7515 * an explicit representation.
7516 * Return -1 on error.
7518 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7520 return isl_basic_map_first_unknown_div(bset);
7523 /* Does "bmap" have an explicit representation for all local variables?
7525 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7527 int first, n;
7529 n = isl_basic_map_dim(bmap, isl_dim_div);
7530 first = isl_basic_map_first_unknown_div(bmap);
7531 if (first < 0)
7532 return isl_bool_error;
7533 return first == n;
7536 /* Do all basic maps in "map" have an explicit representation
7537 * for all local variables?
7539 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7541 int i;
7543 if (!map)
7544 return isl_bool_error;
7546 for (i = 0; i < map->n; ++i) {
7547 int known = isl_basic_map_divs_known(map->p[i]);
7548 if (known <= 0)
7549 return known;
7552 return isl_bool_true;
7555 /* If bmap contains any unknown divs, then compute explicit
7556 * expressions for them. However, this computation may be
7557 * quite expensive, so first try to remove divs that aren't
7558 * strictly needed.
7560 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7562 int known;
7563 struct isl_map *map;
7565 known = isl_basic_map_divs_known(bmap);
7566 if (known < 0)
7567 goto error;
7568 if (known)
7569 return isl_map_from_basic_map(bmap);
7571 bmap = isl_basic_map_drop_redundant_divs(bmap);
7573 known = isl_basic_map_divs_known(bmap);
7574 if (known < 0)
7575 goto error;
7576 if (known)
7577 return isl_map_from_basic_map(bmap);
7579 map = compute_divs(bmap);
7580 return map;
7581 error:
7582 isl_basic_map_free(bmap);
7583 return NULL;
7586 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7588 int i;
7589 int known;
7590 struct isl_map *res;
7592 if (!map)
7593 return NULL;
7594 if (map->n == 0)
7595 return map;
7597 known = isl_map_divs_known(map);
7598 if (known < 0) {
7599 isl_map_free(map);
7600 return NULL;
7602 if (known)
7603 return map;
7605 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7606 for (i = 1 ; i < map->n; ++i) {
7607 struct isl_map *r2;
7608 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7609 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7610 res = isl_map_union_disjoint(res, r2);
7611 else
7612 res = isl_map_union(res, r2);
7614 isl_map_free(map);
7616 return res;
7619 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7621 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7624 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7626 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7629 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7631 int i;
7632 struct isl_set *set;
7634 if (!map)
7635 goto error;
7637 map = isl_map_cow(map);
7638 if (!map)
7639 return NULL;
7641 set = set_from_map(map);
7642 set->dim = isl_space_domain(set->dim);
7643 if (!set->dim)
7644 goto error;
7645 for (i = 0; i < map->n; ++i) {
7646 set->p[i] = isl_basic_map_domain(map->p[i]);
7647 if (!set->p[i])
7648 goto error;
7650 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7651 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7652 return set;
7653 error:
7654 isl_map_free(map);
7655 return NULL;
7658 /* Return the union of "map1" and "map2", where we assume for now that
7659 * "map1" and "map2" are disjoint. Note that the basic maps inside
7660 * "map1" or "map2" may not be disjoint from each other.
7661 * Also note that this function is also called from isl_map_union,
7662 * which takes care of handling the situation where "map1" and "map2"
7663 * may not be disjoint.
7665 * If one of the inputs is empty, we can simply return the other input.
7666 * Similarly, if one of the inputs is universal, then it is equal to the union.
7668 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7669 __isl_take isl_map *map2)
7671 int i;
7672 unsigned flags = 0;
7673 struct isl_map *map = NULL;
7674 int is_universe;
7676 if (!map1 || !map2)
7677 goto error;
7679 if (!isl_space_is_equal(map1->dim, map2->dim))
7680 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7681 "spaces don't match", goto error);
7683 if (map1->n == 0) {
7684 isl_map_free(map1);
7685 return map2;
7687 if (map2->n == 0) {
7688 isl_map_free(map2);
7689 return map1;
7692 is_universe = isl_map_plain_is_universe(map1);
7693 if (is_universe < 0)
7694 goto error;
7695 if (is_universe) {
7696 isl_map_free(map2);
7697 return map1;
7700 is_universe = isl_map_plain_is_universe(map2);
7701 if (is_universe < 0)
7702 goto error;
7703 if (is_universe) {
7704 isl_map_free(map1);
7705 return map2;
7708 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7709 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7710 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7712 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7713 map1->n + map2->n, flags);
7714 if (!map)
7715 goto error;
7716 for (i = 0; i < map1->n; ++i) {
7717 map = isl_map_add_basic_map(map,
7718 isl_basic_map_copy(map1->p[i]));
7719 if (!map)
7720 goto error;
7722 for (i = 0; i < map2->n; ++i) {
7723 map = isl_map_add_basic_map(map,
7724 isl_basic_map_copy(map2->p[i]));
7725 if (!map)
7726 goto error;
7728 isl_map_free(map1);
7729 isl_map_free(map2);
7730 return map;
7731 error:
7732 isl_map_free(map);
7733 isl_map_free(map1);
7734 isl_map_free(map2);
7735 return NULL;
7738 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7739 * guaranteed to be disjoint by the caller.
7741 * Note that this functions is called from within isl_map_make_disjoint,
7742 * so we have to be careful not to touch the constraints of the inputs
7743 * in any way.
7745 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7746 __isl_take isl_map *map2)
7748 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7751 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7752 * not be disjoint. The parameters are assumed to have been aligned.
7754 * We currently simply call map_union_disjoint, the internal operation
7755 * of which does not really depend on the inputs being disjoint.
7756 * If the result contains more than one basic map, then we clear
7757 * the disjoint flag since the result may contain basic maps from
7758 * both inputs and these are not guaranteed to be disjoint.
7760 * As a special case, if "map1" and "map2" are obviously equal,
7761 * then we simply return "map1".
7763 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7764 __isl_take isl_map *map2)
7766 int equal;
7768 if (!map1 || !map2)
7769 goto error;
7771 equal = isl_map_plain_is_equal(map1, map2);
7772 if (equal < 0)
7773 goto error;
7774 if (equal) {
7775 isl_map_free(map2);
7776 return map1;
7779 map1 = map_union_disjoint(map1, map2);
7780 if (!map1)
7781 return NULL;
7782 if (map1->n > 1)
7783 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7784 return map1;
7785 error:
7786 isl_map_free(map1);
7787 isl_map_free(map2);
7788 return NULL;
7791 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7792 * not be disjoint.
7794 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7795 __isl_take isl_map *map2)
7797 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7800 __isl_give isl_set *isl_set_union_disjoint(
7801 __isl_take isl_set *set1, __isl_take isl_set *set2)
7803 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7804 set_to_map(set2)));
7807 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7809 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7812 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7813 * the results.
7815 * "map" and "set" are assumed to be compatible and non-NULL.
7817 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7818 __isl_take isl_set *set,
7819 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7820 __isl_take isl_basic_set *bset))
7822 unsigned flags = 0;
7823 struct isl_map *result;
7824 int i, j;
7826 if (isl_set_plain_is_universe(set)) {
7827 isl_set_free(set);
7828 return map;
7831 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7832 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7833 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7835 result = isl_map_alloc_space(isl_space_copy(map->dim),
7836 map->n * set->n, flags);
7837 for (i = 0; result && i < map->n; ++i)
7838 for (j = 0; j < set->n; ++j) {
7839 result = isl_map_add_basic_map(result,
7840 fn(isl_basic_map_copy(map->p[i]),
7841 isl_basic_set_copy(set->p[j])));
7842 if (!result)
7843 break;
7846 isl_map_free(map);
7847 isl_set_free(set);
7848 return result;
7851 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7852 __isl_take isl_set *set)
7854 isl_bool ok;
7856 ok = isl_map_compatible_range(map, set);
7857 if (ok < 0)
7858 goto error;
7859 if (!ok)
7860 isl_die(set->ctx, isl_error_invalid,
7861 "incompatible spaces", goto error);
7863 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7864 error:
7865 isl_map_free(map);
7866 isl_set_free(set);
7867 return NULL;
7870 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7871 __isl_take isl_set *set)
7873 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7876 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7877 __isl_take isl_set *set)
7879 isl_bool ok;
7881 ok = isl_map_compatible_domain(map, set);
7882 if (ok < 0)
7883 goto error;
7884 if (!ok)
7885 isl_die(set->ctx, isl_error_invalid,
7886 "incompatible spaces", goto error);
7888 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7889 error:
7890 isl_map_free(map);
7891 isl_set_free(set);
7892 return NULL;
7895 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7896 __isl_take isl_set *set)
7898 return isl_map_align_params_map_map_and(map, set,
7899 &map_intersect_domain);
7902 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7903 * in the space B -> C, return the intersection.
7904 * The parameters are assumed to have been aligned.
7906 * The map "factor" is first extended to a map living in the space
7907 * [A -> B] -> C and then a regular intersection is computed.
7909 static __isl_give isl_map *map_intersect_domain_factor_range(
7910 __isl_take isl_map *map, __isl_take isl_map *factor)
7912 isl_space *space;
7913 isl_map *ext_factor;
7915 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7916 ext_factor = isl_map_universe(space);
7917 ext_factor = isl_map_domain_product(ext_factor, factor);
7918 return map_intersect(map, ext_factor);
7921 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7922 * in the space B -> C, return the intersection.
7924 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7925 __isl_take isl_map *map, __isl_take isl_map *factor)
7927 return isl_map_align_params_map_map_and(map, factor,
7928 &map_intersect_domain_factor_range);
7931 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7932 * in the space A -> C, return the intersection.
7934 * The map "factor" is first extended to a map living in the space
7935 * A -> [B -> C] and then a regular intersection is computed.
7937 static __isl_give isl_map *map_intersect_range_factor_range(
7938 __isl_take isl_map *map, __isl_take isl_map *factor)
7940 isl_space *space;
7941 isl_map *ext_factor;
7943 space = isl_space_range_factor_domain(isl_map_get_space(map));
7944 ext_factor = isl_map_universe(space);
7945 ext_factor = isl_map_range_product(ext_factor, factor);
7946 return isl_map_intersect(map, ext_factor);
7949 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7950 * in the space A -> C, return the intersection.
7952 __isl_give isl_map *isl_map_intersect_range_factor_range(
7953 __isl_take isl_map *map, __isl_take isl_map *factor)
7955 return isl_map_align_params_map_map_and(map, factor,
7956 &map_intersect_range_factor_range);
7959 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7960 __isl_take isl_map *map2)
7962 if (!map1 || !map2)
7963 goto error;
7964 map1 = isl_map_reverse(map1);
7965 map1 = isl_map_apply_range(map1, map2);
7966 return isl_map_reverse(map1);
7967 error:
7968 isl_map_free(map1);
7969 isl_map_free(map2);
7970 return NULL;
7973 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7974 __isl_take isl_map *map2)
7976 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7979 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7980 __isl_take isl_map *map2)
7982 isl_space *space;
7983 struct isl_map *result;
7984 int i, j;
7986 if (!map1 || !map2)
7987 goto error;
7989 space = isl_space_join(isl_space_copy(map1->dim),
7990 isl_space_copy(map2->dim));
7992 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7993 if (!result)
7994 goto error;
7995 for (i = 0; i < map1->n; ++i)
7996 for (j = 0; j < map2->n; ++j) {
7997 result = isl_map_add_basic_map(result,
7998 isl_basic_map_apply_range(
7999 isl_basic_map_copy(map1->p[i]),
8000 isl_basic_map_copy(map2->p[j])));
8001 if (!result)
8002 goto error;
8004 isl_map_free(map1);
8005 isl_map_free(map2);
8006 if (result && result->n <= 1)
8007 ISL_F_SET(result, ISL_MAP_DISJOINT);
8008 return result;
8009 error:
8010 isl_map_free(map1);
8011 isl_map_free(map2);
8012 return NULL;
8015 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8016 __isl_take isl_map *map2)
8018 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8022 * returns range - domain
8024 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8026 isl_space *target_space;
8027 struct isl_basic_set *bset;
8028 unsigned dim;
8029 unsigned nparam;
8030 int i;
8032 if (!bmap)
8033 goto error;
8034 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8035 bmap->dim, isl_dim_out),
8036 goto error);
8037 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8038 dim = isl_basic_map_dim(bmap, isl_dim_in);
8039 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8040 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8041 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8042 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8043 for (i = 0; i < dim; ++i) {
8044 int j = isl_basic_map_alloc_equality(bmap);
8045 if (j < 0) {
8046 bmap = isl_basic_map_free(bmap);
8047 break;
8049 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8050 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8051 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8052 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8054 bset = isl_basic_map_domain(bmap);
8055 bset = isl_basic_set_reset_space(bset, target_space);
8056 return bset;
8057 error:
8058 isl_basic_map_free(bmap);
8059 return NULL;
8063 * returns range - domain
8065 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8067 int i;
8068 isl_space *dim;
8069 struct isl_set *result;
8071 if (!map)
8072 return NULL;
8074 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8075 map->dim, isl_dim_out),
8076 goto error);
8077 dim = isl_map_get_space(map);
8078 dim = isl_space_domain(dim);
8079 result = isl_set_alloc_space(dim, map->n, 0);
8080 if (!result)
8081 goto error;
8082 for (i = 0; i < map->n; ++i)
8083 result = isl_set_add_basic_set(result,
8084 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8085 isl_map_free(map);
8086 return result;
8087 error:
8088 isl_map_free(map);
8089 return NULL;
8093 * returns [domain -> range] -> range - domain
8095 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8096 __isl_take isl_basic_map *bmap)
8098 int i, k;
8099 isl_space *space;
8100 isl_basic_map *domain;
8101 int nparam, n;
8102 unsigned total;
8104 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8105 bmap->dim, isl_dim_out))
8106 isl_die(bmap->ctx, isl_error_invalid,
8107 "domain and range don't match", goto error);
8109 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8110 n = isl_basic_map_dim(bmap, isl_dim_in);
8112 space = isl_basic_map_get_space(bmap);
8113 space = isl_space_from_range(isl_space_domain(space));
8114 domain = isl_basic_map_universe(space);
8116 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8117 bmap = isl_basic_map_apply_range(bmap, domain);
8118 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8120 total = isl_basic_map_total_dim(bmap);
8122 for (i = 0; i < n; ++i) {
8123 k = isl_basic_map_alloc_equality(bmap);
8124 if (k < 0)
8125 goto error;
8126 isl_seq_clr(bmap->eq[k], 1 + total);
8127 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8128 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8129 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8132 bmap = isl_basic_map_gauss(bmap, NULL);
8133 return isl_basic_map_finalize(bmap);
8134 error:
8135 isl_basic_map_free(bmap);
8136 return NULL;
8140 * returns [domain -> range] -> range - domain
8142 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8144 int i;
8145 isl_space *domain_space;
8147 if (!map)
8148 return NULL;
8150 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8151 map->dim, isl_dim_out))
8152 isl_die(map->ctx, isl_error_invalid,
8153 "domain and range don't match", goto error);
8155 map = isl_map_cow(map);
8156 if (!map)
8157 return NULL;
8159 domain_space = isl_space_domain(isl_map_get_space(map));
8160 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8161 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8162 if (!map->dim)
8163 goto error;
8164 for (i = 0; i < map->n; ++i) {
8165 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8166 if (!map->p[i])
8167 goto error;
8169 map = isl_map_unmark_normalized(map);
8170 return map;
8171 error:
8172 isl_map_free(map);
8173 return NULL;
8176 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8178 unsigned n_in, n_out;
8180 if (!space)
8181 return NULL;
8182 n_in = isl_space_dim(space, isl_dim_in);
8183 n_out = isl_space_dim(space, isl_dim_out);
8184 if (n_in != n_out)
8185 isl_die(space->ctx, isl_error_invalid,
8186 "number of input and output dimensions needs to be "
8187 "the same", goto error);
8188 return isl_basic_map_equal(space, n_in);
8189 error:
8190 isl_space_free(space);
8191 return NULL;
8194 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8196 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8199 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8201 isl_space *dim = isl_set_get_space(set);
8202 isl_map *id;
8203 id = isl_map_identity(isl_space_map_from_set(dim));
8204 return isl_map_intersect_range(id, set);
8207 /* Construct a basic set with all set dimensions having only non-negative
8208 * values.
8210 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8211 __isl_take isl_space *space)
8213 int i;
8214 unsigned nparam;
8215 unsigned dim;
8216 struct isl_basic_set *bset;
8218 if (!space)
8219 return NULL;
8220 nparam = space->nparam;
8221 dim = space->n_out;
8222 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8223 if (!bset)
8224 return NULL;
8225 for (i = 0; i < dim; ++i) {
8226 int k = isl_basic_set_alloc_inequality(bset);
8227 if (k < 0)
8228 goto error;
8229 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8230 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8232 return bset;
8233 error:
8234 isl_basic_set_free(bset);
8235 return NULL;
8238 /* Construct the half-space x_pos >= 0.
8240 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8241 int pos)
8243 int k;
8244 isl_basic_set *nonneg;
8246 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8247 k = isl_basic_set_alloc_inequality(nonneg);
8248 if (k < 0)
8249 goto error;
8250 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8251 isl_int_set_si(nonneg->ineq[k][pos], 1);
8253 return isl_basic_set_finalize(nonneg);
8254 error:
8255 isl_basic_set_free(nonneg);
8256 return NULL;
8259 /* Construct the half-space x_pos <= -1.
8261 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8262 int pos)
8264 int k;
8265 isl_basic_set *neg;
8267 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8268 k = isl_basic_set_alloc_inequality(neg);
8269 if (k < 0)
8270 goto error;
8271 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8272 isl_int_set_si(neg->ineq[k][0], -1);
8273 isl_int_set_si(neg->ineq[k][pos], -1);
8275 return isl_basic_set_finalize(neg);
8276 error:
8277 isl_basic_set_free(neg);
8278 return NULL;
8281 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8282 enum isl_dim_type type, unsigned first, unsigned n)
8284 int i;
8285 unsigned offset;
8286 isl_basic_set *nonneg;
8287 isl_basic_set *neg;
8289 if (n == 0)
8290 return set;
8292 if (isl_set_check_range(set, type, first, n) < 0)
8293 return isl_set_free(set);
8295 offset = pos(set->dim, type);
8296 for (i = 0; i < n; ++i) {
8297 nonneg = nonneg_halfspace(isl_set_get_space(set),
8298 offset + first + i);
8299 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8301 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8304 return set;
8307 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8308 int len,
8309 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8310 void *user)
8312 isl_set *half;
8314 if (!set)
8315 return isl_stat_error;
8316 if (isl_set_plain_is_empty(set)) {
8317 isl_set_free(set);
8318 return isl_stat_ok;
8320 if (first == len)
8321 return fn(set, signs, user);
8323 signs[first] = 1;
8324 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8325 1 + first));
8326 half = isl_set_intersect(half, isl_set_copy(set));
8327 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8328 goto error;
8330 signs[first] = -1;
8331 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8332 1 + first));
8333 half = isl_set_intersect(half, set);
8334 return foreach_orthant(half, signs, first + 1, len, fn, user);
8335 error:
8336 isl_set_free(set);
8337 return isl_stat_error;
8340 /* Call "fn" on the intersections of "set" with each of the orthants
8341 * (except for obviously empty intersections). The orthant is identified
8342 * by the signs array, with each entry having value 1 or -1 according
8343 * to the sign of the corresponding variable.
8345 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8346 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8347 void *user)
8349 unsigned nparam;
8350 unsigned nvar;
8351 int *signs;
8352 isl_stat r;
8354 if (!set)
8355 return isl_stat_error;
8356 if (isl_set_plain_is_empty(set))
8357 return isl_stat_ok;
8359 nparam = isl_set_dim(set, isl_dim_param);
8360 nvar = isl_set_dim(set, isl_dim_set);
8362 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8364 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8365 fn, user);
8367 free(signs);
8369 return r;
8372 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8374 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8377 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8378 __isl_keep isl_basic_map *bmap2)
8380 isl_bool is_subset;
8381 struct isl_map *map1;
8382 struct isl_map *map2;
8384 if (!bmap1 || !bmap2)
8385 return isl_bool_error;
8387 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8388 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8390 is_subset = isl_map_is_subset(map1, map2);
8392 isl_map_free(map1);
8393 isl_map_free(map2);
8395 return is_subset;
8398 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8399 __isl_keep isl_basic_set *bset2)
8401 return isl_basic_map_is_subset(bset1, bset2);
8404 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8405 __isl_keep isl_basic_map *bmap2)
8407 isl_bool is_subset;
8409 if (!bmap1 || !bmap2)
8410 return isl_bool_error;
8411 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8412 if (is_subset != isl_bool_true)
8413 return is_subset;
8414 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8415 return is_subset;
8418 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8419 __isl_keep isl_basic_set *bset2)
8421 return isl_basic_map_is_equal(
8422 bset_to_bmap(bset1), bset_to_bmap(bset2));
8425 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8427 int i;
8428 int is_empty;
8430 if (!map)
8431 return isl_bool_error;
8432 for (i = 0; i < map->n; ++i) {
8433 is_empty = isl_basic_map_is_empty(map->p[i]);
8434 if (is_empty < 0)
8435 return isl_bool_error;
8436 if (!is_empty)
8437 return isl_bool_false;
8439 return isl_bool_true;
8442 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8444 return map ? map->n == 0 : isl_bool_error;
8447 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8449 return set ? set->n == 0 : isl_bool_error;
8452 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8454 return isl_map_is_empty(set_to_map(set));
8457 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8458 __isl_keep isl_map *map2)
8460 if (!map1 || !map2)
8461 return isl_bool_error;
8463 return isl_space_is_equal(map1->dim, map2->dim);
8466 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8467 __isl_keep isl_set *set2)
8469 if (!set1 || !set2)
8470 return isl_bool_error;
8472 return isl_space_is_equal(set1->dim, set2->dim);
8475 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8477 isl_bool is_subset;
8479 if (!map1 || !map2)
8480 return isl_bool_error;
8481 is_subset = isl_map_is_subset(map1, map2);
8482 if (is_subset != isl_bool_true)
8483 return is_subset;
8484 is_subset = isl_map_is_subset(map2, map1);
8485 return is_subset;
8488 /* Is "map1" equal to "map2"?
8490 * First check if they are obviously equal.
8491 * If not, then perform a more detailed analysis.
8493 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8495 isl_bool equal;
8497 equal = isl_map_plain_is_equal(map1, map2);
8498 if (equal < 0 || equal)
8499 return equal;
8500 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8503 isl_bool isl_basic_map_is_strict_subset(
8504 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8506 isl_bool is_subset;
8508 if (!bmap1 || !bmap2)
8509 return isl_bool_error;
8510 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8511 if (is_subset != isl_bool_true)
8512 return is_subset;
8513 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8514 return isl_bool_not(is_subset);
8517 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8518 __isl_keep isl_map *map2)
8520 isl_bool is_subset;
8522 if (!map1 || !map2)
8523 return isl_bool_error;
8524 is_subset = isl_map_is_subset(map1, map2);
8525 if (is_subset != isl_bool_true)
8526 return is_subset;
8527 is_subset = isl_map_is_subset(map2, map1);
8528 return isl_bool_not(is_subset);
8531 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8532 __isl_keep isl_set *set2)
8534 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8537 /* Is "bmap" obviously equal to the universe with the same space?
8539 * That is, does it not have any constraints?
8541 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8543 if (!bmap)
8544 return isl_bool_error;
8545 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8548 /* Is "bset" obviously equal to the universe with the same space?
8550 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8552 return isl_basic_map_plain_is_universe(bset);
8555 /* If "c" does not involve any existentially quantified variables,
8556 * then set *univ to false and abort
8558 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8560 isl_bool *univ = user;
8561 unsigned n;
8563 n = isl_constraint_dim(c, isl_dim_div);
8564 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8565 isl_constraint_free(c);
8566 if (*univ < 0 || !*univ)
8567 return isl_stat_error;
8568 return isl_stat_ok;
8571 /* Is "bmap" equal to the universe with the same space?
8573 * First check if it is obviously equal to the universe.
8574 * If not and if there are any constraints not involving
8575 * existentially quantified variables, then it is certainly
8576 * not equal to the universe.
8577 * Otherwise, check if the universe is a subset of "bmap".
8579 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8581 isl_bool univ;
8582 isl_basic_map *test;
8584 univ = isl_basic_map_plain_is_universe(bmap);
8585 if (univ < 0 || univ)
8586 return univ;
8587 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8588 return isl_bool_false;
8589 univ = isl_bool_true;
8590 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8591 univ)
8592 return isl_bool_error;
8593 if (univ < 0 || !univ)
8594 return univ;
8595 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8596 univ = isl_basic_map_is_subset(test, bmap);
8597 isl_basic_map_free(test);
8598 return univ;
8601 /* Is "bset" equal to the universe with the same space?
8603 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8605 return isl_basic_map_is_universe(bset);
8608 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8610 int i;
8612 if (!map)
8613 return isl_bool_error;
8615 for (i = 0; i < map->n; ++i) {
8616 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8617 if (r < 0 || r)
8618 return r;
8621 return isl_bool_false;
8624 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8626 return isl_map_plain_is_universe(set_to_map(set));
8629 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8631 struct isl_basic_set *bset = NULL;
8632 struct isl_vec *sample = NULL;
8633 isl_bool empty, non_empty;
8635 if (!bmap)
8636 return isl_bool_error;
8638 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8639 return isl_bool_true;
8641 if (isl_basic_map_plain_is_universe(bmap))
8642 return isl_bool_false;
8644 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8645 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8646 copy = isl_basic_map_remove_redundancies(copy);
8647 empty = isl_basic_map_plain_is_empty(copy);
8648 isl_basic_map_free(copy);
8649 return empty;
8652 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8653 if (non_empty < 0)
8654 return isl_bool_error;
8655 if (non_empty)
8656 return isl_bool_false;
8657 isl_vec_free(bmap->sample);
8658 bmap->sample = NULL;
8659 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8660 if (!bset)
8661 return isl_bool_error;
8662 sample = isl_basic_set_sample_vec(bset);
8663 if (!sample)
8664 return isl_bool_error;
8665 empty = sample->size == 0;
8666 isl_vec_free(bmap->sample);
8667 bmap->sample = sample;
8668 if (empty)
8669 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8671 return empty;
8674 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8676 if (!bmap)
8677 return isl_bool_error;
8678 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8681 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8683 if (!bset)
8684 return isl_bool_error;
8685 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8688 /* Is "bmap" known to be non-empty?
8690 * That is, is the cached sample still valid?
8692 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8694 unsigned total;
8696 if (!bmap)
8697 return isl_bool_error;
8698 if (!bmap->sample)
8699 return isl_bool_false;
8700 total = 1 + isl_basic_map_total_dim(bmap);
8701 if (bmap->sample->size != total)
8702 return isl_bool_false;
8703 return isl_basic_map_contains(bmap, bmap->sample);
8706 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8708 return isl_basic_map_is_empty(bset_to_bmap(bset));
8711 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8712 __isl_take isl_basic_map *bmap2)
8714 struct isl_map *map;
8715 if (!bmap1 || !bmap2)
8716 goto error;
8718 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8720 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8721 if (!map)
8722 goto error;
8723 map = isl_map_add_basic_map(map, bmap1);
8724 map = isl_map_add_basic_map(map, bmap2);
8725 return map;
8726 error:
8727 isl_basic_map_free(bmap1);
8728 isl_basic_map_free(bmap2);
8729 return NULL;
8732 struct isl_set *isl_basic_set_union(
8733 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8735 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8736 bset_to_bmap(bset2)));
8739 /* Order divs such that any div only depends on previous divs */
8740 __isl_give isl_basic_map *isl_basic_map_order_divs(
8741 __isl_take isl_basic_map *bmap)
8743 int i;
8744 unsigned off;
8746 if (!bmap)
8747 return NULL;
8749 off = isl_space_dim(bmap->dim, isl_dim_all);
8751 for (i = 0; i < bmap->n_div; ++i) {
8752 int pos;
8753 if (isl_int_is_zero(bmap->div[i][0]))
8754 continue;
8755 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8756 bmap->n_div-i);
8757 if (pos == -1)
8758 continue;
8759 if (pos == 0)
8760 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8761 "integer division depends on itself",
8762 return isl_basic_map_free(bmap));
8763 isl_basic_map_swap_div(bmap, i, i + pos);
8764 --i;
8766 return bmap;
8769 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8771 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8774 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8776 int i;
8778 if (!map)
8779 return 0;
8781 for (i = 0; i < map->n; ++i) {
8782 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8783 if (!map->p[i])
8784 goto error;
8787 return map;
8788 error:
8789 isl_map_free(map);
8790 return NULL;
8793 /* Sort the local variables of "bset".
8795 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8796 __isl_take isl_basic_set *bset)
8798 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8801 /* Apply the expansion computed by isl_merge_divs.
8802 * The expansion itself is given by "exp" while the resulting
8803 * list of divs is given by "div".
8805 * Move the integer divisions of "bmap" into the right position
8806 * according to "exp" and then introduce the additional integer
8807 * divisions, adding div constraints.
8808 * The moving should be done first to avoid moving coefficients
8809 * in the definitions of the extra integer divisions.
8811 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8812 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8814 int i, j;
8815 int n_div;
8817 bmap = isl_basic_map_cow(bmap);
8818 if (!bmap || !div)
8819 goto error;
8821 if (div->n_row < bmap->n_div)
8822 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8823 "not an expansion", goto error);
8825 n_div = bmap->n_div;
8826 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8827 div->n_row - n_div, 0,
8828 2 * (div->n_row - n_div));
8830 for (i = n_div; i < div->n_row; ++i)
8831 if (isl_basic_map_alloc_div(bmap) < 0)
8832 goto error;
8834 for (j = n_div - 1; j >= 0; --j) {
8835 if (exp[j] == j)
8836 break;
8837 isl_basic_map_swap_div(bmap, j, exp[j]);
8839 j = 0;
8840 for (i = 0; i < div->n_row; ++i) {
8841 if (j < n_div && exp[j] == i) {
8842 j++;
8843 } else {
8844 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8845 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8846 continue;
8847 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8848 goto error;
8852 isl_mat_free(div);
8853 return bmap;
8854 error:
8855 isl_basic_map_free(bmap);
8856 isl_mat_free(div);
8857 return NULL;
8860 /* Apply the expansion computed by isl_merge_divs.
8861 * The expansion itself is given by "exp" while the resulting
8862 * list of divs is given by "div".
8864 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8865 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8867 return isl_basic_map_expand_divs(bset, div, exp);
8870 /* Look for a div in dst that corresponds to the div "div" in src.
8871 * The divs before "div" in src and dst are assumed to be the same.
8873 * Returns -1 if no corresponding div was found and the position
8874 * of the corresponding div in dst otherwise.
8876 static int find_div(__isl_keep isl_basic_map *dst,
8877 __isl_keep isl_basic_map *src, unsigned div)
8879 int i;
8881 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8883 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8884 for (i = div; i < dst->n_div; ++i)
8885 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8886 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8887 dst->n_div - div) == -1)
8888 return i;
8889 return -1;
8892 /* Align the divs of "dst" to those of "src", adding divs from "src"
8893 * if needed. That is, make sure that the first src->n_div divs
8894 * of the result are equal to those of src.
8896 * The result is not finalized as by design it will have redundant
8897 * divs if any divs from "src" were copied.
8899 __isl_give isl_basic_map *isl_basic_map_align_divs(
8900 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8902 int i;
8903 isl_bool known;
8904 int extended;
8905 unsigned total;
8907 if (!dst || !src)
8908 return isl_basic_map_free(dst);
8910 if (src->n_div == 0)
8911 return dst;
8913 known = isl_basic_map_divs_known(src);
8914 if (known < 0)
8915 return isl_basic_map_free(dst);
8916 if (!known)
8917 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8918 "some src divs are unknown",
8919 return isl_basic_map_free(dst));
8921 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8922 if (!src)
8923 return isl_basic_map_free(dst);
8925 extended = 0;
8926 total = isl_space_dim(src->dim, isl_dim_all);
8927 for (i = 0; i < src->n_div; ++i) {
8928 int j = find_div(dst, src, i);
8929 if (j < 0) {
8930 if (!extended) {
8931 int extra = src->n_div - i;
8932 dst = isl_basic_map_cow(dst);
8933 if (!dst)
8934 goto error;
8935 dst = isl_basic_map_extend_space(dst,
8936 isl_space_copy(dst->dim),
8937 extra, 0, 2 * extra);
8938 extended = 1;
8940 j = isl_basic_map_alloc_div(dst);
8941 if (j < 0)
8942 goto error;
8943 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8944 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8945 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8946 goto error;
8948 if (j != i)
8949 isl_basic_map_swap_div(dst, i, j);
8951 isl_basic_map_free(src);
8952 return dst;
8953 error:
8954 isl_basic_map_free(src);
8955 isl_basic_map_free(dst);
8956 return NULL;
8959 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8961 int i;
8963 if (!map)
8964 return NULL;
8965 if (map->n == 0)
8966 return map;
8967 map = isl_map_compute_divs(map);
8968 map = isl_map_cow(map);
8969 if (!map)
8970 return NULL;
8972 for (i = 1; i < map->n; ++i)
8973 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8974 for (i = 1; i < map->n; ++i) {
8975 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8976 if (!map->p[i])
8977 return isl_map_free(map);
8980 map = isl_map_unmark_normalized(map);
8981 return map;
8984 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8986 return isl_map_align_divs_internal(map);
8989 struct isl_set *isl_set_align_divs(struct isl_set *set)
8991 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8994 /* Align the divs of the basic maps in "map" to those
8995 * of the basic maps in "list", as well as to the other basic maps in "map".
8996 * The elements in "list" are assumed to have known divs.
8998 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8999 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9001 int i, n;
9003 map = isl_map_compute_divs(map);
9004 map = isl_map_cow(map);
9005 if (!map || !list)
9006 return isl_map_free(map);
9007 if (map->n == 0)
9008 return map;
9010 n = isl_basic_map_list_n_basic_map(list);
9011 for (i = 0; i < n; ++i) {
9012 isl_basic_map *bmap;
9014 bmap = isl_basic_map_list_get_basic_map(list, i);
9015 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9016 isl_basic_map_free(bmap);
9018 if (!map->p[0])
9019 return isl_map_free(map);
9021 return isl_map_align_divs_internal(map);
9024 /* Align the divs of each element of "list" to those of "bmap".
9025 * Both "bmap" and the elements of "list" are assumed to have known divs.
9027 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9028 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9030 int i, n;
9032 if (!list || !bmap)
9033 return isl_basic_map_list_free(list);
9035 n = isl_basic_map_list_n_basic_map(list);
9036 for (i = 0; i < n; ++i) {
9037 isl_basic_map *bmap_i;
9039 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9040 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9041 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9044 return list;
9047 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9048 __isl_take isl_map *map)
9050 isl_bool ok;
9052 ok = isl_map_compatible_domain(map, set);
9053 if (ok < 0)
9054 goto error;
9055 if (!ok)
9056 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9057 "incompatible spaces", goto error);
9058 map = isl_map_intersect_domain(map, set);
9059 set = isl_map_range(map);
9060 return set;
9061 error:
9062 isl_set_free(set);
9063 isl_map_free(map);
9064 return NULL;
9067 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9068 __isl_take isl_map *map)
9070 return isl_map_align_params_map_map_and(set, map, &set_apply);
9073 /* There is no need to cow as removing empty parts doesn't change
9074 * the meaning of the set.
9076 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9078 int i;
9080 if (!map)
9081 return NULL;
9083 for (i = map->n - 1; i >= 0; --i)
9084 map = remove_if_empty(map, i);
9086 return map;
9089 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9091 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9094 /* Create a binary relation that maps the shared initial "pos" dimensions
9095 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9097 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9098 __isl_keep isl_basic_set *bset2, int pos)
9100 isl_basic_map *bmap1;
9101 isl_basic_map *bmap2;
9103 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9104 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9105 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9106 isl_dim_out, 0, pos);
9107 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9108 isl_dim_out, 0, pos);
9109 return isl_basic_map_range_product(bmap1, bmap2);
9112 /* Given two basic sets bset1 and bset2, compute the maximal difference
9113 * between the values of dimension pos in bset1 and those in bset2
9114 * for any common value of the parameters and dimensions preceding pos.
9116 static enum isl_lp_result basic_set_maximal_difference_at(
9117 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9118 int pos, isl_int *opt)
9120 isl_basic_map *bmap1;
9121 struct isl_ctx *ctx;
9122 struct isl_vec *obj;
9123 unsigned total;
9124 unsigned nparam;
9125 unsigned dim1;
9126 enum isl_lp_result res;
9128 if (!bset1 || !bset2)
9129 return isl_lp_error;
9131 nparam = isl_basic_set_n_param(bset1);
9132 dim1 = isl_basic_set_n_dim(bset1);
9134 bmap1 = join_initial(bset1, bset2, pos);
9135 if (!bmap1)
9136 return isl_lp_error;
9138 total = isl_basic_map_total_dim(bmap1);
9139 ctx = bmap1->ctx;
9140 obj = isl_vec_alloc(ctx, 1 + total);
9141 if (!obj)
9142 goto error;
9143 isl_seq_clr(obj->block.data, 1 + total);
9144 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9145 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9146 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9147 opt, NULL, NULL);
9148 isl_basic_map_free(bmap1);
9149 isl_vec_free(obj);
9150 return res;
9151 error:
9152 isl_basic_map_free(bmap1);
9153 return isl_lp_error;
9156 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9157 * for any common value of the parameters and dimensions preceding pos
9158 * in both basic sets, the values of dimension pos in bset1 are
9159 * smaller or larger than those in bset2.
9161 * Returns
9162 * 1 if bset1 follows bset2
9163 * -1 if bset1 precedes bset2
9164 * 0 if bset1 and bset2 are incomparable
9165 * -2 if some error occurred.
9167 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9168 __isl_keep isl_basic_set *bset2, int pos)
9170 isl_int opt;
9171 enum isl_lp_result res;
9172 int cmp;
9174 isl_int_init(opt);
9176 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9178 if (res == isl_lp_empty)
9179 cmp = 0;
9180 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9181 res == isl_lp_unbounded)
9182 cmp = 1;
9183 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9184 cmp = -1;
9185 else
9186 cmp = -2;
9188 isl_int_clear(opt);
9189 return cmp;
9192 /* Given two basic sets bset1 and bset2, check whether
9193 * for any common value of the parameters and dimensions preceding pos
9194 * there is a value of dimension pos in bset1 that is larger
9195 * than a value of the same dimension in bset2.
9197 * Return
9198 * 1 if there exists such a pair
9199 * 0 if there is no such pair, but there is a pair of equal values
9200 * -1 otherwise
9201 * -2 if some error occurred.
9203 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9204 __isl_keep isl_basic_set *bset2, int pos)
9206 isl_bool empty;
9207 isl_basic_map *bmap;
9208 unsigned dim1;
9210 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9211 bmap = join_initial(bset1, bset2, pos);
9212 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9213 isl_dim_out, dim1 - pos);
9214 empty = isl_basic_map_is_empty(bmap);
9215 if (empty < 0)
9216 goto error;
9217 if (empty) {
9218 isl_basic_map_free(bmap);
9219 return -1;
9221 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9222 isl_dim_out, dim1 - pos);
9223 empty = isl_basic_map_is_empty(bmap);
9224 if (empty < 0)
9225 goto error;
9226 isl_basic_map_free(bmap);
9227 if (empty)
9228 return 0;
9229 return 1;
9230 error:
9231 isl_basic_map_free(bmap);
9232 return -2;
9235 /* Given two sets set1 and set2, check whether
9236 * for any common value of the parameters and dimensions preceding pos
9237 * there is a value of dimension pos in set1 that is larger
9238 * than a value of the same dimension in set2.
9240 * Return
9241 * 1 if there exists such a pair
9242 * 0 if there is no such pair, but there is a pair of equal values
9243 * -1 otherwise
9244 * -2 if some error occurred.
9246 int isl_set_follows_at(__isl_keep isl_set *set1,
9247 __isl_keep isl_set *set2, int pos)
9249 int i, j;
9250 int follows = -1;
9252 if (!set1 || !set2)
9253 return -2;
9255 for (i = 0; i < set1->n; ++i)
9256 for (j = 0; j < set2->n; ++j) {
9257 int f;
9258 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9259 if (f == 1 || f == -2)
9260 return f;
9261 if (f > follows)
9262 follows = f;
9265 return follows;
9268 static isl_bool isl_basic_map_plain_has_fixed_var(
9269 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9271 int i;
9272 int d;
9273 unsigned total;
9275 if (!bmap)
9276 return isl_bool_error;
9277 total = isl_basic_map_total_dim(bmap);
9278 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9279 for (; d+1 > pos; --d)
9280 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9281 break;
9282 if (d != pos)
9283 continue;
9284 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9285 return isl_bool_false;
9286 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9287 return isl_bool_false;
9288 if (!isl_int_is_one(bmap->eq[i][1+d]))
9289 return isl_bool_false;
9290 if (val)
9291 isl_int_neg(*val, bmap->eq[i][0]);
9292 return isl_bool_true;
9294 return isl_bool_false;
9297 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9298 unsigned pos, isl_int *val)
9300 int i;
9301 isl_int v;
9302 isl_int tmp;
9303 isl_bool fixed;
9305 if (!map)
9306 return isl_bool_error;
9307 if (map->n == 0)
9308 return isl_bool_false;
9309 if (map->n == 1)
9310 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9311 isl_int_init(v);
9312 isl_int_init(tmp);
9313 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9314 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9315 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9316 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9317 fixed = isl_bool_false;
9319 if (val)
9320 isl_int_set(*val, v);
9321 isl_int_clear(tmp);
9322 isl_int_clear(v);
9323 return fixed;
9326 static isl_bool isl_basic_set_plain_has_fixed_var(
9327 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9329 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9330 pos, val);
9333 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9334 enum isl_dim_type type, unsigned pos, isl_int *val)
9336 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9337 return isl_bool_error;
9338 return isl_basic_map_plain_has_fixed_var(bmap,
9339 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9342 /* If "bmap" obviously lies on a hyperplane where the given dimension
9343 * has a fixed value, then return that value.
9344 * Otherwise return NaN.
9346 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9347 __isl_keep isl_basic_map *bmap,
9348 enum isl_dim_type type, unsigned pos)
9350 isl_ctx *ctx;
9351 isl_val *v;
9352 isl_bool fixed;
9354 if (!bmap)
9355 return NULL;
9356 ctx = isl_basic_map_get_ctx(bmap);
9357 v = isl_val_alloc(ctx);
9358 if (!v)
9359 return NULL;
9360 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9361 if (fixed < 0)
9362 return isl_val_free(v);
9363 if (fixed) {
9364 isl_int_set_si(v->d, 1);
9365 return v;
9367 isl_val_free(v);
9368 return isl_val_nan(ctx);
9371 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9372 enum isl_dim_type type, unsigned pos, isl_int *val)
9374 if (isl_map_check_range(map, type, pos, 1) < 0)
9375 return isl_bool_error;
9376 return isl_map_plain_has_fixed_var(map,
9377 map_offset(map, type) - 1 + pos, val);
9380 /* If "map" obviously lies on a hyperplane where the given dimension
9381 * has a fixed value, then return that value.
9382 * Otherwise return NaN.
9384 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9385 enum isl_dim_type type, unsigned pos)
9387 isl_ctx *ctx;
9388 isl_val *v;
9389 isl_bool fixed;
9391 if (!map)
9392 return NULL;
9393 ctx = isl_map_get_ctx(map);
9394 v = isl_val_alloc(ctx);
9395 if (!v)
9396 return NULL;
9397 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9398 if (fixed < 0)
9399 return isl_val_free(v);
9400 if (fixed) {
9401 isl_int_set_si(v->d, 1);
9402 return v;
9404 isl_val_free(v);
9405 return isl_val_nan(ctx);
9408 /* If "set" obviously lies on a hyperplane where the given dimension
9409 * has a fixed value, then return that value.
9410 * Otherwise return NaN.
9412 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9413 enum isl_dim_type type, unsigned pos)
9415 return isl_map_plain_get_val_if_fixed(set, type, pos);
9418 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9419 * then return this fixed value in *val.
9421 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9422 unsigned dim, isl_int *val)
9424 return isl_basic_set_plain_has_fixed_var(bset,
9425 isl_basic_set_n_param(bset) + dim, val);
9428 /* Return -1 if the constraint "c1" should be sorted before "c2"
9429 * and 1 if it should be sorted after "c2".
9430 * Return 0 if the two constraints are the same (up to the constant term).
9432 * In particular, if a constraint involves later variables than another
9433 * then it is sorted after this other constraint.
9434 * uset_gist depends on constraints without existentially quantified
9435 * variables sorting first.
9437 * For constraints that have the same latest variable, those
9438 * with the same coefficient for this latest variable (first in absolute value
9439 * and then in actual value) are grouped together.
9440 * This is useful for detecting pairs of constraints that can
9441 * be chained in their printed representation.
9443 * Finally, within a group, constraints are sorted according to
9444 * their coefficients (excluding the constant term).
9446 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9448 isl_int **c1 = (isl_int **) p1;
9449 isl_int **c2 = (isl_int **) p2;
9450 int l1, l2;
9451 unsigned size = *(unsigned *) arg;
9452 int cmp;
9454 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9455 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9457 if (l1 != l2)
9458 return l1 - l2;
9460 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9461 if (cmp != 0)
9462 return cmp;
9463 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9464 if (cmp != 0)
9465 return -cmp;
9467 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9470 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9471 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9472 * and 0 if the two constraints are the same (up to the constant term).
9474 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9475 isl_int *c1, isl_int *c2)
9477 unsigned total;
9479 if (!bmap)
9480 return -2;
9481 total = isl_basic_map_total_dim(bmap);
9482 return sort_constraint_cmp(&c1, &c2, &total);
9485 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9486 __isl_take isl_basic_map *bmap)
9488 unsigned total;
9490 if (!bmap)
9491 return NULL;
9492 if (bmap->n_ineq == 0)
9493 return bmap;
9494 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9495 return bmap;
9496 total = isl_basic_map_total_dim(bmap);
9497 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9498 &sort_constraint_cmp, &total) < 0)
9499 return isl_basic_map_free(bmap);
9500 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9501 return bmap;
9504 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9505 __isl_take isl_basic_set *bset)
9507 isl_basic_map *bmap = bset_to_bmap(bset);
9508 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9511 __isl_give isl_basic_map *isl_basic_map_normalize(
9512 __isl_take isl_basic_map *bmap)
9514 bmap = isl_basic_map_remove_redundancies(bmap);
9515 bmap = isl_basic_map_sort_constraints(bmap);
9516 return bmap;
9518 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9519 __isl_keep isl_basic_map *bmap2)
9521 int i, cmp;
9522 unsigned total;
9523 isl_space *space1, *space2;
9525 if (!bmap1 || !bmap2)
9526 return -1;
9528 if (bmap1 == bmap2)
9529 return 0;
9530 space1 = isl_basic_map_peek_space(bmap1);
9531 space2 = isl_basic_map_peek_space(bmap2);
9532 cmp = isl_space_cmp(space1, space2);
9533 if (cmp)
9534 return cmp;
9535 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9536 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9537 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9538 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9539 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9540 return 0;
9541 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9542 return 1;
9543 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9544 return -1;
9545 if (bmap1->n_eq != bmap2->n_eq)
9546 return bmap1->n_eq - bmap2->n_eq;
9547 if (bmap1->n_ineq != bmap2->n_ineq)
9548 return bmap1->n_ineq - bmap2->n_ineq;
9549 if (bmap1->n_div != bmap2->n_div)
9550 return bmap1->n_div - bmap2->n_div;
9551 total = isl_basic_map_total_dim(bmap1);
9552 for (i = 0; i < bmap1->n_eq; ++i) {
9553 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9554 if (cmp)
9555 return cmp;
9557 for (i = 0; i < bmap1->n_ineq; ++i) {
9558 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9559 if (cmp)
9560 return cmp;
9562 for (i = 0; i < bmap1->n_div; ++i) {
9563 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9564 if (cmp)
9565 return cmp;
9567 return 0;
9570 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9571 __isl_keep isl_basic_set *bset2)
9573 return isl_basic_map_plain_cmp(bset1, bset2);
9576 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9578 int i, cmp;
9580 if (set1 == set2)
9581 return 0;
9582 if (set1->n != set2->n)
9583 return set1->n - set2->n;
9585 for (i = 0; i < set1->n; ++i) {
9586 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9587 if (cmp)
9588 return cmp;
9591 return 0;
9594 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9595 __isl_keep isl_basic_map *bmap2)
9597 if (!bmap1 || !bmap2)
9598 return isl_bool_error;
9599 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9602 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9603 __isl_keep isl_basic_set *bset2)
9605 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9606 bset_to_bmap(bset2));
9609 static int qsort_bmap_cmp(const void *p1, const void *p2)
9611 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9612 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9614 return isl_basic_map_plain_cmp(bmap1, bmap2);
9617 /* Sort the basic maps of "map" and remove duplicate basic maps.
9619 * While removing basic maps, we make sure that the basic maps remain
9620 * sorted because isl_map_normalize expects the basic maps of the result
9621 * to be sorted.
9623 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9625 int i, j;
9627 map = isl_map_remove_empty_parts(map);
9628 if (!map)
9629 return NULL;
9630 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9631 for (i = map->n - 1; i >= 1; --i) {
9632 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9633 continue;
9634 isl_basic_map_free(map->p[i-1]);
9635 for (j = i; j < map->n; ++j)
9636 map->p[j - 1] = map->p[j];
9637 map->n--;
9640 return map;
9643 /* Remove obvious duplicates among the basic maps of "map".
9645 * Unlike isl_map_normalize, this function does not remove redundant
9646 * constraints and only removes duplicates that have exactly the same
9647 * constraints in the input. It does sort the constraints and
9648 * the basic maps to ease the detection of duplicates.
9650 * If "map" has already been normalized or if the basic maps are
9651 * disjoint, then there can be no duplicates.
9653 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9655 int i;
9656 isl_basic_map *bmap;
9658 if (!map)
9659 return NULL;
9660 if (map->n <= 1)
9661 return map;
9662 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9663 return map;
9664 for (i = 0; i < map->n; ++i) {
9665 bmap = isl_basic_map_copy(map->p[i]);
9666 bmap = isl_basic_map_sort_constraints(bmap);
9667 if (!bmap)
9668 return isl_map_free(map);
9669 isl_basic_map_free(map->p[i]);
9670 map->p[i] = bmap;
9673 map = sort_and_remove_duplicates(map);
9674 return map;
9677 /* We normalize in place, but if anything goes wrong we need
9678 * to return NULL, so we need to make sure we don't change the
9679 * meaning of any possible other copies of map.
9681 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9683 int i;
9684 struct isl_basic_map *bmap;
9686 if (!map)
9687 return NULL;
9688 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9689 return map;
9690 for (i = 0; i < map->n; ++i) {
9691 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9692 if (!bmap)
9693 goto error;
9694 isl_basic_map_free(map->p[i]);
9695 map->p[i] = bmap;
9698 map = sort_and_remove_duplicates(map);
9699 if (map)
9700 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9701 return map;
9702 error:
9703 isl_map_free(map);
9704 return NULL;
9707 struct isl_set *isl_set_normalize(struct isl_set *set)
9709 return set_from_map(isl_map_normalize(set_to_map(set)));
9712 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9713 __isl_keep isl_map *map2)
9715 int i;
9716 isl_bool equal;
9718 if (!map1 || !map2)
9719 return isl_bool_error;
9721 if (map1 == map2)
9722 return isl_bool_true;
9723 if (!isl_space_is_equal(map1->dim, map2->dim))
9724 return isl_bool_false;
9726 map1 = isl_map_copy(map1);
9727 map2 = isl_map_copy(map2);
9728 map1 = isl_map_normalize(map1);
9729 map2 = isl_map_normalize(map2);
9730 if (!map1 || !map2)
9731 goto error;
9732 equal = map1->n == map2->n;
9733 for (i = 0; equal && i < map1->n; ++i) {
9734 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9735 if (equal < 0)
9736 goto error;
9738 isl_map_free(map1);
9739 isl_map_free(map2);
9740 return equal;
9741 error:
9742 isl_map_free(map1);
9743 isl_map_free(map2);
9744 return isl_bool_error;
9747 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9748 __isl_keep isl_set *set2)
9750 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9753 /* Return the basic maps in "map" as a list.
9755 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9756 __isl_keep isl_map *map)
9758 int i;
9759 isl_ctx *ctx;
9760 isl_basic_map_list *list;
9762 if (!map)
9763 return NULL;
9764 ctx = isl_map_get_ctx(map);
9765 list = isl_basic_map_list_alloc(ctx, map->n);
9767 for (i = 0; i < map->n; ++i) {
9768 isl_basic_map *bmap;
9770 bmap = isl_basic_map_copy(map->p[i]);
9771 list = isl_basic_map_list_add(list, bmap);
9774 return list;
9777 /* Return the intersection of the elements in the non-empty list "list".
9778 * All elements are assumed to live in the same space.
9780 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9781 __isl_take isl_basic_map_list *list)
9783 int i, n;
9784 isl_basic_map *bmap;
9786 if (!list)
9787 return NULL;
9788 n = isl_basic_map_list_n_basic_map(list);
9789 if (n < 1)
9790 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9791 "expecting non-empty list", goto error);
9793 bmap = isl_basic_map_list_get_basic_map(list, 0);
9794 for (i = 1; i < n; ++i) {
9795 isl_basic_map *bmap_i;
9797 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9798 bmap = isl_basic_map_intersect(bmap, bmap_i);
9801 isl_basic_map_list_free(list);
9802 return bmap;
9803 error:
9804 isl_basic_map_list_free(list);
9805 return NULL;
9808 /* Return the intersection of the elements in the non-empty list "list".
9809 * All elements are assumed to live in the same space.
9811 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9812 __isl_take isl_basic_set_list *list)
9814 return isl_basic_map_list_intersect(list);
9817 /* Return the union of the elements of "list".
9818 * The list is required to have at least one element.
9820 __isl_give isl_set *isl_basic_set_list_union(
9821 __isl_take isl_basic_set_list *list)
9823 int i, n;
9824 isl_space *space;
9825 isl_basic_set *bset;
9826 isl_set *set;
9828 if (!list)
9829 return NULL;
9830 n = isl_basic_set_list_n_basic_set(list);
9831 if (n < 1)
9832 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9833 "expecting non-empty list", goto error);
9835 bset = isl_basic_set_list_get_basic_set(list, 0);
9836 space = isl_basic_set_get_space(bset);
9837 isl_basic_set_free(bset);
9839 set = isl_set_alloc_space(space, n, 0);
9840 for (i = 0; i < n; ++i) {
9841 bset = isl_basic_set_list_get_basic_set(list, i);
9842 set = isl_set_add_basic_set(set, bset);
9845 isl_basic_set_list_free(list);
9846 return set;
9847 error:
9848 isl_basic_set_list_free(list);
9849 return NULL;
9852 /* Return the union of the elements in the non-empty list "list".
9853 * All elements are assumed to live in the same space.
9855 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9857 int i, n;
9858 isl_set *set;
9860 if (!list)
9861 return NULL;
9862 n = isl_set_list_n_set(list);
9863 if (n < 1)
9864 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9865 "expecting non-empty list", goto error);
9867 set = isl_set_list_get_set(list, 0);
9868 for (i = 1; i < n; ++i) {
9869 isl_set *set_i;
9871 set_i = isl_set_list_get_set(list, i);
9872 set = isl_set_union(set, set_i);
9875 isl_set_list_free(list);
9876 return set;
9877 error:
9878 isl_set_list_free(list);
9879 return NULL;
9882 __isl_give isl_basic_map *isl_basic_map_product(
9883 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9885 isl_space *space_result = NULL;
9886 struct isl_basic_map *bmap;
9887 unsigned in1, in2, out1, out2, nparam, total, pos;
9888 struct isl_dim_map *dim_map1, *dim_map2;
9890 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9891 goto error;
9892 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9893 isl_space_copy(bmap2->dim));
9895 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9896 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9897 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9898 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9899 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9901 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9902 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9903 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9904 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9905 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9906 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9907 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9908 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9909 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9910 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9911 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9913 bmap = isl_basic_map_alloc_space(space_result,
9914 bmap1->n_div + bmap2->n_div,
9915 bmap1->n_eq + bmap2->n_eq,
9916 bmap1->n_ineq + bmap2->n_ineq);
9917 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9918 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9919 bmap = isl_basic_map_simplify(bmap);
9920 return isl_basic_map_finalize(bmap);
9921 error:
9922 isl_basic_map_free(bmap1);
9923 isl_basic_map_free(bmap2);
9924 return NULL;
9927 __isl_give isl_basic_map *isl_basic_map_flat_product(
9928 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9930 isl_basic_map *prod;
9932 prod = isl_basic_map_product(bmap1, bmap2);
9933 prod = isl_basic_map_flatten(prod);
9934 return prod;
9937 __isl_give isl_basic_set *isl_basic_set_flat_product(
9938 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9940 return isl_basic_map_flat_range_product(bset1, bset2);
9943 __isl_give isl_basic_map *isl_basic_map_domain_product(
9944 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9946 isl_space *space_result = NULL;
9947 isl_basic_map *bmap;
9948 unsigned in1, in2, out, nparam, total, pos;
9949 struct isl_dim_map *dim_map1, *dim_map2;
9951 if (!bmap1 || !bmap2)
9952 goto error;
9954 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9955 isl_space_copy(bmap2->dim));
9957 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9958 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9959 out = isl_basic_map_dim(bmap1, isl_dim_out);
9960 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9962 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9963 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9964 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9965 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9966 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9967 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9968 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9969 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9970 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9971 isl_dim_map_div(dim_map1, bmap1, pos += out);
9972 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9974 bmap = isl_basic_map_alloc_space(space_result,
9975 bmap1->n_div + bmap2->n_div,
9976 bmap1->n_eq + bmap2->n_eq,
9977 bmap1->n_ineq + bmap2->n_ineq);
9978 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9979 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9980 bmap = isl_basic_map_simplify(bmap);
9981 return isl_basic_map_finalize(bmap);
9982 error:
9983 isl_basic_map_free(bmap1);
9984 isl_basic_map_free(bmap2);
9985 return NULL;
9988 __isl_give isl_basic_map *isl_basic_map_range_product(
9989 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9991 isl_bool rational;
9992 isl_space *space_result = NULL;
9993 isl_basic_map *bmap;
9994 unsigned in, out1, out2, nparam, total, pos;
9995 struct isl_dim_map *dim_map1, *dim_map2;
9997 rational = isl_basic_map_is_rational(bmap1);
9998 if (rational >= 0 && rational)
9999 rational = isl_basic_map_is_rational(bmap2);
10000 if (!bmap1 || !bmap2 || rational < 0)
10001 goto error;
10003 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10004 goto error;
10006 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10007 isl_space_copy(bmap2->dim));
10009 in = isl_basic_map_dim(bmap1, isl_dim_in);
10010 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10011 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10012 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10014 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10015 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10016 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10017 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10018 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10019 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10020 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10021 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10022 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10023 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10024 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10026 bmap = isl_basic_map_alloc_space(space_result,
10027 bmap1->n_div + bmap2->n_div,
10028 bmap1->n_eq + bmap2->n_eq,
10029 bmap1->n_ineq + bmap2->n_ineq);
10030 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10031 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10032 if (rational)
10033 bmap = isl_basic_map_set_rational(bmap);
10034 bmap = isl_basic_map_simplify(bmap);
10035 return isl_basic_map_finalize(bmap);
10036 error:
10037 isl_basic_map_free(bmap1);
10038 isl_basic_map_free(bmap2);
10039 return NULL;
10042 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10043 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10045 isl_basic_map *prod;
10047 prod = isl_basic_map_range_product(bmap1, bmap2);
10048 prod = isl_basic_map_flatten_range(prod);
10049 return prod;
10052 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10053 * and collect the results.
10054 * The result live in the space obtained by calling "space_product"
10055 * on the spaces of "map1" and "map2".
10056 * If "remove_duplicates" is set then the result may contain duplicates
10057 * (even if the inputs do not) and so we try and remove the obvious
10058 * duplicates.
10060 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10061 __isl_take isl_map *map2,
10062 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10063 __isl_take isl_space *right),
10064 __isl_give isl_basic_map *(*basic_map_product)(
10065 __isl_take isl_basic_map *left,
10066 __isl_take isl_basic_map *right),
10067 int remove_duplicates)
10069 unsigned flags = 0;
10070 struct isl_map *result;
10071 int i, j;
10072 isl_bool m;
10074 m = isl_map_has_equal_params(map1, map2);
10075 if (m < 0)
10076 goto error;
10077 if (!m)
10078 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10079 "parameters don't match", goto error);
10081 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10082 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10083 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10085 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10086 isl_space_copy(map2->dim)),
10087 map1->n * map2->n, flags);
10088 if (!result)
10089 goto error;
10090 for (i = 0; i < map1->n; ++i)
10091 for (j = 0; j < map2->n; ++j) {
10092 struct isl_basic_map *part;
10093 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10094 isl_basic_map_copy(map2->p[j]));
10095 if (isl_basic_map_is_empty(part))
10096 isl_basic_map_free(part);
10097 else
10098 result = isl_map_add_basic_map(result, part);
10099 if (!result)
10100 goto error;
10102 if (remove_duplicates)
10103 result = isl_map_remove_obvious_duplicates(result);
10104 isl_map_free(map1);
10105 isl_map_free(map2);
10106 return result;
10107 error:
10108 isl_map_free(map1);
10109 isl_map_free(map2);
10110 return NULL;
10113 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10115 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10116 __isl_take isl_map *map2)
10118 return map_product(map1, map2, &isl_space_product,
10119 &isl_basic_map_product, 0);
10122 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10123 __isl_take isl_map *map2)
10125 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10128 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10130 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10131 __isl_take isl_map *map2)
10133 isl_map *prod;
10135 prod = isl_map_product(map1, map2);
10136 prod = isl_map_flatten(prod);
10137 return prod;
10140 /* Given two set A and B, construct its Cartesian product A x B.
10142 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10144 return isl_map_range_product(set1, set2);
10147 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10148 __isl_take isl_set *set2)
10150 return isl_map_flat_range_product(set1, set2);
10153 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10155 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10156 __isl_take isl_map *map2)
10158 return map_product(map1, map2, &isl_space_domain_product,
10159 &isl_basic_map_domain_product, 1);
10162 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10164 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10165 __isl_take isl_map *map2)
10167 return map_product(map1, map2, &isl_space_range_product,
10168 &isl_basic_map_range_product, 1);
10171 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10172 __isl_take isl_map *map2)
10174 return isl_map_align_params_map_map_and(map1, map2,
10175 &map_domain_product_aligned);
10178 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10179 __isl_take isl_map *map2)
10181 return isl_map_align_params_map_map_and(map1, map2,
10182 &map_range_product_aligned);
10185 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10187 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10189 isl_space *space;
10190 int total1, keep1, total2, keep2;
10192 if (!map)
10193 return NULL;
10194 if (!isl_space_domain_is_wrapping(map->dim) ||
10195 !isl_space_range_is_wrapping(map->dim))
10196 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10197 "not a product", return isl_map_free(map));
10199 space = isl_map_get_space(map);
10200 total1 = isl_space_dim(space, isl_dim_in);
10201 total2 = isl_space_dim(space, isl_dim_out);
10202 space = isl_space_factor_domain(space);
10203 keep1 = isl_space_dim(space, isl_dim_in);
10204 keep2 = isl_space_dim(space, isl_dim_out);
10205 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10206 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10207 map = isl_map_reset_space(map, space);
10209 return map;
10212 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10214 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10216 isl_space *space;
10217 int total1, keep1, total2, keep2;
10219 if (!map)
10220 return NULL;
10221 if (!isl_space_domain_is_wrapping(map->dim) ||
10222 !isl_space_range_is_wrapping(map->dim))
10223 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10224 "not a product", return isl_map_free(map));
10226 space = isl_map_get_space(map);
10227 total1 = isl_space_dim(space, isl_dim_in);
10228 total2 = isl_space_dim(space, isl_dim_out);
10229 space = isl_space_factor_range(space);
10230 keep1 = isl_space_dim(space, isl_dim_in);
10231 keep2 = isl_space_dim(space, isl_dim_out);
10232 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10233 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10234 map = isl_map_reset_space(map, space);
10236 return map;
10239 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10241 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10243 isl_space *space;
10244 int total, keep;
10246 if (!map)
10247 return NULL;
10248 if (!isl_space_domain_is_wrapping(map->dim))
10249 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10250 "domain is not a product", return isl_map_free(map));
10252 space = isl_map_get_space(map);
10253 total = isl_space_dim(space, isl_dim_in);
10254 space = isl_space_domain_factor_domain(space);
10255 keep = isl_space_dim(space, isl_dim_in);
10256 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10257 map = isl_map_reset_space(map, space);
10259 return map;
10262 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10264 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10266 isl_space *space;
10267 int total, keep;
10269 if (!map)
10270 return NULL;
10271 if (!isl_space_domain_is_wrapping(map->dim))
10272 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10273 "domain is not a product", return isl_map_free(map));
10275 space = isl_map_get_space(map);
10276 total = isl_space_dim(space, isl_dim_in);
10277 space = isl_space_domain_factor_range(space);
10278 keep = isl_space_dim(space, isl_dim_in);
10279 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10280 map = isl_map_reset_space(map, space);
10282 return map;
10285 /* Given a map A -> [B -> C], extract the map A -> B.
10287 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10289 isl_space *space;
10290 int total, keep;
10292 if (!map)
10293 return NULL;
10294 if (!isl_space_range_is_wrapping(map->dim))
10295 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10296 "range is not a product", return isl_map_free(map));
10298 space = isl_map_get_space(map);
10299 total = isl_space_dim(space, isl_dim_out);
10300 space = isl_space_range_factor_domain(space);
10301 keep = isl_space_dim(space, isl_dim_out);
10302 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10303 map = isl_map_reset_space(map, space);
10305 return map;
10308 /* Given a map A -> [B -> C], extract the map A -> C.
10310 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10312 isl_space *space;
10313 int total, keep;
10315 if (!map)
10316 return NULL;
10317 if (!isl_space_range_is_wrapping(map->dim))
10318 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10319 "range is not a product", return isl_map_free(map));
10321 space = isl_map_get_space(map);
10322 total = isl_space_dim(space, isl_dim_out);
10323 space = isl_space_range_factor_range(space);
10324 keep = isl_space_dim(space, isl_dim_out);
10325 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10326 map = isl_map_reset_space(map, space);
10328 return map;
10331 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10333 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10334 __isl_take isl_map *map2)
10336 isl_map *prod;
10338 prod = isl_map_domain_product(map1, map2);
10339 prod = isl_map_flatten_domain(prod);
10340 return prod;
10343 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10345 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10346 __isl_take isl_map *map2)
10348 isl_map *prod;
10350 prod = isl_map_range_product(map1, map2);
10351 prod = isl_map_flatten_range(prod);
10352 return prod;
10355 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10357 int i;
10358 uint32_t hash = isl_hash_init();
10359 unsigned total;
10361 if (!bmap)
10362 return 0;
10363 bmap = isl_basic_map_copy(bmap);
10364 bmap = isl_basic_map_normalize(bmap);
10365 if (!bmap)
10366 return 0;
10367 total = isl_basic_map_total_dim(bmap);
10368 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10369 for (i = 0; i < bmap->n_eq; ++i) {
10370 uint32_t c_hash;
10371 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10372 isl_hash_hash(hash, c_hash);
10374 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10375 for (i = 0; i < bmap->n_ineq; ++i) {
10376 uint32_t c_hash;
10377 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10378 isl_hash_hash(hash, c_hash);
10380 isl_hash_byte(hash, bmap->n_div & 0xFF);
10381 for (i = 0; i < bmap->n_div; ++i) {
10382 uint32_t c_hash;
10383 if (isl_int_is_zero(bmap->div[i][0]))
10384 continue;
10385 isl_hash_byte(hash, i & 0xFF);
10386 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10387 isl_hash_hash(hash, c_hash);
10389 isl_basic_map_free(bmap);
10390 return hash;
10393 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10395 return isl_basic_map_get_hash(bset_to_bmap(bset));
10398 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10400 int i;
10401 uint32_t hash;
10403 if (!map)
10404 return 0;
10405 map = isl_map_copy(map);
10406 map = isl_map_normalize(map);
10407 if (!map)
10408 return 0;
10410 hash = isl_hash_init();
10411 for (i = 0; i < map->n; ++i) {
10412 uint32_t bmap_hash;
10413 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10414 isl_hash_hash(hash, bmap_hash);
10417 isl_map_free(map);
10419 return hash;
10422 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10424 return isl_map_get_hash(set_to_map(set));
10427 /* Return the number of basic maps in the (current) representation of "map".
10429 int isl_map_n_basic_map(__isl_keep isl_map *map)
10431 return map ? map->n : 0;
10434 int isl_set_n_basic_set(__isl_keep isl_set *set)
10436 return set ? set->n : 0;
10439 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10440 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10442 int i;
10444 if (!map)
10445 return isl_stat_error;
10447 for (i = 0; i < map->n; ++i)
10448 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10449 return isl_stat_error;
10451 return isl_stat_ok;
10454 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10455 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10457 int i;
10459 if (!set)
10460 return isl_stat_error;
10462 for (i = 0; i < set->n; ++i)
10463 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10464 return isl_stat_error;
10466 return isl_stat_ok;
10469 /* Return a list of basic sets, the union of which is equal to "set".
10471 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10472 __isl_keep isl_set *set)
10474 int i;
10475 isl_basic_set_list *list;
10477 if (!set)
10478 return NULL;
10480 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10481 for (i = 0; i < set->n; ++i) {
10482 isl_basic_set *bset;
10484 bset = isl_basic_set_copy(set->p[i]);
10485 list = isl_basic_set_list_add(list, bset);
10488 return list;
10491 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10493 isl_space *space;
10495 if (!bset)
10496 return NULL;
10498 bset = isl_basic_set_cow(bset);
10499 if (!bset)
10500 return NULL;
10502 space = isl_basic_set_get_space(bset);
10503 space = isl_space_lift(space, bset->n_div);
10504 if (!space)
10505 goto error;
10506 isl_space_free(bset->dim);
10507 bset->dim = space;
10508 bset->extra -= bset->n_div;
10509 bset->n_div = 0;
10511 bset = isl_basic_set_finalize(bset);
10513 return bset;
10514 error:
10515 isl_basic_set_free(bset);
10516 return NULL;
10519 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10521 int i;
10522 isl_space *space;
10523 unsigned n_div;
10525 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10527 if (!set)
10528 return NULL;
10530 set = isl_set_cow(set);
10531 if (!set)
10532 return NULL;
10534 n_div = set->p[0]->n_div;
10535 space = isl_set_get_space(set);
10536 space = isl_space_lift(space, n_div);
10537 if (!space)
10538 goto error;
10539 isl_space_free(set->dim);
10540 set->dim = space;
10542 for (i = 0; i < set->n; ++i) {
10543 set->p[i] = isl_basic_set_lift(set->p[i]);
10544 if (!set->p[i])
10545 goto error;
10548 return set;
10549 error:
10550 isl_set_free(set);
10551 return NULL;
10554 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10556 unsigned dim;
10557 int size = 0;
10559 if (!bset)
10560 return -1;
10562 dim = isl_basic_set_total_dim(bset);
10563 size += bset->n_eq * (1 + dim);
10564 size += bset->n_ineq * (1 + dim);
10565 size += bset->n_div * (2 + dim);
10567 return size;
10570 int isl_set_size(__isl_keep isl_set *set)
10572 int i;
10573 int size = 0;
10575 if (!set)
10576 return -1;
10578 for (i = 0; i < set->n; ++i)
10579 size += isl_basic_set_size(set->p[i]);
10581 return size;
10584 /* Check if there is any lower bound (if lower == 0) and/or upper
10585 * bound (if upper == 0) on the specified dim.
10587 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10588 enum isl_dim_type type, unsigned pos, int lower, int upper)
10590 int i;
10592 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10593 return isl_bool_error;
10595 pos += isl_basic_map_offset(bmap, type);
10597 for (i = 0; i < bmap->n_div; ++i) {
10598 if (isl_int_is_zero(bmap->div[i][0]))
10599 continue;
10600 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10601 return isl_bool_true;
10604 for (i = 0; i < bmap->n_eq; ++i)
10605 if (!isl_int_is_zero(bmap->eq[i][pos]))
10606 return isl_bool_true;
10608 for (i = 0; i < bmap->n_ineq; ++i) {
10609 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10610 if (sgn > 0)
10611 lower = 1;
10612 if (sgn < 0)
10613 upper = 1;
10616 return lower && upper;
10619 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10620 enum isl_dim_type type, unsigned pos)
10622 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10625 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10626 enum isl_dim_type type, unsigned pos)
10628 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10631 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10632 enum isl_dim_type type, unsigned pos)
10634 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10637 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10638 enum isl_dim_type type, unsigned pos)
10640 int i;
10642 if (!map)
10643 return isl_bool_error;
10645 for (i = 0; i < map->n; ++i) {
10646 isl_bool bounded;
10647 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10648 if (bounded < 0 || !bounded)
10649 return bounded;
10652 return isl_bool_true;
10655 /* Return true if the specified dim is involved in both an upper bound
10656 * and a lower bound.
10658 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10659 enum isl_dim_type type, unsigned pos)
10661 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10664 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10666 static isl_bool has_any_bound(__isl_keep isl_map *map,
10667 enum isl_dim_type type, unsigned pos,
10668 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10669 enum isl_dim_type type, unsigned pos))
10671 int i;
10673 if (!map)
10674 return isl_bool_error;
10676 for (i = 0; i < map->n; ++i) {
10677 isl_bool bounded;
10678 bounded = fn(map->p[i], type, pos);
10679 if (bounded < 0 || bounded)
10680 return bounded;
10683 return isl_bool_false;
10686 /* Return 1 if the specified dim is involved in any lower bound.
10688 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10689 enum isl_dim_type type, unsigned pos)
10691 return has_any_bound(set, type, pos,
10692 &isl_basic_map_dim_has_lower_bound);
10695 /* Return 1 if the specified dim is involved in any upper bound.
10697 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10698 enum isl_dim_type type, unsigned pos)
10700 return has_any_bound(set, type, pos,
10701 &isl_basic_map_dim_has_upper_bound);
10704 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10706 static isl_bool has_bound(__isl_keep isl_map *map,
10707 enum isl_dim_type type, unsigned pos,
10708 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10709 enum isl_dim_type type, unsigned pos))
10711 int i;
10713 if (!map)
10714 return isl_bool_error;
10716 for (i = 0; i < map->n; ++i) {
10717 isl_bool bounded;
10718 bounded = fn(map->p[i], type, pos);
10719 if (bounded < 0 || !bounded)
10720 return bounded;
10723 return isl_bool_true;
10726 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10728 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10729 enum isl_dim_type type, unsigned pos)
10731 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10734 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10736 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10737 enum isl_dim_type type, unsigned pos)
10739 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10742 /* For each of the "n" variables starting at "first", determine
10743 * the sign of the variable and put the results in the first "n"
10744 * elements of the array "signs".
10745 * Sign
10746 * 1 means that the variable is non-negative
10747 * -1 means that the variable is non-positive
10748 * 0 means the variable attains both positive and negative values.
10750 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10751 unsigned first, unsigned n, int *signs)
10753 isl_vec *bound = NULL;
10754 struct isl_tab *tab = NULL;
10755 struct isl_tab_undo *snap;
10756 int i;
10758 if (!bset || !signs)
10759 return isl_stat_error;
10761 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10762 tab = isl_tab_from_basic_set(bset, 0);
10763 if (!bound || !tab)
10764 goto error;
10766 isl_seq_clr(bound->el, bound->size);
10767 isl_int_set_si(bound->el[0], -1);
10769 snap = isl_tab_snap(tab);
10770 for (i = 0; i < n; ++i) {
10771 int empty;
10773 isl_int_set_si(bound->el[1 + first + i], -1);
10774 if (isl_tab_add_ineq(tab, bound->el) < 0)
10775 goto error;
10776 empty = tab->empty;
10777 isl_int_set_si(bound->el[1 + first + i], 0);
10778 if (isl_tab_rollback(tab, snap) < 0)
10779 goto error;
10781 if (empty) {
10782 signs[i] = 1;
10783 continue;
10786 isl_int_set_si(bound->el[1 + first + i], 1);
10787 if (isl_tab_add_ineq(tab, bound->el) < 0)
10788 goto error;
10789 empty = tab->empty;
10790 isl_int_set_si(bound->el[1 + first + i], 0);
10791 if (isl_tab_rollback(tab, snap) < 0)
10792 goto error;
10794 signs[i] = empty ? -1 : 0;
10797 isl_tab_free(tab);
10798 isl_vec_free(bound);
10799 return isl_stat_ok;
10800 error:
10801 isl_tab_free(tab);
10802 isl_vec_free(bound);
10803 return isl_stat_error;
10806 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10807 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10809 if (!bset || !signs)
10810 return isl_stat_error;
10811 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10812 return isl_stat_error;
10814 first += pos(bset->dim, type) - 1;
10815 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10818 /* Is it possible for the integer division "div" to depend (possibly
10819 * indirectly) on any output dimensions?
10821 * If the div is undefined, then we conservatively assume that it
10822 * may depend on them.
10823 * Otherwise, we check if it actually depends on them or on any integer
10824 * divisions that may depend on them.
10826 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10828 int i;
10829 unsigned n_out, o_out;
10830 unsigned n_div, o_div;
10832 if (isl_int_is_zero(bmap->div[div][0]))
10833 return isl_bool_true;
10835 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10836 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10838 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10839 return isl_bool_true;
10841 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10842 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10844 for (i = 0; i < n_div; ++i) {
10845 isl_bool may_involve;
10847 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10848 continue;
10849 may_involve = div_may_involve_output(bmap, i);
10850 if (may_involve < 0 || may_involve)
10851 return may_involve;
10854 return isl_bool_false;
10857 /* Return the first integer division of "bmap" in the range
10858 * [first, first + n[ that may depend on any output dimensions and
10859 * that has a non-zero coefficient in "c" (where the first coefficient
10860 * in "c" corresponds to integer division "first").
10862 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10863 isl_int *c, int first, int n)
10865 int k;
10867 if (!bmap)
10868 return -1;
10870 for (k = first; k < first + n; ++k) {
10871 isl_bool may_involve;
10873 if (isl_int_is_zero(c[k]))
10874 continue;
10875 may_involve = div_may_involve_output(bmap, k);
10876 if (may_involve < 0)
10877 return -1;
10878 if (may_involve)
10879 return k;
10882 return first + n;
10885 /* Look for a pair of inequality constraints in "bmap" of the form
10887 * -l + i >= 0 or i >= l
10888 * and
10889 * n + l - i >= 0 or i <= l + n
10891 * with n < "m" and i the output dimension at position "pos".
10892 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10893 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10894 * and earlier output dimensions, as well as integer divisions that do
10895 * not involve any of the output dimensions.
10897 * Return the index of the first inequality constraint or bmap->n_ineq
10898 * if no such pair can be found.
10900 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10901 int pos, isl_int m)
10903 int i, j;
10904 isl_ctx *ctx;
10905 unsigned total;
10906 unsigned n_div, o_div;
10907 unsigned n_out, o_out;
10908 int less;
10910 if (!bmap)
10911 return -1;
10913 ctx = isl_basic_map_get_ctx(bmap);
10914 total = isl_basic_map_total_dim(bmap);
10915 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10916 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10917 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10918 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10919 for (i = 0; i < bmap->n_ineq; ++i) {
10920 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10921 continue;
10922 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10923 n_out - (pos + 1)) != -1)
10924 continue;
10925 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10926 0, n_div) < n_div)
10927 continue;
10928 for (j = i + 1; j < bmap->n_ineq; ++j) {
10929 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10930 ctx->one))
10931 continue;
10932 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10933 bmap->ineq[j] + 1, total))
10934 continue;
10935 break;
10937 if (j >= bmap->n_ineq)
10938 continue;
10939 isl_int_add(bmap->ineq[i][0],
10940 bmap->ineq[i][0], bmap->ineq[j][0]);
10941 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10942 isl_int_sub(bmap->ineq[i][0],
10943 bmap->ineq[i][0], bmap->ineq[j][0]);
10944 if (!less)
10945 continue;
10946 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10947 return i;
10948 else
10949 return j;
10952 return bmap->n_ineq;
10955 /* Return the index of the equality of "bmap" that defines
10956 * the output dimension "pos" in terms of earlier dimensions.
10957 * The equality may also involve integer divisions, as long
10958 * as those integer divisions are defined in terms of
10959 * parameters or input dimensions.
10960 * In this case, *div is set to the number of integer divisions and
10961 * *ineq is set to the number of inequality constraints (provided
10962 * div and ineq are not NULL).
10964 * The equality may also involve a single integer division involving
10965 * the output dimensions (typically only output dimension "pos") as
10966 * long as the coefficient of output dimension "pos" is 1 or -1 and
10967 * there is a pair of constraints i >= l and i <= l + n, with i referring
10968 * to output dimension "pos", l an expression involving only earlier
10969 * dimensions and n smaller than the coefficient of the integer division
10970 * in the equality. In this case, the output dimension can be defined
10971 * in terms of a modulo expression that does not involve the integer division.
10972 * *div is then set to this single integer division and
10973 * *ineq is set to the index of constraint i >= l.
10975 * Return bmap->n_eq if there is no such equality.
10976 * Return -1 on error.
10978 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10979 int pos, int *div, int *ineq)
10981 int j, k, l;
10982 unsigned n_out, o_out;
10983 unsigned n_div, o_div;
10985 if (!bmap)
10986 return -1;
10988 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10989 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10990 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10991 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10993 if (ineq)
10994 *ineq = bmap->n_ineq;
10995 if (div)
10996 *div = n_div;
10997 for (j = 0; j < bmap->n_eq; ++j) {
10998 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10999 continue;
11000 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11001 n_out - (pos + 1)) != -1)
11002 continue;
11003 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11004 0, n_div);
11005 if (k >= n_div)
11006 return j;
11007 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11008 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11009 continue;
11010 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11011 k + 1, n_div - (k+1)) < n_div)
11012 continue;
11013 l = find_modulo_constraint_pair(bmap, pos,
11014 bmap->eq[j][o_div + k]);
11015 if (l < 0)
11016 return -1;
11017 if (l >= bmap->n_ineq)
11018 continue;
11019 if (div)
11020 *div = k;
11021 if (ineq)
11022 *ineq = l;
11023 return j;
11026 return bmap->n_eq;
11029 /* Check if the given basic map is obviously single-valued.
11030 * In particular, for each output dimension, check that there is
11031 * an equality that defines the output dimension in terms of
11032 * earlier dimensions.
11034 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11036 int i;
11037 unsigned n_out;
11039 if (!bmap)
11040 return isl_bool_error;
11042 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11044 for (i = 0; i < n_out; ++i) {
11045 int eq;
11047 eq = isl_basic_map_output_defining_equality(bmap, i,
11048 NULL, NULL);
11049 if (eq < 0)
11050 return isl_bool_error;
11051 if (eq >= bmap->n_eq)
11052 return isl_bool_false;
11055 return isl_bool_true;
11058 /* Check if the given basic map is single-valued.
11059 * We simply compute
11061 * M \circ M^-1
11063 * and check if the result is a subset of the identity mapping.
11065 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11067 isl_space *space;
11068 isl_basic_map *test;
11069 isl_basic_map *id;
11070 isl_bool sv;
11072 sv = isl_basic_map_plain_is_single_valued(bmap);
11073 if (sv < 0 || sv)
11074 return sv;
11076 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11077 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11079 space = isl_basic_map_get_space(bmap);
11080 space = isl_space_map_from_set(isl_space_range(space));
11081 id = isl_basic_map_identity(space);
11083 sv = isl_basic_map_is_subset(test, id);
11085 isl_basic_map_free(test);
11086 isl_basic_map_free(id);
11088 return sv;
11091 /* Check if the given map is obviously single-valued.
11093 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11095 if (!map)
11096 return isl_bool_error;
11097 if (map->n == 0)
11098 return isl_bool_true;
11099 if (map->n >= 2)
11100 return isl_bool_false;
11102 return isl_basic_map_plain_is_single_valued(map->p[0]);
11105 /* Check if the given map is single-valued.
11106 * We simply compute
11108 * M \circ M^-1
11110 * and check if the result is a subset of the identity mapping.
11112 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11114 isl_space *dim;
11115 isl_map *test;
11116 isl_map *id;
11117 isl_bool sv;
11119 sv = isl_map_plain_is_single_valued(map);
11120 if (sv < 0 || sv)
11121 return sv;
11123 test = isl_map_reverse(isl_map_copy(map));
11124 test = isl_map_apply_range(test, isl_map_copy(map));
11126 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11127 id = isl_map_identity(dim);
11129 sv = isl_map_is_subset(test, id);
11131 isl_map_free(test);
11132 isl_map_free(id);
11134 return sv;
11137 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11139 isl_bool in;
11141 map = isl_map_copy(map);
11142 map = isl_map_reverse(map);
11143 in = isl_map_is_single_valued(map);
11144 isl_map_free(map);
11146 return in;
11149 /* Check if the given map is obviously injective.
11151 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11153 isl_bool in;
11155 map = isl_map_copy(map);
11156 map = isl_map_reverse(map);
11157 in = isl_map_plain_is_single_valued(map);
11158 isl_map_free(map);
11160 return in;
11163 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11165 isl_bool sv;
11167 sv = isl_map_is_single_valued(map);
11168 if (sv < 0 || !sv)
11169 return sv;
11171 return isl_map_is_injective(map);
11174 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11176 return isl_map_is_single_valued(set_to_map(set));
11179 /* Does "map" only map elements to themselves?
11181 * If the domain and range spaces are different, then "map"
11182 * is considered not to be an identity relation, even if it is empty.
11183 * Otherwise, construct the maximal identity relation and
11184 * check whether "map" is a subset of this relation.
11186 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11188 isl_space *space;
11189 isl_map *id;
11190 isl_bool equal, is_identity;
11192 space = isl_map_get_space(map);
11193 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11194 isl_space_free(space);
11195 if (equal < 0 || !equal)
11196 return equal;
11198 id = isl_map_identity(isl_map_get_space(map));
11199 is_identity = isl_map_is_subset(map, id);
11200 isl_map_free(id);
11202 return is_identity;
11205 int isl_map_is_translation(__isl_keep isl_map *map)
11207 int ok;
11208 isl_set *delta;
11210 delta = isl_map_deltas(isl_map_copy(map));
11211 ok = isl_set_is_singleton(delta);
11212 isl_set_free(delta);
11214 return ok;
11217 static int unique(isl_int *p, unsigned pos, unsigned len)
11219 if (isl_seq_first_non_zero(p, pos) != -1)
11220 return 0;
11221 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11222 return 0;
11223 return 1;
11226 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11228 int i, j;
11229 unsigned nvar;
11230 unsigned ovar;
11232 if (!bset)
11233 return isl_bool_error;
11235 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11236 return isl_bool_false;
11238 nvar = isl_basic_set_dim(bset, isl_dim_set);
11239 ovar = isl_space_offset(bset->dim, isl_dim_set);
11240 for (j = 0; j < nvar; ++j) {
11241 int lower = 0, upper = 0;
11242 for (i = 0; i < bset->n_eq; ++i) {
11243 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11244 continue;
11245 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11246 return isl_bool_false;
11247 break;
11249 if (i < bset->n_eq)
11250 continue;
11251 for (i = 0; i < bset->n_ineq; ++i) {
11252 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11253 continue;
11254 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11255 return isl_bool_false;
11256 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11257 lower = 1;
11258 else
11259 upper = 1;
11261 if (!lower || !upper)
11262 return isl_bool_false;
11265 return isl_bool_true;
11268 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11270 if (!set)
11271 return isl_bool_error;
11272 if (set->n != 1)
11273 return isl_bool_false;
11275 return isl_basic_set_is_box(set->p[0]);
11278 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11280 if (!bset)
11281 return isl_bool_error;
11283 return isl_space_is_wrapping(bset->dim);
11286 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11288 if (!set)
11289 return isl_bool_error;
11291 return isl_space_is_wrapping(set->dim);
11294 /* Modify the space of "map" through a call to "change".
11295 * If "can_change" is set (not NULL), then first call it to check
11296 * if the modification is allowed, printing the error message "cannot_change"
11297 * if it is not.
11299 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11300 isl_bool (*can_change)(__isl_keep isl_map *map),
11301 const char *cannot_change,
11302 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11304 isl_bool ok;
11305 isl_space *space;
11307 if (!map)
11308 return NULL;
11310 ok = can_change ? can_change(map) : isl_bool_true;
11311 if (ok < 0)
11312 return isl_map_free(map);
11313 if (!ok)
11314 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11315 return isl_map_free(map));
11317 space = change(isl_map_get_space(map));
11318 map = isl_map_reset_space(map, space);
11320 return map;
11323 /* Is the domain of "map" a wrapped relation?
11325 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11327 if (!map)
11328 return isl_bool_error;
11330 return isl_space_domain_is_wrapping(map->dim);
11333 /* Does "map" have a wrapped relation in both domain and range?
11335 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11337 return isl_space_is_product(isl_map_peek_space(map));
11340 /* Is the range of "map" a wrapped relation?
11342 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11344 if (!map)
11345 return isl_bool_error;
11347 return isl_space_range_is_wrapping(map->dim);
11350 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11352 bmap = isl_basic_map_cow(bmap);
11353 if (!bmap)
11354 return NULL;
11356 bmap->dim = isl_space_wrap(bmap->dim);
11357 if (!bmap->dim)
11358 goto error;
11360 bmap = isl_basic_map_finalize(bmap);
11362 return bset_from_bmap(bmap);
11363 error:
11364 isl_basic_map_free(bmap);
11365 return NULL;
11368 /* Given a map A -> B, return the set (A -> B).
11370 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11372 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11375 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11377 bset = isl_basic_set_cow(bset);
11378 if (!bset)
11379 return NULL;
11381 bset->dim = isl_space_unwrap(bset->dim);
11382 if (!bset->dim)
11383 goto error;
11385 bset = isl_basic_set_finalize(bset);
11387 return bset_to_bmap(bset);
11388 error:
11389 isl_basic_set_free(bset);
11390 return NULL;
11393 /* Given a set (A -> B), return the map A -> B.
11394 * Error out if "set" is not of the form (A -> B).
11396 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11398 return isl_map_change_space(set, &isl_set_is_wrapping,
11399 "not a wrapping set", &isl_space_unwrap);
11402 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11403 enum isl_dim_type type)
11405 if (!bmap)
11406 return NULL;
11408 if (!isl_space_is_named_or_nested(bmap->dim, type))
11409 return bmap;
11411 bmap = isl_basic_map_cow(bmap);
11412 if (!bmap)
11413 return NULL;
11415 bmap->dim = isl_space_reset(bmap->dim, type);
11416 if (!bmap->dim)
11417 goto error;
11419 bmap = isl_basic_map_finalize(bmap);
11421 return bmap;
11422 error:
11423 isl_basic_map_free(bmap);
11424 return NULL;
11427 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11428 enum isl_dim_type type)
11430 int i;
11432 if (!map)
11433 return NULL;
11435 if (!isl_space_is_named_or_nested(map->dim, type))
11436 return map;
11438 map = isl_map_cow(map);
11439 if (!map)
11440 return NULL;
11442 for (i = 0; i < map->n; ++i) {
11443 map->p[i] = isl_basic_map_reset(map->p[i], type);
11444 if (!map->p[i])
11445 goto error;
11447 map->dim = isl_space_reset(map->dim, type);
11448 if (!map->dim)
11449 goto error;
11451 return map;
11452 error:
11453 isl_map_free(map);
11454 return NULL;
11457 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11459 if (!bmap)
11460 return NULL;
11462 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11463 return bmap;
11465 bmap = isl_basic_map_cow(bmap);
11466 if (!bmap)
11467 return NULL;
11469 bmap->dim = isl_space_flatten(bmap->dim);
11470 if (!bmap->dim)
11471 goto error;
11473 bmap = isl_basic_map_finalize(bmap);
11475 return bmap;
11476 error:
11477 isl_basic_map_free(bmap);
11478 return NULL;
11481 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11483 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11486 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11487 __isl_take isl_basic_map *bmap)
11489 if (!bmap)
11490 return NULL;
11492 if (!bmap->dim->nested[0])
11493 return bmap;
11495 bmap = isl_basic_map_cow(bmap);
11496 if (!bmap)
11497 return NULL;
11499 bmap->dim = isl_space_flatten_domain(bmap->dim);
11500 if (!bmap->dim)
11501 goto error;
11503 bmap = isl_basic_map_finalize(bmap);
11505 return bmap;
11506 error:
11507 isl_basic_map_free(bmap);
11508 return NULL;
11511 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11512 __isl_take isl_basic_map *bmap)
11514 if (!bmap)
11515 return NULL;
11517 if (!bmap->dim->nested[1])
11518 return bmap;
11520 bmap = isl_basic_map_cow(bmap);
11521 if (!bmap)
11522 return NULL;
11524 bmap->dim = isl_space_flatten_range(bmap->dim);
11525 if (!bmap->dim)
11526 goto error;
11528 bmap = isl_basic_map_finalize(bmap);
11530 return bmap;
11531 error:
11532 isl_basic_map_free(bmap);
11533 return NULL;
11536 /* Remove any internal structure from the spaces of domain and range of "map".
11538 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11540 if (!map)
11541 return NULL;
11543 if (!map->dim->nested[0] && !map->dim->nested[1])
11544 return map;
11546 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11549 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11551 return set_from_map(isl_map_flatten(set_to_map(set)));
11554 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11556 isl_space *space, *flat_space;
11557 isl_map *map;
11559 space = isl_set_get_space(set);
11560 flat_space = isl_space_flatten(isl_space_copy(space));
11561 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11562 flat_space));
11563 map = isl_map_intersect_domain(map, set);
11565 return map;
11568 /* Remove any internal structure from the space of the domain of "map".
11570 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11572 if (!map)
11573 return NULL;
11575 if (!map->dim->nested[0])
11576 return map;
11578 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11581 /* Remove any internal structure from the space of the range of "map".
11583 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11585 if (!map)
11586 return NULL;
11588 if (!map->dim->nested[1])
11589 return map;
11591 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11594 /* Reorder the dimensions of "bmap" according to the given dim_map
11595 * and set the dimension specification to "space" and
11596 * perform Gaussian elimination on the result.
11598 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11599 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11601 isl_basic_map *res;
11602 unsigned flags;
11603 unsigned n_div;
11605 if (!bmap || !space || !dim_map)
11606 goto error;
11608 flags = bmap->flags;
11609 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11610 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11611 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11612 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11613 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11614 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11615 if (res)
11616 res->flags = flags;
11617 res = isl_basic_map_gauss(res, NULL);
11618 res = isl_basic_map_finalize(res);
11619 return res;
11620 error:
11621 isl_dim_map_free(dim_map);
11622 isl_basic_map_free(bmap);
11623 isl_space_free(space);
11624 return NULL;
11627 /* Reorder the dimensions of "map" according to given reordering.
11629 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11630 __isl_take isl_reordering *r)
11632 int i;
11633 struct isl_dim_map *dim_map;
11635 map = isl_map_cow(map);
11636 dim_map = isl_dim_map_from_reordering(r);
11637 if (!map || !r || !dim_map)
11638 goto error;
11640 for (i = 0; i < map->n; ++i) {
11641 struct isl_dim_map *dim_map_i;
11642 isl_space *space;
11644 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11646 space = isl_reordering_get_space(r);
11647 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11649 if (!map->p[i])
11650 goto error;
11653 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11654 map = isl_map_unmark_normalized(map);
11656 isl_reordering_free(r);
11657 isl_dim_map_free(dim_map);
11658 return map;
11659 error:
11660 isl_dim_map_free(dim_map);
11661 isl_map_free(map);
11662 isl_reordering_free(r);
11663 return NULL;
11666 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11667 __isl_take isl_reordering *r)
11669 return set_from_map(isl_map_realign(set_to_map(set), r));
11672 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11673 __isl_take isl_space *model)
11675 isl_ctx *ctx;
11676 isl_bool aligned;
11678 if (!map || !model)
11679 goto error;
11681 ctx = isl_space_get_ctx(model);
11682 if (!isl_space_has_named_params(model))
11683 isl_die(ctx, isl_error_invalid,
11684 "model has unnamed parameters", goto error);
11685 if (isl_map_check_named_params(map) < 0)
11686 goto error;
11687 aligned = isl_map_space_has_equal_params(map, model);
11688 if (aligned < 0)
11689 goto error;
11690 if (!aligned) {
11691 isl_reordering *exp;
11693 exp = isl_parameter_alignment_reordering(map->dim, model);
11694 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11695 map = isl_map_realign(map, exp);
11698 isl_space_free(model);
11699 return map;
11700 error:
11701 isl_space_free(model);
11702 isl_map_free(map);
11703 return NULL;
11706 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11707 __isl_take isl_space *model)
11709 return isl_map_align_params(set, model);
11712 /* Align the parameters of "bmap" to those of "model", introducing
11713 * additional parameters if needed.
11715 __isl_give isl_basic_map *isl_basic_map_align_params(
11716 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11718 isl_ctx *ctx;
11719 isl_bool equal_params;
11721 if (!bmap || !model)
11722 goto error;
11724 ctx = isl_space_get_ctx(model);
11725 if (!isl_space_has_named_params(model))
11726 isl_die(ctx, isl_error_invalid,
11727 "model has unnamed parameters", goto error);
11728 if (isl_basic_map_check_named_params(bmap) < 0)
11729 goto error;
11730 equal_params = isl_space_has_equal_params(bmap->dim, model);
11731 if (equal_params < 0)
11732 goto error;
11733 if (!equal_params) {
11734 isl_reordering *exp;
11735 struct isl_dim_map *dim_map;
11737 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11738 exp = isl_reordering_extend_space(exp,
11739 isl_basic_map_get_space(bmap));
11740 dim_map = isl_dim_map_from_reordering(exp);
11741 bmap = isl_basic_map_realign(bmap,
11742 isl_reordering_get_space(exp),
11743 isl_dim_map_extend(dim_map, bmap));
11744 isl_reordering_free(exp);
11745 isl_dim_map_free(dim_map);
11748 isl_space_free(model);
11749 return bmap;
11750 error:
11751 isl_space_free(model);
11752 isl_basic_map_free(bmap);
11753 return NULL;
11756 /* Do "bset" and "space" have the same parameters?
11758 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11759 __isl_keep isl_space *space)
11761 isl_space *bset_space;
11763 bset_space = isl_basic_set_peek_space(bset);
11764 return isl_space_has_equal_params(bset_space, space);
11767 /* Do "map" and "space" have the same parameters?
11769 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11770 __isl_keep isl_space *space)
11772 isl_space *map_space;
11774 map_space = isl_map_peek_space(map);
11775 return isl_space_has_equal_params(map_space, space);
11778 /* Do "set" and "space" have the same parameters?
11780 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11781 __isl_keep isl_space *space)
11783 return isl_map_space_has_equal_params(set_to_map(set), space);
11786 /* Align the parameters of "bset" to those of "model", introducing
11787 * additional parameters if needed.
11789 __isl_give isl_basic_set *isl_basic_set_align_params(
11790 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11792 return isl_basic_map_align_params(bset, model);
11795 /* Drop all parameters not referenced by "map".
11797 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11799 int i;
11801 if (isl_map_check_named_params(map) < 0)
11802 return isl_map_free(map);
11804 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11805 isl_bool involves;
11807 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11808 if (involves < 0)
11809 return isl_map_free(map);
11810 if (!involves)
11811 map = isl_map_project_out(map, isl_dim_param, i, 1);
11814 return map;
11817 /* Drop all parameters not referenced by "set".
11819 __isl_give isl_set *isl_set_drop_unused_params(
11820 __isl_take isl_set *set)
11822 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11825 /* Drop all parameters not referenced by "bmap".
11827 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11828 __isl_take isl_basic_map *bmap)
11830 int i;
11832 if (isl_basic_map_check_named_params(bmap) < 0)
11833 return isl_basic_map_free(bmap);
11835 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11836 isl_bool involves;
11838 involves = isl_basic_map_involves_dims(bmap,
11839 isl_dim_param, i, 1);
11840 if (involves < 0)
11841 return isl_basic_map_free(bmap);
11842 if (!involves)
11843 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11846 return bmap;
11849 /* Drop all parameters not referenced by "bset".
11851 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11852 __isl_take isl_basic_set *bset)
11854 return bset_from_bmap(isl_basic_map_drop_unused_params(
11855 bset_to_bmap(bset)));
11858 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11859 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11860 enum isl_dim_type c2, enum isl_dim_type c3,
11861 enum isl_dim_type c4, enum isl_dim_type c5)
11863 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11864 struct isl_mat *mat;
11865 int i, j, k;
11866 int pos;
11868 if (!bmap)
11869 return NULL;
11870 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11871 isl_basic_map_total_dim(bmap) + 1);
11872 if (!mat)
11873 return NULL;
11874 for (i = 0; i < bmap->n_eq; ++i)
11875 for (j = 0, pos = 0; j < 5; ++j) {
11876 int off = isl_basic_map_offset(bmap, c[j]);
11877 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11878 isl_int_set(mat->row[i][pos],
11879 bmap->eq[i][off + k]);
11880 ++pos;
11884 return mat;
11887 __isl_give isl_mat *isl_basic_map_inequalities_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_ineq,
11900 isl_basic_map_total_dim(bmap) + 1);
11901 if (!mat)
11902 return NULL;
11903 for (i = 0; i < bmap->n_ineq; ++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->ineq[i][off + k]);
11909 ++pos;
11913 return mat;
11916 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11917 __isl_take isl_space *space,
11918 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11919 enum isl_dim_type c2, enum isl_dim_type c3,
11920 enum isl_dim_type c4, enum isl_dim_type c5)
11922 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11923 isl_basic_map *bmap = NULL;
11924 unsigned total;
11925 unsigned extra;
11926 int i, j, k, l;
11927 int pos;
11929 if (!space || !eq || !ineq)
11930 goto error;
11932 if (eq->n_col != ineq->n_col)
11933 isl_die(space->ctx, isl_error_invalid,
11934 "equalities and inequalities matrices should have "
11935 "same number of columns", goto error);
11937 total = 1 + isl_space_dim(space, isl_dim_all);
11939 if (eq->n_col < total)
11940 isl_die(space->ctx, isl_error_invalid,
11941 "number of columns too small", goto error);
11943 extra = eq->n_col - total;
11945 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11946 eq->n_row, ineq->n_row);
11947 if (!bmap)
11948 goto error;
11949 for (i = 0; i < extra; ++i) {
11950 k = isl_basic_map_alloc_div(bmap);
11951 if (k < 0)
11952 goto error;
11953 isl_int_set_si(bmap->div[k][0], 0);
11955 for (i = 0; i < eq->n_row; ++i) {
11956 l = isl_basic_map_alloc_equality(bmap);
11957 if (l < 0)
11958 goto error;
11959 for (j = 0, pos = 0; j < 5; ++j) {
11960 int off = isl_basic_map_offset(bmap, c[j]);
11961 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11962 isl_int_set(bmap->eq[l][off + k],
11963 eq->row[i][pos]);
11964 ++pos;
11968 for (i = 0; i < ineq->n_row; ++i) {
11969 l = isl_basic_map_alloc_inequality(bmap);
11970 if (l < 0)
11971 goto error;
11972 for (j = 0, pos = 0; j < 5; ++j) {
11973 int off = isl_basic_map_offset(bmap, c[j]);
11974 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11975 isl_int_set(bmap->ineq[l][off + k],
11976 ineq->row[i][pos]);
11977 ++pos;
11982 isl_space_free(space);
11983 isl_mat_free(eq);
11984 isl_mat_free(ineq);
11986 bmap = isl_basic_map_simplify(bmap);
11987 return isl_basic_map_finalize(bmap);
11988 error:
11989 isl_space_free(space);
11990 isl_mat_free(eq);
11991 isl_mat_free(ineq);
11992 isl_basic_map_free(bmap);
11993 return NULL;
11996 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11997 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11998 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12000 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12001 c1, c2, c3, c4, isl_dim_in);
12004 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12005 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12006 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12008 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12009 c1, c2, c3, c4, isl_dim_in);
12012 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12013 __isl_take isl_space *dim,
12014 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12015 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12017 isl_basic_map *bmap;
12018 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12019 c1, c2, c3, c4, isl_dim_in);
12020 return bset_from_bmap(bmap);
12023 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12025 if (!bmap)
12026 return isl_bool_error;
12028 return isl_space_can_zip(bmap->dim);
12031 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12033 if (!map)
12034 return isl_bool_error;
12036 return isl_space_can_zip(map->dim);
12039 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12040 * (A -> C) -> (B -> D).
12042 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12044 unsigned pos;
12045 unsigned n1;
12046 unsigned n2;
12048 if (!bmap)
12049 return NULL;
12051 if (!isl_basic_map_can_zip(bmap))
12052 isl_die(bmap->ctx, isl_error_invalid,
12053 "basic map cannot be zipped", goto error);
12054 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12055 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12056 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12057 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12058 bmap = isl_basic_map_cow(bmap);
12059 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12060 if (!bmap)
12061 return NULL;
12062 bmap->dim = isl_space_zip(bmap->dim);
12063 if (!bmap->dim)
12064 goto error;
12065 bmap = isl_basic_map_mark_final(bmap);
12066 return bmap;
12067 error:
12068 isl_basic_map_free(bmap);
12069 return NULL;
12072 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12073 * (A -> C) -> (B -> D).
12075 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12077 int i;
12079 if (!map)
12080 return NULL;
12082 if (!isl_map_can_zip(map))
12083 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12084 goto error);
12086 map = isl_map_cow(map);
12087 if (!map)
12088 return NULL;
12090 for (i = 0; i < map->n; ++i) {
12091 map->p[i] = isl_basic_map_zip(map->p[i]);
12092 if (!map->p[i])
12093 goto error;
12096 map->dim = isl_space_zip(map->dim);
12097 if (!map->dim)
12098 goto error;
12100 return map;
12101 error:
12102 isl_map_free(map);
12103 return NULL;
12106 /* Can we apply isl_basic_map_curry to "bmap"?
12107 * That is, does it have a nested relation in its domain?
12109 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12111 if (!bmap)
12112 return isl_bool_error;
12114 return isl_space_can_curry(bmap->dim);
12117 /* Can we apply isl_map_curry to "map"?
12118 * That is, does it have a nested relation in its domain?
12120 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12122 if (!map)
12123 return isl_bool_error;
12125 return isl_space_can_curry(map->dim);
12128 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12129 * A -> (B -> C).
12131 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12134 if (!bmap)
12135 return NULL;
12137 if (!isl_basic_map_can_curry(bmap))
12138 isl_die(bmap->ctx, isl_error_invalid,
12139 "basic map cannot be curried", goto error);
12140 bmap = isl_basic_map_cow(bmap);
12141 if (!bmap)
12142 return NULL;
12143 bmap->dim = isl_space_curry(bmap->dim);
12144 if (!bmap->dim)
12145 goto error;
12146 bmap = isl_basic_map_mark_final(bmap);
12147 return bmap;
12148 error:
12149 isl_basic_map_free(bmap);
12150 return NULL;
12153 /* Given a map (A -> B) -> C, return the corresponding map
12154 * A -> (B -> C).
12156 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12158 return isl_map_change_space(map, &isl_map_can_curry,
12159 "map cannot be curried", &isl_space_curry);
12162 /* Can isl_map_range_curry be applied to "map"?
12163 * That is, does it have a nested relation in its range,
12164 * the domain of which is itself a nested relation?
12166 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12168 if (!map)
12169 return isl_bool_error;
12171 return isl_space_can_range_curry(map->dim);
12174 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12175 * A -> (B -> (C -> D)).
12177 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12179 return isl_map_change_space(map, &isl_map_can_range_curry,
12180 "map range cannot be curried",
12181 &isl_space_range_curry);
12184 /* Can we apply isl_basic_map_uncurry to "bmap"?
12185 * That is, does it have a nested relation in its domain?
12187 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12189 if (!bmap)
12190 return isl_bool_error;
12192 return isl_space_can_uncurry(bmap->dim);
12195 /* Can we apply isl_map_uncurry to "map"?
12196 * That is, does it have a nested relation in its domain?
12198 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12200 if (!map)
12201 return isl_bool_error;
12203 return isl_space_can_uncurry(map->dim);
12206 /* Given a basic map A -> (B -> C), return the corresponding basic map
12207 * (A -> B) -> C.
12209 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12212 if (!bmap)
12213 return NULL;
12215 if (!isl_basic_map_can_uncurry(bmap))
12216 isl_die(bmap->ctx, isl_error_invalid,
12217 "basic map cannot be uncurried",
12218 return isl_basic_map_free(bmap));
12219 bmap = isl_basic_map_cow(bmap);
12220 if (!bmap)
12221 return NULL;
12222 bmap->dim = isl_space_uncurry(bmap->dim);
12223 if (!bmap->dim)
12224 return isl_basic_map_free(bmap);
12225 bmap = isl_basic_map_mark_final(bmap);
12226 return bmap;
12229 /* Given a map A -> (B -> C), return the corresponding map
12230 * (A -> B) -> C.
12232 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12234 return isl_map_change_space(map, &isl_map_can_uncurry,
12235 "map cannot be uncurried", &isl_space_uncurry);
12238 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12239 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12241 return isl_map_equate(set, type1, pos1, type2, pos2);
12244 /* Construct a basic map where the given dimensions are equal to each other.
12246 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12247 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12249 isl_basic_map *bmap = NULL;
12250 int i;
12252 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12253 isl_space_check_range(space, type2, pos2, 1) < 0)
12254 goto error;
12256 if (type1 == type2 && pos1 == pos2)
12257 return isl_basic_map_universe(space);
12259 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12260 i = isl_basic_map_alloc_equality(bmap);
12261 if (i < 0)
12262 goto error;
12263 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12264 pos1 += isl_basic_map_offset(bmap, type1);
12265 pos2 += isl_basic_map_offset(bmap, type2);
12266 isl_int_set_si(bmap->eq[i][pos1], -1);
12267 isl_int_set_si(bmap->eq[i][pos2], 1);
12268 bmap = isl_basic_map_finalize(bmap);
12269 isl_space_free(space);
12270 return bmap;
12271 error:
12272 isl_space_free(space);
12273 isl_basic_map_free(bmap);
12274 return NULL;
12277 /* Add a constraint imposing that the given two dimensions are equal.
12279 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12280 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12282 isl_basic_map *eq;
12284 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12286 bmap = isl_basic_map_intersect(bmap, eq);
12288 return bmap;
12291 /* Add a constraint imposing that the given two dimensions are equal.
12293 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12294 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12296 isl_basic_map *bmap;
12298 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12300 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12302 return map;
12305 /* Add a constraint imposing that the given two dimensions have opposite values.
12307 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12308 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12310 isl_basic_map *bmap = NULL;
12311 int i;
12313 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12314 return isl_map_free(map);
12315 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12316 return isl_map_free(map);
12318 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12319 i = isl_basic_map_alloc_equality(bmap);
12320 if (i < 0)
12321 goto error;
12322 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12323 pos1 += isl_basic_map_offset(bmap, type1);
12324 pos2 += isl_basic_map_offset(bmap, type2);
12325 isl_int_set_si(bmap->eq[i][pos1], 1);
12326 isl_int_set_si(bmap->eq[i][pos2], 1);
12327 bmap = isl_basic_map_finalize(bmap);
12329 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12331 return map;
12332 error:
12333 isl_basic_map_free(bmap);
12334 isl_map_free(map);
12335 return NULL;
12338 /* Construct a constraint imposing that the value of the first dimension is
12339 * greater than or equal to that of the second.
12341 static __isl_give isl_constraint *constraint_order_ge(
12342 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12343 enum isl_dim_type type2, int pos2)
12345 isl_constraint *c;
12347 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12348 isl_space_check_range(space, type2, pos2, 1) < 0)
12349 space = isl_space_free(space);
12350 if (!space)
12351 return NULL;
12353 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12355 if (type1 == type2 && pos1 == pos2)
12356 return c;
12358 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12359 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12361 return c;
12364 /* Add a constraint imposing that the value of the first dimension is
12365 * greater than or equal to that of the second.
12367 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12368 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12370 isl_constraint *c;
12371 isl_space *space;
12373 if (type1 == type2 && pos1 == pos2)
12374 return bmap;
12375 space = isl_basic_map_get_space(bmap);
12376 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12377 bmap = isl_basic_map_add_constraint(bmap, c);
12379 return bmap;
12382 /* Add a constraint imposing that the value of the first dimension is
12383 * greater than or equal to that of the second.
12385 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12386 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12388 isl_constraint *c;
12389 isl_space *space;
12391 if (type1 == type2 && pos1 == pos2)
12392 return map;
12393 space = isl_map_get_space(map);
12394 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12395 map = isl_map_add_constraint(map, c);
12397 return map;
12400 /* Add a constraint imposing that the value of the first dimension is
12401 * less than or equal to that of the second.
12403 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12404 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12406 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12409 /* Construct a basic map where the value of the first dimension is
12410 * greater than that of the second.
12412 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12413 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12415 isl_basic_map *bmap = NULL;
12416 int i;
12418 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12419 isl_space_check_range(space, type2, pos2, 1) < 0)
12420 goto error;
12422 if (type1 == type2 && pos1 == pos2)
12423 return isl_basic_map_empty(space);
12425 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12426 i = isl_basic_map_alloc_inequality(bmap);
12427 if (i < 0)
12428 return isl_basic_map_free(bmap);
12429 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12430 pos1 += isl_basic_map_offset(bmap, type1);
12431 pos2 += isl_basic_map_offset(bmap, type2);
12432 isl_int_set_si(bmap->ineq[i][pos1], 1);
12433 isl_int_set_si(bmap->ineq[i][pos2], -1);
12434 isl_int_set_si(bmap->ineq[i][0], -1);
12435 bmap = isl_basic_map_finalize(bmap);
12437 return bmap;
12438 error:
12439 isl_space_free(space);
12440 isl_basic_map_free(bmap);
12441 return NULL;
12444 /* Add a constraint imposing that the value of the first dimension is
12445 * greater than that of the second.
12447 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12448 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12450 isl_basic_map *gt;
12452 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12454 bmap = isl_basic_map_intersect(bmap, gt);
12456 return bmap;
12459 /* Add a constraint imposing that the value of the first dimension is
12460 * greater than that of the second.
12462 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12463 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12465 isl_basic_map *bmap;
12467 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12469 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12471 return map;
12474 /* Add a constraint imposing that the value of the first dimension is
12475 * smaller than that of the second.
12477 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12478 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12480 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12483 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12484 int pos)
12486 isl_aff *div;
12487 isl_local_space *ls;
12489 if (!bmap)
12490 return NULL;
12492 if (!isl_basic_map_divs_known(bmap))
12493 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12494 "some divs are unknown", return NULL);
12496 ls = isl_basic_map_get_local_space(bmap);
12497 div = isl_local_space_get_div(ls, pos);
12498 isl_local_space_free(ls);
12500 return div;
12503 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12504 int pos)
12506 return isl_basic_map_get_div(bset, pos);
12509 /* Plug in "subs" for dimension "type", "pos" of "bset".
12511 * Let i be the dimension to replace and let "subs" be of the form
12513 * f/d
12515 * Any integer division with a non-zero coefficient for i,
12517 * floor((a i + g)/m)
12519 * is replaced by
12521 * floor((a f + d g)/(m d))
12523 * Constraints of the form
12525 * a i + g
12527 * are replaced by
12529 * a f + d g
12531 * We currently require that "subs" is an integral expression.
12532 * Handling rational expressions may require us to add stride constraints
12533 * as we do in isl_basic_set_preimage_multi_aff.
12535 __isl_give isl_basic_set *isl_basic_set_substitute(
12536 __isl_take isl_basic_set *bset,
12537 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12539 int i;
12540 isl_int v;
12541 isl_ctx *ctx;
12543 if (bset && isl_basic_set_plain_is_empty(bset))
12544 return bset;
12546 bset = isl_basic_set_cow(bset);
12547 if (!bset || !subs)
12548 goto error;
12550 ctx = isl_basic_set_get_ctx(bset);
12551 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12552 isl_die(ctx, isl_error_invalid,
12553 "spaces don't match", goto error);
12554 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12555 isl_die(ctx, isl_error_unsupported,
12556 "cannot handle divs yet", goto error);
12557 if (!isl_int_is_one(subs->v->el[0]))
12558 isl_die(ctx, isl_error_invalid,
12559 "can only substitute integer expressions", goto error);
12561 pos += isl_basic_set_offset(bset, type);
12563 isl_int_init(v);
12565 for (i = 0; i < bset->n_eq; ++i) {
12566 if (isl_int_is_zero(bset->eq[i][pos]))
12567 continue;
12568 isl_int_set(v, bset->eq[i][pos]);
12569 isl_int_set_si(bset->eq[i][pos], 0);
12570 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12571 v, subs->v->el + 1, subs->v->size - 1);
12574 for (i = 0; i < bset->n_ineq; ++i) {
12575 if (isl_int_is_zero(bset->ineq[i][pos]))
12576 continue;
12577 isl_int_set(v, bset->ineq[i][pos]);
12578 isl_int_set_si(bset->ineq[i][pos], 0);
12579 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12580 v, subs->v->el + 1, subs->v->size - 1);
12583 for (i = 0; i < bset->n_div; ++i) {
12584 if (isl_int_is_zero(bset->div[i][1 + pos]))
12585 continue;
12586 isl_int_set(v, bset->div[i][1 + pos]);
12587 isl_int_set_si(bset->div[i][1 + pos], 0);
12588 isl_seq_combine(bset->div[i] + 1,
12589 subs->v->el[0], bset->div[i] + 1,
12590 v, subs->v->el + 1, subs->v->size - 1);
12591 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12594 isl_int_clear(v);
12596 bset = isl_basic_set_simplify(bset);
12597 return isl_basic_set_finalize(bset);
12598 error:
12599 isl_basic_set_free(bset);
12600 return NULL;
12603 /* Plug in "subs" for dimension "type", "pos" of "set".
12605 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12606 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12608 int i;
12610 if (set && isl_set_plain_is_empty(set))
12611 return set;
12613 set = isl_set_cow(set);
12614 if (!set || !subs)
12615 goto error;
12617 for (i = set->n - 1; i >= 0; --i) {
12618 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12619 set = set_from_map(remove_if_empty(set_to_map(set), i));
12620 if (!set)
12621 return NULL;
12624 return set;
12625 error:
12626 isl_set_free(set);
12627 return NULL;
12630 /* Check if the range of "ma" is compatible with the domain or range
12631 * (depending on "type") of "bmap".
12633 static isl_stat check_basic_map_compatible_range_multi_aff(
12634 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12635 __isl_keep isl_multi_aff *ma)
12637 isl_bool m;
12638 isl_space *ma_space;
12640 ma_space = isl_multi_aff_get_space(ma);
12642 m = isl_space_has_equal_params(bmap->dim, ma_space);
12643 if (m < 0)
12644 goto error;
12645 if (!m)
12646 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12647 "parameters don't match", goto error);
12648 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12649 if (m < 0)
12650 goto error;
12651 if (!m)
12652 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12653 "spaces don't match", goto error);
12655 isl_space_free(ma_space);
12656 return isl_stat_ok;
12657 error:
12658 isl_space_free(ma_space);
12659 return isl_stat_error;
12662 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12663 * coefficients before the transformed range of dimensions,
12664 * the "n_after" coefficients after the transformed range of dimensions
12665 * and the coefficients of the other divs in "bmap".
12667 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12668 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12670 int i;
12671 int n_param;
12672 int n_set;
12673 isl_local_space *ls;
12675 if (n_div == 0)
12676 return 0;
12678 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12679 if (!ls)
12680 return -1;
12682 n_param = isl_local_space_dim(ls, isl_dim_param);
12683 n_set = isl_local_space_dim(ls, isl_dim_set);
12684 for (i = 0; i < n_div; ++i) {
12685 int o_bmap = 0, o_ls = 0;
12687 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12688 o_bmap += 1 + 1 + n_param;
12689 o_ls += 1 + 1 + n_param;
12690 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12691 o_bmap += n_before;
12692 isl_seq_cpy(bmap->div[i] + o_bmap,
12693 ls->div->row[i] + o_ls, n_set);
12694 o_bmap += n_set;
12695 o_ls += n_set;
12696 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12697 o_bmap += n_after;
12698 isl_seq_cpy(bmap->div[i] + o_bmap,
12699 ls->div->row[i] + o_ls, n_div);
12700 o_bmap += n_div;
12701 o_ls += n_div;
12702 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12703 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12704 goto error;
12707 isl_local_space_free(ls);
12708 return 0;
12709 error:
12710 isl_local_space_free(ls);
12711 return -1;
12714 /* How many stride constraints does "ma" enforce?
12715 * That is, how many of the affine expressions have a denominator
12716 * different from one?
12718 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12720 int i;
12721 int strides = 0;
12723 for (i = 0; i < ma->n; ++i)
12724 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12725 strides++;
12727 return strides;
12730 /* For each affine expression in ma of the form
12732 * x_i = (f_i y + h_i)/m_i
12734 * with m_i different from one, add a constraint to "bmap"
12735 * of the form
12737 * f_i y + h_i = m_i alpha_i
12739 * with alpha_i an additional existentially quantified variable.
12741 * The input variables of "ma" correspond to a subset of the variables
12742 * of "bmap". There are "n_before" variables in "bmap" before this
12743 * subset and "n_after" variables after this subset.
12744 * The integer divisions of the affine expressions in "ma" are assumed
12745 * to have been aligned. There are "n_div_ma" of them and
12746 * they appear first in "bmap", straight after the "n_after" variables.
12748 static __isl_give isl_basic_map *add_ma_strides(
12749 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12750 int n_before, int n_after, int n_div_ma)
12752 int i, k;
12753 int div;
12754 int total;
12755 int n_param;
12756 int n_in;
12758 total = isl_basic_map_total_dim(bmap);
12759 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12760 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12761 for (i = 0; i < ma->n; ++i) {
12762 int o_bmap = 0, o_ma = 1;
12764 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12765 continue;
12766 div = isl_basic_map_alloc_div(bmap);
12767 k = isl_basic_map_alloc_equality(bmap);
12768 if (div < 0 || k < 0)
12769 goto error;
12770 isl_int_set_si(bmap->div[div][0], 0);
12771 isl_seq_cpy(bmap->eq[k] + o_bmap,
12772 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12773 o_bmap += 1 + n_param;
12774 o_ma += 1 + n_param;
12775 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12776 o_bmap += n_before;
12777 isl_seq_cpy(bmap->eq[k] + o_bmap,
12778 ma->u.p[i]->v->el + o_ma, n_in);
12779 o_bmap += n_in;
12780 o_ma += n_in;
12781 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12782 o_bmap += n_after;
12783 isl_seq_cpy(bmap->eq[k] + o_bmap,
12784 ma->u.p[i]->v->el + o_ma, n_div_ma);
12785 o_bmap += n_div_ma;
12786 o_ma += n_div_ma;
12787 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12788 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12789 total++;
12792 return bmap;
12793 error:
12794 isl_basic_map_free(bmap);
12795 return NULL;
12798 /* Replace the domain or range space (depending on "type) of "space" by "set".
12800 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12801 enum isl_dim_type type, __isl_take isl_space *set)
12803 if (type == isl_dim_in) {
12804 space = isl_space_range(space);
12805 space = isl_space_map_from_domain_and_range(set, space);
12806 } else {
12807 space = isl_space_domain(space);
12808 space = isl_space_map_from_domain_and_range(space, set);
12811 return space;
12814 /* Compute the preimage of the domain or range (depending on "type")
12815 * of "bmap" under the function represented by "ma".
12816 * In other words, plug in "ma" in the domain or range of "bmap".
12817 * The result is a basic map that lives in the same space as "bmap"
12818 * except that the domain or range has been replaced by
12819 * the domain space of "ma".
12821 * If bmap is represented by
12823 * A(p) + S u + B x + T v + C(divs) >= 0,
12825 * where u and x are input and output dimensions if type == isl_dim_out
12826 * while x and v are input and output dimensions if type == isl_dim_in,
12827 * and ma is represented by
12829 * x = D(p) + F(y) + G(divs')
12831 * then the result is
12833 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12835 * The divs in the input set are similarly adjusted.
12836 * In particular
12838 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12840 * becomes
12842 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12843 * B_i G(divs') + c_i(divs))/n_i)
12845 * If bmap is not a rational map and if F(y) involves any denominators
12847 * x_i = (f_i y + h_i)/m_i
12849 * then additional constraints are added to ensure that we only
12850 * map back integer points. That is we enforce
12852 * f_i y + h_i = m_i alpha_i
12854 * with alpha_i an additional existentially quantified variable.
12856 * We first copy over the divs from "ma".
12857 * Then we add the modified constraints and divs from "bmap".
12858 * Finally, we add the stride constraints, if needed.
12860 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12861 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12862 __isl_take isl_multi_aff *ma)
12864 int i, k;
12865 isl_space *space;
12866 isl_basic_map *res = NULL;
12867 int n_before, n_after, n_div_bmap, n_div_ma;
12868 isl_int f, c1, c2, g;
12869 isl_bool rational;
12870 int strides;
12872 isl_int_init(f);
12873 isl_int_init(c1);
12874 isl_int_init(c2);
12875 isl_int_init(g);
12877 ma = isl_multi_aff_align_divs(ma);
12878 if (!bmap || !ma)
12879 goto error;
12880 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12881 goto error;
12883 if (type == isl_dim_in) {
12884 n_before = 0;
12885 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12886 } else {
12887 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12888 n_after = 0;
12890 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12891 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12893 space = isl_multi_aff_get_domain_space(ma);
12894 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12895 rational = isl_basic_map_is_rational(bmap);
12896 strides = rational ? 0 : multi_aff_strides(ma);
12897 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12898 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12899 if (rational)
12900 res = isl_basic_map_set_rational(res);
12902 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12903 if (isl_basic_map_alloc_div(res) < 0)
12904 goto error;
12906 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12907 goto error;
12909 for (i = 0; i < bmap->n_eq; ++i) {
12910 k = isl_basic_map_alloc_equality(res);
12911 if (k < 0)
12912 goto error;
12913 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12914 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12917 for (i = 0; i < bmap->n_ineq; ++i) {
12918 k = isl_basic_map_alloc_inequality(res);
12919 if (k < 0)
12920 goto error;
12921 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12922 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12925 for (i = 0; i < bmap->n_div; ++i) {
12926 if (isl_int_is_zero(bmap->div[i][0])) {
12927 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12928 continue;
12930 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12931 n_before, n_after, n_div_ma, n_div_bmap,
12932 f, c1, c2, g, 1);
12935 if (strides)
12936 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12938 isl_int_clear(f);
12939 isl_int_clear(c1);
12940 isl_int_clear(c2);
12941 isl_int_clear(g);
12942 isl_basic_map_free(bmap);
12943 isl_multi_aff_free(ma);
12944 res = isl_basic_map_simplify(res);
12945 return isl_basic_map_finalize(res);
12946 error:
12947 isl_int_clear(f);
12948 isl_int_clear(c1);
12949 isl_int_clear(c2);
12950 isl_int_clear(g);
12951 isl_basic_map_free(bmap);
12952 isl_multi_aff_free(ma);
12953 isl_basic_map_free(res);
12954 return NULL;
12957 /* Compute the preimage of "bset" under the function represented by "ma".
12958 * In other words, plug in "ma" in "bset". The result is a basic set
12959 * that lives in the domain space of "ma".
12961 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12962 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12964 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12967 /* Compute the preimage of the domain of "bmap" under the function
12968 * represented by "ma".
12969 * In other words, plug in "ma" in the domain of "bmap".
12970 * The result is a basic map that lives in the same space as "bmap"
12971 * except that the domain has been replaced by the domain space of "ma".
12973 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12974 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12976 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12979 /* Compute the preimage of the range of "bmap" under the function
12980 * represented by "ma".
12981 * In other words, plug in "ma" in the range of "bmap".
12982 * The result is a basic map that lives in the same space as "bmap"
12983 * except that the range has been replaced by the domain space of "ma".
12985 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12986 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12988 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12991 /* Check if the range of "ma" is compatible with the domain or range
12992 * (depending on "type") of "map".
12993 * Return isl_stat_error if anything is wrong.
12995 static isl_stat check_map_compatible_range_multi_aff(
12996 __isl_keep isl_map *map, enum isl_dim_type type,
12997 __isl_keep isl_multi_aff *ma)
12999 isl_bool m;
13000 isl_space *ma_space;
13002 ma_space = isl_multi_aff_get_space(ma);
13003 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13004 isl_space_free(ma_space);
13005 if (m < 0)
13006 return isl_stat_error;
13007 if (!m)
13008 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13009 "spaces don't match", return isl_stat_error);
13010 return isl_stat_ok;
13013 /* Compute the preimage of the domain or range (depending on "type")
13014 * of "map" under the function represented by "ma".
13015 * In other words, plug in "ma" in the domain or range of "map".
13016 * The result is a map that lives in the same space as "map"
13017 * except that the domain or range has been replaced by
13018 * the domain space of "ma".
13020 * The parameters are assumed to have been aligned.
13022 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13023 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13025 int i;
13026 isl_space *space;
13028 map = isl_map_cow(map);
13029 ma = isl_multi_aff_align_divs(ma);
13030 if (!map || !ma)
13031 goto error;
13032 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13033 goto error;
13035 for (i = 0; i < map->n; ++i) {
13036 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13037 isl_multi_aff_copy(ma));
13038 if (!map->p[i])
13039 goto error;
13042 space = isl_multi_aff_get_domain_space(ma);
13043 space = isl_space_set(isl_map_get_space(map), type, space);
13045 isl_space_free(map->dim);
13046 map->dim = space;
13047 if (!map->dim)
13048 goto error;
13050 isl_multi_aff_free(ma);
13051 if (map->n > 1)
13052 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13053 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13054 return map;
13055 error:
13056 isl_multi_aff_free(ma);
13057 isl_map_free(map);
13058 return NULL;
13061 /* Compute the preimage of the domain or range (depending on "type")
13062 * of "map" under the function represented by "ma".
13063 * In other words, plug in "ma" in the domain or range of "map".
13064 * The result is a map that lives in the same space as "map"
13065 * except that the domain or range has been replaced by
13066 * the domain space of "ma".
13068 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13069 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13071 isl_bool aligned;
13073 if (!map || !ma)
13074 goto error;
13076 aligned = isl_map_space_has_equal_params(map, ma->space);
13077 if (aligned < 0)
13078 goto error;
13079 if (aligned)
13080 return map_preimage_multi_aff(map, type, ma);
13082 if (isl_map_check_named_params(map) < 0)
13083 goto error;
13084 if (!isl_space_has_named_params(ma->space))
13085 isl_die(map->ctx, isl_error_invalid,
13086 "unaligned unnamed parameters", goto error);
13087 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13088 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13090 return map_preimage_multi_aff(map, type, ma);
13091 error:
13092 isl_multi_aff_free(ma);
13093 return isl_map_free(map);
13096 /* Compute the preimage of "set" under the function represented by "ma".
13097 * In other words, plug in "ma" in "set". The result is a set
13098 * that lives in the domain space of "ma".
13100 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13101 __isl_take isl_multi_aff *ma)
13103 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13106 /* Compute the preimage of the domain of "map" under the function
13107 * represented by "ma".
13108 * In other words, plug in "ma" in the domain of "map".
13109 * The result is a map that lives in the same space as "map"
13110 * except that the domain has been replaced by the domain space of "ma".
13112 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13113 __isl_take isl_multi_aff *ma)
13115 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13118 /* Compute the preimage of the range of "map" under the function
13119 * represented by "ma".
13120 * In other words, plug in "ma" in the range of "map".
13121 * The result is a map that lives in the same space as "map"
13122 * except that the range has been replaced by the domain space of "ma".
13124 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13125 __isl_take isl_multi_aff *ma)
13127 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13130 /* Compute the preimage of "map" under the function represented by "pma".
13131 * In other words, plug in "pma" in the domain or range of "map".
13132 * The result is a map that lives in the same space as "map",
13133 * except that the space of type "type" has been replaced by
13134 * the domain space of "pma".
13136 * The parameters of "map" and "pma" are assumed to have been aligned.
13138 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13139 __isl_take isl_map *map, enum isl_dim_type type,
13140 __isl_take isl_pw_multi_aff *pma)
13142 int i;
13143 isl_map *res;
13145 if (!pma)
13146 goto error;
13148 if (pma->n == 0) {
13149 isl_pw_multi_aff_free(pma);
13150 res = isl_map_empty(isl_map_get_space(map));
13151 isl_map_free(map);
13152 return res;
13155 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13156 isl_multi_aff_copy(pma->p[0].maff));
13157 if (type == isl_dim_in)
13158 res = isl_map_intersect_domain(res,
13159 isl_map_copy(pma->p[0].set));
13160 else
13161 res = isl_map_intersect_range(res,
13162 isl_map_copy(pma->p[0].set));
13164 for (i = 1; i < pma->n; ++i) {
13165 isl_map *res_i;
13167 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13168 isl_multi_aff_copy(pma->p[i].maff));
13169 if (type == isl_dim_in)
13170 res_i = isl_map_intersect_domain(res_i,
13171 isl_map_copy(pma->p[i].set));
13172 else
13173 res_i = isl_map_intersect_range(res_i,
13174 isl_map_copy(pma->p[i].set));
13175 res = isl_map_union(res, res_i);
13178 isl_pw_multi_aff_free(pma);
13179 isl_map_free(map);
13180 return res;
13181 error:
13182 isl_pw_multi_aff_free(pma);
13183 isl_map_free(map);
13184 return NULL;
13187 /* Compute the preimage of "map" under the function represented by "pma".
13188 * In other words, plug in "pma" in the domain or range of "map".
13189 * The result is a map that lives in the same space as "map",
13190 * except that the space of type "type" has been replaced by
13191 * the domain space of "pma".
13193 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13194 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13196 isl_bool aligned;
13198 if (!map || !pma)
13199 goto error;
13201 aligned = isl_map_space_has_equal_params(map, pma->dim);
13202 if (aligned < 0)
13203 goto error;
13204 if (aligned)
13205 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13207 if (isl_map_check_named_params(map) < 0)
13208 goto error;
13209 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13210 goto error;
13211 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13212 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13214 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13215 error:
13216 isl_pw_multi_aff_free(pma);
13217 return isl_map_free(map);
13220 /* Compute the preimage of "set" under the function represented by "pma".
13221 * In other words, plug in "pma" in "set". The result is a set
13222 * that lives in the domain space of "pma".
13224 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13225 __isl_take isl_pw_multi_aff *pma)
13227 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13230 /* Compute the preimage of the domain of "map" under the function
13231 * represented by "pma".
13232 * In other words, plug in "pma" in the domain of "map".
13233 * The result is a map that lives in the same space as "map",
13234 * except that domain space has been replaced by the domain space of "pma".
13236 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13237 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13239 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13242 /* Compute the preimage of the range of "map" under the function
13243 * represented by "pma".
13244 * In other words, plug in "pma" in the range of "map".
13245 * The result is a map that lives in the same space as "map",
13246 * except that range space has been replaced by the domain space of "pma".
13248 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13249 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13251 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13254 /* Compute the preimage of "map" under the function represented by "mpa".
13255 * In other words, plug in "mpa" in the domain or range of "map".
13256 * The result is a map that lives in the same space as "map",
13257 * except that the space of type "type" has been replaced by
13258 * the domain space of "mpa".
13260 * If the map does not involve any constraints that refer to the
13261 * dimensions of the substituted space, then the only possible
13262 * effect of "mpa" on the map is to map the space to a different space.
13263 * We create a separate isl_multi_aff to effectuate this change
13264 * in order to avoid spurious splitting of the map along the pieces
13265 * of "mpa".
13266 * If "mpa" has a non-trivial explicit domain, however,
13267 * then the full substitution should be performed.
13269 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13270 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13272 int n;
13273 isl_bool full;
13274 isl_pw_multi_aff *pma;
13276 if (!map || !mpa)
13277 goto error;
13279 n = isl_map_dim(map, type);
13280 full = isl_map_involves_dims(map, type, 0, n);
13281 if (full >= 0 && !full)
13282 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13283 if (full < 0)
13284 goto error;
13285 if (!full) {
13286 isl_space *space;
13287 isl_multi_aff *ma;
13289 space = isl_multi_pw_aff_get_space(mpa);
13290 isl_multi_pw_aff_free(mpa);
13291 ma = isl_multi_aff_zero(space);
13292 return isl_map_preimage_multi_aff(map, type, ma);
13295 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13296 return isl_map_preimage_pw_multi_aff(map, type, pma);
13297 error:
13298 isl_map_free(map);
13299 isl_multi_pw_aff_free(mpa);
13300 return NULL;
13303 /* Compute the preimage of "map" under the function represented by "mpa".
13304 * In other words, plug in "mpa" in the domain "map".
13305 * The result is a map that lives in the same space as "map",
13306 * except that domain space has been replaced by the domain space of "mpa".
13308 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13309 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13311 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13314 /* Compute the preimage of "set" by the function represented by "mpa".
13315 * In other words, plug in "mpa" in "set".
13317 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13318 __isl_take isl_multi_pw_aff *mpa)
13320 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13323 /* Return a copy of the equality constraints of "bset" as a matrix.
13325 __isl_give isl_mat *isl_basic_set_extract_equalities(
13326 __isl_keep isl_basic_set *bset)
13328 isl_ctx *ctx;
13329 unsigned total;
13331 if (!bset)
13332 return NULL;
13334 ctx = isl_basic_set_get_ctx(bset);
13335 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13336 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13339 /* Are the "n" "coefficients" starting at "first" of the integer division
13340 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13341 * to each other?
13342 * The "coefficient" at position 0 is the denominator.
13343 * The "coefficient" at position 1 is the constant term.
13345 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13346 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13347 unsigned first, unsigned n)
13349 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13350 return isl_bool_error;
13351 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13352 return isl_bool_error;
13353 return isl_seq_eq(bmap1->div[pos1] + first,
13354 bmap2->div[pos2] + first, n);
13357 /* Are the integer division expressions at position "pos1" in "bmap1" and
13358 * "pos2" in "bmap2" equal to each other, except that the constant terms
13359 * are different?
13361 isl_bool isl_basic_map_equal_div_expr_except_constant(
13362 __isl_keep isl_basic_map *bmap1, int pos1,
13363 __isl_keep isl_basic_map *bmap2, int pos2)
13365 isl_bool equal;
13366 unsigned total;
13368 if (!bmap1 || !bmap2)
13369 return isl_bool_error;
13370 total = isl_basic_map_total_dim(bmap1);
13371 if (total != isl_basic_map_total_dim(bmap2))
13372 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13373 "incomparable div expressions", return isl_bool_error);
13374 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13375 0, 1);
13376 if (equal < 0 || !equal)
13377 return equal;
13378 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13379 1, 1);
13380 if (equal < 0 || equal)
13381 return isl_bool_not(equal);
13382 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13383 2, total);
13386 /* Replace the numerator of the constant term of the integer division
13387 * expression at position "div" in "bmap" by "value".
13388 * The caller guarantees that this does not change the meaning
13389 * of the input.
13391 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13392 __isl_take isl_basic_map *bmap, int div, int value)
13394 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13395 return isl_basic_map_free(bmap);
13397 isl_int_set_si(bmap->div[div][1], value);
13399 return bmap;
13402 /* Is the point "inner" internal to inequality constraint "ineq"
13403 * of "bset"?
13404 * The point is considered to be internal to the inequality constraint,
13405 * if it strictly lies on the positive side of the inequality constraint,
13406 * or if it lies on the constraint and the constraint is lexico-positive.
13408 static isl_bool is_internal(__isl_keep isl_vec *inner,
13409 __isl_keep isl_basic_set *bset, int ineq)
13411 isl_ctx *ctx;
13412 int pos;
13413 unsigned total;
13415 if (!inner || !bset)
13416 return isl_bool_error;
13418 ctx = isl_basic_set_get_ctx(bset);
13419 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13420 &ctx->normalize_gcd);
13421 if (!isl_int_is_zero(ctx->normalize_gcd))
13422 return isl_int_is_nonneg(ctx->normalize_gcd);
13424 total = isl_basic_set_dim(bset, isl_dim_all);
13425 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13426 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13429 /* Tighten the inequality constraints of "bset" that are outward with respect
13430 * to the point "vec".
13431 * That is, tighten the constraints that are not satisfied by "vec".
13433 * "vec" is a point internal to some superset S of "bset" that is used
13434 * to make the subsets of S disjoint, by tightening one half of the constraints
13435 * that separate two subsets. In particular, the constraints of S
13436 * are all satisfied by "vec" and should not be tightened.
13437 * Of the internal constraints, those that have "vec" on the outside
13438 * are tightened. The shared facet is included in the adjacent subset
13439 * with the opposite constraint.
13440 * For constraints that saturate "vec", this criterion cannot be used
13441 * to determine which of the two sides should be tightened.
13442 * Instead, the sign of the first non-zero coefficient is used
13443 * to make this choice. Note that this second criterion is never used
13444 * on the constraints of S since "vec" is interior to "S".
13446 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13447 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13449 int j;
13451 bset = isl_basic_set_cow(bset);
13452 if (!bset)
13453 return NULL;
13454 for (j = 0; j < bset->n_ineq; ++j) {
13455 isl_bool internal;
13457 internal = is_internal(vec, bset, j);
13458 if (internal < 0)
13459 return isl_basic_set_free(bset);
13460 if (internal)
13461 continue;
13462 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13465 return bset;
13468 /* Replace the variables x of type "type" starting at "first" in "bmap"
13469 * by x' with x = M x' with M the matrix trans.
13470 * That is, replace the corresponding coefficients c by c M.
13472 * The transformation matrix should be a square matrix.
13474 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13475 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13476 __isl_take isl_mat *trans)
13478 unsigned pos;
13480 bmap = isl_basic_map_cow(bmap);
13481 if (!bmap || !trans)
13482 goto error;
13484 if (trans->n_row != trans->n_col)
13485 isl_die(trans->ctx, isl_error_invalid,
13486 "expecting square transformation matrix", goto error);
13487 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13488 goto error;
13490 pos = isl_basic_map_offset(bmap, type) + first;
13492 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13493 isl_mat_copy(trans)) < 0)
13494 goto error;
13495 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13496 isl_mat_copy(trans)) < 0)
13497 goto error;
13498 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13499 isl_mat_copy(trans)) < 0)
13500 goto error;
13502 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13505 isl_mat_free(trans);
13506 return bmap;
13507 error:
13508 isl_mat_free(trans);
13509 isl_basic_map_free(bmap);
13510 return NULL;
13513 /* Replace the variables x of type "type" starting at "first" in "bset"
13514 * by x' with x = M x' with M the matrix trans.
13515 * That is, replace the corresponding coefficients c by c M.
13517 * The transformation matrix should be a square matrix.
13519 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13520 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13521 __isl_take isl_mat *trans)
13523 return isl_basic_map_transform_dims(bset, type, first, trans);