extract out shared isl_basic_set_check_no_locals
[isl.git] / isl_map.c
blobf9513f5699106c8848972b65d71f52056f48dcb3
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/id.h>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl/map.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
36 #include <isl_sort.h>
37 #include "isl_tab.h"
38 #include <isl/vec.h>
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
48 #include <bset_to_bmap.c>
49 #include <bset_from_bmap.c>
50 #include <set_to_map.c>
51 #include <set_from_map.c>
53 /* Treat "bset" as a basic map.
54 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
55 * this function performs a redundant cast.
57 static __isl_keep const isl_basic_map *const_bset_to_bmap(
58 __isl_keep const isl_basic_set *bset)
60 return (const isl_basic_map *) bset;
63 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
65 switch (type) {
66 case isl_dim_param: return dim->nparam;
67 case isl_dim_in: return dim->n_in;
68 case isl_dim_out: return dim->n_out;
69 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
70 default: return 0;
74 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
76 switch (type) {
77 case isl_dim_param: return 1;
78 case isl_dim_in: return 1 + dim->nparam;
79 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
80 default: return 0;
84 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
85 enum isl_dim_type type)
87 if (!bmap)
88 return 0;
89 switch (type) {
90 case isl_dim_cst: return 1;
91 case isl_dim_param:
92 case isl_dim_in:
93 case isl_dim_out: return isl_space_dim(bmap->dim, type);
94 case isl_dim_div: return bmap->n_div;
95 case isl_dim_all: return isl_basic_map_total_dim(bmap);
96 default: return 0;
100 /* Return the space of "map".
102 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
104 return map ? map->dim : NULL;
107 /* Return the space of "set".
109 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
111 return isl_map_peek_space(set_to_map(set));
114 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
116 return map ? n(map->dim, type) : 0;
119 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
121 return set ? n(set->dim, type) : 0;
124 /* Return the position of the variables of the given type
125 * within the sequence of variables of "bmap".
127 int isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
128 enum isl_dim_type type)
130 isl_space *space;
132 space = isl_basic_map_peek_space(bmap);
133 if (!space)
134 return -1;
136 switch (type) {
137 case isl_dim_param:
138 case isl_dim_in:
139 case isl_dim_out: return isl_space_offset(space, type);
140 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
141 case isl_dim_cst:
142 default:
143 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
144 "invalid dimension type", return -1);
148 /* Return the position of the coefficients of the variables of the given type
149 * within the sequence of coefficients of "bmap".
151 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
152 enum isl_dim_type type)
154 switch (type) {
155 case isl_dim_cst: return 0;
156 case isl_dim_param:
157 case isl_dim_in:
158 case isl_dim_out:
159 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
160 default: return 0;
164 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
165 enum isl_dim_type type)
167 return isl_basic_map_offset(bset, type);
170 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
172 return pos(map->dim, type);
175 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
176 enum isl_dim_type type)
178 return isl_basic_map_dim(bset, type);
181 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
183 return isl_basic_set_dim(bset, isl_dim_set);
186 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
188 return isl_basic_set_dim(bset, isl_dim_param);
191 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
193 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
196 unsigned isl_set_n_dim(__isl_keep isl_set *set)
198 return isl_set_dim(set, isl_dim_set);
201 unsigned isl_set_n_param(__isl_keep isl_set *set)
203 return isl_set_dim(set, isl_dim_param);
206 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
208 return bmap ? bmap->dim->n_in : 0;
211 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
213 return bmap ? bmap->dim->n_out : 0;
216 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
218 return bmap ? bmap->dim->nparam : 0;
221 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
223 return bmap ? bmap->n_div : 0;
226 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
228 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
231 unsigned isl_map_n_in(__isl_keep const isl_map *map)
233 return map ? map->dim->n_in : 0;
236 unsigned isl_map_n_out(__isl_keep const isl_map *map)
238 return map ? map->dim->n_out : 0;
241 unsigned isl_map_n_param(__isl_keep const isl_map *map)
243 return map ? map->dim->nparam : 0;
246 /* Return the number of equality constraints in the description of "bmap".
247 * Return -1 on error.
249 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
251 if (!bmap)
252 return -1;
253 return bmap->n_eq;
256 /* Return the number of equality constraints in the description of "bset".
257 * Return -1 on error.
259 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
261 return isl_basic_map_n_equality(bset_to_bmap(bset));
264 /* Return the number of inequality constraints in the description of "bmap".
265 * Return -1 on error.
267 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
269 if (!bmap)
270 return -1;
271 return bmap->n_ineq;
274 /* Return the number of inequality constraints in the description of "bset".
275 * Return -1 on error.
277 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
279 return isl_basic_map_n_inequality(bset_to_bmap(bset));
282 /* Do "bmap1" and "bmap2" have the same parameters?
284 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
285 __isl_keep isl_basic_map *bmap2)
287 isl_space *space1, *space2;
289 space1 = isl_basic_map_peek_space(bmap1);
290 space2 = isl_basic_map_peek_space(bmap2);
291 return isl_space_has_equal_params(space1, space2);
294 /* Do "map1" and "map2" have the same parameters?
296 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
297 __isl_keep isl_map *map2)
299 isl_space *space1, *space2;
301 space1 = isl_map_peek_space(map1);
302 space2 = isl_map_peek_space(map2);
303 return isl_space_has_equal_params(space1, space2);
306 /* Do "map" and "set" have the same parameters?
308 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
309 __isl_keep isl_set *set)
311 return isl_map_has_equal_params(map, set_to_map(set));
314 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
315 __isl_keep isl_set *set)
317 isl_bool m;
318 if (!map || !set)
319 return isl_bool_error;
320 m = isl_map_has_equal_params(map, set_to_map(set));
321 if (m < 0 || !m)
322 return m;
323 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
324 set->dim, isl_dim_set);
327 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
328 __isl_keep isl_basic_set *bset)
330 isl_bool m;
331 if (!bmap || !bset)
332 return isl_bool_error;
333 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
334 if (m < 0 || !m)
335 return m;
336 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
337 bset->dim, isl_dim_set);
340 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
341 __isl_keep isl_set *set)
343 isl_bool m;
344 if (!map || !set)
345 return isl_bool_error;
346 m = isl_map_has_equal_params(map, set_to_map(set));
347 if (m < 0 || !m)
348 return m;
349 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
350 set->dim, isl_dim_set);
353 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
354 __isl_keep isl_basic_set *bset)
356 isl_bool m;
357 if (!bmap || !bset)
358 return isl_bool_error;
359 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
360 if (m < 0 || !m)
361 return m;
362 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
363 bset->dim, isl_dim_set);
366 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
368 return bmap ? bmap->ctx : NULL;
371 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
373 return bset ? bset->ctx : NULL;
376 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
378 return map ? map->ctx : NULL;
381 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
383 return set ? set->ctx : NULL;
386 /* Return the space of "bmap".
388 __isl_keep isl_space *isl_basic_map_peek_space(
389 __isl_keep const isl_basic_map *bmap)
391 return bmap ? bmap->dim : NULL;
394 /* Return the space of "bset".
396 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
398 return isl_basic_map_peek_space(bset_to_bmap(bset));
401 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
403 return isl_space_copy(isl_basic_map_peek_space(bmap));
406 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
408 return isl_basic_map_get_space(bset_to_bmap(bset));
411 /* Extract the divs in "bmap" as a matrix.
413 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
415 int i;
416 isl_ctx *ctx;
417 isl_mat *div;
418 unsigned total;
419 unsigned cols;
421 if (!bmap)
422 return NULL;
424 ctx = isl_basic_map_get_ctx(bmap);
425 total = isl_space_dim(bmap->dim, isl_dim_all);
426 cols = 1 + 1 + total + bmap->n_div;
427 div = isl_mat_alloc(ctx, bmap->n_div, cols);
428 if (!div)
429 return NULL;
431 for (i = 0; i < bmap->n_div; ++i)
432 isl_seq_cpy(div->row[i], bmap->div[i], cols);
434 return div;
437 /* Extract the divs in "bset" as a matrix.
439 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
441 return isl_basic_map_get_divs(bset);
444 __isl_give isl_local_space *isl_basic_map_get_local_space(
445 __isl_keep isl_basic_map *bmap)
447 isl_mat *div;
449 if (!bmap)
450 return NULL;
452 div = isl_basic_map_get_divs(bmap);
453 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
456 __isl_give isl_local_space *isl_basic_set_get_local_space(
457 __isl_keep isl_basic_set *bset)
459 return isl_basic_map_get_local_space(bset);
462 /* For each known div d = floor(f/m), add the constraints
464 * f - m d >= 0
465 * -(f-(m-1)) + m d >= 0
467 * Do not finalize the result.
469 static __isl_give isl_basic_map *add_known_div_constraints(
470 __isl_take isl_basic_map *bmap)
472 int i;
473 unsigned n_div;
475 if (!bmap)
476 return NULL;
477 n_div = isl_basic_map_dim(bmap, isl_dim_div);
478 if (n_div == 0)
479 return bmap;
480 bmap = isl_basic_map_cow(bmap);
481 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
482 if (!bmap)
483 return NULL;
484 for (i = 0; i < n_div; ++i) {
485 if (isl_int_is_zero(bmap->div[i][0]))
486 continue;
487 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
488 return isl_basic_map_free(bmap);
491 return bmap;
494 __isl_give isl_basic_map *isl_basic_map_from_local_space(
495 __isl_take isl_local_space *ls)
497 int i;
498 int n_div;
499 isl_basic_map *bmap;
501 if (!ls)
502 return NULL;
504 n_div = isl_local_space_dim(ls, isl_dim_div);
505 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
506 n_div, 0, 2 * n_div);
508 for (i = 0; i < n_div; ++i)
509 if (isl_basic_map_alloc_div(bmap) < 0)
510 goto error;
512 for (i = 0; i < n_div; ++i)
513 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
514 bmap = add_known_div_constraints(bmap);
516 isl_local_space_free(ls);
517 return bmap;
518 error:
519 isl_local_space_free(ls);
520 isl_basic_map_free(bmap);
521 return NULL;
524 __isl_give isl_basic_set *isl_basic_set_from_local_space(
525 __isl_take isl_local_space *ls)
527 return isl_basic_map_from_local_space(ls);
530 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
532 return isl_space_copy(isl_map_peek_space(map));
535 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
537 if (!set)
538 return NULL;
539 return isl_space_copy(set->dim);
542 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
543 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
545 bmap = isl_basic_map_cow(bmap);
546 if (!bmap)
547 return NULL;
548 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
549 if (!bmap->dim)
550 goto error;
551 bmap = isl_basic_map_finalize(bmap);
552 return bmap;
553 error:
554 isl_basic_map_free(bmap);
555 return NULL;
558 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
559 __isl_take isl_basic_set *bset, const char *s)
561 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
564 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
565 enum isl_dim_type type)
567 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
570 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
571 enum isl_dim_type type, const char *s)
573 int i;
575 map = isl_map_cow(map);
576 if (!map)
577 return NULL;
579 map->dim = isl_space_set_tuple_name(map->dim, type, s);
580 if (!map->dim)
581 goto error;
583 for (i = 0; i < map->n; ++i) {
584 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
585 if (!map->p[i])
586 goto error;
589 return map;
590 error:
591 isl_map_free(map);
592 return NULL;
595 /* Replace the identifier of the tuple of type "type" by "id".
597 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
598 __isl_take isl_basic_map *bmap,
599 enum isl_dim_type type, __isl_take isl_id *id)
601 bmap = isl_basic_map_cow(bmap);
602 if (!bmap)
603 goto error;
604 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
605 if (!bmap->dim)
606 return isl_basic_map_free(bmap);
607 bmap = isl_basic_map_finalize(bmap);
608 return bmap;
609 error:
610 isl_id_free(id);
611 return NULL;
614 /* Replace the identifier of the tuple by "id".
616 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
617 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
619 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
622 /* Does the input or output tuple have a name?
624 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
626 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
629 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
630 enum isl_dim_type type)
632 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
635 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
636 const char *s)
638 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
639 isl_dim_set, s));
642 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
643 enum isl_dim_type type, __isl_take isl_id *id)
645 map = isl_map_cow(map);
646 if (!map)
647 goto error;
649 map->dim = isl_space_set_tuple_id(map->dim, type, id);
651 return isl_map_reset_space(map, isl_space_copy(map->dim));
652 error:
653 isl_id_free(id);
654 return NULL;
657 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
658 __isl_take isl_id *id)
660 return isl_map_set_tuple_id(set, isl_dim_set, id);
663 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
664 enum isl_dim_type type)
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
670 map->dim = isl_space_reset_tuple_id(map->dim, type);
672 return isl_map_reset_space(map, isl_space_copy(map->dim));
675 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
677 return isl_map_reset_tuple_id(set, isl_dim_set);
680 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
682 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
685 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
686 enum isl_dim_type type)
688 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
691 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
693 return isl_map_has_tuple_id(set, isl_dim_set);
696 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
698 return isl_map_get_tuple_id(set, isl_dim_set);
701 /* Does the set tuple have a name?
703 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
705 if (!set)
706 return isl_bool_error;
707 return isl_space_has_tuple_name(set->dim, isl_dim_set);
711 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
713 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
716 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
718 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
721 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
722 enum isl_dim_type type, unsigned pos)
724 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
727 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
728 enum isl_dim_type type, unsigned pos)
730 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
733 /* Does the given dimension have a name?
735 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
736 enum isl_dim_type type, unsigned pos)
738 if (!map)
739 return isl_bool_error;
740 return isl_space_has_dim_name(map->dim, type, pos);
743 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
744 enum isl_dim_type type, unsigned pos)
746 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
749 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
750 enum isl_dim_type type, unsigned pos)
752 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
755 /* Does the given dimension have a name?
757 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
758 enum isl_dim_type type, unsigned pos)
760 if (!set)
761 return isl_bool_error;
762 return isl_space_has_dim_name(set->dim, type, pos);
765 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
766 __isl_take isl_basic_map *bmap,
767 enum isl_dim_type type, unsigned pos, const char *s)
769 bmap = isl_basic_map_cow(bmap);
770 if (!bmap)
771 return NULL;
772 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
773 if (!bmap->dim)
774 goto error;
775 return isl_basic_map_finalize(bmap);
776 error:
777 isl_basic_map_free(bmap);
778 return NULL;
781 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
782 enum isl_dim_type type, unsigned pos, const char *s)
784 int i;
786 map = isl_map_cow(map);
787 if (!map)
788 return NULL;
790 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
791 if (!map->dim)
792 goto error;
794 for (i = 0; i < map->n; ++i) {
795 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
796 if (!map->p[i])
797 goto error;
800 return map;
801 error:
802 isl_map_free(map);
803 return NULL;
806 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
807 __isl_take isl_basic_set *bset,
808 enum isl_dim_type type, unsigned pos, const char *s)
810 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
811 type, pos, s));
814 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
815 enum isl_dim_type type, unsigned pos, const char *s)
817 return set_from_map(isl_map_set_dim_name(set_to_map(set),
818 type, pos, s));
821 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
822 enum isl_dim_type type, unsigned pos)
824 if (!bmap)
825 return isl_bool_error;
826 return isl_space_has_dim_id(bmap->dim, type, pos);
829 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
830 enum isl_dim_type type, unsigned pos)
832 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
835 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
836 enum isl_dim_type type, unsigned pos)
838 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
841 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
842 enum isl_dim_type type, unsigned pos)
844 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
847 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
848 enum isl_dim_type type, unsigned pos)
850 return isl_map_has_dim_id(set, type, pos);
853 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
854 enum isl_dim_type type, unsigned pos)
856 return isl_map_get_dim_id(set, type, pos);
859 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
860 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
862 map = isl_map_cow(map);
863 if (!map)
864 goto error;
866 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
868 return isl_map_reset_space(map, isl_space_copy(map->dim));
869 error:
870 isl_id_free(id);
871 return NULL;
874 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
875 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
877 return isl_map_set_dim_id(set, type, pos, id);
880 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
881 __isl_keep isl_id *id)
883 if (!map)
884 return -1;
885 return isl_space_find_dim_by_id(map->dim, type, id);
888 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
889 __isl_keep isl_id *id)
891 return isl_map_find_dim_by_id(set, type, id);
894 /* Return the position of the dimension of the given type and name
895 * in "bmap".
896 * Return -1 if no such dimension can be found.
898 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
899 enum isl_dim_type type, const char *name)
901 if (!bmap)
902 return -1;
903 return isl_space_find_dim_by_name(bmap->dim, type, name);
906 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
907 const char *name)
909 if (!map)
910 return -1;
911 return isl_space_find_dim_by_name(map->dim, type, name);
914 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
915 const char *name)
917 return isl_map_find_dim_by_name(set, type, name);
920 /* Check whether equality i of bset is a pure stride constraint
921 * on a single dimension, i.e., of the form
923 * v = k e
925 * with k a constant and e an existentially quantified variable.
927 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
929 unsigned nparam;
930 unsigned d;
931 unsigned n_div;
932 int pos1;
933 int pos2;
935 if (!bset)
936 return isl_bool_error;
938 if (!isl_int_is_zero(bset->eq[i][0]))
939 return isl_bool_false;
941 nparam = isl_basic_set_dim(bset, isl_dim_param);
942 d = isl_basic_set_dim(bset, isl_dim_set);
943 n_div = isl_basic_set_dim(bset, isl_dim_div);
945 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
946 return isl_bool_false;
947 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
948 if (pos1 == -1)
949 return isl_bool_false;
950 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
951 d - pos1 - 1) != -1)
952 return isl_bool_false;
954 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
955 if (pos2 == -1)
956 return isl_bool_false;
957 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
958 n_div - pos2 - 1) != -1)
959 return isl_bool_false;
960 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
961 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
962 return isl_bool_false;
964 return isl_bool_true;
967 /* Reset the user pointer on all identifiers of parameters and tuples
968 * of the space of "map".
970 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
972 isl_space *space;
974 space = isl_map_get_space(map);
975 space = isl_space_reset_user(space);
976 map = isl_map_reset_space(map, space);
978 return map;
981 /* Reset the user pointer on all identifiers of parameters and tuples
982 * of the space of "set".
984 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
986 return isl_map_reset_user(set);
989 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
991 if (!bmap)
992 return isl_bool_error;
993 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
996 /* Has "map" been marked as a rational map?
997 * In particular, have all basic maps in "map" been marked this way?
998 * An empty map is not considered to be rational.
999 * Maps where only some of the basic maps are marked rational
1000 * are not allowed.
1002 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1004 int i;
1005 isl_bool rational;
1007 if (!map)
1008 return isl_bool_error;
1009 if (map->n == 0)
1010 return isl_bool_false;
1011 rational = isl_basic_map_is_rational(map->p[0]);
1012 if (rational < 0)
1013 return rational;
1014 for (i = 1; i < map->n; ++i) {
1015 isl_bool rational_i;
1017 rational_i = isl_basic_map_is_rational(map->p[i]);
1018 if (rational_i < 0)
1019 return rational_i;
1020 if (rational != rational_i)
1021 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1022 "mixed rational and integer basic maps "
1023 "not supported", return isl_bool_error);
1026 return rational;
1029 /* Has "set" been marked as a rational set?
1030 * In particular, have all basic set in "set" been marked this way?
1031 * An empty set is not considered to be rational.
1032 * Sets where only some of the basic sets are marked rational
1033 * are not allowed.
1035 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1037 return isl_map_is_rational(set);
1040 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1042 return isl_basic_map_is_rational(bset);
1045 /* Does "bmap" contain any rational points?
1047 * If "bmap" has an equality for each dimension, equating the dimension
1048 * to an integer constant, then it has no rational points, even if it
1049 * is marked as rational.
1051 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1053 isl_bool has_rational = isl_bool_true;
1054 unsigned total;
1056 if (!bmap)
1057 return isl_bool_error;
1058 if (isl_basic_map_plain_is_empty(bmap))
1059 return isl_bool_false;
1060 if (!isl_basic_map_is_rational(bmap))
1061 return isl_bool_false;
1062 bmap = isl_basic_map_copy(bmap);
1063 bmap = isl_basic_map_implicit_equalities(bmap);
1064 if (!bmap)
1065 return isl_bool_error;
1066 total = isl_basic_map_total_dim(bmap);
1067 if (bmap->n_eq == total) {
1068 int i, j;
1069 for (i = 0; i < bmap->n_eq; ++i) {
1070 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1071 if (j < 0)
1072 break;
1073 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1074 !isl_int_is_negone(bmap->eq[i][1 + j]))
1075 break;
1076 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1077 total - j - 1);
1078 if (j >= 0)
1079 break;
1081 if (i == bmap->n_eq)
1082 has_rational = isl_bool_false;
1084 isl_basic_map_free(bmap);
1086 return has_rational;
1089 /* Does "map" contain any rational points?
1091 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1093 int i;
1094 isl_bool has_rational;
1096 if (!map)
1097 return isl_bool_error;
1098 for (i = 0; i < map->n; ++i) {
1099 has_rational = isl_basic_map_has_rational(map->p[i]);
1100 if (has_rational < 0 || has_rational)
1101 return has_rational;
1103 return isl_bool_false;
1106 /* Does "set" contain any rational points?
1108 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1110 return isl_map_has_rational(set);
1113 /* Is this basic set a parameter domain?
1115 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1117 if (!bset)
1118 return isl_bool_error;
1119 return isl_space_is_params(bset->dim);
1122 /* Is this set a parameter domain?
1124 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1126 if (!set)
1127 return isl_bool_error;
1128 return isl_space_is_params(set->dim);
1131 /* Is this map actually a parameter domain?
1132 * Users should never call this function. Outside of isl,
1133 * a map can never be a parameter domain.
1135 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1137 if (!map)
1138 return isl_bool_error;
1139 return isl_space_is_params(map->dim);
1142 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1143 __isl_take isl_basic_map *bmap, unsigned extra,
1144 unsigned n_eq, unsigned n_ineq)
1146 int i;
1147 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1149 bmap->ctx = ctx;
1150 isl_ctx_ref(ctx);
1152 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1153 if (isl_blk_is_error(bmap->block))
1154 goto error;
1156 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1157 if ((n_ineq + n_eq) && !bmap->ineq)
1158 goto error;
1160 if (extra == 0) {
1161 bmap->block2 = isl_blk_empty();
1162 bmap->div = NULL;
1163 } else {
1164 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1165 if (isl_blk_is_error(bmap->block2))
1166 goto error;
1168 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1169 if (!bmap->div)
1170 goto error;
1173 for (i = 0; i < n_ineq + n_eq; ++i)
1174 bmap->ineq[i] = bmap->block.data + i * row_size;
1176 for (i = 0; i < extra; ++i)
1177 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1179 bmap->ref = 1;
1180 bmap->flags = 0;
1181 bmap->c_size = n_eq + n_ineq;
1182 bmap->eq = bmap->ineq + n_ineq;
1183 bmap->extra = extra;
1184 bmap->n_eq = 0;
1185 bmap->n_ineq = 0;
1186 bmap->n_div = 0;
1187 bmap->sample = NULL;
1189 return bmap;
1190 error:
1191 isl_basic_map_free(bmap);
1192 return NULL;
1195 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1196 unsigned nparam, unsigned dim, unsigned extra,
1197 unsigned n_eq, unsigned n_ineq)
1199 struct isl_basic_map *bmap;
1200 isl_space *space;
1202 space = isl_space_set_alloc(ctx, nparam, dim);
1203 if (!space)
1204 return NULL;
1206 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1207 return bset_from_bmap(bmap);
1210 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1211 unsigned extra, unsigned n_eq, unsigned n_ineq)
1213 struct isl_basic_map *bmap;
1214 if (!dim)
1215 return NULL;
1216 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1217 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1218 return bset_from_bmap(bmap);
1219 error:
1220 isl_space_free(dim);
1221 return NULL;
1224 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1225 unsigned extra, unsigned n_eq, unsigned n_ineq)
1227 struct isl_basic_map *bmap;
1229 if (!space)
1230 return NULL;
1231 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1232 if (!bmap)
1233 goto error;
1234 bmap->dim = space;
1236 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1237 error:
1238 isl_space_free(space);
1239 return NULL;
1242 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1243 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1244 unsigned n_eq, unsigned n_ineq)
1246 struct isl_basic_map *bmap;
1247 isl_space *dim;
1249 dim = isl_space_alloc(ctx, nparam, in, out);
1250 if (!dim)
1251 return NULL;
1253 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1254 return bmap;
1257 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1258 __isl_keep isl_basic_map *src)
1260 int i;
1261 unsigned total = isl_basic_map_total_dim(src);
1263 if (!dst)
1264 return NULL;
1266 for (i = 0; i < src->n_eq; ++i) {
1267 int j = isl_basic_map_alloc_equality(dst);
1268 if (j < 0)
1269 return isl_basic_map_free(dst);
1270 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1273 for (i = 0; i < src->n_ineq; ++i) {
1274 int j = isl_basic_map_alloc_inequality(dst);
1275 if (j < 0)
1276 return isl_basic_map_free(dst);
1277 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1280 for (i = 0; i < src->n_div; ++i) {
1281 int j = isl_basic_map_alloc_div(dst);
1282 if (j < 0)
1283 return isl_basic_map_free(dst);
1284 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1286 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1287 return dst;
1290 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1292 struct isl_basic_map *dup;
1294 if (!bmap)
1295 return NULL;
1296 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1297 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1298 dup = dup_constraints(dup, bmap);
1299 if (!dup)
1300 return NULL;
1301 dup->flags = bmap->flags;
1302 dup->sample = isl_vec_copy(bmap->sample);
1303 return dup;
1306 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1308 struct isl_basic_map *dup;
1310 dup = isl_basic_map_dup(bset_to_bmap(bset));
1311 return bset_from_bmap(dup);
1314 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1316 if (!bset)
1317 return NULL;
1319 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1320 bset->ref++;
1321 return bset;
1323 return isl_basic_set_dup(bset);
1326 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1328 if (!set)
1329 return NULL;
1331 set->ref++;
1332 return set;
1335 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1337 if (!bmap)
1338 return NULL;
1340 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1341 bmap->ref++;
1342 return bmap;
1344 bmap = isl_basic_map_dup(bmap);
1345 if (bmap)
1346 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1347 return bmap;
1350 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1352 if (!map)
1353 return NULL;
1355 map->ref++;
1356 return map;
1359 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1361 if (!bmap)
1362 return NULL;
1364 if (--bmap->ref > 0)
1365 return NULL;
1367 isl_ctx_deref(bmap->ctx);
1368 free(bmap->div);
1369 isl_blk_free(bmap->ctx, bmap->block2);
1370 free(bmap->ineq);
1371 isl_blk_free(bmap->ctx, bmap->block);
1372 isl_vec_free(bmap->sample);
1373 isl_space_free(bmap->dim);
1374 free(bmap);
1376 return NULL;
1379 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1381 return isl_basic_map_free(bset_to_bmap(bset));
1384 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1386 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1389 /* Check that "bset" does not involve any parameters.
1391 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1393 if (!bset)
1394 return isl_stat_error;
1395 if (isl_basic_set_dim(bset, isl_dim_param) != 0)
1396 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1397 "basic set should not have any parameters",
1398 return isl_stat_error);
1399 return isl_stat_ok;
1402 /* Check that "bset" does not involve any local variables.
1404 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1406 if (!bset)
1407 return isl_stat_error;
1408 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
1409 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1410 "basic set should not have any local variables",
1411 return isl_stat_error);
1412 return isl_stat_ok;
1415 /* Check that "map" has only named parameters, reporting an error
1416 * if it does not.
1418 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1420 return isl_space_check_named_params(isl_map_peek_space(map));
1423 /* Check that "bmap" has only named parameters, reporting an error
1424 * if it does not.
1426 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1428 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1431 /* Check that "bmap1" and "bmap2" have the same parameters,
1432 * reporting an error if they do not.
1434 static isl_stat isl_basic_map_check_equal_params(
1435 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1437 isl_bool match;
1439 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1440 if (match < 0)
1441 return isl_stat_error;
1442 if (!match)
1443 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1444 "parameters don't match", return isl_stat_error);
1445 return isl_stat_ok;
1448 __isl_give isl_map *isl_map_align_params_map_map_and(
1449 __isl_take isl_map *map1, __isl_take isl_map *map2,
1450 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1451 __isl_take isl_map *map2))
1453 if (!map1 || !map2)
1454 goto error;
1455 if (isl_map_has_equal_params(map1, map2))
1456 return fn(map1, map2);
1457 if (isl_map_check_named_params(map1) < 0)
1458 goto error;
1459 if (isl_map_check_named_params(map2) < 0)
1460 goto error;
1461 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1462 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1463 return fn(map1, map2);
1464 error:
1465 isl_map_free(map1);
1466 isl_map_free(map2);
1467 return NULL;
1470 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1471 __isl_keep isl_map *map2,
1472 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1474 isl_bool r;
1476 if (!map1 || !map2)
1477 return isl_bool_error;
1478 if (isl_map_has_equal_params(map1, map2))
1479 return fn(map1, map2);
1480 if (isl_map_check_named_params(map1) < 0)
1481 return isl_bool_error;
1482 if (isl_map_check_named_params(map2) < 0)
1483 return isl_bool_error;
1484 map1 = isl_map_copy(map1);
1485 map2 = isl_map_copy(map2);
1486 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1487 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1488 r = fn(map1, map2);
1489 isl_map_free(map1);
1490 isl_map_free(map2);
1491 return r;
1494 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1496 struct isl_ctx *ctx;
1497 if (!bmap)
1498 return -1;
1499 ctx = bmap->ctx;
1500 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1501 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1502 return -1);
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1505 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1506 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1507 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1508 isl_int *t;
1509 int j = isl_basic_map_alloc_inequality(bmap);
1510 if (j < 0)
1511 return -1;
1512 t = bmap->ineq[j];
1513 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1514 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1515 bmap->eq[-1] = t;
1516 bmap->n_eq++;
1517 bmap->n_ineq--;
1518 bmap->eq--;
1519 return 0;
1521 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1522 bmap->extra - bmap->n_div);
1523 return bmap->n_eq++;
1526 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1528 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1531 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1533 if (!bmap)
1534 return -1;
1535 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1536 bmap->n_eq -= n;
1537 return 0;
1540 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1542 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1545 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1547 isl_int *t;
1548 if (!bmap)
1549 return -1;
1550 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1552 if (pos != bmap->n_eq - 1) {
1553 t = bmap->eq[pos];
1554 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1555 bmap->eq[bmap->n_eq - 1] = t;
1557 bmap->n_eq--;
1558 return 0;
1561 /* Turn inequality "pos" of "bmap" into an equality.
1563 * In particular, we move the inequality in front of the equalities
1564 * and move the last inequality in the position of the moved inequality.
1565 * Note that isl_tab_make_equalities_explicit depends on this particular
1566 * change in the ordering of the constraints.
1568 void isl_basic_map_inequality_to_equality(
1569 struct isl_basic_map *bmap, unsigned pos)
1571 isl_int *t;
1573 t = bmap->ineq[pos];
1574 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1575 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1576 bmap->eq[-1] = t;
1577 bmap->n_eq++;
1578 bmap->n_ineq--;
1579 bmap->eq--;
1580 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1581 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1582 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1583 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1586 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1588 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1591 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1593 struct isl_ctx *ctx;
1594 if (!bmap)
1595 return -1;
1596 ctx = bmap->ctx;
1597 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1598 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1599 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1600 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1601 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1602 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1603 1 + isl_basic_map_total_dim(bmap),
1604 bmap->extra - bmap->n_div);
1605 return bmap->n_ineq++;
1608 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1610 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1613 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1615 if (!bmap)
1616 return -1;
1617 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1618 bmap->n_ineq -= n;
1619 return 0;
1622 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1624 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1627 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1629 isl_int *t;
1630 if (!bmap)
1631 return -1;
1632 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1634 if (pos != bmap->n_ineq - 1) {
1635 t = bmap->ineq[pos];
1636 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1637 bmap->ineq[bmap->n_ineq - 1] = t;
1638 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1640 bmap->n_ineq--;
1641 return 0;
1644 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1646 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1649 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1650 isl_int *eq)
1652 isl_bool empty;
1653 int k;
1655 empty = isl_basic_map_plain_is_empty(bmap);
1656 if (empty < 0)
1657 return isl_basic_map_free(bmap);
1658 if (empty)
1659 return bmap;
1661 bmap = isl_basic_map_cow(bmap);
1662 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1663 if (!bmap)
1664 return NULL;
1665 k = isl_basic_map_alloc_equality(bmap);
1666 if (k < 0)
1667 goto error;
1668 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1669 return bmap;
1670 error:
1671 isl_basic_map_free(bmap);
1672 return NULL;
1675 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1676 isl_int *eq)
1678 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1681 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1682 isl_int *ineq)
1684 int k;
1686 bmap = isl_basic_map_cow(bmap);
1687 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1688 if (!bmap)
1689 return NULL;
1690 k = isl_basic_map_alloc_inequality(bmap);
1691 if (k < 0)
1692 goto error;
1693 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1694 return bmap;
1695 error:
1696 isl_basic_map_free(bmap);
1697 return NULL;
1700 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1701 isl_int *ineq)
1703 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1706 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1708 if (!bmap)
1709 return -1;
1710 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1711 isl_seq_clr(bmap->div[bmap->n_div] +
1712 1 + 1 + isl_basic_map_total_dim(bmap),
1713 bmap->extra - bmap->n_div);
1714 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1715 return bmap->n_div++;
1718 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1720 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1723 #undef TYPE
1724 #define TYPE isl_basic_map
1725 #include "check_type_range_templ.c"
1727 /* Check that there are "n" dimensions of type "type" starting at "first"
1728 * in "bset".
1730 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1731 enum isl_dim_type type, unsigned first, unsigned n)
1733 return isl_basic_map_check_range(bset_to_bmap(bset),
1734 type, first, n);
1737 /* Insert an extra integer division, prescribed by "div", to "bmap"
1738 * at (integer division) position "pos".
1740 * The integer division is first added at the end and then moved
1741 * into the right position.
1743 __isl_give isl_basic_map *isl_basic_map_insert_div(
1744 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1746 int i, k;
1748 bmap = isl_basic_map_cow(bmap);
1749 if (!bmap || !div)
1750 return isl_basic_map_free(bmap);
1752 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1753 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1754 "unexpected size", return isl_basic_map_free(bmap));
1755 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1756 return isl_basic_map_free(bmap);
1758 bmap = isl_basic_map_extend_space(bmap,
1759 isl_basic_map_get_space(bmap), 1, 0, 2);
1760 k = isl_basic_map_alloc_div(bmap);
1761 if (k < 0)
1762 return isl_basic_map_free(bmap);
1763 isl_seq_cpy(bmap->div[k], div->el, div->size);
1764 isl_int_set_si(bmap->div[k][div->size], 0);
1766 for (i = k; i > pos; --i)
1767 isl_basic_map_swap_div(bmap, i, i - 1);
1769 return bmap;
1772 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1774 if (!bmap)
1775 return isl_stat_error;
1776 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1777 bmap->n_div -= n;
1778 return isl_stat_ok;
1781 static __isl_give isl_basic_map *add_constraints(
1782 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1783 unsigned i_pos, unsigned o_pos)
1785 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1786 isl_ctx *ctx;
1787 isl_space *space;
1788 struct isl_dim_map *dim_map;
1790 space = isl_basic_map_peek_space(bmap2);
1791 if (!bmap1 || !space)
1792 goto error;
1794 total = isl_basic_map_dim(bmap1, isl_dim_all);
1795 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1796 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1797 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1798 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1799 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1800 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1801 ctx = isl_basic_map_get_ctx(bmap1);
1802 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1803 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1804 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1805 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1806 isl_dim_map_div(dim_map, bmap2, total);
1808 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1809 error:
1810 isl_basic_map_free(bmap1);
1811 isl_basic_map_free(bmap2);
1812 return NULL;
1815 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1816 struct isl_basic_set *bset2, unsigned pos)
1818 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1819 bset_to_bmap(bset2), 0, pos));
1822 __isl_give isl_basic_map *isl_basic_map_extend_space(
1823 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1824 unsigned extra, unsigned n_eq, unsigned n_ineq)
1826 struct isl_basic_map *ext;
1827 unsigned flags;
1828 int dims_ok;
1830 if (!space)
1831 goto error;
1833 if (!base)
1834 goto error;
1836 dims_ok = isl_space_is_equal(base->dim, space) &&
1837 base->extra >= base->n_div + extra;
1839 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1840 room_for_ineq(base, n_ineq)) {
1841 isl_space_free(space);
1842 return base;
1845 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1846 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1847 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1848 extra += base->extra;
1849 n_eq += base->n_eq;
1850 n_ineq += base->n_ineq;
1852 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1853 space = NULL;
1854 if (!ext)
1855 goto error;
1857 if (dims_ok)
1858 ext->sample = isl_vec_copy(base->sample);
1859 flags = base->flags;
1860 ext = add_constraints(ext, base, 0, 0);
1861 if (ext) {
1862 ext->flags = flags;
1863 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1866 return ext;
1868 error:
1869 isl_space_free(space);
1870 isl_basic_map_free(base);
1871 return NULL;
1874 __isl_give isl_basic_set *isl_basic_set_extend_space(
1875 __isl_take isl_basic_set *base,
1876 __isl_take isl_space *dim, unsigned extra,
1877 unsigned n_eq, unsigned n_ineq)
1879 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1880 dim, extra, n_eq, n_ineq));
1883 struct isl_basic_map *isl_basic_map_extend_constraints(
1884 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1886 if (!base)
1887 return NULL;
1888 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1889 0, n_eq, n_ineq);
1892 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1893 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1894 unsigned n_eq, unsigned n_ineq)
1896 struct isl_basic_map *bmap;
1897 isl_space *dim;
1899 if (!base)
1900 return NULL;
1901 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1902 if (!dim)
1903 goto error;
1905 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1906 return bmap;
1907 error:
1908 isl_basic_map_free(base);
1909 return NULL;
1912 struct isl_basic_set *isl_basic_set_extend_constraints(
1913 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1915 isl_basic_map *bmap = bset_to_bmap(base);
1916 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1917 return bset_from_bmap(bmap);
1920 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1922 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1925 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1927 if (!bmap)
1928 return NULL;
1930 if (bmap->ref > 1) {
1931 bmap->ref--;
1932 bmap = isl_basic_map_dup(bmap);
1934 if (bmap) {
1935 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1936 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1938 return bmap;
1941 /* Clear all cached information in "map", either because it is about
1942 * to be modified or because it is being freed.
1943 * Always return the same pointer that is passed in.
1944 * This is needed for the use in isl_map_free.
1946 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1948 isl_basic_map_free(map->cached_simple_hull[0]);
1949 isl_basic_map_free(map->cached_simple_hull[1]);
1950 map->cached_simple_hull[0] = NULL;
1951 map->cached_simple_hull[1] = NULL;
1952 return map;
1955 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1957 return isl_map_cow(set);
1960 /* Return an isl_map that is equal to "map" and that has only
1961 * a single reference.
1963 * If the original input already has only one reference, then
1964 * simply return it, but clear all cached information, since
1965 * it may be rendered invalid by the operations that will be
1966 * performed on the result.
1968 * Otherwise, create a duplicate (without any cached information).
1970 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1972 if (!map)
1973 return NULL;
1975 if (map->ref == 1)
1976 return clear_caches(map);
1977 map->ref--;
1978 return isl_map_dup(map);
1981 static void swap_vars(struct isl_blk blk, isl_int *a,
1982 unsigned a_len, unsigned b_len)
1984 isl_seq_cpy(blk.data, a+a_len, b_len);
1985 isl_seq_cpy(blk.data+b_len, a, a_len);
1986 isl_seq_cpy(a, blk.data, b_len+a_len);
1989 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1990 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1992 int i;
1993 struct isl_blk blk;
1995 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1996 goto error;
1998 if (n1 == 0 || n2 == 0)
1999 return bmap;
2001 bmap = isl_basic_map_cow(bmap);
2002 if (!bmap)
2003 return NULL;
2005 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2006 if (isl_blk_is_error(blk))
2007 goto error;
2009 for (i = 0; i < bmap->n_eq; ++i)
2010 swap_vars(blk,
2011 bmap->eq[i] + pos, n1, n2);
2013 for (i = 0; i < bmap->n_ineq; ++i)
2014 swap_vars(blk,
2015 bmap->ineq[i] + pos, n1, n2);
2017 for (i = 0; i < bmap->n_div; ++i)
2018 swap_vars(blk,
2019 bmap->div[i]+1 + pos, n1, n2);
2021 isl_blk_free(bmap->ctx, blk);
2023 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2024 bmap = isl_basic_map_gauss(bmap, NULL);
2025 return isl_basic_map_finalize(bmap);
2026 error:
2027 isl_basic_map_free(bmap);
2028 return NULL;
2031 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2032 __isl_take isl_basic_map *bmap)
2034 int i = 0;
2035 unsigned total;
2036 if (!bmap)
2037 goto error;
2038 total = isl_basic_map_total_dim(bmap);
2039 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2040 return isl_basic_map_free(bmap);
2041 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2042 if (bmap->n_eq > 0)
2043 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2044 else {
2045 i = isl_basic_map_alloc_equality(bmap);
2046 if (i < 0)
2047 goto error;
2049 isl_int_set_si(bmap->eq[i][0], 1);
2050 isl_seq_clr(bmap->eq[i]+1, total);
2051 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2052 isl_vec_free(bmap->sample);
2053 bmap->sample = NULL;
2054 return isl_basic_map_finalize(bmap);
2055 error:
2056 isl_basic_map_free(bmap);
2057 return NULL;
2060 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2061 __isl_take isl_basic_set *bset)
2063 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2066 __isl_give isl_basic_map *isl_basic_map_set_rational(
2067 __isl_take isl_basic_map *bmap)
2069 if (!bmap)
2070 return NULL;
2072 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2073 return bmap;
2075 bmap = isl_basic_map_cow(bmap);
2076 if (!bmap)
2077 return NULL;
2079 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2081 return isl_basic_map_finalize(bmap);
2084 __isl_give isl_basic_set *isl_basic_set_set_rational(
2085 __isl_take isl_basic_set *bset)
2087 return isl_basic_map_set_rational(bset);
2090 __isl_give isl_basic_set *isl_basic_set_set_integral(
2091 __isl_take isl_basic_set *bset)
2093 if (!bset)
2094 return NULL;
2096 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2097 return bset;
2099 bset = isl_basic_set_cow(bset);
2100 if (!bset)
2101 return NULL;
2103 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2105 return isl_basic_set_finalize(bset);
2108 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2110 int i;
2112 map = isl_map_cow(map);
2113 if (!map)
2114 return NULL;
2115 for (i = 0; i < map->n; ++i) {
2116 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2117 if (!map->p[i])
2118 goto error;
2120 return map;
2121 error:
2122 isl_map_free(map);
2123 return NULL;
2126 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2128 return isl_map_set_rational(set);
2131 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2132 * of "bmap").
2134 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2136 isl_int *t = bmap->div[a];
2137 bmap->div[a] = bmap->div[b];
2138 bmap->div[b] = t;
2141 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2142 * div definitions accordingly.
2144 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2146 int i;
2147 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2149 swap_div(bmap, a, b);
2151 for (i = 0; i < bmap->n_eq; ++i)
2152 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2154 for (i = 0; i < bmap->n_ineq; ++i)
2155 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2157 for (i = 0; i < bmap->n_div; ++i)
2158 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2159 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2162 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2164 isl_seq_cpy(c, c + n, rem);
2165 isl_seq_clr(c + rem, n);
2168 /* Drop n dimensions starting at first.
2170 * In principle, this frees up some extra variables as the number
2171 * of columns remains constant, but we would have to extend
2172 * the div array too as the number of rows in this array is assumed
2173 * to be equal to extra.
2175 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2176 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2178 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2181 /* Move "n" divs starting at "first" to the end of the list of divs.
2183 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2184 unsigned first, unsigned n)
2186 isl_int **div;
2187 int i;
2189 if (first + n == bmap->n_div)
2190 return bmap;
2192 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2193 if (!div)
2194 goto error;
2195 for (i = 0; i < n; ++i)
2196 div[i] = bmap->div[first + i];
2197 for (i = 0; i < bmap->n_div - first - n; ++i)
2198 bmap->div[first + i] = bmap->div[first + n + i];
2199 for (i = 0; i < n; ++i)
2200 bmap->div[bmap->n_div - n + i] = div[i];
2201 free(div);
2202 return bmap;
2203 error:
2204 isl_basic_map_free(bmap);
2205 return NULL;
2208 #undef TYPE
2209 #define TYPE isl_map
2210 static
2211 #include "check_type_range_templ.c"
2213 /* Check that there are "n" dimensions of type "type" starting at "first"
2214 * in "set".
2216 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2217 enum isl_dim_type type, unsigned first, unsigned n)
2219 return isl_map_check_range(set_to_map(set), type, first, n);
2222 /* Drop "n" dimensions of type "type" starting at "first".
2223 * Perform the core computation, without cowing or
2224 * simplifying and finalizing the result.
2226 * In principle, this frees up some extra variables as the number
2227 * of columns remains constant, but we would have to extend
2228 * the div array too as the number of rows in this array is assumed
2229 * to be equal to extra.
2231 __isl_give isl_basic_map *isl_basic_map_drop_core(
2232 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2233 unsigned first, unsigned n)
2235 int i;
2236 unsigned offset;
2237 unsigned left;
2239 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2240 return isl_basic_map_free(bmap);
2242 offset = isl_basic_map_offset(bmap, type) + first;
2243 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2244 for (i = 0; i < bmap->n_eq; ++i)
2245 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2247 for (i = 0; i < bmap->n_ineq; ++i)
2248 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2250 for (i = 0; i < bmap->n_div; ++i)
2251 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2253 if (type == isl_dim_div) {
2254 bmap = move_divs_last(bmap, first, n);
2255 if (!bmap)
2256 return NULL;
2257 if (isl_basic_map_free_div(bmap, n) < 0)
2258 return isl_basic_map_free(bmap);
2259 } else
2260 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2261 if (!bmap->dim)
2262 return isl_basic_map_free(bmap);
2264 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2265 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2266 return bmap;
2269 /* Drop "n" dimensions of type "type" starting at "first".
2271 * In principle, this frees up some extra variables as the number
2272 * of columns remains constant, but we would have to extend
2273 * the div array too as the number of rows in this array is assumed
2274 * to be equal to extra.
2276 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2277 enum isl_dim_type type, unsigned first, unsigned n)
2279 if (!bmap)
2280 return NULL;
2281 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2282 return bmap;
2284 bmap = isl_basic_map_cow(bmap);
2285 if (!bmap)
2286 return NULL;
2288 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2290 bmap = isl_basic_map_simplify(bmap);
2291 return isl_basic_map_finalize(bmap);
2294 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2295 enum isl_dim_type type, unsigned first, unsigned n)
2297 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2298 type, first, n));
2301 /* No longer consider "map" to be normalized.
2303 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2305 if (!map)
2306 return NULL;
2307 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2308 return map;
2311 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2312 enum isl_dim_type type, unsigned first, unsigned n)
2314 int i;
2316 if (isl_map_check_range(map, type, first, n) < 0)
2317 return isl_map_free(map);
2319 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2320 return map;
2321 map = isl_map_cow(map);
2322 if (!map)
2323 goto error;
2324 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2325 if (!map->dim)
2326 goto error;
2328 for (i = 0; i < map->n; ++i) {
2329 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2330 if (!map->p[i])
2331 goto error;
2333 map = isl_map_unmark_normalized(map);
2335 return map;
2336 error:
2337 isl_map_free(map);
2338 return NULL;
2341 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2342 enum isl_dim_type type, unsigned first, unsigned n)
2344 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2347 /* Drop the integer division at position "div", which is assumed
2348 * not to appear in any of the constraints or
2349 * in any of the other integer divisions.
2351 * Since the integer division is redundant, there is no need to cow.
2353 __isl_give isl_basic_map *isl_basic_map_drop_div(
2354 __isl_take isl_basic_map *bmap, unsigned div)
2356 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2359 /* Eliminate the specified n dimensions starting at first from the
2360 * constraints, without removing the dimensions from the space.
2361 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2363 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2364 enum isl_dim_type type, unsigned first, unsigned n)
2366 int i;
2368 if (n == 0)
2369 return map;
2371 if (isl_map_check_range(map, type, first, n) < 0)
2372 return isl_map_free(map);
2374 map = isl_map_cow(map);
2375 if (!map)
2376 return NULL;
2378 for (i = 0; i < map->n; ++i) {
2379 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2380 if (!map->p[i])
2381 goto error;
2383 return map;
2384 error:
2385 isl_map_free(map);
2386 return NULL;
2389 /* Eliminate the specified n dimensions starting at first from the
2390 * constraints, without removing the dimensions from the space.
2391 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2393 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2394 enum isl_dim_type type, unsigned first, unsigned n)
2396 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2399 /* Eliminate the specified n dimensions starting at first from the
2400 * constraints, without removing the dimensions from the space.
2401 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2403 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2404 unsigned first, unsigned n)
2406 return isl_set_eliminate(set, isl_dim_set, first, n);
2409 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2410 __isl_take isl_basic_map *bmap)
2412 if (!bmap)
2413 return NULL;
2414 bmap = isl_basic_map_eliminate_vars(bmap,
2415 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2416 if (!bmap)
2417 return NULL;
2418 bmap->n_div = 0;
2419 return isl_basic_map_finalize(bmap);
2422 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2423 __isl_take isl_basic_set *bset)
2425 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2428 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2430 int i;
2432 if (!map)
2433 return NULL;
2434 if (map->n == 0)
2435 return map;
2437 map = isl_map_cow(map);
2438 if (!map)
2439 return NULL;
2441 for (i = 0; i < map->n; ++i) {
2442 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2443 if (!map->p[i])
2444 goto error;
2446 return map;
2447 error:
2448 isl_map_free(map);
2449 return NULL;
2452 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2454 return isl_map_remove_divs(set);
2457 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2458 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2459 unsigned first, unsigned n)
2461 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2462 return isl_basic_map_free(bmap);
2463 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2464 return bmap;
2465 bmap = isl_basic_map_eliminate_vars(bmap,
2466 isl_basic_map_offset(bmap, type) - 1 + first, n);
2467 if (!bmap)
2468 return bmap;
2469 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2470 return bmap;
2471 bmap = isl_basic_map_drop(bmap, type, first, n);
2472 return bmap;
2475 /* Return true if the definition of the given div (recursively) involves
2476 * any of the given variables.
2478 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2479 unsigned first, unsigned n)
2481 int i;
2482 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2484 if (isl_int_is_zero(bmap->div[div][0]))
2485 return isl_bool_false;
2486 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2487 return isl_bool_true;
2489 for (i = bmap->n_div - 1; i >= 0; --i) {
2490 isl_bool involves;
2492 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2493 continue;
2494 involves = div_involves_vars(bmap, i, first, n);
2495 if (involves < 0 || involves)
2496 return involves;
2499 return isl_bool_false;
2502 /* Try and add a lower and/or upper bound on "div" to "bmap"
2503 * based on inequality "i".
2504 * "total" is the total number of variables (excluding the divs).
2505 * "v" is a temporary object that can be used during the calculations.
2506 * If "lb" is set, then a lower bound should be constructed.
2507 * If "ub" is set, then an upper bound should be constructed.
2509 * The calling function has already checked that the inequality does not
2510 * reference "div", but we still need to check that the inequality is
2511 * of the right form. We'll consider the case where we want to construct
2512 * a lower bound. The construction of upper bounds is similar.
2514 * Let "div" be of the form
2516 * q = floor((a + f(x))/d)
2518 * We essentially check if constraint "i" is of the form
2520 * b + f(x) >= 0
2522 * so that we can use it to derive a lower bound on "div".
2523 * However, we allow a slightly more general form
2525 * b + g(x) >= 0
2527 * with the condition that the coefficients of g(x) - f(x) are all
2528 * divisible by d.
2529 * Rewriting this constraint as
2531 * 0 >= -b - g(x)
2533 * adding a + f(x) to both sides and dividing by d, we obtain
2535 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2537 * Taking the floor on both sides, we obtain
2539 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2541 * or
2543 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2545 * In the case of an upper bound, we construct the constraint
2547 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2550 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2551 __isl_take isl_basic_map *bmap, int div, int i,
2552 unsigned total, isl_int v, int lb, int ub)
2554 int j;
2556 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2557 if (lb) {
2558 isl_int_sub(v, bmap->ineq[i][1 + j],
2559 bmap->div[div][1 + 1 + j]);
2560 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2562 if (ub) {
2563 isl_int_add(v, bmap->ineq[i][1 + j],
2564 bmap->div[div][1 + 1 + j]);
2565 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2568 if (!lb && !ub)
2569 return bmap;
2571 bmap = isl_basic_map_cow(bmap);
2572 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2573 if (lb) {
2574 int k = isl_basic_map_alloc_inequality(bmap);
2575 if (k < 0)
2576 goto error;
2577 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2578 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2579 bmap->div[div][1 + j]);
2580 isl_int_cdiv_q(bmap->ineq[k][j],
2581 bmap->ineq[k][j], bmap->div[div][0]);
2583 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2585 if (ub) {
2586 int k = isl_basic_map_alloc_inequality(bmap);
2587 if (k < 0)
2588 goto error;
2589 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2590 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2591 bmap->div[div][1 + j]);
2592 isl_int_fdiv_q(bmap->ineq[k][j],
2593 bmap->ineq[k][j], bmap->div[div][0]);
2595 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2598 return bmap;
2599 error:
2600 isl_basic_map_free(bmap);
2601 return NULL;
2604 /* This function is called right before "div" is eliminated from "bmap"
2605 * using Fourier-Motzkin.
2606 * Look through the constraints of "bmap" for constraints on the argument
2607 * of the integer division and use them to construct constraints on the
2608 * integer division itself. These constraints can then be combined
2609 * during the Fourier-Motzkin elimination.
2610 * Note that it is only useful to introduce lower bounds on "div"
2611 * if "bmap" already contains upper bounds on "div" as the newly
2612 * introduce lower bounds can then be combined with the pre-existing
2613 * upper bounds. Similarly for upper bounds.
2614 * We therefore first check if "bmap" contains any lower and/or upper bounds
2615 * on "div".
2617 * It is interesting to note that the introduction of these constraints
2618 * can indeed lead to more accurate results, even when compared to
2619 * deriving constraints on the argument of "div" from constraints on "div".
2620 * Consider, for example, the set
2622 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2624 * The second constraint can be rewritten as
2626 * 2 * [(-i-2j+3)/4] + k >= 0
2628 * from which we can derive
2630 * -i - 2j + 3 >= -2k
2632 * or
2634 * i + 2j <= 3 + 2k
2636 * Combined with the first constraint, we obtain
2638 * -3 <= 3 + 2k or k >= -3
2640 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2641 * the first constraint, we obtain
2643 * [(i + 2j)/4] >= [-3/4] = -1
2645 * Combining this constraint with the second constraint, we obtain
2647 * k >= -2
2649 static __isl_give isl_basic_map *insert_bounds_on_div(
2650 __isl_take isl_basic_map *bmap, int div)
2652 int i;
2653 int check_lb, check_ub;
2654 isl_int v;
2655 unsigned total;
2657 if (!bmap)
2658 return NULL;
2660 if (isl_int_is_zero(bmap->div[div][0]))
2661 return bmap;
2663 total = isl_space_dim(bmap->dim, isl_dim_all);
2665 check_lb = 0;
2666 check_ub = 0;
2667 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2668 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2669 if (s > 0)
2670 check_ub = 1;
2671 if (s < 0)
2672 check_lb = 1;
2675 if (!check_lb && !check_ub)
2676 return bmap;
2678 isl_int_init(v);
2680 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2681 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2682 continue;
2684 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2685 check_lb, check_ub);
2688 isl_int_clear(v);
2690 return bmap;
2693 /* Remove all divs (recursively) involving any of the given dimensions
2694 * in their definitions.
2696 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2697 __isl_take isl_basic_map *bmap,
2698 enum isl_dim_type type, unsigned first, unsigned n)
2700 int i;
2702 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2703 return isl_basic_map_free(bmap);
2704 first += isl_basic_map_offset(bmap, type);
2706 for (i = bmap->n_div - 1; i >= 0; --i) {
2707 isl_bool involves;
2709 involves = div_involves_vars(bmap, i, first, n);
2710 if (involves < 0)
2711 return isl_basic_map_free(bmap);
2712 if (!involves)
2713 continue;
2714 bmap = insert_bounds_on_div(bmap, i);
2715 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2716 if (!bmap)
2717 return NULL;
2718 i = bmap->n_div;
2721 return bmap;
2724 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2725 __isl_take isl_basic_set *bset,
2726 enum isl_dim_type type, unsigned first, unsigned n)
2728 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2731 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2732 enum isl_dim_type type, unsigned first, unsigned n)
2734 int i;
2736 if (!map)
2737 return NULL;
2738 if (map->n == 0)
2739 return map;
2741 map = isl_map_cow(map);
2742 if (!map)
2743 return NULL;
2745 for (i = 0; i < map->n; ++i) {
2746 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2747 type, first, n);
2748 if (!map->p[i])
2749 goto error;
2751 return map;
2752 error:
2753 isl_map_free(map);
2754 return NULL;
2757 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2758 enum isl_dim_type type, unsigned first, unsigned n)
2760 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2761 type, first, n));
2764 /* Does the description of "bmap" depend on the specified dimensions?
2765 * We also check whether the dimensions appear in any of the div definitions.
2766 * In principle there is no need for this check. If the dimensions appear
2767 * in a div definition, they also appear in the defining constraints of that
2768 * div.
2770 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2771 enum isl_dim_type type, unsigned first, unsigned n)
2773 int i;
2775 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2776 return isl_bool_error;
2778 first += isl_basic_map_offset(bmap, type);
2779 for (i = 0; i < bmap->n_eq; ++i)
2780 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2781 return isl_bool_true;
2782 for (i = 0; i < bmap->n_ineq; ++i)
2783 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2784 return isl_bool_true;
2785 for (i = 0; i < bmap->n_div; ++i) {
2786 if (isl_int_is_zero(bmap->div[i][0]))
2787 continue;
2788 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2789 return isl_bool_true;
2792 return isl_bool_false;
2795 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2796 enum isl_dim_type type, unsigned first, unsigned n)
2798 int i;
2800 if (isl_map_check_range(map, type, first, n) < 0)
2801 return isl_bool_error;
2803 for (i = 0; i < map->n; ++i) {
2804 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2805 type, first, n);
2806 if (involves < 0 || involves)
2807 return involves;
2810 return isl_bool_false;
2813 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2814 enum isl_dim_type type, unsigned first, unsigned n)
2816 return isl_basic_map_involves_dims(bset, type, first, n);
2819 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2820 enum isl_dim_type type, unsigned first, unsigned n)
2822 return isl_map_involves_dims(set, type, first, n);
2825 /* Drop all constraints in bmap that involve any of the dimensions
2826 * first to first+n-1.
2827 * This function only performs the actual removal of constraints.
2829 * This function should not call finalize since it is used by
2830 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2832 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2833 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2835 int i;
2837 if (n == 0)
2838 return bmap;
2840 bmap = isl_basic_map_cow(bmap);
2842 if (!bmap)
2843 return NULL;
2845 for (i = bmap->n_eq - 1; i >= 0; --i) {
2846 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2847 continue;
2848 isl_basic_map_drop_equality(bmap, i);
2851 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2852 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2853 continue;
2854 isl_basic_map_drop_inequality(bmap, i);
2857 return bmap;
2860 /* Drop all constraints in bset that involve any of the dimensions
2861 * first to first+n-1.
2862 * This function only performs the actual removal of constraints.
2864 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2865 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2867 return isl_basic_map_drop_constraints_involving(bset, first, n);
2870 /* Drop all constraints in bmap that do not involve any of the dimensions
2871 * first to first + n - 1 of the given type.
2873 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2874 __isl_take isl_basic_map *bmap,
2875 enum isl_dim_type type, unsigned first, unsigned n)
2877 int i;
2879 if (n == 0) {
2880 isl_space *space = isl_basic_map_get_space(bmap);
2881 isl_basic_map_free(bmap);
2882 return isl_basic_map_universe(space);
2884 bmap = isl_basic_map_cow(bmap);
2885 if (!bmap)
2886 return NULL;
2888 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2889 return isl_basic_map_free(bmap);
2891 first += isl_basic_map_offset(bmap, type) - 1;
2893 for (i = bmap->n_eq - 1; i >= 0; --i) {
2894 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2895 continue;
2896 isl_basic_map_drop_equality(bmap, i);
2899 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2900 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2901 continue;
2902 isl_basic_map_drop_inequality(bmap, i);
2905 bmap = isl_basic_map_add_known_div_constraints(bmap);
2906 return bmap;
2909 /* Drop all constraints in bset that do not involve any of the dimensions
2910 * first to first + n - 1 of the given type.
2912 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2913 __isl_take isl_basic_set *bset,
2914 enum isl_dim_type type, unsigned first, unsigned n)
2916 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2917 type, first, n);
2920 /* Drop all constraints in bmap that involve any of the dimensions
2921 * first to first + n - 1 of the given type.
2923 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2924 __isl_take isl_basic_map *bmap,
2925 enum isl_dim_type type, unsigned first, unsigned n)
2927 if (!bmap)
2928 return NULL;
2929 if (n == 0)
2930 return bmap;
2932 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2933 return isl_basic_map_free(bmap);
2935 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2936 first += isl_basic_map_offset(bmap, type) - 1;
2937 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2938 bmap = isl_basic_map_add_known_div_constraints(bmap);
2939 return bmap;
2942 /* Drop all constraints in bset that involve any of the dimensions
2943 * first to first + n - 1 of the given type.
2945 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2946 __isl_take isl_basic_set *bset,
2947 enum isl_dim_type type, unsigned first, unsigned n)
2949 return isl_basic_map_drop_constraints_involving_dims(bset,
2950 type, first, n);
2953 /* Drop constraints from "map" by applying "drop" to each basic map.
2955 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2956 enum isl_dim_type type, unsigned first, unsigned n,
2957 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2958 enum isl_dim_type type, unsigned first, unsigned n))
2960 int i;
2962 if (isl_map_check_range(map, type, first, n) < 0)
2963 return isl_map_free(map);
2965 map = isl_map_cow(map);
2966 if (!map)
2967 return NULL;
2969 for (i = 0; i < map->n; ++i) {
2970 map->p[i] = drop(map->p[i], type, first, n);
2971 if (!map->p[i])
2972 return isl_map_free(map);
2975 if (map->n > 1)
2976 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2978 return map;
2981 /* Drop all constraints in map that involve any of the dimensions
2982 * first to first + n - 1 of the given type.
2984 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2985 __isl_take isl_map *map,
2986 enum isl_dim_type type, unsigned first, unsigned n)
2988 if (n == 0)
2989 return map;
2990 return drop_constraints(map, type, first, n,
2991 &isl_basic_map_drop_constraints_involving_dims);
2994 /* Drop all constraints in "map" that do not involve any of the dimensions
2995 * first to first + n - 1 of the given type.
2997 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2998 __isl_take isl_map *map,
2999 enum isl_dim_type type, unsigned first, unsigned n)
3001 if (n == 0) {
3002 isl_space *space = isl_map_get_space(map);
3003 isl_map_free(map);
3004 return isl_map_universe(space);
3006 return drop_constraints(map, type, first, n,
3007 &isl_basic_map_drop_constraints_not_involving_dims);
3010 /* Drop all constraints in set that involve any of the dimensions
3011 * first to first + n - 1 of the given type.
3013 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3014 __isl_take isl_set *set,
3015 enum isl_dim_type type, unsigned first, unsigned n)
3017 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3020 /* Drop all constraints in "set" that do not involve any of the dimensions
3021 * first to first + n - 1 of the given type.
3023 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3024 __isl_take isl_set *set,
3025 enum isl_dim_type type, unsigned first, unsigned n)
3027 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3030 /* Does local variable "div" of "bmap" have a complete explicit representation?
3031 * Having a complete explicit representation requires not only
3032 * an explicit representation, but also that all local variables
3033 * that appear in this explicit representation in turn have
3034 * a complete explicit representation.
3036 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3038 int i;
3039 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3040 isl_bool marked;
3042 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3043 if (marked < 0 || marked)
3044 return isl_bool_not(marked);
3046 for (i = bmap->n_div - 1; i >= 0; --i) {
3047 isl_bool known;
3049 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3050 continue;
3051 known = isl_basic_map_div_is_known(bmap, i);
3052 if (known < 0 || !known)
3053 return known;
3056 return isl_bool_true;
3059 /* Remove all divs that are unknown or defined in terms of unknown divs.
3061 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3062 __isl_take isl_basic_map *bmap)
3064 int i;
3066 if (!bmap)
3067 return NULL;
3069 for (i = bmap->n_div - 1; i >= 0; --i) {
3070 if (isl_basic_map_div_is_known(bmap, i))
3071 continue;
3072 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3073 if (!bmap)
3074 return NULL;
3075 i = bmap->n_div;
3078 return bmap;
3081 /* Remove all divs that are unknown or defined in terms of unknown divs.
3083 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3084 __isl_take isl_basic_set *bset)
3086 return isl_basic_map_remove_unknown_divs(bset);
3089 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3091 int i;
3093 if (!map)
3094 return NULL;
3095 if (map->n == 0)
3096 return map;
3098 map = isl_map_cow(map);
3099 if (!map)
3100 return NULL;
3102 for (i = 0; i < map->n; ++i) {
3103 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3104 if (!map->p[i])
3105 goto error;
3107 return map;
3108 error:
3109 isl_map_free(map);
3110 return NULL;
3113 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3115 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3118 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3119 __isl_take isl_basic_set *bset,
3120 enum isl_dim_type type, unsigned first, unsigned n)
3122 isl_basic_map *bmap = bset_to_bmap(bset);
3123 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3124 return bset_from_bmap(bmap);
3127 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3128 enum isl_dim_type type, unsigned first, unsigned n)
3130 int i;
3132 if (n == 0)
3133 return map;
3135 map = isl_map_cow(map);
3136 if (isl_map_check_range(map, type, first, n) < 0)
3137 return isl_map_free(map);
3139 for (i = 0; i < map->n; ++i) {
3140 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3141 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3142 if (!map->p[i])
3143 goto error;
3145 map = isl_map_drop(map, type, first, n);
3146 return map;
3147 error:
3148 isl_map_free(map);
3149 return NULL;
3152 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3153 enum isl_dim_type type, unsigned first, unsigned n)
3155 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3156 type, first, n));
3159 /* Project out n inputs starting at first using Fourier-Motzkin */
3160 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3161 unsigned first, unsigned n)
3163 return isl_map_remove_dims(map, isl_dim_in, first, n);
3166 static void dump_term(struct isl_basic_map *bmap,
3167 isl_int c, int pos, FILE *out)
3169 const char *name;
3170 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3171 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3172 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3173 if (!pos)
3174 isl_int_print(out, c, 0);
3175 else {
3176 if (!isl_int_is_one(c))
3177 isl_int_print(out, c, 0);
3178 if (pos < 1 + nparam) {
3179 name = isl_space_get_dim_name(bmap->dim,
3180 isl_dim_param, pos - 1);
3181 if (name)
3182 fprintf(out, "%s", name);
3183 else
3184 fprintf(out, "p%d", pos - 1);
3185 } else if (pos < 1 + nparam + in)
3186 fprintf(out, "i%d", pos - 1 - nparam);
3187 else if (pos < 1 + nparam + dim)
3188 fprintf(out, "o%d", pos - 1 - nparam - in);
3189 else
3190 fprintf(out, "e%d", pos - 1 - nparam - dim);
3194 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3195 int sign, FILE *out)
3197 int i;
3198 int first;
3199 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3200 isl_int v;
3202 isl_int_init(v);
3203 for (i = 0, first = 1; i < len; ++i) {
3204 if (isl_int_sgn(c[i]) * sign <= 0)
3205 continue;
3206 if (!first)
3207 fprintf(out, " + ");
3208 first = 0;
3209 isl_int_abs(v, c[i]);
3210 dump_term(bmap, v, i, out);
3212 isl_int_clear(v);
3213 if (first)
3214 fprintf(out, "0");
3217 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3218 const char *op, FILE *out, int indent)
3220 int i;
3222 fprintf(out, "%*s", indent, "");
3224 dump_constraint_sign(bmap, c, 1, out);
3225 fprintf(out, " %s ", op);
3226 dump_constraint_sign(bmap, c, -1, out);
3228 fprintf(out, "\n");
3230 for (i = bmap->n_div; i < bmap->extra; ++i) {
3231 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3232 continue;
3233 fprintf(out, "%*s", indent, "");
3234 fprintf(out, "ERROR: unused div coefficient not zero\n");
3235 abort();
3239 static void dump_constraints(struct isl_basic_map *bmap,
3240 isl_int **c, unsigned n,
3241 const char *op, FILE *out, int indent)
3243 int i;
3245 for (i = 0; i < n; ++i)
3246 dump_constraint(bmap, c[i], op, out, indent);
3249 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3251 int j;
3252 int first = 1;
3253 unsigned total = isl_basic_map_total_dim(bmap);
3255 for (j = 0; j < 1 + total; ++j) {
3256 if (isl_int_is_zero(exp[j]))
3257 continue;
3258 if (!first && isl_int_is_pos(exp[j]))
3259 fprintf(out, "+");
3260 dump_term(bmap, exp[j], j, out);
3261 first = 0;
3265 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3267 int i;
3269 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3270 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3272 for (i = 0; i < bmap->n_div; ++i) {
3273 fprintf(out, "%*s", indent, "");
3274 fprintf(out, "e%d = [(", i);
3275 dump_affine(bmap, bmap->div[i]+1, out);
3276 fprintf(out, ")/");
3277 isl_int_print(out, bmap->div[i][0], 0);
3278 fprintf(out, "]\n");
3282 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3283 FILE *out, int indent)
3285 if (!bset) {
3286 fprintf(out, "null basic set\n");
3287 return;
3290 fprintf(out, "%*s", indent, "");
3291 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3292 bset->ref, bset->dim->nparam, bset->dim->n_out,
3293 bset->extra, bset->flags);
3294 dump(bset_to_bmap(bset), out, indent);
3297 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3298 FILE *out, int indent)
3300 if (!bmap) {
3301 fprintf(out, "null basic map\n");
3302 return;
3305 fprintf(out, "%*s", indent, "");
3306 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3307 "flags: %x, n_name: %d\n",
3308 bmap->ref,
3309 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3310 bmap->extra, bmap->flags, bmap->dim->n_id);
3311 dump(bmap, out, indent);
3314 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3316 unsigned total;
3317 if (!bmap)
3318 return -1;
3319 total = isl_basic_map_total_dim(bmap);
3320 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3321 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3322 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3323 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3324 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3325 return 0;
3328 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3329 unsigned flags)
3331 if (isl_space_check_is_set(space) < 0)
3332 goto error;
3333 return isl_map_alloc_space(space, n, flags);
3334 error:
3335 isl_space_free(space);
3336 return NULL;
3339 /* Make sure "map" has room for at least "n" more basic maps.
3341 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3343 int i;
3344 struct isl_map *grown = NULL;
3346 if (!map)
3347 return NULL;
3348 isl_assert(map->ctx, n >= 0, goto error);
3349 if (map->n + n <= map->size)
3350 return map;
3351 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3352 if (!grown)
3353 goto error;
3354 for (i = 0; i < map->n; ++i) {
3355 grown->p[i] = isl_basic_map_copy(map->p[i]);
3356 if (!grown->p[i])
3357 goto error;
3358 grown->n++;
3360 isl_map_free(map);
3361 return grown;
3362 error:
3363 isl_map_free(grown);
3364 isl_map_free(map);
3365 return NULL;
3368 /* Make sure "set" has room for at least "n" more basic sets.
3370 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3372 return set_from_map(isl_map_grow(set_to_map(set), n));
3375 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3377 return isl_map_from_basic_map(bset);
3380 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3382 struct isl_map *map;
3384 if (!bmap)
3385 return NULL;
3387 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3388 return isl_map_add_basic_map(map, bmap);
3391 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3392 __isl_take isl_basic_set *bset)
3394 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3395 bset_to_bmap(bset)));
3398 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3400 return isl_map_free(set);
3403 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3405 int i;
3407 if (!set) {
3408 fprintf(out, "null set\n");
3409 return;
3412 fprintf(out, "%*s", indent, "");
3413 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3414 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3415 set->flags);
3416 for (i = 0; i < set->n; ++i) {
3417 fprintf(out, "%*s", indent, "");
3418 fprintf(out, "basic set %d:\n", i);
3419 isl_basic_set_print_internal(set->p[i], out, indent+4);
3423 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3425 int i;
3427 if (!map) {
3428 fprintf(out, "null map\n");
3429 return;
3432 fprintf(out, "%*s", indent, "");
3433 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3434 "flags: %x, n_name: %d\n",
3435 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3436 map->dim->n_out, map->flags, map->dim->n_id);
3437 for (i = 0; i < map->n; ++i) {
3438 fprintf(out, "%*s", indent, "");
3439 fprintf(out, "basic map %d:\n", i);
3440 isl_basic_map_print_internal(map->p[i], out, indent+4);
3444 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3445 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3447 struct isl_basic_map *bmap_domain;
3449 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3450 goto error;
3452 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3453 isl_assert(bset->ctx,
3454 isl_basic_map_compatible_domain(bmap, bset), goto error);
3456 bmap = isl_basic_map_cow(bmap);
3457 if (!bmap)
3458 goto error;
3459 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3460 bset->n_div, bset->n_eq, bset->n_ineq);
3461 bmap_domain = isl_basic_map_from_domain(bset);
3462 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3464 bmap = isl_basic_map_simplify(bmap);
3465 return isl_basic_map_finalize(bmap);
3466 error:
3467 isl_basic_map_free(bmap);
3468 isl_basic_set_free(bset);
3469 return NULL;
3472 /* Check that the space of "bset" is the same as that of the range of "bmap".
3474 static isl_stat isl_basic_map_check_compatible_range(
3475 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3477 isl_bool ok;
3479 ok = isl_basic_map_compatible_range(bmap, bset);
3480 if (ok < 0)
3481 return isl_stat_error;
3482 if (!ok)
3483 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3484 "incompatible spaces", return isl_stat_error);
3486 return isl_stat_ok;
3489 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3490 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3492 struct isl_basic_map *bmap_range;
3494 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3495 goto error;
3497 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3498 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3499 goto error;
3501 if (isl_basic_set_plain_is_universe(bset)) {
3502 isl_basic_set_free(bset);
3503 return bmap;
3506 bmap = isl_basic_map_cow(bmap);
3507 if (!bmap)
3508 goto error;
3509 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3510 bset->n_div, bset->n_eq, bset->n_ineq);
3511 bmap_range = bset_to_bmap(bset);
3512 bmap = add_constraints(bmap, bmap_range, 0, 0);
3514 bmap = isl_basic_map_simplify(bmap);
3515 return isl_basic_map_finalize(bmap);
3516 error:
3517 isl_basic_map_free(bmap);
3518 isl_basic_set_free(bset);
3519 return NULL;
3522 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3523 __isl_keep isl_vec *vec)
3525 int i;
3526 unsigned total;
3527 isl_int s;
3529 if (!bmap || !vec)
3530 return isl_bool_error;
3532 total = 1 + isl_basic_map_total_dim(bmap);
3533 if (total != vec->size)
3534 return isl_bool_false;
3536 isl_int_init(s);
3538 for (i = 0; i < bmap->n_eq; ++i) {
3539 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3540 if (!isl_int_is_zero(s)) {
3541 isl_int_clear(s);
3542 return isl_bool_false;
3546 for (i = 0; i < bmap->n_ineq; ++i) {
3547 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3548 if (isl_int_is_neg(s)) {
3549 isl_int_clear(s);
3550 return isl_bool_false;
3554 isl_int_clear(s);
3556 return isl_bool_true;
3559 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3560 __isl_keep isl_vec *vec)
3562 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3565 __isl_give isl_basic_map *isl_basic_map_intersect(
3566 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3568 struct isl_vec *sample = NULL;
3570 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3571 goto error;
3572 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3573 isl_space_dim(bmap1->dim, isl_dim_param) &&
3574 isl_space_dim(bmap2->dim, isl_dim_all) !=
3575 isl_space_dim(bmap2->dim, isl_dim_param))
3576 return isl_basic_map_intersect(bmap2, bmap1);
3578 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3579 isl_space_dim(bmap2->dim, isl_dim_param))
3580 isl_assert(bmap1->ctx,
3581 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3583 if (isl_basic_map_plain_is_empty(bmap1)) {
3584 isl_basic_map_free(bmap2);
3585 return bmap1;
3587 if (isl_basic_map_plain_is_empty(bmap2)) {
3588 isl_basic_map_free(bmap1);
3589 return bmap2;
3592 if (bmap1->sample &&
3593 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3594 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3595 sample = isl_vec_copy(bmap1->sample);
3596 else if (bmap2->sample &&
3597 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3598 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3599 sample = isl_vec_copy(bmap2->sample);
3601 bmap1 = isl_basic_map_cow(bmap1);
3602 if (!bmap1)
3603 goto error;
3604 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3605 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3606 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3608 if (!bmap1)
3609 isl_vec_free(sample);
3610 else if (sample) {
3611 isl_vec_free(bmap1->sample);
3612 bmap1->sample = sample;
3615 bmap1 = isl_basic_map_simplify(bmap1);
3616 return isl_basic_map_finalize(bmap1);
3617 error:
3618 if (sample)
3619 isl_vec_free(sample);
3620 isl_basic_map_free(bmap1);
3621 isl_basic_map_free(bmap2);
3622 return NULL;
3625 struct isl_basic_set *isl_basic_set_intersect(
3626 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3628 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3629 bset_to_bmap(bset2)));
3632 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3633 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3635 return isl_basic_set_intersect(bset1, bset2);
3638 /* Special case of isl_map_intersect, where both map1 and map2
3639 * are convex, without any divs and such that either map1 or map2
3640 * contains a single constraint. This constraint is then simply
3641 * added to the other map.
3643 static __isl_give isl_map *map_intersect_add_constraint(
3644 __isl_take isl_map *map1, __isl_take isl_map *map2)
3646 isl_assert(map1->ctx, map1->n == 1, goto error);
3647 isl_assert(map2->ctx, map1->n == 1, goto error);
3648 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3649 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3651 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3652 return isl_map_intersect(map2, map1);
3654 map1 = isl_map_cow(map1);
3655 if (!map1)
3656 goto error;
3657 if (isl_map_plain_is_empty(map1)) {
3658 isl_map_free(map2);
3659 return map1;
3661 if (map2->p[0]->n_eq == 1)
3662 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3663 else
3664 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3665 map2->p[0]->ineq[0]);
3667 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3668 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3669 if (!map1->p[0])
3670 goto error;
3672 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3673 isl_basic_map_free(map1->p[0]);
3674 map1->n = 0;
3677 isl_map_free(map2);
3679 map1 = isl_map_unmark_normalized(map1);
3680 return map1;
3681 error:
3682 isl_map_free(map1);
3683 isl_map_free(map2);
3684 return NULL;
3687 /* map2 may be either a parameter domain or a map living in the same
3688 * space as map1.
3690 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3691 __isl_take isl_map *map2)
3693 unsigned flags = 0;
3694 isl_bool equal;
3695 isl_map *result;
3696 int i, j;
3698 if (!map1 || !map2)
3699 goto error;
3701 if ((isl_map_plain_is_empty(map1) ||
3702 isl_map_plain_is_universe(map2)) &&
3703 isl_space_is_equal(map1->dim, map2->dim)) {
3704 isl_map_free(map2);
3705 return map1;
3707 if ((isl_map_plain_is_empty(map2) ||
3708 isl_map_plain_is_universe(map1)) &&
3709 isl_space_is_equal(map1->dim, map2->dim)) {
3710 isl_map_free(map1);
3711 return map2;
3714 if (map1->n == 1 && map2->n == 1 &&
3715 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3716 isl_space_is_equal(map1->dim, map2->dim) &&
3717 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3718 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3719 return map_intersect_add_constraint(map1, map2);
3721 equal = isl_map_plain_is_equal(map1, map2);
3722 if (equal < 0)
3723 goto error;
3724 if (equal) {
3725 isl_map_free(map2);
3726 return map1;
3729 if (isl_space_dim(map2->dim, isl_dim_all) !=
3730 isl_space_dim(map2->dim, isl_dim_param))
3731 isl_assert(map1->ctx,
3732 isl_space_is_equal(map1->dim, map2->dim), goto error);
3734 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3735 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3736 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3738 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3739 map1->n * map2->n, flags);
3740 if (!result)
3741 goto error;
3742 for (i = 0; i < map1->n; ++i)
3743 for (j = 0; j < map2->n; ++j) {
3744 struct isl_basic_map *part;
3745 part = isl_basic_map_intersect(
3746 isl_basic_map_copy(map1->p[i]),
3747 isl_basic_map_copy(map2->p[j]));
3748 if (isl_basic_map_is_empty(part) < 0)
3749 part = isl_basic_map_free(part);
3750 result = isl_map_add_basic_map(result, part);
3751 if (!result)
3752 goto error;
3754 isl_map_free(map1);
3755 isl_map_free(map2);
3756 return result;
3757 error:
3758 isl_map_free(map1);
3759 isl_map_free(map2);
3760 return NULL;
3763 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3764 __isl_take isl_map *map2)
3766 if (!map1 || !map2)
3767 goto error;
3768 if (!isl_space_is_equal(map1->dim, map2->dim))
3769 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3770 "spaces don't match", goto error);
3771 return map_intersect_internal(map1, map2);
3772 error:
3773 isl_map_free(map1);
3774 isl_map_free(map2);
3775 return NULL;
3778 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3779 __isl_take isl_map *map2)
3781 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3784 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3786 return set_from_map(isl_map_intersect(set_to_map(set1),
3787 set_to_map(set2)));
3790 /* map_intersect_internal accepts intersections
3791 * with parameter domains, so we can just call that function.
3793 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3794 __isl_take isl_set *params)
3796 return map_intersect_internal(map, params);
3799 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3800 __isl_take isl_map *map2)
3802 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3805 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3806 __isl_take isl_set *params)
3808 return isl_map_intersect_params(set, params);
3811 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3813 isl_space *space;
3814 unsigned pos, n1, n2;
3816 if (!bmap)
3817 return NULL;
3818 bmap = isl_basic_map_cow(bmap);
3819 if (!bmap)
3820 return NULL;
3821 space = isl_space_reverse(isl_space_copy(bmap->dim));
3822 pos = isl_basic_map_offset(bmap, isl_dim_in);
3823 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3824 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3825 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3826 return isl_basic_map_reset_space(bmap, space);
3829 static __isl_give isl_basic_map *basic_map_space_reset(
3830 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3832 isl_space *space;
3834 if (!bmap)
3835 return NULL;
3836 if (!isl_space_is_named_or_nested(bmap->dim, type))
3837 return bmap;
3839 space = isl_basic_map_get_space(bmap);
3840 space = isl_space_reset(space, type);
3841 bmap = isl_basic_map_reset_space(bmap, space);
3842 return bmap;
3845 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3846 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3847 unsigned pos, unsigned n)
3849 isl_bool rational;
3850 isl_space *res_space;
3851 struct isl_basic_map *res;
3852 struct isl_dim_map *dim_map;
3853 unsigned total, off;
3854 enum isl_dim_type t;
3856 if (n == 0)
3857 return basic_map_space_reset(bmap, type);
3859 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3860 type, pos, n);
3861 if (!res_space)
3862 return isl_basic_map_free(bmap);
3864 total = isl_basic_map_total_dim(bmap) + n;
3865 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3866 off = 0;
3867 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3868 if (t != type) {
3869 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3870 } else {
3871 unsigned size = isl_basic_map_dim(bmap, t);
3872 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3873 0, pos, off);
3874 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3875 pos, size - pos, off + pos + n);
3877 off += isl_space_dim(res_space, t);
3879 isl_dim_map_div(dim_map, bmap, off);
3881 res = isl_basic_map_alloc_space(res_space,
3882 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3883 rational = isl_basic_map_is_rational(bmap);
3884 if (rational < 0)
3885 res = isl_basic_map_free(res);
3886 if (rational)
3887 res = isl_basic_map_set_rational(res);
3888 if (isl_basic_map_plain_is_empty(bmap)) {
3889 isl_basic_map_free(bmap);
3890 free(dim_map);
3891 return isl_basic_map_set_to_empty(res);
3893 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3894 return isl_basic_map_finalize(res);
3897 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3898 __isl_take isl_basic_set *bset,
3899 enum isl_dim_type type, unsigned pos, unsigned n)
3901 return isl_basic_map_insert_dims(bset, type, pos, n);
3904 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3905 enum isl_dim_type type, unsigned n)
3907 if (!bmap)
3908 return NULL;
3909 return isl_basic_map_insert_dims(bmap, type,
3910 isl_basic_map_dim(bmap, type), n);
3913 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3914 enum isl_dim_type type, unsigned n)
3916 if (!bset)
3917 return NULL;
3918 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3919 return isl_basic_map_add_dims(bset, type, n);
3920 error:
3921 isl_basic_set_free(bset);
3922 return NULL;
3925 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3926 enum isl_dim_type type)
3928 isl_space *space;
3930 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3931 return map;
3933 space = isl_map_get_space(map);
3934 space = isl_space_reset(space, type);
3935 map = isl_map_reset_space(map, space);
3936 return map;
3939 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3940 enum isl_dim_type type, unsigned pos, unsigned n)
3942 int i;
3944 if (n == 0)
3945 return map_space_reset(map, type);
3947 map = isl_map_cow(map);
3948 if (!map)
3949 return NULL;
3951 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3952 if (!map->dim)
3953 goto error;
3955 for (i = 0; i < map->n; ++i) {
3956 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3957 if (!map->p[i])
3958 goto error;
3961 return map;
3962 error:
3963 isl_map_free(map);
3964 return NULL;
3967 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3968 enum isl_dim_type type, unsigned pos, unsigned n)
3970 return isl_map_insert_dims(set, type, pos, n);
3973 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3974 enum isl_dim_type type, unsigned n)
3976 if (!map)
3977 return NULL;
3978 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3981 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3982 enum isl_dim_type type, unsigned n)
3984 if (!set)
3985 return NULL;
3986 isl_assert(set->ctx, type != isl_dim_in, goto error);
3987 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3988 error:
3989 isl_set_free(set);
3990 return NULL;
3993 __isl_give isl_basic_map *isl_basic_map_move_dims(
3994 __isl_take isl_basic_map *bmap,
3995 enum isl_dim_type dst_type, unsigned dst_pos,
3996 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3998 struct isl_dim_map *dim_map;
3999 struct isl_basic_map *res;
4000 enum isl_dim_type t;
4001 unsigned total, off;
4003 if (!bmap)
4004 return NULL;
4005 if (n == 0) {
4006 bmap = isl_basic_map_reset(bmap, src_type);
4007 bmap = isl_basic_map_reset(bmap, dst_type);
4008 return bmap;
4011 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4012 return isl_basic_map_free(bmap);
4014 if (dst_type == src_type && dst_pos == src_pos)
4015 return bmap;
4017 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4019 if (pos(bmap->dim, dst_type) + dst_pos ==
4020 pos(bmap->dim, src_type) + src_pos +
4021 ((src_type < dst_type) ? n : 0)) {
4022 bmap = isl_basic_map_cow(bmap);
4023 if (!bmap)
4024 return NULL;
4026 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4027 src_type, src_pos, n);
4028 if (!bmap->dim)
4029 goto error;
4031 bmap = isl_basic_map_finalize(bmap);
4033 return bmap;
4036 total = isl_basic_map_total_dim(bmap);
4037 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4039 off = 0;
4040 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4041 unsigned size = isl_space_dim(bmap->dim, t);
4042 if (t == dst_type) {
4043 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4044 0, dst_pos, off);
4045 off += dst_pos;
4046 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4047 src_pos, n, off);
4048 off += n;
4049 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4050 dst_pos, size - dst_pos, off);
4051 off += size - dst_pos;
4052 } else if (t == src_type) {
4053 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4054 0, src_pos, off);
4055 off += src_pos;
4056 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4057 src_pos + n, size - src_pos - n, off);
4058 off += size - src_pos - n;
4059 } else {
4060 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4061 off += size;
4064 isl_dim_map_div(dim_map, bmap, off);
4066 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4067 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4068 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4069 if (!bmap)
4070 goto error;
4072 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4073 src_type, src_pos, n);
4074 if (!bmap->dim)
4075 goto error;
4077 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4078 bmap = isl_basic_map_gauss(bmap, NULL);
4079 bmap = isl_basic_map_finalize(bmap);
4081 return bmap;
4082 error:
4083 isl_basic_map_free(bmap);
4084 return NULL;
4087 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4088 enum isl_dim_type dst_type, unsigned dst_pos,
4089 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4091 isl_basic_map *bmap = bset_to_bmap(bset);
4092 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4093 src_type, src_pos, n);
4094 return bset_from_bmap(bmap);
4097 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4098 enum isl_dim_type dst_type, unsigned dst_pos,
4099 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4101 if (!set)
4102 return NULL;
4103 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4104 return set_from_map(isl_map_move_dims(set_to_map(set),
4105 dst_type, dst_pos, src_type, src_pos, n));
4106 error:
4107 isl_set_free(set);
4108 return NULL;
4111 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4112 enum isl_dim_type dst_type, unsigned dst_pos,
4113 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4115 int i;
4117 if (n == 0) {
4118 map = isl_map_reset(map, src_type);
4119 map = isl_map_reset(map, dst_type);
4120 return map;
4123 if (isl_map_check_range(map, src_type, src_pos, n))
4124 return isl_map_free(map);
4126 if (dst_type == src_type && dst_pos == src_pos)
4127 return map;
4129 isl_assert(map->ctx, dst_type != src_type, goto error);
4131 map = isl_map_cow(map);
4132 if (!map)
4133 return NULL;
4135 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4136 if (!map->dim)
4137 goto error;
4139 for (i = 0; i < map->n; ++i) {
4140 map->p[i] = isl_basic_map_move_dims(map->p[i],
4141 dst_type, dst_pos,
4142 src_type, src_pos, n);
4143 if (!map->p[i])
4144 goto error;
4147 return map;
4148 error:
4149 isl_map_free(map);
4150 return NULL;
4153 /* Move the specified dimensions to the last columns right before
4154 * the divs. Don't change the dimension specification of bmap.
4155 * That's the responsibility of the caller.
4157 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4158 enum isl_dim_type type, unsigned first, unsigned n)
4160 struct isl_dim_map *dim_map;
4161 struct isl_basic_map *res;
4162 enum isl_dim_type t;
4163 unsigned total, off;
4165 if (!bmap)
4166 return NULL;
4167 if (pos(bmap->dim, type) + first + n ==
4168 1 + isl_space_dim(bmap->dim, isl_dim_all))
4169 return bmap;
4171 total = isl_basic_map_total_dim(bmap);
4172 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4174 off = 0;
4175 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4176 unsigned size = isl_space_dim(bmap->dim, t);
4177 if (t == type) {
4178 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4179 0, first, off);
4180 off += first;
4181 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4182 first, n, total - bmap->n_div - n);
4183 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4184 first + n, size - (first + n), off);
4185 off += size - (first + n);
4186 } else {
4187 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4188 off += size;
4191 isl_dim_map_div(dim_map, bmap, off + n);
4193 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4194 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4195 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4196 return res;
4199 /* Insert "n" rows in the divs of "bmap".
4201 * The number of columns is not changed, which means that the last
4202 * dimensions of "bmap" are being reintepreted as the new divs.
4203 * The space of "bmap" is not adjusted, however, which means
4204 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4205 * from the space of "bmap" is the responsibility of the caller.
4207 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4208 int n)
4210 int i;
4211 size_t row_size;
4212 isl_int **new_div;
4213 isl_int *old;
4215 bmap = isl_basic_map_cow(bmap);
4216 if (!bmap)
4217 return NULL;
4219 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4220 old = bmap->block2.data;
4221 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4222 (bmap->extra + n) * (1 + row_size));
4223 if (!bmap->block2.data)
4224 return isl_basic_map_free(bmap);
4225 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4226 if (!new_div)
4227 return isl_basic_map_free(bmap);
4228 for (i = 0; i < n; ++i) {
4229 new_div[i] = bmap->block2.data +
4230 (bmap->extra + i) * (1 + row_size);
4231 isl_seq_clr(new_div[i], 1 + row_size);
4233 for (i = 0; i < bmap->extra; ++i)
4234 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4235 free(bmap->div);
4236 bmap->div = new_div;
4237 bmap->n_div += n;
4238 bmap->extra += n;
4240 return bmap;
4243 /* Drop constraints from "bmap" that only involve the variables
4244 * of "type" in the range [first, first + n] that are not related
4245 * to any of the variables outside that interval.
4246 * These constraints cannot influence the values for the variables
4247 * outside the interval, except in case they cause "bmap" to be empty.
4248 * Only drop the constraints if "bmap" is known to be non-empty.
4250 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4251 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4252 unsigned first, unsigned n)
4254 int i;
4255 int *groups;
4256 unsigned dim, n_div;
4257 isl_bool non_empty;
4259 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4260 if (non_empty < 0)
4261 return isl_basic_map_free(bmap);
4262 if (!non_empty)
4263 return bmap;
4265 dim = isl_basic_map_dim(bmap, isl_dim_all);
4266 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4267 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4268 if (!groups)
4269 return isl_basic_map_free(bmap);
4270 first += isl_basic_map_offset(bmap, type) - 1;
4271 for (i = 0; i < first; ++i)
4272 groups[i] = -1;
4273 for (i = first + n; i < dim - n_div; ++i)
4274 groups[i] = -1;
4276 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4278 return bmap;
4281 /* Turn the n dimensions of type type, starting at first
4282 * into existentially quantified variables.
4284 * If a subset of the projected out variables are unrelated
4285 * to any of the variables that remain, then the constraints
4286 * involving this subset are simply dropped first.
4288 __isl_give isl_basic_map *isl_basic_map_project_out(
4289 __isl_take isl_basic_map *bmap,
4290 enum isl_dim_type type, unsigned first, unsigned n)
4292 isl_bool empty;
4294 if (n == 0)
4295 return basic_map_space_reset(bmap, type);
4296 if (type == isl_dim_div)
4297 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4298 "cannot project out existentially quantified variables",
4299 return isl_basic_map_free(bmap));
4301 empty = isl_basic_map_plain_is_empty(bmap);
4302 if (empty < 0)
4303 return isl_basic_map_free(bmap);
4304 if (empty)
4305 bmap = isl_basic_map_set_to_empty(bmap);
4307 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4308 if (!bmap)
4309 return NULL;
4311 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4312 return isl_basic_map_remove_dims(bmap, type, first, n);
4314 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4315 return isl_basic_map_free(bmap);
4317 bmap = move_last(bmap, type, first, n);
4318 bmap = isl_basic_map_cow(bmap);
4319 bmap = insert_div_rows(bmap, n);
4320 if (!bmap)
4321 return NULL;
4323 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4324 if (!bmap->dim)
4325 goto error;
4326 bmap = isl_basic_map_simplify(bmap);
4327 bmap = isl_basic_map_drop_redundant_divs(bmap);
4328 return isl_basic_map_finalize(bmap);
4329 error:
4330 isl_basic_map_free(bmap);
4331 return NULL;
4334 /* Turn the n dimensions of type type, starting at first
4335 * into existentially quantified variables.
4337 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4338 enum isl_dim_type type, unsigned first, unsigned n)
4340 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4341 type, first, n));
4344 /* Turn the n dimensions of type type, starting at first
4345 * into existentially quantified variables.
4347 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4348 enum isl_dim_type type, unsigned first, unsigned n)
4350 int i;
4352 if (n == 0)
4353 return map_space_reset(map, type);
4355 if (isl_map_check_range(map, type, first, n) < 0)
4356 return isl_map_free(map);
4358 map = isl_map_cow(map);
4359 if (!map)
4360 return NULL;
4362 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4363 if (!map->dim)
4364 goto error;
4366 for (i = 0; i < map->n; ++i) {
4367 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4368 if (!map->p[i])
4369 goto error;
4372 return map;
4373 error:
4374 isl_map_free(map);
4375 return NULL;
4378 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4379 * into existentially quantified variables.
4381 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4382 enum isl_dim_type type, unsigned first, unsigned n)
4384 unsigned dim;
4386 if (isl_map_check_range(map, type, first, n) < 0)
4387 return isl_map_free(map);
4388 dim = isl_map_dim(map, type);
4389 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4390 map = isl_map_project_out(map, type, 0, first);
4391 return map;
4394 /* Turn the n dimensions of type type, starting at first
4395 * into existentially quantified variables.
4397 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4398 enum isl_dim_type type, unsigned first, unsigned n)
4400 return set_from_map(isl_map_project_out(set_to_map(set),
4401 type, first, n));
4404 /* Return a map that projects the elements in "set" onto their
4405 * "n" set dimensions starting at "first".
4406 * "type" should be equal to isl_dim_set.
4408 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4409 enum isl_dim_type type, unsigned first, unsigned n)
4411 int i;
4412 isl_map *map;
4414 if (type != isl_dim_set)
4415 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4416 "only set dimensions can be projected out", goto error);
4417 if (isl_set_check_range(set, type, first, n) < 0)
4418 return isl_set_free(set);
4420 map = isl_map_from_domain(set);
4421 map = isl_map_add_dims(map, isl_dim_out, n);
4422 for (i = 0; i < n; ++i)
4423 map = isl_map_equate(map, isl_dim_in, first + i,
4424 isl_dim_out, i);
4425 return map;
4426 error:
4427 isl_set_free(set);
4428 return NULL;
4431 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4432 unsigned n)
4434 int i, j;
4436 for (i = 0; i < n; ++i) {
4437 j = isl_basic_map_alloc_div(bmap);
4438 if (j < 0)
4439 goto error;
4440 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4442 return bmap;
4443 error:
4444 isl_basic_map_free(bmap);
4445 return NULL;
4448 struct isl_basic_map *isl_basic_map_apply_range(
4449 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4451 isl_space *space_result = NULL;
4452 struct isl_basic_map *bmap;
4453 unsigned n_in, n_out, n, nparam, total, pos;
4454 struct isl_dim_map *dim_map1, *dim_map2;
4456 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4457 goto error;
4458 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4459 bmap2->dim, isl_dim_in))
4460 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4461 "spaces don't match", goto error);
4463 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4464 isl_space_copy(bmap2->dim));
4466 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4467 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4468 n = isl_basic_map_dim(bmap1, isl_dim_out);
4469 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4471 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4472 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4473 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4474 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4475 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4476 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4477 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4478 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4479 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4480 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4481 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4483 bmap = isl_basic_map_alloc_space(space_result,
4484 bmap1->n_div + bmap2->n_div + n,
4485 bmap1->n_eq + bmap2->n_eq,
4486 bmap1->n_ineq + bmap2->n_ineq);
4487 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4488 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4489 bmap = add_divs(bmap, n);
4490 bmap = isl_basic_map_simplify(bmap);
4491 bmap = isl_basic_map_drop_redundant_divs(bmap);
4492 return isl_basic_map_finalize(bmap);
4493 error:
4494 isl_basic_map_free(bmap1);
4495 isl_basic_map_free(bmap2);
4496 return NULL;
4499 struct isl_basic_set *isl_basic_set_apply(
4500 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4502 if (!bset || !bmap)
4503 goto error;
4505 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4506 goto error);
4508 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4509 bmap));
4510 error:
4511 isl_basic_set_free(bset);
4512 isl_basic_map_free(bmap);
4513 return NULL;
4516 struct isl_basic_map *isl_basic_map_apply_domain(
4517 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4519 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4520 goto error;
4521 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4522 bmap2->dim, isl_dim_in))
4523 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4524 "spaces don't match", goto error);
4526 bmap1 = isl_basic_map_reverse(bmap1);
4527 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4528 return isl_basic_map_reverse(bmap1);
4529 error:
4530 isl_basic_map_free(bmap1);
4531 isl_basic_map_free(bmap2);
4532 return NULL;
4535 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4536 * A \cap B -> f(A) + f(B)
4538 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4539 __isl_take isl_basic_map *bmap2)
4541 unsigned n_in, n_out, nparam, total, pos;
4542 struct isl_basic_map *bmap = NULL;
4543 struct isl_dim_map *dim_map1, *dim_map2;
4544 int i;
4546 if (!bmap1 || !bmap2)
4547 goto error;
4549 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4550 goto error);
4552 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4553 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4554 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4556 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4557 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4558 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4559 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4560 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4561 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4562 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4563 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4564 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4565 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4566 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4568 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4569 bmap1->n_div + bmap2->n_div + 2 * n_out,
4570 bmap1->n_eq + bmap2->n_eq + n_out,
4571 bmap1->n_ineq + bmap2->n_ineq);
4572 for (i = 0; i < n_out; ++i) {
4573 int j = isl_basic_map_alloc_equality(bmap);
4574 if (j < 0)
4575 goto error;
4576 isl_seq_clr(bmap->eq[j], 1+total);
4577 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4578 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4579 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4581 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4582 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4583 bmap = add_divs(bmap, 2 * n_out);
4585 bmap = isl_basic_map_simplify(bmap);
4586 return isl_basic_map_finalize(bmap);
4587 error:
4588 isl_basic_map_free(bmap);
4589 isl_basic_map_free(bmap1);
4590 isl_basic_map_free(bmap2);
4591 return NULL;
4594 /* Given two maps A -> f(A) and B -> g(B), construct a map
4595 * A \cap B -> f(A) + f(B)
4597 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4598 __isl_take isl_map *map2)
4600 struct isl_map *result;
4601 int i, j;
4603 if (!map1 || !map2)
4604 goto error;
4606 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4608 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4609 map1->n * map2->n, 0);
4610 if (!result)
4611 goto error;
4612 for (i = 0; i < map1->n; ++i)
4613 for (j = 0; j < map2->n; ++j) {
4614 struct isl_basic_map *part;
4615 part = isl_basic_map_sum(
4616 isl_basic_map_copy(map1->p[i]),
4617 isl_basic_map_copy(map2->p[j]));
4618 if (isl_basic_map_is_empty(part))
4619 isl_basic_map_free(part);
4620 else
4621 result = isl_map_add_basic_map(result, part);
4622 if (!result)
4623 goto error;
4625 isl_map_free(map1);
4626 isl_map_free(map2);
4627 return result;
4628 error:
4629 isl_map_free(map1);
4630 isl_map_free(map2);
4631 return NULL;
4634 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4635 __isl_take isl_set *set2)
4637 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4640 /* Given a basic map A -> f(A), construct A -> -f(A).
4642 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4644 int i, j;
4645 unsigned off, n;
4647 bmap = isl_basic_map_cow(bmap);
4648 if (!bmap)
4649 return NULL;
4651 n = isl_basic_map_dim(bmap, isl_dim_out);
4652 off = isl_basic_map_offset(bmap, isl_dim_out);
4653 for (i = 0; i < bmap->n_eq; ++i)
4654 for (j = 0; j < n; ++j)
4655 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4656 for (i = 0; i < bmap->n_ineq; ++i)
4657 for (j = 0; j < n; ++j)
4658 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4659 for (i = 0; i < bmap->n_div; ++i)
4660 for (j = 0; j < n; ++j)
4661 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4662 bmap = isl_basic_map_gauss(bmap, NULL);
4663 return isl_basic_map_finalize(bmap);
4666 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4668 return isl_basic_map_neg(bset);
4671 /* Given a map A -> f(A), construct A -> -f(A).
4673 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4675 int i;
4677 map = isl_map_cow(map);
4678 if (!map)
4679 return NULL;
4681 for (i = 0; i < map->n; ++i) {
4682 map->p[i] = isl_basic_map_neg(map->p[i]);
4683 if (!map->p[i])
4684 goto error;
4687 return map;
4688 error:
4689 isl_map_free(map);
4690 return NULL;
4693 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4695 return set_from_map(isl_map_neg(set_to_map(set)));
4698 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4699 * A -> floor(f(A)/d).
4701 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4702 isl_int d)
4704 unsigned n_in, n_out, nparam, total, pos;
4705 struct isl_basic_map *result = NULL;
4706 struct isl_dim_map *dim_map;
4707 int i;
4709 if (!bmap)
4710 return NULL;
4712 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4713 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4714 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4716 total = nparam + n_in + n_out + bmap->n_div + n_out;
4717 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4718 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4719 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4720 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4721 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4723 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4724 bmap->n_div + n_out,
4725 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4726 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4727 result = add_divs(result, n_out);
4728 for (i = 0; i < n_out; ++i) {
4729 int j;
4730 j = isl_basic_map_alloc_inequality(result);
4731 if (j < 0)
4732 goto error;
4733 isl_seq_clr(result->ineq[j], 1+total);
4734 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4735 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4736 j = isl_basic_map_alloc_inequality(result);
4737 if (j < 0)
4738 goto error;
4739 isl_seq_clr(result->ineq[j], 1+total);
4740 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4741 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4742 isl_int_sub_ui(result->ineq[j][0], d, 1);
4745 result = isl_basic_map_simplify(result);
4746 return isl_basic_map_finalize(result);
4747 error:
4748 isl_basic_map_free(result);
4749 return NULL;
4752 /* Given a map A -> f(A) and an integer d, construct a map
4753 * A -> floor(f(A)/d).
4755 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4757 int i;
4759 map = isl_map_cow(map);
4760 if (!map)
4761 return NULL;
4763 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4764 for (i = 0; i < map->n; ++i) {
4765 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4766 if (!map->p[i])
4767 goto error;
4769 map = isl_map_unmark_normalized(map);
4771 return map;
4772 error:
4773 isl_map_free(map);
4774 return NULL;
4777 /* Given a map A -> f(A) and an integer d, construct a map
4778 * A -> floor(f(A)/d).
4780 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4781 __isl_take isl_val *d)
4783 if (!map || !d)
4784 goto error;
4785 if (!isl_val_is_int(d))
4786 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4787 "expecting integer denominator", goto error);
4788 map = isl_map_floordiv(map, d->n);
4789 isl_val_free(d);
4790 return map;
4791 error:
4792 isl_map_free(map);
4793 isl_val_free(d);
4794 return NULL;
4797 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4798 unsigned pos)
4800 int i;
4801 unsigned nparam;
4802 unsigned n_in;
4804 i = isl_basic_map_alloc_equality(bmap);
4805 if (i < 0)
4806 goto error;
4807 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4808 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4809 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4810 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4811 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4812 return isl_basic_map_finalize(bmap);
4813 error:
4814 isl_basic_map_free(bmap);
4815 return NULL;
4818 /* Add a constraint to "bmap" expressing i_pos < o_pos
4820 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4821 unsigned pos)
4823 int i;
4824 unsigned nparam;
4825 unsigned n_in;
4827 i = isl_basic_map_alloc_inequality(bmap);
4828 if (i < 0)
4829 goto error;
4830 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4831 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4832 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4833 isl_int_set_si(bmap->ineq[i][0], -1);
4834 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4835 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4836 return isl_basic_map_finalize(bmap);
4837 error:
4838 isl_basic_map_free(bmap);
4839 return NULL;
4842 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4844 static __isl_give isl_basic_map *var_less_or_equal(
4845 __isl_take isl_basic_map *bmap, unsigned pos)
4847 int i;
4848 unsigned nparam;
4849 unsigned n_in;
4851 i = isl_basic_map_alloc_inequality(bmap);
4852 if (i < 0)
4853 goto error;
4854 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4855 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4856 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4857 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4858 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4859 return isl_basic_map_finalize(bmap);
4860 error:
4861 isl_basic_map_free(bmap);
4862 return NULL;
4865 /* Add a constraint to "bmap" expressing i_pos > o_pos
4867 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4868 unsigned pos)
4870 int i;
4871 unsigned nparam;
4872 unsigned n_in;
4874 i = isl_basic_map_alloc_inequality(bmap);
4875 if (i < 0)
4876 goto error;
4877 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4878 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4879 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4880 isl_int_set_si(bmap->ineq[i][0], -1);
4881 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4882 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4883 return isl_basic_map_finalize(bmap);
4884 error:
4885 isl_basic_map_free(bmap);
4886 return NULL;
4889 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4891 static __isl_give isl_basic_map *var_more_or_equal(
4892 __isl_take isl_basic_map *bmap, unsigned pos)
4894 int i;
4895 unsigned nparam;
4896 unsigned n_in;
4898 i = isl_basic_map_alloc_inequality(bmap);
4899 if (i < 0)
4900 goto error;
4901 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4902 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4903 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4904 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4905 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4906 return isl_basic_map_finalize(bmap);
4907 error:
4908 isl_basic_map_free(bmap);
4909 return NULL;
4912 __isl_give isl_basic_map *isl_basic_map_equal(
4913 __isl_take isl_space *space, unsigned n_equal)
4915 int i;
4916 struct isl_basic_map *bmap;
4917 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4918 if (!bmap)
4919 return NULL;
4920 for (i = 0; i < n_equal && bmap; ++i)
4921 bmap = var_equal(bmap, i);
4922 return isl_basic_map_finalize(bmap);
4925 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4927 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4928 unsigned pos)
4930 int i;
4931 struct isl_basic_map *bmap;
4932 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4933 if (!bmap)
4934 return NULL;
4935 for (i = 0; i < pos && bmap; ++i)
4936 bmap = var_equal(bmap, i);
4937 if (bmap)
4938 bmap = var_less(bmap, pos);
4939 return isl_basic_map_finalize(bmap);
4942 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4944 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4945 __isl_take isl_space *space, unsigned pos)
4947 int i;
4948 isl_basic_map *bmap;
4950 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4951 for (i = 0; i < pos; ++i)
4952 bmap = var_equal(bmap, i);
4953 bmap = var_less_or_equal(bmap, pos);
4954 return isl_basic_map_finalize(bmap);
4957 /* Return a relation on "space" expressing i_pos > o_pos
4959 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4960 unsigned pos)
4962 int i;
4963 struct isl_basic_map *bmap;
4964 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4965 if (!bmap)
4966 return NULL;
4967 for (i = 0; i < pos && bmap; ++i)
4968 bmap = var_equal(bmap, i);
4969 if (bmap)
4970 bmap = var_more(bmap, pos);
4971 return isl_basic_map_finalize(bmap);
4974 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4976 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4977 __isl_take isl_space *space, unsigned pos)
4979 int i;
4980 isl_basic_map *bmap;
4982 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4983 for (i = 0; i < pos; ++i)
4984 bmap = var_equal(bmap, i);
4985 bmap = var_more_or_equal(bmap, pos);
4986 return isl_basic_map_finalize(bmap);
4989 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4990 unsigned n, int equal)
4992 struct isl_map *map;
4993 int i;
4995 if (n == 0 && equal)
4996 return isl_map_universe(space);
4998 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5000 for (i = 0; i + 1 < n; ++i)
5001 map = isl_map_add_basic_map(map,
5002 isl_basic_map_less_at(isl_space_copy(space), i));
5003 if (n > 0) {
5004 if (equal)
5005 map = isl_map_add_basic_map(map,
5006 isl_basic_map_less_or_equal_at(space, n - 1));
5007 else
5008 map = isl_map_add_basic_map(map,
5009 isl_basic_map_less_at(space, n - 1));
5010 } else
5011 isl_space_free(space);
5013 return map;
5016 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5018 if (!space)
5019 return NULL;
5020 return map_lex_lte_first(space, space->n_out, equal);
5023 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5025 return map_lex_lte_first(dim, n, 0);
5028 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5030 return map_lex_lte_first(dim, n, 1);
5033 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5035 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5038 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5040 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5043 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5044 unsigned n, int equal)
5046 struct isl_map *map;
5047 int i;
5049 if (n == 0 && equal)
5050 return isl_map_universe(space);
5052 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5054 for (i = 0; i + 1 < n; ++i)
5055 map = isl_map_add_basic_map(map,
5056 isl_basic_map_more_at(isl_space_copy(space), i));
5057 if (n > 0) {
5058 if (equal)
5059 map = isl_map_add_basic_map(map,
5060 isl_basic_map_more_or_equal_at(space, n - 1));
5061 else
5062 map = isl_map_add_basic_map(map,
5063 isl_basic_map_more_at(space, n - 1));
5064 } else
5065 isl_space_free(space);
5067 return map;
5070 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5072 if (!space)
5073 return NULL;
5074 return map_lex_gte_first(space, space->n_out, equal);
5077 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5079 return map_lex_gte_first(dim, n, 0);
5082 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5084 return map_lex_gte_first(dim, n, 1);
5087 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5089 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5092 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5094 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5097 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5098 __isl_take isl_set *set2)
5100 isl_map *map;
5101 map = isl_map_lex_le(isl_set_get_space(set1));
5102 map = isl_map_intersect_domain(map, set1);
5103 map = isl_map_intersect_range(map, set2);
5104 return map;
5107 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5108 __isl_take isl_set *set2)
5110 isl_map *map;
5111 map = isl_map_lex_lt(isl_set_get_space(set1));
5112 map = isl_map_intersect_domain(map, set1);
5113 map = isl_map_intersect_range(map, set2);
5114 return map;
5117 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5118 __isl_take isl_set *set2)
5120 isl_map *map;
5121 map = isl_map_lex_ge(isl_set_get_space(set1));
5122 map = isl_map_intersect_domain(map, set1);
5123 map = isl_map_intersect_range(map, set2);
5124 return map;
5127 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5128 __isl_take isl_set *set2)
5130 isl_map *map;
5131 map = isl_map_lex_gt(isl_set_get_space(set1));
5132 map = isl_map_intersect_domain(map, set1);
5133 map = isl_map_intersect_range(map, set2);
5134 return map;
5137 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5138 __isl_take isl_map *map2)
5140 isl_map *map;
5141 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5142 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5143 map = isl_map_apply_range(map, isl_map_reverse(map2));
5144 return map;
5147 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5148 __isl_take isl_map *map2)
5150 isl_map *map;
5151 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5152 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5153 map = isl_map_apply_range(map, isl_map_reverse(map2));
5154 return map;
5157 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5158 __isl_take isl_map *map2)
5160 isl_map *map;
5161 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5162 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5163 map = isl_map_apply_range(map, isl_map_reverse(map2));
5164 return map;
5167 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5168 __isl_take isl_map *map2)
5170 isl_map *map;
5171 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5172 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5173 map = isl_map_apply_range(map, isl_map_reverse(map2));
5174 return map;
5177 /* For a div d = floor(f/m), add the constraint
5179 * f - m d >= 0
5181 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5182 unsigned pos, isl_int *div)
5184 int i;
5185 unsigned total = isl_basic_map_total_dim(bmap);
5187 i = isl_basic_map_alloc_inequality(bmap);
5188 if (i < 0)
5189 return isl_stat_error;
5190 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5191 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5193 return isl_stat_ok;
5196 /* For a div d = floor(f/m), add the constraint
5198 * -(f-(m-1)) + m d >= 0
5200 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5201 unsigned pos, isl_int *div)
5203 int i;
5204 unsigned total = isl_basic_map_total_dim(bmap);
5206 i = isl_basic_map_alloc_inequality(bmap);
5207 if (i < 0)
5208 return isl_stat_error;
5209 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5210 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5211 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5212 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5214 return isl_stat_ok;
5217 /* For a div d = floor(f/m), add the constraints
5219 * f - m d >= 0
5220 * -(f-(m-1)) + m d >= 0
5222 * Note that the second constraint is the negation of
5224 * f - m d >= m
5226 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5227 unsigned pos, isl_int *div)
5229 if (add_upper_div_constraint(bmap, pos, div) < 0)
5230 return -1;
5231 if (add_lower_div_constraint(bmap, pos, div) < 0)
5232 return -1;
5233 return 0;
5236 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5237 unsigned pos, isl_int *div)
5239 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5240 pos, div);
5243 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map *bmap,
5244 unsigned div)
5246 unsigned total = isl_basic_map_total_dim(bmap);
5247 unsigned div_pos = total - bmap->n_div + div;
5249 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5250 bmap->div[div]);
5253 /* For each known div d = floor(f/m), add the constraints
5255 * f - m d >= 0
5256 * -(f-(m-1)) + m d >= 0
5258 * Remove duplicate constraints in case of some these div constraints
5259 * already appear in "bmap".
5261 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5262 __isl_take isl_basic_map *bmap)
5264 unsigned n_div;
5266 if (!bmap)
5267 return NULL;
5268 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5269 if (n_div == 0)
5270 return bmap;
5272 bmap = add_known_div_constraints(bmap);
5273 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5274 bmap = isl_basic_map_finalize(bmap);
5275 return bmap;
5278 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5280 * In particular, if this div is of the form d = floor(f/m),
5281 * then add the constraint
5283 * f - m d >= 0
5285 * if sign < 0 or the constraint
5287 * -(f-(m-1)) + m d >= 0
5289 * if sign > 0.
5291 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5292 unsigned div, int sign)
5294 unsigned total;
5295 unsigned div_pos;
5297 if (!bmap)
5298 return -1;
5300 total = isl_basic_map_total_dim(bmap);
5301 div_pos = total - bmap->n_div + div;
5303 if (sign < 0)
5304 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5305 else
5306 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5309 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5310 __isl_take isl_basic_map *bmap)
5312 if (!bmap)
5313 goto error;
5314 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5315 bmap->n_div == 0 &&
5316 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5317 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5318 return bset_from_bmap(bmap);
5319 bmap = isl_basic_map_cow(bmap);
5320 if (!bmap)
5321 goto error;
5322 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5323 if (!bmap->dim)
5324 goto error;
5325 bmap->extra -= bmap->n_div;
5326 bmap->n_div = 0;
5327 bmap = isl_basic_map_finalize(bmap);
5328 return bset_from_bmap(bmap);
5329 error:
5330 isl_basic_map_free(bmap);
5331 return NULL;
5334 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5335 __isl_take isl_basic_set *bset)
5337 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5340 /* Replace each element in "list" by the result of applying
5341 * isl_basic_map_underlying_set to the element.
5343 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5344 __isl_take isl_basic_map_list *list)
5346 int i, n;
5348 if (!list)
5349 return NULL;
5351 n = isl_basic_map_list_n_basic_map(list);
5352 for (i = 0; i < n; ++i) {
5353 isl_basic_map *bmap;
5354 isl_basic_set *bset;
5356 bmap = isl_basic_map_list_get_basic_map(list, i);
5357 bset = isl_basic_set_underlying_set(bmap);
5358 list = isl_basic_set_list_set_basic_set(list, i, bset);
5361 return list;
5364 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5365 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5367 struct isl_basic_map *bmap;
5368 struct isl_ctx *ctx;
5369 unsigned total;
5370 int i;
5372 if (!bset || !like)
5373 goto error;
5374 ctx = bset->ctx;
5375 if (isl_basic_set_check_no_params(bset) < 0 ||
5376 isl_basic_set_check_no_locals(bset) < 0)
5377 goto error;
5378 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5379 goto error);
5380 if (like->n_div == 0) {
5381 isl_space *space = isl_basic_map_get_space(like);
5382 isl_basic_map_free(like);
5383 return isl_basic_map_reset_space(bset, space);
5385 bset = isl_basic_set_cow(bset);
5386 if (!bset)
5387 goto error;
5388 total = bset->dim->n_out + bset->extra;
5389 bmap = bset_to_bmap(bset);
5390 isl_space_free(bmap->dim);
5391 bmap->dim = isl_space_copy(like->dim);
5392 if (!bmap->dim)
5393 goto error;
5394 bmap->n_div = like->n_div;
5395 bmap->extra += like->n_div;
5396 if (bmap->extra) {
5397 unsigned ltotal;
5398 isl_int **div;
5399 ltotal = total - bmap->extra + like->extra;
5400 if (ltotal > total)
5401 ltotal = total;
5402 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5403 bmap->extra * (1 + 1 + total));
5404 if (isl_blk_is_error(bmap->block2))
5405 goto error;
5406 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5407 if (!div)
5408 goto error;
5409 bmap->div = div;
5410 for (i = 0; i < bmap->extra; ++i)
5411 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5412 for (i = 0; i < like->n_div; ++i) {
5413 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5414 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5416 bmap = isl_basic_map_add_known_div_constraints(bmap);
5418 isl_basic_map_free(like);
5419 bmap = isl_basic_map_simplify(bmap);
5420 bmap = isl_basic_map_finalize(bmap);
5421 return bmap;
5422 error:
5423 isl_basic_map_free(like);
5424 isl_basic_set_free(bset);
5425 return NULL;
5428 struct isl_basic_set *isl_basic_set_from_underlying_set(
5429 struct isl_basic_set *bset, struct isl_basic_set *like)
5431 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5432 bset_to_bmap(like)));
5435 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5437 int i;
5439 map = isl_map_cow(map);
5440 if (!map)
5441 return NULL;
5442 map->dim = isl_space_cow(map->dim);
5443 if (!map->dim)
5444 goto error;
5446 for (i = 1; i < map->n; ++i)
5447 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5448 goto error);
5449 for (i = 0; i < map->n; ++i) {
5450 map->p[i] = bset_to_bmap(
5451 isl_basic_map_underlying_set(map->p[i]));
5452 if (!map->p[i])
5453 goto error;
5455 if (map->n == 0)
5456 map->dim = isl_space_underlying(map->dim, 0);
5457 else {
5458 isl_space_free(map->dim);
5459 map->dim = isl_space_copy(map->p[0]->dim);
5461 if (!map->dim)
5462 goto error;
5463 return set_from_map(map);
5464 error:
5465 isl_map_free(map);
5466 return NULL;
5469 /* Replace the space of "bmap" by "space".
5471 * If the space of "bmap" is identical to "space" (including the identifiers
5472 * of the input and output dimensions), then simply return the original input.
5474 __isl_give isl_basic_map *isl_basic_map_reset_space(
5475 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5477 isl_bool equal;
5478 isl_space *bmap_space;
5480 bmap_space = isl_basic_map_peek_space(bmap);
5481 equal = isl_space_is_equal(bmap_space, space);
5482 if (equal >= 0 && equal)
5483 equal = isl_space_has_equal_ids(bmap_space, space);
5484 if (equal < 0)
5485 goto error;
5486 if (equal) {
5487 isl_space_free(space);
5488 return bmap;
5490 bmap = isl_basic_map_cow(bmap);
5491 if (!bmap || !space)
5492 goto error;
5494 isl_space_free(bmap->dim);
5495 bmap->dim = space;
5497 bmap = isl_basic_map_finalize(bmap);
5499 return bmap;
5500 error:
5501 isl_basic_map_free(bmap);
5502 isl_space_free(space);
5503 return NULL;
5506 __isl_give isl_basic_set *isl_basic_set_reset_space(
5507 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5509 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5510 dim));
5513 /* Check that the total dimensions of "map" and "space" are the same.
5515 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5516 __isl_keep isl_space *space)
5518 unsigned dim1, dim2;
5520 if (!map || !space)
5521 return isl_stat_error;
5522 dim1 = isl_map_dim(map, isl_dim_all);
5523 dim2 = isl_space_dim(space, isl_dim_all);
5524 if (dim1 == dim2)
5525 return isl_stat_ok;
5526 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5527 "total dimensions do not match", return isl_stat_error);
5530 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5531 __isl_take isl_space *space)
5533 int i;
5535 map = isl_map_cow(map);
5536 if (!map || !space)
5537 goto error;
5539 for (i = 0; i < map->n; ++i) {
5540 map->p[i] = isl_basic_map_reset_space(map->p[i],
5541 isl_space_copy(space));
5542 if (!map->p[i])
5543 goto error;
5545 isl_space_free(map->dim);
5546 map->dim = space;
5548 return map;
5549 error:
5550 isl_map_free(map);
5551 isl_space_free(space);
5552 return NULL;
5555 /* Replace the space of "map" by "space", without modifying
5556 * the dimension of "map".
5558 * If the space of "map" is identical to "space" (including the identifiers
5559 * of the input and output dimensions), then simply return the original input.
5561 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5562 __isl_take isl_space *space)
5564 isl_bool equal;
5565 isl_space *map_space;
5567 map_space = isl_map_peek_space(map);
5568 equal = isl_space_is_equal(map_space, space);
5569 if (equal >= 0 && equal)
5570 equal = isl_space_has_equal_ids(map_space, space);
5571 if (equal < 0)
5572 goto error;
5573 if (equal) {
5574 isl_space_free(space);
5575 return map;
5577 if (check_map_space_equal_total_dim(map, space) < 0)
5578 goto error;
5579 return isl_map_reset_space(map, space);
5580 error:
5581 isl_map_free(map);
5582 isl_space_free(space);
5583 return NULL;
5586 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5587 __isl_take isl_space *dim)
5589 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5592 /* Compute the parameter domain of the given basic set.
5594 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5596 isl_bool is_params;
5597 isl_space *space;
5598 unsigned n;
5600 is_params = isl_basic_set_is_params(bset);
5601 if (is_params < 0)
5602 return isl_basic_set_free(bset);
5603 if (is_params)
5604 return bset;
5606 n = isl_basic_set_dim(bset, isl_dim_set);
5607 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5608 space = isl_basic_set_get_space(bset);
5609 space = isl_space_params(space);
5610 bset = isl_basic_set_reset_space(bset, space);
5611 return bset;
5614 /* Construct a zero-dimensional basic set with the given parameter domain.
5616 __isl_give isl_basic_set *isl_basic_set_from_params(
5617 __isl_take isl_basic_set *bset)
5619 isl_space *space;
5620 space = isl_basic_set_get_space(bset);
5621 space = isl_space_set_from_params(space);
5622 bset = isl_basic_set_reset_space(bset, space);
5623 return bset;
5626 /* Compute the parameter domain of the given set.
5628 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5630 isl_space *space;
5631 unsigned n;
5633 if (isl_set_is_params(set))
5634 return set;
5636 n = isl_set_dim(set, isl_dim_set);
5637 set = isl_set_project_out(set, isl_dim_set, 0, n);
5638 space = isl_set_get_space(set);
5639 space = isl_space_params(space);
5640 set = isl_set_reset_space(set, space);
5641 return set;
5644 /* Construct a zero-dimensional set with the given parameter domain.
5646 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5648 isl_space *space;
5649 space = isl_set_get_space(set);
5650 space = isl_space_set_from_params(space);
5651 set = isl_set_reset_space(set, space);
5652 return set;
5655 /* Compute the parameter domain of the given map.
5657 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5659 isl_space *space;
5660 unsigned n;
5662 n = isl_map_dim(map, isl_dim_in);
5663 map = isl_map_project_out(map, isl_dim_in, 0, n);
5664 n = isl_map_dim(map, isl_dim_out);
5665 map = isl_map_project_out(map, isl_dim_out, 0, n);
5666 space = isl_map_get_space(map);
5667 space = isl_space_params(space);
5668 map = isl_map_reset_space(map, space);
5669 return map;
5672 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5674 isl_space *space;
5675 unsigned n_out;
5677 if (!bmap)
5678 return NULL;
5679 space = isl_space_domain(isl_basic_map_get_space(bmap));
5681 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5682 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5684 return isl_basic_map_reset_space(bmap, space);
5687 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5689 if (!bmap)
5690 return isl_bool_error;
5691 return isl_space_may_be_set(bmap->dim);
5694 /* Is this basic map actually a set?
5695 * Users should never call this function. Outside of isl,
5696 * the type should indicate whether something is a set or a map.
5698 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5700 if (!bmap)
5701 return isl_bool_error;
5702 return isl_space_is_set(bmap->dim);
5705 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5707 isl_bool is_set;
5709 is_set = isl_basic_map_is_set(bmap);
5710 if (is_set < 0)
5711 goto error;
5712 if (is_set)
5713 return bmap;
5714 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5715 error:
5716 isl_basic_map_free(bmap);
5717 return NULL;
5720 __isl_give isl_basic_map *isl_basic_map_domain_map(
5721 __isl_take isl_basic_map *bmap)
5723 int i;
5724 isl_space *space;
5725 isl_basic_map *domain;
5726 int nparam, n_in, n_out;
5728 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5729 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5730 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5732 space = isl_basic_map_get_space(bmap);
5733 space = isl_space_from_range(isl_space_domain(space));
5734 domain = isl_basic_map_universe(space);
5736 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5737 bmap = isl_basic_map_apply_range(bmap, domain);
5738 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5740 for (i = 0; i < n_in; ++i)
5741 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5742 isl_dim_out, i);
5744 bmap = isl_basic_map_gauss(bmap, NULL);
5745 return isl_basic_map_finalize(bmap);
5748 __isl_give isl_basic_map *isl_basic_map_range_map(
5749 __isl_take isl_basic_map *bmap)
5751 int i;
5752 isl_space *space;
5753 isl_basic_map *range;
5754 int nparam, n_in, n_out;
5756 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5757 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5758 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5760 space = isl_basic_map_get_space(bmap);
5761 space = isl_space_from_range(isl_space_range(space));
5762 range = isl_basic_map_universe(space);
5764 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5765 bmap = isl_basic_map_apply_range(bmap, range);
5766 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5768 for (i = 0; i < n_out; ++i)
5769 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5770 isl_dim_out, i);
5772 bmap = isl_basic_map_gauss(bmap, NULL);
5773 return isl_basic_map_finalize(bmap);
5776 int isl_map_may_be_set(__isl_keep isl_map *map)
5778 if (!map)
5779 return -1;
5780 return isl_space_may_be_set(map->dim);
5783 /* Is this map actually a set?
5784 * Users should never call this function. Outside of isl,
5785 * the type should indicate whether something is a set or a map.
5787 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5789 if (!map)
5790 return isl_bool_error;
5791 return isl_space_is_set(map->dim);
5794 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5796 int i;
5797 isl_bool is_set;
5798 struct isl_set *set;
5800 is_set = isl_map_is_set(map);
5801 if (is_set < 0)
5802 goto error;
5803 if (is_set)
5804 return set_from_map(map);
5806 map = isl_map_cow(map);
5807 if (!map)
5808 goto error;
5810 set = set_from_map(map);
5811 set->dim = isl_space_range(set->dim);
5812 if (!set->dim)
5813 goto error;
5814 for (i = 0; i < map->n; ++i) {
5815 set->p[i] = isl_basic_map_range(map->p[i]);
5816 if (!set->p[i])
5817 goto error;
5819 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5820 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5821 return set;
5822 error:
5823 isl_map_free(map);
5824 return NULL;
5827 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5829 int i;
5831 map = isl_map_cow(map);
5832 if (!map)
5833 return NULL;
5835 map->dim = isl_space_domain_map(map->dim);
5836 if (!map->dim)
5837 goto error;
5838 for (i = 0; i < map->n; ++i) {
5839 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5840 if (!map->p[i])
5841 goto error;
5843 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5844 map = isl_map_unmark_normalized(map);
5845 return map;
5846 error:
5847 isl_map_free(map);
5848 return NULL;
5851 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5853 int i;
5854 isl_space *range_dim;
5856 map = isl_map_cow(map);
5857 if (!map)
5858 return NULL;
5860 range_dim = isl_space_range(isl_map_get_space(map));
5861 range_dim = isl_space_from_range(range_dim);
5862 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5863 map->dim = isl_space_join(map->dim, range_dim);
5864 if (!map->dim)
5865 goto error;
5866 for (i = 0; i < map->n; ++i) {
5867 map->p[i] = isl_basic_map_range_map(map->p[i]);
5868 if (!map->p[i])
5869 goto error;
5871 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5872 map = isl_map_unmark_normalized(map);
5873 return map;
5874 error:
5875 isl_map_free(map);
5876 return NULL;
5879 /* Given a wrapped map of the form A[B -> C],
5880 * return the map A[B -> C] -> B.
5882 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5884 isl_id *id;
5885 isl_map *map;
5887 if (!set)
5888 return NULL;
5889 if (!isl_set_has_tuple_id(set))
5890 return isl_map_domain_map(isl_set_unwrap(set));
5892 id = isl_set_get_tuple_id(set);
5893 map = isl_map_domain_map(isl_set_unwrap(set));
5894 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5896 return map;
5899 __isl_give isl_basic_map *isl_basic_map_from_domain(
5900 __isl_take isl_basic_set *bset)
5902 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5905 __isl_give isl_basic_map *isl_basic_map_from_range(
5906 __isl_take isl_basic_set *bset)
5908 isl_space *space;
5909 space = isl_basic_set_get_space(bset);
5910 space = isl_space_from_range(space);
5911 bset = isl_basic_set_reset_space(bset, space);
5912 return bset_to_bmap(bset);
5915 /* Create a relation with the given set as range.
5916 * The domain of the created relation is a zero-dimensional
5917 * flat anonymous space.
5919 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5921 isl_space *space;
5922 space = isl_set_get_space(set);
5923 space = isl_space_from_range(space);
5924 set = isl_set_reset_space(set, space);
5925 return set_to_map(set);
5928 /* Create a relation with the given set as domain.
5929 * The range of the created relation is a zero-dimensional
5930 * flat anonymous space.
5932 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5934 return isl_map_reverse(isl_map_from_range(set));
5937 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5938 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5940 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5943 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5944 __isl_take isl_set *range)
5946 return isl_map_apply_range(isl_map_reverse(domain), range);
5949 /* Return a newly allocated isl_map with given space and flags and
5950 * room for "n" basic maps.
5951 * Make sure that all cached information is cleared.
5953 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5954 unsigned flags)
5956 struct isl_map *map;
5958 if (!space)
5959 return NULL;
5960 if (n < 0)
5961 isl_die(space->ctx, isl_error_internal,
5962 "negative number of basic maps", goto error);
5963 map = isl_calloc(space->ctx, struct isl_map,
5964 sizeof(struct isl_map) +
5965 (n - 1) * sizeof(struct isl_basic_map *));
5966 if (!map)
5967 goto error;
5969 map->ctx = space->ctx;
5970 isl_ctx_ref(map->ctx);
5971 map->ref = 1;
5972 map->size = n;
5973 map->n = 0;
5974 map->dim = space;
5975 map->flags = flags;
5976 return map;
5977 error:
5978 isl_space_free(space);
5979 return NULL;
5982 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5984 struct isl_basic_map *bmap;
5985 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5986 bmap = isl_basic_map_set_to_empty(bmap);
5987 return bmap;
5990 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5992 struct isl_basic_set *bset;
5993 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5994 bset = isl_basic_set_set_to_empty(bset);
5995 return bset;
5998 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6000 struct isl_basic_map *bmap;
6001 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6002 bmap = isl_basic_map_finalize(bmap);
6003 return bmap;
6006 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6008 struct isl_basic_set *bset;
6009 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6010 bset = isl_basic_set_finalize(bset);
6011 return bset;
6014 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6015 __isl_take isl_space *space)
6017 int i;
6018 unsigned total = isl_space_dim(space, isl_dim_all);
6019 isl_basic_map *bmap;
6021 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6022 for (i = 0; i < total; ++i) {
6023 int k = isl_basic_map_alloc_inequality(bmap);
6024 if (k < 0)
6025 goto error;
6026 isl_seq_clr(bmap->ineq[k], 1 + total);
6027 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6029 return bmap;
6030 error:
6031 isl_basic_map_free(bmap);
6032 return NULL;
6035 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6036 __isl_take isl_space *space)
6038 return isl_basic_map_nat_universe(space);
6041 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6043 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6046 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6048 return isl_map_nat_universe(dim);
6051 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6053 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6056 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6058 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6061 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6063 struct isl_map *map;
6064 if (!space)
6065 return NULL;
6066 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6067 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6068 return map;
6071 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6073 struct isl_set *set;
6074 if (!space)
6075 return NULL;
6076 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6077 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6078 return set;
6081 struct isl_map *isl_map_dup(struct isl_map *map)
6083 int i;
6084 struct isl_map *dup;
6086 if (!map)
6087 return NULL;
6088 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6089 for (i = 0; i < map->n; ++i)
6090 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6091 return dup;
6094 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6095 __isl_take isl_basic_map *bmap)
6097 if (!bmap || !map)
6098 goto error;
6099 if (isl_basic_map_plain_is_empty(bmap)) {
6100 isl_basic_map_free(bmap);
6101 return map;
6103 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6104 isl_assert(map->ctx, map->n < map->size, goto error);
6105 map->p[map->n] = bmap;
6106 map->n++;
6107 map = isl_map_unmark_normalized(map);
6108 return map;
6109 error:
6110 if (map)
6111 isl_map_free(map);
6112 if (bmap)
6113 isl_basic_map_free(bmap);
6114 return NULL;
6117 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6119 int i;
6121 if (!map)
6122 return NULL;
6124 if (--map->ref > 0)
6125 return NULL;
6127 clear_caches(map);
6128 isl_ctx_deref(map->ctx);
6129 for (i = 0; i < map->n; ++i)
6130 isl_basic_map_free(map->p[i]);
6131 isl_space_free(map->dim);
6132 free(map);
6134 return NULL;
6137 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6138 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6140 int j;
6142 bmap = isl_basic_map_cow(bmap);
6143 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6144 j = isl_basic_map_alloc_equality(bmap);
6145 if (j < 0)
6146 goto error;
6147 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6148 isl_int_set_si(bmap->eq[j][pos], -1);
6149 isl_int_set_si(bmap->eq[j][0], value);
6150 bmap = isl_basic_map_simplify(bmap);
6151 return isl_basic_map_finalize(bmap);
6152 error:
6153 isl_basic_map_free(bmap);
6154 return NULL;
6157 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6158 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6160 int j;
6162 bmap = isl_basic_map_cow(bmap);
6163 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6164 j = isl_basic_map_alloc_equality(bmap);
6165 if (j < 0)
6166 goto error;
6167 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6168 isl_int_set_si(bmap->eq[j][pos], -1);
6169 isl_int_set(bmap->eq[j][0], value);
6170 bmap = isl_basic_map_simplify(bmap);
6171 return isl_basic_map_finalize(bmap);
6172 error:
6173 isl_basic_map_free(bmap);
6174 return NULL;
6177 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6178 enum isl_dim_type type, unsigned pos, int value)
6180 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6181 return isl_basic_map_free(bmap);
6182 return isl_basic_map_fix_pos_si(bmap,
6183 isl_basic_map_offset(bmap, type) + pos, value);
6186 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6187 enum isl_dim_type type, unsigned pos, isl_int value)
6189 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6190 return isl_basic_map_free(bmap);
6191 return isl_basic_map_fix_pos(bmap,
6192 isl_basic_map_offset(bmap, type) + pos, value);
6195 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6196 * to be equal to "v".
6198 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6199 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6201 if (!bmap || !v)
6202 goto error;
6203 if (!isl_val_is_int(v))
6204 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6205 "expecting integer value", goto error);
6206 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6207 goto error;
6208 pos += isl_basic_map_offset(bmap, type);
6209 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6210 isl_val_free(v);
6211 return bmap;
6212 error:
6213 isl_basic_map_free(bmap);
6214 isl_val_free(v);
6215 return NULL;
6218 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6219 * to be equal to "v".
6221 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6222 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6224 return isl_basic_map_fix_val(bset, type, pos, v);
6227 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6228 enum isl_dim_type type, unsigned pos, int value)
6230 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6231 type, pos, value));
6234 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6235 enum isl_dim_type type, unsigned pos, isl_int value)
6237 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6238 type, pos, value));
6241 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6242 unsigned input, int value)
6244 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6247 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6248 unsigned dim, int value)
6250 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6251 isl_dim_set, dim, value));
6254 /* Remove the basic map at position "i" from "map" if this basic map
6255 * is (obviously) empty.
6257 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6259 isl_bool empty;
6261 if (!map)
6262 return NULL;
6264 empty = isl_basic_map_plain_is_empty(map->p[i]);
6265 if (empty < 0)
6266 return isl_map_free(map);
6267 if (!empty)
6268 return map;
6270 isl_basic_map_free(map->p[i]);
6271 map->n--;
6272 if (i != map->n) {
6273 map->p[i] = map->p[map->n];
6274 map = isl_map_unmark_normalized(map);
6278 return map;
6281 /* Perform "fn" on each basic map of "map", where we may not be holding
6282 * the only reference to "map".
6283 * In particular, "fn" should be a semantics preserving operation
6284 * that we want to apply to all copies of "map". We therefore need
6285 * to be careful not to modify "map" in a way that breaks "map"
6286 * in case anything goes wrong.
6288 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6289 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6291 struct isl_basic_map *bmap;
6292 int i;
6294 if (!map)
6295 return NULL;
6297 for (i = map->n - 1; i >= 0; --i) {
6298 bmap = isl_basic_map_copy(map->p[i]);
6299 bmap = fn(bmap);
6300 if (!bmap)
6301 goto error;
6302 isl_basic_map_free(map->p[i]);
6303 map->p[i] = bmap;
6304 map = remove_if_empty(map, i);
6305 if (!map)
6306 return NULL;
6309 return map;
6310 error:
6311 isl_map_free(map);
6312 return NULL;
6315 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6316 enum isl_dim_type type, unsigned pos, int value)
6318 int i;
6320 map = isl_map_cow(map);
6321 if (isl_map_check_range(map, type, pos, 1) < 0)
6322 return isl_map_free(map);
6323 for (i = map->n - 1; i >= 0; --i) {
6324 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6325 map = remove_if_empty(map, i);
6326 if (!map)
6327 return NULL;
6329 map = isl_map_unmark_normalized(map);
6330 return map;
6333 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6334 enum isl_dim_type type, unsigned pos, int value)
6336 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6339 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6340 enum isl_dim_type type, unsigned pos, isl_int value)
6342 int i;
6344 map = isl_map_cow(map);
6345 if (isl_map_check_range(map, type, pos, 1) < 0)
6346 return isl_map_free(map);
6347 for (i = 0; i < map->n; ++i) {
6348 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6349 if (!map->p[i])
6350 goto error;
6352 map = isl_map_unmark_normalized(map);
6353 return map;
6354 error:
6355 isl_map_free(map);
6356 return NULL;
6359 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6360 enum isl_dim_type type, unsigned pos, isl_int value)
6362 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6365 /* Fix the value of the variable at position "pos" of type "type" of "map"
6366 * to be equal to "v".
6368 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6369 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6371 int i;
6373 map = isl_map_cow(map);
6374 if (!map || !v)
6375 goto error;
6377 if (!isl_val_is_int(v))
6378 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6379 "expecting integer value", goto error);
6380 if (isl_map_check_range(map, type, pos, 1) < 0)
6381 goto error;
6382 for (i = map->n - 1; i >= 0; --i) {
6383 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6384 isl_val_copy(v));
6385 map = remove_if_empty(map, i);
6386 if (!map)
6387 goto error;
6389 map = isl_map_unmark_normalized(map);
6390 isl_val_free(v);
6391 return map;
6392 error:
6393 isl_map_free(map);
6394 isl_val_free(v);
6395 return NULL;
6398 /* Fix the value of the variable at position "pos" of type "type" of "set"
6399 * to be equal to "v".
6401 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6402 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6404 return isl_map_fix_val(set, type, pos, v);
6407 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6408 unsigned input, int value)
6410 return isl_map_fix_si(map, isl_dim_in, input, value);
6413 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6415 return set_from_map(isl_map_fix_si(set_to_map(set),
6416 isl_dim_set, dim, value));
6419 static __isl_give isl_basic_map *basic_map_bound_si(
6420 __isl_take isl_basic_map *bmap,
6421 enum isl_dim_type type, unsigned pos, int value, int upper)
6423 int j;
6425 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6426 return isl_basic_map_free(bmap);
6427 pos += isl_basic_map_offset(bmap, type);
6428 bmap = isl_basic_map_cow(bmap);
6429 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6430 j = isl_basic_map_alloc_inequality(bmap);
6431 if (j < 0)
6432 goto error;
6433 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6434 if (upper) {
6435 isl_int_set_si(bmap->ineq[j][pos], -1);
6436 isl_int_set_si(bmap->ineq[j][0], value);
6437 } else {
6438 isl_int_set_si(bmap->ineq[j][pos], 1);
6439 isl_int_set_si(bmap->ineq[j][0], -value);
6441 bmap = isl_basic_map_simplify(bmap);
6442 return isl_basic_map_finalize(bmap);
6443 error:
6444 isl_basic_map_free(bmap);
6445 return NULL;
6448 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6449 __isl_take isl_basic_map *bmap,
6450 enum isl_dim_type type, unsigned pos, int value)
6452 return basic_map_bound_si(bmap, type, pos, value, 0);
6455 /* Constrain the values of the given dimension to be no greater than "value".
6457 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6458 __isl_take isl_basic_map *bmap,
6459 enum isl_dim_type type, unsigned pos, int value)
6461 return basic_map_bound_si(bmap, type, pos, value, 1);
6464 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6465 enum isl_dim_type type, unsigned pos, int value, int upper)
6467 int i;
6469 map = isl_map_cow(map);
6470 if (isl_map_check_range(map, type, pos, 1) < 0)
6471 return isl_map_free(map);
6472 for (i = 0; i < map->n; ++i) {
6473 map->p[i] = basic_map_bound_si(map->p[i],
6474 type, pos, value, upper);
6475 if (!map->p[i])
6476 goto error;
6478 map = isl_map_unmark_normalized(map);
6479 return map;
6480 error:
6481 isl_map_free(map);
6482 return NULL;
6485 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6486 enum isl_dim_type type, unsigned pos, int value)
6488 return map_bound_si(map, type, pos, value, 0);
6491 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6492 enum isl_dim_type type, unsigned pos, int value)
6494 return map_bound_si(map, type, pos, value, 1);
6497 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6498 enum isl_dim_type type, unsigned pos, int value)
6500 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6501 type, pos, value));
6504 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6505 enum isl_dim_type type, unsigned pos, int value)
6507 return isl_map_upper_bound_si(set, type, pos, value);
6510 /* Bound the given variable of "bmap" from below (or above is "upper"
6511 * is set) to "value".
6513 static __isl_give isl_basic_map *basic_map_bound(
6514 __isl_take isl_basic_map *bmap,
6515 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6517 int j;
6519 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6520 return isl_basic_map_free(bmap);
6521 pos += isl_basic_map_offset(bmap, type);
6522 bmap = isl_basic_map_cow(bmap);
6523 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6524 j = isl_basic_map_alloc_inequality(bmap);
6525 if (j < 0)
6526 goto error;
6527 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6528 if (upper) {
6529 isl_int_set_si(bmap->ineq[j][pos], -1);
6530 isl_int_set(bmap->ineq[j][0], value);
6531 } else {
6532 isl_int_set_si(bmap->ineq[j][pos], 1);
6533 isl_int_neg(bmap->ineq[j][0], value);
6535 bmap = isl_basic_map_simplify(bmap);
6536 return isl_basic_map_finalize(bmap);
6537 error:
6538 isl_basic_map_free(bmap);
6539 return NULL;
6542 /* Bound the given variable of "map" from below (or above is "upper"
6543 * is set) to "value".
6545 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6546 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6548 int i;
6550 map = isl_map_cow(map);
6551 if (isl_map_check_range(map, type, pos, 1) < 0)
6552 return isl_map_free(map);
6553 for (i = map->n - 1; i >= 0; --i) {
6554 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6555 map = remove_if_empty(map, i);
6556 if (!map)
6557 return NULL;
6559 map = isl_map_unmark_normalized(map);
6560 return map;
6563 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6564 enum isl_dim_type type, unsigned pos, isl_int value)
6566 return map_bound(map, type, pos, value, 0);
6569 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6570 enum isl_dim_type type, unsigned pos, isl_int value)
6572 return map_bound(map, type, pos, value, 1);
6575 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6576 enum isl_dim_type type, unsigned pos, isl_int value)
6578 return isl_map_lower_bound(set, type, pos, value);
6581 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6582 enum isl_dim_type type, unsigned pos, isl_int value)
6584 return isl_map_upper_bound(set, type, pos, value);
6587 /* Force the values of the variable at position "pos" of type "type" of "set"
6588 * to be no smaller than "value".
6590 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6591 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6593 if (!value)
6594 goto error;
6595 if (!isl_val_is_int(value))
6596 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6597 "expecting integer value", goto error);
6598 set = isl_set_lower_bound(set, type, pos, value->n);
6599 isl_val_free(value);
6600 return set;
6601 error:
6602 isl_val_free(value);
6603 isl_set_free(set);
6604 return NULL;
6607 /* Force the values of the variable at position "pos" of type "type" of "set"
6608 * to be no greater than "value".
6610 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6611 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6613 if (!value)
6614 goto error;
6615 if (!isl_val_is_int(value))
6616 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6617 "expecting integer value", goto error);
6618 set = isl_set_upper_bound(set, type, pos, value->n);
6619 isl_val_free(value);
6620 return set;
6621 error:
6622 isl_val_free(value);
6623 isl_set_free(set);
6624 return NULL;
6627 /* Bound the given variable of "bset" from below (or above is "upper"
6628 * is set) to "value".
6630 static __isl_give isl_basic_set *isl_basic_set_bound(
6631 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6632 isl_int value, int upper)
6634 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6635 type, pos, value, upper));
6638 /* Bound the given variable of "bset" from below (or above is "upper"
6639 * is set) to "value".
6641 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6642 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6643 __isl_take isl_val *value, int upper)
6645 if (!value)
6646 goto error;
6647 if (!isl_val_is_int(value))
6648 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6649 "expecting integer value", goto error);
6650 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6651 isl_val_free(value);
6652 return bset;
6653 error:
6654 isl_val_free(value);
6655 isl_basic_set_free(bset);
6656 return NULL;
6659 /* Bound the given variable of "bset" from below to "value".
6661 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6662 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6663 __isl_take isl_val *value)
6665 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6668 /* Bound the given variable of "bset" from above to "value".
6670 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6671 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6672 __isl_take isl_val *value)
6674 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6677 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6679 int i;
6681 map = isl_map_cow(map);
6682 if (!map)
6683 return NULL;
6685 map->dim = isl_space_reverse(map->dim);
6686 if (!map->dim)
6687 goto error;
6688 for (i = 0; i < map->n; ++i) {
6689 map->p[i] = isl_basic_map_reverse(map->p[i]);
6690 if (!map->p[i])
6691 goto error;
6693 map = isl_map_unmark_normalized(map);
6694 return map;
6695 error:
6696 isl_map_free(map);
6697 return NULL;
6700 #undef TYPE
6701 #define TYPE isl_pw_multi_aff
6702 #undef SUFFIX
6703 #define SUFFIX _pw_multi_aff
6704 #undef EMPTY
6705 #define EMPTY isl_pw_multi_aff_empty
6706 #undef ADD
6707 #define ADD isl_pw_multi_aff_union_add
6708 #include "isl_map_lexopt_templ.c"
6710 /* Given a map "map", compute the lexicographically minimal
6711 * (or maximal) image element for each domain element in dom,
6712 * in the form of an isl_pw_multi_aff.
6713 * If "empty" is not NULL, then set *empty to those elements in dom that
6714 * do not have an image element.
6715 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6716 * should be computed over the domain of "map". "empty" is also NULL
6717 * in this case.
6719 * We first compute the lexicographically minimal or maximal element
6720 * in the first basic map. This results in a partial solution "res"
6721 * and a subset "todo" of dom that still need to be handled.
6722 * We then consider each of the remaining maps in "map" and successively
6723 * update both "res" and "todo".
6724 * If "empty" is NULL, then the todo sets are not needed and therefore
6725 * also not computed.
6727 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6728 __isl_take isl_map *map, __isl_take isl_set *dom,
6729 __isl_give isl_set **empty, unsigned flags)
6731 int i;
6732 int full;
6733 isl_pw_multi_aff *res;
6734 isl_set *todo;
6736 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6737 if (!map || (!full && !dom))
6738 goto error;
6740 if (isl_map_plain_is_empty(map)) {
6741 if (empty)
6742 *empty = dom;
6743 else
6744 isl_set_free(dom);
6745 return isl_pw_multi_aff_from_map(map);
6748 res = basic_map_partial_lexopt_pw_multi_aff(
6749 isl_basic_map_copy(map->p[0]),
6750 isl_set_copy(dom), empty, flags);
6752 if (empty)
6753 todo = *empty;
6754 for (i = 1; i < map->n; ++i) {
6755 isl_pw_multi_aff *res_i;
6757 res_i = basic_map_partial_lexopt_pw_multi_aff(
6758 isl_basic_map_copy(map->p[i]),
6759 isl_set_copy(dom), empty, flags);
6761 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6762 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6763 else
6764 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6766 if (empty)
6767 todo = isl_set_intersect(todo, *empty);
6770 isl_set_free(dom);
6771 isl_map_free(map);
6773 if (empty)
6774 *empty = todo;
6776 return res;
6777 error:
6778 if (empty)
6779 *empty = NULL;
6780 isl_set_free(dom);
6781 isl_map_free(map);
6782 return NULL;
6785 #undef TYPE
6786 #define TYPE isl_map
6787 #undef SUFFIX
6788 #define SUFFIX
6789 #undef EMPTY
6790 #define EMPTY isl_map_empty
6791 #undef ADD
6792 #define ADD isl_map_union_disjoint
6793 #include "isl_map_lexopt_templ.c"
6795 /* Given a map "map", compute the lexicographically minimal
6796 * (or maximal) image element for each domain element in "dom",
6797 * in the form of an isl_map.
6798 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6799 * do not have an image element.
6800 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6801 * should be computed over the domain of "map". "empty" is also NULL
6802 * in this case.
6804 * If the input consists of more than one disjunct, then first
6805 * compute the desired result in the form of an isl_pw_multi_aff and
6806 * then convert that into an isl_map.
6808 * This function used to have an explicit implementation in terms
6809 * of isl_maps, but it would continually intersect the domains of
6810 * partial results with the complement of the domain of the next
6811 * partial solution, potentially leading to an explosion in the number
6812 * of disjuncts if there are several disjuncts in the input.
6813 * An even earlier implementation of this function would look for
6814 * better results in the domain of the partial result and for extra
6815 * results in the complement of this domain, which would lead to
6816 * even more splintering.
6818 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6819 __isl_take isl_map *map, __isl_take isl_set *dom,
6820 __isl_give isl_set **empty, unsigned flags)
6822 int full;
6823 struct isl_map *res;
6824 isl_pw_multi_aff *pma;
6826 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6827 if (!map || (!full && !dom))
6828 goto error;
6830 if (isl_map_plain_is_empty(map)) {
6831 if (empty)
6832 *empty = dom;
6833 else
6834 isl_set_free(dom);
6835 return map;
6838 if (map->n == 1) {
6839 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6840 dom, empty, flags);
6841 isl_map_free(map);
6842 return res;
6845 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6846 flags);
6847 return isl_map_from_pw_multi_aff(pma);
6848 error:
6849 if (empty)
6850 *empty = NULL;
6851 isl_set_free(dom);
6852 isl_map_free(map);
6853 return NULL;
6856 __isl_give isl_map *isl_map_partial_lexmax(
6857 __isl_take isl_map *map, __isl_take isl_set *dom,
6858 __isl_give isl_set **empty)
6860 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6863 __isl_give isl_map *isl_map_partial_lexmin(
6864 __isl_take isl_map *map, __isl_take isl_set *dom,
6865 __isl_give isl_set **empty)
6867 return isl_map_partial_lexopt(map, dom, empty, 0);
6870 __isl_give isl_set *isl_set_partial_lexmin(
6871 __isl_take isl_set *set, __isl_take isl_set *dom,
6872 __isl_give isl_set **empty)
6874 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6875 dom, empty));
6878 __isl_give isl_set *isl_set_partial_lexmax(
6879 __isl_take isl_set *set, __isl_take isl_set *dom,
6880 __isl_give isl_set **empty)
6882 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6883 dom, empty));
6886 /* Compute the lexicographic minimum (or maximum if "flags" includes
6887 * ISL_OPT_MAX) of "bset" over its parametric domain.
6889 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6890 unsigned flags)
6892 return isl_basic_map_lexopt(bset, flags);
6895 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6897 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6900 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6902 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6905 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6907 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6910 /* Compute the lexicographic minimum of "bset" over its parametric domain
6911 * for the purpose of quantifier elimination.
6912 * That is, find an explicit representation for all the existentially
6913 * quantified variables in "bset" by computing their lexicographic
6914 * minimum.
6916 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6917 __isl_take isl_basic_set *bset)
6919 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6922 /* Given a basic map with one output dimension, compute the minimum or
6923 * maximum of that dimension as an isl_pw_aff.
6925 * Compute the optimum as a lexicographic optimum over the single
6926 * output dimension and extract the single isl_pw_aff from the result.
6928 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6929 int max)
6931 isl_pw_multi_aff *pma;
6932 isl_pw_aff *pwaff;
6934 bmap = isl_basic_map_copy(bmap);
6935 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6936 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6937 isl_pw_multi_aff_free(pma);
6939 return pwaff;
6942 /* Compute the minimum or maximum of the given output dimension
6943 * as a function of the parameters and the input dimensions,
6944 * but independently of the other output dimensions.
6946 * We first project out the other output dimension and then compute
6947 * the "lexicographic" maximum in each basic map, combining the results
6948 * using isl_pw_aff_union_max.
6950 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6951 int max)
6953 int i;
6954 isl_pw_aff *pwaff;
6955 unsigned n_out;
6957 n_out = isl_map_dim(map, isl_dim_out);
6958 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6959 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6960 if (!map)
6961 return NULL;
6963 if (map->n == 0) {
6964 isl_space *space = isl_map_get_space(map);
6965 isl_map_free(map);
6966 return isl_pw_aff_empty(space);
6969 pwaff = basic_map_dim_opt(map->p[0], max);
6970 for (i = 1; i < map->n; ++i) {
6971 isl_pw_aff *pwaff_i;
6973 pwaff_i = basic_map_dim_opt(map->p[i], max);
6974 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6977 isl_map_free(map);
6979 return pwaff;
6982 /* Compute the minimum of the given output dimension as a function of the
6983 * parameters and input dimensions, but independently of
6984 * the other output dimensions.
6986 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6988 return map_dim_opt(map, pos, 0);
6991 /* Compute the maximum of the given output dimension as a function of the
6992 * parameters and input dimensions, but independently of
6993 * the other output dimensions.
6995 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6997 return map_dim_opt(map, pos, 1);
7000 /* Compute the minimum or maximum of the given set dimension
7001 * as a function of the parameters,
7002 * but independently of the other set dimensions.
7004 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7005 int max)
7007 return map_dim_opt(set, pos, max);
7010 /* Compute the maximum of the given set dimension as a function of the
7011 * parameters, but independently of the other set dimensions.
7013 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7015 return set_dim_opt(set, pos, 1);
7018 /* Compute the minimum of the given set dimension as a function of the
7019 * parameters, but independently of the other set dimensions.
7021 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7023 return set_dim_opt(set, pos, 0);
7026 /* Apply a preimage specified by "mat" on the parameters of "bset".
7027 * bset is assumed to have only parameters and divs.
7029 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7030 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7032 unsigned nparam;
7034 if (!bset || !mat)
7035 goto error;
7037 bset->dim = isl_space_cow(bset->dim);
7038 if (!bset->dim)
7039 goto error;
7041 nparam = isl_basic_set_dim(bset, isl_dim_param);
7043 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7045 bset->dim->nparam = 0;
7046 bset->dim->n_out = nparam;
7047 bset = isl_basic_set_preimage(bset, mat);
7048 if (bset) {
7049 bset->dim->nparam = bset->dim->n_out;
7050 bset->dim->n_out = 0;
7052 return bset;
7053 error:
7054 isl_mat_free(mat);
7055 isl_basic_set_free(bset);
7056 return NULL;
7059 /* Apply a preimage specified by "mat" on the parameters of "set".
7060 * set is assumed to have only parameters and divs.
7062 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7063 __isl_take isl_mat *mat)
7065 isl_space *space;
7066 unsigned nparam;
7068 if (!set || !mat)
7069 goto error;
7071 nparam = isl_set_dim(set, isl_dim_param);
7073 if (mat->n_row != 1 + nparam)
7074 isl_die(isl_set_get_ctx(set), isl_error_internal,
7075 "unexpected number of rows", goto error);
7077 space = isl_set_get_space(set);
7078 space = isl_space_move_dims(space, isl_dim_set, 0,
7079 isl_dim_param, 0, nparam);
7080 set = isl_set_reset_space(set, space);
7081 set = isl_set_preimage(set, mat);
7082 nparam = isl_set_dim(set, isl_dim_out);
7083 space = isl_set_get_space(set);
7084 space = isl_space_move_dims(space, isl_dim_param, 0,
7085 isl_dim_out, 0, nparam);
7086 set = isl_set_reset_space(set, space);
7087 return set;
7088 error:
7089 isl_mat_free(mat);
7090 isl_set_free(set);
7091 return NULL;
7094 /* Intersect the basic set "bset" with the affine space specified by the
7095 * equalities in "eq".
7097 static __isl_give isl_basic_set *basic_set_append_equalities(
7098 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7100 int i, k;
7101 unsigned len;
7103 if (!bset || !eq)
7104 goto error;
7106 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7107 eq->n_row, 0);
7108 if (!bset)
7109 goto error;
7111 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7112 for (i = 0; i < eq->n_row; ++i) {
7113 k = isl_basic_set_alloc_equality(bset);
7114 if (k < 0)
7115 goto error;
7116 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7117 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7119 isl_mat_free(eq);
7121 bset = isl_basic_set_gauss(bset, NULL);
7122 bset = isl_basic_set_finalize(bset);
7124 return bset;
7125 error:
7126 isl_mat_free(eq);
7127 isl_basic_set_free(bset);
7128 return NULL;
7131 /* Intersect the set "set" with the affine space specified by the
7132 * equalities in "eq".
7134 static struct isl_set *set_append_equalities(struct isl_set *set,
7135 struct isl_mat *eq)
7137 int i;
7139 if (!set || !eq)
7140 goto error;
7142 for (i = 0; i < set->n; ++i) {
7143 set->p[i] = basic_set_append_equalities(set->p[i],
7144 isl_mat_copy(eq));
7145 if (!set->p[i])
7146 goto error;
7148 isl_mat_free(eq);
7149 return set;
7150 error:
7151 isl_mat_free(eq);
7152 isl_set_free(set);
7153 return NULL;
7156 /* Given a basic set "bset" that only involves parameters and existentially
7157 * quantified variables, return the index of the first equality
7158 * that only involves parameters. If there is no such equality then
7159 * return bset->n_eq.
7161 * This function assumes that isl_basic_set_gauss has been called on "bset".
7163 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7165 int i, j;
7166 unsigned nparam, n_div;
7168 if (!bset)
7169 return -1;
7171 nparam = isl_basic_set_dim(bset, isl_dim_param);
7172 n_div = isl_basic_set_dim(bset, isl_dim_div);
7174 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7175 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7176 ++i;
7179 return i;
7182 /* Compute an explicit representation for the existentially quantified
7183 * variables in "bset" by computing the "minimal value" of the set
7184 * variables. Since there are no set variables, the computation of
7185 * the minimal value essentially computes an explicit representation
7186 * of the non-empty part(s) of "bset".
7188 * The input only involves parameters and existentially quantified variables.
7189 * All equalities among parameters have been removed.
7191 * Since the existentially quantified variables in the result are in general
7192 * going to be different from those in the input, we first replace
7193 * them by the minimal number of variables based on their equalities.
7194 * This should simplify the parametric integer programming.
7196 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7198 isl_morph *morph1, *morph2;
7199 isl_set *set;
7200 unsigned n;
7202 if (!bset)
7203 return NULL;
7204 if (bset->n_eq == 0)
7205 return isl_basic_set_lexmin_compute_divs(bset);
7207 morph1 = isl_basic_set_parameter_compression(bset);
7208 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7209 bset = isl_basic_set_lift(bset);
7210 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7211 bset = isl_morph_basic_set(morph2, bset);
7212 n = isl_basic_set_dim(bset, isl_dim_set);
7213 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7215 set = isl_basic_set_lexmin_compute_divs(bset);
7217 set = isl_morph_set(isl_morph_inverse(morph1), set);
7219 return set;
7222 /* Project the given basic set onto its parameter domain, possibly introducing
7223 * new, explicit, existential variables in the constraints.
7224 * The input has parameters and (possibly implicit) existential variables.
7225 * The output has the same parameters, but only
7226 * explicit existentially quantified variables.
7228 * The actual projection is performed by pip, but pip doesn't seem
7229 * to like equalities very much, so we first remove the equalities
7230 * among the parameters by performing a variable compression on
7231 * the parameters. Afterward, an inverse transformation is performed
7232 * and the equalities among the parameters are inserted back in.
7234 * The variable compression on the parameters may uncover additional
7235 * equalities that were only implicit before. We therefore check
7236 * if there are any new parameter equalities in the result and
7237 * if so recurse. The removal of parameter equalities is required
7238 * for the parameter compression performed by base_compute_divs.
7240 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7242 int i;
7243 struct isl_mat *eq;
7244 struct isl_mat *T, *T2;
7245 struct isl_set *set;
7246 unsigned nparam;
7248 bset = isl_basic_set_cow(bset);
7249 if (!bset)
7250 return NULL;
7252 if (bset->n_eq == 0)
7253 return base_compute_divs(bset);
7255 bset = isl_basic_set_gauss(bset, NULL);
7256 if (!bset)
7257 return NULL;
7258 if (isl_basic_set_plain_is_empty(bset))
7259 return isl_set_from_basic_set(bset);
7261 i = first_parameter_equality(bset);
7262 if (i == bset->n_eq)
7263 return base_compute_divs(bset);
7265 nparam = isl_basic_set_dim(bset, isl_dim_param);
7266 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7267 0, 1 + nparam);
7268 eq = isl_mat_cow(eq);
7269 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7270 if (T && T->n_col == 0) {
7271 isl_mat_free(T);
7272 isl_mat_free(T2);
7273 isl_mat_free(eq);
7274 bset = isl_basic_set_set_to_empty(bset);
7275 return isl_set_from_basic_set(bset);
7277 bset = basic_set_parameter_preimage(bset, T);
7279 i = first_parameter_equality(bset);
7280 if (!bset)
7281 set = NULL;
7282 else if (i == bset->n_eq)
7283 set = base_compute_divs(bset);
7284 else
7285 set = parameter_compute_divs(bset);
7286 set = set_parameter_preimage(set, T2);
7287 set = set_append_equalities(set, eq);
7288 return set;
7291 /* Insert the divs from "ls" before those of "bmap".
7293 * The number of columns is not changed, which means that the last
7294 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7295 * The caller is responsible for removing the same number of dimensions
7296 * from the space of "bmap".
7298 static __isl_give isl_basic_map *insert_divs_from_local_space(
7299 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7301 int i;
7302 int n_div;
7303 int old_n_div;
7305 n_div = isl_local_space_dim(ls, isl_dim_div);
7306 if (n_div == 0)
7307 return bmap;
7309 old_n_div = bmap->n_div;
7310 bmap = insert_div_rows(bmap, n_div);
7311 if (!bmap)
7312 return NULL;
7314 for (i = 0; i < n_div; ++i) {
7315 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7316 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7319 return bmap;
7322 /* Replace the space of "bmap" by the space and divs of "ls".
7324 * If "ls" has any divs, then we simplify the result since we may
7325 * have discovered some additional equalities that could simplify
7326 * the div expressions.
7328 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7329 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7331 int n_div;
7333 bmap = isl_basic_map_cow(bmap);
7334 if (!bmap || !ls)
7335 goto error;
7337 n_div = isl_local_space_dim(ls, isl_dim_div);
7338 bmap = insert_divs_from_local_space(bmap, ls);
7339 if (!bmap)
7340 goto error;
7342 isl_space_free(bmap->dim);
7343 bmap->dim = isl_local_space_get_space(ls);
7344 if (!bmap->dim)
7345 goto error;
7347 isl_local_space_free(ls);
7348 if (n_div > 0)
7349 bmap = isl_basic_map_simplify(bmap);
7350 bmap = isl_basic_map_finalize(bmap);
7351 return bmap;
7352 error:
7353 isl_basic_map_free(bmap);
7354 isl_local_space_free(ls);
7355 return NULL;
7358 /* Replace the space of "map" by the space and divs of "ls".
7360 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7361 __isl_take isl_local_space *ls)
7363 int i;
7365 map = isl_map_cow(map);
7366 if (!map || !ls)
7367 goto error;
7369 for (i = 0; i < map->n; ++i) {
7370 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7371 isl_local_space_copy(ls));
7372 if (!map->p[i])
7373 goto error;
7375 isl_space_free(map->dim);
7376 map->dim = isl_local_space_get_space(ls);
7377 if (!map->dim)
7378 goto error;
7380 isl_local_space_free(ls);
7381 return map;
7382 error:
7383 isl_local_space_free(ls);
7384 isl_map_free(map);
7385 return NULL;
7388 /* Compute an explicit representation for the existentially
7389 * quantified variables for which do not know any explicit representation yet.
7391 * We first sort the existentially quantified variables so that the
7392 * existentially quantified variables for which we already have an explicit
7393 * representation are placed before those for which we do not.
7394 * The input dimensions, the output dimensions and the existentially
7395 * quantified variables for which we already have an explicit
7396 * representation are then turned into parameters.
7397 * compute_divs returns a map with the same parameters and
7398 * no input or output dimensions and the dimension specification
7399 * is reset to that of the input, including the existentially quantified
7400 * variables for which we already had an explicit representation.
7402 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7404 struct isl_basic_set *bset;
7405 struct isl_set *set;
7406 struct isl_map *map;
7407 isl_space *space;
7408 isl_local_space *ls;
7409 unsigned nparam;
7410 unsigned n_in;
7411 unsigned n_out;
7412 int n_known;
7413 int i;
7415 bmap = isl_basic_map_sort_divs(bmap);
7416 bmap = isl_basic_map_cow(bmap);
7417 if (!bmap)
7418 return NULL;
7420 n_known = isl_basic_map_first_unknown_div(bmap);
7421 if (n_known < 0)
7422 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7424 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7425 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7426 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7427 space = isl_space_set_alloc(bmap->ctx,
7428 nparam + n_in + n_out + n_known, 0);
7429 if (!space)
7430 goto error;
7432 ls = isl_basic_map_get_local_space(bmap);
7433 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7434 n_known, bmap->n_div - n_known);
7435 if (n_known > 0) {
7436 for (i = n_known; i < bmap->n_div; ++i)
7437 swap_div(bmap, i - n_known, i);
7438 bmap->n_div -= n_known;
7439 bmap->extra -= n_known;
7441 bmap = isl_basic_map_reset_space(bmap, space);
7442 bset = bset_from_bmap(bmap);
7444 set = parameter_compute_divs(bset);
7445 map = set_to_map(set);
7446 map = replace_space_by_local_space(map, ls);
7448 return map;
7449 error:
7450 isl_basic_map_free(bmap);
7451 return NULL;
7454 /* Remove the explicit representation of local variable "div",
7455 * if there is any.
7457 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7458 __isl_take isl_basic_map *bmap, int div)
7460 isl_bool unknown;
7462 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7463 if (unknown < 0)
7464 return isl_basic_map_free(bmap);
7465 if (unknown)
7466 return bmap;
7468 bmap = isl_basic_map_cow(bmap);
7469 if (!bmap)
7470 return NULL;
7471 isl_int_set_si(bmap->div[div][0], 0);
7472 return bmap;
7475 /* Is local variable "div" of "bmap" marked as not having an explicit
7476 * representation?
7477 * Note that even if "div" is not marked in this way and therefore
7478 * has an explicit representation, this representation may still
7479 * depend (indirectly) on other local variables that do not
7480 * have an explicit representation.
7482 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7483 int div)
7485 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7486 return isl_bool_error;
7487 return isl_int_is_zero(bmap->div[div][0]);
7490 /* Return the position of the first local variable that does not
7491 * have an explicit representation.
7492 * Return the total number of local variables if they all have
7493 * an explicit representation.
7494 * Return -1 on error.
7496 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7498 int i;
7500 if (!bmap)
7501 return -1;
7503 for (i = 0; i < bmap->n_div; ++i) {
7504 if (!isl_basic_map_div_is_known(bmap, i))
7505 return i;
7507 return bmap->n_div;
7510 /* Return the position of the first local variable that does not
7511 * have an explicit representation.
7512 * Return the total number of local variables if they all have
7513 * an explicit representation.
7514 * Return -1 on error.
7516 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7518 return isl_basic_map_first_unknown_div(bset);
7521 /* Does "bmap" have an explicit representation for all local variables?
7523 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7525 int first, n;
7527 n = isl_basic_map_dim(bmap, isl_dim_div);
7528 first = isl_basic_map_first_unknown_div(bmap);
7529 if (first < 0)
7530 return isl_bool_error;
7531 return first == n;
7534 /* Do all basic maps in "map" have an explicit representation
7535 * for all local variables?
7537 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7539 int i;
7541 if (!map)
7542 return isl_bool_error;
7544 for (i = 0; i < map->n; ++i) {
7545 int known = isl_basic_map_divs_known(map->p[i]);
7546 if (known <= 0)
7547 return known;
7550 return isl_bool_true;
7553 /* If bmap contains any unknown divs, then compute explicit
7554 * expressions for them. However, this computation may be
7555 * quite expensive, so first try to remove divs that aren't
7556 * strictly needed.
7558 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7560 int known;
7561 struct isl_map *map;
7563 known = isl_basic_map_divs_known(bmap);
7564 if (known < 0)
7565 goto error;
7566 if (known)
7567 return isl_map_from_basic_map(bmap);
7569 bmap = isl_basic_map_drop_redundant_divs(bmap);
7571 known = isl_basic_map_divs_known(bmap);
7572 if (known < 0)
7573 goto error;
7574 if (known)
7575 return isl_map_from_basic_map(bmap);
7577 map = compute_divs(bmap);
7578 return map;
7579 error:
7580 isl_basic_map_free(bmap);
7581 return NULL;
7584 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7586 int i;
7587 int known;
7588 struct isl_map *res;
7590 if (!map)
7591 return NULL;
7592 if (map->n == 0)
7593 return map;
7595 known = isl_map_divs_known(map);
7596 if (known < 0) {
7597 isl_map_free(map);
7598 return NULL;
7600 if (known)
7601 return map;
7603 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7604 for (i = 1 ; i < map->n; ++i) {
7605 struct isl_map *r2;
7606 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7607 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7608 res = isl_map_union_disjoint(res, r2);
7609 else
7610 res = isl_map_union(res, r2);
7612 isl_map_free(map);
7614 return res;
7617 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7619 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7622 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7624 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7627 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7629 int i;
7630 struct isl_set *set;
7632 if (!map)
7633 goto error;
7635 map = isl_map_cow(map);
7636 if (!map)
7637 return NULL;
7639 set = set_from_map(map);
7640 set->dim = isl_space_domain(set->dim);
7641 if (!set->dim)
7642 goto error;
7643 for (i = 0; i < map->n; ++i) {
7644 set->p[i] = isl_basic_map_domain(map->p[i]);
7645 if (!set->p[i])
7646 goto error;
7648 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7649 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7650 return set;
7651 error:
7652 isl_map_free(map);
7653 return NULL;
7656 /* Return the union of "map1" and "map2", where we assume for now that
7657 * "map1" and "map2" are disjoint. Note that the basic maps inside
7658 * "map1" or "map2" may not be disjoint from each other.
7659 * Also note that this function is also called from isl_map_union,
7660 * which takes care of handling the situation where "map1" and "map2"
7661 * may not be disjoint.
7663 * If one of the inputs is empty, we can simply return the other input.
7664 * Similarly, if one of the inputs is universal, then it is equal to the union.
7666 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7667 __isl_take isl_map *map2)
7669 int i;
7670 unsigned flags = 0;
7671 struct isl_map *map = NULL;
7672 int is_universe;
7674 if (!map1 || !map2)
7675 goto error;
7677 if (!isl_space_is_equal(map1->dim, map2->dim))
7678 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7679 "spaces don't match", goto error);
7681 if (map1->n == 0) {
7682 isl_map_free(map1);
7683 return map2;
7685 if (map2->n == 0) {
7686 isl_map_free(map2);
7687 return map1;
7690 is_universe = isl_map_plain_is_universe(map1);
7691 if (is_universe < 0)
7692 goto error;
7693 if (is_universe) {
7694 isl_map_free(map2);
7695 return map1;
7698 is_universe = isl_map_plain_is_universe(map2);
7699 if (is_universe < 0)
7700 goto error;
7701 if (is_universe) {
7702 isl_map_free(map1);
7703 return map2;
7706 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7707 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7708 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7710 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7711 map1->n + map2->n, flags);
7712 if (!map)
7713 goto error;
7714 for (i = 0; i < map1->n; ++i) {
7715 map = isl_map_add_basic_map(map,
7716 isl_basic_map_copy(map1->p[i]));
7717 if (!map)
7718 goto error;
7720 for (i = 0; i < map2->n; ++i) {
7721 map = isl_map_add_basic_map(map,
7722 isl_basic_map_copy(map2->p[i]));
7723 if (!map)
7724 goto error;
7726 isl_map_free(map1);
7727 isl_map_free(map2);
7728 return map;
7729 error:
7730 isl_map_free(map);
7731 isl_map_free(map1);
7732 isl_map_free(map2);
7733 return NULL;
7736 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7737 * guaranteed to be disjoint by the caller.
7739 * Note that this functions is called from within isl_map_make_disjoint,
7740 * so we have to be careful not to touch the constraints of the inputs
7741 * in any way.
7743 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7744 __isl_take isl_map *map2)
7746 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7749 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7750 * not be disjoint. The parameters are assumed to have been aligned.
7752 * We currently simply call map_union_disjoint, the internal operation
7753 * of which does not really depend on the inputs being disjoint.
7754 * If the result contains more than one basic map, then we clear
7755 * the disjoint flag since the result may contain basic maps from
7756 * both inputs and these are not guaranteed to be disjoint.
7758 * As a special case, if "map1" and "map2" are obviously equal,
7759 * then we simply return "map1".
7761 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7762 __isl_take isl_map *map2)
7764 int equal;
7766 if (!map1 || !map2)
7767 goto error;
7769 equal = isl_map_plain_is_equal(map1, map2);
7770 if (equal < 0)
7771 goto error;
7772 if (equal) {
7773 isl_map_free(map2);
7774 return map1;
7777 map1 = map_union_disjoint(map1, map2);
7778 if (!map1)
7779 return NULL;
7780 if (map1->n > 1)
7781 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7782 return map1;
7783 error:
7784 isl_map_free(map1);
7785 isl_map_free(map2);
7786 return NULL;
7789 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7790 * not be disjoint.
7792 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7793 __isl_take isl_map *map2)
7795 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7798 __isl_give isl_set *isl_set_union_disjoint(
7799 __isl_take isl_set *set1, __isl_take isl_set *set2)
7801 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7802 set_to_map(set2)));
7805 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7807 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7810 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7811 * the results.
7813 * "map" and "set" are assumed to be compatible and non-NULL.
7815 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7816 __isl_take isl_set *set,
7817 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7818 __isl_take isl_basic_set *bset))
7820 unsigned flags = 0;
7821 struct isl_map *result;
7822 int i, j;
7824 if (isl_set_plain_is_universe(set)) {
7825 isl_set_free(set);
7826 return map;
7829 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7830 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7831 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7833 result = isl_map_alloc_space(isl_space_copy(map->dim),
7834 map->n * set->n, flags);
7835 for (i = 0; result && i < map->n; ++i)
7836 for (j = 0; j < set->n; ++j) {
7837 result = isl_map_add_basic_map(result,
7838 fn(isl_basic_map_copy(map->p[i]),
7839 isl_basic_set_copy(set->p[j])));
7840 if (!result)
7841 break;
7844 isl_map_free(map);
7845 isl_set_free(set);
7846 return result;
7849 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7850 __isl_take isl_set *set)
7852 isl_bool ok;
7854 ok = isl_map_compatible_range(map, set);
7855 if (ok < 0)
7856 goto error;
7857 if (!ok)
7858 isl_die(set->ctx, isl_error_invalid,
7859 "incompatible spaces", goto error);
7861 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7862 error:
7863 isl_map_free(map);
7864 isl_set_free(set);
7865 return NULL;
7868 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7869 __isl_take isl_set *set)
7871 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7874 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7875 __isl_take isl_set *set)
7877 isl_bool ok;
7879 ok = isl_map_compatible_domain(map, set);
7880 if (ok < 0)
7881 goto error;
7882 if (!ok)
7883 isl_die(set->ctx, isl_error_invalid,
7884 "incompatible spaces", goto error);
7886 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7887 error:
7888 isl_map_free(map);
7889 isl_set_free(set);
7890 return NULL;
7893 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7894 __isl_take isl_set *set)
7896 return isl_map_align_params_map_map_and(map, set,
7897 &map_intersect_domain);
7900 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7901 * in the space B -> C, return the intersection.
7902 * The parameters are assumed to have been aligned.
7904 * The map "factor" is first extended to a map living in the space
7905 * [A -> B] -> C and then a regular intersection is computed.
7907 static __isl_give isl_map *map_intersect_domain_factor_range(
7908 __isl_take isl_map *map, __isl_take isl_map *factor)
7910 isl_space *space;
7911 isl_map *ext_factor;
7913 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7914 ext_factor = isl_map_universe(space);
7915 ext_factor = isl_map_domain_product(ext_factor, factor);
7916 return map_intersect(map, ext_factor);
7919 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7920 * in the space B -> C, return the intersection.
7922 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7923 __isl_take isl_map *map, __isl_take isl_map *factor)
7925 return isl_map_align_params_map_map_and(map, factor,
7926 &map_intersect_domain_factor_range);
7929 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7930 * in the space A -> C, return the intersection.
7932 * The map "factor" is first extended to a map living in the space
7933 * A -> [B -> C] and then a regular intersection is computed.
7935 static __isl_give isl_map *map_intersect_range_factor_range(
7936 __isl_take isl_map *map, __isl_take isl_map *factor)
7938 isl_space *space;
7939 isl_map *ext_factor;
7941 space = isl_space_range_factor_domain(isl_map_get_space(map));
7942 ext_factor = isl_map_universe(space);
7943 ext_factor = isl_map_range_product(ext_factor, factor);
7944 return isl_map_intersect(map, ext_factor);
7947 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7948 * in the space A -> C, return the intersection.
7950 __isl_give isl_map *isl_map_intersect_range_factor_range(
7951 __isl_take isl_map *map, __isl_take isl_map *factor)
7953 return isl_map_align_params_map_map_and(map, factor,
7954 &map_intersect_range_factor_range);
7957 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7958 __isl_take isl_map *map2)
7960 if (!map1 || !map2)
7961 goto error;
7962 map1 = isl_map_reverse(map1);
7963 map1 = isl_map_apply_range(map1, map2);
7964 return isl_map_reverse(map1);
7965 error:
7966 isl_map_free(map1);
7967 isl_map_free(map2);
7968 return NULL;
7971 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7972 __isl_take isl_map *map2)
7974 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7977 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7978 __isl_take isl_map *map2)
7980 isl_space *space;
7981 struct isl_map *result;
7982 int i, j;
7984 if (!map1 || !map2)
7985 goto error;
7987 space = isl_space_join(isl_space_copy(map1->dim),
7988 isl_space_copy(map2->dim));
7990 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7991 if (!result)
7992 goto error;
7993 for (i = 0; i < map1->n; ++i)
7994 for (j = 0; j < map2->n; ++j) {
7995 result = isl_map_add_basic_map(result,
7996 isl_basic_map_apply_range(
7997 isl_basic_map_copy(map1->p[i]),
7998 isl_basic_map_copy(map2->p[j])));
7999 if (!result)
8000 goto error;
8002 isl_map_free(map1);
8003 isl_map_free(map2);
8004 if (result && result->n <= 1)
8005 ISL_F_SET(result, ISL_MAP_DISJOINT);
8006 return result;
8007 error:
8008 isl_map_free(map1);
8009 isl_map_free(map2);
8010 return NULL;
8013 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8014 __isl_take isl_map *map2)
8016 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8020 * returns range - domain
8022 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8024 isl_space *target_space;
8025 struct isl_basic_set *bset;
8026 unsigned dim;
8027 unsigned nparam;
8028 int i;
8030 if (!bmap)
8031 goto error;
8032 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8033 bmap->dim, isl_dim_out),
8034 goto error);
8035 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8036 dim = isl_basic_map_dim(bmap, isl_dim_in);
8037 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8038 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8039 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8040 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8041 for (i = 0; i < dim; ++i) {
8042 int j = isl_basic_map_alloc_equality(bmap);
8043 if (j < 0) {
8044 bmap = isl_basic_map_free(bmap);
8045 break;
8047 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8048 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8049 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8050 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8052 bset = isl_basic_map_domain(bmap);
8053 bset = isl_basic_set_reset_space(bset, target_space);
8054 return bset;
8055 error:
8056 isl_basic_map_free(bmap);
8057 return NULL;
8061 * returns range - domain
8063 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8065 int i;
8066 isl_space *dim;
8067 struct isl_set *result;
8069 if (!map)
8070 return NULL;
8072 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8073 map->dim, isl_dim_out),
8074 goto error);
8075 dim = isl_map_get_space(map);
8076 dim = isl_space_domain(dim);
8077 result = isl_set_alloc_space(dim, map->n, 0);
8078 if (!result)
8079 goto error;
8080 for (i = 0; i < map->n; ++i)
8081 result = isl_set_add_basic_set(result,
8082 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8083 isl_map_free(map);
8084 return result;
8085 error:
8086 isl_map_free(map);
8087 return NULL;
8091 * returns [domain -> range] -> range - domain
8093 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8094 __isl_take isl_basic_map *bmap)
8096 int i, k;
8097 isl_space *space;
8098 isl_basic_map *domain;
8099 int nparam, n;
8100 unsigned total;
8102 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8103 bmap->dim, isl_dim_out))
8104 isl_die(bmap->ctx, isl_error_invalid,
8105 "domain and range don't match", goto error);
8107 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8108 n = isl_basic_map_dim(bmap, isl_dim_in);
8110 space = isl_basic_map_get_space(bmap);
8111 space = isl_space_from_range(isl_space_domain(space));
8112 domain = isl_basic_map_universe(space);
8114 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8115 bmap = isl_basic_map_apply_range(bmap, domain);
8116 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8118 total = isl_basic_map_total_dim(bmap);
8120 for (i = 0; i < n; ++i) {
8121 k = isl_basic_map_alloc_equality(bmap);
8122 if (k < 0)
8123 goto error;
8124 isl_seq_clr(bmap->eq[k], 1 + total);
8125 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8126 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8127 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8130 bmap = isl_basic_map_gauss(bmap, NULL);
8131 return isl_basic_map_finalize(bmap);
8132 error:
8133 isl_basic_map_free(bmap);
8134 return NULL;
8138 * returns [domain -> range] -> range - domain
8140 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8142 int i;
8143 isl_space *domain_space;
8145 if (!map)
8146 return NULL;
8148 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8149 map->dim, isl_dim_out))
8150 isl_die(map->ctx, isl_error_invalid,
8151 "domain and range don't match", goto error);
8153 map = isl_map_cow(map);
8154 if (!map)
8155 return NULL;
8157 domain_space = isl_space_domain(isl_map_get_space(map));
8158 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8159 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8160 if (!map->dim)
8161 goto error;
8162 for (i = 0; i < map->n; ++i) {
8163 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8164 if (!map->p[i])
8165 goto error;
8167 map = isl_map_unmark_normalized(map);
8168 return map;
8169 error:
8170 isl_map_free(map);
8171 return NULL;
8174 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8176 unsigned n_in, n_out;
8178 if (!space)
8179 return NULL;
8180 n_in = isl_space_dim(space, isl_dim_in);
8181 n_out = isl_space_dim(space, isl_dim_out);
8182 if (n_in != n_out)
8183 isl_die(space->ctx, isl_error_invalid,
8184 "number of input and output dimensions needs to be "
8185 "the same", goto error);
8186 return isl_basic_map_equal(space, n_in);
8187 error:
8188 isl_space_free(space);
8189 return NULL;
8192 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8194 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8197 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8199 isl_space *dim = isl_set_get_space(set);
8200 isl_map *id;
8201 id = isl_map_identity(isl_space_map_from_set(dim));
8202 return isl_map_intersect_range(id, set);
8205 /* Construct a basic set with all set dimensions having only non-negative
8206 * values.
8208 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8209 __isl_take isl_space *space)
8211 int i;
8212 unsigned nparam;
8213 unsigned dim;
8214 struct isl_basic_set *bset;
8216 if (!space)
8217 return NULL;
8218 nparam = space->nparam;
8219 dim = space->n_out;
8220 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8221 if (!bset)
8222 return NULL;
8223 for (i = 0; i < dim; ++i) {
8224 int k = isl_basic_set_alloc_inequality(bset);
8225 if (k < 0)
8226 goto error;
8227 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8228 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8230 return bset;
8231 error:
8232 isl_basic_set_free(bset);
8233 return NULL;
8236 /* Construct the half-space x_pos >= 0.
8238 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8239 int pos)
8241 int k;
8242 isl_basic_set *nonneg;
8244 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8245 k = isl_basic_set_alloc_inequality(nonneg);
8246 if (k < 0)
8247 goto error;
8248 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8249 isl_int_set_si(nonneg->ineq[k][pos], 1);
8251 return isl_basic_set_finalize(nonneg);
8252 error:
8253 isl_basic_set_free(nonneg);
8254 return NULL;
8257 /* Construct the half-space x_pos <= -1.
8259 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8260 int pos)
8262 int k;
8263 isl_basic_set *neg;
8265 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8266 k = isl_basic_set_alloc_inequality(neg);
8267 if (k < 0)
8268 goto error;
8269 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8270 isl_int_set_si(neg->ineq[k][0], -1);
8271 isl_int_set_si(neg->ineq[k][pos], -1);
8273 return isl_basic_set_finalize(neg);
8274 error:
8275 isl_basic_set_free(neg);
8276 return NULL;
8279 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8280 enum isl_dim_type type, unsigned first, unsigned n)
8282 int i;
8283 unsigned offset;
8284 isl_basic_set *nonneg;
8285 isl_basic_set *neg;
8287 if (n == 0)
8288 return set;
8290 if (isl_set_check_range(set, type, first, n) < 0)
8291 return isl_set_free(set);
8293 offset = pos(set->dim, type);
8294 for (i = 0; i < n; ++i) {
8295 nonneg = nonneg_halfspace(isl_set_get_space(set),
8296 offset + first + i);
8297 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8299 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8302 return set;
8305 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8306 int len,
8307 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8308 void *user)
8310 isl_set *half;
8312 if (!set)
8313 return isl_stat_error;
8314 if (isl_set_plain_is_empty(set)) {
8315 isl_set_free(set);
8316 return isl_stat_ok;
8318 if (first == len)
8319 return fn(set, signs, user);
8321 signs[first] = 1;
8322 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8323 1 + first));
8324 half = isl_set_intersect(half, isl_set_copy(set));
8325 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8326 goto error;
8328 signs[first] = -1;
8329 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8330 1 + first));
8331 half = isl_set_intersect(half, set);
8332 return foreach_orthant(half, signs, first + 1, len, fn, user);
8333 error:
8334 isl_set_free(set);
8335 return isl_stat_error;
8338 /* Call "fn" on the intersections of "set" with each of the orthants
8339 * (except for obviously empty intersections). The orthant is identified
8340 * by the signs array, with each entry having value 1 or -1 according
8341 * to the sign of the corresponding variable.
8343 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8344 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8345 void *user)
8347 unsigned nparam;
8348 unsigned nvar;
8349 int *signs;
8350 isl_stat r;
8352 if (!set)
8353 return isl_stat_error;
8354 if (isl_set_plain_is_empty(set))
8355 return isl_stat_ok;
8357 nparam = isl_set_dim(set, isl_dim_param);
8358 nvar = isl_set_dim(set, isl_dim_set);
8360 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8362 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8363 fn, user);
8365 free(signs);
8367 return r;
8370 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8372 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8375 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8376 __isl_keep isl_basic_map *bmap2)
8378 isl_bool is_subset;
8379 struct isl_map *map1;
8380 struct isl_map *map2;
8382 if (!bmap1 || !bmap2)
8383 return isl_bool_error;
8385 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8386 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8388 is_subset = isl_map_is_subset(map1, map2);
8390 isl_map_free(map1);
8391 isl_map_free(map2);
8393 return is_subset;
8396 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8397 __isl_keep isl_basic_set *bset2)
8399 return isl_basic_map_is_subset(bset1, bset2);
8402 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8403 __isl_keep isl_basic_map *bmap2)
8405 isl_bool is_subset;
8407 if (!bmap1 || !bmap2)
8408 return isl_bool_error;
8409 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8410 if (is_subset != isl_bool_true)
8411 return is_subset;
8412 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8413 return is_subset;
8416 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8417 __isl_keep isl_basic_set *bset2)
8419 return isl_basic_map_is_equal(
8420 bset_to_bmap(bset1), bset_to_bmap(bset2));
8423 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8425 int i;
8426 int is_empty;
8428 if (!map)
8429 return isl_bool_error;
8430 for (i = 0; i < map->n; ++i) {
8431 is_empty = isl_basic_map_is_empty(map->p[i]);
8432 if (is_empty < 0)
8433 return isl_bool_error;
8434 if (!is_empty)
8435 return isl_bool_false;
8437 return isl_bool_true;
8440 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8442 return map ? map->n == 0 : isl_bool_error;
8445 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8447 return set ? set->n == 0 : isl_bool_error;
8450 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8452 return isl_map_is_empty(set_to_map(set));
8455 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8456 __isl_keep isl_map *map2)
8458 if (!map1 || !map2)
8459 return isl_bool_error;
8461 return isl_space_is_equal(map1->dim, map2->dim);
8464 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8465 __isl_keep isl_set *set2)
8467 if (!set1 || !set2)
8468 return isl_bool_error;
8470 return isl_space_is_equal(set1->dim, set2->dim);
8473 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8475 isl_bool is_subset;
8477 if (!map1 || !map2)
8478 return isl_bool_error;
8479 is_subset = isl_map_is_subset(map1, map2);
8480 if (is_subset != isl_bool_true)
8481 return is_subset;
8482 is_subset = isl_map_is_subset(map2, map1);
8483 return is_subset;
8486 /* Is "map1" equal to "map2"?
8488 * First check if they are obviously equal.
8489 * If not, then perform a more detailed analysis.
8491 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8493 isl_bool equal;
8495 equal = isl_map_plain_is_equal(map1, map2);
8496 if (equal < 0 || equal)
8497 return equal;
8498 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8501 isl_bool isl_basic_map_is_strict_subset(
8502 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8504 isl_bool is_subset;
8506 if (!bmap1 || !bmap2)
8507 return isl_bool_error;
8508 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8509 if (is_subset != isl_bool_true)
8510 return is_subset;
8511 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8512 return isl_bool_not(is_subset);
8515 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8516 __isl_keep isl_map *map2)
8518 isl_bool is_subset;
8520 if (!map1 || !map2)
8521 return isl_bool_error;
8522 is_subset = isl_map_is_subset(map1, map2);
8523 if (is_subset != isl_bool_true)
8524 return is_subset;
8525 is_subset = isl_map_is_subset(map2, map1);
8526 return isl_bool_not(is_subset);
8529 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8530 __isl_keep isl_set *set2)
8532 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8535 /* Is "bmap" obviously equal to the universe with the same space?
8537 * That is, does it not have any constraints?
8539 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8541 if (!bmap)
8542 return isl_bool_error;
8543 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8546 /* Is "bset" obviously equal to the universe with the same space?
8548 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8550 return isl_basic_map_plain_is_universe(bset);
8553 /* If "c" does not involve any existentially quantified variables,
8554 * then set *univ to false and abort
8556 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8558 isl_bool *univ = user;
8559 unsigned n;
8561 n = isl_constraint_dim(c, isl_dim_div);
8562 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8563 isl_constraint_free(c);
8564 if (*univ < 0 || !*univ)
8565 return isl_stat_error;
8566 return isl_stat_ok;
8569 /* Is "bmap" equal to the universe with the same space?
8571 * First check if it is obviously equal to the universe.
8572 * If not and if there are any constraints not involving
8573 * existentially quantified variables, then it is certainly
8574 * not equal to the universe.
8575 * Otherwise, check if the universe is a subset of "bmap".
8577 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8579 isl_bool univ;
8580 isl_basic_map *test;
8582 univ = isl_basic_map_plain_is_universe(bmap);
8583 if (univ < 0 || univ)
8584 return univ;
8585 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8586 return isl_bool_false;
8587 univ = isl_bool_true;
8588 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8589 univ)
8590 return isl_bool_error;
8591 if (univ < 0 || !univ)
8592 return univ;
8593 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8594 univ = isl_basic_map_is_subset(test, bmap);
8595 isl_basic_map_free(test);
8596 return univ;
8599 /* Is "bset" equal to the universe with the same space?
8601 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8603 return isl_basic_map_is_universe(bset);
8606 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8608 int i;
8610 if (!map)
8611 return isl_bool_error;
8613 for (i = 0; i < map->n; ++i) {
8614 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8615 if (r < 0 || r)
8616 return r;
8619 return isl_bool_false;
8622 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8624 return isl_map_plain_is_universe(set_to_map(set));
8627 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8629 struct isl_basic_set *bset = NULL;
8630 struct isl_vec *sample = NULL;
8631 isl_bool empty, non_empty;
8633 if (!bmap)
8634 return isl_bool_error;
8636 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8637 return isl_bool_true;
8639 if (isl_basic_map_plain_is_universe(bmap))
8640 return isl_bool_false;
8642 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8643 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8644 copy = isl_basic_map_remove_redundancies(copy);
8645 empty = isl_basic_map_plain_is_empty(copy);
8646 isl_basic_map_free(copy);
8647 return empty;
8650 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8651 if (non_empty < 0)
8652 return isl_bool_error;
8653 if (non_empty)
8654 return isl_bool_false;
8655 isl_vec_free(bmap->sample);
8656 bmap->sample = NULL;
8657 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8658 if (!bset)
8659 return isl_bool_error;
8660 sample = isl_basic_set_sample_vec(bset);
8661 if (!sample)
8662 return isl_bool_error;
8663 empty = sample->size == 0;
8664 isl_vec_free(bmap->sample);
8665 bmap->sample = sample;
8666 if (empty)
8667 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8669 return empty;
8672 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8674 if (!bmap)
8675 return isl_bool_error;
8676 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8679 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8681 if (!bset)
8682 return isl_bool_error;
8683 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8686 /* Is "bmap" known to be non-empty?
8688 * That is, is the cached sample still valid?
8690 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8692 unsigned total;
8694 if (!bmap)
8695 return isl_bool_error;
8696 if (!bmap->sample)
8697 return isl_bool_false;
8698 total = 1 + isl_basic_map_total_dim(bmap);
8699 if (bmap->sample->size != total)
8700 return isl_bool_false;
8701 return isl_basic_map_contains(bmap, bmap->sample);
8704 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8706 return isl_basic_map_is_empty(bset_to_bmap(bset));
8709 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8710 __isl_take isl_basic_map *bmap2)
8712 struct isl_map *map;
8713 if (!bmap1 || !bmap2)
8714 goto error;
8716 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8718 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8719 if (!map)
8720 goto error;
8721 map = isl_map_add_basic_map(map, bmap1);
8722 map = isl_map_add_basic_map(map, bmap2);
8723 return map;
8724 error:
8725 isl_basic_map_free(bmap1);
8726 isl_basic_map_free(bmap2);
8727 return NULL;
8730 struct isl_set *isl_basic_set_union(
8731 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8733 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8734 bset_to_bmap(bset2)));
8737 /* Order divs such that any div only depends on previous divs */
8738 __isl_give isl_basic_map *isl_basic_map_order_divs(
8739 __isl_take isl_basic_map *bmap)
8741 int i;
8742 unsigned off;
8744 if (!bmap)
8745 return NULL;
8747 off = isl_space_dim(bmap->dim, isl_dim_all);
8749 for (i = 0; i < bmap->n_div; ++i) {
8750 int pos;
8751 if (isl_int_is_zero(bmap->div[i][0]))
8752 continue;
8753 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8754 bmap->n_div-i);
8755 if (pos == -1)
8756 continue;
8757 if (pos == 0)
8758 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8759 "integer division depends on itself",
8760 return isl_basic_map_free(bmap));
8761 isl_basic_map_swap_div(bmap, i, i + pos);
8762 --i;
8764 return bmap;
8767 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8769 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8772 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8774 int i;
8776 if (!map)
8777 return 0;
8779 for (i = 0; i < map->n; ++i) {
8780 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8781 if (!map->p[i])
8782 goto error;
8785 return map;
8786 error:
8787 isl_map_free(map);
8788 return NULL;
8791 /* Sort the local variables of "bset".
8793 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8794 __isl_take isl_basic_set *bset)
8796 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8799 /* Apply the expansion computed by isl_merge_divs.
8800 * The expansion itself is given by "exp" while the resulting
8801 * list of divs is given by "div".
8803 * Move the integer divisions of "bmap" into the right position
8804 * according to "exp" and then introduce the additional integer
8805 * divisions, adding div constraints.
8806 * The moving should be done first to avoid moving coefficients
8807 * in the definitions of the extra integer divisions.
8809 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8810 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8812 int i, j;
8813 int n_div;
8815 bmap = isl_basic_map_cow(bmap);
8816 if (!bmap || !div)
8817 goto error;
8819 if (div->n_row < bmap->n_div)
8820 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8821 "not an expansion", goto error);
8823 n_div = bmap->n_div;
8824 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8825 div->n_row - n_div, 0,
8826 2 * (div->n_row - n_div));
8828 for (i = n_div; i < div->n_row; ++i)
8829 if (isl_basic_map_alloc_div(bmap) < 0)
8830 goto error;
8832 for (j = n_div - 1; j >= 0; --j) {
8833 if (exp[j] == j)
8834 break;
8835 isl_basic_map_swap_div(bmap, j, exp[j]);
8837 j = 0;
8838 for (i = 0; i < div->n_row; ++i) {
8839 if (j < n_div && exp[j] == i) {
8840 j++;
8841 } else {
8842 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8843 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8844 continue;
8845 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8846 goto error;
8850 isl_mat_free(div);
8851 return bmap;
8852 error:
8853 isl_basic_map_free(bmap);
8854 isl_mat_free(div);
8855 return NULL;
8858 /* Apply the expansion computed by isl_merge_divs.
8859 * The expansion itself is given by "exp" while the resulting
8860 * list of divs is given by "div".
8862 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8863 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8865 return isl_basic_map_expand_divs(bset, div, exp);
8868 /* Look for a div in dst that corresponds to the div "div" in src.
8869 * The divs before "div" in src and dst are assumed to be the same.
8871 * Returns -1 if no corresponding div was found and the position
8872 * of the corresponding div in dst otherwise.
8874 static int find_div(__isl_keep isl_basic_map *dst,
8875 __isl_keep isl_basic_map *src, unsigned div)
8877 int i;
8879 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8881 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8882 for (i = div; i < dst->n_div; ++i)
8883 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8884 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8885 dst->n_div - div) == -1)
8886 return i;
8887 return -1;
8890 /* Align the divs of "dst" to those of "src", adding divs from "src"
8891 * if needed. That is, make sure that the first src->n_div divs
8892 * of the result are equal to those of src.
8894 * The result is not finalized as by design it will have redundant
8895 * divs if any divs from "src" were copied.
8897 __isl_give isl_basic_map *isl_basic_map_align_divs(
8898 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8900 int i;
8901 isl_bool known;
8902 int extended;
8903 unsigned total;
8905 if (!dst || !src)
8906 return isl_basic_map_free(dst);
8908 if (src->n_div == 0)
8909 return dst;
8911 known = isl_basic_map_divs_known(src);
8912 if (known < 0)
8913 return isl_basic_map_free(dst);
8914 if (!known)
8915 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8916 "some src divs are unknown",
8917 return isl_basic_map_free(dst));
8919 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8920 if (!src)
8921 return isl_basic_map_free(dst);
8923 extended = 0;
8924 total = isl_space_dim(src->dim, isl_dim_all);
8925 for (i = 0; i < src->n_div; ++i) {
8926 int j = find_div(dst, src, i);
8927 if (j < 0) {
8928 if (!extended) {
8929 int extra = src->n_div - i;
8930 dst = isl_basic_map_cow(dst);
8931 if (!dst)
8932 goto error;
8933 dst = isl_basic_map_extend_space(dst,
8934 isl_space_copy(dst->dim),
8935 extra, 0, 2 * extra);
8936 extended = 1;
8938 j = isl_basic_map_alloc_div(dst);
8939 if (j < 0)
8940 goto error;
8941 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8942 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8943 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8944 goto error;
8946 if (j != i)
8947 isl_basic_map_swap_div(dst, i, j);
8949 isl_basic_map_free(src);
8950 return dst;
8951 error:
8952 isl_basic_map_free(src);
8953 isl_basic_map_free(dst);
8954 return NULL;
8957 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8959 int i;
8961 if (!map)
8962 return NULL;
8963 if (map->n == 0)
8964 return map;
8965 map = isl_map_compute_divs(map);
8966 map = isl_map_cow(map);
8967 if (!map)
8968 return NULL;
8970 for (i = 1; i < map->n; ++i)
8971 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8972 for (i = 1; i < map->n; ++i) {
8973 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8974 if (!map->p[i])
8975 return isl_map_free(map);
8978 map = isl_map_unmark_normalized(map);
8979 return map;
8982 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8984 return isl_map_align_divs_internal(map);
8987 struct isl_set *isl_set_align_divs(struct isl_set *set)
8989 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8992 /* Align the divs of the basic maps in "map" to those
8993 * of the basic maps in "list", as well as to the other basic maps in "map".
8994 * The elements in "list" are assumed to have known divs.
8996 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8997 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8999 int i, n;
9001 map = isl_map_compute_divs(map);
9002 map = isl_map_cow(map);
9003 if (!map || !list)
9004 return isl_map_free(map);
9005 if (map->n == 0)
9006 return map;
9008 n = isl_basic_map_list_n_basic_map(list);
9009 for (i = 0; i < n; ++i) {
9010 isl_basic_map *bmap;
9012 bmap = isl_basic_map_list_get_basic_map(list, i);
9013 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9014 isl_basic_map_free(bmap);
9016 if (!map->p[0])
9017 return isl_map_free(map);
9019 return isl_map_align_divs_internal(map);
9022 /* Align the divs of each element of "list" to those of "bmap".
9023 * Both "bmap" and the elements of "list" are assumed to have known divs.
9025 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9026 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9028 int i, n;
9030 if (!list || !bmap)
9031 return isl_basic_map_list_free(list);
9033 n = isl_basic_map_list_n_basic_map(list);
9034 for (i = 0; i < n; ++i) {
9035 isl_basic_map *bmap_i;
9037 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9038 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9039 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9042 return list;
9045 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9046 __isl_take isl_map *map)
9048 isl_bool ok;
9050 ok = isl_map_compatible_domain(map, set);
9051 if (ok < 0)
9052 goto error;
9053 if (!ok)
9054 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9055 "incompatible spaces", goto error);
9056 map = isl_map_intersect_domain(map, set);
9057 set = isl_map_range(map);
9058 return set;
9059 error:
9060 isl_set_free(set);
9061 isl_map_free(map);
9062 return NULL;
9065 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9066 __isl_take isl_map *map)
9068 return isl_map_align_params_map_map_and(set, map, &set_apply);
9071 /* There is no need to cow as removing empty parts doesn't change
9072 * the meaning of the set.
9074 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9076 int i;
9078 if (!map)
9079 return NULL;
9081 for (i = map->n - 1; i >= 0; --i)
9082 map = remove_if_empty(map, i);
9084 return map;
9087 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9089 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9092 /* Create a binary relation that maps the shared initial "pos" dimensions
9093 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9095 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9096 __isl_keep isl_basic_set *bset2, int pos)
9098 isl_basic_map *bmap1;
9099 isl_basic_map *bmap2;
9101 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9102 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9103 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9104 isl_dim_out, 0, pos);
9105 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9106 isl_dim_out, 0, pos);
9107 return isl_basic_map_range_product(bmap1, bmap2);
9110 /* Given two basic sets bset1 and bset2, compute the maximal difference
9111 * between the values of dimension pos in bset1 and those in bset2
9112 * for any common value of the parameters and dimensions preceding pos.
9114 static enum isl_lp_result basic_set_maximal_difference_at(
9115 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9116 int pos, isl_int *opt)
9118 isl_basic_map *bmap1;
9119 struct isl_ctx *ctx;
9120 struct isl_vec *obj;
9121 unsigned total;
9122 unsigned nparam;
9123 unsigned dim1;
9124 enum isl_lp_result res;
9126 if (!bset1 || !bset2)
9127 return isl_lp_error;
9129 nparam = isl_basic_set_n_param(bset1);
9130 dim1 = isl_basic_set_n_dim(bset1);
9132 bmap1 = join_initial(bset1, bset2, pos);
9133 if (!bmap1)
9134 return isl_lp_error;
9136 total = isl_basic_map_total_dim(bmap1);
9137 ctx = bmap1->ctx;
9138 obj = isl_vec_alloc(ctx, 1 + total);
9139 if (!obj)
9140 goto error;
9141 isl_seq_clr(obj->block.data, 1 + total);
9142 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9143 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9144 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9145 opt, NULL, NULL);
9146 isl_basic_map_free(bmap1);
9147 isl_vec_free(obj);
9148 return res;
9149 error:
9150 isl_basic_map_free(bmap1);
9151 return isl_lp_error;
9154 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9155 * for any common value of the parameters and dimensions preceding pos
9156 * in both basic sets, the values of dimension pos in bset1 are
9157 * smaller or larger than those in bset2.
9159 * Returns
9160 * 1 if bset1 follows bset2
9161 * -1 if bset1 precedes bset2
9162 * 0 if bset1 and bset2 are incomparable
9163 * -2 if some error occurred.
9165 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9166 __isl_keep isl_basic_set *bset2, int pos)
9168 isl_int opt;
9169 enum isl_lp_result res;
9170 int cmp;
9172 isl_int_init(opt);
9174 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9176 if (res == isl_lp_empty)
9177 cmp = 0;
9178 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9179 res == isl_lp_unbounded)
9180 cmp = 1;
9181 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9182 cmp = -1;
9183 else
9184 cmp = -2;
9186 isl_int_clear(opt);
9187 return cmp;
9190 /* Given two basic sets bset1 and bset2, check whether
9191 * for any common value of the parameters and dimensions preceding pos
9192 * there is a value of dimension pos in bset1 that is larger
9193 * than a value of the same dimension in bset2.
9195 * Return
9196 * 1 if there exists such a pair
9197 * 0 if there is no such pair, but there is a pair of equal values
9198 * -1 otherwise
9199 * -2 if some error occurred.
9201 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9202 __isl_keep isl_basic_set *bset2, int pos)
9204 isl_bool empty;
9205 isl_basic_map *bmap;
9206 unsigned dim1;
9208 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9209 bmap = join_initial(bset1, bset2, pos);
9210 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9211 isl_dim_out, dim1 - pos);
9212 empty = isl_basic_map_is_empty(bmap);
9213 if (empty < 0)
9214 goto error;
9215 if (empty) {
9216 isl_basic_map_free(bmap);
9217 return -1;
9219 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9220 isl_dim_out, dim1 - pos);
9221 empty = isl_basic_map_is_empty(bmap);
9222 if (empty < 0)
9223 goto error;
9224 isl_basic_map_free(bmap);
9225 if (empty)
9226 return 0;
9227 return 1;
9228 error:
9229 isl_basic_map_free(bmap);
9230 return -2;
9233 /* Given two sets set1 and set2, check whether
9234 * for any common value of the parameters and dimensions preceding pos
9235 * there is a value of dimension pos in set1 that is larger
9236 * than a value of the same dimension in set2.
9238 * Return
9239 * 1 if there exists such a pair
9240 * 0 if there is no such pair, but there is a pair of equal values
9241 * -1 otherwise
9242 * -2 if some error occurred.
9244 int isl_set_follows_at(__isl_keep isl_set *set1,
9245 __isl_keep isl_set *set2, int pos)
9247 int i, j;
9248 int follows = -1;
9250 if (!set1 || !set2)
9251 return -2;
9253 for (i = 0; i < set1->n; ++i)
9254 for (j = 0; j < set2->n; ++j) {
9255 int f;
9256 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9257 if (f == 1 || f == -2)
9258 return f;
9259 if (f > follows)
9260 follows = f;
9263 return follows;
9266 static isl_bool isl_basic_map_plain_has_fixed_var(
9267 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9269 int i;
9270 int d;
9271 unsigned total;
9273 if (!bmap)
9274 return isl_bool_error;
9275 total = isl_basic_map_total_dim(bmap);
9276 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9277 for (; d+1 > pos; --d)
9278 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9279 break;
9280 if (d != pos)
9281 continue;
9282 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9283 return isl_bool_false;
9284 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9285 return isl_bool_false;
9286 if (!isl_int_is_one(bmap->eq[i][1+d]))
9287 return isl_bool_false;
9288 if (val)
9289 isl_int_neg(*val, bmap->eq[i][0]);
9290 return isl_bool_true;
9292 return isl_bool_false;
9295 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9296 unsigned pos, isl_int *val)
9298 int i;
9299 isl_int v;
9300 isl_int tmp;
9301 isl_bool fixed;
9303 if (!map)
9304 return isl_bool_error;
9305 if (map->n == 0)
9306 return isl_bool_false;
9307 if (map->n == 1)
9308 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9309 isl_int_init(v);
9310 isl_int_init(tmp);
9311 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9312 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9313 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9314 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9315 fixed = isl_bool_false;
9317 if (val)
9318 isl_int_set(*val, v);
9319 isl_int_clear(tmp);
9320 isl_int_clear(v);
9321 return fixed;
9324 static isl_bool isl_basic_set_plain_has_fixed_var(
9325 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9327 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9328 pos, val);
9331 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9332 enum isl_dim_type type, unsigned pos, isl_int *val)
9334 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9335 return isl_bool_error;
9336 return isl_basic_map_plain_has_fixed_var(bmap,
9337 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9340 /* If "bmap" obviously lies on a hyperplane where the given dimension
9341 * has a fixed value, then return that value.
9342 * Otherwise return NaN.
9344 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9345 __isl_keep isl_basic_map *bmap,
9346 enum isl_dim_type type, unsigned pos)
9348 isl_ctx *ctx;
9349 isl_val *v;
9350 isl_bool fixed;
9352 if (!bmap)
9353 return NULL;
9354 ctx = isl_basic_map_get_ctx(bmap);
9355 v = isl_val_alloc(ctx);
9356 if (!v)
9357 return NULL;
9358 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9359 if (fixed < 0)
9360 return isl_val_free(v);
9361 if (fixed) {
9362 isl_int_set_si(v->d, 1);
9363 return v;
9365 isl_val_free(v);
9366 return isl_val_nan(ctx);
9369 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9370 enum isl_dim_type type, unsigned pos, isl_int *val)
9372 if (isl_map_check_range(map, type, pos, 1) < 0)
9373 return isl_bool_error;
9374 return isl_map_plain_has_fixed_var(map,
9375 map_offset(map, type) - 1 + pos, val);
9378 /* If "map" obviously lies on a hyperplane where the given dimension
9379 * has a fixed value, then return that value.
9380 * Otherwise return NaN.
9382 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9383 enum isl_dim_type type, unsigned pos)
9385 isl_ctx *ctx;
9386 isl_val *v;
9387 isl_bool fixed;
9389 if (!map)
9390 return NULL;
9391 ctx = isl_map_get_ctx(map);
9392 v = isl_val_alloc(ctx);
9393 if (!v)
9394 return NULL;
9395 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9396 if (fixed < 0)
9397 return isl_val_free(v);
9398 if (fixed) {
9399 isl_int_set_si(v->d, 1);
9400 return v;
9402 isl_val_free(v);
9403 return isl_val_nan(ctx);
9406 /* If "set" obviously lies on a hyperplane where the given dimension
9407 * has a fixed value, then return that value.
9408 * Otherwise return NaN.
9410 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9411 enum isl_dim_type type, unsigned pos)
9413 return isl_map_plain_get_val_if_fixed(set, type, pos);
9416 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9417 * then return this fixed value in *val.
9419 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9420 unsigned dim, isl_int *val)
9422 return isl_basic_set_plain_has_fixed_var(bset,
9423 isl_basic_set_n_param(bset) + dim, val);
9426 /* Return -1 if the constraint "c1" should be sorted before "c2"
9427 * and 1 if it should be sorted after "c2".
9428 * Return 0 if the two constraints are the same (up to the constant term).
9430 * In particular, if a constraint involves later variables than another
9431 * then it is sorted after this other constraint.
9432 * uset_gist depends on constraints without existentially quantified
9433 * variables sorting first.
9435 * For constraints that have the same latest variable, those
9436 * with the same coefficient for this latest variable (first in absolute value
9437 * and then in actual value) are grouped together.
9438 * This is useful for detecting pairs of constraints that can
9439 * be chained in their printed representation.
9441 * Finally, within a group, constraints are sorted according to
9442 * their coefficients (excluding the constant term).
9444 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9446 isl_int **c1 = (isl_int **) p1;
9447 isl_int **c2 = (isl_int **) p2;
9448 int l1, l2;
9449 unsigned size = *(unsigned *) arg;
9450 int cmp;
9452 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9453 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9455 if (l1 != l2)
9456 return l1 - l2;
9458 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9459 if (cmp != 0)
9460 return cmp;
9461 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9462 if (cmp != 0)
9463 return -cmp;
9465 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9468 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9469 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9470 * and 0 if the two constraints are the same (up to the constant term).
9472 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9473 isl_int *c1, isl_int *c2)
9475 unsigned total;
9477 if (!bmap)
9478 return -2;
9479 total = isl_basic_map_total_dim(bmap);
9480 return sort_constraint_cmp(&c1, &c2, &total);
9483 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9484 __isl_take isl_basic_map *bmap)
9486 unsigned total;
9488 if (!bmap)
9489 return NULL;
9490 if (bmap->n_ineq == 0)
9491 return bmap;
9492 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9493 return bmap;
9494 total = isl_basic_map_total_dim(bmap);
9495 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9496 &sort_constraint_cmp, &total) < 0)
9497 return isl_basic_map_free(bmap);
9498 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9499 return bmap;
9502 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9503 __isl_take isl_basic_set *bset)
9505 isl_basic_map *bmap = bset_to_bmap(bset);
9506 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9509 __isl_give isl_basic_map *isl_basic_map_normalize(
9510 __isl_take isl_basic_map *bmap)
9512 bmap = isl_basic_map_remove_redundancies(bmap);
9513 bmap = isl_basic_map_sort_constraints(bmap);
9514 return bmap;
9516 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9517 __isl_keep isl_basic_map *bmap2)
9519 int i, cmp;
9520 unsigned total;
9521 isl_space *space1, *space2;
9523 if (!bmap1 || !bmap2)
9524 return -1;
9526 if (bmap1 == bmap2)
9527 return 0;
9528 space1 = isl_basic_map_peek_space(bmap1);
9529 space2 = isl_basic_map_peek_space(bmap2);
9530 cmp = isl_space_cmp(space1, space2);
9531 if (cmp)
9532 return cmp;
9533 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9534 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9535 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9536 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9537 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9538 return 0;
9539 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9540 return 1;
9541 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9542 return -1;
9543 if (bmap1->n_eq != bmap2->n_eq)
9544 return bmap1->n_eq - bmap2->n_eq;
9545 if (bmap1->n_ineq != bmap2->n_ineq)
9546 return bmap1->n_ineq - bmap2->n_ineq;
9547 if (bmap1->n_div != bmap2->n_div)
9548 return bmap1->n_div - bmap2->n_div;
9549 total = isl_basic_map_total_dim(bmap1);
9550 for (i = 0; i < bmap1->n_eq; ++i) {
9551 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9552 if (cmp)
9553 return cmp;
9555 for (i = 0; i < bmap1->n_ineq; ++i) {
9556 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9557 if (cmp)
9558 return cmp;
9560 for (i = 0; i < bmap1->n_div; ++i) {
9561 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9562 if (cmp)
9563 return cmp;
9565 return 0;
9568 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9569 __isl_keep isl_basic_set *bset2)
9571 return isl_basic_map_plain_cmp(bset1, bset2);
9574 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9576 int i, cmp;
9578 if (set1 == set2)
9579 return 0;
9580 if (set1->n != set2->n)
9581 return set1->n - set2->n;
9583 for (i = 0; i < set1->n; ++i) {
9584 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9585 if (cmp)
9586 return cmp;
9589 return 0;
9592 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9593 __isl_keep isl_basic_map *bmap2)
9595 if (!bmap1 || !bmap2)
9596 return isl_bool_error;
9597 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9600 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9601 __isl_keep isl_basic_set *bset2)
9603 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9604 bset_to_bmap(bset2));
9607 static int qsort_bmap_cmp(const void *p1, const void *p2)
9609 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9610 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9612 return isl_basic_map_plain_cmp(bmap1, bmap2);
9615 /* Sort the basic maps of "map" and remove duplicate basic maps.
9617 * While removing basic maps, we make sure that the basic maps remain
9618 * sorted because isl_map_normalize expects the basic maps of the result
9619 * to be sorted.
9621 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9623 int i, j;
9625 map = isl_map_remove_empty_parts(map);
9626 if (!map)
9627 return NULL;
9628 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9629 for (i = map->n - 1; i >= 1; --i) {
9630 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9631 continue;
9632 isl_basic_map_free(map->p[i-1]);
9633 for (j = i; j < map->n; ++j)
9634 map->p[j - 1] = map->p[j];
9635 map->n--;
9638 return map;
9641 /* Remove obvious duplicates among the basic maps of "map".
9643 * Unlike isl_map_normalize, this function does not remove redundant
9644 * constraints and only removes duplicates that have exactly the same
9645 * constraints in the input. It does sort the constraints and
9646 * the basic maps to ease the detection of duplicates.
9648 * If "map" has already been normalized or if the basic maps are
9649 * disjoint, then there can be no duplicates.
9651 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9653 int i;
9654 isl_basic_map *bmap;
9656 if (!map)
9657 return NULL;
9658 if (map->n <= 1)
9659 return map;
9660 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9661 return map;
9662 for (i = 0; i < map->n; ++i) {
9663 bmap = isl_basic_map_copy(map->p[i]);
9664 bmap = isl_basic_map_sort_constraints(bmap);
9665 if (!bmap)
9666 return isl_map_free(map);
9667 isl_basic_map_free(map->p[i]);
9668 map->p[i] = bmap;
9671 map = sort_and_remove_duplicates(map);
9672 return map;
9675 /* We normalize in place, but if anything goes wrong we need
9676 * to return NULL, so we need to make sure we don't change the
9677 * meaning of any possible other copies of map.
9679 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9681 int i;
9682 struct isl_basic_map *bmap;
9684 if (!map)
9685 return NULL;
9686 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9687 return map;
9688 for (i = 0; i < map->n; ++i) {
9689 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9690 if (!bmap)
9691 goto error;
9692 isl_basic_map_free(map->p[i]);
9693 map->p[i] = bmap;
9696 map = sort_and_remove_duplicates(map);
9697 if (map)
9698 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9699 return map;
9700 error:
9701 isl_map_free(map);
9702 return NULL;
9705 struct isl_set *isl_set_normalize(struct isl_set *set)
9707 return set_from_map(isl_map_normalize(set_to_map(set)));
9710 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9711 __isl_keep isl_map *map2)
9713 int i;
9714 isl_bool equal;
9716 if (!map1 || !map2)
9717 return isl_bool_error;
9719 if (map1 == map2)
9720 return isl_bool_true;
9721 if (!isl_space_is_equal(map1->dim, map2->dim))
9722 return isl_bool_false;
9724 map1 = isl_map_copy(map1);
9725 map2 = isl_map_copy(map2);
9726 map1 = isl_map_normalize(map1);
9727 map2 = isl_map_normalize(map2);
9728 if (!map1 || !map2)
9729 goto error;
9730 equal = map1->n == map2->n;
9731 for (i = 0; equal && i < map1->n; ++i) {
9732 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9733 if (equal < 0)
9734 goto error;
9736 isl_map_free(map1);
9737 isl_map_free(map2);
9738 return equal;
9739 error:
9740 isl_map_free(map1);
9741 isl_map_free(map2);
9742 return isl_bool_error;
9745 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9746 __isl_keep isl_set *set2)
9748 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9751 /* Return the basic maps in "map" as a list.
9753 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9754 __isl_keep isl_map *map)
9756 int i;
9757 isl_ctx *ctx;
9758 isl_basic_map_list *list;
9760 if (!map)
9761 return NULL;
9762 ctx = isl_map_get_ctx(map);
9763 list = isl_basic_map_list_alloc(ctx, map->n);
9765 for (i = 0; i < map->n; ++i) {
9766 isl_basic_map *bmap;
9768 bmap = isl_basic_map_copy(map->p[i]);
9769 list = isl_basic_map_list_add(list, bmap);
9772 return list;
9775 /* Return the intersection of the elements in the non-empty list "list".
9776 * All elements are assumed to live in the same space.
9778 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9779 __isl_take isl_basic_map_list *list)
9781 int i, n;
9782 isl_basic_map *bmap;
9784 if (!list)
9785 return NULL;
9786 n = isl_basic_map_list_n_basic_map(list);
9787 if (n < 1)
9788 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9789 "expecting non-empty list", goto error);
9791 bmap = isl_basic_map_list_get_basic_map(list, 0);
9792 for (i = 1; i < n; ++i) {
9793 isl_basic_map *bmap_i;
9795 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9796 bmap = isl_basic_map_intersect(bmap, bmap_i);
9799 isl_basic_map_list_free(list);
9800 return bmap;
9801 error:
9802 isl_basic_map_list_free(list);
9803 return NULL;
9806 /* Return the intersection of the elements in the non-empty list "list".
9807 * All elements are assumed to live in the same space.
9809 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9810 __isl_take isl_basic_set_list *list)
9812 return isl_basic_map_list_intersect(list);
9815 /* Return the union of the elements of "list".
9816 * The list is required to have at least one element.
9818 __isl_give isl_set *isl_basic_set_list_union(
9819 __isl_take isl_basic_set_list *list)
9821 int i, n;
9822 isl_space *space;
9823 isl_basic_set *bset;
9824 isl_set *set;
9826 if (!list)
9827 return NULL;
9828 n = isl_basic_set_list_n_basic_set(list);
9829 if (n < 1)
9830 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9831 "expecting non-empty list", goto error);
9833 bset = isl_basic_set_list_get_basic_set(list, 0);
9834 space = isl_basic_set_get_space(bset);
9835 isl_basic_set_free(bset);
9837 set = isl_set_alloc_space(space, n, 0);
9838 for (i = 0; i < n; ++i) {
9839 bset = isl_basic_set_list_get_basic_set(list, i);
9840 set = isl_set_add_basic_set(set, bset);
9843 isl_basic_set_list_free(list);
9844 return set;
9845 error:
9846 isl_basic_set_list_free(list);
9847 return NULL;
9850 /* Return the union of the elements in the non-empty list "list".
9851 * All elements are assumed to live in the same space.
9853 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9855 int i, n;
9856 isl_set *set;
9858 if (!list)
9859 return NULL;
9860 n = isl_set_list_n_set(list);
9861 if (n < 1)
9862 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9863 "expecting non-empty list", goto error);
9865 set = isl_set_list_get_set(list, 0);
9866 for (i = 1; i < n; ++i) {
9867 isl_set *set_i;
9869 set_i = isl_set_list_get_set(list, i);
9870 set = isl_set_union(set, set_i);
9873 isl_set_list_free(list);
9874 return set;
9875 error:
9876 isl_set_list_free(list);
9877 return NULL;
9880 __isl_give isl_basic_map *isl_basic_map_product(
9881 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9883 isl_space *space_result = NULL;
9884 struct isl_basic_map *bmap;
9885 unsigned in1, in2, out1, out2, nparam, total, pos;
9886 struct isl_dim_map *dim_map1, *dim_map2;
9888 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9889 goto error;
9890 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9891 isl_space_copy(bmap2->dim));
9893 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9894 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9895 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9896 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9897 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9899 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9900 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9901 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9902 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9903 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9904 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9905 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9906 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9907 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9908 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9909 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9911 bmap = isl_basic_map_alloc_space(space_result,
9912 bmap1->n_div + bmap2->n_div,
9913 bmap1->n_eq + bmap2->n_eq,
9914 bmap1->n_ineq + bmap2->n_ineq);
9915 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9916 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9917 bmap = isl_basic_map_simplify(bmap);
9918 return isl_basic_map_finalize(bmap);
9919 error:
9920 isl_basic_map_free(bmap1);
9921 isl_basic_map_free(bmap2);
9922 return NULL;
9925 __isl_give isl_basic_map *isl_basic_map_flat_product(
9926 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9928 isl_basic_map *prod;
9930 prod = isl_basic_map_product(bmap1, bmap2);
9931 prod = isl_basic_map_flatten(prod);
9932 return prod;
9935 __isl_give isl_basic_set *isl_basic_set_flat_product(
9936 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9938 return isl_basic_map_flat_range_product(bset1, bset2);
9941 __isl_give isl_basic_map *isl_basic_map_domain_product(
9942 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9944 isl_space *space_result = NULL;
9945 isl_basic_map *bmap;
9946 unsigned in1, in2, out, nparam, total, pos;
9947 struct isl_dim_map *dim_map1, *dim_map2;
9949 if (!bmap1 || !bmap2)
9950 goto error;
9952 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9953 isl_space_copy(bmap2->dim));
9955 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9956 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9957 out = isl_basic_map_dim(bmap1, isl_dim_out);
9958 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9960 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9961 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9962 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9963 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9964 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9965 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9966 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9967 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9968 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9969 isl_dim_map_div(dim_map1, bmap1, pos += out);
9970 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9972 bmap = isl_basic_map_alloc_space(space_result,
9973 bmap1->n_div + bmap2->n_div,
9974 bmap1->n_eq + bmap2->n_eq,
9975 bmap1->n_ineq + bmap2->n_ineq);
9976 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9977 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9978 bmap = isl_basic_map_simplify(bmap);
9979 return isl_basic_map_finalize(bmap);
9980 error:
9981 isl_basic_map_free(bmap1);
9982 isl_basic_map_free(bmap2);
9983 return NULL;
9986 __isl_give isl_basic_map *isl_basic_map_range_product(
9987 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9989 isl_bool rational;
9990 isl_space *space_result = NULL;
9991 isl_basic_map *bmap;
9992 unsigned in, out1, out2, nparam, total, pos;
9993 struct isl_dim_map *dim_map1, *dim_map2;
9995 rational = isl_basic_map_is_rational(bmap1);
9996 if (rational >= 0 && rational)
9997 rational = isl_basic_map_is_rational(bmap2);
9998 if (!bmap1 || !bmap2 || rational < 0)
9999 goto error;
10001 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10002 goto error;
10004 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10005 isl_space_copy(bmap2->dim));
10007 in = isl_basic_map_dim(bmap1, isl_dim_in);
10008 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10009 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10010 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10012 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10013 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10014 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10015 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10016 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10017 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10018 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10019 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10020 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10021 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10022 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10024 bmap = isl_basic_map_alloc_space(space_result,
10025 bmap1->n_div + bmap2->n_div,
10026 bmap1->n_eq + bmap2->n_eq,
10027 bmap1->n_ineq + bmap2->n_ineq);
10028 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10029 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10030 if (rational)
10031 bmap = isl_basic_map_set_rational(bmap);
10032 bmap = isl_basic_map_simplify(bmap);
10033 return isl_basic_map_finalize(bmap);
10034 error:
10035 isl_basic_map_free(bmap1);
10036 isl_basic_map_free(bmap2);
10037 return NULL;
10040 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10041 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10043 isl_basic_map *prod;
10045 prod = isl_basic_map_range_product(bmap1, bmap2);
10046 prod = isl_basic_map_flatten_range(prod);
10047 return prod;
10050 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10051 * and collect the results.
10052 * The result live in the space obtained by calling "space_product"
10053 * on the spaces of "map1" and "map2".
10054 * If "remove_duplicates" is set then the result may contain duplicates
10055 * (even if the inputs do not) and so we try and remove the obvious
10056 * duplicates.
10058 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10059 __isl_take isl_map *map2,
10060 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10061 __isl_take isl_space *right),
10062 __isl_give isl_basic_map *(*basic_map_product)(
10063 __isl_take isl_basic_map *left,
10064 __isl_take isl_basic_map *right),
10065 int remove_duplicates)
10067 unsigned flags = 0;
10068 struct isl_map *result;
10069 int i, j;
10070 isl_bool m;
10072 m = isl_map_has_equal_params(map1, map2);
10073 if (m < 0)
10074 goto error;
10075 if (!m)
10076 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10077 "parameters don't match", goto error);
10079 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10080 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10081 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10083 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10084 isl_space_copy(map2->dim)),
10085 map1->n * map2->n, flags);
10086 if (!result)
10087 goto error;
10088 for (i = 0; i < map1->n; ++i)
10089 for (j = 0; j < map2->n; ++j) {
10090 struct isl_basic_map *part;
10091 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10092 isl_basic_map_copy(map2->p[j]));
10093 if (isl_basic_map_is_empty(part))
10094 isl_basic_map_free(part);
10095 else
10096 result = isl_map_add_basic_map(result, part);
10097 if (!result)
10098 goto error;
10100 if (remove_duplicates)
10101 result = isl_map_remove_obvious_duplicates(result);
10102 isl_map_free(map1);
10103 isl_map_free(map2);
10104 return result;
10105 error:
10106 isl_map_free(map1);
10107 isl_map_free(map2);
10108 return NULL;
10111 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10113 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10114 __isl_take isl_map *map2)
10116 return map_product(map1, map2, &isl_space_product,
10117 &isl_basic_map_product, 0);
10120 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10121 __isl_take isl_map *map2)
10123 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10126 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10128 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10129 __isl_take isl_map *map2)
10131 isl_map *prod;
10133 prod = isl_map_product(map1, map2);
10134 prod = isl_map_flatten(prod);
10135 return prod;
10138 /* Given two set A and B, construct its Cartesian product A x B.
10140 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10142 return isl_map_range_product(set1, set2);
10145 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10146 __isl_take isl_set *set2)
10148 return isl_map_flat_range_product(set1, set2);
10151 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10153 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10154 __isl_take isl_map *map2)
10156 return map_product(map1, map2, &isl_space_domain_product,
10157 &isl_basic_map_domain_product, 1);
10160 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10162 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10163 __isl_take isl_map *map2)
10165 return map_product(map1, map2, &isl_space_range_product,
10166 &isl_basic_map_range_product, 1);
10169 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10170 __isl_take isl_map *map2)
10172 return isl_map_align_params_map_map_and(map1, map2,
10173 &map_domain_product_aligned);
10176 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10177 __isl_take isl_map *map2)
10179 return isl_map_align_params_map_map_and(map1, map2,
10180 &map_range_product_aligned);
10183 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10185 __isl_give isl_map *isl_map_factor_domain(__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_domain(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, keep1, total1 - keep1);
10204 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10205 map = isl_map_reset_space(map, space);
10207 return map;
10210 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10212 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10214 isl_space *space;
10215 int total1, keep1, total2, keep2;
10217 if (!map)
10218 return NULL;
10219 if (!isl_space_domain_is_wrapping(map->dim) ||
10220 !isl_space_range_is_wrapping(map->dim))
10221 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10222 "not a product", return isl_map_free(map));
10224 space = isl_map_get_space(map);
10225 total1 = isl_space_dim(space, isl_dim_in);
10226 total2 = isl_space_dim(space, isl_dim_out);
10227 space = isl_space_factor_range(space);
10228 keep1 = isl_space_dim(space, isl_dim_in);
10229 keep2 = isl_space_dim(space, isl_dim_out);
10230 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10231 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10232 map = isl_map_reset_space(map, space);
10234 return map;
10237 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10239 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10241 isl_space *space;
10242 int total, keep;
10244 if (!map)
10245 return NULL;
10246 if (!isl_space_domain_is_wrapping(map->dim))
10247 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10248 "domain is not a product", return isl_map_free(map));
10250 space = isl_map_get_space(map);
10251 total = isl_space_dim(space, isl_dim_in);
10252 space = isl_space_domain_factor_domain(space);
10253 keep = isl_space_dim(space, isl_dim_in);
10254 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10255 map = isl_map_reset_space(map, space);
10257 return map;
10260 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10262 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10264 isl_space *space;
10265 int total, keep;
10267 if (!map)
10268 return NULL;
10269 if (!isl_space_domain_is_wrapping(map->dim))
10270 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10271 "domain is not a product", return isl_map_free(map));
10273 space = isl_map_get_space(map);
10274 total = isl_space_dim(space, isl_dim_in);
10275 space = isl_space_domain_factor_range(space);
10276 keep = isl_space_dim(space, isl_dim_in);
10277 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10278 map = isl_map_reset_space(map, space);
10280 return map;
10283 /* Given a map A -> [B -> C], extract the map A -> B.
10285 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10287 isl_space *space;
10288 int total, keep;
10290 if (!map)
10291 return NULL;
10292 if (!isl_space_range_is_wrapping(map->dim))
10293 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10294 "range is not a product", return isl_map_free(map));
10296 space = isl_map_get_space(map);
10297 total = isl_space_dim(space, isl_dim_out);
10298 space = isl_space_range_factor_domain(space);
10299 keep = isl_space_dim(space, isl_dim_out);
10300 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10301 map = isl_map_reset_space(map, space);
10303 return map;
10306 /* Given a map A -> [B -> C], extract the map A -> C.
10308 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10310 isl_space *space;
10311 int total, keep;
10313 if (!map)
10314 return NULL;
10315 if (!isl_space_range_is_wrapping(map->dim))
10316 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10317 "range is not a product", return isl_map_free(map));
10319 space = isl_map_get_space(map);
10320 total = isl_space_dim(space, isl_dim_out);
10321 space = isl_space_range_factor_range(space);
10322 keep = isl_space_dim(space, isl_dim_out);
10323 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10324 map = isl_map_reset_space(map, space);
10326 return map;
10329 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10331 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10332 __isl_take isl_map *map2)
10334 isl_map *prod;
10336 prod = isl_map_domain_product(map1, map2);
10337 prod = isl_map_flatten_domain(prod);
10338 return prod;
10341 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10343 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10344 __isl_take isl_map *map2)
10346 isl_map *prod;
10348 prod = isl_map_range_product(map1, map2);
10349 prod = isl_map_flatten_range(prod);
10350 return prod;
10353 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10355 int i;
10356 uint32_t hash = isl_hash_init();
10357 unsigned total;
10359 if (!bmap)
10360 return 0;
10361 bmap = isl_basic_map_copy(bmap);
10362 bmap = isl_basic_map_normalize(bmap);
10363 if (!bmap)
10364 return 0;
10365 total = isl_basic_map_total_dim(bmap);
10366 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10367 for (i = 0; i < bmap->n_eq; ++i) {
10368 uint32_t c_hash;
10369 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10370 isl_hash_hash(hash, c_hash);
10372 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10373 for (i = 0; i < bmap->n_ineq; ++i) {
10374 uint32_t c_hash;
10375 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10376 isl_hash_hash(hash, c_hash);
10378 isl_hash_byte(hash, bmap->n_div & 0xFF);
10379 for (i = 0; i < bmap->n_div; ++i) {
10380 uint32_t c_hash;
10381 if (isl_int_is_zero(bmap->div[i][0]))
10382 continue;
10383 isl_hash_byte(hash, i & 0xFF);
10384 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10385 isl_hash_hash(hash, c_hash);
10387 isl_basic_map_free(bmap);
10388 return hash;
10391 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10393 return isl_basic_map_get_hash(bset_to_bmap(bset));
10396 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10398 int i;
10399 uint32_t hash;
10401 if (!map)
10402 return 0;
10403 map = isl_map_copy(map);
10404 map = isl_map_normalize(map);
10405 if (!map)
10406 return 0;
10408 hash = isl_hash_init();
10409 for (i = 0; i < map->n; ++i) {
10410 uint32_t bmap_hash;
10411 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10412 isl_hash_hash(hash, bmap_hash);
10415 isl_map_free(map);
10417 return hash;
10420 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10422 return isl_map_get_hash(set_to_map(set));
10425 /* Return the number of basic maps in the (current) representation of "map".
10427 int isl_map_n_basic_map(__isl_keep isl_map *map)
10429 return map ? map->n : 0;
10432 int isl_set_n_basic_set(__isl_keep isl_set *set)
10434 return set ? set->n : 0;
10437 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10438 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10440 int i;
10442 if (!map)
10443 return isl_stat_error;
10445 for (i = 0; i < map->n; ++i)
10446 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10447 return isl_stat_error;
10449 return isl_stat_ok;
10452 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10453 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10455 int i;
10457 if (!set)
10458 return isl_stat_error;
10460 for (i = 0; i < set->n; ++i)
10461 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10462 return isl_stat_error;
10464 return isl_stat_ok;
10467 /* Return a list of basic sets, the union of which is equal to "set".
10469 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10470 __isl_keep isl_set *set)
10472 int i;
10473 isl_basic_set_list *list;
10475 if (!set)
10476 return NULL;
10478 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10479 for (i = 0; i < set->n; ++i) {
10480 isl_basic_set *bset;
10482 bset = isl_basic_set_copy(set->p[i]);
10483 list = isl_basic_set_list_add(list, bset);
10486 return list;
10489 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10491 isl_space *space;
10493 if (!bset)
10494 return NULL;
10496 bset = isl_basic_set_cow(bset);
10497 if (!bset)
10498 return NULL;
10500 space = isl_basic_set_get_space(bset);
10501 space = isl_space_lift(space, bset->n_div);
10502 if (!space)
10503 goto error;
10504 isl_space_free(bset->dim);
10505 bset->dim = space;
10506 bset->extra -= bset->n_div;
10507 bset->n_div = 0;
10509 bset = isl_basic_set_finalize(bset);
10511 return bset;
10512 error:
10513 isl_basic_set_free(bset);
10514 return NULL;
10517 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10519 int i;
10520 isl_space *space;
10521 unsigned n_div;
10523 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10525 if (!set)
10526 return NULL;
10528 set = isl_set_cow(set);
10529 if (!set)
10530 return NULL;
10532 n_div = set->p[0]->n_div;
10533 space = isl_set_get_space(set);
10534 space = isl_space_lift(space, n_div);
10535 if (!space)
10536 goto error;
10537 isl_space_free(set->dim);
10538 set->dim = space;
10540 for (i = 0; i < set->n; ++i) {
10541 set->p[i] = isl_basic_set_lift(set->p[i]);
10542 if (!set->p[i])
10543 goto error;
10546 return set;
10547 error:
10548 isl_set_free(set);
10549 return NULL;
10552 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10554 unsigned dim;
10555 int size = 0;
10557 if (!bset)
10558 return -1;
10560 dim = isl_basic_set_total_dim(bset);
10561 size += bset->n_eq * (1 + dim);
10562 size += bset->n_ineq * (1 + dim);
10563 size += bset->n_div * (2 + dim);
10565 return size;
10568 int isl_set_size(__isl_keep isl_set *set)
10570 int i;
10571 int size = 0;
10573 if (!set)
10574 return -1;
10576 for (i = 0; i < set->n; ++i)
10577 size += isl_basic_set_size(set->p[i]);
10579 return size;
10582 /* Check if there is any lower bound (if lower == 0) and/or upper
10583 * bound (if upper == 0) on the specified dim.
10585 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10586 enum isl_dim_type type, unsigned pos, int lower, int upper)
10588 int i;
10590 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10591 return isl_bool_error;
10593 pos += isl_basic_map_offset(bmap, type);
10595 for (i = 0; i < bmap->n_div; ++i) {
10596 if (isl_int_is_zero(bmap->div[i][0]))
10597 continue;
10598 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10599 return isl_bool_true;
10602 for (i = 0; i < bmap->n_eq; ++i)
10603 if (!isl_int_is_zero(bmap->eq[i][pos]))
10604 return isl_bool_true;
10606 for (i = 0; i < bmap->n_ineq; ++i) {
10607 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10608 if (sgn > 0)
10609 lower = 1;
10610 if (sgn < 0)
10611 upper = 1;
10614 return lower && upper;
10617 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10618 enum isl_dim_type type, unsigned pos)
10620 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10623 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10624 enum isl_dim_type type, unsigned pos)
10626 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10629 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10630 enum isl_dim_type type, unsigned pos)
10632 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10635 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10636 enum isl_dim_type type, unsigned pos)
10638 int i;
10640 if (!map)
10641 return isl_bool_error;
10643 for (i = 0; i < map->n; ++i) {
10644 isl_bool bounded;
10645 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10646 if (bounded < 0 || !bounded)
10647 return bounded;
10650 return isl_bool_true;
10653 /* Return true if the specified dim is involved in both an upper bound
10654 * and a lower bound.
10656 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10657 enum isl_dim_type type, unsigned pos)
10659 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10662 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10664 static isl_bool has_any_bound(__isl_keep isl_map *map,
10665 enum isl_dim_type type, unsigned pos,
10666 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10667 enum isl_dim_type type, unsigned pos))
10669 int i;
10671 if (!map)
10672 return isl_bool_error;
10674 for (i = 0; i < map->n; ++i) {
10675 isl_bool bounded;
10676 bounded = fn(map->p[i], type, pos);
10677 if (bounded < 0 || bounded)
10678 return bounded;
10681 return isl_bool_false;
10684 /* Return 1 if the specified dim is involved in any lower bound.
10686 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10687 enum isl_dim_type type, unsigned pos)
10689 return has_any_bound(set, type, pos,
10690 &isl_basic_map_dim_has_lower_bound);
10693 /* Return 1 if the specified dim is involved in any upper bound.
10695 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10696 enum isl_dim_type type, unsigned pos)
10698 return has_any_bound(set, type, pos,
10699 &isl_basic_map_dim_has_upper_bound);
10702 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10704 static isl_bool has_bound(__isl_keep isl_map *map,
10705 enum isl_dim_type type, unsigned pos,
10706 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10707 enum isl_dim_type type, unsigned pos))
10709 int i;
10711 if (!map)
10712 return isl_bool_error;
10714 for (i = 0; i < map->n; ++i) {
10715 isl_bool bounded;
10716 bounded = fn(map->p[i], type, pos);
10717 if (bounded < 0 || !bounded)
10718 return bounded;
10721 return isl_bool_true;
10724 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10726 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10727 enum isl_dim_type type, unsigned pos)
10729 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10732 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10734 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10735 enum isl_dim_type type, unsigned pos)
10737 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10740 /* For each of the "n" variables starting at "first", determine
10741 * the sign of the variable and put the results in the first "n"
10742 * elements of the array "signs".
10743 * Sign
10744 * 1 means that the variable is non-negative
10745 * -1 means that the variable is non-positive
10746 * 0 means the variable attains both positive and negative values.
10748 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10749 unsigned first, unsigned n, int *signs)
10751 isl_vec *bound = NULL;
10752 struct isl_tab *tab = NULL;
10753 struct isl_tab_undo *snap;
10754 int i;
10756 if (!bset || !signs)
10757 return isl_stat_error;
10759 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10760 tab = isl_tab_from_basic_set(bset, 0);
10761 if (!bound || !tab)
10762 goto error;
10764 isl_seq_clr(bound->el, bound->size);
10765 isl_int_set_si(bound->el[0], -1);
10767 snap = isl_tab_snap(tab);
10768 for (i = 0; i < n; ++i) {
10769 int empty;
10771 isl_int_set_si(bound->el[1 + first + i], -1);
10772 if (isl_tab_add_ineq(tab, bound->el) < 0)
10773 goto error;
10774 empty = tab->empty;
10775 isl_int_set_si(bound->el[1 + first + i], 0);
10776 if (isl_tab_rollback(tab, snap) < 0)
10777 goto error;
10779 if (empty) {
10780 signs[i] = 1;
10781 continue;
10784 isl_int_set_si(bound->el[1 + first + i], 1);
10785 if (isl_tab_add_ineq(tab, bound->el) < 0)
10786 goto error;
10787 empty = tab->empty;
10788 isl_int_set_si(bound->el[1 + first + i], 0);
10789 if (isl_tab_rollback(tab, snap) < 0)
10790 goto error;
10792 signs[i] = empty ? -1 : 0;
10795 isl_tab_free(tab);
10796 isl_vec_free(bound);
10797 return isl_stat_ok;
10798 error:
10799 isl_tab_free(tab);
10800 isl_vec_free(bound);
10801 return isl_stat_error;
10804 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10805 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10807 if (!bset || !signs)
10808 return isl_stat_error;
10809 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10810 return isl_stat_error;
10812 first += pos(bset->dim, type) - 1;
10813 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10816 /* Is it possible for the integer division "div" to depend (possibly
10817 * indirectly) on any output dimensions?
10819 * If the div is undefined, then we conservatively assume that it
10820 * may depend on them.
10821 * Otherwise, we check if it actually depends on them or on any integer
10822 * divisions that may depend on them.
10824 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10826 int i;
10827 unsigned n_out, o_out;
10828 unsigned n_div, o_div;
10830 if (isl_int_is_zero(bmap->div[div][0]))
10831 return isl_bool_true;
10833 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10834 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10836 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10837 return isl_bool_true;
10839 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10840 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10842 for (i = 0; i < n_div; ++i) {
10843 isl_bool may_involve;
10845 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10846 continue;
10847 may_involve = div_may_involve_output(bmap, i);
10848 if (may_involve < 0 || may_involve)
10849 return may_involve;
10852 return isl_bool_false;
10855 /* Return the first integer division of "bmap" in the range
10856 * [first, first + n[ that may depend on any output dimensions and
10857 * that has a non-zero coefficient in "c" (where the first coefficient
10858 * in "c" corresponds to integer division "first").
10860 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10861 isl_int *c, int first, int n)
10863 int k;
10865 if (!bmap)
10866 return -1;
10868 for (k = first; k < first + n; ++k) {
10869 isl_bool may_involve;
10871 if (isl_int_is_zero(c[k]))
10872 continue;
10873 may_involve = div_may_involve_output(bmap, k);
10874 if (may_involve < 0)
10875 return -1;
10876 if (may_involve)
10877 return k;
10880 return first + n;
10883 /* Look for a pair of inequality constraints in "bmap" of the form
10885 * -l + i >= 0 or i >= l
10886 * and
10887 * n + l - i >= 0 or i <= l + n
10889 * with n < "m" and i the output dimension at position "pos".
10890 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10891 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10892 * and earlier output dimensions, as well as integer divisions that do
10893 * not involve any of the output dimensions.
10895 * Return the index of the first inequality constraint or bmap->n_ineq
10896 * if no such pair can be found.
10898 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10899 int pos, isl_int m)
10901 int i, j;
10902 isl_ctx *ctx;
10903 unsigned total;
10904 unsigned n_div, o_div;
10905 unsigned n_out, o_out;
10906 int less;
10908 if (!bmap)
10909 return -1;
10911 ctx = isl_basic_map_get_ctx(bmap);
10912 total = isl_basic_map_total_dim(bmap);
10913 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10914 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10915 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10916 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10917 for (i = 0; i < bmap->n_ineq; ++i) {
10918 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10919 continue;
10920 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10921 n_out - (pos + 1)) != -1)
10922 continue;
10923 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10924 0, n_div) < n_div)
10925 continue;
10926 for (j = i + 1; j < bmap->n_ineq; ++j) {
10927 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10928 ctx->one))
10929 continue;
10930 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10931 bmap->ineq[j] + 1, total))
10932 continue;
10933 break;
10935 if (j >= bmap->n_ineq)
10936 continue;
10937 isl_int_add(bmap->ineq[i][0],
10938 bmap->ineq[i][0], bmap->ineq[j][0]);
10939 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10940 isl_int_sub(bmap->ineq[i][0],
10941 bmap->ineq[i][0], bmap->ineq[j][0]);
10942 if (!less)
10943 continue;
10944 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10945 return i;
10946 else
10947 return j;
10950 return bmap->n_ineq;
10953 /* Return the index of the equality of "bmap" that defines
10954 * the output dimension "pos" in terms of earlier dimensions.
10955 * The equality may also involve integer divisions, as long
10956 * as those integer divisions are defined in terms of
10957 * parameters or input dimensions.
10958 * In this case, *div is set to the number of integer divisions and
10959 * *ineq is set to the number of inequality constraints (provided
10960 * div and ineq are not NULL).
10962 * The equality may also involve a single integer division involving
10963 * the output dimensions (typically only output dimension "pos") as
10964 * long as the coefficient of output dimension "pos" is 1 or -1 and
10965 * there is a pair of constraints i >= l and i <= l + n, with i referring
10966 * to output dimension "pos", l an expression involving only earlier
10967 * dimensions and n smaller than the coefficient of the integer division
10968 * in the equality. In this case, the output dimension can be defined
10969 * in terms of a modulo expression that does not involve the integer division.
10970 * *div is then set to this single integer division and
10971 * *ineq is set to the index of constraint i >= l.
10973 * Return bmap->n_eq if there is no such equality.
10974 * Return -1 on error.
10976 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10977 int pos, int *div, int *ineq)
10979 int j, k, l;
10980 unsigned n_out, o_out;
10981 unsigned n_div, o_div;
10983 if (!bmap)
10984 return -1;
10986 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10987 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10988 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10989 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10991 if (ineq)
10992 *ineq = bmap->n_ineq;
10993 if (div)
10994 *div = n_div;
10995 for (j = 0; j < bmap->n_eq; ++j) {
10996 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10997 continue;
10998 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10999 n_out - (pos + 1)) != -1)
11000 continue;
11001 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11002 0, n_div);
11003 if (k >= n_div)
11004 return j;
11005 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11006 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11007 continue;
11008 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11009 k + 1, n_div - (k+1)) < n_div)
11010 continue;
11011 l = find_modulo_constraint_pair(bmap, pos,
11012 bmap->eq[j][o_div + k]);
11013 if (l < 0)
11014 return -1;
11015 if (l >= bmap->n_ineq)
11016 continue;
11017 if (div)
11018 *div = k;
11019 if (ineq)
11020 *ineq = l;
11021 return j;
11024 return bmap->n_eq;
11027 /* Check if the given basic map is obviously single-valued.
11028 * In particular, for each output dimension, check that there is
11029 * an equality that defines the output dimension in terms of
11030 * earlier dimensions.
11032 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11034 int i;
11035 unsigned n_out;
11037 if (!bmap)
11038 return isl_bool_error;
11040 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11042 for (i = 0; i < n_out; ++i) {
11043 int eq;
11045 eq = isl_basic_map_output_defining_equality(bmap, i,
11046 NULL, NULL);
11047 if (eq < 0)
11048 return isl_bool_error;
11049 if (eq >= bmap->n_eq)
11050 return isl_bool_false;
11053 return isl_bool_true;
11056 /* Check if the given basic map is single-valued.
11057 * We simply compute
11059 * M \circ M^-1
11061 * and check if the result is a subset of the identity mapping.
11063 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11065 isl_space *space;
11066 isl_basic_map *test;
11067 isl_basic_map *id;
11068 isl_bool sv;
11070 sv = isl_basic_map_plain_is_single_valued(bmap);
11071 if (sv < 0 || sv)
11072 return sv;
11074 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11075 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11077 space = isl_basic_map_get_space(bmap);
11078 space = isl_space_map_from_set(isl_space_range(space));
11079 id = isl_basic_map_identity(space);
11081 sv = isl_basic_map_is_subset(test, id);
11083 isl_basic_map_free(test);
11084 isl_basic_map_free(id);
11086 return sv;
11089 /* Check if the given map is obviously single-valued.
11091 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11093 if (!map)
11094 return isl_bool_error;
11095 if (map->n == 0)
11096 return isl_bool_true;
11097 if (map->n >= 2)
11098 return isl_bool_false;
11100 return isl_basic_map_plain_is_single_valued(map->p[0]);
11103 /* Check if the given map is single-valued.
11104 * We simply compute
11106 * M \circ M^-1
11108 * and check if the result is a subset of the identity mapping.
11110 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11112 isl_space *dim;
11113 isl_map *test;
11114 isl_map *id;
11115 isl_bool sv;
11117 sv = isl_map_plain_is_single_valued(map);
11118 if (sv < 0 || sv)
11119 return sv;
11121 test = isl_map_reverse(isl_map_copy(map));
11122 test = isl_map_apply_range(test, isl_map_copy(map));
11124 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11125 id = isl_map_identity(dim);
11127 sv = isl_map_is_subset(test, id);
11129 isl_map_free(test);
11130 isl_map_free(id);
11132 return sv;
11135 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11137 isl_bool in;
11139 map = isl_map_copy(map);
11140 map = isl_map_reverse(map);
11141 in = isl_map_is_single_valued(map);
11142 isl_map_free(map);
11144 return in;
11147 /* Check if the given map is obviously injective.
11149 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11151 isl_bool in;
11153 map = isl_map_copy(map);
11154 map = isl_map_reverse(map);
11155 in = isl_map_plain_is_single_valued(map);
11156 isl_map_free(map);
11158 return in;
11161 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11163 isl_bool sv;
11165 sv = isl_map_is_single_valued(map);
11166 if (sv < 0 || !sv)
11167 return sv;
11169 return isl_map_is_injective(map);
11172 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11174 return isl_map_is_single_valued(set_to_map(set));
11177 /* Does "map" only map elements to themselves?
11179 * If the domain and range spaces are different, then "map"
11180 * is considered not to be an identity relation, even if it is empty.
11181 * Otherwise, construct the maximal identity relation and
11182 * check whether "map" is a subset of this relation.
11184 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11186 isl_space *space;
11187 isl_map *id;
11188 isl_bool equal, is_identity;
11190 space = isl_map_get_space(map);
11191 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11192 isl_space_free(space);
11193 if (equal < 0 || !equal)
11194 return equal;
11196 id = isl_map_identity(isl_map_get_space(map));
11197 is_identity = isl_map_is_subset(map, id);
11198 isl_map_free(id);
11200 return is_identity;
11203 int isl_map_is_translation(__isl_keep isl_map *map)
11205 int ok;
11206 isl_set *delta;
11208 delta = isl_map_deltas(isl_map_copy(map));
11209 ok = isl_set_is_singleton(delta);
11210 isl_set_free(delta);
11212 return ok;
11215 static int unique(isl_int *p, unsigned pos, unsigned len)
11217 if (isl_seq_first_non_zero(p, pos) != -1)
11218 return 0;
11219 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11220 return 0;
11221 return 1;
11224 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11226 int i, j;
11227 unsigned nvar;
11228 unsigned ovar;
11230 if (!bset)
11231 return isl_bool_error;
11233 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11234 return isl_bool_false;
11236 nvar = isl_basic_set_dim(bset, isl_dim_set);
11237 ovar = isl_space_offset(bset->dim, isl_dim_set);
11238 for (j = 0; j < nvar; ++j) {
11239 int lower = 0, upper = 0;
11240 for (i = 0; i < bset->n_eq; ++i) {
11241 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11242 continue;
11243 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11244 return isl_bool_false;
11245 break;
11247 if (i < bset->n_eq)
11248 continue;
11249 for (i = 0; i < bset->n_ineq; ++i) {
11250 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11251 continue;
11252 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11253 return isl_bool_false;
11254 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11255 lower = 1;
11256 else
11257 upper = 1;
11259 if (!lower || !upper)
11260 return isl_bool_false;
11263 return isl_bool_true;
11266 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11268 if (!set)
11269 return isl_bool_error;
11270 if (set->n != 1)
11271 return isl_bool_false;
11273 return isl_basic_set_is_box(set->p[0]);
11276 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11278 if (!bset)
11279 return isl_bool_error;
11281 return isl_space_is_wrapping(bset->dim);
11284 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11286 if (!set)
11287 return isl_bool_error;
11289 return isl_space_is_wrapping(set->dim);
11292 /* Modify the space of "map" through a call to "change".
11293 * If "can_change" is set (not NULL), then first call it to check
11294 * if the modification is allowed, printing the error message "cannot_change"
11295 * if it is not.
11297 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11298 isl_bool (*can_change)(__isl_keep isl_map *map),
11299 const char *cannot_change,
11300 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11302 isl_bool ok;
11303 isl_space *space;
11305 if (!map)
11306 return NULL;
11308 ok = can_change ? can_change(map) : isl_bool_true;
11309 if (ok < 0)
11310 return isl_map_free(map);
11311 if (!ok)
11312 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11313 return isl_map_free(map));
11315 space = change(isl_map_get_space(map));
11316 map = isl_map_reset_space(map, space);
11318 return map;
11321 /* Is the domain of "map" a wrapped relation?
11323 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11325 if (!map)
11326 return isl_bool_error;
11328 return isl_space_domain_is_wrapping(map->dim);
11331 /* Does "map" have a wrapped relation in both domain and range?
11333 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11335 return isl_space_is_product(isl_map_peek_space(map));
11338 /* Is the range of "map" a wrapped relation?
11340 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11342 if (!map)
11343 return isl_bool_error;
11345 return isl_space_range_is_wrapping(map->dim);
11348 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11350 bmap = isl_basic_map_cow(bmap);
11351 if (!bmap)
11352 return NULL;
11354 bmap->dim = isl_space_wrap(bmap->dim);
11355 if (!bmap->dim)
11356 goto error;
11358 bmap = isl_basic_map_finalize(bmap);
11360 return bset_from_bmap(bmap);
11361 error:
11362 isl_basic_map_free(bmap);
11363 return NULL;
11366 /* Given a map A -> B, return the set (A -> B).
11368 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11370 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11373 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11375 bset = isl_basic_set_cow(bset);
11376 if (!bset)
11377 return NULL;
11379 bset->dim = isl_space_unwrap(bset->dim);
11380 if (!bset->dim)
11381 goto error;
11383 bset = isl_basic_set_finalize(bset);
11385 return bset_to_bmap(bset);
11386 error:
11387 isl_basic_set_free(bset);
11388 return NULL;
11391 /* Given a set (A -> B), return the map A -> B.
11392 * Error out if "set" is not of the form (A -> B).
11394 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11396 return isl_map_change_space(set, &isl_set_is_wrapping,
11397 "not a wrapping set", &isl_space_unwrap);
11400 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11401 enum isl_dim_type type)
11403 if (!bmap)
11404 return NULL;
11406 if (!isl_space_is_named_or_nested(bmap->dim, type))
11407 return bmap;
11409 bmap = isl_basic_map_cow(bmap);
11410 if (!bmap)
11411 return NULL;
11413 bmap->dim = isl_space_reset(bmap->dim, type);
11414 if (!bmap->dim)
11415 goto error;
11417 bmap = isl_basic_map_finalize(bmap);
11419 return bmap;
11420 error:
11421 isl_basic_map_free(bmap);
11422 return NULL;
11425 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11426 enum isl_dim_type type)
11428 int i;
11430 if (!map)
11431 return NULL;
11433 if (!isl_space_is_named_or_nested(map->dim, type))
11434 return map;
11436 map = isl_map_cow(map);
11437 if (!map)
11438 return NULL;
11440 for (i = 0; i < map->n; ++i) {
11441 map->p[i] = isl_basic_map_reset(map->p[i], type);
11442 if (!map->p[i])
11443 goto error;
11445 map->dim = isl_space_reset(map->dim, type);
11446 if (!map->dim)
11447 goto error;
11449 return map;
11450 error:
11451 isl_map_free(map);
11452 return NULL;
11455 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11457 if (!bmap)
11458 return NULL;
11460 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11461 return bmap;
11463 bmap = isl_basic_map_cow(bmap);
11464 if (!bmap)
11465 return NULL;
11467 bmap->dim = isl_space_flatten(bmap->dim);
11468 if (!bmap->dim)
11469 goto error;
11471 bmap = isl_basic_map_finalize(bmap);
11473 return bmap;
11474 error:
11475 isl_basic_map_free(bmap);
11476 return NULL;
11479 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11481 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11484 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11485 __isl_take isl_basic_map *bmap)
11487 if (!bmap)
11488 return NULL;
11490 if (!bmap->dim->nested[0])
11491 return bmap;
11493 bmap = isl_basic_map_cow(bmap);
11494 if (!bmap)
11495 return NULL;
11497 bmap->dim = isl_space_flatten_domain(bmap->dim);
11498 if (!bmap->dim)
11499 goto error;
11501 bmap = isl_basic_map_finalize(bmap);
11503 return bmap;
11504 error:
11505 isl_basic_map_free(bmap);
11506 return NULL;
11509 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11510 __isl_take isl_basic_map *bmap)
11512 if (!bmap)
11513 return NULL;
11515 if (!bmap->dim->nested[1])
11516 return bmap;
11518 bmap = isl_basic_map_cow(bmap);
11519 if (!bmap)
11520 return NULL;
11522 bmap->dim = isl_space_flatten_range(bmap->dim);
11523 if (!bmap->dim)
11524 goto error;
11526 bmap = isl_basic_map_finalize(bmap);
11528 return bmap;
11529 error:
11530 isl_basic_map_free(bmap);
11531 return NULL;
11534 /* Remove any internal structure from the spaces of domain and range of "map".
11536 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11538 if (!map)
11539 return NULL;
11541 if (!map->dim->nested[0] && !map->dim->nested[1])
11542 return map;
11544 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11547 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11549 return set_from_map(isl_map_flatten(set_to_map(set)));
11552 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11554 isl_space *space, *flat_space;
11555 isl_map *map;
11557 space = isl_set_get_space(set);
11558 flat_space = isl_space_flatten(isl_space_copy(space));
11559 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11560 flat_space));
11561 map = isl_map_intersect_domain(map, set);
11563 return map;
11566 /* Remove any internal structure from the space of the domain of "map".
11568 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11570 if (!map)
11571 return NULL;
11573 if (!map->dim->nested[0])
11574 return map;
11576 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11579 /* Remove any internal structure from the space of the range of "map".
11581 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11583 if (!map)
11584 return NULL;
11586 if (!map->dim->nested[1])
11587 return map;
11589 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11592 /* Reorder the dimensions of "bmap" according to the given dim_map
11593 * and set the dimension specification to "space" and
11594 * perform Gaussian elimination on the result.
11596 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11597 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11599 isl_basic_map *res;
11600 unsigned flags;
11601 unsigned n_div;
11603 if (!bmap || !space || !dim_map)
11604 goto error;
11606 flags = bmap->flags;
11607 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11608 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11609 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11610 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11611 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11612 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11613 if (res)
11614 res->flags = flags;
11615 res = isl_basic_map_gauss(res, NULL);
11616 res = isl_basic_map_finalize(res);
11617 return res;
11618 error:
11619 free(dim_map);
11620 isl_basic_map_free(bmap);
11621 isl_space_free(space);
11622 return NULL;
11625 /* Reorder the dimensions of "map" according to given reordering.
11627 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11628 __isl_take isl_reordering *r)
11630 int i;
11631 struct isl_dim_map *dim_map;
11633 map = isl_map_cow(map);
11634 dim_map = isl_dim_map_from_reordering(r);
11635 if (!map || !r || !dim_map)
11636 goto error;
11638 for (i = 0; i < map->n; ++i) {
11639 struct isl_dim_map *dim_map_i;
11640 isl_space *space;
11642 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11644 space = isl_reordering_get_space(r);
11645 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11647 if (!map->p[i])
11648 goto error;
11651 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11652 map = isl_map_unmark_normalized(map);
11654 isl_reordering_free(r);
11655 free(dim_map);
11656 return map;
11657 error:
11658 free(dim_map);
11659 isl_map_free(map);
11660 isl_reordering_free(r);
11661 return NULL;
11664 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11665 __isl_take isl_reordering *r)
11667 return set_from_map(isl_map_realign(set_to_map(set), r));
11670 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11671 __isl_take isl_space *model)
11673 isl_ctx *ctx;
11674 isl_bool aligned;
11676 if (!map || !model)
11677 goto error;
11679 ctx = isl_space_get_ctx(model);
11680 if (!isl_space_has_named_params(model))
11681 isl_die(ctx, isl_error_invalid,
11682 "model has unnamed parameters", goto error);
11683 if (isl_map_check_named_params(map) < 0)
11684 goto error;
11685 aligned = isl_map_space_has_equal_params(map, model);
11686 if (aligned < 0)
11687 goto error;
11688 if (!aligned) {
11689 isl_reordering *exp;
11691 exp = isl_parameter_alignment_reordering(map->dim, model);
11692 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11693 map = isl_map_realign(map, exp);
11696 isl_space_free(model);
11697 return map;
11698 error:
11699 isl_space_free(model);
11700 isl_map_free(map);
11701 return NULL;
11704 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11705 __isl_take isl_space *model)
11707 return isl_map_align_params(set, model);
11710 /* Align the parameters of "bmap" to those of "model", introducing
11711 * additional parameters if needed.
11713 __isl_give isl_basic_map *isl_basic_map_align_params(
11714 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11716 isl_ctx *ctx;
11717 isl_bool equal_params;
11719 if (!bmap || !model)
11720 goto error;
11722 ctx = isl_space_get_ctx(model);
11723 if (!isl_space_has_named_params(model))
11724 isl_die(ctx, isl_error_invalid,
11725 "model has unnamed parameters", goto error);
11726 if (isl_basic_map_check_named_params(bmap) < 0)
11727 goto error;
11728 equal_params = isl_space_has_equal_params(bmap->dim, model);
11729 if (equal_params < 0)
11730 goto error;
11731 if (!equal_params) {
11732 isl_reordering *exp;
11733 struct isl_dim_map *dim_map;
11735 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11736 exp = isl_reordering_extend_space(exp,
11737 isl_basic_map_get_space(bmap));
11738 dim_map = isl_dim_map_from_reordering(exp);
11739 bmap = isl_basic_map_realign(bmap,
11740 isl_reordering_get_space(exp),
11741 isl_dim_map_extend(dim_map, bmap));
11742 isl_reordering_free(exp);
11743 free(dim_map);
11746 isl_space_free(model);
11747 return bmap;
11748 error:
11749 isl_space_free(model);
11750 isl_basic_map_free(bmap);
11751 return NULL;
11754 /* Do "bset" and "space" have the same parameters?
11756 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11757 __isl_keep isl_space *space)
11759 isl_space *bset_space;
11761 bset_space = isl_basic_set_peek_space(bset);
11762 return isl_space_has_equal_params(bset_space, space);
11765 /* Do "map" and "space" have the same parameters?
11767 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11768 __isl_keep isl_space *space)
11770 isl_space *map_space;
11772 map_space = isl_map_peek_space(map);
11773 return isl_space_has_equal_params(map_space, space);
11776 /* Do "set" and "space" have the same parameters?
11778 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11779 __isl_keep isl_space *space)
11781 return isl_map_space_has_equal_params(set_to_map(set), space);
11784 /* Align the parameters of "bset" to those of "model", introducing
11785 * additional parameters if needed.
11787 __isl_give isl_basic_set *isl_basic_set_align_params(
11788 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11790 return isl_basic_map_align_params(bset, model);
11793 /* Drop all parameters not referenced by "map".
11795 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11797 int i;
11799 if (isl_map_check_named_params(map) < 0)
11800 return isl_map_free(map);
11802 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11803 isl_bool involves;
11805 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11806 if (involves < 0)
11807 return isl_map_free(map);
11808 if (!involves)
11809 map = isl_map_project_out(map, isl_dim_param, i, 1);
11812 return map;
11815 /* Drop all parameters not referenced by "set".
11817 __isl_give isl_set *isl_set_drop_unused_params(
11818 __isl_take isl_set *set)
11820 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11823 /* Drop all parameters not referenced by "bmap".
11825 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11826 __isl_take isl_basic_map *bmap)
11828 int i;
11830 if (isl_basic_map_check_named_params(bmap) < 0)
11831 return isl_basic_map_free(bmap);
11833 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11834 isl_bool involves;
11836 involves = isl_basic_map_involves_dims(bmap,
11837 isl_dim_param, i, 1);
11838 if (involves < 0)
11839 return isl_basic_map_free(bmap);
11840 if (!involves)
11841 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11844 return bmap;
11847 /* Drop all parameters not referenced by "bset".
11849 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11850 __isl_take isl_basic_set *bset)
11852 return bset_from_bmap(isl_basic_map_drop_unused_params(
11853 bset_to_bmap(bset)));
11856 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11857 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11858 enum isl_dim_type c2, enum isl_dim_type c3,
11859 enum isl_dim_type c4, enum isl_dim_type c5)
11861 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11862 struct isl_mat *mat;
11863 int i, j, k;
11864 int pos;
11866 if (!bmap)
11867 return NULL;
11868 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11869 isl_basic_map_total_dim(bmap) + 1);
11870 if (!mat)
11871 return NULL;
11872 for (i = 0; i < bmap->n_eq; ++i)
11873 for (j = 0, pos = 0; j < 5; ++j) {
11874 int off = isl_basic_map_offset(bmap, c[j]);
11875 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11876 isl_int_set(mat->row[i][pos],
11877 bmap->eq[i][off + k]);
11878 ++pos;
11882 return mat;
11885 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11886 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11887 enum isl_dim_type c2, enum isl_dim_type c3,
11888 enum isl_dim_type c4, enum isl_dim_type c5)
11890 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11891 struct isl_mat *mat;
11892 int i, j, k;
11893 int pos;
11895 if (!bmap)
11896 return NULL;
11897 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11898 isl_basic_map_total_dim(bmap) + 1);
11899 if (!mat)
11900 return NULL;
11901 for (i = 0; i < bmap->n_ineq; ++i)
11902 for (j = 0, pos = 0; j < 5; ++j) {
11903 int off = isl_basic_map_offset(bmap, c[j]);
11904 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11905 isl_int_set(mat->row[i][pos],
11906 bmap->ineq[i][off + k]);
11907 ++pos;
11911 return mat;
11914 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11915 __isl_take isl_space *space,
11916 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11917 enum isl_dim_type c2, enum isl_dim_type c3,
11918 enum isl_dim_type c4, enum isl_dim_type c5)
11920 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11921 isl_basic_map *bmap = NULL;
11922 unsigned total;
11923 unsigned extra;
11924 int i, j, k, l;
11925 int pos;
11927 if (!space || !eq || !ineq)
11928 goto error;
11930 if (eq->n_col != ineq->n_col)
11931 isl_die(space->ctx, isl_error_invalid,
11932 "equalities and inequalities matrices should have "
11933 "same number of columns", goto error);
11935 total = 1 + isl_space_dim(space, isl_dim_all);
11937 if (eq->n_col < total)
11938 isl_die(space->ctx, isl_error_invalid,
11939 "number of columns too small", goto error);
11941 extra = eq->n_col - total;
11943 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11944 eq->n_row, ineq->n_row);
11945 if (!bmap)
11946 goto error;
11947 for (i = 0; i < extra; ++i) {
11948 k = isl_basic_map_alloc_div(bmap);
11949 if (k < 0)
11950 goto error;
11951 isl_int_set_si(bmap->div[k][0], 0);
11953 for (i = 0; i < eq->n_row; ++i) {
11954 l = isl_basic_map_alloc_equality(bmap);
11955 if (l < 0)
11956 goto error;
11957 for (j = 0, pos = 0; j < 5; ++j) {
11958 int off = isl_basic_map_offset(bmap, c[j]);
11959 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11960 isl_int_set(bmap->eq[l][off + k],
11961 eq->row[i][pos]);
11962 ++pos;
11966 for (i = 0; i < ineq->n_row; ++i) {
11967 l = isl_basic_map_alloc_inequality(bmap);
11968 if (l < 0)
11969 goto error;
11970 for (j = 0, pos = 0; j < 5; ++j) {
11971 int off = isl_basic_map_offset(bmap, c[j]);
11972 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11973 isl_int_set(bmap->ineq[l][off + k],
11974 ineq->row[i][pos]);
11975 ++pos;
11980 isl_space_free(space);
11981 isl_mat_free(eq);
11982 isl_mat_free(ineq);
11984 bmap = isl_basic_map_simplify(bmap);
11985 return isl_basic_map_finalize(bmap);
11986 error:
11987 isl_space_free(space);
11988 isl_mat_free(eq);
11989 isl_mat_free(ineq);
11990 isl_basic_map_free(bmap);
11991 return NULL;
11994 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11995 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11996 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11998 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11999 c1, c2, c3, c4, isl_dim_in);
12002 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12003 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12004 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12006 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12007 c1, c2, c3, c4, isl_dim_in);
12010 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12011 __isl_take isl_space *dim,
12012 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12013 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12015 isl_basic_map *bmap;
12016 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12017 c1, c2, c3, c4, isl_dim_in);
12018 return bset_from_bmap(bmap);
12021 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12023 if (!bmap)
12024 return isl_bool_error;
12026 return isl_space_can_zip(bmap->dim);
12029 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12031 if (!map)
12032 return isl_bool_error;
12034 return isl_space_can_zip(map->dim);
12037 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12038 * (A -> C) -> (B -> D).
12040 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12042 unsigned pos;
12043 unsigned n1;
12044 unsigned n2;
12046 if (!bmap)
12047 return NULL;
12049 if (!isl_basic_map_can_zip(bmap))
12050 isl_die(bmap->ctx, isl_error_invalid,
12051 "basic map cannot be zipped", goto error);
12052 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12053 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12054 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12055 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12056 bmap = isl_basic_map_cow(bmap);
12057 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12058 if (!bmap)
12059 return NULL;
12060 bmap->dim = isl_space_zip(bmap->dim);
12061 if (!bmap->dim)
12062 goto error;
12063 bmap = isl_basic_map_mark_final(bmap);
12064 return bmap;
12065 error:
12066 isl_basic_map_free(bmap);
12067 return NULL;
12070 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12071 * (A -> C) -> (B -> D).
12073 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12075 int i;
12077 if (!map)
12078 return NULL;
12080 if (!isl_map_can_zip(map))
12081 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12082 goto error);
12084 map = isl_map_cow(map);
12085 if (!map)
12086 return NULL;
12088 for (i = 0; i < map->n; ++i) {
12089 map->p[i] = isl_basic_map_zip(map->p[i]);
12090 if (!map->p[i])
12091 goto error;
12094 map->dim = isl_space_zip(map->dim);
12095 if (!map->dim)
12096 goto error;
12098 return map;
12099 error:
12100 isl_map_free(map);
12101 return NULL;
12104 /* Can we apply isl_basic_map_curry to "bmap"?
12105 * That is, does it have a nested relation in its domain?
12107 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12109 if (!bmap)
12110 return isl_bool_error;
12112 return isl_space_can_curry(bmap->dim);
12115 /* Can we apply isl_map_curry to "map"?
12116 * That is, does it have a nested relation in its domain?
12118 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12120 if (!map)
12121 return isl_bool_error;
12123 return isl_space_can_curry(map->dim);
12126 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12127 * A -> (B -> C).
12129 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12132 if (!bmap)
12133 return NULL;
12135 if (!isl_basic_map_can_curry(bmap))
12136 isl_die(bmap->ctx, isl_error_invalid,
12137 "basic map cannot be curried", goto error);
12138 bmap = isl_basic_map_cow(bmap);
12139 if (!bmap)
12140 return NULL;
12141 bmap->dim = isl_space_curry(bmap->dim);
12142 if (!bmap->dim)
12143 goto error;
12144 bmap = isl_basic_map_mark_final(bmap);
12145 return bmap;
12146 error:
12147 isl_basic_map_free(bmap);
12148 return NULL;
12151 /* Given a map (A -> B) -> C, return the corresponding map
12152 * A -> (B -> C).
12154 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12156 return isl_map_change_space(map, &isl_map_can_curry,
12157 "map cannot be curried", &isl_space_curry);
12160 /* Can isl_map_range_curry be applied to "map"?
12161 * That is, does it have a nested relation in its range,
12162 * the domain of which is itself a nested relation?
12164 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12166 if (!map)
12167 return isl_bool_error;
12169 return isl_space_can_range_curry(map->dim);
12172 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12173 * A -> (B -> (C -> D)).
12175 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12177 return isl_map_change_space(map, &isl_map_can_range_curry,
12178 "map range cannot be curried",
12179 &isl_space_range_curry);
12182 /* Can we apply isl_basic_map_uncurry to "bmap"?
12183 * That is, does it have a nested relation in its domain?
12185 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12187 if (!bmap)
12188 return isl_bool_error;
12190 return isl_space_can_uncurry(bmap->dim);
12193 /* Can we apply isl_map_uncurry to "map"?
12194 * That is, does it have a nested relation in its domain?
12196 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12198 if (!map)
12199 return isl_bool_error;
12201 return isl_space_can_uncurry(map->dim);
12204 /* Given a basic map A -> (B -> C), return the corresponding basic map
12205 * (A -> B) -> C.
12207 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12210 if (!bmap)
12211 return NULL;
12213 if (!isl_basic_map_can_uncurry(bmap))
12214 isl_die(bmap->ctx, isl_error_invalid,
12215 "basic map cannot be uncurried",
12216 return isl_basic_map_free(bmap));
12217 bmap = isl_basic_map_cow(bmap);
12218 if (!bmap)
12219 return NULL;
12220 bmap->dim = isl_space_uncurry(bmap->dim);
12221 if (!bmap->dim)
12222 return isl_basic_map_free(bmap);
12223 bmap = isl_basic_map_mark_final(bmap);
12224 return bmap;
12227 /* Given a map A -> (B -> C), return the corresponding map
12228 * (A -> B) -> C.
12230 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12232 return isl_map_change_space(map, &isl_map_can_uncurry,
12233 "map cannot be uncurried", &isl_space_uncurry);
12236 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12237 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12239 return isl_map_equate(set, type1, pos1, type2, pos2);
12242 /* Construct a basic map where the given dimensions are equal to each other.
12244 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12245 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12247 isl_basic_map *bmap = NULL;
12248 int i;
12250 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12251 isl_space_check_range(space, type2, pos2, 1) < 0)
12252 goto error;
12254 if (type1 == type2 && pos1 == pos2)
12255 return isl_basic_map_universe(space);
12257 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12258 i = isl_basic_map_alloc_equality(bmap);
12259 if (i < 0)
12260 goto error;
12261 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12262 pos1 += isl_basic_map_offset(bmap, type1);
12263 pos2 += isl_basic_map_offset(bmap, type2);
12264 isl_int_set_si(bmap->eq[i][pos1], -1);
12265 isl_int_set_si(bmap->eq[i][pos2], 1);
12266 bmap = isl_basic_map_finalize(bmap);
12267 isl_space_free(space);
12268 return bmap;
12269 error:
12270 isl_space_free(space);
12271 isl_basic_map_free(bmap);
12272 return NULL;
12275 /* Add a constraint imposing that the given two dimensions are equal.
12277 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12278 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12280 isl_basic_map *eq;
12282 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12284 bmap = isl_basic_map_intersect(bmap, eq);
12286 return bmap;
12289 /* Add a constraint imposing that the given two dimensions are equal.
12291 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12292 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12294 isl_basic_map *bmap;
12296 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12298 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12300 return map;
12303 /* Add a constraint imposing that the given two dimensions have opposite values.
12305 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12306 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12308 isl_basic_map *bmap = NULL;
12309 int i;
12311 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12312 return isl_map_free(map);
12313 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12314 return isl_map_free(map);
12316 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12317 i = isl_basic_map_alloc_equality(bmap);
12318 if (i < 0)
12319 goto error;
12320 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12321 pos1 += isl_basic_map_offset(bmap, type1);
12322 pos2 += isl_basic_map_offset(bmap, type2);
12323 isl_int_set_si(bmap->eq[i][pos1], 1);
12324 isl_int_set_si(bmap->eq[i][pos2], 1);
12325 bmap = isl_basic_map_finalize(bmap);
12327 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12329 return map;
12330 error:
12331 isl_basic_map_free(bmap);
12332 isl_map_free(map);
12333 return NULL;
12336 /* Construct a constraint imposing that the value of the first dimension is
12337 * greater than or equal to that of the second.
12339 static __isl_give isl_constraint *constraint_order_ge(
12340 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12341 enum isl_dim_type type2, int pos2)
12343 isl_constraint *c;
12345 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12346 isl_space_check_range(space, type2, pos2, 1) < 0)
12347 space = isl_space_free(space);
12348 if (!space)
12349 return NULL;
12351 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12353 if (type1 == type2 && pos1 == pos2)
12354 return c;
12356 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12357 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12359 return c;
12362 /* Add a constraint imposing that the value of the first dimension is
12363 * greater than or equal to that of the second.
12365 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12366 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12368 isl_constraint *c;
12369 isl_space *space;
12371 if (type1 == type2 && pos1 == pos2)
12372 return bmap;
12373 space = isl_basic_map_get_space(bmap);
12374 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12375 bmap = isl_basic_map_add_constraint(bmap, c);
12377 return bmap;
12380 /* Add a constraint imposing that the value of the first dimension is
12381 * greater than or equal to that of the second.
12383 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12384 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12386 isl_constraint *c;
12387 isl_space *space;
12389 if (type1 == type2 && pos1 == pos2)
12390 return map;
12391 space = isl_map_get_space(map);
12392 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12393 map = isl_map_add_constraint(map, c);
12395 return map;
12398 /* Add a constraint imposing that the value of the first dimension is
12399 * less than or equal to that of the second.
12401 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12402 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12404 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12407 /* Construct a basic map where the value of the first dimension is
12408 * greater than that of the second.
12410 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12411 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12413 isl_basic_map *bmap = NULL;
12414 int i;
12416 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12417 isl_space_check_range(space, type2, pos2, 1) < 0)
12418 goto error;
12420 if (type1 == type2 && pos1 == pos2)
12421 return isl_basic_map_empty(space);
12423 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12424 i = isl_basic_map_alloc_inequality(bmap);
12425 if (i < 0)
12426 return isl_basic_map_free(bmap);
12427 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12428 pos1 += isl_basic_map_offset(bmap, type1);
12429 pos2 += isl_basic_map_offset(bmap, type2);
12430 isl_int_set_si(bmap->ineq[i][pos1], 1);
12431 isl_int_set_si(bmap->ineq[i][pos2], -1);
12432 isl_int_set_si(bmap->ineq[i][0], -1);
12433 bmap = isl_basic_map_finalize(bmap);
12435 return bmap;
12436 error:
12437 isl_space_free(space);
12438 isl_basic_map_free(bmap);
12439 return NULL;
12442 /* Add a constraint imposing that the value of the first dimension is
12443 * greater than that of the second.
12445 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12446 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12448 isl_basic_map *gt;
12450 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12452 bmap = isl_basic_map_intersect(bmap, gt);
12454 return bmap;
12457 /* Add a constraint imposing that the value of the first dimension is
12458 * greater than that of the second.
12460 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12461 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12463 isl_basic_map *bmap;
12465 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12467 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12469 return map;
12472 /* Add a constraint imposing that the value of the first dimension is
12473 * smaller than that of the second.
12475 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12476 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12478 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12481 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12482 int pos)
12484 isl_aff *div;
12485 isl_local_space *ls;
12487 if (!bmap)
12488 return NULL;
12490 if (!isl_basic_map_divs_known(bmap))
12491 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12492 "some divs are unknown", return NULL);
12494 ls = isl_basic_map_get_local_space(bmap);
12495 div = isl_local_space_get_div(ls, pos);
12496 isl_local_space_free(ls);
12498 return div;
12501 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12502 int pos)
12504 return isl_basic_map_get_div(bset, pos);
12507 /* Plug in "subs" for dimension "type", "pos" of "bset".
12509 * Let i be the dimension to replace and let "subs" be of the form
12511 * f/d
12513 * Any integer division with a non-zero coefficient for i,
12515 * floor((a i + g)/m)
12517 * is replaced by
12519 * floor((a f + d g)/(m d))
12521 * Constraints of the form
12523 * a i + g
12525 * are replaced by
12527 * a f + d g
12529 * We currently require that "subs" is an integral expression.
12530 * Handling rational expressions may require us to add stride constraints
12531 * as we do in isl_basic_set_preimage_multi_aff.
12533 __isl_give isl_basic_set *isl_basic_set_substitute(
12534 __isl_take isl_basic_set *bset,
12535 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12537 int i;
12538 isl_int v;
12539 isl_ctx *ctx;
12541 if (bset && isl_basic_set_plain_is_empty(bset))
12542 return bset;
12544 bset = isl_basic_set_cow(bset);
12545 if (!bset || !subs)
12546 goto error;
12548 ctx = isl_basic_set_get_ctx(bset);
12549 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12550 isl_die(ctx, isl_error_invalid,
12551 "spaces don't match", goto error);
12552 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12553 isl_die(ctx, isl_error_unsupported,
12554 "cannot handle divs yet", goto error);
12555 if (!isl_int_is_one(subs->v->el[0]))
12556 isl_die(ctx, isl_error_invalid,
12557 "can only substitute integer expressions", goto error);
12559 pos += isl_basic_set_offset(bset, type);
12561 isl_int_init(v);
12563 for (i = 0; i < bset->n_eq; ++i) {
12564 if (isl_int_is_zero(bset->eq[i][pos]))
12565 continue;
12566 isl_int_set(v, bset->eq[i][pos]);
12567 isl_int_set_si(bset->eq[i][pos], 0);
12568 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12569 v, subs->v->el + 1, subs->v->size - 1);
12572 for (i = 0; i < bset->n_ineq; ++i) {
12573 if (isl_int_is_zero(bset->ineq[i][pos]))
12574 continue;
12575 isl_int_set(v, bset->ineq[i][pos]);
12576 isl_int_set_si(bset->ineq[i][pos], 0);
12577 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12578 v, subs->v->el + 1, subs->v->size - 1);
12581 for (i = 0; i < bset->n_div; ++i) {
12582 if (isl_int_is_zero(bset->div[i][1 + pos]))
12583 continue;
12584 isl_int_set(v, bset->div[i][1 + pos]);
12585 isl_int_set_si(bset->div[i][1 + pos], 0);
12586 isl_seq_combine(bset->div[i] + 1,
12587 subs->v->el[0], bset->div[i] + 1,
12588 v, subs->v->el + 1, subs->v->size - 1);
12589 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12592 isl_int_clear(v);
12594 bset = isl_basic_set_simplify(bset);
12595 return isl_basic_set_finalize(bset);
12596 error:
12597 isl_basic_set_free(bset);
12598 return NULL;
12601 /* Plug in "subs" for dimension "type", "pos" of "set".
12603 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12604 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12606 int i;
12608 if (set && isl_set_plain_is_empty(set))
12609 return set;
12611 set = isl_set_cow(set);
12612 if (!set || !subs)
12613 goto error;
12615 for (i = set->n - 1; i >= 0; --i) {
12616 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12617 set = set_from_map(remove_if_empty(set_to_map(set), i));
12618 if (!set)
12619 return NULL;
12622 return set;
12623 error:
12624 isl_set_free(set);
12625 return NULL;
12628 /* Check if the range of "ma" is compatible with the domain or range
12629 * (depending on "type") of "bmap".
12631 static isl_stat check_basic_map_compatible_range_multi_aff(
12632 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12633 __isl_keep isl_multi_aff *ma)
12635 isl_bool m;
12636 isl_space *ma_space;
12638 ma_space = isl_multi_aff_get_space(ma);
12640 m = isl_space_has_equal_params(bmap->dim, ma_space);
12641 if (m < 0)
12642 goto error;
12643 if (!m)
12644 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12645 "parameters don't match", goto error);
12646 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12647 if (m < 0)
12648 goto error;
12649 if (!m)
12650 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12651 "spaces don't match", goto error);
12653 isl_space_free(ma_space);
12654 return isl_stat_ok;
12655 error:
12656 isl_space_free(ma_space);
12657 return isl_stat_error;
12660 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12661 * coefficients before the transformed range of dimensions,
12662 * the "n_after" coefficients after the transformed range of dimensions
12663 * and the coefficients of the other divs in "bmap".
12665 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12666 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12668 int i;
12669 int n_param;
12670 int n_set;
12671 isl_local_space *ls;
12673 if (n_div == 0)
12674 return 0;
12676 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12677 if (!ls)
12678 return -1;
12680 n_param = isl_local_space_dim(ls, isl_dim_param);
12681 n_set = isl_local_space_dim(ls, isl_dim_set);
12682 for (i = 0; i < n_div; ++i) {
12683 int o_bmap = 0, o_ls = 0;
12685 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12686 o_bmap += 1 + 1 + n_param;
12687 o_ls += 1 + 1 + n_param;
12688 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12689 o_bmap += n_before;
12690 isl_seq_cpy(bmap->div[i] + o_bmap,
12691 ls->div->row[i] + o_ls, n_set);
12692 o_bmap += n_set;
12693 o_ls += n_set;
12694 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12695 o_bmap += n_after;
12696 isl_seq_cpy(bmap->div[i] + o_bmap,
12697 ls->div->row[i] + o_ls, n_div);
12698 o_bmap += n_div;
12699 o_ls += n_div;
12700 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12701 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12702 goto error;
12705 isl_local_space_free(ls);
12706 return 0;
12707 error:
12708 isl_local_space_free(ls);
12709 return -1;
12712 /* How many stride constraints does "ma" enforce?
12713 * That is, how many of the affine expressions have a denominator
12714 * different from one?
12716 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12718 int i;
12719 int strides = 0;
12721 for (i = 0; i < ma->n; ++i)
12722 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12723 strides++;
12725 return strides;
12728 /* For each affine expression in ma of the form
12730 * x_i = (f_i y + h_i)/m_i
12732 * with m_i different from one, add a constraint to "bmap"
12733 * of the form
12735 * f_i y + h_i = m_i alpha_i
12737 * with alpha_i an additional existentially quantified variable.
12739 * The input variables of "ma" correspond to a subset of the variables
12740 * of "bmap". There are "n_before" variables in "bmap" before this
12741 * subset and "n_after" variables after this subset.
12742 * The integer divisions of the affine expressions in "ma" are assumed
12743 * to have been aligned. There are "n_div_ma" of them and
12744 * they appear first in "bmap", straight after the "n_after" variables.
12746 static __isl_give isl_basic_map *add_ma_strides(
12747 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12748 int n_before, int n_after, int n_div_ma)
12750 int i, k;
12751 int div;
12752 int total;
12753 int n_param;
12754 int n_in;
12756 total = isl_basic_map_total_dim(bmap);
12757 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12758 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12759 for (i = 0; i < ma->n; ++i) {
12760 int o_bmap = 0, o_ma = 1;
12762 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12763 continue;
12764 div = isl_basic_map_alloc_div(bmap);
12765 k = isl_basic_map_alloc_equality(bmap);
12766 if (div < 0 || k < 0)
12767 goto error;
12768 isl_int_set_si(bmap->div[div][0], 0);
12769 isl_seq_cpy(bmap->eq[k] + o_bmap,
12770 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12771 o_bmap += 1 + n_param;
12772 o_ma += 1 + n_param;
12773 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12774 o_bmap += n_before;
12775 isl_seq_cpy(bmap->eq[k] + o_bmap,
12776 ma->u.p[i]->v->el + o_ma, n_in);
12777 o_bmap += n_in;
12778 o_ma += n_in;
12779 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12780 o_bmap += n_after;
12781 isl_seq_cpy(bmap->eq[k] + o_bmap,
12782 ma->u.p[i]->v->el + o_ma, n_div_ma);
12783 o_bmap += n_div_ma;
12784 o_ma += n_div_ma;
12785 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12786 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12787 total++;
12790 return bmap;
12791 error:
12792 isl_basic_map_free(bmap);
12793 return NULL;
12796 /* Replace the domain or range space (depending on "type) of "space" by "set".
12798 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12799 enum isl_dim_type type, __isl_take isl_space *set)
12801 if (type == isl_dim_in) {
12802 space = isl_space_range(space);
12803 space = isl_space_map_from_domain_and_range(set, space);
12804 } else {
12805 space = isl_space_domain(space);
12806 space = isl_space_map_from_domain_and_range(space, set);
12809 return space;
12812 /* Compute the preimage of the domain or range (depending on "type")
12813 * of "bmap" under the function represented by "ma".
12814 * In other words, plug in "ma" in the domain or range of "bmap".
12815 * The result is a basic map that lives in the same space as "bmap"
12816 * except that the domain or range has been replaced by
12817 * the domain space of "ma".
12819 * If bmap is represented by
12821 * A(p) + S u + B x + T v + C(divs) >= 0,
12823 * where u and x are input and output dimensions if type == isl_dim_out
12824 * while x and v are input and output dimensions if type == isl_dim_in,
12825 * and ma is represented by
12827 * x = D(p) + F(y) + G(divs')
12829 * then the result is
12831 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12833 * The divs in the input set are similarly adjusted.
12834 * In particular
12836 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12838 * becomes
12840 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12841 * B_i G(divs') + c_i(divs))/n_i)
12843 * If bmap is not a rational map and if F(y) involves any denominators
12845 * x_i = (f_i y + h_i)/m_i
12847 * then additional constraints are added to ensure that we only
12848 * map back integer points. That is we enforce
12850 * f_i y + h_i = m_i alpha_i
12852 * with alpha_i an additional existentially quantified variable.
12854 * We first copy over the divs from "ma".
12855 * Then we add the modified constraints and divs from "bmap".
12856 * Finally, we add the stride constraints, if needed.
12858 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12859 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12860 __isl_take isl_multi_aff *ma)
12862 int i, k;
12863 isl_space *space;
12864 isl_basic_map *res = NULL;
12865 int n_before, n_after, n_div_bmap, n_div_ma;
12866 isl_int f, c1, c2, g;
12867 isl_bool rational;
12868 int strides;
12870 isl_int_init(f);
12871 isl_int_init(c1);
12872 isl_int_init(c2);
12873 isl_int_init(g);
12875 ma = isl_multi_aff_align_divs(ma);
12876 if (!bmap || !ma)
12877 goto error;
12878 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12879 goto error;
12881 if (type == isl_dim_in) {
12882 n_before = 0;
12883 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12884 } else {
12885 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12886 n_after = 0;
12888 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12889 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12891 space = isl_multi_aff_get_domain_space(ma);
12892 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12893 rational = isl_basic_map_is_rational(bmap);
12894 strides = rational ? 0 : multi_aff_strides(ma);
12895 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12896 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12897 if (rational)
12898 res = isl_basic_map_set_rational(res);
12900 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12901 if (isl_basic_map_alloc_div(res) < 0)
12902 goto error;
12904 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12905 goto error;
12907 for (i = 0; i < bmap->n_eq; ++i) {
12908 k = isl_basic_map_alloc_equality(res);
12909 if (k < 0)
12910 goto error;
12911 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12912 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12915 for (i = 0; i < bmap->n_ineq; ++i) {
12916 k = isl_basic_map_alloc_inequality(res);
12917 if (k < 0)
12918 goto error;
12919 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12920 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12923 for (i = 0; i < bmap->n_div; ++i) {
12924 if (isl_int_is_zero(bmap->div[i][0])) {
12925 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12926 continue;
12928 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12929 n_before, n_after, n_div_ma, n_div_bmap,
12930 f, c1, c2, g, 1);
12933 if (strides)
12934 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12936 isl_int_clear(f);
12937 isl_int_clear(c1);
12938 isl_int_clear(c2);
12939 isl_int_clear(g);
12940 isl_basic_map_free(bmap);
12941 isl_multi_aff_free(ma);
12942 res = isl_basic_map_simplify(res);
12943 return isl_basic_map_finalize(res);
12944 error:
12945 isl_int_clear(f);
12946 isl_int_clear(c1);
12947 isl_int_clear(c2);
12948 isl_int_clear(g);
12949 isl_basic_map_free(bmap);
12950 isl_multi_aff_free(ma);
12951 isl_basic_map_free(res);
12952 return NULL;
12955 /* Compute the preimage of "bset" under the function represented by "ma".
12956 * In other words, plug in "ma" in "bset". The result is a basic set
12957 * that lives in the domain space of "ma".
12959 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12960 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12962 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12965 /* Compute the preimage of the domain of "bmap" under the function
12966 * represented by "ma".
12967 * In other words, plug in "ma" in the domain of "bmap".
12968 * The result is a basic map that lives in the same space as "bmap"
12969 * except that the domain has been replaced by the domain space of "ma".
12971 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12972 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12974 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12977 /* Compute the preimage of the range of "bmap" under the function
12978 * represented by "ma".
12979 * In other words, plug in "ma" in the range of "bmap".
12980 * The result is a basic map that lives in the same space as "bmap"
12981 * except that the range has been replaced by the domain space of "ma".
12983 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12984 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12986 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12989 /* Check if the range of "ma" is compatible with the domain or range
12990 * (depending on "type") of "map".
12991 * Return isl_stat_error if anything is wrong.
12993 static isl_stat check_map_compatible_range_multi_aff(
12994 __isl_keep isl_map *map, enum isl_dim_type type,
12995 __isl_keep isl_multi_aff *ma)
12997 isl_bool m;
12998 isl_space *ma_space;
13000 ma_space = isl_multi_aff_get_space(ma);
13001 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13002 isl_space_free(ma_space);
13003 if (m < 0)
13004 return isl_stat_error;
13005 if (!m)
13006 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13007 "spaces don't match", return isl_stat_error);
13008 return isl_stat_ok;
13011 /* Compute the preimage of the domain or range (depending on "type")
13012 * of "map" under the function represented by "ma".
13013 * In other words, plug in "ma" in the domain or range of "map".
13014 * The result is a map that lives in the same space as "map"
13015 * except that the domain or range has been replaced by
13016 * the domain space of "ma".
13018 * The parameters are assumed to have been aligned.
13020 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13021 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13023 int i;
13024 isl_space *space;
13026 map = isl_map_cow(map);
13027 ma = isl_multi_aff_align_divs(ma);
13028 if (!map || !ma)
13029 goto error;
13030 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13031 goto error;
13033 for (i = 0; i < map->n; ++i) {
13034 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13035 isl_multi_aff_copy(ma));
13036 if (!map->p[i])
13037 goto error;
13040 space = isl_multi_aff_get_domain_space(ma);
13041 space = isl_space_set(isl_map_get_space(map), type, space);
13043 isl_space_free(map->dim);
13044 map->dim = space;
13045 if (!map->dim)
13046 goto error;
13048 isl_multi_aff_free(ma);
13049 if (map->n > 1)
13050 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13051 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13052 return map;
13053 error:
13054 isl_multi_aff_free(ma);
13055 isl_map_free(map);
13056 return NULL;
13059 /* Compute the preimage of the domain or range (depending on "type")
13060 * of "map" under the function represented by "ma".
13061 * In other words, plug in "ma" in the domain or range of "map".
13062 * The result is a map that lives in the same space as "map"
13063 * except that the domain or range has been replaced by
13064 * the domain space of "ma".
13066 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13067 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13069 isl_bool aligned;
13071 if (!map || !ma)
13072 goto error;
13074 aligned = isl_map_space_has_equal_params(map, ma->space);
13075 if (aligned < 0)
13076 goto error;
13077 if (aligned)
13078 return map_preimage_multi_aff(map, type, ma);
13080 if (isl_map_check_named_params(map) < 0)
13081 goto error;
13082 if (!isl_space_has_named_params(ma->space))
13083 isl_die(map->ctx, isl_error_invalid,
13084 "unaligned unnamed parameters", goto error);
13085 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13086 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13088 return map_preimage_multi_aff(map, type, ma);
13089 error:
13090 isl_multi_aff_free(ma);
13091 return isl_map_free(map);
13094 /* Compute the preimage of "set" under the function represented by "ma".
13095 * In other words, plug in "ma" in "set". The result is a set
13096 * that lives in the domain space of "ma".
13098 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13099 __isl_take isl_multi_aff *ma)
13101 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13104 /* Compute the preimage of the domain of "map" under the function
13105 * represented by "ma".
13106 * In other words, plug in "ma" in the domain of "map".
13107 * The result is a map that lives in the same space as "map"
13108 * except that the domain has been replaced by the domain space of "ma".
13110 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13111 __isl_take isl_multi_aff *ma)
13113 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13116 /* Compute the preimage of the range of "map" under the function
13117 * represented by "ma".
13118 * In other words, plug in "ma" in the range of "map".
13119 * The result is a map that lives in the same space as "map"
13120 * except that the range has been replaced by the domain space of "ma".
13122 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13123 __isl_take isl_multi_aff *ma)
13125 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13128 /* Compute the preimage of "map" under the function represented by "pma".
13129 * In other words, plug in "pma" in the domain or range of "map".
13130 * The result is a map that lives in the same space as "map",
13131 * except that the space of type "type" has been replaced by
13132 * the domain space of "pma".
13134 * The parameters of "map" and "pma" are assumed to have been aligned.
13136 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13137 __isl_take isl_map *map, enum isl_dim_type type,
13138 __isl_take isl_pw_multi_aff *pma)
13140 int i;
13141 isl_map *res;
13143 if (!pma)
13144 goto error;
13146 if (pma->n == 0) {
13147 isl_pw_multi_aff_free(pma);
13148 res = isl_map_empty(isl_map_get_space(map));
13149 isl_map_free(map);
13150 return res;
13153 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13154 isl_multi_aff_copy(pma->p[0].maff));
13155 if (type == isl_dim_in)
13156 res = isl_map_intersect_domain(res,
13157 isl_map_copy(pma->p[0].set));
13158 else
13159 res = isl_map_intersect_range(res,
13160 isl_map_copy(pma->p[0].set));
13162 for (i = 1; i < pma->n; ++i) {
13163 isl_map *res_i;
13165 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13166 isl_multi_aff_copy(pma->p[i].maff));
13167 if (type == isl_dim_in)
13168 res_i = isl_map_intersect_domain(res_i,
13169 isl_map_copy(pma->p[i].set));
13170 else
13171 res_i = isl_map_intersect_range(res_i,
13172 isl_map_copy(pma->p[i].set));
13173 res = isl_map_union(res, res_i);
13176 isl_pw_multi_aff_free(pma);
13177 isl_map_free(map);
13178 return res;
13179 error:
13180 isl_pw_multi_aff_free(pma);
13181 isl_map_free(map);
13182 return NULL;
13185 /* Compute the preimage of "map" under the function represented by "pma".
13186 * In other words, plug in "pma" in the domain or range of "map".
13187 * The result is a map that lives in the same space as "map",
13188 * except that the space of type "type" has been replaced by
13189 * the domain space of "pma".
13191 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13192 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13194 isl_bool aligned;
13196 if (!map || !pma)
13197 goto error;
13199 aligned = isl_map_space_has_equal_params(map, pma->dim);
13200 if (aligned < 0)
13201 goto error;
13202 if (aligned)
13203 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13205 if (isl_map_check_named_params(map) < 0)
13206 goto error;
13207 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13208 goto error;
13209 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13210 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13212 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13213 error:
13214 isl_pw_multi_aff_free(pma);
13215 return isl_map_free(map);
13218 /* Compute the preimage of "set" under the function represented by "pma".
13219 * In other words, plug in "pma" in "set". The result is a set
13220 * that lives in the domain space of "pma".
13222 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13223 __isl_take isl_pw_multi_aff *pma)
13225 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13228 /* Compute the preimage of the domain of "map" under the function
13229 * represented by "pma".
13230 * In other words, plug in "pma" in the domain of "map".
13231 * The result is a map that lives in the same space as "map",
13232 * except that domain space has been replaced by the domain space of "pma".
13234 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13235 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13237 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13240 /* Compute the preimage of the range of "map" under the function
13241 * represented by "pma".
13242 * In other words, plug in "pma" in the range of "map".
13243 * The result is a map that lives in the same space as "map",
13244 * except that range space has been replaced by the domain space of "pma".
13246 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13247 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13249 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13252 /* Compute the preimage of "map" under the function represented by "mpa".
13253 * In other words, plug in "mpa" in the domain or range of "map".
13254 * The result is a map that lives in the same space as "map",
13255 * except that the space of type "type" has been replaced by
13256 * the domain space of "mpa".
13258 * If the map does not involve any constraints that refer to the
13259 * dimensions of the substituted space, then the only possible
13260 * effect of "mpa" on the map is to map the space to a different space.
13261 * We create a separate isl_multi_aff to effectuate this change
13262 * in order to avoid spurious splitting of the map along the pieces
13263 * of "mpa".
13264 * If "mpa" has a non-trivial explicit domain, however,
13265 * then the full substitution should be performed.
13267 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13268 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13270 int n;
13271 isl_bool full;
13272 isl_pw_multi_aff *pma;
13274 if (!map || !mpa)
13275 goto error;
13277 n = isl_map_dim(map, type);
13278 full = isl_map_involves_dims(map, type, 0, n);
13279 if (full >= 0 && !full)
13280 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13281 if (full < 0)
13282 goto error;
13283 if (!full) {
13284 isl_space *space;
13285 isl_multi_aff *ma;
13287 space = isl_multi_pw_aff_get_space(mpa);
13288 isl_multi_pw_aff_free(mpa);
13289 ma = isl_multi_aff_zero(space);
13290 return isl_map_preimage_multi_aff(map, type, ma);
13293 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13294 return isl_map_preimage_pw_multi_aff(map, type, pma);
13295 error:
13296 isl_map_free(map);
13297 isl_multi_pw_aff_free(mpa);
13298 return NULL;
13301 /* Compute the preimage of "map" under the function represented by "mpa".
13302 * In other words, plug in "mpa" in the domain "map".
13303 * The result is a map that lives in the same space as "map",
13304 * except that domain space has been replaced by the domain space of "mpa".
13306 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13307 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13309 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13312 /* Compute the preimage of "set" by the function represented by "mpa".
13313 * In other words, plug in "mpa" in "set".
13315 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13316 __isl_take isl_multi_pw_aff *mpa)
13318 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13321 /* Return a copy of the equality constraints of "bset" as a matrix.
13323 __isl_give isl_mat *isl_basic_set_extract_equalities(
13324 __isl_keep isl_basic_set *bset)
13326 isl_ctx *ctx;
13327 unsigned total;
13329 if (!bset)
13330 return NULL;
13332 ctx = isl_basic_set_get_ctx(bset);
13333 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13334 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13337 /* Are the "n" "coefficients" starting at "first" of the integer division
13338 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13339 * to each other?
13340 * The "coefficient" at position 0 is the denominator.
13341 * The "coefficient" at position 1 is the constant term.
13343 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13344 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13345 unsigned first, unsigned n)
13347 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13348 return isl_bool_error;
13349 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13350 return isl_bool_error;
13351 return isl_seq_eq(bmap1->div[pos1] + first,
13352 bmap2->div[pos2] + first, n);
13355 /* Are the integer division expressions at position "pos1" in "bmap1" and
13356 * "pos2" in "bmap2" equal to each other, except that the constant terms
13357 * are different?
13359 isl_bool isl_basic_map_equal_div_expr_except_constant(
13360 __isl_keep isl_basic_map *bmap1, int pos1,
13361 __isl_keep isl_basic_map *bmap2, int pos2)
13363 isl_bool equal;
13364 unsigned total;
13366 if (!bmap1 || !bmap2)
13367 return isl_bool_error;
13368 total = isl_basic_map_total_dim(bmap1);
13369 if (total != isl_basic_map_total_dim(bmap2))
13370 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13371 "incomparable div expressions", return isl_bool_error);
13372 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13373 0, 1);
13374 if (equal < 0 || !equal)
13375 return equal;
13376 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13377 1, 1);
13378 if (equal < 0 || equal)
13379 return isl_bool_not(equal);
13380 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13381 2, total);
13384 /* Replace the numerator of the constant term of the integer division
13385 * expression at position "div" in "bmap" by "value".
13386 * The caller guarantees that this does not change the meaning
13387 * of the input.
13389 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13390 __isl_take isl_basic_map *bmap, int div, int value)
13392 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13393 return isl_basic_map_free(bmap);
13395 isl_int_set_si(bmap->div[div][1], value);
13397 return bmap;
13400 /* Is the point "inner" internal to inequality constraint "ineq"
13401 * of "bset"?
13402 * The point is considered to be internal to the inequality constraint,
13403 * if it strictly lies on the positive side of the inequality constraint,
13404 * or if it lies on the constraint and the constraint is lexico-positive.
13406 static isl_bool is_internal(__isl_keep isl_vec *inner,
13407 __isl_keep isl_basic_set *bset, int ineq)
13409 isl_ctx *ctx;
13410 int pos;
13411 unsigned total;
13413 if (!inner || !bset)
13414 return isl_bool_error;
13416 ctx = isl_basic_set_get_ctx(bset);
13417 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13418 &ctx->normalize_gcd);
13419 if (!isl_int_is_zero(ctx->normalize_gcd))
13420 return isl_int_is_nonneg(ctx->normalize_gcd);
13422 total = isl_basic_set_dim(bset, isl_dim_all);
13423 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13424 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13427 /* Tighten the inequality constraints of "bset" that are outward with respect
13428 * to the point "vec".
13429 * That is, tighten the constraints that are not satisfied by "vec".
13431 * "vec" is a point internal to some superset S of "bset" that is used
13432 * to make the subsets of S disjoint, by tightening one half of the constraints
13433 * that separate two subsets. In particular, the constraints of S
13434 * are all satisfied by "vec" and should not be tightened.
13435 * Of the internal constraints, those that have "vec" on the outside
13436 * are tightened. The shared facet is included in the adjacent subset
13437 * with the opposite constraint.
13438 * For constraints that saturate "vec", this criterion cannot be used
13439 * to determine which of the two sides should be tightened.
13440 * Instead, the sign of the first non-zero coefficient is used
13441 * to make this choice. Note that this second criterion is never used
13442 * on the constraints of S since "vec" is interior to "S".
13444 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13445 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13447 int j;
13449 bset = isl_basic_set_cow(bset);
13450 if (!bset)
13451 return NULL;
13452 for (j = 0; j < bset->n_ineq; ++j) {
13453 isl_bool internal;
13455 internal = is_internal(vec, bset, j);
13456 if (internal < 0)
13457 return isl_basic_set_free(bset);
13458 if (internal)
13459 continue;
13460 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13463 return bset;
13466 /* Replace the variables x of type "type" starting at "first" in "bmap"
13467 * by x' with x = M x' with M the matrix trans.
13468 * That is, replace the corresponding coefficients c by c M.
13470 * The transformation matrix should be a square matrix.
13472 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13473 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13474 __isl_take isl_mat *trans)
13476 unsigned pos;
13478 bmap = isl_basic_map_cow(bmap);
13479 if (!bmap || !trans)
13480 goto error;
13482 if (trans->n_row != trans->n_col)
13483 isl_die(trans->ctx, isl_error_invalid,
13484 "expecting square transformation matrix", goto error);
13485 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13486 goto error;
13488 pos = isl_basic_map_offset(bmap, type) + first;
13490 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13491 isl_mat_copy(trans)) < 0)
13492 goto error;
13493 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13494 isl_mat_copy(trans)) < 0)
13495 goto error;
13496 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13497 isl_mat_copy(trans)) < 0)
13498 goto error;
13500 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13501 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13503 isl_mat_free(trans);
13504 return bmap;
13505 error:
13506 isl_mat_free(trans);
13507 isl_basic_map_free(bmap);
13508 return NULL;
13511 /* Replace the variables x of type "type" starting at "first" in "bset"
13512 * by x' with x = M x' with M the matrix trans.
13513 * That is, replace the corresponding coefficients c by c M.
13515 * The transformation matrix should be a square matrix.
13517 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13518 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13519 __isl_take isl_mat *trans)
13521 return isl_basic_map_transform_dims(bset, type, first, trans);