extract out shared isl_pw_multi_aff_check_range
[isl.git] / isl_map.c
blobe6bf7b6c55b3ba6520619594cbb3fe09857ce135
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/id.h>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl/map.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
36 #include <isl_sort.h>
37 #include "isl_tab.h"
38 #include <isl/vec.h>
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
48 #include <bset_to_bmap.c>
49 #include <bset_from_bmap.c>
50 #include <set_to_map.c>
51 #include <set_from_map.c>
53 /* Treat "bset" as a basic map.
54 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
55 * this function performs a redundant cast.
57 static __isl_keep const isl_basic_map *const_bset_to_bmap(
58 __isl_keep const isl_basic_set *bset)
60 return (const isl_basic_map *) bset;
63 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
65 switch (type) {
66 case isl_dim_param: return dim->nparam;
67 case isl_dim_in: return dim->n_in;
68 case isl_dim_out: return dim->n_out;
69 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
70 default: return 0;
74 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
76 switch (type) {
77 case isl_dim_param: return 1;
78 case isl_dim_in: return 1 + dim->nparam;
79 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
80 default: return 0;
84 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
85 enum isl_dim_type type)
87 if (!bmap)
88 return 0;
89 switch (type) {
90 case isl_dim_cst: return 1;
91 case isl_dim_param:
92 case isl_dim_in:
93 case isl_dim_out: return isl_space_dim(bmap->dim, type);
94 case isl_dim_div: return bmap->n_div;
95 case isl_dim_all: return isl_basic_map_total_dim(bmap);
96 default: return 0;
100 /* Return the space of "map".
102 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
104 return map ? map->dim : NULL;
107 /* Return the space of "set".
109 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
111 return isl_map_peek_space(set_to_map(set));
114 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
116 return map ? n(map->dim, type) : 0;
119 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
121 return set ? n(set->dim, type) : 0;
124 /* Return the position of the variables of the given type
125 * within the sequence of variables of "bmap".
127 int isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
128 enum isl_dim_type type)
130 isl_space *space;
132 space = isl_basic_map_peek_space(bmap);
133 if (!space)
134 return -1;
136 switch (type) {
137 case isl_dim_param:
138 case isl_dim_in:
139 case isl_dim_out: return isl_space_offset(space, type);
140 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
141 case isl_dim_cst:
142 default:
143 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
144 "invalid dimension type", return -1);
148 /* Return the position of the coefficients of the variables of the given type
149 * within the sequence of coefficients of "bmap".
151 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
152 enum isl_dim_type type)
154 switch (type) {
155 case isl_dim_cst: return 0;
156 case isl_dim_param:
157 case isl_dim_in:
158 case isl_dim_out:
159 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
160 default: return 0;
164 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
165 enum isl_dim_type type)
167 return isl_basic_map_offset(bset, type);
170 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
172 return pos(map->dim, type);
175 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
176 enum isl_dim_type type)
178 return isl_basic_map_dim(bset, type);
181 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
183 return isl_basic_set_dim(bset, isl_dim_set);
186 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
188 return isl_basic_set_dim(bset, isl_dim_param);
191 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
193 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
196 unsigned isl_set_n_dim(__isl_keep isl_set *set)
198 return isl_set_dim(set, isl_dim_set);
201 unsigned isl_set_n_param(__isl_keep isl_set *set)
203 return isl_set_dim(set, isl_dim_param);
206 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
208 return bmap ? bmap->dim->n_in : 0;
211 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
213 return bmap ? bmap->dim->n_out : 0;
216 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
218 return bmap ? bmap->dim->nparam : 0;
221 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
223 return bmap ? bmap->n_div : 0;
226 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
228 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
231 unsigned isl_map_n_in(__isl_keep const isl_map *map)
233 return map ? map->dim->n_in : 0;
236 unsigned isl_map_n_out(__isl_keep const isl_map *map)
238 return map ? map->dim->n_out : 0;
241 unsigned isl_map_n_param(__isl_keep const isl_map *map)
243 return map ? map->dim->nparam : 0;
246 /* Return the number of equality constraints in the description of "bmap".
247 * Return -1 on error.
249 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
251 if (!bmap)
252 return -1;
253 return bmap->n_eq;
256 /* Return the number of equality constraints in the description of "bset".
257 * Return -1 on error.
259 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
261 return isl_basic_map_n_equality(bset_to_bmap(bset));
264 /* Return the number of inequality constraints in the description of "bmap".
265 * Return -1 on error.
267 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
269 if (!bmap)
270 return -1;
271 return bmap->n_ineq;
274 /* Return the number of inequality constraints in the description of "bset".
275 * Return -1 on error.
277 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
279 return isl_basic_map_n_inequality(bset_to_bmap(bset));
282 /* Do "bmap1" and "bmap2" have the same parameters?
284 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
285 __isl_keep isl_basic_map *bmap2)
287 isl_space *space1, *space2;
289 space1 = isl_basic_map_peek_space(bmap1);
290 space2 = isl_basic_map_peek_space(bmap2);
291 return isl_space_has_equal_params(space1, space2);
294 /* Do "map1" and "map2" have the same parameters?
296 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
297 __isl_keep isl_map *map2)
299 isl_space *space1, *space2;
301 space1 = isl_map_peek_space(map1);
302 space2 = isl_map_peek_space(map2);
303 return isl_space_has_equal_params(space1, space2);
306 /* Do "map" and "set" have the same parameters?
308 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
309 __isl_keep isl_set *set)
311 return isl_map_has_equal_params(map, set_to_map(set));
314 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
315 __isl_keep isl_set *set)
317 isl_bool m;
318 if (!map || !set)
319 return isl_bool_error;
320 m = isl_map_has_equal_params(map, set_to_map(set));
321 if (m < 0 || !m)
322 return m;
323 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
324 set->dim, isl_dim_set);
327 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
328 __isl_keep isl_basic_set *bset)
330 isl_bool m;
331 if (!bmap || !bset)
332 return isl_bool_error;
333 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
334 if (m < 0 || !m)
335 return m;
336 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
337 bset->dim, isl_dim_set);
340 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
341 __isl_keep isl_set *set)
343 isl_bool m;
344 if (!map || !set)
345 return isl_bool_error;
346 m = isl_map_has_equal_params(map, set_to_map(set));
347 if (m < 0 || !m)
348 return m;
349 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
350 set->dim, isl_dim_set);
353 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
354 __isl_keep isl_basic_set *bset)
356 isl_bool m;
357 if (!bmap || !bset)
358 return isl_bool_error;
359 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
360 if (m < 0 || !m)
361 return m;
362 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
363 bset->dim, isl_dim_set);
366 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
368 return bmap ? bmap->ctx : NULL;
371 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
373 return bset ? bset->ctx : NULL;
376 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
378 return map ? map->ctx : NULL;
381 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
383 return set ? set->ctx : NULL;
386 /* Return the space of "bmap".
388 __isl_keep isl_space *isl_basic_map_peek_space(
389 __isl_keep const isl_basic_map *bmap)
391 return bmap ? bmap->dim : NULL;
394 /* Return the space of "bset".
396 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
398 return isl_basic_map_peek_space(bset_to_bmap(bset));
401 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
403 return isl_space_copy(isl_basic_map_peek_space(bmap));
406 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
408 return isl_basic_map_get_space(bset_to_bmap(bset));
411 /* Extract the divs in "bmap" as a matrix.
413 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
415 int i;
416 isl_ctx *ctx;
417 isl_mat *div;
418 unsigned total;
419 unsigned cols;
421 if (!bmap)
422 return NULL;
424 ctx = isl_basic_map_get_ctx(bmap);
425 total = isl_space_dim(bmap->dim, isl_dim_all);
426 cols = 1 + 1 + total + bmap->n_div;
427 div = isl_mat_alloc(ctx, bmap->n_div, cols);
428 if (!div)
429 return NULL;
431 for (i = 0; i < bmap->n_div; ++i)
432 isl_seq_cpy(div->row[i], bmap->div[i], cols);
434 return div;
437 /* Extract the divs in "bset" as a matrix.
439 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
441 return isl_basic_map_get_divs(bset);
444 __isl_give isl_local_space *isl_basic_map_get_local_space(
445 __isl_keep isl_basic_map *bmap)
447 isl_mat *div;
449 if (!bmap)
450 return NULL;
452 div = isl_basic_map_get_divs(bmap);
453 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
456 __isl_give isl_local_space *isl_basic_set_get_local_space(
457 __isl_keep isl_basic_set *bset)
459 return isl_basic_map_get_local_space(bset);
462 /* For each known div d = floor(f/m), add the constraints
464 * f - m d >= 0
465 * -(f-(m-1)) + m d >= 0
467 * Do not finalize the result.
469 static __isl_give isl_basic_map *add_known_div_constraints(
470 __isl_take isl_basic_map *bmap)
472 int i;
473 unsigned n_div;
475 if (!bmap)
476 return NULL;
477 n_div = isl_basic_map_dim(bmap, isl_dim_div);
478 if (n_div == 0)
479 return bmap;
480 bmap = isl_basic_map_cow(bmap);
481 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
482 if (!bmap)
483 return NULL;
484 for (i = 0; i < n_div; ++i) {
485 if (isl_int_is_zero(bmap->div[i][0]))
486 continue;
487 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
488 return isl_basic_map_free(bmap);
491 return bmap;
494 __isl_give isl_basic_map *isl_basic_map_from_local_space(
495 __isl_take isl_local_space *ls)
497 int i;
498 int n_div;
499 isl_basic_map *bmap;
501 if (!ls)
502 return NULL;
504 n_div = isl_local_space_dim(ls, isl_dim_div);
505 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
506 n_div, 0, 2 * n_div);
508 for (i = 0; i < n_div; ++i)
509 if (isl_basic_map_alloc_div(bmap) < 0)
510 goto error;
512 for (i = 0; i < n_div; ++i)
513 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
514 bmap = add_known_div_constraints(bmap);
516 isl_local_space_free(ls);
517 return bmap;
518 error:
519 isl_local_space_free(ls);
520 isl_basic_map_free(bmap);
521 return NULL;
524 __isl_give isl_basic_set *isl_basic_set_from_local_space(
525 __isl_take isl_local_space *ls)
527 return isl_basic_map_from_local_space(ls);
530 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
532 return isl_space_copy(isl_map_peek_space(map));
535 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
537 if (!set)
538 return NULL;
539 return isl_space_copy(set->dim);
542 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
543 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
545 bmap = isl_basic_map_cow(bmap);
546 if (!bmap)
547 return NULL;
548 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
549 if (!bmap->dim)
550 goto error;
551 bmap = isl_basic_map_finalize(bmap);
552 return bmap;
553 error:
554 isl_basic_map_free(bmap);
555 return NULL;
558 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
559 __isl_take isl_basic_set *bset, const char *s)
561 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
564 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
565 enum isl_dim_type type)
567 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
570 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
571 enum isl_dim_type type, const char *s)
573 int i;
575 map = isl_map_cow(map);
576 if (!map)
577 return NULL;
579 map->dim = isl_space_set_tuple_name(map->dim, type, s);
580 if (!map->dim)
581 goto error;
583 for (i = 0; i < map->n; ++i) {
584 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
585 if (!map->p[i])
586 goto error;
589 return map;
590 error:
591 isl_map_free(map);
592 return NULL;
595 /* Replace the identifier of the tuple of type "type" by "id".
597 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
598 __isl_take isl_basic_map *bmap,
599 enum isl_dim_type type, __isl_take isl_id *id)
601 bmap = isl_basic_map_cow(bmap);
602 if (!bmap)
603 goto error;
604 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
605 if (!bmap->dim)
606 return isl_basic_map_free(bmap);
607 bmap = isl_basic_map_finalize(bmap);
608 return bmap;
609 error:
610 isl_id_free(id);
611 return NULL;
614 /* Replace the identifier of the tuple by "id".
616 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
617 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
619 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
622 /* Does the input or output tuple have a name?
624 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
626 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
629 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
630 enum isl_dim_type type)
632 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
635 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
636 const char *s)
638 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
639 isl_dim_set, s));
642 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
643 enum isl_dim_type type, __isl_take isl_id *id)
645 map = isl_map_cow(map);
646 if (!map)
647 goto error;
649 map->dim = isl_space_set_tuple_id(map->dim, type, id);
651 return isl_map_reset_space(map, isl_space_copy(map->dim));
652 error:
653 isl_id_free(id);
654 return NULL;
657 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
658 __isl_take isl_id *id)
660 return isl_map_set_tuple_id(set, isl_dim_set, id);
663 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
664 enum isl_dim_type type)
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
670 map->dim = isl_space_reset_tuple_id(map->dim, type);
672 return isl_map_reset_space(map, isl_space_copy(map->dim));
675 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
677 return isl_map_reset_tuple_id(set, isl_dim_set);
680 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
682 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
685 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
686 enum isl_dim_type type)
688 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
691 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
693 return isl_map_has_tuple_id(set, isl_dim_set);
696 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
698 return isl_map_get_tuple_id(set, isl_dim_set);
701 /* Does the set tuple have a name?
703 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
705 if (!set)
706 return isl_bool_error;
707 return isl_space_has_tuple_name(set->dim, isl_dim_set);
711 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
713 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
716 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
718 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
721 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
722 enum isl_dim_type type, unsigned pos)
724 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
727 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
728 enum isl_dim_type type, unsigned pos)
730 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
733 /* Does the given dimension have a name?
735 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
736 enum isl_dim_type type, unsigned pos)
738 if (!map)
739 return isl_bool_error;
740 return isl_space_has_dim_name(map->dim, type, pos);
743 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
744 enum isl_dim_type type, unsigned pos)
746 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
749 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
750 enum isl_dim_type type, unsigned pos)
752 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
755 /* Does the given dimension have a name?
757 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
758 enum isl_dim_type type, unsigned pos)
760 if (!set)
761 return isl_bool_error;
762 return isl_space_has_dim_name(set->dim, type, pos);
765 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
766 __isl_take isl_basic_map *bmap,
767 enum isl_dim_type type, unsigned pos, const char *s)
769 bmap = isl_basic_map_cow(bmap);
770 if (!bmap)
771 return NULL;
772 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
773 if (!bmap->dim)
774 goto error;
775 return isl_basic_map_finalize(bmap);
776 error:
777 isl_basic_map_free(bmap);
778 return NULL;
781 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
782 enum isl_dim_type type, unsigned pos, const char *s)
784 int i;
786 map = isl_map_cow(map);
787 if (!map)
788 return NULL;
790 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
791 if (!map->dim)
792 goto error;
794 for (i = 0; i < map->n; ++i) {
795 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
796 if (!map->p[i])
797 goto error;
800 return map;
801 error:
802 isl_map_free(map);
803 return NULL;
806 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
807 __isl_take isl_basic_set *bset,
808 enum isl_dim_type type, unsigned pos, const char *s)
810 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
811 type, pos, s));
814 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
815 enum isl_dim_type type, unsigned pos, const char *s)
817 return set_from_map(isl_map_set_dim_name(set_to_map(set),
818 type, pos, s));
821 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
822 enum isl_dim_type type, unsigned pos)
824 if (!bmap)
825 return isl_bool_error;
826 return isl_space_has_dim_id(bmap->dim, type, pos);
829 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
830 enum isl_dim_type type, unsigned pos)
832 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
835 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
836 enum isl_dim_type type, unsigned pos)
838 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
841 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
842 enum isl_dim_type type, unsigned pos)
844 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
847 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
848 enum isl_dim_type type, unsigned pos)
850 return isl_map_has_dim_id(set, type, pos);
853 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
854 enum isl_dim_type type, unsigned pos)
856 return isl_map_get_dim_id(set, type, pos);
859 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
860 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
862 map = isl_map_cow(map);
863 if (!map)
864 goto error;
866 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
868 return isl_map_reset_space(map, isl_space_copy(map->dim));
869 error:
870 isl_id_free(id);
871 return NULL;
874 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
875 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
877 return isl_map_set_dim_id(set, type, pos, id);
880 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
881 __isl_keep isl_id *id)
883 if (!map)
884 return -1;
885 return isl_space_find_dim_by_id(map->dim, type, id);
888 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
889 __isl_keep isl_id *id)
891 return isl_map_find_dim_by_id(set, type, id);
894 /* Return the position of the dimension of the given type and name
895 * in "bmap".
896 * Return -1 if no such dimension can be found.
898 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
899 enum isl_dim_type type, const char *name)
901 if (!bmap)
902 return -1;
903 return isl_space_find_dim_by_name(bmap->dim, type, name);
906 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
907 const char *name)
909 if (!map)
910 return -1;
911 return isl_space_find_dim_by_name(map->dim, type, name);
914 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
915 const char *name)
917 return isl_map_find_dim_by_name(set, type, name);
920 /* Check whether equality i of bset is a pure stride constraint
921 * on a single dimension, i.e., of the form
923 * v = k e
925 * with k a constant and e an existentially quantified variable.
927 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
929 unsigned nparam;
930 unsigned d;
931 unsigned n_div;
932 int pos1;
933 int pos2;
935 if (!bset)
936 return isl_bool_error;
938 if (!isl_int_is_zero(bset->eq[i][0]))
939 return isl_bool_false;
941 nparam = isl_basic_set_dim(bset, isl_dim_param);
942 d = isl_basic_set_dim(bset, isl_dim_set);
943 n_div = isl_basic_set_dim(bset, isl_dim_div);
945 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
946 return isl_bool_false;
947 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
948 if (pos1 == -1)
949 return isl_bool_false;
950 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
951 d - pos1 - 1) != -1)
952 return isl_bool_false;
954 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
955 if (pos2 == -1)
956 return isl_bool_false;
957 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
958 n_div - pos2 - 1) != -1)
959 return isl_bool_false;
960 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
961 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
962 return isl_bool_false;
964 return isl_bool_true;
967 /* Reset the user pointer on all identifiers of parameters and tuples
968 * of the space of "map".
970 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
972 isl_space *space;
974 space = isl_map_get_space(map);
975 space = isl_space_reset_user(space);
976 map = isl_map_reset_space(map, space);
978 return map;
981 /* Reset the user pointer on all identifiers of parameters and tuples
982 * of the space of "set".
984 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
986 return isl_map_reset_user(set);
989 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
991 if (!bmap)
992 return isl_bool_error;
993 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
996 /* Has "map" been marked as a rational map?
997 * In particular, have all basic maps in "map" been marked this way?
998 * An empty map is not considered to be rational.
999 * Maps where only some of the basic maps are marked rational
1000 * are not allowed.
1002 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1004 int i;
1005 isl_bool rational;
1007 if (!map)
1008 return isl_bool_error;
1009 if (map->n == 0)
1010 return isl_bool_false;
1011 rational = isl_basic_map_is_rational(map->p[0]);
1012 if (rational < 0)
1013 return rational;
1014 for (i = 1; i < map->n; ++i) {
1015 isl_bool rational_i;
1017 rational_i = isl_basic_map_is_rational(map->p[i]);
1018 if (rational_i < 0)
1019 return rational_i;
1020 if (rational != rational_i)
1021 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1022 "mixed rational and integer basic maps "
1023 "not supported", return isl_bool_error);
1026 return rational;
1029 /* Has "set" been marked as a rational set?
1030 * In particular, have all basic set in "set" been marked this way?
1031 * An empty set is not considered to be rational.
1032 * Sets where only some of the basic sets are marked rational
1033 * are not allowed.
1035 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1037 return isl_map_is_rational(set);
1040 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1042 return isl_basic_map_is_rational(bset);
1045 /* Does "bmap" contain any rational points?
1047 * If "bmap" has an equality for each dimension, equating the dimension
1048 * to an integer constant, then it has no rational points, even if it
1049 * is marked as rational.
1051 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1053 isl_bool has_rational = isl_bool_true;
1054 unsigned total;
1056 if (!bmap)
1057 return isl_bool_error;
1058 if (isl_basic_map_plain_is_empty(bmap))
1059 return isl_bool_false;
1060 if (!isl_basic_map_is_rational(bmap))
1061 return isl_bool_false;
1062 bmap = isl_basic_map_copy(bmap);
1063 bmap = isl_basic_map_implicit_equalities(bmap);
1064 if (!bmap)
1065 return isl_bool_error;
1066 total = isl_basic_map_total_dim(bmap);
1067 if (bmap->n_eq == total) {
1068 int i, j;
1069 for (i = 0; i < bmap->n_eq; ++i) {
1070 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1071 if (j < 0)
1072 break;
1073 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1074 !isl_int_is_negone(bmap->eq[i][1 + j]))
1075 break;
1076 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1077 total - j - 1);
1078 if (j >= 0)
1079 break;
1081 if (i == bmap->n_eq)
1082 has_rational = isl_bool_false;
1084 isl_basic_map_free(bmap);
1086 return has_rational;
1089 /* Does "map" contain any rational points?
1091 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1093 int i;
1094 isl_bool has_rational;
1096 if (!map)
1097 return isl_bool_error;
1098 for (i = 0; i < map->n; ++i) {
1099 has_rational = isl_basic_map_has_rational(map->p[i]);
1100 if (has_rational < 0 || has_rational)
1101 return has_rational;
1103 return isl_bool_false;
1106 /* Does "set" contain any rational points?
1108 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1110 return isl_map_has_rational(set);
1113 /* Is this basic set a parameter domain?
1115 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1117 if (!bset)
1118 return isl_bool_error;
1119 return isl_space_is_params(bset->dim);
1122 /* Is this set a parameter domain?
1124 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1126 if (!set)
1127 return isl_bool_error;
1128 return isl_space_is_params(set->dim);
1131 /* Is this map actually a parameter domain?
1132 * Users should never call this function. Outside of isl,
1133 * a map can never be a parameter domain.
1135 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1137 if (!map)
1138 return isl_bool_error;
1139 return isl_space_is_params(map->dim);
1142 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1143 __isl_take isl_basic_map *bmap, unsigned extra,
1144 unsigned n_eq, unsigned n_ineq)
1146 int i;
1147 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1149 bmap->ctx = ctx;
1150 isl_ctx_ref(ctx);
1152 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1153 if (isl_blk_is_error(bmap->block))
1154 goto error;
1156 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1157 if ((n_ineq + n_eq) && !bmap->ineq)
1158 goto error;
1160 if (extra == 0) {
1161 bmap->block2 = isl_blk_empty();
1162 bmap->div = NULL;
1163 } else {
1164 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1165 if (isl_blk_is_error(bmap->block2))
1166 goto error;
1168 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1169 if (!bmap->div)
1170 goto error;
1173 for (i = 0; i < n_ineq + n_eq; ++i)
1174 bmap->ineq[i] = bmap->block.data + i * row_size;
1176 for (i = 0; i < extra; ++i)
1177 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1179 bmap->ref = 1;
1180 bmap->flags = 0;
1181 bmap->c_size = n_eq + n_ineq;
1182 bmap->eq = bmap->ineq + n_ineq;
1183 bmap->extra = extra;
1184 bmap->n_eq = 0;
1185 bmap->n_ineq = 0;
1186 bmap->n_div = 0;
1187 bmap->sample = NULL;
1189 return bmap;
1190 error:
1191 isl_basic_map_free(bmap);
1192 return NULL;
1195 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1196 unsigned nparam, unsigned dim, unsigned extra,
1197 unsigned n_eq, unsigned n_ineq)
1199 struct isl_basic_map *bmap;
1200 isl_space *space;
1202 space = isl_space_set_alloc(ctx, nparam, dim);
1203 if (!space)
1204 return NULL;
1206 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1207 return bset_from_bmap(bmap);
1210 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1211 unsigned extra, unsigned n_eq, unsigned n_ineq)
1213 struct isl_basic_map *bmap;
1214 if (!dim)
1215 return NULL;
1216 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1217 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1218 return bset_from_bmap(bmap);
1219 error:
1220 isl_space_free(dim);
1221 return NULL;
1224 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1225 unsigned extra, unsigned n_eq, unsigned n_ineq)
1227 struct isl_basic_map *bmap;
1229 if (!space)
1230 return NULL;
1231 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1232 if (!bmap)
1233 goto error;
1234 bmap->dim = space;
1236 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1237 error:
1238 isl_space_free(space);
1239 return NULL;
1242 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1243 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1244 unsigned n_eq, unsigned n_ineq)
1246 struct isl_basic_map *bmap;
1247 isl_space *dim;
1249 dim = isl_space_alloc(ctx, nparam, in, out);
1250 if (!dim)
1251 return NULL;
1253 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1254 return bmap;
1257 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1258 __isl_keep isl_basic_map *src)
1260 int i;
1261 unsigned total = isl_basic_map_total_dim(src);
1263 if (!dst)
1264 return NULL;
1266 for (i = 0; i < src->n_eq; ++i) {
1267 int j = isl_basic_map_alloc_equality(dst);
1268 if (j < 0)
1269 return isl_basic_map_free(dst);
1270 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1273 for (i = 0; i < src->n_ineq; ++i) {
1274 int j = isl_basic_map_alloc_inequality(dst);
1275 if (j < 0)
1276 return isl_basic_map_free(dst);
1277 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1280 for (i = 0; i < src->n_div; ++i) {
1281 int j = isl_basic_map_alloc_div(dst);
1282 if (j < 0)
1283 return isl_basic_map_free(dst);
1284 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1286 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1287 return dst;
1290 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1292 struct isl_basic_map *dup;
1294 if (!bmap)
1295 return NULL;
1296 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1297 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1298 dup = dup_constraints(dup, bmap);
1299 if (!dup)
1300 return NULL;
1301 dup->flags = bmap->flags;
1302 dup->sample = isl_vec_copy(bmap->sample);
1303 return dup;
1306 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1308 struct isl_basic_map *dup;
1310 dup = isl_basic_map_dup(bset_to_bmap(bset));
1311 return bset_from_bmap(dup);
1314 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1316 if (!bset)
1317 return NULL;
1319 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1320 bset->ref++;
1321 return bset;
1323 return isl_basic_set_dup(bset);
1326 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1328 if (!set)
1329 return NULL;
1331 set->ref++;
1332 return set;
1335 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1337 if (!bmap)
1338 return NULL;
1340 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1341 bmap->ref++;
1342 return bmap;
1344 bmap = isl_basic_map_dup(bmap);
1345 if (bmap)
1346 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1347 return bmap;
1350 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1352 if (!map)
1353 return NULL;
1355 map->ref++;
1356 return map;
1359 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1361 if (!bmap)
1362 return NULL;
1364 if (--bmap->ref > 0)
1365 return NULL;
1367 isl_ctx_deref(bmap->ctx);
1368 free(bmap->div);
1369 isl_blk_free(bmap->ctx, bmap->block2);
1370 free(bmap->ineq);
1371 isl_blk_free(bmap->ctx, bmap->block);
1372 isl_vec_free(bmap->sample);
1373 isl_space_free(bmap->dim);
1374 free(bmap);
1376 return NULL;
1379 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1381 return isl_basic_map_free(bset_to_bmap(bset));
1384 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1386 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1389 /* Check that "map" has only named parameters, reporting an error
1390 * if it does not.
1392 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1394 return isl_space_check_named_params(isl_map_peek_space(map));
1397 /* Check that "bmap" has only named parameters, reporting an error
1398 * if it does not.
1400 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1402 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1405 /* Check that "bmap1" and "bmap2" have the same parameters,
1406 * reporting an error if they do not.
1408 static isl_stat isl_basic_map_check_equal_params(
1409 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1411 isl_bool match;
1413 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1414 if (match < 0)
1415 return isl_stat_error;
1416 if (!match)
1417 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1418 "parameters don't match", return isl_stat_error);
1419 return isl_stat_ok;
1422 __isl_give isl_map *isl_map_align_params_map_map_and(
1423 __isl_take isl_map *map1, __isl_take isl_map *map2,
1424 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1425 __isl_take isl_map *map2))
1427 if (!map1 || !map2)
1428 goto error;
1429 if (isl_map_has_equal_params(map1, map2))
1430 return fn(map1, map2);
1431 if (isl_map_check_named_params(map1) < 0)
1432 goto error;
1433 if (isl_map_check_named_params(map2) < 0)
1434 goto error;
1435 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1436 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1437 return fn(map1, map2);
1438 error:
1439 isl_map_free(map1);
1440 isl_map_free(map2);
1441 return NULL;
1444 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1445 __isl_keep isl_map *map2,
1446 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1448 isl_bool r;
1450 if (!map1 || !map2)
1451 return isl_bool_error;
1452 if (isl_map_has_equal_params(map1, map2))
1453 return fn(map1, map2);
1454 if (isl_map_check_named_params(map1) < 0)
1455 return isl_bool_error;
1456 if (isl_map_check_named_params(map2) < 0)
1457 return isl_bool_error;
1458 map1 = isl_map_copy(map1);
1459 map2 = isl_map_copy(map2);
1460 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1461 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1462 r = fn(map1, map2);
1463 isl_map_free(map1);
1464 isl_map_free(map2);
1465 return r;
1468 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1470 struct isl_ctx *ctx;
1471 if (!bmap)
1472 return -1;
1473 ctx = bmap->ctx;
1474 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1475 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1476 return -1);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1478 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1479 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1480 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1481 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1482 isl_int *t;
1483 int j = isl_basic_map_alloc_inequality(bmap);
1484 if (j < 0)
1485 return -1;
1486 t = bmap->ineq[j];
1487 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1488 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1489 bmap->eq[-1] = t;
1490 bmap->n_eq++;
1491 bmap->n_ineq--;
1492 bmap->eq--;
1493 return 0;
1495 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1496 bmap->extra - bmap->n_div);
1497 return bmap->n_eq++;
1500 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1502 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1505 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1507 if (!bmap)
1508 return -1;
1509 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1510 bmap->n_eq -= n;
1511 return 0;
1514 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1516 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1519 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1521 isl_int *t;
1522 if (!bmap)
1523 return -1;
1524 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1526 if (pos != bmap->n_eq - 1) {
1527 t = bmap->eq[pos];
1528 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1529 bmap->eq[bmap->n_eq - 1] = t;
1531 bmap->n_eq--;
1532 return 0;
1535 /* Turn inequality "pos" of "bmap" into an equality.
1537 * In particular, we move the inequality in front of the equalities
1538 * and move the last inequality in the position of the moved inequality.
1539 * Note that isl_tab_make_equalities_explicit depends on this particular
1540 * change in the ordering of the constraints.
1542 void isl_basic_map_inequality_to_equality(
1543 struct isl_basic_map *bmap, unsigned pos)
1545 isl_int *t;
1547 t = bmap->ineq[pos];
1548 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1549 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1550 bmap->eq[-1] = t;
1551 bmap->n_eq++;
1552 bmap->n_ineq--;
1553 bmap->eq--;
1554 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1555 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1556 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1557 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1560 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1562 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1565 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1567 struct isl_ctx *ctx;
1568 if (!bmap)
1569 return -1;
1570 ctx = bmap->ctx;
1571 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1572 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1573 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1574 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1575 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1576 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1577 1 + isl_basic_map_total_dim(bmap),
1578 bmap->extra - bmap->n_div);
1579 return bmap->n_ineq++;
1582 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1584 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1587 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1589 if (!bmap)
1590 return -1;
1591 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1592 bmap->n_ineq -= n;
1593 return 0;
1596 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1598 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1601 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1603 isl_int *t;
1604 if (!bmap)
1605 return -1;
1606 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1608 if (pos != bmap->n_ineq - 1) {
1609 t = bmap->ineq[pos];
1610 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1611 bmap->ineq[bmap->n_ineq - 1] = t;
1612 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1614 bmap->n_ineq--;
1615 return 0;
1618 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1620 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1623 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1624 isl_int *eq)
1626 isl_bool empty;
1627 int k;
1629 empty = isl_basic_map_plain_is_empty(bmap);
1630 if (empty < 0)
1631 return isl_basic_map_free(bmap);
1632 if (empty)
1633 return bmap;
1635 bmap = isl_basic_map_cow(bmap);
1636 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1637 if (!bmap)
1638 return NULL;
1639 k = isl_basic_map_alloc_equality(bmap);
1640 if (k < 0)
1641 goto error;
1642 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1643 return bmap;
1644 error:
1645 isl_basic_map_free(bmap);
1646 return NULL;
1649 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1650 isl_int *eq)
1652 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1655 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1656 isl_int *ineq)
1658 int k;
1660 bmap = isl_basic_map_cow(bmap);
1661 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1662 if (!bmap)
1663 return NULL;
1664 k = isl_basic_map_alloc_inequality(bmap);
1665 if (k < 0)
1666 goto error;
1667 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1668 return bmap;
1669 error:
1670 isl_basic_map_free(bmap);
1671 return NULL;
1674 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1675 isl_int *ineq)
1677 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1680 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1682 if (!bmap)
1683 return -1;
1684 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1685 isl_seq_clr(bmap->div[bmap->n_div] +
1686 1 + 1 + isl_basic_map_total_dim(bmap),
1687 bmap->extra - bmap->n_div);
1688 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1689 return bmap->n_div++;
1692 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1694 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1697 #undef TYPE
1698 #define TYPE isl_basic_map
1699 #include "check_type_range_templ.c"
1701 /* Check that there are "n" dimensions of type "type" starting at "first"
1702 * in "bset".
1704 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1705 enum isl_dim_type type, unsigned first, unsigned n)
1707 return isl_basic_map_check_range(bset_to_bmap(bset),
1708 type, first, n);
1711 /* Insert an extra integer division, prescribed by "div", to "bmap"
1712 * at (integer division) position "pos".
1714 * The integer division is first added at the end and then moved
1715 * into the right position.
1717 __isl_give isl_basic_map *isl_basic_map_insert_div(
1718 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1720 int i, k;
1722 bmap = isl_basic_map_cow(bmap);
1723 if (!bmap || !div)
1724 return isl_basic_map_free(bmap);
1726 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1727 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1728 "unexpected size", return isl_basic_map_free(bmap));
1729 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1730 return isl_basic_map_free(bmap);
1732 bmap = isl_basic_map_extend_space(bmap,
1733 isl_basic_map_get_space(bmap), 1, 0, 2);
1734 k = isl_basic_map_alloc_div(bmap);
1735 if (k < 0)
1736 return isl_basic_map_free(bmap);
1737 isl_seq_cpy(bmap->div[k], div->el, div->size);
1738 isl_int_set_si(bmap->div[k][div->size], 0);
1740 for (i = k; i > pos; --i)
1741 isl_basic_map_swap_div(bmap, i, i - 1);
1743 return bmap;
1746 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1748 if (!bmap)
1749 return isl_stat_error;
1750 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1751 bmap->n_div -= n;
1752 return isl_stat_ok;
1755 static __isl_give isl_basic_map *add_constraints(
1756 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1757 unsigned i_pos, unsigned o_pos)
1759 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1760 isl_ctx *ctx;
1761 isl_space *space;
1762 struct isl_dim_map *dim_map;
1764 space = isl_basic_map_peek_space(bmap2);
1765 if (!bmap1 || !space)
1766 goto error;
1768 total = isl_basic_map_dim(bmap1, isl_dim_all);
1769 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1770 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1771 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1772 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1773 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1774 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1775 ctx = isl_basic_map_get_ctx(bmap1);
1776 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1777 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1778 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1779 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1780 isl_dim_map_div(dim_map, bmap2, total);
1782 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1783 error:
1784 isl_basic_map_free(bmap1);
1785 isl_basic_map_free(bmap2);
1786 return NULL;
1789 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1790 struct isl_basic_set *bset2, unsigned pos)
1792 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1793 bset_to_bmap(bset2), 0, pos));
1796 __isl_give isl_basic_map *isl_basic_map_extend_space(
1797 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1798 unsigned extra, unsigned n_eq, unsigned n_ineq)
1800 struct isl_basic_map *ext;
1801 unsigned flags;
1802 int dims_ok;
1804 if (!space)
1805 goto error;
1807 if (!base)
1808 goto error;
1810 dims_ok = isl_space_is_equal(base->dim, space) &&
1811 base->extra >= base->n_div + extra;
1813 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1814 room_for_ineq(base, n_ineq)) {
1815 isl_space_free(space);
1816 return base;
1819 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1820 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1821 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1822 extra += base->extra;
1823 n_eq += base->n_eq;
1824 n_ineq += base->n_ineq;
1826 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1827 space = NULL;
1828 if (!ext)
1829 goto error;
1831 if (dims_ok)
1832 ext->sample = isl_vec_copy(base->sample);
1833 flags = base->flags;
1834 ext = add_constraints(ext, base, 0, 0);
1835 if (ext) {
1836 ext->flags = flags;
1837 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1840 return ext;
1842 error:
1843 isl_space_free(space);
1844 isl_basic_map_free(base);
1845 return NULL;
1848 __isl_give isl_basic_set *isl_basic_set_extend_space(
1849 __isl_take isl_basic_set *base,
1850 __isl_take isl_space *dim, unsigned extra,
1851 unsigned n_eq, unsigned n_ineq)
1853 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1854 dim, extra, n_eq, n_ineq));
1857 struct isl_basic_map *isl_basic_map_extend_constraints(
1858 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1860 if (!base)
1861 return NULL;
1862 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1863 0, n_eq, n_ineq);
1866 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1867 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1868 unsigned n_eq, unsigned n_ineq)
1870 struct isl_basic_map *bmap;
1871 isl_space *dim;
1873 if (!base)
1874 return NULL;
1875 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1876 if (!dim)
1877 goto error;
1879 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1880 return bmap;
1881 error:
1882 isl_basic_map_free(base);
1883 return NULL;
1886 struct isl_basic_set *isl_basic_set_extend_constraints(
1887 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1889 isl_basic_map *bmap = bset_to_bmap(base);
1890 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1891 return bset_from_bmap(bmap);
1894 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1896 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1899 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1901 if (!bmap)
1902 return NULL;
1904 if (bmap->ref > 1) {
1905 bmap->ref--;
1906 bmap = isl_basic_map_dup(bmap);
1908 if (bmap) {
1909 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1910 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1912 return bmap;
1915 /* Clear all cached information in "map", either because it is about
1916 * to be modified or because it is being freed.
1917 * Always return the same pointer that is passed in.
1918 * This is needed for the use in isl_map_free.
1920 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1922 isl_basic_map_free(map->cached_simple_hull[0]);
1923 isl_basic_map_free(map->cached_simple_hull[1]);
1924 map->cached_simple_hull[0] = NULL;
1925 map->cached_simple_hull[1] = NULL;
1926 return map;
1929 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1931 return isl_map_cow(set);
1934 /* Return an isl_map that is equal to "map" and that has only
1935 * a single reference.
1937 * If the original input already has only one reference, then
1938 * simply return it, but clear all cached information, since
1939 * it may be rendered invalid by the operations that will be
1940 * performed on the result.
1942 * Otherwise, create a duplicate (without any cached information).
1944 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1946 if (!map)
1947 return NULL;
1949 if (map->ref == 1)
1950 return clear_caches(map);
1951 map->ref--;
1952 return isl_map_dup(map);
1955 static void swap_vars(struct isl_blk blk, isl_int *a,
1956 unsigned a_len, unsigned b_len)
1958 isl_seq_cpy(blk.data, a+a_len, b_len);
1959 isl_seq_cpy(blk.data+b_len, a, a_len);
1960 isl_seq_cpy(a, blk.data, b_len+a_len);
1963 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1964 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1966 int i;
1967 struct isl_blk blk;
1969 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1970 goto error;
1972 if (n1 == 0 || n2 == 0)
1973 return bmap;
1975 bmap = isl_basic_map_cow(bmap);
1976 if (!bmap)
1977 return NULL;
1979 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1980 if (isl_blk_is_error(blk))
1981 goto error;
1983 for (i = 0; i < bmap->n_eq; ++i)
1984 swap_vars(blk,
1985 bmap->eq[i] + pos, n1, n2);
1987 for (i = 0; i < bmap->n_ineq; ++i)
1988 swap_vars(blk,
1989 bmap->ineq[i] + pos, n1, n2);
1991 for (i = 0; i < bmap->n_div; ++i)
1992 swap_vars(blk,
1993 bmap->div[i]+1 + pos, n1, n2);
1995 isl_blk_free(bmap->ctx, blk);
1997 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
1998 bmap = isl_basic_map_gauss(bmap, NULL);
1999 return isl_basic_map_finalize(bmap);
2000 error:
2001 isl_basic_map_free(bmap);
2002 return NULL;
2005 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2006 __isl_take isl_basic_map *bmap)
2008 int i = 0;
2009 unsigned total;
2010 if (!bmap)
2011 goto error;
2012 total = isl_basic_map_total_dim(bmap);
2013 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2014 return isl_basic_map_free(bmap);
2015 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2016 if (bmap->n_eq > 0)
2017 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2018 else {
2019 i = isl_basic_map_alloc_equality(bmap);
2020 if (i < 0)
2021 goto error;
2023 isl_int_set_si(bmap->eq[i][0], 1);
2024 isl_seq_clr(bmap->eq[i]+1, total);
2025 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2026 isl_vec_free(bmap->sample);
2027 bmap->sample = NULL;
2028 return isl_basic_map_finalize(bmap);
2029 error:
2030 isl_basic_map_free(bmap);
2031 return NULL;
2034 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2035 __isl_take isl_basic_set *bset)
2037 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2040 __isl_give isl_basic_map *isl_basic_map_set_rational(
2041 __isl_take isl_basic_map *bmap)
2043 if (!bmap)
2044 return NULL;
2046 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2047 return bmap;
2049 bmap = isl_basic_map_cow(bmap);
2050 if (!bmap)
2051 return NULL;
2053 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2055 return isl_basic_map_finalize(bmap);
2058 __isl_give isl_basic_set *isl_basic_set_set_rational(
2059 __isl_take isl_basic_set *bset)
2061 return isl_basic_map_set_rational(bset);
2064 __isl_give isl_basic_set *isl_basic_set_set_integral(
2065 __isl_take isl_basic_set *bset)
2067 if (!bset)
2068 return NULL;
2070 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2071 return bset;
2073 bset = isl_basic_set_cow(bset);
2074 if (!bset)
2075 return NULL;
2077 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2079 return isl_basic_set_finalize(bset);
2082 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2084 int i;
2086 map = isl_map_cow(map);
2087 if (!map)
2088 return NULL;
2089 for (i = 0; i < map->n; ++i) {
2090 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2091 if (!map->p[i])
2092 goto error;
2094 return map;
2095 error:
2096 isl_map_free(map);
2097 return NULL;
2100 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2102 return isl_map_set_rational(set);
2105 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2106 * of "bmap").
2108 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2110 isl_int *t = bmap->div[a];
2111 bmap->div[a] = bmap->div[b];
2112 bmap->div[b] = t;
2115 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2116 * div definitions accordingly.
2118 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2120 int i;
2121 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2123 swap_div(bmap, a, b);
2125 for (i = 0; i < bmap->n_eq; ++i)
2126 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2128 for (i = 0; i < bmap->n_ineq; ++i)
2129 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2131 for (i = 0; i < bmap->n_div; ++i)
2132 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2133 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2136 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2138 isl_seq_cpy(c, c + n, rem);
2139 isl_seq_clr(c + rem, n);
2142 /* Drop n dimensions starting at first.
2144 * In principle, this frees up some extra variables as the number
2145 * of columns remains constant, but we would have to extend
2146 * the div array too as the number of rows in this array is assumed
2147 * to be equal to extra.
2149 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2150 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2152 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2155 /* Move "n" divs starting at "first" to the end of the list of divs.
2157 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2158 unsigned first, unsigned n)
2160 isl_int **div;
2161 int i;
2163 if (first + n == bmap->n_div)
2164 return bmap;
2166 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2167 if (!div)
2168 goto error;
2169 for (i = 0; i < n; ++i)
2170 div[i] = bmap->div[first + i];
2171 for (i = 0; i < bmap->n_div - first - n; ++i)
2172 bmap->div[first + i] = bmap->div[first + n + i];
2173 for (i = 0; i < n; ++i)
2174 bmap->div[bmap->n_div - n + i] = div[i];
2175 free(div);
2176 return bmap;
2177 error:
2178 isl_basic_map_free(bmap);
2179 return NULL;
2182 #undef TYPE
2183 #define TYPE isl_map
2184 static
2185 #include "check_type_range_templ.c"
2187 /* Check that there are "n" dimensions of type "type" starting at "first"
2188 * in "set".
2190 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2191 enum isl_dim_type type, unsigned first, unsigned n)
2193 return isl_map_check_range(set_to_map(set), type, first, n);
2196 /* Drop "n" dimensions of type "type" starting at "first".
2197 * Perform the core computation, without cowing or
2198 * simplifying and finalizing the result.
2200 * In principle, this frees up some extra variables as the number
2201 * of columns remains constant, but we would have to extend
2202 * the div array too as the number of rows in this array is assumed
2203 * to be equal to extra.
2205 __isl_give isl_basic_map *isl_basic_map_drop_core(
2206 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2207 unsigned first, unsigned n)
2209 int i;
2210 unsigned offset;
2211 unsigned left;
2213 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2214 return isl_basic_map_free(bmap);
2216 offset = isl_basic_map_offset(bmap, type) + first;
2217 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2218 for (i = 0; i < bmap->n_eq; ++i)
2219 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2221 for (i = 0; i < bmap->n_ineq; ++i)
2222 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2224 for (i = 0; i < bmap->n_div; ++i)
2225 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2227 if (type == isl_dim_div) {
2228 bmap = move_divs_last(bmap, first, n);
2229 if (!bmap)
2230 return NULL;
2231 if (isl_basic_map_free_div(bmap, n) < 0)
2232 return isl_basic_map_free(bmap);
2233 } else
2234 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2235 if (!bmap->dim)
2236 return isl_basic_map_free(bmap);
2238 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2239 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2240 return bmap;
2243 /* Drop "n" dimensions of type "type" starting at "first".
2245 * In principle, this frees up some extra variables as the number
2246 * of columns remains constant, but we would have to extend
2247 * the div array too as the number of rows in this array is assumed
2248 * to be equal to extra.
2250 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2251 enum isl_dim_type type, unsigned first, unsigned n)
2253 if (!bmap)
2254 return NULL;
2255 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2256 return bmap;
2258 bmap = isl_basic_map_cow(bmap);
2259 if (!bmap)
2260 return NULL;
2262 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2264 bmap = isl_basic_map_simplify(bmap);
2265 return isl_basic_map_finalize(bmap);
2268 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2269 enum isl_dim_type type, unsigned first, unsigned n)
2271 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2272 type, first, n));
2275 /* No longer consider "map" to be normalized.
2277 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2279 if (!map)
2280 return NULL;
2281 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2282 return map;
2285 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2286 enum isl_dim_type type, unsigned first, unsigned n)
2288 int i;
2290 if (isl_map_check_range(map, type, first, n) < 0)
2291 return isl_map_free(map);
2293 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2294 return map;
2295 map = isl_map_cow(map);
2296 if (!map)
2297 goto error;
2298 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2299 if (!map->dim)
2300 goto error;
2302 for (i = 0; i < map->n; ++i) {
2303 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2304 if (!map->p[i])
2305 goto error;
2307 map = isl_map_unmark_normalized(map);
2309 return map;
2310 error:
2311 isl_map_free(map);
2312 return NULL;
2315 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2316 enum isl_dim_type type, unsigned first, unsigned n)
2318 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2321 /* Drop the integer division at position "div", which is assumed
2322 * not to appear in any of the constraints or
2323 * in any of the other integer divisions.
2325 * Since the integer division is redundant, there is no need to cow.
2327 __isl_give isl_basic_map *isl_basic_map_drop_div(
2328 __isl_take isl_basic_map *bmap, unsigned div)
2330 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2333 /* Eliminate the specified n dimensions starting at first from the
2334 * constraints, without removing the dimensions from the space.
2335 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2337 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2338 enum isl_dim_type type, unsigned first, unsigned n)
2340 int i;
2342 if (n == 0)
2343 return map;
2345 if (isl_map_check_range(map, type, first, n) < 0)
2346 return isl_map_free(map);
2348 map = isl_map_cow(map);
2349 if (!map)
2350 return NULL;
2352 for (i = 0; i < map->n; ++i) {
2353 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2354 if (!map->p[i])
2355 goto error;
2357 return map;
2358 error:
2359 isl_map_free(map);
2360 return NULL;
2363 /* Eliminate the specified n dimensions starting at first from the
2364 * constraints, without removing the dimensions from the space.
2365 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2367 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2368 enum isl_dim_type type, unsigned first, unsigned n)
2370 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2373 /* Eliminate the specified n dimensions starting at first from the
2374 * constraints, without removing the dimensions from the space.
2375 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2377 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2378 unsigned first, unsigned n)
2380 return isl_set_eliminate(set, isl_dim_set, first, n);
2383 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2384 __isl_take isl_basic_map *bmap)
2386 if (!bmap)
2387 return NULL;
2388 bmap = isl_basic_map_eliminate_vars(bmap,
2389 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2390 if (!bmap)
2391 return NULL;
2392 bmap->n_div = 0;
2393 return isl_basic_map_finalize(bmap);
2396 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2397 __isl_take isl_basic_set *bset)
2399 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2402 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2404 int i;
2406 if (!map)
2407 return NULL;
2408 if (map->n == 0)
2409 return map;
2411 map = isl_map_cow(map);
2412 if (!map)
2413 return NULL;
2415 for (i = 0; i < map->n; ++i) {
2416 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2417 if (!map->p[i])
2418 goto error;
2420 return map;
2421 error:
2422 isl_map_free(map);
2423 return NULL;
2426 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2428 return isl_map_remove_divs(set);
2431 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2432 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2433 unsigned first, unsigned n)
2435 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2436 return isl_basic_map_free(bmap);
2437 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2438 return bmap;
2439 bmap = isl_basic_map_eliminate_vars(bmap,
2440 isl_basic_map_offset(bmap, type) - 1 + first, n);
2441 if (!bmap)
2442 return bmap;
2443 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2444 return bmap;
2445 bmap = isl_basic_map_drop(bmap, type, first, n);
2446 return bmap;
2449 /* Return true if the definition of the given div (recursively) involves
2450 * any of the given variables.
2452 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2453 unsigned first, unsigned n)
2455 int i;
2456 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2458 if (isl_int_is_zero(bmap->div[div][0]))
2459 return isl_bool_false;
2460 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2461 return isl_bool_true;
2463 for (i = bmap->n_div - 1; i >= 0; --i) {
2464 isl_bool involves;
2466 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2467 continue;
2468 involves = div_involves_vars(bmap, i, first, n);
2469 if (involves < 0 || involves)
2470 return involves;
2473 return isl_bool_false;
2476 /* Try and add a lower and/or upper bound on "div" to "bmap"
2477 * based on inequality "i".
2478 * "total" is the total number of variables (excluding the divs).
2479 * "v" is a temporary object that can be used during the calculations.
2480 * If "lb" is set, then a lower bound should be constructed.
2481 * If "ub" is set, then an upper bound should be constructed.
2483 * The calling function has already checked that the inequality does not
2484 * reference "div", but we still need to check that the inequality is
2485 * of the right form. We'll consider the case where we want to construct
2486 * a lower bound. The construction of upper bounds is similar.
2488 * Let "div" be of the form
2490 * q = floor((a + f(x))/d)
2492 * We essentially check if constraint "i" is of the form
2494 * b + f(x) >= 0
2496 * so that we can use it to derive a lower bound on "div".
2497 * However, we allow a slightly more general form
2499 * b + g(x) >= 0
2501 * with the condition that the coefficients of g(x) - f(x) are all
2502 * divisible by d.
2503 * Rewriting this constraint as
2505 * 0 >= -b - g(x)
2507 * adding a + f(x) to both sides and dividing by d, we obtain
2509 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2511 * Taking the floor on both sides, we obtain
2513 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2515 * or
2517 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2519 * In the case of an upper bound, we construct the constraint
2521 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2524 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2525 __isl_take isl_basic_map *bmap, int div, int i,
2526 unsigned total, isl_int v, int lb, int ub)
2528 int j;
2530 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2531 if (lb) {
2532 isl_int_sub(v, bmap->ineq[i][1 + j],
2533 bmap->div[div][1 + 1 + j]);
2534 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2536 if (ub) {
2537 isl_int_add(v, bmap->ineq[i][1 + j],
2538 bmap->div[div][1 + 1 + j]);
2539 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2542 if (!lb && !ub)
2543 return bmap;
2545 bmap = isl_basic_map_cow(bmap);
2546 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2547 if (lb) {
2548 int k = isl_basic_map_alloc_inequality(bmap);
2549 if (k < 0)
2550 goto error;
2551 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2552 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2553 bmap->div[div][1 + j]);
2554 isl_int_cdiv_q(bmap->ineq[k][j],
2555 bmap->ineq[k][j], bmap->div[div][0]);
2557 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2559 if (ub) {
2560 int k = isl_basic_map_alloc_inequality(bmap);
2561 if (k < 0)
2562 goto error;
2563 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2564 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2565 bmap->div[div][1 + j]);
2566 isl_int_fdiv_q(bmap->ineq[k][j],
2567 bmap->ineq[k][j], bmap->div[div][0]);
2569 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2572 return bmap;
2573 error:
2574 isl_basic_map_free(bmap);
2575 return NULL;
2578 /* This function is called right before "div" is eliminated from "bmap"
2579 * using Fourier-Motzkin.
2580 * Look through the constraints of "bmap" for constraints on the argument
2581 * of the integer division and use them to construct constraints on the
2582 * integer division itself. These constraints can then be combined
2583 * during the Fourier-Motzkin elimination.
2584 * Note that it is only useful to introduce lower bounds on "div"
2585 * if "bmap" already contains upper bounds on "div" as the newly
2586 * introduce lower bounds can then be combined with the pre-existing
2587 * upper bounds. Similarly for upper bounds.
2588 * We therefore first check if "bmap" contains any lower and/or upper bounds
2589 * on "div".
2591 * It is interesting to note that the introduction of these constraints
2592 * can indeed lead to more accurate results, even when compared to
2593 * deriving constraints on the argument of "div" from constraints on "div".
2594 * Consider, for example, the set
2596 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2598 * The second constraint can be rewritten as
2600 * 2 * [(-i-2j+3)/4] + k >= 0
2602 * from which we can derive
2604 * -i - 2j + 3 >= -2k
2606 * or
2608 * i + 2j <= 3 + 2k
2610 * Combined with the first constraint, we obtain
2612 * -3 <= 3 + 2k or k >= -3
2614 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2615 * the first constraint, we obtain
2617 * [(i + 2j)/4] >= [-3/4] = -1
2619 * Combining this constraint with the second constraint, we obtain
2621 * k >= -2
2623 static __isl_give isl_basic_map *insert_bounds_on_div(
2624 __isl_take isl_basic_map *bmap, int div)
2626 int i;
2627 int check_lb, check_ub;
2628 isl_int v;
2629 unsigned total;
2631 if (!bmap)
2632 return NULL;
2634 if (isl_int_is_zero(bmap->div[div][0]))
2635 return bmap;
2637 total = isl_space_dim(bmap->dim, isl_dim_all);
2639 check_lb = 0;
2640 check_ub = 0;
2641 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2642 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2643 if (s > 0)
2644 check_ub = 1;
2645 if (s < 0)
2646 check_lb = 1;
2649 if (!check_lb && !check_ub)
2650 return bmap;
2652 isl_int_init(v);
2654 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2655 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2656 continue;
2658 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2659 check_lb, check_ub);
2662 isl_int_clear(v);
2664 return bmap;
2667 /* Remove all divs (recursively) involving any of the given dimensions
2668 * in their definitions.
2670 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2671 __isl_take isl_basic_map *bmap,
2672 enum isl_dim_type type, unsigned first, unsigned n)
2674 int i;
2676 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2677 return isl_basic_map_free(bmap);
2678 first += isl_basic_map_offset(bmap, type);
2680 for (i = bmap->n_div - 1; i >= 0; --i) {
2681 isl_bool involves;
2683 involves = div_involves_vars(bmap, i, first, n);
2684 if (involves < 0)
2685 return isl_basic_map_free(bmap);
2686 if (!involves)
2687 continue;
2688 bmap = insert_bounds_on_div(bmap, i);
2689 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2690 if (!bmap)
2691 return NULL;
2692 i = bmap->n_div;
2695 return bmap;
2698 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2699 __isl_take isl_basic_set *bset,
2700 enum isl_dim_type type, unsigned first, unsigned n)
2702 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2705 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2706 enum isl_dim_type type, unsigned first, unsigned n)
2708 int i;
2710 if (!map)
2711 return NULL;
2712 if (map->n == 0)
2713 return map;
2715 map = isl_map_cow(map);
2716 if (!map)
2717 return NULL;
2719 for (i = 0; i < map->n; ++i) {
2720 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2721 type, first, n);
2722 if (!map->p[i])
2723 goto error;
2725 return map;
2726 error:
2727 isl_map_free(map);
2728 return NULL;
2731 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2732 enum isl_dim_type type, unsigned first, unsigned n)
2734 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2735 type, first, n));
2738 /* Does the description of "bmap" depend on the specified dimensions?
2739 * We also check whether the dimensions appear in any of the div definitions.
2740 * In principle there is no need for this check. If the dimensions appear
2741 * in a div definition, they also appear in the defining constraints of that
2742 * div.
2744 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2745 enum isl_dim_type type, unsigned first, unsigned n)
2747 int i;
2749 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2750 return isl_bool_error;
2752 first += isl_basic_map_offset(bmap, type);
2753 for (i = 0; i < bmap->n_eq; ++i)
2754 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2755 return isl_bool_true;
2756 for (i = 0; i < bmap->n_ineq; ++i)
2757 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2758 return isl_bool_true;
2759 for (i = 0; i < bmap->n_div; ++i) {
2760 if (isl_int_is_zero(bmap->div[i][0]))
2761 continue;
2762 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2763 return isl_bool_true;
2766 return isl_bool_false;
2769 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2770 enum isl_dim_type type, unsigned first, unsigned n)
2772 int i;
2774 if (isl_map_check_range(map, type, first, n) < 0)
2775 return isl_bool_error;
2777 for (i = 0; i < map->n; ++i) {
2778 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2779 type, first, n);
2780 if (involves < 0 || involves)
2781 return involves;
2784 return isl_bool_false;
2787 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2788 enum isl_dim_type type, unsigned first, unsigned n)
2790 return isl_basic_map_involves_dims(bset, type, first, n);
2793 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2794 enum isl_dim_type type, unsigned first, unsigned n)
2796 return isl_map_involves_dims(set, type, first, n);
2799 /* Drop all constraints in bmap that involve any of the dimensions
2800 * first to first+n-1.
2801 * This function only performs the actual removal of constraints.
2803 * This function should not call finalize since it is used by
2804 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2806 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2807 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2809 int i;
2811 if (n == 0)
2812 return bmap;
2814 bmap = isl_basic_map_cow(bmap);
2816 if (!bmap)
2817 return NULL;
2819 for (i = bmap->n_eq - 1; i >= 0; --i) {
2820 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2821 continue;
2822 isl_basic_map_drop_equality(bmap, i);
2825 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2826 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2827 continue;
2828 isl_basic_map_drop_inequality(bmap, i);
2831 return bmap;
2834 /* Drop all constraints in bset that involve any of the dimensions
2835 * first to first+n-1.
2836 * This function only performs the actual removal of constraints.
2838 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2839 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2841 return isl_basic_map_drop_constraints_involving(bset, first, n);
2844 /* Drop all constraints in bmap that do not involve any of the dimensions
2845 * first to first + n - 1 of the given type.
2847 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2848 __isl_take isl_basic_map *bmap,
2849 enum isl_dim_type type, unsigned first, unsigned n)
2851 int i;
2853 if (n == 0) {
2854 isl_space *space = isl_basic_map_get_space(bmap);
2855 isl_basic_map_free(bmap);
2856 return isl_basic_map_universe(space);
2858 bmap = isl_basic_map_cow(bmap);
2859 if (!bmap)
2860 return NULL;
2862 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2863 return isl_basic_map_free(bmap);
2865 first += isl_basic_map_offset(bmap, type) - 1;
2867 for (i = bmap->n_eq - 1; i >= 0; --i) {
2868 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2869 continue;
2870 isl_basic_map_drop_equality(bmap, i);
2873 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2874 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2875 continue;
2876 isl_basic_map_drop_inequality(bmap, i);
2879 bmap = isl_basic_map_add_known_div_constraints(bmap);
2880 return bmap;
2883 /* Drop all constraints in bset that do not involve any of the dimensions
2884 * first to first + n - 1 of the given type.
2886 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2887 __isl_take isl_basic_set *bset,
2888 enum isl_dim_type type, unsigned first, unsigned n)
2890 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2891 type, first, n);
2894 /* Drop all constraints in bmap that involve any of the dimensions
2895 * first to first + n - 1 of the given type.
2897 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2898 __isl_take isl_basic_map *bmap,
2899 enum isl_dim_type type, unsigned first, unsigned n)
2901 if (!bmap)
2902 return NULL;
2903 if (n == 0)
2904 return bmap;
2906 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2907 return isl_basic_map_free(bmap);
2909 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2910 first += isl_basic_map_offset(bmap, type) - 1;
2911 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2912 bmap = isl_basic_map_add_known_div_constraints(bmap);
2913 return bmap;
2916 /* Drop all constraints in bset that involve any of the dimensions
2917 * first to first + n - 1 of the given type.
2919 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2920 __isl_take isl_basic_set *bset,
2921 enum isl_dim_type type, unsigned first, unsigned n)
2923 return isl_basic_map_drop_constraints_involving_dims(bset,
2924 type, first, n);
2927 /* Drop constraints from "map" by applying "drop" to each basic map.
2929 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2930 enum isl_dim_type type, unsigned first, unsigned n,
2931 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2932 enum isl_dim_type type, unsigned first, unsigned n))
2934 int i;
2936 if (isl_map_check_range(map, type, first, n) < 0)
2937 return isl_map_free(map);
2939 map = isl_map_cow(map);
2940 if (!map)
2941 return NULL;
2943 for (i = 0; i < map->n; ++i) {
2944 map->p[i] = drop(map->p[i], type, first, n);
2945 if (!map->p[i])
2946 return isl_map_free(map);
2949 if (map->n > 1)
2950 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2952 return map;
2955 /* Drop all constraints in map that involve any of the dimensions
2956 * first to first + n - 1 of the given type.
2958 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2959 __isl_take isl_map *map,
2960 enum isl_dim_type type, unsigned first, unsigned n)
2962 if (n == 0)
2963 return map;
2964 return drop_constraints(map, type, first, n,
2965 &isl_basic_map_drop_constraints_involving_dims);
2968 /* Drop all constraints in "map" that do not involve any of the dimensions
2969 * first to first + n - 1 of the given type.
2971 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2972 __isl_take isl_map *map,
2973 enum isl_dim_type type, unsigned first, unsigned n)
2975 if (n == 0) {
2976 isl_space *space = isl_map_get_space(map);
2977 isl_map_free(map);
2978 return isl_map_universe(space);
2980 return drop_constraints(map, type, first, n,
2981 &isl_basic_map_drop_constraints_not_involving_dims);
2984 /* Drop all constraints in set that involve any of the dimensions
2985 * first to first + n - 1 of the given type.
2987 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2988 __isl_take isl_set *set,
2989 enum isl_dim_type type, unsigned first, unsigned n)
2991 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2994 /* Drop all constraints in "set" that do not involve any of the dimensions
2995 * first to first + n - 1 of the given type.
2997 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2998 __isl_take isl_set *set,
2999 enum isl_dim_type type, unsigned first, unsigned n)
3001 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3004 /* Does local variable "div" of "bmap" have a complete explicit representation?
3005 * Having a complete explicit representation requires not only
3006 * an explicit representation, but also that all local variables
3007 * that appear in this explicit representation in turn have
3008 * a complete explicit representation.
3010 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3012 int i;
3013 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3014 isl_bool marked;
3016 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3017 if (marked < 0 || marked)
3018 return isl_bool_not(marked);
3020 for (i = bmap->n_div - 1; i >= 0; --i) {
3021 isl_bool known;
3023 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3024 continue;
3025 known = isl_basic_map_div_is_known(bmap, i);
3026 if (known < 0 || !known)
3027 return known;
3030 return isl_bool_true;
3033 /* Remove all divs that are unknown or defined in terms of unknown divs.
3035 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3036 __isl_take isl_basic_map *bmap)
3038 int i;
3040 if (!bmap)
3041 return NULL;
3043 for (i = bmap->n_div - 1; i >= 0; --i) {
3044 if (isl_basic_map_div_is_known(bmap, i))
3045 continue;
3046 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3047 if (!bmap)
3048 return NULL;
3049 i = bmap->n_div;
3052 return bmap;
3055 /* Remove all divs that are unknown or defined in terms of unknown divs.
3057 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3058 __isl_take isl_basic_set *bset)
3060 return isl_basic_map_remove_unknown_divs(bset);
3063 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3065 int i;
3067 if (!map)
3068 return NULL;
3069 if (map->n == 0)
3070 return map;
3072 map = isl_map_cow(map);
3073 if (!map)
3074 return NULL;
3076 for (i = 0; i < map->n; ++i) {
3077 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3078 if (!map->p[i])
3079 goto error;
3081 return map;
3082 error:
3083 isl_map_free(map);
3084 return NULL;
3087 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3089 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3092 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3093 __isl_take isl_basic_set *bset,
3094 enum isl_dim_type type, unsigned first, unsigned n)
3096 isl_basic_map *bmap = bset_to_bmap(bset);
3097 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3098 return bset_from_bmap(bmap);
3101 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3102 enum isl_dim_type type, unsigned first, unsigned n)
3104 int i;
3106 if (n == 0)
3107 return map;
3109 map = isl_map_cow(map);
3110 if (isl_map_check_range(map, type, first, n) < 0)
3111 return isl_map_free(map);
3113 for (i = 0; i < map->n; ++i) {
3114 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3115 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3116 if (!map->p[i])
3117 goto error;
3119 map = isl_map_drop(map, type, first, n);
3120 return map;
3121 error:
3122 isl_map_free(map);
3123 return NULL;
3126 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3127 enum isl_dim_type type, unsigned first, unsigned n)
3129 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3130 type, first, n));
3133 /* Project out n inputs starting at first using Fourier-Motzkin */
3134 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3135 unsigned first, unsigned n)
3137 return isl_map_remove_dims(map, isl_dim_in, first, n);
3140 static void dump_term(struct isl_basic_map *bmap,
3141 isl_int c, int pos, FILE *out)
3143 const char *name;
3144 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3145 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3146 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3147 if (!pos)
3148 isl_int_print(out, c, 0);
3149 else {
3150 if (!isl_int_is_one(c))
3151 isl_int_print(out, c, 0);
3152 if (pos < 1 + nparam) {
3153 name = isl_space_get_dim_name(bmap->dim,
3154 isl_dim_param, pos - 1);
3155 if (name)
3156 fprintf(out, "%s", name);
3157 else
3158 fprintf(out, "p%d", pos - 1);
3159 } else if (pos < 1 + nparam + in)
3160 fprintf(out, "i%d", pos - 1 - nparam);
3161 else if (pos < 1 + nparam + dim)
3162 fprintf(out, "o%d", pos - 1 - nparam - in);
3163 else
3164 fprintf(out, "e%d", pos - 1 - nparam - dim);
3168 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3169 int sign, FILE *out)
3171 int i;
3172 int first;
3173 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3174 isl_int v;
3176 isl_int_init(v);
3177 for (i = 0, first = 1; i < len; ++i) {
3178 if (isl_int_sgn(c[i]) * sign <= 0)
3179 continue;
3180 if (!first)
3181 fprintf(out, " + ");
3182 first = 0;
3183 isl_int_abs(v, c[i]);
3184 dump_term(bmap, v, i, out);
3186 isl_int_clear(v);
3187 if (first)
3188 fprintf(out, "0");
3191 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3192 const char *op, FILE *out, int indent)
3194 int i;
3196 fprintf(out, "%*s", indent, "");
3198 dump_constraint_sign(bmap, c, 1, out);
3199 fprintf(out, " %s ", op);
3200 dump_constraint_sign(bmap, c, -1, out);
3202 fprintf(out, "\n");
3204 for (i = bmap->n_div; i < bmap->extra; ++i) {
3205 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3206 continue;
3207 fprintf(out, "%*s", indent, "");
3208 fprintf(out, "ERROR: unused div coefficient not zero\n");
3209 abort();
3213 static void dump_constraints(struct isl_basic_map *bmap,
3214 isl_int **c, unsigned n,
3215 const char *op, FILE *out, int indent)
3217 int i;
3219 for (i = 0; i < n; ++i)
3220 dump_constraint(bmap, c[i], op, out, indent);
3223 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3225 int j;
3226 int first = 1;
3227 unsigned total = isl_basic_map_total_dim(bmap);
3229 for (j = 0; j < 1 + total; ++j) {
3230 if (isl_int_is_zero(exp[j]))
3231 continue;
3232 if (!first && isl_int_is_pos(exp[j]))
3233 fprintf(out, "+");
3234 dump_term(bmap, exp[j], j, out);
3235 first = 0;
3239 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3241 int i;
3243 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3244 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3246 for (i = 0; i < bmap->n_div; ++i) {
3247 fprintf(out, "%*s", indent, "");
3248 fprintf(out, "e%d = [(", i);
3249 dump_affine(bmap, bmap->div[i]+1, out);
3250 fprintf(out, ")/");
3251 isl_int_print(out, bmap->div[i][0], 0);
3252 fprintf(out, "]\n");
3256 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3257 FILE *out, int indent)
3259 if (!bset) {
3260 fprintf(out, "null basic set\n");
3261 return;
3264 fprintf(out, "%*s", indent, "");
3265 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3266 bset->ref, bset->dim->nparam, bset->dim->n_out,
3267 bset->extra, bset->flags);
3268 dump(bset_to_bmap(bset), out, indent);
3271 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3272 FILE *out, int indent)
3274 if (!bmap) {
3275 fprintf(out, "null basic map\n");
3276 return;
3279 fprintf(out, "%*s", indent, "");
3280 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3281 "flags: %x, n_name: %d\n",
3282 bmap->ref,
3283 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3284 bmap->extra, bmap->flags, bmap->dim->n_id);
3285 dump(bmap, out, indent);
3288 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3290 unsigned total;
3291 if (!bmap)
3292 return -1;
3293 total = isl_basic_map_total_dim(bmap);
3294 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3295 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3296 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3297 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3298 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3299 return 0;
3302 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3303 unsigned flags)
3305 if (isl_space_check_is_set(space) < 0)
3306 goto error;
3307 return isl_map_alloc_space(space, n, flags);
3308 error:
3309 isl_space_free(space);
3310 return NULL;
3313 /* Make sure "map" has room for at least "n" more basic maps.
3315 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3317 int i;
3318 struct isl_map *grown = NULL;
3320 if (!map)
3321 return NULL;
3322 isl_assert(map->ctx, n >= 0, goto error);
3323 if (map->n + n <= map->size)
3324 return map;
3325 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3326 if (!grown)
3327 goto error;
3328 for (i = 0; i < map->n; ++i) {
3329 grown->p[i] = isl_basic_map_copy(map->p[i]);
3330 if (!grown->p[i])
3331 goto error;
3332 grown->n++;
3334 isl_map_free(map);
3335 return grown;
3336 error:
3337 isl_map_free(grown);
3338 isl_map_free(map);
3339 return NULL;
3342 /* Make sure "set" has room for at least "n" more basic sets.
3344 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3346 return set_from_map(isl_map_grow(set_to_map(set), n));
3349 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3351 return isl_map_from_basic_map(bset);
3354 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3356 struct isl_map *map;
3358 if (!bmap)
3359 return NULL;
3361 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3362 return isl_map_add_basic_map(map, bmap);
3365 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3366 __isl_take isl_basic_set *bset)
3368 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3369 bset_to_bmap(bset)));
3372 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3374 return isl_map_free(set);
3377 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3379 int i;
3381 if (!set) {
3382 fprintf(out, "null set\n");
3383 return;
3386 fprintf(out, "%*s", indent, "");
3387 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3388 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3389 set->flags);
3390 for (i = 0; i < set->n; ++i) {
3391 fprintf(out, "%*s", indent, "");
3392 fprintf(out, "basic set %d:\n", i);
3393 isl_basic_set_print_internal(set->p[i], out, indent+4);
3397 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3399 int i;
3401 if (!map) {
3402 fprintf(out, "null map\n");
3403 return;
3406 fprintf(out, "%*s", indent, "");
3407 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3408 "flags: %x, n_name: %d\n",
3409 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3410 map->dim->n_out, map->flags, map->dim->n_id);
3411 for (i = 0; i < map->n; ++i) {
3412 fprintf(out, "%*s", indent, "");
3413 fprintf(out, "basic map %d:\n", i);
3414 isl_basic_map_print_internal(map->p[i], out, indent+4);
3418 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3419 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3421 struct isl_basic_map *bmap_domain;
3423 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3424 goto error;
3426 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3427 isl_assert(bset->ctx,
3428 isl_basic_map_compatible_domain(bmap, bset), goto error);
3430 bmap = isl_basic_map_cow(bmap);
3431 if (!bmap)
3432 goto error;
3433 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3434 bset->n_div, bset->n_eq, bset->n_ineq);
3435 bmap_domain = isl_basic_map_from_domain(bset);
3436 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3438 bmap = isl_basic_map_simplify(bmap);
3439 return isl_basic_map_finalize(bmap);
3440 error:
3441 isl_basic_map_free(bmap);
3442 isl_basic_set_free(bset);
3443 return NULL;
3446 /* Check that the space of "bset" is the same as that of the range of "bmap".
3448 static isl_stat isl_basic_map_check_compatible_range(
3449 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3451 isl_bool ok;
3453 ok = isl_basic_map_compatible_range(bmap, bset);
3454 if (ok < 0)
3455 return isl_stat_error;
3456 if (!ok)
3457 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3458 "incompatible spaces", return isl_stat_error);
3460 return isl_stat_ok;
3463 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3464 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3466 struct isl_basic_map *bmap_range;
3468 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3469 goto error;
3471 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3472 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3473 goto error;
3475 if (isl_basic_set_plain_is_universe(bset)) {
3476 isl_basic_set_free(bset);
3477 return bmap;
3480 bmap = isl_basic_map_cow(bmap);
3481 if (!bmap)
3482 goto error;
3483 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3484 bset->n_div, bset->n_eq, bset->n_ineq);
3485 bmap_range = bset_to_bmap(bset);
3486 bmap = add_constraints(bmap, bmap_range, 0, 0);
3488 bmap = isl_basic_map_simplify(bmap);
3489 return isl_basic_map_finalize(bmap);
3490 error:
3491 isl_basic_map_free(bmap);
3492 isl_basic_set_free(bset);
3493 return NULL;
3496 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3497 __isl_keep isl_vec *vec)
3499 int i;
3500 unsigned total;
3501 isl_int s;
3503 if (!bmap || !vec)
3504 return isl_bool_error;
3506 total = 1 + isl_basic_map_total_dim(bmap);
3507 if (total != vec->size)
3508 return isl_bool_false;
3510 isl_int_init(s);
3512 for (i = 0; i < bmap->n_eq; ++i) {
3513 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3514 if (!isl_int_is_zero(s)) {
3515 isl_int_clear(s);
3516 return isl_bool_false;
3520 for (i = 0; i < bmap->n_ineq; ++i) {
3521 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3522 if (isl_int_is_neg(s)) {
3523 isl_int_clear(s);
3524 return isl_bool_false;
3528 isl_int_clear(s);
3530 return isl_bool_true;
3533 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3534 __isl_keep isl_vec *vec)
3536 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3539 __isl_give isl_basic_map *isl_basic_map_intersect(
3540 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3542 struct isl_vec *sample = NULL;
3544 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3545 goto error;
3546 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3547 isl_space_dim(bmap1->dim, isl_dim_param) &&
3548 isl_space_dim(bmap2->dim, isl_dim_all) !=
3549 isl_space_dim(bmap2->dim, isl_dim_param))
3550 return isl_basic_map_intersect(bmap2, bmap1);
3552 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3553 isl_space_dim(bmap2->dim, isl_dim_param))
3554 isl_assert(bmap1->ctx,
3555 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3557 if (isl_basic_map_plain_is_empty(bmap1)) {
3558 isl_basic_map_free(bmap2);
3559 return bmap1;
3561 if (isl_basic_map_plain_is_empty(bmap2)) {
3562 isl_basic_map_free(bmap1);
3563 return bmap2;
3566 if (bmap1->sample &&
3567 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3568 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3569 sample = isl_vec_copy(bmap1->sample);
3570 else if (bmap2->sample &&
3571 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3572 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3573 sample = isl_vec_copy(bmap2->sample);
3575 bmap1 = isl_basic_map_cow(bmap1);
3576 if (!bmap1)
3577 goto error;
3578 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3579 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3580 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3582 if (!bmap1)
3583 isl_vec_free(sample);
3584 else if (sample) {
3585 isl_vec_free(bmap1->sample);
3586 bmap1->sample = sample;
3589 bmap1 = isl_basic_map_simplify(bmap1);
3590 return isl_basic_map_finalize(bmap1);
3591 error:
3592 if (sample)
3593 isl_vec_free(sample);
3594 isl_basic_map_free(bmap1);
3595 isl_basic_map_free(bmap2);
3596 return NULL;
3599 struct isl_basic_set *isl_basic_set_intersect(
3600 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3602 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3603 bset_to_bmap(bset2)));
3606 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3607 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3609 return isl_basic_set_intersect(bset1, bset2);
3612 /* Special case of isl_map_intersect, where both map1 and map2
3613 * are convex, without any divs and such that either map1 or map2
3614 * contains a single constraint. This constraint is then simply
3615 * added to the other map.
3617 static __isl_give isl_map *map_intersect_add_constraint(
3618 __isl_take isl_map *map1, __isl_take isl_map *map2)
3620 isl_assert(map1->ctx, map1->n == 1, goto error);
3621 isl_assert(map2->ctx, map1->n == 1, goto error);
3622 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3623 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3625 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3626 return isl_map_intersect(map2, map1);
3628 map1 = isl_map_cow(map1);
3629 if (!map1)
3630 goto error;
3631 if (isl_map_plain_is_empty(map1)) {
3632 isl_map_free(map2);
3633 return map1;
3635 if (map2->p[0]->n_eq == 1)
3636 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3637 else
3638 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3639 map2->p[0]->ineq[0]);
3641 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3642 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3643 if (!map1->p[0])
3644 goto error;
3646 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3647 isl_basic_map_free(map1->p[0]);
3648 map1->n = 0;
3651 isl_map_free(map2);
3653 map1 = isl_map_unmark_normalized(map1);
3654 return map1;
3655 error:
3656 isl_map_free(map1);
3657 isl_map_free(map2);
3658 return NULL;
3661 /* map2 may be either a parameter domain or a map living in the same
3662 * space as map1.
3664 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3665 __isl_take isl_map *map2)
3667 unsigned flags = 0;
3668 isl_bool equal;
3669 isl_map *result;
3670 int i, j;
3672 if (!map1 || !map2)
3673 goto error;
3675 if ((isl_map_plain_is_empty(map1) ||
3676 isl_map_plain_is_universe(map2)) &&
3677 isl_space_is_equal(map1->dim, map2->dim)) {
3678 isl_map_free(map2);
3679 return map1;
3681 if ((isl_map_plain_is_empty(map2) ||
3682 isl_map_plain_is_universe(map1)) &&
3683 isl_space_is_equal(map1->dim, map2->dim)) {
3684 isl_map_free(map1);
3685 return map2;
3688 if (map1->n == 1 && map2->n == 1 &&
3689 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3690 isl_space_is_equal(map1->dim, map2->dim) &&
3691 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3692 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3693 return map_intersect_add_constraint(map1, map2);
3695 equal = isl_map_plain_is_equal(map1, map2);
3696 if (equal < 0)
3697 goto error;
3698 if (equal) {
3699 isl_map_free(map2);
3700 return map1;
3703 if (isl_space_dim(map2->dim, isl_dim_all) !=
3704 isl_space_dim(map2->dim, isl_dim_param))
3705 isl_assert(map1->ctx,
3706 isl_space_is_equal(map1->dim, map2->dim), goto error);
3708 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3709 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3710 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3712 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3713 map1->n * map2->n, flags);
3714 if (!result)
3715 goto error;
3716 for (i = 0; i < map1->n; ++i)
3717 for (j = 0; j < map2->n; ++j) {
3718 struct isl_basic_map *part;
3719 part = isl_basic_map_intersect(
3720 isl_basic_map_copy(map1->p[i]),
3721 isl_basic_map_copy(map2->p[j]));
3722 if (isl_basic_map_is_empty(part) < 0)
3723 part = isl_basic_map_free(part);
3724 result = isl_map_add_basic_map(result, part);
3725 if (!result)
3726 goto error;
3728 isl_map_free(map1);
3729 isl_map_free(map2);
3730 return result;
3731 error:
3732 isl_map_free(map1);
3733 isl_map_free(map2);
3734 return NULL;
3737 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3738 __isl_take isl_map *map2)
3740 if (!map1 || !map2)
3741 goto error;
3742 if (!isl_space_is_equal(map1->dim, map2->dim))
3743 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3744 "spaces don't match", goto error);
3745 return map_intersect_internal(map1, map2);
3746 error:
3747 isl_map_free(map1);
3748 isl_map_free(map2);
3749 return NULL;
3752 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3753 __isl_take isl_map *map2)
3755 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3758 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3760 return set_from_map(isl_map_intersect(set_to_map(set1),
3761 set_to_map(set2)));
3764 /* map_intersect_internal accepts intersections
3765 * with parameter domains, so we can just call that function.
3767 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3768 __isl_take isl_set *params)
3770 return map_intersect_internal(map, params);
3773 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3774 __isl_take isl_map *map2)
3776 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3779 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3780 __isl_take isl_set *params)
3782 return isl_map_intersect_params(set, params);
3785 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3787 isl_space *space;
3788 unsigned pos, n1, n2;
3790 if (!bmap)
3791 return NULL;
3792 bmap = isl_basic_map_cow(bmap);
3793 if (!bmap)
3794 return NULL;
3795 space = isl_space_reverse(isl_space_copy(bmap->dim));
3796 pos = isl_basic_map_offset(bmap, isl_dim_in);
3797 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3798 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3799 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3800 return isl_basic_map_reset_space(bmap, space);
3803 static __isl_give isl_basic_map *basic_map_space_reset(
3804 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3806 isl_space *space;
3808 if (!bmap)
3809 return NULL;
3810 if (!isl_space_is_named_or_nested(bmap->dim, type))
3811 return bmap;
3813 space = isl_basic_map_get_space(bmap);
3814 space = isl_space_reset(space, type);
3815 bmap = isl_basic_map_reset_space(bmap, space);
3816 return bmap;
3819 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3820 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3821 unsigned pos, unsigned n)
3823 isl_bool rational;
3824 isl_space *res_space;
3825 struct isl_basic_map *res;
3826 struct isl_dim_map *dim_map;
3827 unsigned total, off;
3828 enum isl_dim_type t;
3830 if (n == 0)
3831 return basic_map_space_reset(bmap, type);
3833 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3834 type, pos, n);
3835 if (!res_space)
3836 return isl_basic_map_free(bmap);
3838 total = isl_basic_map_total_dim(bmap) + n;
3839 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3840 off = 0;
3841 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3842 if (t != type) {
3843 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3844 } else {
3845 unsigned size = isl_basic_map_dim(bmap, t);
3846 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3847 0, pos, off);
3848 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3849 pos, size - pos, off + pos + n);
3851 off += isl_space_dim(res_space, t);
3853 isl_dim_map_div(dim_map, bmap, off);
3855 res = isl_basic_map_alloc_space(res_space,
3856 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3857 rational = isl_basic_map_is_rational(bmap);
3858 if (rational < 0)
3859 res = isl_basic_map_free(res);
3860 if (rational)
3861 res = isl_basic_map_set_rational(res);
3862 if (isl_basic_map_plain_is_empty(bmap)) {
3863 isl_basic_map_free(bmap);
3864 free(dim_map);
3865 return isl_basic_map_set_to_empty(res);
3867 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3868 return isl_basic_map_finalize(res);
3871 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3872 __isl_take isl_basic_set *bset,
3873 enum isl_dim_type type, unsigned pos, unsigned n)
3875 return isl_basic_map_insert_dims(bset, type, pos, n);
3878 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3879 enum isl_dim_type type, unsigned n)
3881 if (!bmap)
3882 return NULL;
3883 return isl_basic_map_insert_dims(bmap, type,
3884 isl_basic_map_dim(bmap, type), n);
3887 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3888 enum isl_dim_type type, unsigned n)
3890 if (!bset)
3891 return NULL;
3892 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3893 return isl_basic_map_add_dims(bset, type, n);
3894 error:
3895 isl_basic_set_free(bset);
3896 return NULL;
3899 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3900 enum isl_dim_type type)
3902 isl_space *space;
3904 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3905 return map;
3907 space = isl_map_get_space(map);
3908 space = isl_space_reset(space, type);
3909 map = isl_map_reset_space(map, space);
3910 return map;
3913 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3914 enum isl_dim_type type, unsigned pos, unsigned n)
3916 int i;
3918 if (n == 0)
3919 return map_space_reset(map, type);
3921 map = isl_map_cow(map);
3922 if (!map)
3923 return NULL;
3925 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3926 if (!map->dim)
3927 goto error;
3929 for (i = 0; i < map->n; ++i) {
3930 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3931 if (!map->p[i])
3932 goto error;
3935 return map;
3936 error:
3937 isl_map_free(map);
3938 return NULL;
3941 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3942 enum isl_dim_type type, unsigned pos, unsigned n)
3944 return isl_map_insert_dims(set, type, pos, n);
3947 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3948 enum isl_dim_type type, unsigned n)
3950 if (!map)
3951 return NULL;
3952 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3955 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3956 enum isl_dim_type type, unsigned n)
3958 if (!set)
3959 return NULL;
3960 isl_assert(set->ctx, type != isl_dim_in, goto error);
3961 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3962 error:
3963 isl_set_free(set);
3964 return NULL;
3967 __isl_give isl_basic_map *isl_basic_map_move_dims(
3968 __isl_take isl_basic_map *bmap,
3969 enum isl_dim_type dst_type, unsigned dst_pos,
3970 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3972 struct isl_dim_map *dim_map;
3973 struct isl_basic_map *res;
3974 enum isl_dim_type t;
3975 unsigned total, off;
3977 if (!bmap)
3978 return NULL;
3979 if (n == 0) {
3980 bmap = isl_basic_map_reset(bmap, src_type);
3981 bmap = isl_basic_map_reset(bmap, dst_type);
3982 return bmap;
3985 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3986 return isl_basic_map_free(bmap);
3988 if (dst_type == src_type && dst_pos == src_pos)
3989 return bmap;
3991 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3993 if (pos(bmap->dim, dst_type) + dst_pos ==
3994 pos(bmap->dim, src_type) + src_pos +
3995 ((src_type < dst_type) ? n : 0)) {
3996 bmap = isl_basic_map_cow(bmap);
3997 if (!bmap)
3998 return NULL;
4000 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4001 src_type, src_pos, n);
4002 if (!bmap->dim)
4003 goto error;
4005 bmap = isl_basic_map_finalize(bmap);
4007 return bmap;
4010 total = isl_basic_map_total_dim(bmap);
4011 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4013 off = 0;
4014 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4015 unsigned size = isl_space_dim(bmap->dim, t);
4016 if (t == dst_type) {
4017 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4018 0, dst_pos, off);
4019 off += dst_pos;
4020 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4021 src_pos, n, off);
4022 off += n;
4023 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4024 dst_pos, size - dst_pos, off);
4025 off += size - dst_pos;
4026 } else if (t == src_type) {
4027 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4028 0, src_pos, off);
4029 off += src_pos;
4030 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4031 src_pos + n, size - src_pos - n, off);
4032 off += size - src_pos - n;
4033 } else {
4034 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4035 off += size;
4038 isl_dim_map_div(dim_map, bmap, off);
4040 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4041 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4042 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4043 if (!bmap)
4044 goto error;
4046 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4047 src_type, src_pos, n);
4048 if (!bmap->dim)
4049 goto error;
4051 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4052 bmap = isl_basic_map_gauss(bmap, NULL);
4053 bmap = isl_basic_map_finalize(bmap);
4055 return bmap;
4056 error:
4057 isl_basic_map_free(bmap);
4058 return NULL;
4061 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4062 enum isl_dim_type dst_type, unsigned dst_pos,
4063 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4065 isl_basic_map *bmap = bset_to_bmap(bset);
4066 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4067 src_type, src_pos, n);
4068 return bset_from_bmap(bmap);
4071 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4072 enum isl_dim_type dst_type, unsigned dst_pos,
4073 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4075 if (!set)
4076 return NULL;
4077 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4078 return set_from_map(isl_map_move_dims(set_to_map(set),
4079 dst_type, dst_pos, src_type, src_pos, n));
4080 error:
4081 isl_set_free(set);
4082 return NULL;
4085 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4086 enum isl_dim_type dst_type, unsigned dst_pos,
4087 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4089 int i;
4091 if (n == 0) {
4092 map = isl_map_reset(map, src_type);
4093 map = isl_map_reset(map, dst_type);
4094 return map;
4097 if (isl_map_check_range(map, src_type, src_pos, n))
4098 return isl_map_free(map);
4100 if (dst_type == src_type && dst_pos == src_pos)
4101 return map;
4103 isl_assert(map->ctx, dst_type != src_type, goto error);
4105 map = isl_map_cow(map);
4106 if (!map)
4107 return NULL;
4109 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4110 if (!map->dim)
4111 goto error;
4113 for (i = 0; i < map->n; ++i) {
4114 map->p[i] = isl_basic_map_move_dims(map->p[i],
4115 dst_type, dst_pos,
4116 src_type, src_pos, n);
4117 if (!map->p[i])
4118 goto error;
4121 return map;
4122 error:
4123 isl_map_free(map);
4124 return NULL;
4127 /* Move the specified dimensions to the last columns right before
4128 * the divs. Don't change the dimension specification of bmap.
4129 * That's the responsibility of the caller.
4131 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4132 enum isl_dim_type type, unsigned first, unsigned n)
4134 struct isl_dim_map *dim_map;
4135 struct isl_basic_map *res;
4136 enum isl_dim_type t;
4137 unsigned total, off;
4139 if (!bmap)
4140 return NULL;
4141 if (pos(bmap->dim, type) + first + n ==
4142 1 + isl_space_dim(bmap->dim, isl_dim_all))
4143 return bmap;
4145 total = isl_basic_map_total_dim(bmap);
4146 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4148 off = 0;
4149 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4150 unsigned size = isl_space_dim(bmap->dim, t);
4151 if (t == type) {
4152 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4153 0, first, off);
4154 off += first;
4155 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4156 first, n, total - bmap->n_div - n);
4157 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4158 first + n, size - (first + n), off);
4159 off += size - (first + n);
4160 } else {
4161 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4162 off += size;
4165 isl_dim_map_div(dim_map, bmap, off + n);
4167 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4168 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4169 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4170 return res;
4173 /* Insert "n" rows in the divs of "bmap".
4175 * The number of columns is not changed, which means that the last
4176 * dimensions of "bmap" are being reintepreted as the new divs.
4177 * The space of "bmap" is not adjusted, however, which means
4178 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4179 * from the space of "bmap" is the responsibility of the caller.
4181 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4182 int n)
4184 int i;
4185 size_t row_size;
4186 isl_int **new_div;
4187 isl_int *old;
4189 bmap = isl_basic_map_cow(bmap);
4190 if (!bmap)
4191 return NULL;
4193 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4194 old = bmap->block2.data;
4195 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4196 (bmap->extra + n) * (1 + row_size));
4197 if (!bmap->block2.data)
4198 return isl_basic_map_free(bmap);
4199 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4200 if (!new_div)
4201 return isl_basic_map_free(bmap);
4202 for (i = 0; i < n; ++i) {
4203 new_div[i] = bmap->block2.data +
4204 (bmap->extra + i) * (1 + row_size);
4205 isl_seq_clr(new_div[i], 1 + row_size);
4207 for (i = 0; i < bmap->extra; ++i)
4208 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4209 free(bmap->div);
4210 bmap->div = new_div;
4211 bmap->n_div += n;
4212 bmap->extra += n;
4214 return bmap;
4217 /* Drop constraints from "bmap" that only involve the variables
4218 * of "type" in the range [first, first + n] that are not related
4219 * to any of the variables outside that interval.
4220 * These constraints cannot influence the values for the variables
4221 * outside the interval, except in case they cause "bmap" to be empty.
4222 * Only drop the constraints if "bmap" is known to be non-empty.
4224 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4225 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4226 unsigned first, unsigned n)
4228 int i;
4229 int *groups;
4230 unsigned dim, n_div;
4231 isl_bool non_empty;
4233 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4234 if (non_empty < 0)
4235 return isl_basic_map_free(bmap);
4236 if (!non_empty)
4237 return bmap;
4239 dim = isl_basic_map_dim(bmap, isl_dim_all);
4240 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4241 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4242 if (!groups)
4243 return isl_basic_map_free(bmap);
4244 first += isl_basic_map_offset(bmap, type) - 1;
4245 for (i = 0; i < first; ++i)
4246 groups[i] = -1;
4247 for (i = first + n; i < dim - n_div; ++i)
4248 groups[i] = -1;
4250 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4252 return bmap;
4255 /* Turn the n dimensions of type type, starting at first
4256 * into existentially quantified variables.
4258 * If a subset of the projected out variables are unrelated
4259 * to any of the variables that remain, then the constraints
4260 * involving this subset are simply dropped first.
4262 __isl_give isl_basic_map *isl_basic_map_project_out(
4263 __isl_take isl_basic_map *bmap,
4264 enum isl_dim_type type, unsigned first, unsigned n)
4266 isl_bool empty;
4268 if (n == 0)
4269 return basic_map_space_reset(bmap, type);
4270 if (type == isl_dim_div)
4271 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4272 "cannot project out existentially quantified variables",
4273 return isl_basic_map_free(bmap));
4275 empty = isl_basic_map_plain_is_empty(bmap);
4276 if (empty < 0)
4277 return isl_basic_map_free(bmap);
4278 if (empty)
4279 bmap = isl_basic_map_set_to_empty(bmap);
4281 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4282 if (!bmap)
4283 return NULL;
4285 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4286 return isl_basic_map_remove_dims(bmap, type, first, n);
4288 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4289 return isl_basic_map_free(bmap);
4291 bmap = move_last(bmap, type, first, n);
4292 bmap = isl_basic_map_cow(bmap);
4293 bmap = insert_div_rows(bmap, n);
4294 if (!bmap)
4295 return NULL;
4297 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4298 if (!bmap->dim)
4299 goto error;
4300 bmap = isl_basic_map_simplify(bmap);
4301 bmap = isl_basic_map_drop_redundant_divs(bmap);
4302 return isl_basic_map_finalize(bmap);
4303 error:
4304 isl_basic_map_free(bmap);
4305 return NULL;
4308 /* Turn the n dimensions of type type, starting at first
4309 * into existentially quantified variables.
4311 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4312 enum isl_dim_type type, unsigned first, unsigned n)
4314 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4315 type, first, n));
4318 /* Turn the n dimensions of type type, starting at first
4319 * into existentially quantified variables.
4321 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4322 enum isl_dim_type type, unsigned first, unsigned n)
4324 int i;
4326 if (n == 0)
4327 return map_space_reset(map, type);
4329 if (isl_map_check_range(map, type, first, n) < 0)
4330 return isl_map_free(map);
4332 map = isl_map_cow(map);
4333 if (!map)
4334 return NULL;
4336 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4337 if (!map->dim)
4338 goto error;
4340 for (i = 0; i < map->n; ++i) {
4341 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4342 if (!map->p[i])
4343 goto error;
4346 return map;
4347 error:
4348 isl_map_free(map);
4349 return NULL;
4352 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4353 * into existentially quantified variables.
4355 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4356 enum isl_dim_type type, unsigned first, unsigned n)
4358 unsigned dim;
4360 if (isl_map_check_range(map, type, first, n) < 0)
4361 return isl_map_free(map);
4362 dim = isl_map_dim(map, type);
4363 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4364 map = isl_map_project_out(map, type, 0, first);
4365 return map;
4368 /* Turn the n dimensions of type type, starting at first
4369 * into existentially quantified variables.
4371 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4372 enum isl_dim_type type, unsigned first, unsigned n)
4374 return set_from_map(isl_map_project_out(set_to_map(set),
4375 type, first, n));
4378 /* Return a map that projects the elements in "set" onto their
4379 * "n" set dimensions starting at "first".
4380 * "type" should be equal to isl_dim_set.
4382 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4383 enum isl_dim_type type, unsigned first, unsigned n)
4385 int i;
4386 isl_map *map;
4388 if (type != isl_dim_set)
4389 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4390 "only set dimensions can be projected out", goto error);
4391 if (isl_set_check_range(set, type, first, n) < 0)
4392 return isl_set_free(set);
4394 map = isl_map_from_domain(set);
4395 map = isl_map_add_dims(map, isl_dim_out, n);
4396 for (i = 0; i < n; ++i)
4397 map = isl_map_equate(map, isl_dim_in, first + i,
4398 isl_dim_out, i);
4399 return map;
4400 error:
4401 isl_set_free(set);
4402 return NULL;
4405 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4406 unsigned n)
4408 int i, j;
4410 for (i = 0; i < n; ++i) {
4411 j = isl_basic_map_alloc_div(bmap);
4412 if (j < 0)
4413 goto error;
4414 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4416 return bmap;
4417 error:
4418 isl_basic_map_free(bmap);
4419 return NULL;
4422 struct isl_basic_map *isl_basic_map_apply_range(
4423 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4425 isl_space *space_result = NULL;
4426 struct isl_basic_map *bmap;
4427 unsigned n_in, n_out, n, nparam, total, pos;
4428 struct isl_dim_map *dim_map1, *dim_map2;
4430 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4431 goto error;
4432 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4433 bmap2->dim, isl_dim_in))
4434 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4435 "spaces don't match", goto error);
4437 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4438 isl_space_copy(bmap2->dim));
4440 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4441 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4442 n = isl_basic_map_dim(bmap1, isl_dim_out);
4443 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4445 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4446 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4447 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4448 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4449 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4450 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4451 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4452 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4453 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4454 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4455 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4457 bmap = isl_basic_map_alloc_space(space_result,
4458 bmap1->n_div + bmap2->n_div + n,
4459 bmap1->n_eq + bmap2->n_eq,
4460 bmap1->n_ineq + bmap2->n_ineq);
4461 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4462 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4463 bmap = add_divs(bmap, n);
4464 bmap = isl_basic_map_simplify(bmap);
4465 bmap = isl_basic_map_drop_redundant_divs(bmap);
4466 return isl_basic_map_finalize(bmap);
4467 error:
4468 isl_basic_map_free(bmap1);
4469 isl_basic_map_free(bmap2);
4470 return NULL;
4473 struct isl_basic_set *isl_basic_set_apply(
4474 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4476 if (!bset || !bmap)
4477 goto error;
4479 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4480 goto error);
4482 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4483 bmap));
4484 error:
4485 isl_basic_set_free(bset);
4486 isl_basic_map_free(bmap);
4487 return NULL;
4490 struct isl_basic_map *isl_basic_map_apply_domain(
4491 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4493 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4494 goto error;
4495 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4496 bmap2->dim, isl_dim_in))
4497 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4498 "spaces don't match", goto error);
4500 bmap1 = isl_basic_map_reverse(bmap1);
4501 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4502 return isl_basic_map_reverse(bmap1);
4503 error:
4504 isl_basic_map_free(bmap1);
4505 isl_basic_map_free(bmap2);
4506 return NULL;
4509 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4510 * A \cap B -> f(A) + f(B)
4512 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4513 __isl_take isl_basic_map *bmap2)
4515 unsigned n_in, n_out, nparam, total, pos;
4516 struct isl_basic_map *bmap = NULL;
4517 struct isl_dim_map *dim_map1, *dim_map2;
4518 int i;
4520 if (!bmap1 || !bmap2)
4521 goto error;
4523 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4524 goto error);
4526 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4527 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4528 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4530 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4531 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4532 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4533 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4534 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4535 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4536 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4537 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4538 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4539 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4540 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4542 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4543 bmap1->n_div + bmap2->n_div + 2 * n_out,
4544 bmap1->n_eq + bmap2->n_eq + n_out,
4545 bmap1->n_ineq + bmap2->n_ineq);
4546 for (i = 0; i < n_out; ++i) {
4547 int j = isl_basic_map_alloc_equality(bmap);
4548 if (j < 0)
4549 goto error;
4550 isl_seq_clr(bmap->eq[j], 1+total);
4551 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4552 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4553 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4555 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4556 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4557 bmap = add_divs(bmap, 2 * n_out);
4559 bmap = isl_basic_map_simplify(bmap);
4560 return isl_basic_map_finalize(bmap);
4561 error:
4562 isl_basic_map_free(bmap);
4563 isl_basic_map_free(bmap1);
4564 isl_basic_map_free(bmap2);
4565 return NULL;
4568 /* Given two maps A -> f(A) and B -> g(B), construct a map
4569 * A \cap B -> f(A) + f(B)
4571 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4572 __isl_take isl_map *map2)
4574 struct isl_map *result;
4575 int i, j;
4577 if (!map1 || !map2)
4578 goto error;
4580 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4582 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4583 map1->n * map2->n, 0);
4584 if (!result)
4585 goto error;
4586 for (i = 0; i < map1->n; ++i)
4587 for (j = 0; j < map2->n; ++j) {
4588 struct isl_basic_map *part;
4589 part = isl_basic_map_sum(
4590 isl_basic_map_copy(map1->p[i]),
4591 isl_basic_map_copy(map2->p[j]));
4592 if (isl_basic_map_is_empty(part))
4593 isl_basic_map_free(part);
4594 else
4595 result = isl_map_add_basic_map(result, part);
4596 if (!result)
4597 goto error;
4599 isl_map_free(map1);
4600 isl_map_free(map2);
4601 return result;
4602 error:
4603 isl_map_free(map1);
4604 isl_map_free(map2);
4605 return NULL;
4608 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4609 __isl_take isl_set *set2)
4611 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4614 /* Given a basic map A -> f(A), construct A -> -f(A).
4616 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4618 int i, j;
4619 unsigned off, n;
4621 bmap = isl_basic_map_cow(bmap);
4622 if (!bmap)
4623 return NULL;
4625 n = isl_basic_map_dim(bmap, isl_dim_out);
4626 off = isl_basic_map_offset(bmap, isl_dim_out);
4627 for (i = 0; i < bmap->n_eq; ++i)
4628 for (j = 0; j < n; ++j)
4629 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4630 for (i = 0; i < bmap->n_ineq; ++i)
4631 for (j = 0; j < n; ++j)
4632 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4633 for (i = 0; i < bmap->n_div; ++i)
4634 for (j = 0; j < n; ++j)
4635 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4636 bmap = isl_basic_map_gauss(bmap, NULL);
4637 return isl_basic_map_finalize(bmap);
4640 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4642 return isl_basic_map_neg(bset);
4645 /* Given a map A -> f(A), construct A -> -f(A).
4647 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4649 int i;
4651 map = isl_map_cow(map);
4652 if (!map)
4653 return NULL;
4655 for (i = 0; i < map->n; ++i) {
4656 map->p[i] = isl_basic_map_neg(map->p[i]);
4657 if (!map->p[i])
4658 goto error;
4661 return map;
4662 error:
4663 isl_map_free(map);
4664 return NULL;
4667 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4669 return set_from_map(isl_map_neg(set_to_map(set)));
4672 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4673 * A -> floor(f(A)/d).
4675 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4676 isl_int d)
4678 unsigned n_in, n_out, nparam, total, pos;
4679 struct isl_basic_map *result = NULL;
4680 struct isl_dim_map *dim_map;
4681 int i;
4683 if (!bmap)
4684 return NULL;
4686 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4687 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4688 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4690 total = nparam + n_in + n_out + bmap->n_div + n_out;
4691 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4692 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4693 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4694 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4695 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4697 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4698 bmap->n_div + n_out,
4699 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4700 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4701 result = add_divs(result, n_out);
4702 for (i = 0; i < n_out; ++i) {
4703 int j;
4704 j = isl_basic_map_alloc_inequality(result);
4705 if (j < 0)
4706 goto error;
4707 isl_seq_clr(result->ineq[j], 1+total);
4708 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4709 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4710 j = isl_basic_map_alloc_inequality(result);
4711 if (j < 0)
4712 goto error;
4713 isl_seq_clr(result->ineq[j], 1+total);
4714 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4715 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4716 isl_int_sub_ui(result->ineq[j][0], d, 1);
4719 result = isl_basic_map_simplify(result);
4720 return isl_basic_map_finalize(result);
4721 error:
4722 isl_basic_map_free(result);
4723 return NULL;
4726 /* Given a map A -> f(A) and an integer d, construct a map
4727 * A -> floor(f(A)/d).
4729 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4731 int i;
4733 map = isl_map_cow(map);
4734 if (!map)
4735 return NULL;
4737 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4738 for (i = 0; i < map->n; ++i) {
4739 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4740 if (!map->p[i])
4741 goto error;
4743 map = isl_map_unmark_normalized(map);
4745 return map;
4746 error:
4747 isl_map_free(map);
4748 return NULL;
4751 /* Given a map A -> f(A) and an integer d, construct a map
4752 * A -> floor(f(A)/d).
4754 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4755 __isl_take isl_val *d)
4757 if (!map || !d)
4758 goto error;
4759 if (!isl_val_is_int(d))
4760 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4761 "expecting integer denominator", goto error);
4762 map = isl_map_floordiv(map, d->n);
4763 isl_val_free(d);
4764 return map;
4765 error:
4766 isl_map_free(map);
4767 isl_val_free(d);
4768 return NULL;
4771 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4772 unsigned pos)
4774 int i;
4775 unsigned nparam;
4776 unsigned n_in;
4778 i = isl_basic_map_alloc_equality(bmap);
4779 if (i < 0)
4780 goto error;
4781 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4782 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4783 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4784 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4785 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4786 return isl_basic_map_finalize(bmap);
4787 error:
4788 isl_basic_map_free(bmap);
4789 return NULL;
4792 /* Add a constraint to "bmap" expressing i_pos < o_pos
4794 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4795 unsigned pos)
4797 int i;
4798 unsigned nparam;
4799 unsigned n_in;
4801 i = isl_basic_map_alloc_inequality(bmap);
4802 if (i < 0)
4803 goto error;
4804 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4805 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4806 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4807 isl_int_set_si(bmap->ineq[i][0], -1);
4808 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4809 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4810 return isl_basic_map_finalize(bmap);
4811 error:
4812 isl_basic_map_free(bmap);
4813 return NULL;
4816 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4818 static __isl_give isl_basic_map *var_less_or_equal(
4819 __isl_take isl_basic_map *bmap, unsigned pos)
4821 int i;
4822 unsigned nparam;
4823 unsigned n_in;
4825 i = isl_basic_map_alloc_inequality(bmap);
4826 if (i < 0)
4827 goto error;
4828 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4829 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4830 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4831 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4832 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4833 return isl_basic_map_finalize(bmap);
4834 error:
4835 isl_basic_map_free(bmap);
4836 return NULL;
4839 /* Add a constraint to "bmap" expressing i_pos > o_pos
4841 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4842 unsigned pos)
4844 int i;
4845 unsigned nparam;
4846 unsigned n_in;
4848 i = isl_basic_map_alloc_inequality(bmap);
4849 if (i < 0)
4850 goto error;
4851 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4852 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4853 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4854 isl_int_set_si(bmap->ineq[i][0], -1);
4855 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4856 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4857 return isl_basic_map_finalize(bmap);
4858 error:
4859 isl_basic_map_free(bmap);
4860 return NULL;
4863 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4865 static __isl_give isl_basic_map *var_more_or_equal(
4866 __isl_take isl_basic_map *bmap, unsigned pos)
4868 int i;
4869 unsigned nparam;
4870 unsigned n_in;
4872 i = isl_basic_map_alloc_inequality(bmap);
4873 if (i < 0)
4874 goto error;
4875 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4876 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4877 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4878 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4879 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4880 return isl_basic_map_finalize(bmap);
4881 error:
4882 isl_basic_map_free(bmap);
4883 return NULL;
4886 __isl_give isl_basic_map *isl_basic_map_equal(
4887 __isl_take isl_space *space, unsigned n_equal)
4889 int i;
4890 struct isl_basic_map *bmap;
4891 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4892 if (!bmap)
4893 return NULL;
4894 for (i = 0; i < n_equal && bmap; ++i)
4895 bmap = var_equal(bmap, i);
4896 return isl_basic_map_finalize(bmap);
4899 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4901 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4902 unsigned pos)
4904 int i;
4905 struct isl_basic_map *bmap;
4906 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4907 if (!bmap)
4908 return NULL;
4909 for (i = 0; i < pos && bmap; ++i)
4910 bmap = var_equal(bmap, i);
4911 if (bmap)
4912 bmap = var_less(bmap, pos);
4913 return isl_basic_map_finalize(bmap);
4916 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4918 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4919 __isl_take isl_space *space, unsigned pos)
4921 int i;
4922 isl_basic_map *bmap;
4924 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4925 for (i = 0; i < pos; ++i)
4926 bmap = var_equal(bmap, i);
4927 bmap = var_less_or_equal(bmap, pos);
4928 return isl_basic_map_finalize(bmap);
4931 /* Return a relation on "space" expressing i_pos > o_pos
4933 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4934 unsigned pos)
4936 int i;
4937 struct isl_basic_map *bmap;
4938 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4939 if (!bmap)
4940 return NULL;
4941 for (i = 0; i < pos && bmap; ++i)
4942 bmap = var_equal(bmap, i);
4943 if (bmap)
4944 bmap = var_more(bmap, pos);
4945 return isl_basic_map_finalize(bmap);
4948 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4950 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4951 __isl_take isl_space *space, unsigned pos)
4953 int i;
4954 isl_basic_map *bmap;
4956 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4957 for (i = 0; i < pos; ++i)
4958 bmap = var_equal(bmap, i);
4959 bmap = var_more_or_equal(bmap, pos);
4960 return isl_basic_map_finalize(bmap);
4963 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4964 unsigned n, int equal)
4966 struct isl_map *map;
4967 int i;
4969 if (n == 0 && equal)
4970 return isl_map_universe(space);
4972 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4974 for (i = 0; i + 1 < n; ++i)
4975 map = isl_map_add_basic_map(map,
4976 isl_basic_map_less_at(isl_space_copy(space), i));
4977 if (n > 0) {
4978 if (equal)
4979 map = isl_map_add_basic_map(map,
4980 isl_basic_map_less_or_equal_at(space, n - 1));
4981 else
4982 map = isl_map_add_basic_map(map,
4983 isl_basic_map_less_at(space, n - 1));
4984 } else
4985 isl_space_free(space);
4987 return map;
4990 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
4992 if (!space)
4993 return NULL;
4994 return map_lex_lte_first(space, space->n_out, equal);
4997 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4999 return map_lex_lte_first(dim, n, 0);
5002 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5004 return map_lex_lte_first(dim, n, 1);
5007 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5009 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5012 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5014 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5017 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5018 unsigned n, int equal)
5020 struct isl_map *map;
5021 int i;
5023 if (n == 0 && equal)
5024 return isl_map_universe(space);
5026 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5028 for (i = 0; i + 1 < n; ++i)
5029 map = isl_map_add_basic_map(map,
5030 isl_basic_map_more_at(isl_space_copy(space), i));
5031 if (n > 0) {
5032 if (equal)
5033 map = isl_map_add_basic_map(map,
5034 isl_basic_map_more_or_equal_at(space, n - 1));
5035 else
5036 map = isl_map_add_basic_map(map,
5037 isl_basic_map_more_at(space, n - 1));
5038 } else
5039 isl_space_free(space);
5041 return map;
5044 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5046 if (!space)
5047 return NULL;
5048 return map_lex_gte_first(space, space->n_out, equal);
5051 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5053 return map_lex_gte_first(dim, n, 0);
5056 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5058 return map_lex_gte_first(dim, n, 1);
5061 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5063 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5066 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5068 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5071 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5072 __isl_take isl_set *set2)
5074 isl_map *map;
5075 map = isl_map_lex_le(isl_set_get_space(set1));
5076 map = isl_map_intersect_domain(map, set1);
5077 map = isl_map_intersect_range(map, set2);
5078 return map;
5081 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5082 __isl_take isl_set *set2)
5084 isl_map *map;
5085 map = isl_map_lex_lt(isl_set_get_space(set1));
5086 map = isl_map_intersect_domain(map, set1);
5087 map = isl_map_intersect_range(map, set2);
5088 return map;
5091 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5092 __isl_take isl_set *set2)
5094 isl_map *map;
5095 map = isl_map_lex_ge(isl_set_get_space(set1));
5096 map = isl_map_intersect_domain(map, set1);
5097 map = isl_map_intersect_range(map, set2);
5098 return map;
5101 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5102 __isl_take isl_set *set2)
5104 isl_map *map;
5105 map = isl_map_lex_gt(isl_set_get_space(set1));
5106 map = isl_map_intersect_domain(map, set1);
5107 map = isl_map_intersect_range(map, set2);
5108 return map;
5111 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5112 __isl_take isl_map *map2)
5114 isl_map *map;
5115 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5116 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5117 map = isl_map_apply_range(map, isl_map_reverse(map2));
5118 return map;
5121 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5122 __isl_take isl_map *map2)
5124 isl_map *map;
5125 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5126 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5127 map = isl_map_apply_range(map, isl_map_reverse(map2));
5128 return map;
5131 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5132 __isl_take isl_map *map2)
5134 isl_map *map;
5135 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5136 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5137 map = isl_map_apply_range(map, isl_map_reverse(map2));
5138 return map;
5141 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5142 __isl_take isl_map *map2)
5144 isl_map *map;
5145 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5146 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5147 map = isl_map_apply_range(map, isl_map_reverse(map2));
5148 return map;
5151 /* For a div d = floor(f/m), add the constraint
5153 * f - m d >= 0
5155 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5156 unsigned pos, isl_int *div)
5158 int i;
5159 unsigned total = isl_basic_map_total_dim(bmap);
5161 i = isl_basic_map_alloc_inequality(bmap);
5162 if (i < 0)
5163 return isl_stat_error;
5164 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5165 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5167 return isl_stat_ok;
5170 /* For a div d = floor(f/m), add the constraint
5172 * -(f-(m-1)) + m d >= 0
5174 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5175 unsigned pos, isl_int *div)
5177 int i;
5178 unsigned total = isl_basic_map_total_dim(bmap);
5180 i = isl_basic_map_alloc_inequality(bmap);
5181 if (i < 0)
5182 return isl_stat_error;
5183 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5184 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5185 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5186 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5188 return isl_stat_ok;
5191 /* For a div d = floor(f/m), add the constraints
5193 * f - m d >= 0
5194 * -(f-(m-1)) + m d >= 0
5196 * Note that the second constraint is the negation of
5198 * f - m d >= m
5200 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5201 unsigned pos, isl_int *div)
5203 if (add_upper_div_constraint(bmap, pos, div) < 0)
5204 return -1;
5205 if (add_lower_div_constraint(bmap, pos, div) < 0)
5206 return -1;
5207 return 0;
5210 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5211 unsigned pos, isl_int *div)
5213 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5214 pos, div);
5217 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map *bmap,
5218 unsigned div)
5220 unsigned total = isl_basic_map_total_dim(bmap);
5221 unsigned div_pos = total - bmap->n_div + div;
5223 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5224 bmap->div[div]);
5227 /* For each known div d = floor(f/m), add the constraints
5229 * f - m d >= 0
5230 * -(f-(m-1)) + m d >= 0
5232 * Remove duplicate constraints in case of some these div constraints
5233 * already appear in "bmap".
5235 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5236 __isl_take isl_basic_map *bmap)
5238 unsigned n_div;
5240 if (!bmap)
5241 return NULL;
5242 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5243 if (n_div == 0)
5244 return bmap;
5246 bmap = add_known_div_constraints(bmap);
5247 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5248 bmap = isl_basic_map_finalize(bmap);
5249 return bmap;
5252 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5254 * In particular, if this div is of the form d = floor(f/m),
5255 * then add the constraint
5257 * f - m d >= 0
5259 * if sign < 0 or the constraint
5261 * -(f-(m-1)) + m d >= 0
5263 * if sign > 0.
5265 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5266 unsigned div, int sign)
5268 unsigned total;
5269 unsigned div_pos;
5271 if (!bmap)
5272 return -1;
5274 total = isl_basic_map_total_dim(bmap);
5275 div_pos = total - bmap->n_div + div;
5277 if (sign < 0)
5278 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5279 else
5280 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5283 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5284 __isl_take isl_basic_map *bmap)
5286 if (!bmap)
5287 goto error;
5288 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5289 bmap->n_div == 0 &&
5290 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5291 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5292 return bset_from_bmap(bmap);
5293 bmap = isl_basic_map_cow(bmap);
5294 if (!bmap)
5295 goto error;
5296 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5297 if (!bmap->dim)
5298 goto error;
5299 bmap->extra -= bmap->n_div;
5300 bmap->n_div = 0;
5301 bmap = isl_basic_map_finalize(bmap);
5302 return bset_from_bmap(bmap);
5303 error:
5304 isl_basic_map_free(bmap);
5305 return NULL;
5308 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5309 __isl_take isl_basic_set *bset)
5311 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5314 /* Replace each element in "list" by the result of applying
5315 * isl_basic_map_underlying_set to the element.
5317 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5318 __isl_take isl_basic_map_list *list)
5320 int i, n;
5322 if (!list)
5323 return NULL;
5325 n = isl_basic_map_list_n_basic_map(list);
5326 for (i = 0; i < n; ++i) {
5327 isl_basic_map *bmap;
5328 isl_basic_set *bset;
5330 bmap = isl_basic_map_list_get_basic_map(list, i);
5331 bset = isl_basic_set_underlying_set(bmap);
5332 list = isl_basic_set_list_set_basic_set(list, i, bset);
5335 return list;
5338 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5339 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5341 struct isl_basic_map *bmap;
5342 struct isl_ctx *ctx;
5343 unsigned total;
5344 int i;
5346 if (!bset || !like)
5347 goto error;
5348 ctx = bset->ctx;
5349 isl_assert(ctx, bset->n_div == 0, goto error);
5350 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5351 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5352 goto error);
5353 if (like->n_div == 0) {
5354 isl_space *space = isl_basic_map_get_space(like);
5355 isl_basic_map_free(like);
5356 return isl_basic_map_reset_space(bset, space);
5358 bset = isl_basic_set_cow(bset);
5359 if (!bset)
5360 goto error;
5361 total = bset->dim->n_out + bset->extra;
5362 bmap = bset_to_bmap(bset);
5363 isl_space_free(bmap->dim);
5364 bmap->dim = isl_space_copy(like->dim);
5365 if (!bmap->dim)
5366 goto error;
5367 bmap->n_div = like->n_div;
5368 bmap->extra += like->n_div;
5369 if (bmap->extra) {
5370 unsigned ltotal;
5371 isl_int **div;
5372 ltotal = total - bmap->extra + like->extra;
5373 if (ltotal > total)
5374 ltotal = total;
5375 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5376 bmap->extra * (1 + 1 + total));
5377 if (isl_blk_is_error(bmap->block2))
5378 goto error;
5379 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5380 if (!div)
5381 goto error;
5382 bmap->div = div;
5383 for (i = 0; i < bmap->extra; ++i)
5384 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5385 for (i = 0; i < like->n_div; ++i) {
5386 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5387 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5389 bmap = isl_basic_map_add_known_div_constraints(bmap);
5391 isl_basic_map_free(like);
5392 bmap = isl_basic_map_simplify(bmap);
5393 bmap = isl_basic_map_finalize(bmap);
5394 return bmap;
5395 error:
5396 isl_basic_map_free(like);
5397 isl_basic_set_free(bset);
5398 return NULL;
5401 struct isl_basic_set *isl_basic_set_from_underlying_set(
5402 struct isl_basic_set *bset, struct isl_basic_set *like)
5404 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5405 bset_to_bmap(like)));
5408 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5410 int i;
5412 map = isl_map_cow(map);
5413 if (!map)
5414 return NULL;
5415 map->dim = isl_space_cow(map->dim);
5416 if (!map->dim)
5417 goto error;
5419 for (i = 1; i < map->n; ++i)
5420 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5421 goto error);
5422 for (i = 0; i < map->n; ++i) {
5423 map->p[i] = bset_to_bmap(
5424 isl_basic_map_underlying_set(map->p[i]));
5425 if (!map->p[i])
5426 goto error;
5428 if (map->n == 0)
5429 map->dim = isl_space_underlying(map->dim, 0);
5430 else {
5431 isl_space_free(map->dim);
5432 map->dim = isl_space_copy(map->p[0]->dim);
5434 if (!map->dim)
5435 goto error;
5436 return set_from_map(map);
5437 error:
5438 isl_map_free(map);
5439 return NULL;
5442 /* Replace the space of "bmap" by "space".
5444 * If the space of "bmap" is identical to "space" (including the identifiers
5445 * of the input and output dimensions), then simply return the original input.
5447 __isl_give isl_basic_map *isl_basic_map_reset_space(
5448 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5450 isl_bool equal;
5451 isl_space *bmap_space;
5453 bmap_space = isl_basic_map_peek_space(bmap);
5454 equal = isl_space_is_equal(bmap_space, space);
5455 if (equal >= 0 && equal)
5456 equal = isl_space_has_equal_ids(bmap_space, space);
5457 if (equal < 0)
5458 goto error;
5459 if (equal) {
5460 isl_space_free(space);
5461 return bmap;
5463 bmap = isl_basic_map_cow(bmap);
5464 if (!bmap || !space)
5465 goto error;
5467 isl_space_free(bmap->dim);
5468 bmap->dim = space;
5470 bmap = isl_basic_map_finalize(bmap);
5472 return bmap;
5473 error:
5474 isl_basic_map_free(bmap);
5475 isl_space_free(space);
5476 return NULL;
5479 __isl_give isl_basic_set *isl_basic_set_reset_space(
5480 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5482 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5483 dim));
5486 /* Check that the total dimensions of "map" and "space" are the same.
5488 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5489 __isl_keep isl_space *space)
5491 unsigned dim1, dim2;
5493 if (!map || !space)
5494 return isl_stat_error;
5495 dim1 = isl_map_dim(map, isl_dim_all);
5496 dim2 = isl_space_dim(space, isl_dim_all);
5497 if (dim1 == dim2)
5498 return isl_stat_ok;
5499 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5500 "total dimensions do not match", return isl_stat_error);
5503 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5504 __isl_take isl_space *space)
5506 int i;
5508 map = isl_map_cow(map);
5509 if (!map || !space)
5510 goto error;
5512 for (i = 0; i < map->n; ++i) {
5513 map->p[i] = isl_basic_map_reset_space(map->p[i],
5514 isl_space_copy(space));
5515 if (!map->p[i])
5516 goto error;
5518 isl_space_free(map->dim);
5519 map->dim = space;
5521 return map;
5522 error:
5523 isl_map_free(map);
5524 isl_space_free(space);
5525 return NULL;
5528 /* Replace the space of "map" by "space", without modifying
5529 * the dimension of "map".
5531 * If the space of "map" is identical to "space" (including the identifiers
5532 * of the input and output dimensions), then simply return the original input.
5534 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5535 __isl_take isl_space *space)
5537 isl_bool equal;
5538 isl_space *map_space;
5540 map_space = isl_map_peek_space(map);
5541 equal = isl_space_is_equal(map_space, space);
5542 if (equal >= 0 && equal)
5543 equal = isl_space_has_equal_ids(map_space, space);
5544 if (equal < 0)
5545 goto error;
5546 if (equal) {
5547 isl_space_free(space);
5548 return map;
5550 if (check_map_space_equal_total_dim(map, space) < 0)
5551 goto error;
5552 return isl_map_reset_space(map, space);
5553 error:
5554 isl_map_free(map);
5555 isl_space_free(space);
5556 return NULL;
5559 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5560 __isl_take isl_space *dim)
5562 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5565 /* Compute the parameter domain of the given basic set.
5567 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5569 isl_bool is_params;
5570 isl_space *space;
5571 unsigned n;
5573 is_params = isl_basic_set_is_params(bset);
5574 if (is_params < 0)
5575 return isl_basic_set_free(bset);
5576 if (is_params)
5577 return bset;
5579 n = isl_basic_set_dim(bset, isl_dim_set);
5580 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5581 space = isl_basic_set_get_space(bset);
5582 space = isl_space_params(space);
5583 bset = isl_basic_set_reset_space(bset, space);
5584 return bset;
5587 /* Construct a zero-dimensional basic set with the given parameter domain.
5589 __isl_give isl_basic_set *isl_basic_set_from_params(
5590 __isl_take isl_basic_set *bset)
5592 isl_space *space;
5593 space = isl_basic_set_get_space(bset);
5594 space = isl_space_set_from_params(space);
5595 bset = isl_basic_set_reset_space(bset, space);
5596 return bset;
5599 /* Compute the parameter domain of the given set.
5601 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5603 isl_space *space;
5604 unsigned n;
5606 if (isl_set_is_params(set))
5607 return set;
5609 n = isl_set_dim(set, isl_dim_set);
5610 set = isl_set_project_out(set, isl_dim_set, 0, n);
5611 space = isl_set_get_space(set);
5612 space = isl_space_params(space);
5613 set = isl_set_reset_space(set, space);
5614 return set;
5617 /* Construct a zero-dimensional set with the given parameter domain.
5619 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5621 isl_space *space;
5622 space = isl_set_get_space(set);
5623 space = isl_space_set_from_params(space);
5624 set = isl_set_reset_space(set, space);
5625 return set;
5628 /* Compute the parameter domain of the given map.
5630 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5632 isl_space *space;
5633 unsigned n;
5635 n = isl_map_dim(map, isl_dim_in);
5636 map = isl_map_project_out(map, isl_dim_in, 0, n);
5637 n = isl_map_dim(map, isl_dim_out);
5638 map = isl_map_project_out(map, isl_dim_out, 0, n);
5639 space = isl_map_get_space(map);
5640 space = isl_space_params(space);
5641 map = isl_map_reset_space(map, space);
5642 return map;
5645 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5647 isl_space *space;
5648 unsigned n_out;
5650 if (!bmap)
5651 return NULL;
5652 space = isl_space_domain(isl_basic_map_get_space(bmap));
5654 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5655 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5657 return isl_basic_map_reset_space(bmap, space);
5660 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5662 if (!bmap)
5663 return isl_bool_error;
5664 return isl_space_may_be_set(bmap->dim);
5667 /* Is this basic map actually a set?
5668 * Users should never call this function. Outside of isl,
5669 * the type should indicate whether something is a set or a map.
5671 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5673 if (!bmap)
5674 return isl_bool_error;
5675 return isl_space_is_set(bmap->dim);
5678 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5680 isl_bool is_set;
5682 is_set = isl_basic_map_is_set(bmap);
5683 if (is_set < 0)
5684 goto error;
5685 if (is_set)
5686 return bmap;
5687 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5688 error:
5689 isl_basic_map_free(bmap);
5690 return NULL;
5693 __isl_give isl_basic_map *isl_basic_map_domain_map(
5694 __isl_take isl_basic_map *bmap)
5696 int i;
5697 isl_space *space;
5698 isl_basic_map *domain;
5699 int nparam, n_in, n_out;
5701 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5702 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5703 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5705 space = isl_basic_map_get_space(bmap);
5706 space = isl_space_from_range(isl_space_domain(space));
5707 domain = isl_basic_map_universe(space);
5709 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5710 bmap = isl_basic_map_apply_range(bmap, domain);
5711 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5713 for (i = 0; i < n_in; ++i)
5714 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5715 isl_dim_out, i);
5717 bmap = isl_basic_map_gauss(bmap, NULL);
5718 return isl_basic_map_finalize(bmap);
5721 __isl_give isl_basic_map *isl_basic_map_range_map(
5722 __isl_take isl_basic_map *bmap)
5724 int i;
5725 isl_space *space;
5726 isl_basic_map *range;
5727 int nparam, n_in, n_out;
5729 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5730 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5731 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5733 space = isl_basic_map_get_space(bmap);
5734 space = isl_space_from_range(isl_space_range(space));
5735 range = isl_basic_map_universe(space);
5737 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5738 bmap = isl_basic_map_apply_range(bmap, range);
5739 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5741 for (i = 0; i < n_out; ++i)
5742 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5743 isl_dim_out, i);
5745 bmap = isl_basic_map_gauss(bmap, NULL);
5746 return isl_basic_map_finalize(bmap);
5749 int isl_map_may_be_set(__isl_keep isl_map *map)
5751 if (!map)
5752 return -1;
5753 return isl_space_may_be_set(map->dim);
5756 /* Is this map actually a set?
5757 * Users should never call this function. Outside of isl,
5758 * the type should indicate whether something is a set or a map.
5760 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5762 if (!map)
5763 return isl_bool_error;
5764 return isl_space_is_set(map->dim);
5767 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5769 int i;
5770 isl_bool is_set;
5771 struct isl_set *set;
5773 is_set = isl_map_is_set(map);
5774 if (is_set < 0)
5775 goto error;
5776 if (is_set)
5777 return set_from_map(map);
5779 map = isl_map_cow(map);
5780 if (!map)
5781 goto error;
5783 set = set_from_map(map);
5784 set->dim = isl_space_range(set->dim);
5785 if (!set->dim)
5786 goto error;
5787 for (i = 0; i < map->n; ++i) {
5788 set->p[i] = isl_basic_map_range(map->p[i]);
5789 if (!set->p[i])
5790 goto error;
5792 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5793 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5794 return set;
5795 error:
5796 isl_map_free(map);
5797 return NULL;
5800 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5802 int i;
5804 map = isl_map_cow(map);
5805 if (!map)
5806 return NULL;
5808 map->dim = isl_space_domain_map(map->dim);
5809 if (!map->dim)
5810 goto error;
5811 for (i = 0; i < map->n; ++i) {
5812 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5813 if (!map->p[i])
5814 goto error;
5816 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5817 map = isl_map_unmark_normalized(map);
5818 return map;
5819 error:
5820 isl_map_free(map);
5821 return NULL;
5824 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5826 int i;
5827 isl_space *range_dim;
5829 map = isl_map_cow(map);
5830 if (!map)
5831 return NULL;
5833 range_dim = isl_space_range(isl_map_get_space(map));
5834 range_dim = isl_space_from_range(range_dim);
5835 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5836 map->dim = isl_space_join(map->dim, range_dim);
5837 if (!map->dim)
5838 goto error;
5839 for (i = 0; i < map->n; ++i) {
5840 map->p[i] = isl_basic_map_range_map(map->p[i]);
5841 if (!map->p[i])
5842 goto error;
5844 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5845 map = isl_map_unmark_normalized(map);
5846 return map;
5847 error:
5848 isl_map_free(map);
5849 return NULL;
5852 /* Given a wrapped map of the form A[B -> C],
5853 * return the map A[B -> C] -> B.
5855 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5857 isl_id *id;
5858 isl_map *map;
5860 if (!set)
5861 return NULL;
5862 if (!isl_set_has_tuple_id(set))
5863 return isl_map_domain_map(isl_set_unwrap(set));
5865 id = isl_set_get_tuple_id(set);
5866 map = isl_map_domain_map(isl_set_unwrap(set));
5867 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5869 return map;
5872 __isl_give isl_basic_map *isl_basic_map_from_domain(
5873 __isl_take isl_basic_set *bset)
5875 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5878 __isl_give isl_basic_map *isl_basic_map_from_range(
5879 __isl_take isl_basic_set *bset)
5881 isl_space *space;
5882 space = isl_basic_set_get_space(bset);
5883 space = isl_space_from_range(space);
5884 bset = isl_basic_set_reset_space(bset, space);
5885 return bset_to_bmap(bset);
5888 /* Create a relation with the given set as range.
5889 * The domain of the created relation is a zero-dimensional
5890 * flat anonymous space.
5892 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5894 isl_space *space;
5895 space = isl_set_get_space(set);
5896 space = isl_space_from_range(space);
5897 set = isl_set_reset_space(set, space);
5898 return set_to_map(set);
5901 /* Create a relation with the given set as domain.
5902 * The range of the created relation is a zero-dimensional
5903 * flat anonymous space.
5905 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5907 return isl_map_reverse(isl_map_from_range(set));
5910 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5911 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5913 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5916 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5917 __isl_take isl_set *range)
5919 return isl_map_apply_range(isl_map_reverse(domain), range);
5922 /* Return a newly allocated isl_map with given space and flags and
5923 * room for "n" basic maps.
5924 * Make sure that all cached information is cleared.
5926 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5927 unsigned flags)
5929 struct isl_map *map;
5931 if (!space)
5932 return NULL;
5933 if (n < 0)
5934 isl_die(space->ctx, isl_error_internal,
5935 "negative number of basic maps", goto error);
5936 map = isl_calloc(space->ctx, struct isl_map,
5937 sizeof(struct isl_map) +
5938 (n - 1) * sizeof(struct isl_basic_map *));
5939 if (!map)
5940 goto error;
5942 map->ctx = space->ctx;
5943 isl_ctx_ref(map->ctx);
5944 map->ref = 1;
5945 map->size = n;
5946 map->n = 0;
5947 map->dim = space;
5948 map->flags = flags;
5949 return map;
5950 error:
5951 isl_space_free(space);
5952 return NULL;
5955 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5957 struct isl_basic_map *bmap;
5958 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5959 bmap = isl_basic_map_set_to_empty(bmap);
5960 return bmap;
5963 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5965 struct isl_basic_set *bset;
5966 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5967 bset = isl_basic_set_set_to_empty(bset);
5968 return bset;
5971 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5973 struct isl_basic_map *bmap;
5974 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5975 bmap = isl_basic_map_finalize(bmap);
5976 return bmap;
5979 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5981 struct isl_basic_set *bset;
5982 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5983 bset = isl_basic_set_finalize(bset);
5984 return bset;
5987 __isl_give isl_basic_map *isl_basic_map_nat_universe(
5988 __isl_take isl_space *space)
5990 int i;
5991 unsigned total = isl_space_dim(space, isl_dim_all);
5992 isl_basic_map *bmap;
5994 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
5995 for (i = 0; i < total; ++i) {
5996 int k = isl_basic_map_alloc_inequality(bmap);
5997 if (k < 0)
5998 goto error;
5999 isl_seq_clr(bmap->ineq[k], 1 + total);
6000 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6002 return bmap;
6003 error:
6004 isl_basic_map_free(bmap);
6005 return NULL;
6008 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6009 __isl_take isl_space *space)
6011 return isl_basic_map_nat_universe(space);
6014 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6016 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6019 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6021 return isl_map_nat_universe(dim);
6024 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6026 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6029 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6031 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6034 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6036 struct isl_map *map;
6037 if (!space)
6038 return NULL;
6039 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6040 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6041 return map;
6044 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6046 struct isl_set *set;
6047 if (!space)
6048 return NULL;
6049 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6050 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6051 return set;
6054 struct isl_map *isl_map_dup(struct isl_map *map)
6056 int i;
6057 struct isl_map *dup;
6059 if (!map)
6060 return NULL;
6061 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6062 for (i = 0; i < map->n; ++i)
6063 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6064 return dup;
6067 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6068 __isl_take isl_basic_map *bmap)
6070 if (!bmap || !map)
6071 goto error;
6072 if (isl_basic_map_plain_is_empty(bmap)) {
6073 isl_basic_map_free(bmap);
6074 return map;
6076 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6077 isl_assert(map->ctx, map->n < map->size, goto error);
6078 map->p[map->n] = bmap;
6079 map->n++;
6080 map = isl_map_unmark_normalized(map);
6081 return map;
6082 error:
6083 if (map)
6084 isl_map_free(map);
6085 if (bmap)
6086 isl_basic_map_free(bmap);
6087 return NULL;
6090 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6092 int i;
6094 if (!map)
6095 return NULL;
6097 if (--map->ref > 0)
6098 return NULL;
6100 clear_caches(map);
6101 isl_ctx_deref(map->ctx);
6102 for (i = 0; i < map->n; ++i)
6103 isl_basic_map_free(map->p[i]);
6104 isl_space_free(map->dim);
6105 free(map);
6107 return NULL;
6110 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6111 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6113 int j;
6115 bmap = isl_basic_map_cow(bmap);
6116 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6117 j = isl_basic_map_alloc_equality(bmap);
6118 if (j < 0)
6119 goto error;
6120 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6121 isl_int_set_si(bmap->eq[j][pos], -1);
6122 isl_int_set_si(bmap->eq[j][0], value);
6123 bmap = isl_basic_map_simplify(bmap);
6124 return isl_basic_map_finalize(bmap);
6125 error:
6126 isl_basic_map_free(bmap);
6127 return NULL;
6130 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6131 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6133 int j;
6135 bmap = isl_basic_map_cow(bmap);
6136 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6137 j = isl_basic_map_alloc_equality(bmap);
6138 if (j < 0)
6139 goto error;
6140 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6141 isl_int_set_si(bmap->eq[j][pos], -1);
6142 isl_int_set(bmap->eq[j][0], value);
6143 bmap = isl_basic_map_simplify(bmap);
6144 return isl_basic_map_finalize(bmap);
6145 error:
6146 isl_basic_map_free(bmap);
6147 return NULL;
6150 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6151 enum isl_dim_type type, unsigned pos, int value)
6153 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6154 return isl_basic_map_free(bmap);
6155 return isl_basic_map_fix_pos_si(bmap,
6156 isl_basic_map_offset(bmap, type) + pos, value);
6159 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6160 enum isl_dim_type type, unsigned pos, isl_int value)
6162 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6163 return isl_basic_map_free(bmap);
6164 return isl_basic_map_fix_pos(bmap,
6165 isl_basic_map_offset(bmap, type) + pos, value);
6168 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6169 * to be equal to "v".
6171 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6172 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6174 if (!bmap || !v)
6175 goto error;
6176 if (!isl_val_is_int(v))
6177 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6178 "expecting integer value", goto error);
6179 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6180 goto error;
6181 pos += isl_basic_map_offset(bmap, type);
6182 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6183 isl_val_free(v);
6184 return bmap;
6185 error:
6186 isl_basic_map_free(bmap);
6187 isl_val_free(v);
6188 return NULL;
6191 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6192 * to be equal to "v".
6194 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6195 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6197 return isl_basic_map_fix_val(bset, type, pos, v);
6200 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6201 enum isl_dim_type type, unsigned pos, int value)
6203 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6204 type, pos, value));
6207 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6208 enum isl_dim_type type, unsigned pos, isl_int value)
6210 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6211 type, pos, value));
6214 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6215 unsigned input, int value)
6217 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6220 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6221 unsigned dim, int value)
6223 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6224 isl_dim_set, dim, value));
6227 /* Remove the basic map at position "i" from "map" if this basic map
6228 * is (obviously) empty.
6230 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6232 isl_bool empty;
6234 if (!map)
6235 return NULL;
6237 empty = isl_basic_map_plain_is_empty(map->p[i]);
6238 if (empty < 0)
6239 return isl_map_free(map);
6240 if (!empty)
6241 return map;
6243 isl_basic_map_free(map->p[i]);
6244 map->n--;
6245 if (i != map->n) {
6246 map->p[i] = map->p[map->n];
6247 map = isl_map_unmark_normalized(map);
6251 return map;
6254 /* Perform "fn" on each basic map of "map", where we may not be holding
6255 * the only reference to "map".
6256 * In particular, "fn" should be a semantics preserving operation
6257 * that we want to apply to all copies of "map". We therefore need
6258 * to be careful not to modify "map" in a way that breaks "map"
6259 * in case anything goes wrong.
6261 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6262 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6264 struct isl_basic_map *bmap;
6265 int i;
6267 if (!map)
6268 return NULL;
6270 for (i = map->n - 1; i >= 0; --i) {
6271 bmap = isl_basic_map_copy(map->p[i]);
6272 bmap = fn(bmap);
6273 if (!bmap)
6274 goto error;
6275 isl_basic_map_free(map->p[i]);
6276 map->p[i] = bmap;
6277 map = remove_if_empty(map, i);
6278 if (!map)
6279 return NULL;
6282 return map;
6283 error:
6284 isl_map_free(map);
6285 return NULL;
6288 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6289 enum isl_dim_type type, unsigned pos, int value)
6291 int i;
6293 map = isl_map_cow(map);
6294 if (isl_map_check_range(map, type, pos, 1) < 0)
6295 return isl_map_free(map);
6296 for (i = map->n - 1; i >= 0; --i) {
6297 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6298 map = remove_if_empty(map, i);
6299 if (!map)
6300 return NULL;
6302 map = isl_map_unmark_normalized(map);
6303 return map;
6306 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6307 enum isl_dim_type type, unsigned pos, int value)
6309 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6312 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6313 enum isl_dim_type type, unsigned pos, isl_int value)
6315 int i;
6317 map = isl_map_cow(map);
6318 if (isl_map_check_range(map, type, pos, 1) < 0)
6319 return isl_map_free(map);
6320 for (i = 0; i < map->n; ++i) {
6321 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6322 if (!map->p[i])
6323 goto error;
6325 map = isl_map_unmark_normalized(map);
6326 return map;
6327 error:
6328 isl_map_free(map);
6329 return NULL;
6332 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6333 enum isl_dim_type type, unsigned pos, isl_int value)
6335 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6338 /* Fix the value of the variable at position "pos" of type "type" of "map"
6339 * to be equal to "v".
6341 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6342 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6344 int i;
6346 map = isl_map_cow(map);
6347 if (!map || !v)
6348 goto error;
6350 if (!isl_val_is_int(v))
6351 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6352 "expecting integer value", goto error);
6353 if (isl_map_check_range(map, type, pos, 1) < 0)
6354 goto error;
6355 for (i = map->n - 1; i >= 0; --i) {
6356 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6357 isl_val_copy(v));
6358 map = remove_if_empty(map, i);
6359 if (!map)
6360 goto error;
6362 map = isl_map_unmark_normalized(map);
6363 isl_val_free(v);
6364 return map;
6365 error:
6366 isl_map_free(map);
6367 isl_val_free(v);
6368 return NULL;
6371 /* Fix the value of the variable at position "pos" of type "type" of "set"
6372 * to be equal to "v".
6374 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6375 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6377 return isl_map_fix_val(set, type, pos, v);
6380 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6381 unsigned input, int value)
6383 return isl_map_fix_si(map, isl_dim_in, input, value);
6386 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6388 return set_from_map(isl_map_fix_si(set_to_map(set),
6389 isl_dim_set, dim, value));
6392 static __isl_give isl_basic_map *basic_map_bound_si(
6393 __isl_take isl_basic_map *bmap,
6394 enum isl_dim_type type, unsigned pos, int value, int upper)
6396 int j;
6398 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6399 return isl_basic_map_free(bmap);
6400 pos += isl_basic_map_offset(bmap, type);
6401 bmap = isl_basic_map_cow(bmap);
6402 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6403 j = isl_basic_map_alloc_inequality(bmap);
6404 if (j < 0)
6405 goto error;
6406 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6407 if (upper) {
6408 isl_int_set_si(bmap->ineq[j][pos], -1);
6409 isl_int_set_si(bmap->ineq[j][0], value);
6410 } else {
6411 isl_int_set_si(bmap->ineq[j][pos], 1);
6412 isl_int_set_si(bmap->ineq[j][0], -value);
6414 bmap = isl_basic_map_simplify(bmap);
6415 return isl_basic_map_finalize(bmap);
6416 error:
6417 isl_basic_map_free(bmap);
6418 return NULL;
6421 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6422 __isl_take isl_basic_map *bmap,
6423 enum isl_dim_type type, unsigned pos, int value)
6425 return basic_map_bound_si(bmap, type, pos, value, 0);
6428 /* Constrain the values of the given dimension to be no greater than "value".
6430 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6431 __isl_take isl_basic_map *bmap,
6432 enum isl_dim_type type, unsigned pos, int value)
6434 return basic_map_bound_si(bmap, type, pos, value, 1);
6437 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6438 enum isl_dim_type type, unsigned pos, int value, int upper)
6440 int i;
6442 map = isl_map_cow(map);
6443 if (isl_map_check_range(map, type, pos, 1) < 0)
6444 return isl_map_free(map);
6445 for (i = 0; i < map->n; ++i) {
6446 map->p[i] = basic_map_bound_si(map->p[i],
6447 type, pos, value, upper);
6448 if (!map->p[i])
6449 goto error;
6451 map = isl_map_unmark_normalized(map);
6452 return map;
6453 error:
6454 isl_map_free(map);
6455 return NULL;
6458 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6459 enum isl_dim_type type, unsigned pos, int value)
6461 return map_bound_si(map, type, pos, value, 0);
6464 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6465 enum isl_dim_type type, unsigned pos, int value)
6467 return map_bound_si(map, type, pos, value, 1);
6470 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6471 enum isl_dim_type type, unsigned pos, int value)
6473 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6474 type, pos, value));
6477 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6478 enum isl_dim_type type, unsigned pos, int value)
6480 return isl_map_upper_bound_si(set, type, pos, value);
6483 /* Bound the given variable of "bmap" from below (or above is "upper"
6484 * is set) to "value".
6486 static __isl_give isl_basic_map *basic_map_bound(
6487 __isl_take isl_basic_map *bmap,
6488 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6490 int j;
6492 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6493 return isl_basic_map_free(bmap);
6494 pos += isl_basic_map_offset(bmap, type);
6495 bmap = isl_basic_map_cow(bmap);
6496 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6497 j = isl_basic_map_alloc_inequality(bmap);
6498 if (j < 0)
6499 goto error;
6500 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6501 if (upper) {
6502 isl_int_set_si(bmap->ineq[j][pos], -1);
6503 isl_int_set(bmap->ineq[j][0], value);
6504 } else {
6505 isl_int_set_si(bmap->ineq[j][pos], 1);
6506 isl_int_neg(bmap->ineq[j][0], value);
6508 bmap = isl_basic_map_simplify(bmap);
6509 return isl_basic_map_finalize(bmap);
6510 error:
6511 isl_basic_map_free(bmap);
6512 return NULL;
6515 /* Bound the given variable of "map" from below (or above is "upper"
6516 * is set) to "value".
6518 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6519 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6521 int i;
6523 map = isl_map_cow(map);
6524 if (isl_map_check_range(map, type, pos, 1) < 0)
6525 return isl_map_free(map);
6526 for (i = map->n - 1; i >= 0; --i) {
6527 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6528 map = remove_if_empty(map, i);
6529 if (!map)
6530 return NULL;
6532 map = isl_map_unmark_normalized(map);
6533 return map;
6536 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6537 enum isl_dim_type type, unsigned pos, isl_int value)
6539 return map_bound(map, type, pos, value, 0);
6542 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6543 enum isl_dim_type type, unsigned pos, isl_int value)
6545 return map_bound(map, type, pos, value, 1);
6548 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6549 enum isl_dim_type type, unsigned pos, isl_int value)
6551 return isl_map_lower_bound(set, type, pos, value);
6554 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6555 enum isl_dim_type type, unsigned pos, isl_int value)
6557 return isl_map_upper_bound(set, type, pos, value);
6560 /* Force the values of the variable at position "pos" of type "type" of "set"
6561 * to be no smaller than "value".
6563 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6564 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6566 if (!value)
6567 goto error;
6568 if (!isl_val_is_int(value))
6569 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6570 "expecting integer value", goto error);
6571 set = isl_set_lower_bound(set, type, pos, value->n);
6572 isl_val_free(value);
6573 return set;
6574 error:
6575 isl_val_free(value);
6576 isl_set_free(set);
6577 return NULL;
6580 /* Force the values of the variable at position "pos" of type "type" of "set"
6581 * to be no greater than "value".
6583 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6584 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6586 if (!value)
6587 goto error;
6588 if (!isl_val_is_int(value))
6589 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6590 "expecting integer value", goto error);
6591 set = isl_set_upper_bound(set, type, pos, value->n);
6592 isl_val_free(value);
6593 return set;
6594 error:
6595 isl_val_free(value);
6596 isl_set_free(set);
6597 return NULL;
6600 /* Bound the given variable of "bset" from below (or above is "upper"
6601 * is set) to "value".
6603 static __isl_give isl_basic_set *isl_basic_set_bound(
6604 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6605 isl_int value, int upper)
6607 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6608 type, pos, value, upper));
6611 /* Bound the given variable of "bset" from below (or above is "upper"
6612 * is set) to "value".
6614 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6615 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6616 __isl_take isl_val *value, int upper)
6618 if (!value)
6619 goto error;
6620 if (!isl_val_is_int(value))
6621 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6622 "expecting integer value", goto error);
6623 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6624 isl_val_free(value);
6625 return bset;
6626 error:
6627 isl_val_free(value);
6628 isl_basic_set_free(bset);
6629 return NULL;
6632 /* Bound the given variable of "bset" from below to "value".
6634 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6635 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6636 __isl_take isl_val *value)
6638 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6641 /* Bound the given variable of "bset" from above to "value".
6643 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6644 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6645 __isl_take isl_val *value)
6647 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6650 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6652 int i;
6654 map = isl_map_cow(map);
6655 if (!map)
6656 return NULL;
6658 map->dim = isl_space_reverse(map->dim);
6659 if (!map->dim)
6660 goto error;
6661 for (i = 0; i < map->n; ++i) {
6662 map->p[i] = isl_basic_map_reverse(map->p[i]);
6663 if (!map->p[i])
6664 goto error;
6666 map = isl_map_unmark_normalized(map);
6667 return map;
6668 error:
6669 isl_map_free(map);
6670 return NULL;
6673 #undef TYPE
6674 #define TYPE isl_pw_multi_aff
6675 #undef SUFFIX
6676 #define SUFFIX _pw_multi_aff
6677 #undef EMPTY
6678 #define EMPTY isl_pw_multi_aff_empty
6679 #undef ADD
6680 #define ADD isl_pw_multi_aff_union_add
6681 #include "isl_map_lexopt_templ.c"
6683 /* Given a map "map", compute the lexicographically minimal
6684 * (or maximal) image element for each domain element in dom,
6685 * in the form of an isl_pw_multi_aff.
6686 * If "empty" is not NULL, then set *empty to those elements in dom that
6687 * do not have an image element.
6688 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6689 * should be computed over the domain of "map". "empty" is also NULL
6690 * in this case.
6692 * We first compute the lexicographically minimal or maximal element
6693 * in the first basic map. This results in a partial solution "res"
6694 * and a subset "todo" of dom that still need to be handled.
6695 * We then consider each of the remaining maps in "map" and successively
6696 * update both "res" and "todo".
6697 * If "empty" is NULL, then the todo sets are not needed and therefore
6698 * also not computed.
6700 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6701 __isl_take isl_map *map, __isl_take isl_set *dom,
6702 __isl_give isl_set **empty, unsigned flags)
6704 int i;
6705 int full;
6706 isl_pw_multi_aff *res;
6707 isl_set *todo;
6709 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6710 if (!map || (!full && !dom))
6711 goto error;
6713 if (isl_map_plain_is_empty(map)) {
6714 if (empty)
6715 *empty = dom;
6716 else
6717 isl_set_free(dom);
6718 return isl_pw_multi_aff_from_map(map);
6721 res = basic_map_partial_lexopt_pw_multi_aff(
6722 isl_basic_map_copy(map->p[0]),
6723 isl_set_copy(dom), empty, flags);
6725 if (empty)
6726 todo = *empty;
6727 for (i = 1; i < map->n; ++i) {
6728 isl_pw_multi_aff *res_i;
6730 res_i = basic_map_partial_lexopt_pw_multi_aff(
6731 isl_basic_map_copy(map->p[i]),
6732 isl_set_copy(dom), empty, flags);
6734 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6735 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6736 else
6737 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6739 if (empty)
6740 todo = isl_set_intersect(todo, *empty);
6743 isl_set_free(dom);
6744 isl_map_free(map);
6746 if (empty)
6747 *empty = todo;
6749 return res;
6750 error:
6751 if (empty)
6752 *empty = NULL;
6753 isl_set_free(dom);
6754 isl_map_free(map);
6755 return NULL;
6758 #undef TYPE
6759 #define TYPE isl_map
6760 #undef SUFFIX
6761 #define SUFFIX
6762 #undef EMPTY
6763 #define EMPTY isl_map_empty
6764 #undef ADD
6765 #define ADD isl_map_union_disjoint
6766 #include "isl_map_lexopt_templ.c"
6768 /* Given a map "map", compute the lexicographically minimal
6769 * (or maximal) image element for each domain element in "dom",
6770 * in the form of an isl_map.
6771 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6772 * do not have an image element.
6773 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6774 * should be computed over the domain of "map". "empty" is also NULL
6775 * in this case.
6777 * If the input consists of more than one disjunct, then first
6778 * compute the desired result in the form of an isl_pw_multi_aff and
6779 * then convert that into an isl_map.
6781 * This function used to have an explicit implementation in terms
6782 * of isl_maps, but it would continually intersect the domains of
6783 * partial results with the complement of the domain of the next
6784 * partial solution, potentially leading to an explosion in the number
6785 * of disjuncts if there are several disjuncts in the input.
6786 * An even earlier implementation of this function would look for
6787 * better results in the domain of the partial result and for extra
6788 * results in the complement of this domain, which would lead to
6789 * even more splintering.
6791 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6792 __isl_take isl_map *map, __isl_take isl_set *dom,
6793 __isl_give isl_set **empty, unsigned flags)
6795 int full;
6796 struct isl_map *res;
6797 isl_pw_multi_aff *pma;
6799 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6800 if (!map || (!full && !dom))
6801 goto error;
6803 if (isl_map_plain_is_empty(map)) {
6804 if (empty)
6805 *empty = dom;
6806 else
6807 isl_set_free(dom);
6808 return map;
6811 if (map->n == 1) {
6812 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6813 dom, empty, flags);
6814 isl_map_free(map);
6815 return res;
6818 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6819 flags);
6820 return isl_map_from_pw_multi_aff(pma);
6821 error:
6822 if (empty)
6823 *empty = NULL;
6824 isl_set_free(dom);
6825 isl_map_free(map);
6826 return NULL;
6829 __isl_give isl_map *isl_map_partial_lexmax(
6830 __isl_take isl_map *map, __isl_take isl_set *dom,
6831 __isl_give isl_set **empty)
6833 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6836 __isl_give isl_map *isl_map_partial_lexmin(
6837 __isl_take isl_map *map, __isl_take isl_set *dom,
6838 __isl_give isl_set **empty)
6840 return isl_map_partial_lexopt(map, dom, empty, 0);
6843 __isl_give isl_set *isl_set_partial_lexmin(
6844 __isl_take isl_set *set, __isl_take isl_set *dom,
6845 __isl_give isl_set **empty)
6847 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6848 dom, empty));
6851 __isl_give isl_set *isl_set_partial_lexmax(
6852 __isl_take isl_set *set, __isl_take isl_set *dom,
6853 __isl_give isl_set **empty)
6855 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6856 dom, empty));
6859 /* Compute the lexicographic minimum (or maximum if "flags" includes
6860 * ISL_OPT_MAX) of "bset" over its parametric domain.
6862 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6863 unsigned flags)
6865 return isl_basic_map_lexopt(bset, flags);
6868 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6870 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6873 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6875 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6878 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6880 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6883 /* Compute the lexicographic minimum of "bset" over its parametric domain
6884 * for the purpose of quantifier elimination.
6885 * That is, find an explicit representation for all the existentially
6886 * quantified variables in "bset" by computing their lexicographic
6887 * minimum.
6889 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6890 __isl_take isl_basic_set *bset)
6892 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6895 /* Given a basic map with one output dimension, compute the minimum or
6896 * maximum of that dimension as an isl_pw_aff.
6898 * Compute the optimum as a lexicographic optimum over the single
6899 * output dimension and extract the single isl_pw_aff from the result.
6901 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6902 int max)
6904 isl_pw_multi_aff *pma;
6905 isl_pw_aff *pwaff;
6907 bmap = isl_basic_map_copy(bmap);
6908 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6909 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6910 isl_pw_multi_aff_free(pma);
6912 return pwaff;
6915 /* Compute the minimum or maximum of the given output dimension
6916 * as a function of the parameters and the input dimensions,
6917 * but independently of the other output dimensions.
6919 * We first project out the other output dimension and then compute
6920 * the "lexicographic" maximum in each basic map, combining the results
6921 * using isl_pw_aff_union_max.
6923 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6924 int max)
6926 int i;
6927 isl_pw_aff *pwaff;
6928 unsigned n_out;
6930 n_out = isl_map_dim(map, isl_dim_out);
6931 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6932 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6933 if (!map)
6934 return NULL;
6936 if (map->n == 0) {
6937 isl_space *space = isl_map_get_space(map);
6938 isl_map_free(map);
6939 return isl_pw_aff_empty(space);
6942 pwaff = basic_map_dim_opt(map->p[0], max);
6943 for (i = 1; i < map->n; ++i) {
6944 isl_pw_aff *pwaff_i;
6946 pwaff_i = basic_map_dim_opt(map->p[i], max);
6947 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6950 isl_map_free(map);
6952 return pwaff;
6955 /* Compute the minimum of the given output dimension as a function of the
6956 * parameters and input dimensions, but independently of
6957 * the other output dimensions.
6959 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6961 return map_dim_opt(map, pos, 0);
6964 /* Compute the maximum of the given output dimension as a function of the
6965 * parameters and input dimensions, but independently of
6966 * the other output dimensions.
6968 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6970 return map_dim_opt(map, pos, 1);
6973 /* Compute the minimum or maximum of the given set dimension
6974 * as a function of the parameters,
6975 * but independently of the other set dimensions.
6977 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6978 int max)
6980 return map_dim_opt(set, pos, max);
6983 /* Compute the maximum of the given set dimension as a function of the
6984 * parameters, but independently of the other set dimensions.
6986 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6988 return set_dim_opt(set, pos, 1);
6991 /* Compute the minimum of the given set dimension as a function of the
6992 * parameters, but independently of the other set dimensions.
6994 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6996 return set_dim_opt(set, pos, 0);
6999 /* Apply a preimage specified by "mat" on the parameters of "bset".
7000 * bset is assumed to have only parameters and divs.
7002 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7003 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7005 unsigned nparam;
7007 if (!bset || !mat)
7008 goto error;
7010 bset->dim = isl_space_cow(bset->dim);
7011 if (!bset->dim)
7012 goto error;
7014 nparam = isl_basic_set_dim(bset, isl_dim_param);
7016 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7018 bset->dim->nparam = 0;
7019 bset->dim->n_out = nparam;
7020 bset = isl_basic_set_preimage(bset, mat);
7021 if (bset) {
7022 bset->dim->nparam = bset->dim->n_out;
7023 bset->dim->n_out = 0;
7025 return bset;
7026 error:
7027 isl_mat_free(mat);
7028 isl_basic_set_free(bset);
7029 return NULL;
7032 /* Apply a preimage specified by "mat" on the parameters of "set".
7033 * set is assumed to have only parameters and divs.
7035 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7036 __isl_take isl_mat *mat)
7038 isl_space *space;
7039 unsigned nparam;
7041 if (!set || !mat)
7042 goto error;
7044 nparam = isl_set_dim(set, isl_dim_param);
7046 if (mat->n_row != 1 + nparam)
7047 isl_die(isl_set_get_ctx(set), isl_error_internal,
7048 "unexpected number of rows", goto error);
7050 space = isl_set_get_space(set);
7051 space = isl_space_move_dims(space, isl_dim_set, 0,
7052 isl_dim_param, 0, nparam);
7053 set = isl_set_reset_space(set, space);
7054 set = isl_set_preimage(set, mat);
7055 nparam = isl_set_dim(set, isl_dim_out);
7056 space = isl_set_get_space(set);
7057 space = isl_space_move_dims(space, isl_dim_param, 0,
7058 isl_dim_out, 0, nparam);
7059 set = isl_set_reset_space(set, space);
7060 return set;
7061 error:
7062 isl_mat_free(mat);
7063 isl_set_free(set);
7064 return NULL;
7067 /* Intersect the basic set "bset" with the affine space specified by the
7068 * equalities in "eq".
7070 static __isl_give isl_basic_set *basic_set_append_equalities(
7071 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7073 int i, k;
7074 unsigned len;
7076 if (!bset || !eq)
7077 goto error;
7079 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7080 eq->n_row, 0);
7081 if (!bset)
7082 goto error;
7084 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7085 for (i = 0; i < eq->n_row; ++i) {
7086 k = isl_basic_set_alloc_equality(bset);
7087 if (k < 0)
7088 goto error;
7089 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7090 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7092 isl_mat_free(eq);
7094 bset = isl_basic_set_gauss(bset, NULL);
7095 bset = isl_basic_set_finalize(bset);
7097 return bset;
7098 error:
7099 isl_mat_free(eq);
7100 isl_basic_set_free(bset);
7101 return NULL;
7104 /* Intersect the set "set" with the affine space specified by the
7105 * equalities in "eq".
7107 static struct isl_set *set_append_equalities(struct isl_set *set,
7108 struct isl_mat *eq)
7110 int i;
7112 if (!set || !eq)
7113 goto error;
7115 for (i = 0; i < set->n; ++i) {
7116 set->p[i] = basic_set_append_equalities(set->p[i],
7117 isl_mat_copy(eq));
7118 if (!set->p[i])
7119 goto error;
7121 isl_mat_free(eq);
7122 return set;
7123 error:
7124 isl_mat_free(eq);
7125 isl_set_free(set);
7126 return NULL;
7129 /* Given a basic set "bset" that only involves parameters and existentially
7130 * quantified variables, return the index of the first equality
7131 * that only involves parameters. If there is no such equality then
7132 * return bset->n_eq.
7134 * This function assumes that isl_basic_set_gauss has been called on "bset".
7136 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7138 int i, j;
7139 unsigned nparam, n_div;
7141 if (!bset)
7142 return -1;
7144 nparam = isl_basic_set_dim(bset, isl_dim_param);
7145 n_div = isl_basic_set_dim(bset, isl_dim_div);
7147 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7148 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7149 ++i;
7152 return i;
7155 /* Compute an explicit representation for the existentially quantified
7156 * variables in "bset" by computing the "minimal value" of the set
7157 * variables. Since there are no set variables, the computation of
7158 * the minimal value essentially computes an explicit representation
7159 * of the non-empty part(s) of "bset".
7161 * The input only involves parameters and existentially quantified variables.
7162 * All equalities among parameters have been removed.
7164 * Since the existentially quantified variables in the result are in general
7165 * going to be different from those in the input, we first replace
7166 * them by the minimal number of variables based on their equalities.
7167 * This should simplify the parametric integer programming.
7169 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7171 isl_morph *morph1, *morph2;
7172 isl_set *set;
7173 unsigned n;
7175 if (!bset)
7176 return NULL;
7177 if (bset->n_eq == 0)
7178 return isl_basic_set_lexmin_compute_divs(bset);
7180 morph1 = isl_basic_set_parameter_compression(bset);
7181 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7182 bset = isl_basic_set_lift(bset);
7183 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7184 bset = isl_morph_basic_set(morph2, bset);
7185 n = isl_basic_set_dim(bset, isl_dim_set);
7186 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7188 set = isl_basic_set_lexmin_compute_divs(bset);
7190 set = isl_morph_set(isl_morph_inverse(morph1), set);
7192 return set;
7195 /* Project the given basic set onto its parameter domain, possibly introducing
7196 * new, explicit, existential variables in the constraints.
7197 * The input has parameters and (possibly implicit) existential variables.
7198 * The output has the same parameters, but only
7199 * explicit existentially quantified variables.
7201 * The actual projection is performed by pip, but pip doesn't seem
7202 * to like equalities very much, so we first remove the equalities
7203 * among the parameters by performing a variable compression on
7204 * the parameters. Afterward, an inverse transformation is performed
7205 * and the equalities among the parameters are inserted back in.
7207 * The variable compression on the parameters may uncover additional
7208 * equalities that were only implicit before. We therefore check
7209 * if there are any new parameter equalities in the result and
7210 * if so recurse. The removal of parameter equalities is required
7211 * for the parameter compression performed by base_compute_divs.
7213 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7215 int i;
7216 struct isl_mat *eq;
7217 struct isl_mat *T, *T2;
7218 struct isl_set *set;
7219 unsigned nparam;
7221 bset = isl_basic_set_cow(bset);
7222 if (!bset)
7223 return NULL;
7225 if (bset->n_eq == 0)
7226 return base_compute_divs(bset);
7228 bset = isl_basic_set_gauss(bset, NULL);
7229 if (!bset)
7230 return NULL;
7231 if (isl_basic_set_plain_is_empty(bset))
7232 return isl_set_from_basic_set(bset);
7234 i = first_parameter_equality(bset);
7235 if (i == bset->n_eq)
7236 return base_compute_divs(bset);
7238 nparam = isl_basic_set_dim(bset, isl_dim_param);
7239 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7240 0, 1 + nparam);
7241 eq = isl_mat_cow(eq);
7242 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7243 if (T && T->n_col == 0) {
7244 isl_mat_free(T);
7245 isl_mat_free(T2);
7246 isl_mat_free(eq);
7247 bset = isl_basic_set_set_to_empty(bset);
7248 return isl_set_from_basic_set(bset);
7250 bset = basic_set_parameter_preimage(bset, T);
7252 i = first_parameter_equality(bset);
7253 if (!bset)
7254 set = NULL;
7255 else if (i == bset->n_eq)
7256 set = base_compute_divs(bset);
7257 else
7258 set = parameter_compute_divs(bset);
7259 set = set_parameter_preimage(set, T2);
7260 set = set_append_equalities(set, eq);
7261 return set;
7264 /* Insert the divs from "ls" before those of "bmap".
7266 * The number of columns is not changed, which means that the last
7267 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7268 * The caller is responsible for removing the same number of dimensions
7269 * from the space of "bmap".
7271 static __isl_give isl_basic_map *insert_divs_from_local_space(
7272 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7274 int i;
7275 int n_div;
7276 int old_n_div;
7278 n_div = isl_local_space_dim(ls, isl_dim_div);
7279 if (n_div == 0)
7280 return bmap;
7282 old_n_div = bmap->n_div;
7283 bmap = insert_div_rows(bmap, n_div);
7284 if (!bmap)
7285 return NULL;
7287 for (i = 0; i < n_div; ++i) {
7288 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7289 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7292 return bmap;
7295 /* Replace the space of "bmap" by the space and divs of "ls".
7297 * If "ls" has any divs, then we simplify the result since we may
7298 * have discovered some additional equalities that could simplify
7299 * the div expressions.
7301 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7302 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7304 int n_div;
7306 bmap = isl_basic_map_cow(bmap);
7307 if (!bmap || !ls)
7308 goto error;
7310 n_div = isl_local_space_dim(ls, isl_dim_div);
7311 bmap = insert_divs_from_local_space(bmap, ls);
7312 if (!bmap)
7313 goto error;
7315 isl_space_free(bmap->dim);
7316 bmap->dim = isl_local_space_get_space(ls);
7317 if (!bmap->dim)
7318 goto error;
7320 isl_local_space_free(ls);
7321 if (n_div > 0)
7322 bmap = isl_basic_map_simplify(bmap);
7323 bmap = isl_basic_map_finalize(bmap);
7324 return bmap;
7325 error:
7326 isl_basic_map_free(bmap);
7327 isl_local_space_free(ls);
7328 return NULL;
7331 /* Replace the space of "map" by the space and divs of "ls".
7333 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7334 __isl_take isl_local_space *ls)
7336 int i;
7338 map = isl_map_cow(map);
7339 if (!map || !ls)
7340 goto error;
7342 for (i = 0; i < map->n; ++i) {
7343 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7344 isl_local_space_copy(ls));
7345 if (!map->p[i])
7346 goto error;
7348 isl_space_free(map->dim);
7349 map->dim = isl_local_space_get_space(ls);
7350 if (!map->dim)
7351 goto error;
7353 isl_local_space_free(ls);
7354 return map;
7355 error:
7356 isl_local_space_free(ls);
7357 isl_map_free(map);
7358 return NULL;
7361 /* Compute an explicit representation for the existentially
7362 * quantified variables for which do not know any explicit representation yet.
7364 * We first sort the existentially quantified variables so that the
7365 * existentially quantified variables for which we already have an explicit
7366 * representation are placed before those for which we do not.
7367 * The input dimensions, the output dimensions and the existentially
7368 * quantified variables for which we already have an explicit
7369 * representation are then turned into parameters.
7370 * compute_divs returns a map with the same parameters and
7371 * no input or output dimensions and the dimension specification
7372 * is reset to that of the input, including the existentially quantified
7373 * variables for which we already had an explicit representation.
7375 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7377 struct isl_basic_set *bset;
7378 struct isl_set *set;
7379 struct isl_map *map;
7380 isl_space *space;
7381 isl_local_space *ls;
7382 unsigned nparam;
7383 unsigned n_in;
7384 unsigned n_out;
7385 int n_known;
7386 int i;
7388 bmap = isl_basic_map_sort_divs(bmap);
7389 bmap = isl_basic_map_cow(bmap);
7390 if (!bmap)
7391 return NULL;
7393 n_known = isl_basic_map_first_unknown_div(bmap);
7394 if (n_known < 0)
7395 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7397 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7398 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7399 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7400 space = isl_space_set_alloc(bmap->ctx,
7401 nparam + n_in + n_out + n_known, 0);
7402 if (!space)
7403 goto error;
7405 ls = isl_basic_map_get_local_space(bmap);
7406 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7407 n_known, bmap->n_div - n_known);
7408 if (n_known > 0) {
7409 for (i = n_known; i < bmap->n_div; ++i)
7410 swap_div(bmap, i - n_known, i);
7411 bmap->n_div -= n_known;
7412 bmap->extra -= n_known;
7414 bmap = isl_basic_map_reset_space(bmap, space);
7415 bset = bset_from_bmap(bmap);
7417 set = parameter_compute_divs(bset);
7418 map = set_to_map(set);
7419 map = replace_space_by_local_space(map, ls);
7421 return map;
7422 error:
7423 isl_basic_map_free(bmap);
7424 return NULL;
7427 /* Remove the explicit representation of local variable "div",
7428 * if there is any.
7430 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7431 __isl_take isl_basic_map *bmap, int div)
7433 isl_bool unknown;
7435 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7436 if (unknown < 0)
7437 return isl_basic_map_free(bmap);
7438 if (unknown)
7439 return bmap;
7441 bmap = isl_basic_map_cow(bmap);
7442 if (!bmap)
7443 return NULL;
7444 isl_int_set_si(bmap->div[div][0], 0);
7445 return bmap;
7448 /* Is local variable "div" of "bmap" marked as not having an explicit
7449 * representation?
7450 * Note that even if "div" is not marked in this way and therefore
7451 * has an explicit representation, this representation may still
7452 * depend (indirectly) on other local variables that do not
7453 * have an explicit representation.
7455 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7456 int div)
7458 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7459 return isl_bool_error;
7460 return isl_int_is_zero(bmap->div[div][0]);
7463 /* Return the position of the first local variable that does not
7464 * have an explicit representation.
7465 * Return the total number of local variables if they all have
7466 * an explicit representation.
7467 * Return -1 on error.
7469 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7471 int i;
7473 if (!bmap)
7474 return -1;
7476 for (i = 0; i < bmap->n_div; ++i) {
7477 if (!isl_basic_map_div_is_known(bmap, i))
7478 return i;
7480 return bmap->n_div;
7483 /* Return the position of the first local variable that does not
7484 * have an explicit representation.
7485 * Return the total number of local variables if they all have
7486 * an explicit representation.
7487 * Return -1 on error.
7489 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7491 return isl_basic_map_first_unknown_div(bset);
7494 /* Does "bmap" have an explicit representation for all local variables?
7496 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7498 int first, n;
7500 n = isl_basic_map_dim(bmap, isl_dim_div);
7501 first = isl_basic_map_first_unknown_div(bmap);
7502 if (first < 0)
7503 return isl_bool_error;
7504 return first == n;
7507 /* Do all basic maps in "map" have an explicit representation
7508 * for all local variables?
7510 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7512 int i;
7514 if (!map)
7515 return isl_bool_error;
7517 for (i = 0; i < map->n; ++i) {
7518 int known = isl_basic_map_divs_known(map->p[i]);
7519 if (known <= 0)
7520 return known;
7523 return isl_bool_true;
7526 /* If bmap contains any unknown divs, then compute explicit
7527 * expressions for them. However, this computation may be
7528 * quite expensive, so first try to remove divs that aren't
7529 * strictly needed.
7531 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7533 int known;
7534 struct isl_map *map;
7536 known = isl_basic_map_divs_known(bmap);
7537 if (known < 0)
7538 goto error;
7539 if (known)
7540 return isl_map_from_basic_map(bmap);
7542 bmap = isl_basic_map_drop_redundant_divs(bmap);
7544 known = isl_basic_map_divs_known(bmap);
7545 if (known < 0)
7546 goto error;
7547 if (known)
7548 return isl_map_from_basic_map(bmap);
7550 map = compute_divs(bmap);
7551 return map;
7552 error:
7553 isl_basic_map_free(bmap);
7554 return NULL;
7557 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7559 int i;
7560 int known;
7561 struct isl_map *res;
7563 if (!map)
7564 return NULL;
7565 if (map->n == 0)
7566 return map;
7568 known = isl_map_divs_known(map);
7569 if (known < 0) {
7570 isl_map_free(map);
7571 return NULL;
7573 if (known)
7574 return map;
7576 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7577 for (i = 1 ; i < map->n; ++i) {
7578 struct isl_map *r2;
7579 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7580 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7581 res = isl_map_union_disjoint(res, r2);
7582 else
7583 res = isl_map_union(res, r2);
7585 isl_map_free(map);
7587 return res;
7590 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7592 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7595 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7597 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7600 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7602 int i;
7603 struct isl_set *set;
7605 if (!map)
7606 goto error;
7608 map = isl_map_cow(map);
7609 if (!map)
7610 return NULL;
7612 set = set_from_map(map);
7613 set->dim = isl_space_domain(set->dim);
7614 if (!set->dim)
7615 goto error;
7616 for (i = 0; i < map->n; ++i) {
7617 set->p[i] = isl_basic_map_domain(map->p[i]);
7618 if (!set->p[i])
7619 goto error;
7621 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7622 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7623 return set;
7624 error:
7625 isl_map_free(map);
7626 return NULL;
7629 /* Return the union of "map1" and "map2", where we assume for now that
7630 * "map1" and "map2" are disjoint. Note that the basic maps inside
7631 * "map1" or "map2" may not be disjoint from each other.
7632 * Also note that this function is also called from isl_map_union,
7633 * which takes care of handling the situation where "map1" and "map2"
7634 * may not be disjoint.
7636 * If one of the inputs is empty, we can simply return the other input.
7637 * Similarly, if one of the inputs is universal, then it is equal to the union.
7639 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7640 __isl_take isl_map *map2)
7642 int i;
7643 unsigned flags = 0;
7644 struct isl_map *map = NULL;
7645 int is_universe;
7647 if (!map1 || !map2)
7648 goto error;
7650 if (!isl_space_is_equal(map1->dim, map2->dim))
7651 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7652 "spaces don't match", goto error);
7654 if (map1->n == 0) {
7655 isl_map_free(map1);
7656 return map2;
7658 if (map2->n == 0) {
7659 isl_map_free(map2);
7660 return map1;
7663 is_universe = isl_map_plain_is_universe(map1);
7664 if (is_universe < 0)
7665 goto error;
7666 if (is_universe) {
7667 isl_map_free(map2);
7668 return map1;
7671 is_universe = isl_map_plain_is_universe(map2);
7672 if (is_universe < 0)
7673 goto error;
7674 if (is_universe) {
7675 isl_map_free(map1);
7676 return map2;
7679 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7680 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7681 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7683 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7684 map1->n + map2->n, flags);
7685 if (!map)
7686 goto error;
7687 for (i = 0; i < map1->n; ++i) {
7688 map = isl_map_add_basic_map(map,
7689 isl_basic_map_copy(map1->p[i]));
7690 if (!map)
7691 goto error;
7693 for (i = 0; i < map2->n; ++i) {
7694 map = isl_map_add_basic_map(map,
7695 isl_basic_map_copy(map2->p[i]));
7696 if (!map)
7697 goto error;
7699 isl_map_free(map1);
7700 isl_map_free(map2);
7701 return map;
7702 error:
7703 isl_map_free(map);
7704 isl_map_free(map1);
7705 isl_map_free(map2);
7706 return NULL;
7709 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7710 * guaranteed to be disjoint by the caller.
7712 * Note that this functions is called from within isl_map_make_disjoint,
7713 * so we have to be careful not to touch the constraints of the inputs
7714 * in any way.
7716 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7717 __isl_take isl_map *map2)
7719 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7722 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7723 * not be disjoint. The parameters are assumed to have been aligned.
7725 * We currently simply call map_union_disjoint, the internal operation
7726 * of which does not really depend on the inputs being disjoint.
7727 * If the result contains more than one basic map, then we clear
7728 * the disjoint flag since the result may contain basic maps from
7729 * both inputs and these are not guaranteed to be disjoint.
7731 * As a special case, if "map1" and "map2" are obviously equal,
7732 * then we simply return "map1".
7734 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7735 __isl_take isl_map *map2)
7737 int equal;
7739 if (!map1 || !map2)
7740 goto error;
7742 equal = isl_map_plain_is_equal(map1, map2);
7743 if (equal < 0)
7744 goto error;
7745 if (equal) {
7746 isl_map_free(map2);
7747 return map1;
7750 map1 = map_union_disjoint(map1, map2);
7751 if (!map1)
7752 return NULL;
7753 if (map1->n > 1)
7754 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7755 return map1;
7756 error:
7757 isl_map_free(map1);
7758 isl_map_free(map2);
7759 return NULL;
7762 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7763 * not be disjoint.
7765 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7766 __isl_take isl_map *map2)
7768 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7771 __isl_give isl_set *isl_set_union_disjoint(
7772 __isl_take isl_set *set1, __isl_take isl_set *set2)
7774 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7775 set_to_map(set2)));
7778 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7780 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7783 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7784 * the results.
7786 * "map" and "set" are assumed to be compatible and non-NULL.
7788 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7789 __isl_take isl_set *set,
7790 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7791 __isl_take isl_basic_set *bset))
7793 unsigned flags = 0;
7794 struct isl_map *result;
7795 int i, j;
7797 if (isl_set_plain_is_universe(set)) {
7798 isl_set_free(set);
7799 return map;
7802 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7803 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7804 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7806 result = isl_map_alloc_space(isl_space_copy(map->dim),
7807 map->n * set->n, flags);
7808 for (i = 0; result && i < map->n; ++i)
7809 for (j = 0; j < set->n; ++j) {
7810 result = isl_map_add_basic_map(result,
7811 fn(isl_basic_map_copy(map->p[i]),
7812 isl_basic_set_copy(set->p[j])));
7813 if (!result)
7814 break;
7817 isl_map_free(map);
7818 isl_set_free(set);
7819 return result;
7822 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7823 __isl_take isl_set *set)
7825 isl_bool ok;
7827 ok = isl_map_compatible_range(map, set);
7828 if (ok < 0)
7829 goto error;
7830 if (!ok)
7831 isl_die(set->ctx, isl_error_invalid,
7832 "incompatible spaces", goto error);
7834 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7835 error:
7836 isl_map_free(map);
7837 isl_set_free(set);
7838 return NULL;
7841 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7842 __isl_take isl_set *set)
7844 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7847 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7848 __isl_take isl_set *set)
7850 isl_bool ok;
7852 ok = isl_map_compatible_domain(map, set);
7853 if (ok < 0)
7854 goto error;
7855 if (!ok)
7856 isl_die(set->ctx, isl_error_invalid,
7857 "incompatible spaces", goto error);
7859 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7860 error:
7861 isl_map_free(map);
7862 isl_set_free(set);
7863 return NULL;
7866 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7867 __isl_take isl_set *set)
7869 return isl_map_align_params_map_map_and(map, set,
7870 &map_intersect_domain);
7873 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7874 * in the space B -> C, return the intersection.
7875 * The parameters are assumed to have been aligned.
7877 * The map "factor" is first extended to a map living in the space
7878 * [A -> B] -> C and then a regular intersection is computed.
7880 static __isl_give isl_map *map_intersect_domain_factor_range(
7881 __isl_take isl_map *map, __isl_take isl_map *factor)
7883 isl_space *space;
7884 isl_map *ext_factor;
7886 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7887 ext_factor = isl_map_universe(space);
7888 ext_factor = isl_map_domain_product(ext_factor, factor);
7889 return map_intersect(map, ext_factor);
7892 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7893 * in the space B -> C, return the intersection.
7895 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7896 __isl_take isl_map *map, __isl_take isl_map *factor)
7898 return isl_map_align_params_map_map_and(map, factor,
7899 &map_intersect_domain_factor_range);
7902 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7903 * in the space A -> C, return the intersection.
7905 * The map "factor" is first extended to a map living in the space
7906 * A -> [B -> C] and then a regular intersection is computed.
7908 static __isl_give isl_map *map_intersect_range_factor_range(
7909 __isl_take isl_map *map, __isl_take isl_map *factor)
7911 isl_space *space;
7912 isl_map *ext_factor;
7914 space = isl_space_range_factor_domain(isl_map_get_space(map));
7915 ext_factor = isl_map_universe(space);
7916 ext_factor = isl_map_range_product(ext_factor, factor);
7917 return isl_map_intersect(map, ext_factor);
7920 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7921 * in the space A -> C, return the intersection.
7923 __isl_give isl_map *isl_map_intersect_range_factor_range(
7924 __isl_take isl_map *map, __isl_take isl_map *factor)
7926 return isl_map_align_params_map_map_and(map, factor,
7927 &map_intersect_range_factor_range);
7930 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7931 __isl_take isl_map *map2)
7933 if (!map1 || !map2)
7934 goto error;
7935 map1 = isl_map_reverse(map1);
7936 map1 = isl_map_apply_range(map1, map2);
7937 return isl_map_reverse(map1);
7938 error:
7939 isl_map_free(map1);
7940 isl_map_free(map2);
7941 return NULL;
7944 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7945 __isl_take isl_map *map2)
7947 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7950 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7951 __isl_take isl_map *map2)
7953 isl_space *space;
7954 struct isl_map *result;
7955 int i, j;
7957 if (!map1 || !map2)
7958 goto error;
7960 space = isl_space_join(isl_space_copy(map1->dim),
7961 isl_space_copy(map2->dim));
7963 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7964 if (!result)
7965 goto error;
7966 for (i = 0; i < map1->n; ++i)
7967 for (j = 0; j < map2->n; ++j) {
7968 result = isl_map_add_basic_map(result,
7969 isl_basic_map_apply_range(
7970 isl_basic_map_copy(map1->p[i]),
7971 isl_basic_map_copy(map2->p[j])));
7972 if (!result)
7973 goto error;
7975 isl_map_free(map1);
7976 isl_map_free(map2);
7977 if (result && result->n <= 1)
7978 ISL_F_SET(result, ISL_MAP_DISJOINT);
7979 return result;
7980 error:
7981 isl_map_free(map1);
7982 isl_map_free(map2);
7983 return NULL;
7986 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7987 __isl_take isl_map *map2)
7989 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7993 * returns range - domain
7995 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
7997 isl_space *target_space;
7998 struct isl_basic_set *bset;
7999 unsigned dim;
8000 unsigned nparam;
8001 int i;
8003 if (!bmap)
8004 goto error;
8005 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8006 bmap->dim, isl_dim_out),
8007 goto error);
8008 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8009 dim = isl_basic_map_dim(bmap, isl_dim_in);
8010 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8011 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8012 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8013 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8014 for (i = 0; i < dim; ++i) {
8015 int j = isl_basic_map_alloc_equality(bmap);
8016 if (j < 0) {
8017 bmap = isl_basic_map_free(bmap);
8018 break;
8020 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8021 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8022 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8023 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8025 bset = isl_basic_map_domain(bmap);
8026 bset = isl_basic_set_reset_space(bset, target_space);
8027 return bset;
8028 error:
8029 isl_basic_map_free(bmap);
8030 return NULL;
8034 * returns range - domain
8036 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8038 int i;
8039 isl_space *dim;
8040 struct isl_set *result;
8042 if (!map)
8043 return NULL;
8045 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8046 map->dim, isl_dim_out),
8047 goto error);
8048 dim = isl_map_get_space(map);
8049 dim = isl_space_domain(dim);
8050 result = isl_set_alloc_space(dim, map->n, 0);
8051 if (!result)
8052 goto error;
8053 for (i = 0; i < map->n; ++i)
8054 result = isl_set_add_basic_set(result,
8055 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8056 isl_map_free(map);
8057 return result;
8058 error:
8059 isl_map_free(map);
8060 return NULL;
8064 * returns [domain -> range] -> range - domain
8066 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8067 __isl_take isl_basic_map *bmap)
8069 int i, k;
8070 isl_space *space;
8071 isl_basic_map *domain;
8072 int nparam, n;
8073 unsigned total;
8075 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8076 bmap->dim, isl_dim_out))
8077 isl_die(bmap->ctx, isl_error_invalid,
8078 "domain and range don't match", goto error);
8080 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8081 n = isl_basic_map_dim(bmap, isl_dim_in);
8083 space = isl_basic_map_get_space(bmap);
8084 space = isl_space_from_range(isl_space_domain(space));
8085 domain = isl_basic_map_universe(space);
8087 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8088 bmap = isl_basic_map_apply_range(bmap, domain);
8089 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8091 total = isl_basic_map_total_dim(bmap);
8093 for (i = 0; i < n; ++i) {
8094 k = isl_basic_map_alloc_equality(bmap);
8095 if (k < 0)
8096 goto error;
8097 isl_seq_clr(bmap->eq[k], 1 + total);
8098 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8099 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8100 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8103 bmap = isl_basic_map_gauss(bmap, NULL);
8104 return isl_basic_map_finalize(bmap);
8105 error:
8106 isl_basic_map_free(bmap);
8107 return NULL;
8111 * returns [domain -> range] -> range - domain
8113 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8115 int i;
8116 isl_space *domain_space;
8118 if (!map)
8119 return NULL;
8121 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8122 map->dim, isl_dim_out))
8123 isl_die(map->ctx, isl_error_invalid,
8124 "domain and range don't match", goto error);
8126 map = isl_map_cow(map);
8127 if (!map)
8128 return NULL;
8130 domain_space = isl_space_domain(isl_map_get_space(map));
8131 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8132 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8133 if (!map->dim)
8134 goto error;
8135 for (i = 0; i < map->n; ++i) {
8136 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8137 if (!map->p[i])
8138 goto error;
8140 map = isl_map_unmark_normalized(map);
8141 return map;
8142 error:
8143 isl_map_free(map);
8144 return NULL;
8147 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8149 unsigned n_in, n_out;
8151 if (!space)
8152 return NULL;
8153 n_in = isl_space_dim(space, isl_dim_in);
8154 n_out = isl_space_dim(space, isl_dim_out);
8155 if (n_in != n_out)
8156 isl_die(space->ctx, isl_error_invalid,
8157 "number of input and output dimensions needs to be "
8158 "the same", goto error);
8159 return isl_basic_map_equal(space, n_in);
8160 error:
8161 isl_space_free(space);
8162 return NULL;
8165 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8167 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8170 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8172 isl_space *dim = isl_set_get_space(set);
8173 isl_map *id;
8174 id = isl_map_identity(isl_space_map_from_set(dim));
8175 return isl_map_intersect_range(id, set);
8178 /* Construct a basic set with all set dimensions having only non-negative
8179 * values.
8181 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8182 __isl_take isl_space *space)
8184 int i;
8185 unsigned nparam;
8186 unsigned dim;
8187 struct isl_basic_set *bset;
8189 if (!space)
8190 return NULL;
8191 nparam = space->nparam;
8192 dim = space->n_out;
8193 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8194 if (!bset)
8195 return NULL;
8196 for (i = 0; i < dim; ++i) {
8197 int k = isl_basic_set_alloc_inequality(bset);
8198 if (k < 0)
8199 goto error;
8200 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8201 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8203 return bset;
8204 error:
8205 isl_basic_set_free(bset);
8206 return NULL;
8209 /* Construct the half-space x_pos >= 0.
8211 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8212 int pos)
8214 int k;
8215 isl_basic_set *nonneg;
8217 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8218 k = isl_basic_set_alloc_inequality(nonneg);
8219 if (k < 0)
8220 goto error;
8221 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8222 isl_int_set_si(nonneg->ineq[k][pos], 1);
8224 return isl_basic_set_finalize(nonneg);
8225 error:
8226 isl_basic_set_free(nonneg);
8227 return NULL;
8230 /* Construct the half-space x_pos <= -1.
8232 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8233 int pos)
8235 int k;
8236 isl_basic_set *neg;
8238 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8239 k = isl_basic_set_alloc_inequality(neg);
8240 if (k < 0)
8241 goto error;
8242 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8243 isl_int_set_si(neg->ineq[k][0], -1);
8244 isl_int_set_si(neg->ineq[k][pos], -1);
8246 return isl_basic_set_finalize(neg);
8247 error:
8248 isl_basic_set_free(neg);
8249 return NULL;
8252 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8253 enum isl_dim_type type, unsigned first, unsigned n)
8255 int i;
8256 unsigned offset;
8257 isl_basic_set *nonneg;
8258 isl_basic_set *neg;
8260 if (n == 0)
8261 return set;
8263 if (isl_set_check_range(set, type, first, n) < 0)
8264 return isl_set_free(set);
8266 offset = pos(set->dim, type);
8267 for (i = 0; i < n; ++i) {
8268 nonneg = nonneg_halfspace(isl_set_get_space(set),
8269 offset + first + i);
8270 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8272 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8275 return set;
8278 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8279 int len,
8280 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8281 void *user)
8283 isl_set *half;
8285 if (!set)
8286 return isl_stat_error;
8287 if (isl_set_plain_is_empty(set)) {
8288 isl_set_free(set);
8289 return isl_stat_ok;
8291 if (first == len)
8292 return fn(set, signs, user);
8294 signs[first] = 1;
8295 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8296 1 + first));
8297 half = isl_set_intersect(half, isl_set_copy(set));
8298 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8299 goto error;
8301 signs[first] = -1;
8302 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8303 1 + first));
8304 half = isl_set_intersect(half, set);
8305 return foreach_orthant(half, signs, first + 1, len, fn, user);
8306 error:
8307 isl_set_free(set);
8308 return isl_stat_error;
8311 /* Call "fn" on the intersections of "set" with each of the orthants
8312 * (except for obviously empty intersections). The orthant is identified
8313 * by the signs array, with each entry having value 1 or -1 according
8314 * to the sign of the corresponding variable.
8316 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8317 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8318 void *user)
8320 unsigned nparam;
8321 unsigned nvar;
8322 int *signs;
8323 isl_stat r;
8325 if (!set)
8326 return isl_stat_error;
8327 if (isl_set_plain_is_empty(set))
8328 return isl_stat_ok;
8330 nparam = isl_set_dim(set, isl_dim_param);
8331 nvar = isl_set_dim(set, isl_dim_set);
8333 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8335 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8336 fn, user);
8338 free(signs);
8340 return r;
8343 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8345 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8348 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8349 __isl_keep isl_basic_map *bmap2)
8351 isl_bool is_subset;
8352 struct isl_map *map1;
8353 struct isl_map *map2;
8355 if (!bmap1 || !bmap2)
8356 return isl_bool_error;
8358 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8359 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8361 is_subset = isl_map_is_subset(map1, map2);
8363 isl_map_free(map1);
8364 isl_map_free(map2);
8366 return is_subset;
8369 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8370 __isl_keep isl_basic_set *bset2)
8372 return isl_basic_map_is_subset(bset1, bset2);
8375 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8376 __isl_keep isl_basic_map *bmap2)
8378 isl_bool is_subset;
8380 if (!bmap1 || !bmap2)
8381 return isl_bool_error;
8382 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8383 if (is_subset != isl_bool_true)
8384 return is_subset;
8385 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8386 return is_subset;
8389 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8390 __isl_keep isl_basic_set *bset2)
8392 return isl_basic_map_is_equal(
8393 bset_to_bmap(bset1), bset_to_bmap(bset2));
8396 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8398 int i;
8399 int is_empty;
8401 if (!map)
8402 return isl_bool_error;
8403 for (i = 0; i < map->n; ++i) {
8404 is_empty = isl_basic_map_is_empty(map->p[i]);
8405 if (is_empty < 0)
8406 return isl_bool_error;
8407 if (!is_empty)
8408 return isl_bool_false;
8410 return isl_bool_true;
8413 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8415 return map ? map->n == 0 : isl_bool_error;
8418 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8420 return set ? set->n == 0 : isl_bool_error;
8423 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8425 return isl_map_is_empty(set_to_map(set));
8428 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8429 __isl_keep isl_map *map2)
8431 if (!map1 || !map2)
8432 return isl_bool_error;
8434 return isl_space_is_equal(map1->dim, map2->dim);
8437 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8438 __isl_keep isl_set *set2)
8440 if (!set1 || !set2)
8441 return isl_bool_error;
8443 return isl_space_is_equal(set1->dim, set2->dim);
8446 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8448 isl_bool is_subset;
8450 if (!map1 || !map2)
8451 return isl_bool_error;
8452 is_subset = isl_map_is_subset(map1, map2);
8453 if (is_subset != isl_bool_true)
8454 return is_subset;
8455 is_subset = isl_map_is_subset(map2, map1);
8456 return is_subset;
8459 /* Is "map1" equal to "map2"?
8461 * First check if they are obviously equal.
8462 * If not, then perform a more detailed analysis.
8464 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8466 isl_bool equal;
8468 equal = isl_map_plain_is_equal(map1, map2);
8469 if (equal < 0 || equal)
8470 return equal;
8471 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8474 isl_bool isl_basic_map_is_strict_subset(
8475 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8477 isl_bool is_subset;
8479 if (!bmap1 || !bmap2)
8480 return isl_bool_error;
8481 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8482 if (is_subset != isl_bool_true)
8483 return is_subset;
8484 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8485 return isl_bool_not(is_subset);
8488 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8489 __isl_keep isl_map *map2)
8491 isl_bool is_subset;
8493 if (!map1 || !map2)
8494 return isl_bool_error;
8495 is_subset = isl_map_is_subset(map1, map2);
8496 if (is_subset != isl_bool_true)
8497 return is_subset;
8498 is_subset = isl_map_is_subset(map2, map1);
8499 return isl_bool_not(is_subset);
8502 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8503 __isl_keep isl_set *set2)
8505 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8508 /* Is "bmap" obviously equal to the universe with the same space?
8510 * That is, does it not have any constraints?
8512 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8514 if (!bmap)
8515 return isl_bool_error;
8516 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8519 /* Is "bset" obviously equal to the universe with the same space?
8521 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8523 return isl_basic_map_plain_is_universe(bset);
8526 /* If "c" does not involve any existentially quantified variables,
8527 * then set *univ to false and abort
8529 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8531 isl_bool *univ = user;
8532 unsigned n;
8534 n = isl_constraint_dim(c, isl_dim_div);
8535 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8536 isl_constraint_free(c);
8537 if (*univ < 0 || !*univ)
8538 return isl_stat_error;
8539 return isl_stat_ok;
8542 /* Is "bmap" equal to the universe with the same space?
8544 * First check if it is obviously equal to the universe.
8545 * If not and if there are any constraints not involving
8546 * existentially quantified variables, then it is certainly
8547 * not equal to the universe.
8548 * Otherwise, check if the universe is a subset of "bmap".
8550 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8552 isl_bool univ;
8553 isl_basic_map *test;
8555 univ = isl_basic_map_plain_is_universe(bmap);
8556 if (univ < 0 || univ)
8557 return univ;
8558 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8559 return isl_bool_false;
8560 univ = isl_bool_true;
8561 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8562 univ)
8563 return isl_bool_error;
8564 if (univ < 0 || !univ)
8565 return univ;
8566 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8567 univ = isl_basic_map_is_subset(test, bmap);
8568 isl_basic_map_free(test);
8569 return univ;
8572 /* Is "bset" equal to the universe with the same space?
8574 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8576 return isl_basic_map_is_universe(bset);
8579 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8581 int i;
8583 if (!map)
8584 return isl_bool_error;
8586 for (i = 0; i < map->n; ++i) {
8587 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8588 if (r < 0 || r)
8589 return r;
8592 return isl_bool_false;
8595 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8597 return isl_map_plain_is_universe(set_to_map(set));
8600 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8602 struct isl_basic_set *bset = NULL;
8603 struct isl_vec *sample = NULL;
8604 isl_bool empty, non_empty;
8606 if (!bmap)
8607 return isl_bool_error;
8609 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8610 return isl_bool_true;
8612 if (isl_basic_map_plain_is_universe(bmap))
8613 return isl_bool_false;
8615 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8616 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8617 copy = isl_basic_map_remove_redundancies(copy);
8618 empty = isl_basic_map_plain_is_empty(copy);
8619 isl_basic_map_free(copy);
8620 return empty;
8623 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8624 if (non_empty < 0)
8625 return isl_bool_error;
8626 if (non_empty)
8627 return isl_bool_false;
8628 isl_vec_free(bmap->sample);
8629 bmap->sample = NULL;
8630 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8631 if (!bset)
8632 return isl_bool_error;
8633 sample = isl_basic_set_sample_vec(bset);
8634 if (!sample)
8635 return isl_bool_error;
8636 empty = sample->size == 0;
8637 isl_vec_free(bmap->sample);
8638 bmap->sample = sample;
8639 if (empty)
8640 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8642 return empty;
8645 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8647 if (!bmap)
8648 return isl_bool_error;
8649 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8652 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8654 if (!bset)
8655 return isl_bool_error;
8656 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8659 /* Is "bmap" known to be non-empty?
8661 * That is, is the cached sample still valid?
8663 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8665 unsigned total;
8667 if (!bmap)
8668 return isl_bool_error;
8669 if (!bmap->sample)
8670 return isl_bool_false;
8671 total = 1 + isl_basic_map_total_dim(bmap);
8672 if (bmap->sample->size != total)
8673 return isl_bool_false;
8674 return isl_basic_map_contains(bmap, bmap->sample);
8677 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8679 return isl_basic_map_is_empty(bset_to_bmap(bset));
8682 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8683 __isl_take isl_basic_map *bmap2)
8685 struct isl_map *map;
8686 if (!bmap1 || !bmap2)
8687 goto error;
8689 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8691 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8692 if (!map)
8693 goto error;
8694 map = isl_map_add_basic_map(map, bmap1);
8695 map = isl_map_add_basic_map(map, bmap2);
8696 return map;
8697 error:
8698 isl_basic_map_free(bmap1);
8699 isl_basic_map_free(bmap2);
8700 return NULL;
8703 struct isl_set *isl_basic_set_union(
8704 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8706 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8707 bset_to_bmap(bset2)));
8710 /* Order divs such that any div only depends on previous divs */
8711 __isl_give isl_basic_map *isl_basic_map_order_divs(
8712 __isl_take isl_basic_map *bmap)
8714 int i;
8715 unsigned off;
8717 if (!bmap)
8718 return NULL;
8720 off = isl_space_dim(bmap->dim, isl_dim_all);
8722 for (i = 0; i < bmap->n_div; ++i) {
8723 int pos;
8724 if (isl_int_is_zero(bmap->div[i][0]))
8725 continue;
8726 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8727 bmap->n_div-i);
8728 if (pos == -1)
8729 continue;
8730 if (pos == 0)
8731 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8732 "integer division depends on itself",
8733 return isl_basic_map_free(bmap));
8734 isl_basic_map_swap_div(bmap, i, i + pos);
8735 --i;
8737 return bmap;
8740 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8742 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8745 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8747 int i;
8749 if (!map)
8750 return 0;
8752 for (i = 0; i < map->n; ++i) {
8753 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8754 if (!map->p[i])
8755 goto error;
8758 return map;
8759 error:
8760 isl_map_free(map);
8761 return NULL;
8764 /* Sort the local variables of "bset".
8766 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8767 __isl_take isl_basic_set *bset)
8769 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8772 /* Apply the expansion computed by isl_merge_divs.
8773 * The expansion itself is given by "exp" while the resulting
8774 * list of divs is given by "div".
8776 * Move the integer divisions of "bmap" into the right position
8777 * according to "exp" and then introduce the additional integer
8778 * divisions, adding div constraints.
8779 * The moving should be done first to avoid moving coefficients
8780 * in the definitions of the extra integer divisions.
8782 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8783 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8785 int i, j;
8786 int n_div;
8788 bmap = isl_basic_map_cow(bmap);
8789 if (!bmap || !div)
8790 goto error;
8792 if (div->n_row < bmap->n_div)
8793 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8794 "not an expansion", goto error);
8796 n_div = bmap->n_div;
8797 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8798 div->n_row - n_div, 0,
8799 2 * (div->n_row - n_div));
8801 for (i = n_div; i < div->n_row; ++i)
8802 if (isl_basic_map_alloc_div(bmap) < 0)
8803 goto error;
8805 for (j = n_div - 1; j >= 0; --j) {
8806 if (exp[j] == j)
8807 break;
8808 isl_basic_map_swap_div(bmap, j, exp[j]);
8810 j = 0;
8811 for (i = 0; i < div->n_row; ++i) {
8812 if (j < n_div && exp[j] == i) {
8813 j++;
8814 } else {
8815 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8816 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8817 continue;
8818 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8819 goto error;
8823 isl_mat_free(div);
8824 return bmap;
8825 error:
8826 isl_basic_map_free(bmap);
8827 isl_mat_free(div);
8828 return NULL;
8831 /* Apply the expansion computed by isl_merge_divs.
8832 * The expansion itself is given by "exp" while the resulting
8833 * list of divs is given by "div".
8835 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8836 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8838 return isl_basic_map_expand_divs(bset, div, exp);
8841 /* Look for a div in dst that corresponds to the div "div" in src.
8842 * The divs before "div" in src and dst are assumed to be the same.
8844 * Returns -1 if no corresponding div was found and the position
8845 * of the corresponding div in dst otherwise.
8847 static int find_div(__isl_keep isl_basic_map *dst,
8848 __isl_keep isl_basic_map *src, unsigned div)
8850 int i;
8852 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8854 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8855 for (i = div; i < dst->n_div; ++i)
8856 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8857 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8858 dst->n_div - div) == -1)
8859 return i;
8860 return -1;
8863 /* Align the divs of "dst" to those of "src", adding divs from "src"
8864 * if needed. That is, make sure that the first src->n_div divs
8865 * of the result are equal to those of src.
8867 * The result is not finalized as by design it will have redundant
8868 * divs if any divs from "src" were copied.
8870 __isl_give isl_basic_map *isl_basic_map_align_divs(
8871 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8873 int i;
8874 isl_bool known;
8875 int extended;
8876 unsigned total;
8878 if (!dst || !src)
8879 return isl_basic_map_free(dst);
8881 if (src->n_div == 0)
8882 return dst;
8884 known = isl_basic_map_divs_known(src);
8885 if (known < 0)
8886 return isl_basic_map_free(dst);
8887 if (!known)
8888 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8889 "some src divs are unknown",
8890 return isl_basic_map_free(dst));
8892 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8893 if (!src)
8894 return isl_basic_map_free(dst);
8896 extended = 0;
8897 total = isl_space_dim(src->dim, isl_dim_all);
8898 for (i = 0; i < src->n_div; ++i) {
8899 int j = find_div(dst, src, i);
8900 if (j < 0) {
8901 if (!extended) {
8902 int extra = src->n_div - i;
8903 dst = isl_basic_map_cow(dst);
8904 if (!dst)
8905 goto error;
8906 dst = isl_basic_map_extend_space(dst,
8907 isl_space_copy(dst->dim),
8908 extra, 0, 2 * extra);
8909 extended = 1;
8911 j = isl_basic_map_alloc_div(dst);
8912 if (j < 0)
8913 goto error;
8914 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8915 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8916 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8917 goto error;
8919 if (j != i)
8920 isl_basic_map_swap_div(dst, i, j);
8922 isl_basic_map_free(src);
8923 return dst;
8924 error:
8925 isl_basic_map_free(src);
8926 isl_basic_map_free(dst);
8927 return NULL;
8930 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8932 int i;
8934 if (!map)
8935 return NULL;
8936 if (map->n == 0)
8937 return map;
8938 map = isl_map_compute_divs(map);
8939 map = isl_map_cow(map);
8940 if (!map)
8941 return NULL;
8943 for (i = 1; i < map->n; ++i)
8944 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8945 for (i = 1; i < map->n; ++i) {
8946 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8947 if (!map->p[i])
8948 return isl_map_free(map);
8951 map = isl_map_unmark_normalized(map);
8952 return map;
8955 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8957 return isl_map_align_divs_internal(map);
8960 struct isl_set *isl_set_align_divs(struct isl_set *set)
8962 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8965 /* Align the divs of the basic maps in "map" to those
8966 * of the basic maps in "list", as well as to the other basic maps in "map".
8967 * The elements in "list" are assumed to have known divs.
8969 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8970 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8972 int i, n;
8974 map = isl_map_compute_divs(map);
8975 map = isl_map_cow(map);
8976 if (!map || !list)
8977 return isl_map_free(map);
8978 if (map->n == 0)
8979 return map;
8981 n = isl_basic_map_list_n_basic_map(list);
8982 for (i = 0; i < n; ++i) {
8983 isl_basic_map *bmap;
8985 bmap = isl_basic_map_list_get_basic_map(list, i);
8986 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8987 isl_basic_map_free(bmap);
8989 if (!map->p[0])
8990 return isl_map_free(map);
8992 return isl_map_align_divs_internal(map);
8995 /* Align the divs of each element of "list" to those of "bmap".
8996 * Both "bmap" and the elements of "list" are assumed to have known divs.
8998 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8999 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9001 int i, n;
9003 if (!list || !bmap)
9004 return isl_basic_map_list_free(list);
9006 n = isl_basic_map_list_n_basic_map(list);
9007 for (i = 0; i < n; ++i) {
9008 isl_basic_map *bmap_i;
9010 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9011 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9012 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9015 return list;
9018 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9019 __isl_take isl_map *map)
9021 isl_bool ok;
9023 ok = isl_map_compatible_domain(map, set);
9024 if (ok < 0)
9025 goto error;
9026 if (!ok)
9027 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9028 "incompatible spaces", goto error);
9029 map = isl_map_intersect_domain(map, set);
9030 set = isl_map_range(map);
9031 return set;
9032 error:
9033 isl_set_free(set);
9034 isl_map_free(map);
9035 return NULL;
9038 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9039 __isl_take isl_map *map)
9041 return isl_map_align_params_map_map_and(set, map, &set_apply);
9044 /* There is no need to cow as removing empty parts doesn't change
9045 * the meaning of the set.
9047 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9049 int i;
9051 if (!map)
9052 return NULL;
9054 for (i = map->n - 1; i >= 0; --i)
9055 map = remove_if_empty(map, i);
9057 return map;
9060 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9062 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9065 /* Create a binary relation that maps the shared initial "pos" dimensions
9066 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9068 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9069 __isl_keep isl_basic_set *bset2, int pos)
9071 isl_basic_map *bmap1;
9072 isl_basic_map *bmap2;
9074 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9075 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9076 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9077 isl_dim_out, 0, pos);
9078 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9079 isl_dim_out, 0, pos);
9080 return isl_basic_map_range_product(bmap1, bmap2);
9083 /* Given two basic sets bset1 and bset2, compute the maximal difference
9084 * between the values of dimension pos in bset1 and those in bset2
9085 * for any common value of the parameters and dimensions preceding pos.
9087 static enum isl_lp_result basic_set_maximal_difference_at(
9088 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9089 int pos, isl_int *opt)
9091 isl_basic_map *bmap1;
9092 struct isl_ctx *ctx;
9093 struct isl_vec *obj;
9094 unsigned total;
9095 unsigned nparam;
9096 unsigned dim1;
9097 enum isl_lp_result res;
9099 if (!bset1 || !bset2)
9100 return isl_lp_error;
9102 nparam = isl_basic_set_n_param(bset1);
9103 dim1 = isl_basic_set_n_dim(bset1);
9105 bmap1 = join_initial(bset1, bset2, pos);
9106 if (!bmap1)
9107 return isl_lp_error;
9109 total = isl_basic_map_total_dim(bmap1);
9110 ctx = bmap1->ctx;
9111 obj = isl_vec_alloc(ctx, 1 + total);
9112 if (!obj)
9113 goto error;
9114 isl_seq_clr(obj->block.data, 1 + total);
9115 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9116 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9117 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9118 opt, NULL, NULL);
9119 isl_basic_map_free(bmap1);
9120 isl_vec_free(obj);
9121 return res;
9122 error:
9123 isl_basic_map_free(bmap1);
9124 return isl_lp_error;
9127 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9128 * for any common value of the parameters and dimensions preceding pos
9129 * in both basic sets, the values of dimension pos in bset1 are
9130 * smaller or larger than those in bset2.
9132 * Returns
9133 * 1 if bset1 follows bset2
9134 * -1 if bset1 precedes bset2
9135 * 0 if bset1 and bset2 are incomparable
9136 * -2 if some error occurred.
9138 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9139 __isl_keep isl_basic_set *bset2, int pos)
9141 isl_int opt;
9142 enum isl_lp_result res;
9143 int cmp;
9145 isl_int_init(opt);
9147 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9149 if (res == isl_lp_empty)
9150 cmp = 0;
9151 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9152 res == isl_lp_unbounded)
9153 cmp = 1;
9154 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9155 cmp = -1;
9156 else
9157 cmp = -2;
9159 isl_int_clear(opt);
9160 return cmp;
9163 /* Given two basic sets bset1 and bset2, check whether
9164 * for any common value of the parameters and dimensions preceding pos
9165 * there is a value of dimension pos in bset1 that is larger
9166 * than a value of the same dimension in bset2.
9168 * Return
9169 * 1 if there exists such a pair
9170 * 0 if there is no such pair, but there is a pair of equal values
9171 * -1 otherwise
9172 * -2 if some error occurred.
9174 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9175 __isl_keep isl_basic_set *bset2, int pos)
9177 isl_bool empty;
9178 isl_basic_map *bmap;
9179 unsigned dim1;
9181 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9182 bmap = join_initial(bset1, bset2, pos);
9183 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9184 isl_dim_out, dim1 - pos);
9185 empty = isl_basic_map_is_empty(bmap);
9186 if (empty < 0)
9187 goto error;
9188 if (empty) {
9189 isl_basic_map_free(bmap);
9190 return -1;
9192 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9193 isl_dim_out, dim1 - pos);
9194 empty = isl_basic_map_is_empty(bmap);
9195 if (empty < 0)
9196 goto error;
9197 isl_basic_map_free(bmap);
9198 if (empty)
9199 return 0;
9200 return 1;
9201 error:
9202 isl_basic_map_free(bmap);
9203 return -2;
9206 /* Given two sets set1 and set2, check whether
9207 * for any common value of the parameters and dimensions preceding pos
9208 * there is a value of dimension pos in set1 that is larger
9209 * than a value of the same dimension in set2.
9211 * Return
9212 * 1 if there exists such a pair
9213 * 0 if there is no such pair, but there is a pair of equal values
9214 * -1 otherwise
9215 * -2 if some error occurred.
9217 int isl_set_follows_at(__isl_keep isl_set *set1,
9218 __isl_keep isl_set *set2, int pos)
9220 int i, j;
9221 int follows = -1;
9223 if (!set1 || !set2)
9224 return -2;
9226 for (i = 0; i < set1->n; ++i)
9227 for (j = 0; j < set2->n; ++j) {
9228 int f;
9229 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9230 if (f == 1 || f == -2)
9231 return f;
9232 if (f > follows)
9233 follows = f;
9236 return follows;
9239 static isl_bool isl_basic_map_plain_has_fixed_var(
9240 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9242 int i;
9243 int d;
9244 unsigned total;
9246 if (!bmap)
9247 return isl_bool_error;
9248 total = isl_basic_map_total_dim(bmap);
9249 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9250 for (; d+1 > pos; --d)
9251 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9252 break;
9253 if (d != pos)
9254 continue;
9255 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9256 return isl_bool_false;
9257 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9258 return isl_bool_false;
9259 if (!isl_int_is_one(bmap->eq[i][1+d]))
9260 return isl_bool_false;
9261 if (val)
9262 isl_int_neg(*val, bmap->eq[i][0]);
9263 return isl_bool_true;
9265 return isl_bool_false;
9268 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9269 unsigned pos, isl_int *val)
9271 int i;
9272 isl_int v;
9273 isl_int tmp;
9274 isl_bool fixed;
9276 if (!map)
9277 return isl_bool_error;
9278 if (map->n == 0)
9279 return isl_bool_false;
9280 if (map->n == 1)
9281 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9282 isl_int_init(v);
9283 isl_int_init(tmp);
9284 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9285 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9286 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9287 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9288 fixed = isl_bool_false;
9290 if (val)
9291 isl_int_set(*val, v);
9292 isl_int_clear(tmp);
9293 isl_int_clear(v);
9294 return fixed;
9297 static isl_bool isl_basic_set_plain_has_fixed_var(
9298 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9300 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9301 pos, val);
9304 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9305 enum isl_dim_type type, unsigned pos, isl_int *val)
9307 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9308 return isl_bool_error;
9309 return isl_basic_map_plain_has_fixed_var(bmap,
9310 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9313 /* If "bmap" obviously lies on a hyperplane where the given dimension
9314 * has a fixed value, then return that value.
9315 * Otherwise return NaN.
9317 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9318 __isl_keep isl_basic_map *bmap,
9319 enum isl_dim_type type, unsigned pos)
9321 isl_ctx *ctx;
9322 isl_val *v;
9323 isl_bool fixed;
9325 if (!bmap)
9326 return NULL;
9327 ctx = isl_basic_map_get_ctx(bmap);
9328 v = isl_val_alloc(ctx);
9329 if (!v)
9330 return NULL;
9331 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9332 if (fixed < 0)
9333 return isl_val_free(v);
9334 if (fixed) {
9335 isl_int_set_si(v->d, 1);
9336 return v;
9338 isl_val_free(v);
9339 return isl_val_nan(ctx);
9342 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9343 enum isl_dim_type type, unsigned pos, isl_int *val)
9345 if (isl_map_check_range(map, type, pos, 1) < 0)
9346 return isl_bool_error;
9347 return isl_map_plain_has_fixed_var(map,
9348 map_offset(map, type) - 1 + pos, val);
9351 /* If "map" obviously lies on a hyperplane where the given dimension
9352 * has a fixed value, then return that value.
9353 * Otherwise return NaN.
9355 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9356 enum isl_dim_type type, unsigned pos)
9358 isl_ctx *ctx;
9359 isl_val *v;
9360 isl_bool fixed;
9362 if (!map)
9363 return NULL;
9364 ctx = isl_map_get_ctx(map);
9365 v = isl_val_alloc(ctx);
9366 if (!v)
9367 return NULL;
9368 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9369 if (fixed < 0)
9370 return isl_val_free(v);
9371 if (fixed) {
9372 isl_int_set_si(v->d, 1);
9373 return v;
9375 isl_val_free(v);
9376 return isl_val_nan(ctx);
9379 /* If "set" obviously lies on a hyperplane where the given dimension
9380 * has a fixed value, then return that value.
9381 * Otherwise return NaN.
9383 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9384 enum isl_dim_type type, unsigned pos)
9386 return isl_map_plain_get_val_if_fixed(set, type, pos);
9389 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9390 * then return this fixed value in *val.
9392 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9393 unsigned dim, isl_int *val)
9395 return isl_basic_set_plain_has_fixed_var(bset,
9396 isl_basic_set_n_param(bset) + dim, val);
9399 /* Return -1 if the constraint "c1" should be sorted before "c2"
9400 * and 1 if it should be sorted after "c2".
9401 * Return 0 if the two constraints are the same (up to the constant term).
9403 * In particular, if a constraint involves later variables than another
9404 * then it is sorted after this other constraint.
9405 * uset_gist depends on constraints without existentially quantified
9406 * variables sorting first.
9408 * For constraints that have the same latest variable, those
9409 * with the same coefficient for this latest variable (first in absolute value
9410 * and then in actual value) are grouped together.
9411 * This is useful for detecting pairs of constraints that can
9412 * be chained in their printed representation.
9414 * Finally, within a group, constraints are sorted according to
9415 * their coefficients (excluding the constant term).
9417 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9419 isl_int **c1 = (isl_int **) p1;
9420 isl_int **c2 = (isl_int **) p2;
9421 int l1, l2;
9422 unsigned size = *(unsigned *) arg;
9423 int cmp;
9425 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9426 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9428 if (l1 != l2)
9429 return l1 - l2;
9431 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9432 if (cmp != 0)
9433 return cmp;
9434 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9435 if (cmp != 0)
9436 return -cmp;
9438 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9441 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9442 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9443 * and 0 if the two constraints are the same (up to the constant term).
9445 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9446 isl_int *c1, isl_int *c2)
9448 unsigned total;
9450 if (!bmap)
9451 return -2;
9452 total = isl_basic_map_total_dim(bmap);
9453 return sort_constraint_cmp(&c1, &c2, &total);
9456 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9457 __isl_take isl_basic_map *bmap)
9459 unsigned total;
9461 if (!bmap)
9462 return NULL;
9463 if (bmap->n_ineq == 0)
9464 return bmap;
9465 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9466 return bmap;
9467 total = isl_basic_map_total_dim(bmap);
9468 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9469 &sort_constraint_cmp, &total) < 0)
9470 return isl_basic_map_free(bmap);
9471 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9472 return bmap;
9475 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9476 __isl_take isl_basic_set *bset)
9478 isl_basic_map *bmap = bset_to_bmap(bset);
9479 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9482 __isl_give isl_basic_map *isl_basic_map_normalize(
9483 __isl_take isl_basic_map *bmap)
9485 bmap = isl_basic_map_remove_redundancies(bmap);
9486 bmap = isl_basic_map_sort_constraints(bmap);
9487 return bmap;
9489 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9490 __isl_keep isl_basic_map *bmap2)
9492 int i, cmp;
9493 unsigned total;
9494 isl_space *space1, *space2;
9496 if (!bmap1 || !bmap2)
9497 return -1;
9499 if (bmap1 == bmap2)
9500 return 0;
9501 space1 = isl_basic_map_peek_space(bmap1);
9502 space2 = isl_basic_map_peek_space(bmap2);
9503 cmp = isl_space_cmp(space1, space2);
9504 if (cmp)
9505 return cmp;
9506 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9507 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9508 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9509 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9510 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9511 return 0;
9512 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9513 return 1;
9514 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9515 return -1;
9516 if (bmap1->n_eq != bmap2->n_eq)
9517 return bmap1->n_eq - bmap2->n_eq;
9518 if (bmap1->n_ineq != bmap2->n_ineq)
9519 return bmap1->n_ineq - bmap2->n_ineq;
9520 if (bmap1->n_div != bmap2->n_div)
9521 return bmap1->n_div - bmap2->n_div;
9522 total = isl_basic_map_total_dim(bmap1);
9523 for (i = 0; i < bmap1->n_eq; ++i) {
9524 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9525 if (cmp)
9526 return cmp;
9528 for (i = 0; i < bmap1->n_ineq; ++i) {
9529 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9530 if (cmp)
9531 return cmp;
9533 for (i = 0; i < bmap1->n_div; ++i) {
9534 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9535 if (cmp)
9536 return cmp;
9538 return 0;
9541 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9542 __isl_keep isl_basic_set *bset2)
9544 return isl_basic_map_plain_cmp(bset1, bset2);
9547 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9549 int i, cmp;
9551 if (set1 == set2)
9552 return 0;
9553 if (set1->n != set2->n)
9554 return set1->n - set2->n;
9556 for (i = 0; i < set1->n; ++i) {
9557 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9558 if (cmp)
9559 return cmp;
9562 return 0;
9565 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9566 __isl_keep isl_basic_map *bmap2)
9568 if (!bmap1 || !bmap2)
9569 return isl_bool_error;
9570 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9573 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9574 __isl_keep isl_basic_set *bset2)
9576 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9577 bset_to_bmap(bset2));
9580 static int qsort_bmap_cmp(const void *p1, const void *p2)
9582 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9583 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9585 return isl_basic_map_plain_cmp(bmap1, bmap2);
9588 /* Sort the basic maps of "map" and remove duplicate basic maps.
9590 * While removing basic maps, we make sure that the basic maps remain
9591 * sorted because isl_map_normalize expects the basic maps of the result
9592 * to be sorted.
9594 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9596 int i, j;
9598 map = isl_map_remove_empty_parts(map);
9599 if (!map)
9600 return NULL;
9601 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9602 for (i = map->n - 1; i >= 1; --i) {
9603 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9604 continue;
9605 isl_basic_map_free(map->p[i-1]);
9606 for (j = i; j < map->n; ++j)
9607 map->p[j - 1] = map->p[j];
9608 map->n--;
9611 return map;
9614 /* Remove obvious duplicates among the basic maps of "map".
9616 * Unlike isl_map_normalize, this function does not remove redundant
9617 * constraints and only removes duplicates that have exactly the same
9618 * constraints in the input. It does sort the constraints and
9619 * the basic maps to ease the detection of duplicates.
9621 * If "map" has already been normalized or if the basic maps are
9622 * disjoint, then there can be no duplicates.
9624 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9626 int i;
9627 isl_basic_map *bmap;
9629 if (!map)
9630 return NULL;
9631 if (map->n <= 1)
9632 return map;
9633 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9634 return map;
9635 for (i = 0; i < map->n; ++i) {
9636 bmap = isl_basic_map_copy(map->p[i]);
9637 bmap = isl_basic_map_sort_constraints(bmap);
9638 if (!bmap)
9639 return isl_map_free(map);
9640 isl_basic_map_free(map->p[i]);
9641 map->p[i] = bmap;
9644 map = sort_and_remove_duplicates(map);
9645 return map;
9648 /* We normalize in place, but if anything goes wrong we need
9649 * to return NULL, so we need to make sure we don't change the
9650 * meaning of any possible other copies of map.
9652 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9654 int i;
9655 struct isl_basic_map *bmap;
9657 if (!map)
9658 return NULL;
9659 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9660 return map;
9661 for (i = 0; i < map->n; ++i) {
9662 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9663 if (!bmap)
9664 goto error;
9665 isl_basic_map_free(map->p[i]);
9666 map->p[i] = bmap;
9669 map = sort_and_remove_duplicates(map);
9670 if (map)
9671 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9672 return map;
9673 error:
9674 isl_map_free(map);
9675 return NULL;
9678 struct isl_set *isl_set_normalize(struct isl_set *set)
9680 return set_from_map(isl_map_normalize(set_to_map(set)));
9683 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9684 __isl_keep isl_map *map2)
9686 int i;
9687 isl_bool equal;
9689 if (!map1 || !map2)
9690 return isl_bool_error;
9692 if (map1 == map2)
9693 return isl_bool_true;
9694 if (!isl_space_is_equal(map1->dim, map2->dim))
9695 return isl_bool_false;
9697 map1 = isl_map_copy(map1);
9698 map2 = isl_map_copy(map2);
9699 map1 = isl_map_normalize(map1);
9700 map2 = isl_map_normalize(map2);
9701 if (!map1 || !map2)
9702 goto error;
9703 equal = map1->n == map2->n;
9704 for (i = 0; equal && i < map1->n; ++i) {
9705 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9706 if (equal < 0)
9707 goto error;
9709 isl_map_free(map1);
9710 isl_map_free(map2);
9711 return equal;
9712 error:
9713 isl_map_free(map1);
9714 isl_map_free(map2);
9715 return isl_bool_error;
9718 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9719 __isl_keep isl_set *set2)
9721 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9724 /* Return the basic maps in "map" as a list.
9726 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9727 __isl_keep isl_map *map)
9729 int i;
9730 isl_ctx *ctx;
9731 isl_basic_map_list *list;
9733 if (!map)
9734 return NULL;
9735 ctx = isl_map_get_ctx(map);
9736 list = isl_basic_map_list_alloc(ctx, map->n);
9738 for (i = 0; i < map->n; ++i) {
9739 isl_basic_map *bmap;
9741 bmap = isl_basic_map_copy(map->p[i]);
9742 list = isl_basic_map_list_add(list, bmap);
9745 return list;
9748 /* Return the intersection of the elements in the non-empty list "list".
9749 * All elements are assumed to live in the same space.
9751 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9752 __isl_take isl_basic_map_list *list)
9754 int i, n;
9755 isl_basic_map *bmap;
9757 if (!list)
9758 return NULL;
9759 n = isl_basic_map_list_n_basic_map(list);
9760 if (n < 1)
9761 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9762 "expecting non-empty list", goto error);
9764 bmap = isl_basic_map_list_get_basic_map(list, 0);
9765 for (i = 1; i < n; ++i) {
9766 isl_basic_map *bmap_i;
9768 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9769 bmap = isl_basic_map_intersect(bmap, bmap_i);
9772 isl_basic_map_list_free(list);
9773 return bmap;
9774 error:
9775 isl_basic_map_list_free(list);
9776 return NULL;
9779 /* Return the intersection of the elements in the non-empty list "list".
9780 * All elements are assumed to live in the same space.
9782 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9783 __isl_take isl_basic_set_list *list)
9785 return isl_basic_map_list_intersect(list);
9788 /* Return the union of the elements of "list".
9789 * The list is required to have at least one element.
9791 __isl_give isl_set *isl_basic_set_list_union(
9792 __isl_take isl_basic_set_list *list)
9794 int i, n;
9795 isl_space *space;
9796 isl_basic_set *bset;
9797 isl_set *set;
9799 if (!list)
9800 return NULL;
9801 n = isl_basic_set_list_n_basic_set(list);
9802 if (n < 1)
9803 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9804 "expecting non-empty list", goto error);
9806 bset = isl_basic_set_list_get_basic_set(list, 0);
9807 space = isl_basic_set_get_space(bset);
9808 isl_basic_set_free(bset);
9810 set = isl_set_alloc_space(space, n, 0);
9811 for (i = 0; i < n; ++i) {
9812 bset = isl_basic_set_list_get_basic_set(list, i);
9813 set = isl_set_add_basic_set(set, bset);
9816 isl_basic_set_list_free(list);
9817 return set;
9818 error:
9819 isl_basic_set_list_free(list);
9820 return NULL;
9823 /* Return the union of the elements in the non-empty list "list".
9824 * All elements are assumed to live in the same space.
9826 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9828 int i, n;
9829 isl_set *set;
9831 if (!list)
9832 return NULL;
9833 n = isl_set_list_n_set(list);
9834 if (n < 1)
9835 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9836 "expecting non-empty list", goto error);
9838 set = isl_set_list_get_set(list, 0);
9839 for (i = 1; i < n; ++i) {
9840 isl_set *set_i;
9842 set_i = isl_set_list_get_set(list, i);
9843 set = isl_set_union(set, set_i);
9846 isl_set_list_free(list);
9847 return set;
9848 error:
9849 isl_set_list_free(list);
9850 return NULL;
9853 __isl_give isl_basic_map *isl_basic_map_product(
9854 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9856 isl_space *space_result = NULL;
9857 struct isl_basic_map *bmap;
9858 unsigned in1, in2, out1, out2, nparam, total, pos;
9859 struct isl_dim_map *dim_map1, *dim_map2;
9861 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9862 goto error;
9863 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9864 isl_space_copy(bmap2->dim));
9866 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9867 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9868 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9869 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9870 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9872 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9873 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9874 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9875 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9876 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9877 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9878 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9879 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9880 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9881 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9882 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9884 bmap = isl_basic_map_alloc_space(space_result,
9885 bmap1->n_div + bmap2->n_div,
9886 bmap1->n_eq + bmap2->n_eq,
9887 bmap1->n_ineq + bmap2->n_ineq);
9888 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9889 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9890 bmap = isl_basic_map_simplify(bmap);
9891 return isl_basic_map_finalize(bmap);
9892 error:
9893 isl_basic_map_free(bmap1);
9894 isl_basic_map_free(bmap2);
9895 return NULL;
9898 __isl_give isl_basic_map *isl_basic_map_flat_product(
9899 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9901 isl_basic_map *prod;
9903 prod = isl_basic_map_product(bmap1, bmap2);
9904 prod = isl_basic_map_flatten(prod);
9905 return prod;
9908 __isl_give isl_basic_set *isl_basic_set_flat_product(
9909 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9911 return isl_basic_map_flat_range_product(bset1, bset2);
9914 __isl_give isl_basic_map *isl_basic_map_domain_product(
9915 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9917 isl_space *space_result = NULL;
9918 isl_basic_map *bmap;
9919 unsigned in1, in2, out, nparam, total, pos;
9920 struct isl_dim_map *dim_map1, *dim_map2;
9922 if (!bmap1 || !bmap2)
9923 goto error;
9925 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9926 isl_space_copy(bmap2->dim));
9928 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9929 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9930 out = isl_basic_map_dim(bmap1, isl_dim_out);
9931 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9933 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9934 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9935 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9936 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9937 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9938 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9939 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9940 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9941 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9942 isl_dim_map_div(dim_map1, bmap1, pos += out);
9943 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9945 bmap = isl_basic_map_alloc_space(space_result,
9946 bmap1->n_div + bmap2->n_div,
9947 bmap1->n_eq + bmap2->n_eq,
9948 bmap1->n_ineq + bmap2->n_ineq);
9949 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9950 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9951 bmap = isl_basic_map_simplify(bmap);
9952 return isl_basic_map_finalize(bmap);
9953 error:
9954 isl_basic_map_free(bmap1);
9955 isl_basic_map_free(bmap2);
9956 return NULL;
9959 __isl_give isl_basic_map *isl_basic_map_range_product(
9960 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9962 isl_bool rational;
9963 isl_space *space_result = NULL;
9964 isl_basic_map *bmap;
9965 unsigned in, out1, out2, nparam, total, pos;
9966 struct isl_dim_map *dim_map1, *dim_map2;
9968 rational = isl_basic_map_is_rational(bmap1);
9969 if (rational >= 0 && rational)
9970 rational = isl_basic_map_is_rational(bmap2);
9971 if (!bmap1 || !bmap2 || rational < 0)
9972 goto error;
9974 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9975 goto error;
9977 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9978 isl_space_copy(bmap2->dim));
9980 in = isl_basic_map_dim(bmap1, isl_dim_in);
9981 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9982 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9983 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9985 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9986 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9987 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9988 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9989 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9990 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9991 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9992 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9993 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9994 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9995 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9997 bmap = isl_basic_map_alloc_space(space_result,
9998 bmap1->n_div + bmap2->n_div,
9999 bmap1->n_eq + bmap2->n_eq,
10000 bmap1->n_ineq + bmap2->n_ineq);
10001 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10002 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10003 if (rational)
10004 bmap = isl_basic_map_set_rational(bmap);
10005 bmap = isl_basic_map_simplify(bmap);
10006 return isl_basic_map_finalize(bmap);
10007 error:
10008 isl_basic_map_free(bmap1);
10009 isl_basic_map_free(bmap2);
10010 return NULL;
10013 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10014 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10016 isl_basic_map *prod;
10018 prod = isl_basic_map_range_product(bmap1, bmap2);
10019 prod = isl_basic_map_flatten_range(prod);
10020 return prod;
10023 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10024 * and collect the results.
10025 * The result live in the space obtained by calling "space_product"
10026 * on the spaces of "map1" and "map2".
10027 * If "remove_duplicates" is set then the result may contain duplicates
10028 * (even if the inputs do not) and so we try and remove the obvious
10029 * duplicates.
10031 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10032 __isl_take isl_map *map2,
10033 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10034 __isl_take isl_space *right),
10035 __isl_give isl_basic_map *(*basic_map_product)(
10036 __isl_take isl_basic_map *left,
10037 __isl_take isl_basic_map *right),
10038 int remove_duplicates)
10040 unsigned flags = 0;
10041 struct isl_map *result;
10042 int i, j;
10043 isl_bool m;
10045 m = isl_map_has_equal_params(map1, map2);
10046 if (m < 0)
10047 goto error;
10048 if (!m)
10049 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10050 "parameters don't match", goto error);
10052 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10053 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10054 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10056 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10057 isl_space_copy(map2->dim)),
10058 map1->n * map2->n, flags);
10059 if (!result)
10060 goto error;
10061 for (i = 0; i < map1->n; ++i)
10062 for (j = 0; j < map2->n; ++j) {
10063 struct isl_basic_map *part;
10064 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10065 isl_basic_map_copy(map2->p[j]));
10066 if (isl_basic_map_is_empty(part))
10067 isl_basic_map_free(part);
10068 else
10069 result = isl_map_add_basic_map(result, part);
10070 if (!result)
10071 goto error;
10073 if (remove_duplicates)
10074 result = isl_map_remove_obvious_duplicates(result);
10075 isl_map_free(map1);
10076 isl_map_free(map2);
10077 return result;
10078 error:
10079 isl_map_free(map1);
10080 isl_map_free(map2);
10081 return NULL;
10084 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10086 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10087 __isl_take isl_map *map2)
10089 return map_product(map1, map2, &isl_space_product,
10090 &isl_basic_map_product, 0);
10093 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10094 __isl_take isl_map *map2)
10096 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10099 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10101 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10102 __isl_take isl_map *map2)
10104 isl_map *prod;
10106 prod = isl_map_product(map1, map2);
10107 prod = isl_map_flatten(prod);
10108 return prod;
10111 /* Given two set A and B, construct its Cartesian product A x B.
10113 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10115 return isl_map_range_product(set1, set2);
10118 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10119 __isl_take isl_set *set2)
10121 return isl_map_flat_range_product(set1, set2);
10124 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10126 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10127 __isl_take isl_map *map2)
10129 return map_product(map1, map2, &isl_space_domain_product,
10130 &isl_basic_map_domain_product, 1);
10133 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10135 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10136 __isl_take isl_map *map2)
10138 return map_product(map1, map2, &isl_space_range_product,
10139 &isl_basic_map_range_product, 1);
10142 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10143 __isl_take isl_map *map2)
10145 return isl_map_align_params_map_map_and(map1, map2,
10146 &map_domain_product_aligned);
10149 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10150 __isl_take isl_map *map2)
10152 return isl_map_align_params_map_map_and(map1, map2,
10153 &map_range_product_aligned);
10156 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10158 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10160 isl_space *space;
10161 int total1, keep1, total2, keep2;
10163 if (!map)
10164 return NULL;
10165 if (!isl_space_domain_is_wrapping(map->dim) ||
10166 !isl_space_range_is_wrapping(map->dim))
10167 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10168 "not a product", return isl_map_free(map));
10170 space = isl_map_get_space(map);
10171 total1 = isl_space_dim(space, isl_dim_in);
10172 total2 = isl_space_dim(space, isl_dim_out);
10173 space = isl_space_factor_domain(space);
10174 keep1 = isl_space_dim(space, isl_dim_in);
10175 keep2 = isl_space_dim(space, isl_dim_out);
10176 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10177 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10178 map = isl_map_reset_space(map, space);
10180 return map;
10183 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10185 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10187 isl_space *space;
10188 int total1, keep1, total2, keep2;
10190 if (!map)
10191 return NULL;
10192 if (!isl_space_domain_is_wrapping(map->dim) ||
10193 !isl_space_range_is_wrapping(map->dim))
10194 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10195 "not a product", return isl_map_free(map));
10197 space = isl_map_get_space(map);
10198 total1 = isl_space_dim(space, isl_dim_in);
10199 total2 = isl_space_dim(space, isl_dim_out);
10200 space = isl_space_factor_range(space);
10201 keep1 = isl_space_dim(space, isl_dim_in);
10202 keep2 = isl_space_dim(space, isl_dim_out);
10203 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10204 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10205 map = isl_map_reset_space(map, space);
10207 return map;
10210 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10212 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10214 isl_space *space;
10215 int total, keep;
10217 if (!map)
10218 return NULL;
10219 if (!isl_space_domain_is_wrapping(map->dim))
10220 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10221 "domain is not a product", return isl_map_free(map));
10223 space = isl_map_get_space(map);
10224 total = isl_space_dim(space, isl_dim_in);
10225 space = isl_space_domain_factor_domain(space);
10226 keep = isl_space_dim(space, isl_dim_in);
10227 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10228 map = isl_map_reset_space(map, space);
10230 return map;
10233 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10235 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10237 isl_space *space;
10238 int total, keep;
10240 if (!map)
10241 return NULL;
10242 if (!isl_space_domain_is_wrapping(map->dim))
10243 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10244 "domain is not a product", return isl_map_free(map));
10246 space = isl_map_get_space(map);
10247 total = isl_space_dim(space, isl_dim_in);
10248 space = isl_space_domain_factor_range(space);
10249 keep = isl_space_dim(space, isl_dim_in);
10250 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10251 map = isl_map_reset_space(map, space);
10253 return map;
10256 /* Given a map A -> [B -> C], extract the map A -> B.
10258 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10260 isl_space *space;
10261 int total, keep;
10263 if (!map)
10264 return NULL;
10265 if (!isl_space_range_is_wrapping(map->dim))
10266 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10267 "range is not a product", return isl_map_free(map));
10269 space = isl_map_get_space(map);
10270 total = isl_space_dim(space, isl_dim_out);
10271 space = isl_space_range_factor_domain(space);
10272 keep = isl_space_dim(space, isl_dim_out);
10273 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10274 map = isl_map_reset_space(map, space);
10276 return map;
10279 /* Given a map A -> [B -> C], extract the map A -> C.
10281 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10283 isl_space *space;
10284 int total, keep;
10286 if (!map)
10287 return NULL;
10288 if (!isl_space_range_is_wrapping(map->dim))
10289 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10290 "range is not a product", return isl_map_free(map));
10292 space = isl_map_get_space(map);
10293 total = isl_space_dim(space, isl_dim_out);
10294 space = isl_space_range_factor_range(space);
10295 keep = isl_space_dim(space, isl_dim_out);
10296 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10297 map = isl_map_reset_space(map, space);
10299 return map;
10302 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10304 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10305 __isl_take isl_map *map2)
10307 isl_map *prod;
10309 prod = isl_map_domain_product(map1, map2);
10310 prod = isl_map_flatten_domain(prod);
10311 return prod;
10314 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10316 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10317 __isl_take isl_map *map2)
10319 isl_map *prod;
10321 prod = isl_map_range_product(map1, map2);
10322 prod = isl_map_flatten_range(prod);
10323 return prod;
10326 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10328 int i;
10329 uint32_t hash = isl_hash_init();
10330 unsigned total;
10332 if (!bmap)
10333 return 0;
10334 bmap = isl_basic_map_copy(bmap);
10335 bmap = isl_basic_map_normalize(bmap);
10336 if (!bmap)
10337 return 0;
10338 total = isl_basic_map_total_dim(bmap);
10339 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10340 for (i = 0; i < bmap->n_eq; ++i) {
10341 uint32_t c_hash;
10342 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10343 isl_hash_hash(hash, c_hash);
10345 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10346 for (i = 0; i < bmap->n_ineq; ++i) {
10347 uint32_t c_hash;
10348 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10349 isl_hash_hash(hash, c_hash);
10351 isl_hash_byte(hash, bmap->n_div & 0xFF);
10352 for (i = 0; i < bmap->n_div; ++i) {
10353 uint32_t c_hash;
10354 if (isl_int_is_zero(bmap->div[i][0]))
10355 continue;
10356 isl_hash_byte(hash, i & 0xFF);
10357 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10358 isl_hash_hash(hash, c_hash);
10360 isl_basic_map_free(bmap);
10361 return hash;
10364 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10366 return isl_basic_map_get_hash(bset_to_bmap(bset));
10369 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10371 int i;
10372 uint32_t hash;
10374 if (!map)
10375 return 0;
10376 map = isl_map_copy(map);
10377 map = isl_map_normalize(map);
10378 if (!map)
10379 return 0;
10381 hash = isl_hash_init();
10382 for (i = 0; i < map->n; ++i) {
10383 uint32_t bmap_hash;
10384 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10385 isl_hash_hash(hash, bmap_hash);
10388 isl_map_free(map);
10390 return hash;
10393 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10395 return isl_map_get_hash(set_to_map(set));
10398 /* Return the number of basic maps in the (current) representation of "map".
10400 int isl_map_n_basic_map(__isl_keep isl_map *map)
10402 return map ? map->n : 0;
10405 int isl_set_n_basic_set(__isl_keep isl_set *set)
10407 return set ? set->n : 0;
10410 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10411 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10413 int i;
10415 if (!map)
10416 return isl_stat_error;
10418 for (i = 0; i < map->n; ++i)
10419 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10420 return isl_stat_error;
10422 return isl_stat_ok;
10425 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10426 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10428 int i;
10430 if (!set)
10431 return isl_stat_error;
10433 for (i = 0; i < set->n; ++i)
10434 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10435 return isl_stat_error;
10437 return isl_stat_ok;
10440 /* Return a list of basic sets, the union of which is equal to "set".
10442 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10443 __isl_keep isl_set *set)
10445 int i;
10446 isl_basic_set_list *list;
10448 if (!set)
10449 return NULL;
10451 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10452 for (i = 0; i < set->n; ++i) {
10453 isl_basic_set *bset;
10455 bset = isl_basic_set_copy(set->p[i]);
10456 list = isl_basic_set_list_add(list, bset);
10459 return list;
10462 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10464 isl_space *space;
10466 if (!bset)
10467 return NULL;
10469 bset = isl_basic_set_cow(bset);
10470 if (!bset)
10471 return NULL;
10473 space = isl_basic_set_get_space(bset);
10474 space = isl_space_lift(space, bset->n_div);
10475 if (!space)
10476 goto error;
10477 isl_space_free(bset->dim);
10478 bset->dim = space;
10479 bset->extra -= bset->n_div;
10480 bset->n_div = 0;
10482 bset = isl_basic_set_finalize(bset);
10484 return bset;
10485 error:
10486 isl_basic_set_free(bset);
10487 return NULL;
10490 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10492 int i;
10493 isl_space *space;
10494 unsigned n_div;
10496 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10498 if (!set)
10499 return NULL;
10501 set = isl_set_cow(set);
10502 if (!set)
10503 return NULL;
10505 n_div = set->p[0]->n_div;
10506 space = isl_set_get_space(set);
10507 space = isl_space_lift(space, n_div);
10508 if (!space)
10509 goto error;
10510 isl_space_free(set->dim);
10511 set->dim = space;
10513 for (i = 0; i < set->n; ++i) {
10514 set->p[i] = isl_basic_set_lift(set->p[i]);
10515 if (!set->p[i])
10516 goto error;
10519 return set;
10520 error:
10521 isl_set_free(set);
10522 return NULL;
10525 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10527 unsigned dim;
10528 int size = 0;
10530 if (!bset)
10531 return -1;
10533 dim = isl_basic_set_total_dim(bset);
10534 size += bset->n_eq * (1 + dim);
10535 size += bset->n_ineq * (1 + dim);
10536 size += bset->n_div * (2 + dim);
10538 return size;
10541 int isl_set_size(__isl_keep isl_set *set)
10543 int i;
10544 int size = 0;
10546 if (!set)
10547 return -1;
10549 for (i = 0; i < set->n; ++i)
10550 size += isl_basic_set_size(set->p[i]);
10552 return size;
10555 /* Check if there is any lower bound (if lower == 0) and/or upper
10556 * bound (if upper == 0) on the specified dim.
10558 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10559 enum isl_dim_type type, unsigned pos, int lower, int upper)
10561 int i;
10563 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10564 return isl_bool_error;
10566 pos += isl_basic_map_offset(bmap, type);
10568 for (i = 0; i < bmap->n_div; ++i) {
10569 if (isl_int_is_zero(bmap->div[i][0]))
10570 continue;
10571 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10572 return isl_bool_true;
10575 for (i = 0; i < bmap->n_eq; ++i)
10576 if (!isl_int_is_zero(bmap->eq[i][pos]))
10577 return isl_bool_true;
10579 for (i = 0; i < bmap->n_ineq; ++i) {
10580 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10581 if (sgn > 0)
10582 lower = 1;
10583 if (sgn < 0)
10584 upper = 1;
10587 return lower && upper;
10590 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10591 enum isl_dim_type type, unsigned pos)
10593 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10596 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10597 enum isl_dim_type type, unsigned pos)
10599 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10602 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10603 enum isl_dim_type type, unsigned pos)
10605 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10608 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10609 enum isl_dim_type type, unsigned pos)
10611 int i;
10613 if (!map)
10614 return isl_bool_error;
10616 for (i = 0; i < map->n; ++i) {
10617 isl_bool bounded;
10618 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10619 if (bounded < 0 || !bounded)
10620 return bounded;
10623 return isl_bool_true;
10626 /* Return true if the specified dim is involved in both an upper bound
10627 * and a lower bound.
10629 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10630 enum isl_dim_type type, unsigned pos)
10632 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10635 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10637 static isl_bool has_any_bound(__isl_keep isl_map *map,
10638 enum isl_dim_type type, unsigned pos,
10639 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10640 enum isl_dim_type type, unsigned pos))
10642 int i;
10644 if (!map)
10645 return isl_bool_error;
10647 for (i = 0; i < map->n; ++i) {
10648 isl_bool bounded;
10649 bounded = fn(map->p[i], type, pos);
10650 if (bounded < 0 || bounded)
10651 return bounded;
10654 return isl_bool_false;
10657 /* Return 1 if the specified dim is involved in any lower bound.
10659 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10660 enum isl_dim_type type, unsigned pos)
10662 return has_any_bound(set, type, pos,
10663 &isl_basic_map_dim_has_lower_bound);
10666 /* Return 1 if the specified dim is involved in any upper bound.
10668 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10669 enum isl_dim_type type, unsigned pos)
10671 return has_any_bound(set, type, pos,
10672 &isl_basic_map_dim_has_upper_bound);
10675 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10677 static isl_bool has_bound(__isl_keep isl_map *map,
10678 enum isl_dim_type type, unsigned pos,
10679 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10680 enum isl_dim_type type, unsigned pos))
10682 int i;
10684 if (!map)
10685 return isl_bool_error;
10687 for (i = 0; i < map->n; ++i) {
10688 isl_bool bounded;
10689 bounded = fn(map->p[i], type, pos);
10690 if (bounded < 0 || !bounded)
10691 return bounded;
10694 return isl_bool_true;
10697 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10699 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10700 enum isl_dim_type type, unsigned pos)
10702 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10705 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10707 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10708 enum isl_dim_type type, unsigned pos)
10710 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10713 /* For each of the "n" variables starting at "first", determine
10714 * the sign of the variable and put the results in the first "n"
10715 * elements of the array "signs".
10716 * Sign
10717 * 1 means that the variable is non-negative
10718 * -1 means that the variable is non-positive
10719 * 0 means the variable attains both positive and negative values.
10721 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10722 unsigned first, unsigned n, int *signs)
10724 isl_vec *bound = NULL;
10725 struct isl_tab *tab = NULL;
10726 struct isl_tab_undo *snap;
10727 int i;
10729 if (!bset || !signs)
10730 return isl_stat_error;
10732 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10733 tab = isl_tab_from_basic_set(bset, 0);
10734 if (!bound || !tab)
10735 goto error;
10737 isl_seq_clr(bound->el, bound->size);
10738 isl_int_set_si(bound->el[0], -1);
10740 snap = isl_tab_snap(tab);
10741 for (i = 0; i < n; ++i) {
10742 int empty;
10744 isl_int_set_si(bound->el[1 + first + i], -1);
10745 if (isl_tab_add_ineq(tab, bound->el) < 0)
10746 goto error;
10747 empty = tab->empty;
10748 isl_int_set_si(bound->el[1 + first + i], 0);
10749 if (isl_tab_rollback(tab, snap) < 0)
10750 goto error;
10752 if (empty) {
10753 signs[i] = 1;
10754 continue;
10757 isl_int_set_si(bound->el[1 + first + i], 1);
10758 if (isl_tab_add_ineq(tab, bound->el) < 0)
10759 goto error;
10760 empty = tab->empty;
10761 isl_int_set_si(bound->el[1 + first + i], 0);
10762 if (isl_tab_rollback(tab, snap) < 0)
10763 goto error;
10765 signs[i] = empty ? -1 : 0;
10768 isl_tab_free(tab);
10769 isl_vec_free(bound);
10770 return isl_stat_ok;
10771 error:
10772 isl_tab_free(tab);
10773 isl_vec_free(bound);
10774 return isl_stat_error;
10777 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10778 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10780 if (!bset || !signs)
10781 return isl_stat_error;
10782 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10783 return isl_stat_error;
10785 first += pos(bset->dim, type) - 1;
10786 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10789 /* Is it possible for the integer division "div" to depend (possibly
10790 * indirectly) on any output dimensions?
10792 * If the div is undefined, then we conservatively assume that it
10793 * may depend on them.
10794 * Otherwise, we check if it actually depends on them or on any integer
10795 * divisions that may depend on them.
10797 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10799 int i;
10800 unsigned n_out, o_out;
10801 unsigned n_div, o_div;
10803 if (isl_int_is_zero(bmap->div[div][0]))
10804 return isl_bool_true;
10806 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10807 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10809 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10810 return isl_bool_true;
10812 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10813 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10815 for (i = 0; i < n_div; ++i) {
10816 isl_bool may_involve;
10818 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10819 continue;
10820 may_involve = div_may_involve_output(bmap, i);
10821 if (may_involve < 0 || may_involve)
10822 return may_involve;
10825 return isl_bool_false;
10828 /* Return the first integer division of "bmap" in the range
10829 * [first, first + n[ that may depend on any output dimensions and
10830 * that has a non-zero coefficient in "c" (where the first coefficient
10831 * in "c" corresponds to integer division "first").
10833 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10834 isl_int *c, int first, int n)
10836 int k;
10838 if (!bmap)
10839 return -1;
10841 for (k = first; k < first + n; ++k) {
10842 isl_bool may_involve;
10844 if (isl_int_is_zero(c[k]))
10845 continue;
10846 may_involve = div_may_involve_output(bmap, k);
10847 if (may_involve < 0)
10848 return -1;
10849 if (may_involve)
10850 return k;
10853 return first + n;
10856 /* Look for a pair of inequality constraints in "bmap" of the form
10858 * -l + i >= 0 or i >= l
10859 * and
10860 * n + l - i >= 0 or i <= l + n
10862 * with n < "m" and i the output dimension at position "pos".
10863 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10864 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10865 * and earlier output dimensions, as well as integer divisions that do
10866 * not involve any of the output dimensions.
10868 * Return the index of the first inequality constraint or bmap->n_ineq
10869 * if no such pair can be found.
10871 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10872 int pos, isl_int m)
10874 int i, j;
10875 isl_ctx *ctx;
10876 unsigned total;
10877 unsigned n_div, o_div;
10878 unsigned n_out, o_out;
10879 int less;
10881 if (!bmap)
10882 return -1;
10884 ctx = isl_basic_map_get_ctx(bmap);
10885 total = isl_basic_map_total_dim(bmap);
10886 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10887 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10888 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10889 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10890 for (i = 0; i < bmap->n_ineq; ++i) {
10891 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10892 continue;
10893 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10894 n_out - (pos + 1)) != -1)
10895 continue;
10896 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10897 0, n_div) < n_div)
10898 continue;
10899 for (j = i + 1; j < bmap->n_ineq; ++j) {
10900 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10901 ctx->one))
10902 continue;
10903 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10904 bmap->ineq[j] + 1, total))
10905 continue;
10906 break;
10908 if (j >= bmap->n_ineq)
10909 continue;
10910 isl_int_add(bmap->ineq[i][0],
10911 bmap->ineq[i][0], bmap->ineq[j][0]);
10912 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10913 isl_int_sub(bmap->ineq[i][0],
10914 bmap->ineq[i][0], bmap->ineq[j][0]);
10915 if (!less)
10916 continue;
10917 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10918 return i;
10919 else
10920 return j;
10923 return bmap->n_ineq;
10926 /* Return the index of the equality of "bmap" that defines
10927 * the output dimension "pos" in terms of earlier dimensions.
10928 * The equality may also involve integer divisions, as long
10929 * as those integer divisions are defined in terms of
10930 * parameters or input dimensions.
10931 * In this case, *div is set to the number of integer divisions and
10932 * *ineq is set to the number of inequality constraints (provided
10933 * div and ineq are not NULL).
10935 * The equality may also involve a single integer division involving
10936 * the output dimensions (typically only output dimension "pos") as
10937 * long as the coefficient of output dimension "pos" is 1 or -1 and
10938 * there is a pair of constraints i >= l and i <= l + n, with i referring
10939 * to output dimension "pos", l an expression involving only earlier
10940 * dimensions and n smaller than the coefficient of the integer division
10941 * in the equality. In this case, the output dimension can be defined
10942 * in terms of a modulo expression that does not involve the integer division.
10943 * *div is then set to this single integer division and
10944 * *ineq is set to the index of constraint i >= l.
10946 * Return bmap->n_eq if there is no such equality.
10947 * Return -1 on error.
10949 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10950 int pos, int *div, int *ineq)
10952 int j, k, l;
10953 unsigned n_out, o_out;
10954 unsigned n_div, o_div;
10956 if (!bmap)
10957 return -1;
10959 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10960 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10961 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10962 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10964 if (ineq)
10965 *ineq = bmap->n_ineq;
10966 if (div)
10967 *div = n_div;
10968 for (j = 0; j < bmap->n_eq; ++j) {
10969 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10970 continue;
10971 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10972 n_out - (pos + 1)) != -1)
10973 continue;
10974 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10975 0, n_div);
10976 if (k >= n_div)
10977 return j;
10978 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10979 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10980 continue;
10981 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10982 k + 1, n_div - (k+1)) < n_div)
10983 continue;
10984 l = find_modulo_constraint_pair(bmap, pos,
10985 bmap->eq[j][o_div + k]);
10986 if (l < 0)
10987 return -1;
10988 if (l >= bmap->n_ineq)
10989 continue;
10990 if (div)
10991 *div = k;
10992 if (ineq)
10993 *ineq = l;
10994 return j;
10997 return bmap->n_eq;
11000 /* Check if the given basic map is obviously single-valued.
11001 * In particular, for each output dimension, check that there is
11002 * an equality that defines the output dimension in terms of
11003 * earlier dimensions.
11005 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11007 int i;
11008 unsigned n_out;
11010 if (!bmap)
11011 return isl_bool_error;
11013 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11015 for (i = 0; i < n_out; ++i) {
11016 int eq;
11018 eq = isl_basic_map_output_defining_equality(bmap, i,
11019 NULL, NULL);
11020 if (eq < 0)
11021 return isl_bool_error;
11022 if (eq >= bmap->n_eq)
11023 return isl_bool_false;
11026 return isl_bool_true;
11029 /* Check if the given basic map is single-valued.
11030 * We simply compute
11032 * M \circ M^-1
11034 * and check if the result is a subset of the identity mapping.
11036 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11038 isl_space *space;
11039 isl_basic_map *test;
11040 isl_basic_map *id;
11041 isl_bool sv;
11043 sv = isl_basic_map_plain_is_single_valued(bmap);
11044 if (sv < 0 || sv)
11045 return sv;
11047 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11048 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11050 space = isl_basic_map_get_space(bmap);
11051 space = isl_space_map_from_set(isl_space_range(space));
11052 id = isl_basic_map_identity(space);
11054 sv = isl_basic_map_is_subset(test, id);
11056 isl_basic_map_free(test);
11057 isl_basic_map_free(id);
11059 return sv;
11062 /* Check if the given map is obviously single-valued.
11064 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11066 if (!map)
11067 return isl_bool_error;
11068 if (map->n == 0)
11069 return isl_bool_true;
11070 if (map->n >= 2)
11071 return isl_bool_false;
11073 return isl_basic_map_plain_is_single_valued(map->p[0]);
11076 /* Check if the given map is single-valued.
11077 * We simply compute
11079 * M \circ M^-1
11081 * and check if the result is a subset of the identity mapping.
11083 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11085 isl_space *dim;
11086 isl_map *test;
11087 isl_map *id;
11088 isl_bool sv;
11090 sv = isl_map_plain_is_single_valued(map);
11091 if (sv < 0 || sv)
11092 return sv;
11094 test = isl_map_reverse(isl_map_copy(map));
11095 test = isl_map_apply_range(test, isl_map_copy(map));
11097 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11098 id = isl_map_identity(dim);
11100 sv = isl_map_is_subset(test, id);
11102 isl_map_free(test);
11103 isl_map_free(id);
11105 return sv;
11108 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11110 isl_bool in;
11112 map = isl_map_copy(map);
11113 map = isl_map_reverse(map);
11114 in = isl_map_is_single_valued(map);
11115 isl_map_free(map);
11117 return in;
11120 /* Check if the given map is obviously injective.
11122 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11124 isl_bool in;
11126 map = isl_map_copy(map);
11127 map = isl_map_reverse(map);
11128 in = isl_map_plain_is_single_valued(map);
11129 isl_map_free(map);
11131 return in;
11134 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11136 isl_bool sv;
11138 sv = isl_map_is_single_valued(map);
11139 if (sv < 0 || !sv)
11140 return sv;
11142 return isl_map_is_injective(map);
11145 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11147 return isl_map_is_single_valued(set_to_map(set));
11150 /* Does "map" only map elements to themselves?
11152 * If the domain and range spaces are different, then "map"
11153 * is considered not to be an identity relation, even if it is empty.
11154 * Otherwise, construct the maximal identity relation and
11155 * check whether "map" is a subset of this relation.
11157 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11159 isl_space *space;
11160 isl_map *id;
11161 isl_bool equal, is_identity;
11163 space = isl_map_get_space(map);
11164 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11165 isl_space_free(space);
11166 if (equal < 0 || !equal)
11167 return equal;
11169 id = isl_map_identity(isl_map_get_space(map));
11170 is_identity = isl_map_is_subset(map, id);
11171 isl_map_free(id);
11173 return is_identity;
11176 int isl_map_is_translation(__isl_keep isl_map *map)
11178 int ok;
11179 isl_set *delta;
11181 delta = isl_map_deltas(isl_map_copy(map));
11182 ok = isl_set_is_singleton(delta);
11183 isl_set_free(delta);
11185 return ok;
11188 static int unique(isl_int *p, unsigned pos, unsigned len)
11190 if (isl_seq_first_non_zero(p, pos) != -1)
11191 return 0;
11192 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11193 return 0;
11194 return 1;
11197 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11199 int i, j;
11200 unsigned nvar;
11201 unsigned ovar;
11203 if (!bset)
11204 return isl_bool_error;
11206 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11207 return isl_bool_false;
11209 nvar = isl_basic_set_dim(bset, isl_dim_set);
11210 ovar = isl_space_offset(bset->dim, isl_dim_set);
11211 for (j = 0; j < nvar; ++j) {
11212 int lower = 0, upper = 0;
11213 for (i = 0; i < bset->n_eq; ++i) {
11214 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11215 continue;
11216 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11217 return isl_bool_false;
11218 break;
11220 if (i < bset->n_eq)
11221 continue;
11222 for (i = 0; i < bset->n_ineq; ++i) {
11223 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11224 continue;
11225 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11226 return isl_bool_false;
11227 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11228 lower = 1;
11229 else
11230 upper = 1;
11232 if (!lower || !upper)
11233 return isl_bool_false;
11236 return isl_bool_true;
11239 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11241 if (!set)
11242 return isl_bool_error;
11243 if (set->n != 1)
11244 return isl_bool_false;
11246 return isl_basic_set_is_box(set->p[0]);
11249 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11251 if (!bset)
11252 return isl_bool_error;
11254 return isl_space_is_wrapping(bset->dim);
11257 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11259 if (!set)
11260 return isl_bool_error;
11262 return isl_space_is_wrapping(set->dim);
11265 /* Modify the space of "map" through a call to "change".
11266 * If "can_change" is set (not NULL), then first call it to check
11267 * if the modification is allowed, printing the error message "cannot_change"
11268 * if it is not.
11270 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11271 isl_bool (*can_change)(__isl_keep isl_map *map),
11272 const char *cannot_change,
11273 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11275 isl_bool ok;
11276 isl_space *space;
11278 if (!map)
11279 return NULL;
11281 ok = can_change ? can_change(map) : isl_bool_true;
11282 if (ok < 0)
11283 return isl_map_free(map);
11284 if (!ok)
11285 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11286 return isl_map_free(map));
11288 space = change(isl_map_get_space(map));
11289 map = isl_map_reset_space(map, space);
11291 return map;
11294 /* Is the domain of "map" a wrapped relation?
11296 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11298 if (!map)
11299 return isl_bool_error;
11301 return isl_space_domain_is_wrapping(map->dim);
11304 /* Does "map" have a wrapped relation in both domain and range?
11306 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11308 return isl_space_is_product(isl_map_peek_space(map));
11311 /* Is the range of "map" a wrapped relation?
11313 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11315 if (!map)
11316 return isl_bool_error;
11318 return isl_space_range_is_wrapping(map->dim);
11321 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11323 bmap = isl_basic_map_cow(bmap);
11324 if (!bmap)
11325 return NULL;
11327 bmap->dim = isl_space_wrap(bmap->dim);
11328 if (!bmap->dim)
11329 goto error;
11331 bmap = isl_basic_map_finalize(bmap);
11333 return bset_from_bmap(bmap);
11334 error:
11335 isl_basic_map_free(bmap);
11336 return NULL;
11339 /* Given a map A -> B, return the set (A -> B).
11341 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11343 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11346 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11348 bset = isl_basic_set_cow(bset);
11349 if (!bset)
11350 return NULL;
11352 bset->dim = isl_space_unwrap(bset->dim);
11353 if (!bset->dim)
11354 goto error;
11356 bset = isl_basic_set_finalize(bset);
11358 return bset_to_bmap(bset);
11359 error:
11360 isl_basic_set_free(bset);
11361 return NULL;
11364 /* Given a set (A -> B), return the map A -> B.
11365 * Error out if "set" is not of the form (A -> B).
11367 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11369 return isl_map_change_space(set, &isl_set_is_wrapping,
11370 "not a wrapping set", &isl_space_unwrap);
11373 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11374 enum isl_dim_type type)
11376 if (!bmap)
11377 return NULL;
11379 if (!isl_space_is_named_or_nested(bmap->dim, type))
11380 return bmap;
11382 bmap = isl_basic_map_cow(bmap);
11383 if (!bmap)
11384 return NULL;
11386 bmap->dim = isl_space_reset(bmap->dim, type);
11387 if (!bmap->dim)
11388 goto error;
11390 bmap = isl_basic_map_finalize(bmap);
11392 return bmap;
11393 error:
11394 isl_basic_map_free(bmap);
11395 return NULL;
11398 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11399 enum isl_dim_type type)
11401 int i;
11403 if (!map)
11404 return NULL;
11406 if (!isl_space_is_named_or_nested(map->dim, type))
11407 return map;
11409 map = isl_map_cow(map);
11410 if (!map)
11411 return NULL;
11413 for (i = 0; i < map->n; ++i) {
11414 map->p[i] = isl_basic_map_reset(map->p[i], type);
11415 if (!map->p[i])
11416 goto error;
11418 map->dim = isl_space_reset(map->dim, type);
11419 if (!map->dim)
11420 goto error;
11422 return map;
11423 error:
11424 isl_map_free(map);
11425 return NULL;
11428 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11430 if (!bmap)
11431 return NULL;
11433 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11434 return bmap;
11436 bmap = isl_basic_map_cow(bmap);
11437 if (!bmap)
11438 return NULL;
11440 bmap->dim = isl_space_flatten(bmap->dim);
11441 if (!bmap->dim)
11442 goto error;
11444 bmap = isl_basic_map_finalize(bmap);
11446 return bmap;
11447 error:
11448 isl_basic_map_free(bmap);
11449 return NULL;
11452 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11454 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11457 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11458 __isl_take isl_basic_map *bmap)
11460 if (!bmap)
11461 return NULL;
11463 if (!bmap->dim->nested[0])
11464 return bmap;
11466 bmap = isl_basic_map_cow(bmap);
11467 if (!bmap)
11468 return NULL;
11470 bmap->dim = isl_space_flatten_domain(bmap->dim);
11471 if (!bmap->dim)
11472 goto error;
11474 bmap = isl_basic_map_finalize(bmap);
11476 return bmap;
11477 error:
11478 isl_basic_map_free(bmap);
11479 return NULL;
11482 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11483 __isl_take isl_basic_map *bmap)
11485 if (!bmap)
11486 return NULL;
11488 if (!bmap->dim->nested[1])
11489 return bmap;
11491 bmap = isl_basic_map_cow(bmap);
11492 if (!bmap)
11493 return NULL;
11495 bmap->dim = isl_space_flatten_range(bmap->dim);
11496 if (!bmap->dim)
11497 goto error;
11499 bmap = isl_basic_map_finalize(bmap);
11501 return bmap;
11502 error:
11503 isl_basic_map_free(bmap);
11504 return NULL;
11507 /* Remove any internal structure from the spaces of domain and range of "map".
11509 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11511 if (!map)
11512 return NULL;
11514 if (!map->dim->nested[0] && !map->dim->nested[1])
11515 return map;
11517 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11520 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11522 return set_from_map(isl_map_flatten(set_to_map(set)));
11525 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11527 isl_space *space, *flat_space;
11528 isl_map *map;
11530 space = isl_set_get_space(set);
11531 flat_space = isl_space_flatten(isl_space_copy(space));
11532 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11533 flat_space));
11534 map = isl_map_intersect_domain(map, set);
11536 return map;
11539 /* Remove any internal structure from the space of the domain of "map".
11541 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11543 if (!map)
11544 return NULL;
11546 if (!map->dim->nested[0])
11547 return map;
11549 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11552 /* Remove any internal structure from the space of the range of "map".
11554 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11556 if (!map)
11557 return NULL;
11559 if (!map->dim->nested[1])
11560 return map;
11562 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11565 /* Reorder the dimensions of "bmap" according to the given dim_map
11566 * and set the dimension specification to "space" and
11567 * perform Gaussian elimination on the result.
11569 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11570 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11572 isl_basic_map *res;
11573 unsigned flags;
11574 unsigned n_div;
11576 if (!bmap || !space || !dim_map)
11577 goto error;
11579 flags = bmap->flags;
11580 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11581 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11582 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11583 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11584 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11585 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11586 if (res)
11587 res->flags = flags;
11588 res = isl_basic_map_gauss(res, NULL);
11589 res = isl_basic_map_finalize(res);
11590 return res;
11591 error:
11592 free(dim_map);
11593 isl_basic_map_free(bmap);
11594 isl_space_free(space);
11595 return NULL;
11598 /* Reorder the dimensions of "map" according to given reordering.
11600 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11601 __isl_take isl_reordering *r)
11603 int i;
11604 struct isl_dim_map *dim_map;
11606 map = isl_map_cow(map);
11607 dim_map = isl_dim_map_from_reordering(r);
11608 if (!map || !r || !dim_map)
11609 goto error;
11611 for (i = 0; i < map->n; ++i) {
11612 struct isl_dim_map *dim_map_i;
11613 isl_space *space;
11615 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11617 space = isl_reordering_get_space(r);
11618 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11620 if (!map->p[i])
11621 goto error;
11624 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11625 map = isl_map_unmark_normalized(map);
11627 isl_reordering_free(r);
11628 free(dim_map);
11629 return map;
11630 error:
11631 free(dim_map);
11632 isl_map_free(map);
11633 isl_reordering_free(r);
11634 return NULL;
11637 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11638 __isl_take isl_reordering *r)
11640 return set_from_map(isl_map_realign(set_to_map(set), r));
11643 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11644 __isl_take isl_space *model)
11646 isl_ctx *ctx;
11647 isl_bool aligned;
11649 if (!map || !model)
11650 goto error;
11652 ctx = isl_space_get_ctx(model);
11653 if (!isl_space_has_named_params(model))
11654 isl_die(ctx, isl_error_invalid,
11655 "model has unnamed parameters", goto error);
11656 if (isl_map_check_named_params(map) < 0)
11657 goto error;
11658 aligned = isl_map_space_has_equal_params(map, model);
11659 if (aligned < 0)
11660 goto error;
11661 if (!aligned) {
11662 isl_reordering *exp;
11664 exp = isl_parameter_alignment_reordering(map->dim, model);
11665 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11666 map = isl_map_realign(map, exp);
11669 isl_space_free(model);
11670 return map;
11671 error:
11672 isl_space_free(model);
11673 isl_map_free(map);
11674 return NULL;
11677 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11678 __isl_take isl_space *model)
11680 return isl_map_align_params(set, model);
11683 /* Align the parameters of "bmap" to those of "model", introducing
11684 * additional parameters if needed.
11686 __isl_give isl_basic_map *isl_basic_map_align_params(
11687 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11689 isl_ctx *ctx;
11690 isl_bool equal_params;
11692 if (!bmap || !model)
11693 goto error;
11695 ctx = isl_space_get_ctx(model);
11696 if (!isl_space_has_named_params(model))
11697 isl_die(ctx, isl_error_invalid,
11698 "model has unnamed parameters", goto error);
11699 if (isl_basic_map_check_named_params(bmap) < 0)
11700 goto error;
11701 equal_params = isl_space_has_equal_params(bmap->dim, model);
11702 if (equal_params < 0)
11703 goto error;
11704 if (!equal_params) {
11705 isl_reordering *exp;
11706 struct isl_dim_map *dim_map;
11708 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11709 exp = isl_reordering_extend_space(exp,
11710 isl_basic_map_get_space(bmap));
11711 dim_map = isl_dim_map_from_reordering(exp);
11712 bmap = isl_basic_map_realign(bmap,
11713 isl_reordering_get_space(exp),
11714 isl_dim_map_extend(dim_map, bmap));
11715 isl_reordering_free(exp);
11716 free(dim_map);
11719 isl_space_free(model);
11720 return bmap;
11721 error:
11722 isl_space_free(model);
11723 isl_basic_map_free(bmap);
11724 return NULL;
11727 /* Do "bset" and "space" have the same parameters?
11729 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11730 __isl_keep isl_space *space)
11732 isl_space *bset_space;
11734 bset_space = isl_basic_set_peek_space(bset);
11735 return isl_space_has_equal_params(bset_space, space);
11738 /* Do "map" and "space" have the same parameters?
11740 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11741 __isl_keep isl_space *space)
11743 isl_space *map_space;
11745 map_space = isl_map_peek_space(map);
11746 return isl_space_has_equal_params(map_space, space);
11749 /* Do "set" and "space" have the same parameters?
11751 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11752 __isl_keep isl_space *space)
11754 return isl_map_space_has_equal_params(set_to_map(set), space);
11757 /* Align the parameters of "bset" to those of "model", introducing
11758 * additional parameters if needed.
11760 __isl_give isl_basic_set *isl_basic_set_align_params(
11761 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11763 return isl_basic_map_align_params(bset, model);
11766 /* Drop all parameters not referenced by "map".
11768 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11770 int i;
11772 if (isl_map_check_named_params(map) < 0)
11773 return isl_map_free(map);
11775 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11776 isl_bool involves;
11778 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11779 if (involves < 0)
11780 return isl_map_free(map);
11781 if (!involves)
11782 map = isl_map_project_out(map, isl_dim_param, i, 1);
11785 return map;
11788 /* Drop all parameters not referenced by "set".
11790 __isl_give isl_set *isl_set_drop_unused_params(
11791 __isl_take isl_set *set)
11793 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11796 /* Drop all parameters not referenced by "bmap".
11798 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11799 __isl_take isl_basic_map *bmap)
11801 int i;
11803 if (isl_basic_map_check_named_params(bmap) < 0)
11804 return isl_basic_map_free(bmap);
11806 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11807 isl_bool involves;
11809 involves = isl_basic_map_involves_dims(bmap,
11810 isl_dim_param, i, 1);
11811 if (involves < 0)
11812 return isl_basic_map_free(bmap);
11813 if (!involves)
11814 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11817 return bmap;
11820 /* Drop all parameters not referenced by "bset".
11822 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11823 __isl_take isl_basic_set *bset)
11825 return bset_from_bmap(isl_basic_map_drop_unused_params(
11826 bset_to_bmap(bset)));
11829 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11830 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11831 enum isl_dim_type c2, enum isl_dim_type c3,
11832 enum isl_dim_type c4, enum isl_dim_type c5)
11834 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11835 struct isl_mat *mat;
11836 int i, j, k;
11837 int pos;
11839 if (!bmap)
11840 return NULL;
11841 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11842 isl_basic_map_total_dim(bmap) + 1);
11843 if (!mat)
11844 return NULL;
11845 for (i = 0; i < bmap->n_eq; ++i)
11846 for (j = 0, pos = 0; j < 5; ++j) {
11847 int off = isl_basic_map_offset(bmap, c[j]);
11848 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11849 isl_int_set(mat->row[i][pos],
11850 bmap->eq[i][off + k]);
11851 ++pos;
11855 return mat;
11858 __isl_give isl_mat *isl_basic_map_inequalities_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_ineq,
11871 isl_basic_map_total_dim(bmap) + 1);
11872 if (!mat)
11873 return NULL;
11874 for (i = 0; i < bmap->n_ineq; ++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->ineq[i][off + k]);
11880 ++pos;
11884 return mat;
11887 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11888 __isl_take isl_space *space,
11889 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11890 enum isl_dim_type c2, enum isl_dim_type c3,
11891 enum isl_dim_type c4, enum isl_dim_type c5)
11893 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11894 isl_basic_map *bmap = NULL;
11895 unsigned total;
11896 unsigned extra;
11897 int i, j, k, l;
11898 int pos;
11900 if (!space || !eq || !ineq)
11901 goto error;
11903 if (eq->n_col != ineq->n_col)
11904 isl_die(space->ctx, isl_error_invalid,
11905 "equalities and inequalities matrices should have "
11906 "same number of columns", goto error);
11908 total = 1 + isl_space_dim(space, isl_dim_all);
11910 if (eq->n_col < total)
11911 isl_die(space->ctx, isl_error_invalid,
11912 "number of columns too small", goto error);
11914 extra = eq->n_col - total;
11916 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11917 eq->n_row, ineq->n_row);
11918 if (!bmap)
11919 goto error;
11920 for (i = 0; i < extra; ++i) {
11921 k = isl_basic_map_alloc_div(bmap);
11922 if (k < 0)
11923 goto error;
11924 isl_int_set_si(bmap->div[k][0], 0);
11926 for (i = 0; i < eq->n_row; ++i) {
11927 l = isl_basic_map_alloc_equality(bmap);
11928 if (l < 0)
11929 goto error;
11930 for (j = 0, pos = 0; j < 5; ++j) {
11931 int off = isl_basic_map_offset(bmap, c[j]);
11932 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11933 isl_int_set(bmap->eq[l][off + k],
11934 eq->row[i][pos]);
11935 ++pos;
11939 for (i = 0; i < ineq->n_row; ++i) {
11940 l = isl_basic_map_alloc_inequality(bmap);
11941 if (l < 0)
11942 goto error;
11943 for (j = 0, pos = 0; j < 5; ++j) {
11944 int off = isl_basic_map_offset(bmap, c[j]);
11945 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11946 isl_int_set(bmap->ineq[l][off + k],
11947 ineq->row[i][pos]);
11948 ++pos;
11953 isl_space_free(space);
11954 isl_mat_free(eq);
11955 isl_mat_free(ineq);
11957 bmap = isl_basic_map_simplify(bmap);
11958 return isl_basic_map_finalize(bmap);
11959 error:
11960 isl_space_free(space);
11961 isl_mat_free(eq);
11962 isl_mat_free(ineq);
11963 isl_basic_map_free(bmap);
11964 return NULL;
11967 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11968 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11969 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11971 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11972 c1, c2, c3, c4, isl_dim_in);
11975 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11976 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11977 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11979 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11980 c1, c2, c3, c4, isl_dim_in);
11983 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11984 __isl_take isl_space *dim,
11985 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11986 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11988 isl_basic_map *bmap;
11989 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11990 c1, c2, c3, c4, isl_dim_in);
11991 return bset_from_bmap(bmap);
11994 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11996 if (!bmap)
11997 return isl_bool_error;
11999 return isl_space_can_zip(bmap->dim);
12002 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12004 if (!map)
12005 return isl_bool_error;
12007 return isl_space_can_zip(map->dim);
12010 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12011 * (A -> C) -> (B -> D).
12013 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12015 unsigned pos;
12016 unsigned n1;
12017 unsigned n2;
12019 if (!bmap)
12020 return NULL;
12022 if (!isl_basic_map_can_zip(bmap))
12023 isl_die(bmap->ctx, isl_error_invalid,
12024 "basic map cannot be zipped", goto error);
12025 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12026 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12027 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12028 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12029 bmap = isl_basic_map_cow(bmap);
12030 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12031 if (!bmap)
12032 return NULL;
12033 bmap->dim = isl_space_zip(bmap->dim);
12034 if (!bmap->dim)
12035 goto error;
12036 bmap = isl_basic_map_mark_final(bmap);
12037 return bmap;
12038 error:
12039 isl_basic_map_free(bmap);
12040 return NULL;
12043 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12044 * (A -> C) -> (B -> D).
12046 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12048 int i;
12050 if (!map)
12051 return NULL;
12053 if (!isl_map_can_zip(map))
12054 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12055 goto error);
12057 map = isl_map_cow(map);
12058 if (!map)
12059 return NULL;
12061 for (i = 0; i < map->n; ++i) {
12062 map->p[i] = isl_basic_map_zip(map->p[i]);
12063 if (!map->p[i])
12064 goto error;
12067 map->dim = isl_space_zip(map->dim);
12068 if (!map->dim)
12069 goto error;
12071 return map;
12072 error:
12073 isl_map_free(map);
12074 return NULL;
12077 /* Can we apply isl_basic_map_curry to "bmap"?
12078 * That is, does it have a nested relation in its domain?
12080 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12082 if (!bmap)
12083 return isl_bool_error;
12085 return isl_space_can_curry(bmap->dim);
12088 /* Can we apply isl_map_curry to "map"?
12089 * That is, does it have a nested relation in its domain?
12091 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12093 if (!map)
12094 return isl_bool_error;
12096 return isl_space_can_curry(map->dim);
12099 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12100 * A -> (B -> C).
12102 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12105 if (!bmap)
12106 return NULL;
12108 if (!isl_basic_map_can_curry(bmap))
12109 isl_die(bmap->ctx, isl_error_invalid,
12110 "basic map cannot be curried", goto error);
12111 bmap = isl_basic_map_cow(bmap);
12112 if (!bmap)
12113 return NULL;
12114 bmap->dim = isl_space_curry(bmap->dim);
12115 if (!bmap->dim)
12116 goto error;
12117 bmap = isl_basic_map_mark_final(bmap);
12118 return bmap;
12119 error:
12120 isl_basic_map_free(bmap);
12121 return NULL;
12124 /* Given a map (A -> B) -> C, return the corresponding map
12125 * A -> (B -> C).
12127 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12129 return isl_map_change_space(map, &isl_map_can_curry,
12130 "map cannot be curried", &isl_space_curry);
12133 /* Can isl_map_range_curry be applied to "map"?
12134 * That is, does it have a nested relation in its range,
12135 * the domain of which is itself a nested relation?
12137 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12139 if (!map)
12140 return isl_bool_error;
12142 return isl_space_can_range_curry(map->dim);
12145 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12146 * A -> (B -> (C -> D)).
12148 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12150 return isl_map_change_space(map, &isl_map_can_range_curry,
12151 "map range cannot be curried",
12152 &isl_space_range_curry);
12155 /* Can we apply isl_basic_map_uncurry to "bmap"?
12156 * That is, does it have a nested relation in its domain?
12158 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12160 if (!bmap)
12161 return isl_bool_error;
12163 return isl_space_can_uncurry(bmap->dim);
12166 /* Can we apply isl_map_uncurry to "map"?
12167 * That is, does it have a nested relation in its domain?
12169 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12171 if (!map)
12172 return isl_bool_error;
12174 return isl_space_can_uncurry(map->dim);
12177 /* Given a basic map A -> (B -> C), return the corresponding basic map
12178 * (A -> B) -> C.
12180 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12183 if (!bmap)
12184 return NULL;
12186 if (!isl_basic_map_can_uncurry(bmap))
12187 isl_die(bmap->ctx, isl_error_invalid,
12188 "basic map cannot be uncurried",
12189 return isl_basic_map_free(bmap));
12190 bmap = isl_basic_map_cow(bmap);
12191 if (!bmap)
12192 return NULL;
12193 bmap->dim = isl_space_uncurry(bmap->dim);
12194 if (!bmap->dim)
12195 return isl_basic_map_free(bmap);
12196 bmap = isl_basic_map_mark_final(bmap);
12197 return bmap;
12200 /* Given a map A -> (B -> C), return the corresponding map
12201 * (A -> B) -> C.
12203 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12205 return isl_map_change_space(map, &isl_map_can_uncurry,
12206 "map cannot be uncurried", &isl_space_uncurry);
12209 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12210 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12212 return isl_map_equate(set, type1, pos1, type2, pos2);
12215 /* Construct a basic map where the given dimensions are equal to each other.
12217 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12218 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12220 isl_basic_map *bmap = NULL;
12221 int i;
12223 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12224 isl_space_check_range(space, type2, pos2, 1) < 0)
12225 goto error;
12227 if (type1 == type2 && pos1 == pos2)
12228 return isl_basic_map_universe(space);
12230 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12231 i = isl_basic_map_alloc_equality(bmap);
12232 if (i < 0)
12233 goto error;
12234 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12235 pos1 += isl_basic_map_offset(bmap, type1);
12236 pos2 += isl_basic_map_offset(bmap, type2);
12237 isl_int_set_si(bmap->eq[i][pos1], -1);
12238 isl_int_set_si(bmap->eq[i][pos2], 1);
12239 bmap = isl_basic_map_finalize(bmap);
12240 isl_space_free(space);
12241 return bmap;
12242 error:
12243 isl_space_free(space);
12244 isl_basic_map_free(bmap);
12245 return NULL;
12248 /* Add a constraint imposing that the given two dimensions are equal.
12250 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12251 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12253 isl_basic_map *eq;
12255 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12257 bmap = isl_basic_map_intersect(bmap, eq);
12259 return bmap;
12262 /* Add a constraint imposing that the given two dimensions are equal.
12264 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12265 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12267 isl_basic_map *bmap;
12269 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12271 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12273 return map;
12276 /* Add a constraint imposing that the given two dimensions have opposite values.
12278 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12279 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12281 isl_basic_map *bmap = NULL;
12282 int i;
12284 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12285 return isl_map_free(map);
12286 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12287 return isl_map_free(map);
12289 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12290 i = isl_basic_map_alloc_equality(bmap);
12291 if (i < 0)
12292 goto error;
12293 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12294 pos1 += isl_basic_map_offset(bmap, type1);
12295 pos2 += isl_basic_map_offset(bmap, type2);
12296 isl_int_set_si(bmap->eq[i][pos1], 1);
12297 isl_int_set_si(bmap->eq[i][pos2], 1);
12298 bmap = isl_basic_map_finalize(bmap);
12300 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12302 return map;
12303 error:
12304 isl_basic_map_free(bmap);
12305 isl_map_free(map);
12306 return NULL;
12309 /* Construct a constraint imposing that the value of the first dimension is
12310 * greater than or equal to that of the second.
12312 static __isl_give isl_constraint *constraint_order_ge(
12313 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12314 enum isl_dim_type type2, int pos2)
12316 isl_constraint *c;
12318 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12319 isl_space_check_range(space, type2, pos2, 1) < 0)
12320 space = isl_space_free(space);
12321 if (!space)
12322 return NULL;
12324 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12326 if (type1 == type2 && pos1 == pos2)
12327 return c;
12329 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12330 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12332 return c;
12335 /* Add a constraint imposing that the value of the first dimension is
12336 * greater than or equal to that of the second.
12338 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12339 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12341 isl_constraint *c;
12342 isl_space *space;
12344 if (type1 == type2 && pos1 == pos2)
12345 return bmap;
12346 space = isl_basic_map_get_space(bmap);
12347 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12348 bmap = isl_basic_map_add_constraint(bmap, c);
12350 return bmap;
12353 /* Add a constraint imposing that the value of the first dimension is
12354 * greater than or equal to that of the second.
12356 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12357 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12359 isl_constraint *c;
12360 isl_space *space;
12362 if (type1 == type2 && pos1 == pos2)
12363 return map;
12364 space = isl_map_get_space(map);
12365 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12366 map = isl_map_add_constraint(map, c);
12368 return map;
12371 /* Add a constraint imposing that the value of the first dimension is
12372 * less than or equal to that of the second.
12374 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12375 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12377 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12380 /* Construct a basic map where the value of the first dimension is
12381 * greater than that of the second.
12383 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12384 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12386 isl_basic_map *bmap = NULL;
12387 int i;
12389 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12390 isl_space_check_range(space, type2, pos2, 1) < 0)
12391 goto error;
12393 if (type1 == type2 && pos1 == pos2)
12394 return isl_basic_map_empty(space);
12396 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12397 i = isl_basic_map_alloc_inequality(bmap);
12398 if (i < 0)
12399 return isl_basic_map_free(bmap);
12400 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12401 pos1 += isl_basic_map_offset(bmap, type1);
12402 pos2 += isl_basic_map_offset(bmap, type2);
12403 isl_int_set_si(bmap->ineq[i][pos1], 1);
12404 isl_int_set_si(bmap->ineq[i][pos2], -1);
12405 isl_int_set_si(bmap->ineq[i][0], -1);
12406 bmap = isl_basic_map_finalize(bmap);
12408 return bmap;
12409 error:
12410 isl_space_free(space);
12411 isl_basic_map_free(bmap);
12412 return NULL;
12415 /* Add a constraint imposing that the value of the first dimension is
12416 * greater than that of the second.
12418 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12419 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12421 isl_basic_map *gt;
12423 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12425 bmap = isl_basic_map_intersect(bmap, gt);
12427 return bmap;
12430 /* Add a constraint imposing that the value of the first dimension is
12431 * greater than that of the second.
12433 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12434 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12436 isl_basic_map *bmap;
12438 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12440 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12442 return map;
12445 /* Add a constraint imposing that the value of the first dimension is
12446 * smaller than that of the second.
12448 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12449 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12451 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12454 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12455 int pos)
12457 isl_aff *div;
12458 isl_local_space *ls;
12460 if (!bmap)
12461 return NULL;
12463 if (!isl_basic_map_divs_known(bmap))
12464 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12465 "some divs are unknown", return NULL);
12467 ls = isl_basic_map_get_local_space(bmap);
12468 div = isl_local_space_get_div(ls, pos);
12469 isl_local_space_free(ls);
12471 return div;
12474 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12475 int pos)
12477 return isl_basic_map_get_div(bset, pos);
12480 /* Plug in "subs" for dimension "type", "pos" of "bset".
12482 * Let i be the dimension to replace and let "subs" be of the form
12484 * f/d
12486 * Any integer division with a non-zero coefficient for i,
12488 * floor((a i + g)/m)
12490 * is replaced by
12492 * floor((a f + d g)/(m d))
12494 * Constraints of the form
12496 * a i + g
12498 * are replaced by
12500 * a f + d g
12502 * We currently require that "subs" is an integral expression.
12503 * Handling rational expressions may require us to add stride constraints
12504 * as we do in isl_basic_set_preimage_multi_aff.
12506 __isl_give isl_basic_set *isl_basic_set_substitute(
12507 __isl_take isl_basic_set *bset,
12508 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12510 int i;
12511 isl_int v;
12512 isl_ctx *ctx;
12514 if (bset && isl_basic_set_plain_is_empty(bset))
12515 return bset;
12517 bset = isl_basic_set_cow(bset);
12518 if (!bset || !subs)
12519 goto error;
12521 ctx = isl_basic_set_get_ctx(bset);
12522 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12523 isl_die(ctx, isl_error_invalid,
12524 "spaces don't match", goto error);
12525 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12526 isl_die(ctx, isl_error_unsupported,
12527 "cannot handle divs yet", goto error);
12528 if (!isl_int_is_one(subs->v->el[0]))
12529 isl_die(ctx, isl_error_invalid,
12530 "can only substitute integer expressions", goto error);
12532 pos += isl_basic_set_offset(bset, type);
12534 isl_int_init(v);
12536 for (i = 0; i < bset->n_eq; ++i) {
12537 if (isl_int_is_zero(bset->eq[i][pos]))
12538 continue;
12539 isl_int_set(v, bset->eq[i][pos]);
12540 isl_int_set_si(bset->eq[i][pos], 0);
12541 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12542 v, subs->v->el + 1, subs->v->size - 1);
12545 for (i = 0; i < bset->n_ineq; ++i) {
12546 if (isl_int_is_zero(bset->ineq[i][pos]))
12547 continue;
12548 isl_int_set(v, bset->ineq[i][pos]);
12549 isl_int_set_si(bset->ineq[i][pos], 0);
12550 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12551 v, subs->v->el + 1, subs->v->size - 1);
12554 for (i = 0; i < bset->n_div; ++i) {
12555 if (isl_int_is_zero(bset->div[i][1 + pos]))
12556 continue;
12557 isl_int_set(v, bset->div[i][1 + pos]);
12558 isl_int_set_si(bset->div[i][1 + pos], 0);
12559 isl_seq_combine(bset->div[i] + 1,
12560 subs->v->el[0], bset->div[i] + 1,
12561 v, subs->v->el + 1, subs->v->size - 1);
12562 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12565 isl_int_clear(v);
12567 bset = isl_basic_set_simplify(bset);
12568 return isl_basic_set_finalize(bset);
12569 error:
12570 isl_basic_set_free(bset);
12571 return NULL;
12574 /* Plug in "subs" for dimension "type", "pos" of "set".
12576 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12577 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12579 int i;
12581 if (set && isl_set_plain_is_empty(set))
12582 return set;
12584 set = isl_set_cow(set);
12585 if (!set || !subs)
12586 goto error;
12588 for (i = set->n - 1; i >= 0; --i) {
12589 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12590 set = set_from_map(remove_if_empty(set_to_map(set), i));
12591 if (!set)
12592 return NULL;
12595 return set;
12596 error:
12597 isl_set_free(set);
12598 return NULL;
12601 /* Check if the range of "ma" is compatible with the domain or range
12602 * (depending on "type") of "bmap".
12604 static isl_stat check_basic_map_compatible_range_multi_aff(
12605 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12606 __isl_keep isl_multi_aff *ma)
12608 isl_bool m;
12609 isl_space *ma_space;
12611 ma_space = isl_multi_aff_get_space(ma);
12613 m = isl_space_has_equal_params(bmap->dim, ma_space);
12614 if (m < 0)
12615 goto error;
12616 if (!m)
12617 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12618 "parameters don't match", goto error);
12619 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12620 if (m < 0)
12621 goto error;
12622 if (!m)
12623 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12624 "spaces don't match", goto error);
12626 isl_space_free(ma_space);
12627 return isl_stat_ok;
12628 error:
12629 isl_space_free(ma_space);
12630 return isl_stat_error;
12633 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12634 * coefficients before the transformed range of dimensions,
12635 * the "n_after" coefficients after the transformed range of dimensions
12636 * and the coefficients of the other divs in "bmap".
12638 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12639 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12641 int i;
12642 int n_param;
12643 int n_set;
12644 isl_local_space *ls;
12646 if (n_div == 0)
12647 return 0;
12649 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12650 if (!ls)
12651 return -1;
12653 n_param = isl_local_space_dim(ls, isl_dim_param);
12654 n_set = isl_local_space_dim(ls, isl_dim_set);
12655 for (i = 0; i < n_div; ++i) {
12656 int o_bmap = 0, o_ls = 0;
12658 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12659 o_bmap += 1 + 1 + n_param;
12660 o_ls += 1 + 1 + n_param;
12661 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12662 o_bmap += n_before;
12663 isl_seq_cpy(bmap->div[i] + o_bmap,
12664 ls->div->row[i] + o_ls, n_set);
12665 o_bmap += n_set;
12666 o_ls += n_set;
12667 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12668 o_bmap += n_after;
12669 isl_seq_cpy(bmap->div[i] + o_bmap,
12670 ls->div->row[i] + o_ls, n_div);
12671 o_bmap += n_div;
12672 o_ls += n_div;
12673 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12674 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12675 goto error;
12678 isl_local_space_free(ls);
12679 return 0;
12680 error:
12681 isl_local_space_free(ls);
12682 return -1;
12685 /* How many stride constraints does "ma" enforce?
12686 * That is, how many of the affine expressions have a denominator
12687 * different from one?
12689 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12691 int i;
12692 int strides = 0;
12694 for (i = 0; i < ma->n; ++i)
12695 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12696 strides++;
12698 return strides;
12701 /* For each affine expression in ma of the form
12703 * x_i = (f_i y + h_i)/m_i
12705 * with m_i different from one, add a constraint to "bmap"
12706 * of the form
12708 * f_i y + h_i = m_i alpha_i
12710 * with alpha_i an additional existentially quantified variable.
12712 * The input variables of "ma" correspond to a subset of the variables
12713 * of "bmap". There are "n_before" variables in "bmap" before this
12714 * subset and "n_after" variables after this subset.
12715 * The integer divisions of the affine expressions in "ma" are assumed
12716 * to have been aligned. There are "n_div_ma" of them and
12717 * they appear first in "bmap", straight after the "n_after" variables.
12719 static __isl_give isl_basic_map *add_ma_strides(
12720 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12721 int n_before, int n_after, int n_div_ma)
12723 int i, k;
12724 int div;
12725 int total;
12726 int n_param;
12727 int n_in;
12729 total = isl_basic_map_total_dim(bmap);
12730 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12731 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12732 for (i = 0; i < ma->n; ++i) {
12733 int o_bmap = 0, o_ma = 1;
12735 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12736 continue;
12737 div = isl_basic_map_alloc_div(bmap);
12738 k = isl_basic_map_alloc_equality(bmap);
12739 if (div < 0 || k < 0)
12740 goto error;
12741 isl_int_set_si(bmap->div[div][0], 0);
12742 isl_seq_cpy(bmap->eq[k] + o_bmap,
12743 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12744 o_bmap += 1 + n_param;
12745 o_ma += 1 + n_param;
12746 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12747 o_bmap += n_before;
12748 isl_seq_cpy(bmap->eq[k] + o_bmap,
12749 ma->u.p[i]->v->el + o_ma, n_in);
12750 o_bmap += n_in;
12751 o_ma += n_in;
12752 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12753 o_bmap += n_after;
12754 isl_seq_cpy(bmap->eq[k] + o_bmap,
12755 ma->u.p[i]->v->el + o_ma, n_div_ma);
12756 o_bmap += n_div_ma;
12757 o_ma += n_div_ma;
12758 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12759 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12760 total++;
12763 return bmap;
12764 error:
12765 isl_basic_map_free(bmap);
12766 return NULL;
12769 /* Replace the domain or range space (depending on "type) of "space" by "set".
12771 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12772 enum isl_dim_type type, __isl_take isl_space *set)
12774 if (type == isl_dim_in) {
12775 space = isl_space_range(space);
12776 space = isl_space_map_from_domain_and_range(set, space);
12777 } else {
12778 space = isl_space_domain(space);
12779 space = isl_space_map_from_domain_and_range(space, set);
12782 return space;
12785 /* Compute the preimage of the domain or range (depending on "type")
12786 * of "bmap" under the function represented by "ma".
12787 * In other words, plug in "ma" in the domain or range of "bmap".
12788 * The result is a basic map that lives in the same space as "bmap"
12789 * except that the domain or range has been replaced by
12790 * the domain space of "ma".
12792 * If bmap is represented by
12794 * A(p) + S u + B x + T v + C(divs) >= 0,
12796 * where u and x are input and output dimensions if type == isl_dim_out
12797 * while x and v are input and output dimensions if type == isl_dim_in,
12798 * and ma is represented by
12800 * x = D(p) + F(y) + G(divs')
12802 * then the result is
12804 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12806 * The divs in the input set are similarly adjusted.
12807 * In particular
12809 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12811 * becomes
12813 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12814 * B_i G(divs') + c_i(divs))/n_i)
12816 * If bmap is not a rational map and if F(y) involves any denominators
12818 * x_i = (f_i y + h_i)/m_i
12820 * then additional constraints are added to ensure that we only
12821 * map back integer points. That is we enforce
12823 * f_i y + h_i = m_i alpha_i
12825 * with alpha_i an additional existentially quantified variable.
12827 * We first copy over the divs from "ma".
12828 * Then we add the modified constraints and divs from "bmap".
12829 * Finally, we add the stride constraints, if needed.
12831 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12832 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12833 __isl_take isl_multi_aff *ma)
12835 int i, k;
12836 isl_space *space;
12837 isl_basic_map *res = NULL;
12838 int n_before, n_after, n_div_bmap, n_div_ma;
12839 isl_int f, c1, c2, g;
12840 isl_bool rational;
12841 int strides;
12843 isl_int_init(f);
12844 isl_int_init(c1);
12845 isl_int_init(c2);
12846 isl_int_init(g);
12848 ma = isl_multi_aff_align_divs(ma);
12849 if (!bmap || !ma)
12850 goto error;
12851 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12852 goto error;
12854 if (type == isl_dim_in) {
12855 n_before = 0;
12856 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12857 } else {
12858 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12859 n_after = 0;
12861 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12862 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12864 space = isl_multi_aff_get_domain_space(ma);
12865 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12866 rational = isl_basic_map_is_rational(bmap);
12867 strides = rational ? 0 : multi_aff_strides(ma);
12868 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12869 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12870 if (rational)
12871 res = isl_basic_map_set_rational(res);
12873 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12874 if (isl_basic_map_alloc_div(res) < 0)
12875 goto error;
12877 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12878 goto error;
12880 for (i = 0; i < bmap->n_eq; ++i) {
12881 k = isl_basic_map_alloc_equality(res);
12882 if (k < 0)
12883 goto error;
12884 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12885 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12888 for (i = 0; i < bmap->n_ineq; ++i) {
12889 k = isl_basic_map_alloc_inequality(res);
12890 if (k < 0)
12891 goto error;
12892 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12893 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12896 for (i = 0; i < bmap->n_div; ++i) {
12897 if (isl_int_is_zero(bmap->div[i][0])) {
12898 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12899 continue;
12901 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12902 n_before, n_after, n_div_ma, n_div_bmap,
12903 f, c1, c2, g, 1);
12906 if (strides)
12907 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12909 isl_int_clear(f);
12910 isl_int_clear(c1);
12911 isl_int_clear(c2);
12912 isl_int_clear(g);
12913 isl_basic_map_free(bmap);
12914 isl_multi_aff_free(ma);
12915 res = isl_basic_map_simplify(res);
12916 return isl_basic_map_finalize(res);
12917 error:
12918 isl_int_clear(f);
12919 isl_int_clear(c1);
12920 isl_int_clear(c2);
12921 isl_int_clear(g);
12922 isl_basic_map_free(bmap);
12923 isl_multi_aff_free(ma);
12924 isl_basic_map_free(res);
12925 return NULL;
12928 /* Compute the preimage of "bset" under the function represented by "ma".
12929 * In other words, plug in "ma" in "bset". The result is a basic set
12930 * that lives in the domain space of "ma".
12932 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12933 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12935 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12938 /* Compute the preimage of the domain of "bmap" under the function
12939 * represented by "ma".
12940 * In other words, plug in "ma" in the domain of "bmap".
12941 * The result is a basic map that lives in the same space as "bmap"
12942 * except that the domain has been replaced by the domain space of "ma".
12944 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12945 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12947 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12950 /* Compute the preimage of the range of "bmap" under the function
12951 * represented by "ma".
12952 * In other words, plug in "ma" in the range of "bmap".
12953 * The result is a basic map that lives in the same space as "bmap"
12954 * except that the range has been replaced by the domain space of "ma".
12956 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12957 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12959 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12962 /* Check if the range of "ma" is compatible with the domain or range
12963 * (depending on "type") of "map".
12964 * Return isl_stat_error if anything is wrong.
12966 static isl_stat check_map_compatible_range_multi_aff(
12967 __isl_keep isl_map *map, enum isl_dim_type type,
12968 __isl_keep isl_multi_aff *ma)
12970 isl_bool m;
12971 isl_space *ma_space;
12973 ma_space = isl_multi_aff_get_space(ma);
12974 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12975 isl_space_free(ma_space);
12976 if (m < 0)
12977 return isl_stat_error;
12978 if (!m)
12979 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12980 "spaces don't match", return isl_stat_error);
12981 return isl_stat_ok;
12984 /* Compute the preimage of the domain or range (depending on "type")
12985 * of "map" under the function represented by "ma".
12986 * In other words, plug in "ma" in the domain or range of "map".
12987 * The result is a map that lives in the same space as "map"
12988 * except that the domain or range has been replaced by
12989 * the domain space of "ma".
12991 * The parameters are assumed to have been aligned.
12993 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12994 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12996 int i;
12997 isl_space *space;
12999 map = isl_map_cow(map);
13000 ma = isl_multi_aff_align_divs(ma);
13001 if (!map || !ma)
13002 goto error;
13003 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13004 goto error;
13006 for (i = 0; i < map->n; ++i) {
13007 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13008 isl_multi_aff_copy(ma));
13009 if (!map->p[i])
13010 goto error;
13013 space = isl_multi_aff_get_domain_space(ma);
13014 space = isl_space_set(isl_map_get_space(map), type, space);
13016 isl_space_free(map->dim);
13017 map->dim = space;
13018 if (!map->dim)
13019 goto error;
13021 isl_multi_aff_free(ma);
13022 if (map->n > 1)
13023 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13024 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13025 return map;
13026 error:
13027 isl_multi_aff_free(ma);
13028 isl_map_free(map);
13029 return NULL;
13032 /* Compute the preimage of the domain or range (depending on "type")
13033 * of "map" under the function represented by "ma".
13034 * In other words, plug in "ma" in the domain or range of "map".
13035 * The result is a map that lives in the same space as "map"
13036 * except that the domain or range has been replaced by
13037 * the domain space of "ma".
13039 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13040 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13042 isl_bool aligned;
13044 if (!map || !ma)
13045 goto error;
13047 aligned = isl_map_space_has_equal_params(map, ma->space);
13048 if (aligned < 0)
13049 goto error;
13050 if (aligned)
13051 return map_preimage_multi_aff(map, type, ma);
13053 if (isl_map_check_named_params(map) < 0)
13054 goto error;
13055 if (!isl_space_has_named_params(ma->space))
13056 isl_die(map->ctx, isl_error_invalid,
13057 "unaligned unnamed parameters", goto error);
13058 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13059 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13061 return map_preimage_multi_aff(map, type, ma);
13062 error:
13063 isl_multi_aff_free(ma);
13064 return isl_map_free(map);
13067 /* Compute the preimage of "set" under the function represented by "ma".
13068 * In other words, plug in "ma" in "set". The result is a set
13069 * that lives in the domain space of "ma".
13071 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13072 __isl_take isl_multi_aff *ma)
13074 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13077 /* Compute the preimage of the domain of "map" under the function
13078 * represented by "ma".
13079 * In other words, plug in "ma" in the domain of "map".
13080 * The result is a map that lives in the same space as "map"
13081 * except that the domain has been replaced by the domain space of "ma".
13083 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13084 __isl_take isl_multi_aff *ma)
13086 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13089 /* Compute the preimage of the range of "map" under the function
13090 * represented by "ma".
13091 * In other words, plug in "ma" in the range of "map".
13092 * The result is a map that lives in the same space as "map"
13093 * except that the range has been replaced by the domain space of "ma".
13095 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13096 __isl_take isl_multi_aff *ma)
13098 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13101 /* Compute the preimage of "map" under the function represented by "pma".
13102 * In other words, plug in "pma" in the domain or range of "map".
13103 * The result is a map that lives in the same space as "map",
13104 * except that the space of type "type" has been replaced by
13105 * the domain space of "pma".
13107 * The parameters of "map" and "pma" are assumed to have been aligned.
13109 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13110 __isl_take isl_map *map, enum isl_dim_type type,
13111 __isl_take isl_pw_multi_aff *pma)
13113 int i;
13114 isl_map *res;
13116 if (!pma)
13117 goto error;
13119 if (pma->n == 0) {
13120 isl_pw_multi_aff_free(pma);
13121 res = isl_map_empty(isl_map_get_space(map));
13122 isl_map_free(map);
13123 return res;
13126 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13127 isl_multi_aff_copy(pma->p[0].maff));
13128 if (type == isl_dim_in)
13129 res = isl_map_intersect_domain(res,
13130 isl_map_copy(pma->p[0].set));
13131 else
13132 res = isl_map_intersect_range(res,
13133 isl_map_copy(pma->p[0].set));
13135 for (i = 1; i < pma->n; ++i) {
13136 isl_map *res_i;
13138 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13139 isl_multi_aff_copy(pma->p[i].maff));
13140 if (type == isl_dim_in)
13141 res_i = isl_map_intersect_domain(res_i,
13142 isl_map_copy(pma->p[i].set));
13143 else
13144 res_i = isl_map_intersect_range(res_i,
13145 isl_map_copy(pma->p[i].set));
13146 res = isl_map_union(res, res_i);
13149 isl_pw_multi_aff_free(pma);
13150 isl_map_free(map);
13151 return res;
13152 error:
13153 isl_pw_multi_aff_free(pma);
13154 isl_map_free(map);
13155 return NULL;
13158 /* Compute the preimage of "map" under the function represented by "pma".
13159 * In other words, plug in "pma" in the domain or range of "map".
13160 * The result is a map that lives in the same space as "map",
13161 * except that the space of type "type" has been replaced by
13162 * the domain space of "pma".
13164 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13165 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13167 isl_bool aligned;
13169 if (!map || !pma)
13170 goto error;
13172 aligned = isl_map_space_has_equal_params(map, pma->dim);
13173 if (aligned < 0)
13174 goto error;
13175 if (aligned)
13176 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13178 if (isl_map_check_named_params(map) < 0)
13179 goto error;
13180 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13181 goto error;
13182 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13183 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13185 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13186 error:
13187 isl_pw_multi_aff_free(pma);
13188 return isl_map_free(map);
13191 /* Compute the preimage of "set" under the function represented by "pma".
13192 * In other words, plug in "pma" in "set". The result is a set
13193 * that lives in the domain space of "pma".
13195 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13196 __isl_take isl_pw_multi_aff *pma)
13198 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13201 /* Compute the preimage of the domain of "map" under the function
13202 * represented by "pma".
13203 * In other words, plug in "pma" in the domain of "map".
13204 * The result is a map that lives in the same space as "map",
13205 * except that domain space has been replaced by the domain space of "pma".
13207 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13208 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13210 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13213 /* Compute the preimage of the range of "map" under the function
13214 * represented by "pma".
13215 * In other words, plug in "pma" in the range of "map".
13216 * The result is a map that lives in the same space as "map",
13217 * except that range space has been replaced by the domain space of "pma".
13219 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13220 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13222 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13225 /* Compute the preimage of "map" under the function represented by "mpa".
13226 * In other words, plug in "mpa" in the domain or range of "map".
13227 * The result is a map that lives in the same space as "map",
13228 * except that the space of type "type" has been replaced by
13229 * the domain space of "mpa".
13231 * If the map does not involve any constraints that refer to the
13232 * dimensions of the substituted space, then the only possible
13233 * effect of "mpa" on the map is to map the space to a different space.
13234 * We create a separate isl_multi_aff to effectuate this change
13235 * in order to avoid spurious splitting of the map along the pieces
13236 * of "mpa".
13237 * If "mpa" has a non-trivial explicit domain, however,
13238 * then the full substitution should be performed.
13240 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13241 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13243 int n;
13244 isl_bool full;
13245 isl_pw_multi_aff *pma;
13247 if (!map || !mpa)
13248 goto error;
13250 n = isl_map_dim(map, type);
13251 full = isl_map_involves_dims(map, type, 0, n);
13252 if (full >= 0 && !full)
13253 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13254 if (full < 0)
13255 goto error;
13256 if (!full) {
13257 isl_space *space;
13258 isl_multi_aff *ma;
13260 space = isl_multi_pw_aff_get_space(mpa);
13261 isl_multi_pw_aff_free(mpa);
13262 ma = isl_multi_aff_zero(space);
13263 return isl_map_preimage_multi_aff(map, type, ma);
13266 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13267 return isl_map_preimage_pw_multi_aff(map, type, pma);
13268 error:
13269 isl_map_free(map);
13270 isl_multi_pw_aff_free(mpa);
13271 return NULL;
13274 /* Compute the preimage of "map" under the function represented by "mpa".
13275 * In other words, plug in "mpa" in the domain "map".
13276 * The result is a map that lives in the same space as "map",
13277 * except that domain space has been replaced by the domain space of "mpa".
13279 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13280 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13282 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13285 /* Compute the preimage of "set" by the function represented by "mpa".
13286 * In other words, plug in "mpa" in "set".
13288 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13289 __isl_take isl_multi_pw_aff *mpa)
13291 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13294 /* Return a copy of the equality constraints of "bset" as a matrix.
13296 __isl_give isl_mat *isl_basic_set_extract_equalities(
13297 __isl_keep isl_basic_set *bset)
13299 isl_ctx *ctx;
13300 unsigned total;
13302 if (!bset)
13303 return NULL;
13305 ctx = isl_basic_set_get_ctx(bset);
13306 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13307 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13310 /* Are the "n" "coefficients" starting at "first" of the integer division
13311 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13312 * to each other?
13313 * The "coefficient" at position 0 is the denominator.
13314 * The "coefficient" at position 1 is the constant term.
13316 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13317 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13318 unsigned first, unsigned n)
13320 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13321 return isl_bool_error;
13322 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13323 return isl_bool_error;
13324 return isl_seq_eq(bmap1->div[pos1] + first,
13325 bmap2->div[pos2] + first, n);
13328 /* Are the integer division expressions at position "pos1" in "bmap1" and
13329 * "pos2" in "bmap2" equal to each other, except that the constant terms
13330 * are different?
13332 isl_bool isl_basic_map_equal_div_expr_except_constant(
13333 __isl_keep isl_basic_map *bmap1, int pos1,
13334 __isl_keep isl_basic_map *bmap2, int pos2)
13336 isl_bool equal;
13337 unsigned total;
13339 if (!bmap1 || !bmap2)
13340 return isl_bool_error;
13341 total = isl_basic_map_total_dim(bmap1);
13342 if (total != isl_basic_map_total_dim(bmap2))
13343 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13344 "incomparable div expressions", return isl_bool_error);
13345 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13346 0, 1);
13347 if (equal < 0 || !equal)
13348 return equal;
13349 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13350 1, 1);
13351 if (equal < 0 || equal)
13352 return isl_bool_not(equal);
13353 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13354 2, total);
13357 /* Replace the numerator of the constant term of the integer division
13358 * expression at position "div" in "bmap" by "value".
13359 * The caller guarantees that this does not change the meaning
13360 * of the input.
13362 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13363 __isl_take isl_basic_map *bmap, int div, int value)
13365 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13366 return isl_basic_map_free(bmap);
13368 isl_int_set_si(bmap->div[div][1], value);
13370 return bmap;
13373 /* Is the point "inner" internal to inequality constraint "ineq"
13374 * of "bset"?
13375 * The point is considered to be internal to the inequality constraint,
13376 * if it strictly lies on the positive side of the inequality constraint,
13377 * or if it lies on the constraint and the constraint is lexico-positive.
13379 static isl_bool is_internal(__isl_keep isl_vec *inner,
13380 __isl_keep isl_basic_set *bset, int ineq)
13382 isl_ctx *ctx;
13383 int pos;
13384 unsigned total;
13386 if (!inner || !bset)
13387 return isl_bool_error;
13389 ctx = isl_basic_set_get_ctx(bset);
13390 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13391 &ctx->normalize_gcd);
13392 if (!isl_int_is_zero(ctx->normalize_gcd))
13393 return isl_int_is_nonneg(ctx->normalize_gcd);
13395 total = isl_basic_set_dim(bset, isl_dim_all);
13396 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13397 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13400 /* Tighten the inequality constraints of "bset" that are outward with respect
13401 * to the point "vec".
13402 * That is, tighten the constraints that are not satisfied by "vec".
13404 * "vec" is a point internal to some superset S of "bset" that is used
13405 * to make the subsets of S disjoint, by tightening one half of the constraints
13406 * that separate two subsets. In particular, the constraints of S
13407 * are all satisfied by "vec" and should not be tightened.
13408 * Of the internal constraints, those that have "vec" on the outside
13409 * are tightened. The shared facet is included in the adjacent subset
13410 * with the opposite constraint.
13411 * For constraints that saturate "vec", this criterion cannot be used
13412 * to determine which of the two sides should be tightened.
13413 * Instead, the sign of the first non-zero coefficient is used
13414 * to make this choice. Note that this second criterion is never used
13415 * on the constraints of S since "vec" is interior to "S".
13417 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13418 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13420 int j;
13422 bset = isl_basic_set_cow(bset);
13423 if (!bset)
13424 return NULL;
13425 for (j = 0; j < bset->n_ineq; ++j) {
13426 isl_bool internal;
13428 internal = is_internal(vec, bset, j);
13429 if (internal < 0)
13430 return isl_basic_set_free(bset);
13431 if (internal)
13432 continue;
13433 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13436 return bset;
13439 /* Replace the variables x of type "type" starting at "first" in "bmap"
13440 * by x' with x = M x' with M the matrix trans.
13441 * That is, replace the corresponding coefficients c by c M.
13443 * The transformation matrix should be a square matrix.
13445 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13446 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13447 __isl_take isl_mat *trans)
13449 unsigned pos;
13451 bmap = isl_basic_map_cow(bmap);
13452 if (!bmap || !trans)
13453 goto error;
13455 if (trans->n_row != trans->n_col)
13456 isl_die(trans->ctx, isl_error_invalid,
13457 "expecting square transformation matrix", goto error);
13458 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13459 goto error;
13461 pos = isl_basic_map_offset(bmap, type) + first;
13463 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13464 isl_mat_copy(trans)) < 0)
13465 goto error;
13466 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13467 isl_mat_copy(trans)) < 0)
13468 goto error;
13469 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13470 isl_mat_copy(trans)) < 0)
13471 goto error;
13473 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13474 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13476 isl_mat_free(trans);
13477 return bmap;
13478 error:
13479 isl_mat_free(trans);
13480 isl_basic_map_free(bmap);
13481 return NULL;
13484 /* Replace the variables x of type "type" starting at "first" in "bset"
13485 * by x' with x = M x' with M the matrix trans.
13486 * That is, replace the corresponding coefficients c by c M.
13488 * The transformation matrix should be a square matrix.
13490 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13491 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13492 __isl_take isl_mat *trans)
13494 return isl_basic_map_transform_dims(bset, type, first, trans);