isl_polynomial.c: set_div: use isl_qpolynomial_domain_var_offset
[isl.git] / isl_map.c
blob12873a2139f6d030fa154baecd4f8bf6398b6381
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>
47 #include <isl_printer_private.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 /* Treat "bset" as a basic map.
55 * Internally, isl_basic_set is defined to isl_basic_map, so in practice,
56 * this function performs a redundant cast.
58 static __isl_keep const isl_basic_map *const_bset_to_bmap(
59 __isl_keep const isl_basic_set *bset)
61 return (const isl_basic_map *) bset;
64 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
66 switch (type) {
67 case isl_dim_param: return dim->nparam;
68 case isl_dim_in: return dim->n_in;
69 case isl_dim_out: return dim->n_out;
70 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
71 default: return 0;
75 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
77 switch (type) {
78 case isl_dim_param: return 1;
79 case isl_dim_in: return 1 + dim->nparam;
80 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
81 default: return 0;
85 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
86 enum isl_dim_type type)
88 if (!bmap)
89 return 0;
90 switch (type) {
91 case isl_dim_cst: return 1;
92 case isl_dim_param:
93 case isl_dim_in:
94 case isl_dim_out: return isl_space_dim(bmap->dim, type);
95 case isl_dim_div: return bmap->n_div;
96 case isl_dim_all: return isl_basic_map_total_dim(bmap);
97 default: return 0;
101 /* Return the space of "map".
103 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
105 return map ? map->dim : NULL;
108 /* Return the space of "set".
110 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
112 return isl_map_peek_space(set_to_map(set));
115 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
117 return map ? n(map->dim, type) : 0;
120 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
122 return set ? n(set->dim, type) : 0;
125 /* Return the position of the variables of the given type
126 * within the sequence of variables of "bmap".
128 int isl_basic_map_var_offset(__isl_keep isl_basic_map *bmap,
129 enum isl_dim_type type)
131 isl_space *space;
133 space = isl_basic_map_peek_space(bmap);
134 if (!space)
135 return -1;
137 switch (type) {
138 case isl_dim_param:
139 case isl_dim_in:
140 case isl_dim_out: return isl_space_offset(space, type);
141 case isl_dim_div: return isl_space_dim(space, isl_dim_all);
142 case isl_dim_cst:
143 default:
144 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
145 "invalid dimension type", return -1);
149 /* Return the position of the variables of the given type
150 * within the sequence of variables of "bset".
152 int isl_basic_set_var_offset(__isl_keep isl_basic_set *bset,
153 enum isl_dim_type type)
155 return isl_basic_map_var_offset(bset_to_bmap(bset), type);
158 /* Return the position of the coefficients of the variables of the given type
159 * within the sequence of coefficients of "bmap".
161 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
162 enum isl_dim_type type)
164 switch (type) {
165 case isl_dim_cst: return 0;
166 case isl_dim_param:
167 case isl_dim_in:
168 case isl_dim_out:
169 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
170 default: return 0;
174 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
175 enum isl_dim_type type)
177 return isl_basic_map_offset(bset, type);
180 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
182 return pos(map->dim, type);
185 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
186 enum isl_dim_type type)
188 return isl_basic_map_dim(bset, type);
191 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
193 return isl_basic_set_dim(bset, isl_dim_set);
196 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
198 return isl_basic_set_dim(bset, isl_dim_param);
201 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
203 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
206 unsigned isl_set_n_dim(__isl_keep isl_set *set)
208 return isl_set_dim(set, isl_dim_set);
211 unsigned isl_set_n_param(__isl_keep isl_set *set)
213 return isl_set_dim(set, isl_dim_param);
216 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
218 return bmap ? bmap->dim->n_in : 0;
221 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
223 return bmap ? bmap->dim->n_out : 0;
226 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
228 return bmap ? bmap->dim->nparam : 0;
231 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
233 return bmap ? bmap->n_div : 0;
236 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
238 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
241 unsigned isl_map_n_in(__isl_keep const isl_map *map)
243 return map ? map->dim->n_in : 0;
246 unsigned isl_map_n_out(__isl_keep const isl_map *map)
248 return map ? map->dim->n_out : 0;
251 unsigned isl_map_n_param(__isl_keep const isl_map *map)
253 return map ? map->dim->nparam : 0;
256 /* Return the number of equality constraints in the description of "bmap".
257 * Return -1 on error.
259 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
261 if (!bmap)
262 return -1;
263 return bmap->n_eq;
266 /* Return the number of equality constraints in the description of "bset".
267 * Return -1 on error.
269 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
271 return isl_basic_map_n_equality(bset_to_bmap(bset));
274 /* Return the number of inequality constraints in the description of "bmap".
275 * Return -1 on error.
277 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
279 if (!bmap)
280 return -1;
281 return bmap->n_ineq;
284 /* Return the number of inequality constraints in the description of "bset".
285 * Return -1 on error.
287 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
289 return isl_basic_map_n_inequality(bset_to_bmap(bset));
292 /* Do "bmap1" and "bmap2" have the same parameters?
294 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
295 __isl_keep isl_basic_map *bmap2)
297 isl_space *space1, *space2;
299 space1 = isl_basic_map_peek_space(bmap1);
300 space2 = isl_basic_map_peek_space(bmap2);
301 return isl_space_has_equal_params(space1, space2);
304 /* Do "map1" and "map2" have the same parameters?
306 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
307 __isl_keep isl_map *map2)
309 isl_space *space1, *space2;
311 space1 = isl_map_peek_space(map1);
312 space2 = isl_map_peek_space(map2);
313 return isl_space_has_equal_params(space1, space2);
316 /* Do "map" and "set" have the same parameters?
318 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
319 __isl_keep isl_set *set)
321 return isl_map_has_equal_params(map, set_to_map(set));
324 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
325 __isl_keep isl_set *set)
327 isl_bool m;
328 if (!map || !set)
329 return isl_bool_error;
330 m = isl_map_has_equal_params(map, set_to_map(set));
331 if (m < 0 || !m)
332 return m;
333 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
334 set->dim, isl_dim_set);
337 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
338 __isl_keep isl_basic_set *bset)
340 isl_bool m;
341 if (!bmap || !bset)
342 return isl_bool_error;
343 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
344 if (m < 0 || !m)
345 return m;
346 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
347 bset->dim, isl_dim_set);
350 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
351 __isl_keep isl_set *set)
353 isl_bool m;
354 if (!map || !set)
355 return isl_bool_error;
356 m = isl_map_has_equal_params(map, set_to_map(set));
357 if (m < 0 || !m)
358 return m;
359 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
360 set->dim, isl_dim_set);
363 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
364 __isl_keep isl_basic_set *bset)
366 isl_bool m;
367 if (!bmap || !bset)
368 return isl_bool_error;
369 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
370 if (m < 0 || !m)
371 return m;
372 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
373 bset->dim, isl_dim_set);
376 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
378 return bmap ? bmap->ctx : NULL;
381 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
383 return bset ? bset->ctx : NULL;
386 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
388 return map ? map->ctx : NULL;
391 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
393 return set ? set->ctx : NULL;
396 /* Return the space of "bmap".
398 __isl_keep isl_space *isl_basic_map_peek_space(
399 __isl_keep const isl_basic_map *bmap)
401 return bmap ? bmap->dim : NULL;
404 /* Return the space of "bset".
406 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
408 return isl_basic_map_peek_space(bset_to_bmap(bset));
411 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
413 return isl_space_copy(isl_basic_map_peek_space(bmap));
416 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
418 return isl_basic_map_get_space(bset_to_bmap(bset));
421 /* Extract the divs in "bmap" as a matrix.
423 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
425 int i;
426 isl_ctx *ctx;
427 isl_mat *div;
428 int v_div;
429 unsigned cols;
431 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
432 if (v_div < 0)
433 return NULL;
435 ctx = isl_basic_map_get_ctx(bmap);
436 cols = 1 + 1 + v_div + bmap->n_div;
437 div = isl_mat_alloc(ctx, bmap->n_div, cols);
438 if (!div)
439 return NULL;
441 for (i = 0; i < bmap->n_div; ++i)
442 isl_seq_cpy(div->row[i], bmap->div[i], cols);
444 return div;
447 /* Extract the divs in "bset" as a matrix.
449 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
451 return isl_basic_map_get_divs(bset);
454 __isl_give isl_local_space *isl_basic_map_get_local_space(
455 __isl_keep isl_basic_map *bmap)
457 isl_mat *div;
459 if (!bmap)
460 return NULL;
462 div = isl_basic_map_get_divs(bmap);
463 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
466 __isl_give isl_local_space *isl_basic_set_get_local_space(
467 __isl_keep isl_basic_set *bset)
469 return isl_basic_map_get_local_space(bset);
472 /* For each known div d = floor(f/m), add the constraints
474 * f - m d >= 0
475 * -(f-(m-1)) + m d >= 0
477 * Do not finalize the result.
479 static __isl_give isl_basic_map *add_known_div_constraints(
480 __isl_take isl_basic_map *bmap)
482 int i;
483 unsigned n_div;
485 if (!bmap)
486 return NULL;
487 n_div = isl_basic_map_dim(bmap, isl_dim_div);
488 if (n_div == 0)
489 return bmap;
490 bmap = isl_basic_map_cow(bmap);
491 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
492 if (!bmap)
493 return NULL;
494 for (i = 0; i < n_div; ++i) {
495 if (isl_int_is_zero(bmap->div[i][0]))
496 continue;
497 bmap = isl_basic_map_add_div_constraints(bmap, i);
500 return bmap;
503 __isl_give isl_basic_map *isl_basic_map_from_local_space(
504 __isl_take isl_local_space *ls)
506 int i;
507 int n_div;
508 isl_basic_map *bmap;
510 if (!ls)
511 return NULL;
513 n_div = isl_local_space_dim(ls, isl_dim_div);
514 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
515 n_div, 0, 2 * n_div);
517 for (i = 0; i < n_div; ++i)
518 if (isl_basic_map_alloc_div(bmap) < 0)
519 goto error;
521 for (i = 0; i < n_div; ++i)
522 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
523 bmap = add_known_div_constraints(bmap);
525 isl_local_space_free(ls);
526 return bmap;
527 error:
528 isl_local_space_free(ls);
529 isl_basic_map_free(bmap);
530 return NULL;
533 __isl_give isl_basic_set *isl_basic_set_from_local_space(
534 __isl_take isl_local_space *ls)
536 return isl_basic_map_from_local_space(ls);
539 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
541 return isl_space_copy(isl_map_peek_space(map));
544 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
546 if (!set)
547 return NULL;
548 return isl_space_copy(set->dim);
551 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
552 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
554 bmap = isl_basic_map_cow(bmap);
555 if (!bmap)
556 return NULL;
557 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
558 if (!bmap->dim)
559 goto error;
560 bmap = isl_basic_map_finalize(bmap);
561 return bmap;
562 error:
563 isl_basic_map_free(bmap);
564 return NULL;
567 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
568 __isl_take isl_basic_set *bset, const char *s)
570 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
573 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
574 enum isl_dim_type type)
576 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
579 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
580 enum isl_dim_type type, const char *s)
582 int i;
584 map = isl_map_cow(map);
585 if (!map)
586 return NULL;
588 map->dim = isl_space_set_tuple_name(map->dim, type, s);
589 if (!map->dim)
590 goto error;
592 for (i = 0; i < map->n; ++i) {
593 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
594 if (!map->p[i])
595 goto error;
598 return map;
599 error:
600 isl_map_free(map);
601 return NULL;
604 /* Replace the identifier of the tuple of type "type" by "id".
606 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
607 __isl_take isl_basic_map *bmap,
608 enum isl_dim_type type, __isl_take isl_id *id)
610 bmap = isl_basic_map_cow(bmap);
611 if (!bmap)
612 goto error;
613 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
614 if (!bmap->dim)
615 return isl_basic_map_free(bmap);
616 bmap = isl_basic_map_finalize(bmap);
617 return bmap;
618 error:
619 isl_id_free(id);
620 return NULL;
623 /* Replace the identifier of the tuple by "id".
625 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
626 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
628 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
631 /* Does the input or output tuple have a name?
633 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
635 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
638 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
639 enum isl_dim_type type)
641 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
644 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
645 const char *s)
647 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
648 isl_dim_set, s));
651 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
652 enum isl_dim_type type, __isl_take isl_id *id)
654 map = isl_map_cow(map);
655 if (!map)
656 goto error;
658 map->dim = isl_space_set_tuple_id(map->dim, type, id);
660 return isl_map_reset_space(map, isl_space_copy(map->dim));
661 error:
662 isl_id_free(id);
663 return NULL;
666 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
667 __isl_take isl_id *id)
669 return isl_map_set_tuple_id(set, isl_dim_set, id);
672 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
673 enum isl_dim_type type)
675 map = isl_map_cow(map);
676 if (!map)
677 return NULL;
679 map->dim = isl_space_reset_tuple_id(map->dim, type);
681 return isl_map_reset_space(map, isl_space_copy(map->dim));
684 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
686 return isl_map_reset_tuple_id(set, isl_dim_set);
689 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
691 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
694 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
695 enum isl_dim_type type)
697 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
700 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
702 return isl_map_has_tuple_id(set, isl_dim_set);
705 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
707 return isl_map_get_tuple_id(set, isl_dim_set);
710 /* Does the set tuple have a name?
712 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
714 if (!set)
715 return isl_bool_error;
716 return isl_space_has_tuple_name(set->dim, isl_dim_set);
720 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
722 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
725 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
727 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
730 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
731 enum isl_dim_type type, unsigned pos)
733 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
736 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
737 enum isl_dim_type type, unsigned pos)
739 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
742 /* Does the given dimension have a name?
744 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
745 enum isl_dim_type type, unsigned pos)
747 if (!map)
748 return isl_bool_error;
749 return isl_space_has_dim_name(map->dim, type, pos);
752 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
753 enum isl_dim_type type, unsigned pos)
755 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
758 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
759 enum isl_dim_type type, unsigned pos)
761 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
764 /* Does the given dimension have a name?
766 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
767 enum isl_dim_type type, unsigned pos)
769 if (!set)
770 return isl_bool_error;
771 return isl_space_has_dim_name(set->dim, type, pos);
774 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
775 __isl_take isl_basic_map *bmap,
776 enum isl_dim_type type, unsigned pos, const char *s)
778 bmap = isl_basic_map_cow(bmap);
779 if (!bmap)
780 return NULL;
781 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
782 if (!bmap->dim)
783 goto error;
784 return isl_basic_map_finalize(bmap);
785 error:
786 isl_basic_map_free(bmap);
787 return NULL;
790 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
791 enum isl_dim_type type, unsigned pos, const char *s)
793 int i;
795 map = isl_map_cow(map);
796 if (!map)
797 return NULL;
799 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
800 if (!map->dim)
801 goto error;
803 for (i = 0; i < map->n; ++i) {
804 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
805 if (!map->p[i])
806 goto error;
809 return map;
810 error:
811 isl_map_free(map);
812 return NULL;
815 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
816 __isl_take isl_basic_set *bset,
817 enum isl_dim_type type, unsigned pos, const char *s)
819 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
820 type, pos, s));
823 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
824 enum isl_dim_type type, unsigned pos, const char *s)
826 return set_from_map(isl_map_set_dim_name(set_to_map(set),
827 type, pos, s));
830 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
831 enum isl_dim_type type, unsigned pos)
833 if (!bmap)
834 return isl_bool_error;
835 return isl_space_has_dim_id(bmap->dim, type, pos);
838 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
839 enum isl_dim_type type, unsigned pos)
841 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
844 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
845 enum isl_dim_type type, unsigned pos)
847 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
850 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
851 enum isl_dim_type type, unsigned pos)
853 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
856 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
857 enum isl_dim_type type, unsigned pos)
859 return isl_map_has_dim_id(set, type, pos);
862 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
863 enum isl_dim_type type, unsigned pos)
865 return isl_map_get_dim_id(set, type, pos);
868 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
869 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
871 map = isl_map_cow(map);
872 if (!map)
873 goto error;
875 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
877 return isl_map_reset_space(map, isl_space_copy(map->dim));
878 error:
879 isl_id_free(id);
880 return NULL;
883 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
884 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
886 return isl_map_set_dim_id(set, type, pos, id);
889 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
890 __isl_keep isl_id *id)
892 if (!map)
893 return -1;
894 return isl_space_find_dim_by_id(map->dim, type, id);
897 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
898 __isl_keep isl_id *id)
900 return isl_map_find_dim_by_id(set, type, id);
903 /* Return the position of the dimension of the given type and name
904 * in "bmap".
905 * Return -1 if no such dimension can be found.
907 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
908 enum isl_dim_type type, const char *name)
910 if (!bmap)
911 return -1;
912 return isl_space_find_dim_by_name(bmap->dim, type, name);
915 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
916 const char *name)
918 if (!map)
919 return -1;
920 return isl_space_find_dim_by_name(map->dim, type, name);
923 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
924 const char *name)
926 return isl_map_find_dim_by_name(set, type, name);
929 /* Check whether equality i of bset is a pure stride constraint
930 * on a single dimension, i.e., of the form
932 * v = k e
934 * with k a constant and e an existentially quantified variable.
936 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
938 unsigned nparam;
939 unsigned d;
940 unsigned n_div;
941 int pos1;
942 int pos2;
944 if (!bset)
945 return isl_bool_error;
947 if (!isl_int_is_zero(bset->eq[i][0]))
948 return isl_bool_false;
950 nparam = isl_basic_set_dim(bset, isl_dim_param);
951 d = isl_basic_set_dim(bset, isl_dim_set);
952 n_div = isl_basic_set_dim(bset, isl_dim_div);
954 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
955 return isl_bool_false;
956 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
957 if (pos1 == -1)
958 return isl_bool_false;
959 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
960 d - pos1 - 1) != -1)
961 return isl_bool_false;
963 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
964 if (pos2 == -1)
965 return isl_bool_false;
966 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
967 n_div - pos2 - 1) != -1)
968 return isl_bool_false;
969 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
970 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
971 return isl_bool_false;
973 return isl_bool_true;
976 /* Reset the user pointer on all identifiers of parameters and tuples
977 * of the space of "map".
979 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
981 isl_space *space;
983 space = isl_map_get_space(map);
984 space = isl_space_reset_user(space);
985 map = isl_map_reset_space(map, space);
987 return map;
990 /* Reset the user pointer on all identifiers of parameters and tuples
991 * of the space of "set".
993 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
995 return isl_map_reset_user(set);
998 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
1000 if (!bmap)
1001 return isl_bool_error;
1002 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
1005 /* Has "map" been marked as a rational map?
1006 * In particular, have all basic maps in "map" been marked this way?
1007 * An empty map is not considered to be rational.
1008 * Maps where only some of the basic maps are marked rational
1009 * are not allowed.
1011 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1013 int i;
1014 isl_bool rational;
1016 if (!map)
1017 return isl_bool_error;
1018 if (map->n == 0)
1019 return isl_bool_false;
1020 rational = isl_basic_map_is_rational(map->p[0]);
1021 if (rational < 0)
1022 return rational;
1023 for (i = 1; i < map->n; ++i) {
1024 isl_bool rational_i;
1026 rational_i = isl_basic_map_is_rational(map->p[i]);
1027 if (rational_i < 0)
1028 return rational_i;
1029 if (rational != rational_i)
1030 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1031 "mixed rational and integer basic maps "
1032 "not supported", return isl_bool_error);
1035 return rational;
1038 /* Has "set" been marked as a rational set?
1039 * In particular, have all basic set in "set" been marked this way?
1040 * An empty set is not considered to be rational.
1041 * Sets where only some of the basic sets are marked rational
1042 * are not allowed.
1044 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1046 return isl_map_is_rational(set);
1049 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1051 return isl_basic_map_is_rational(bset);
1054 /* Does "bmap" contain any rational points?
1056 * If "bmap" has an equality for each dimension, equating the dimension
1057 * to an integer constant, then it has no rational points, even if it
1058 * is marked as rational.
1060 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1062 isl_bool has_rational = isl_bool_true;
1063 unsigned total;
1065 if (!bmap)
1066 return isl_bool_error;
1067 if (isl_basic_map_plain_is_empty(bmap))
1068 return isl_bool_false;
1069 if (!isl_basic_map_is_rational(bmap))
1070 return isl_bool_false;
1071 bmap = isl_basic_map_copy(bmap);
1072 bmap = isl_basic_map_implicit_equalities(bmap);
1073 if (!bmap)
1074 return isl_bool_error;
1075 total = isl_basic_map_total_dim(bmap);
1076 if (bmap->n_eq == total) {
1077 int i, j;
1078 for (i = 0; i < bmap->n_eq; ++i) {
1079 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1080 if (j < 0)
1081 break;
1082 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1083 !isl_int_is_negone(bmap->eq[i][1 + j]))
1084 break;
1085 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1086 total - j - 1);
1087 if (j >= 0)
1088 break;
1090 if (i == bmap->n_eq)
1091 has_rational = isl_bool_false;
1093 isl_basic_map_free(bmap);
1095 return has_rational;
1098 /* Does "map" contain any rational points?
1100 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1102 int i;
1103 isl_bool has_rational;
1105 if (!map)
1106 return isl_bool_error;
1107 for (i = 0; i < map->n; ++i) {
1108 has_rational = isl_basic_map_has_rational(map->p[i]);
1109 if (has_rational < 0 || has_rational)
1110 return has_rational;
1112 return isl_bool_false;
1115 /* Does "set" contain any rational points?
1117 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1119 return isl_map_has_rational(set);
1122 /* Is this basic set a parameter domain?
1124 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1126 if (!bset)
1127 return isl_bool_error;
1128 return isl_space_is_params(bset->dim);
1131 /* Is this set a parameter domain?
1133 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1135 if (!set)
1136 return isl_bool_error;
1137 return isl_space_is_params(set->dim);
1140 /* Is this map actually a parameter domain?
1141 * Users should never call this function. Outside of isl,
1142 * a map can never be a parameter domain.
1144 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1146 if (!map)
1147 return isl_bool_error;
1148 return isl_space_is_params(map->dim);
1151 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1152 __isl_take isl_basic_map *bmap, unsigned extra,
1153 unsigned n_eq, unsigned n_ineq)
1155 int i;
1156 isl_space *space = isl_basic_map_peek_space(bmap);
1157 unsigned n_var = isl_space_dim(space, isl_dim_all);
1158 size_t row_size = 1 + n_var + extra;
1160 bmap->ctx = ctx;
1161 isl_ctx_ref(ctx);
1163 if (!space)
1164 return isl_basic_map_free(bmap);
1166 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1167 if (isl_blk_is_error(bmap->block))
1168 goto error;
1170 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1171 if ((n_ineq + n_eq) && !bmap->ineq)
1172 goto error;
1174 if (extra == 0) {
1175 bmap->block2 = isl_blk_empty();
1176 bmap->div = NULL;
1177 } else {
1178 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1179 if (isl_blk_is_error(bmap->block2))
1180 goto error;
1182 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1183 if (!bmap->div)
1184 goto error;
1187 for (i = 0; i < n_ineq + n_eq; ++i)
1188 bmap->ineq[i] = bmap->block.data + i * row_size;
1190 for (i = 0; i < extra; ++i)
1191 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1193 bmap->ref = 1;
1194 bmap->flags = 0;
1195 bmap->c_size = n_eq + n_ineq;
1196 bmap->eq = bmap->ineq + n_ineq;
1197 bmap->extra = extra;
1198 bmap->n_eq = 0;
1199 bmap->n_ineq = 0;
1200 bmap->n_div = 0;
1201 bmap->sample = NULL;
1203 return bmap;
1204 error:
1205 isl_basic_map_free(bmap);
1206 return NULL;
1209 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1210 unsigned nparam, unsigned dim, unsigned extra,
1211 unsigned n_eq, unsigned n_ineq)
1213 struct isl_basic_map *bmap;
1214 isl_space *space;
1216 space = isl_space_set_alloc(ctx, nparam, dim);
1217 if (!space)
1218 return NULL;
1220 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1221 return bset_from_bmap(bmap);
1224 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1225 unsigned extra, unsigned n_eq, unsigned n_ineq)
1227 struct isl_basic_map *bmap;
1228 if (!dim)
1229 return NULL;
1230 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1231 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1232 return bset_from_bmap(bmap);
1233 error:
1234 isl_space_free(dim);
1235 return NULL;
1238 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1239 unsigned extra, unsigned n_eq, unsigned n_ineq)
1241 struct isl_basic_map *bmap;
1243 if (!space)
1244 return NULL;
1245 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1246 if (!bmap)
1247 goto error;
1248 bmap->dim = space;
1250 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1251 error:
1252 isl_space_free(space);
1253 return NULL;
1256 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1257 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1258 unsigned n_eq, unsigned n_ineq)
1260 struct isl_basic_map *bmap;
1261 isl_space *dim;
1263 dim = isl_space_alloc(ctx, nparam, in, out);
1264 if (!dim)
1265 return NULL;
1267 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1268 return bmap;
1271 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1272 __isl_keep isl_basic_map *src)
1274 int i;
1275 unsigned total = isl_basic_map_total_dim(src);
1277 if (!dst)
1278 return NULL;
1280 for (i = 0; i < src->n_eq; ++i) {
1281 int j = isl_basic_map_alloc_equality(dst);
1282 if (j < 0)
1283 return isl_basic_map_free(dst);
1284 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1287 for (i = 0; i < src->n_ineq; ++i) {
1288 int j = isl_basic_map_alloc_inequality(dst);
1289 if (j < 0)
1290 return isl_basic_map_free(dst);
1291 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1294 for (i = 0; i < src->n_div; ++i) {
1295 int j = isl_basic_map_alloc_div(dst);
1296 if (j < 0)
1297 return isl_basic_map_free(dst);
1298 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1300 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1301 return dst;
1304 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1306 struct isl_basic_map *dup;
1308 if (!bmap)
1309 return NULL;
1310 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1311 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1312 dup = dup_constraints(dup, bmap);
1313 if (!dup)
1314 return NULL;
1315 dup->flags = bmap->flags;
1316 dup->sample = isl_vec_copy(bmap->sample);
1317 return dup;
1320 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1322 struct isl_basic_map *dup;
1324 dup = isl_basic_map_dup(bset_to_bmap(bset));
1325 return bset_from_bmap(dup);
1328 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1330 if (!bset)
1331 return NULL;
1333 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1334 bset->ref++;
1335 return bset;
1337 return isl_basic_set_dup(bset);
1340 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1342 if (!set)
1343 return NULL;
1345 set->ref++;
1346 return set;
1349 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1351 if (!bmap)
1352 return NULL;
1354 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1355 bmap->ref++;
1356 return bmap;
1358 bmap = isl_basic_map_dup(bmap);
1359 if (bmap)
1360 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1361 return bmap;
1364 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1366 if (!map)
1367 return NULL;
1369 map->ref++;
1370 return map;
1373 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1375 if (!bmap)
1376 return NULL;
1378 if (--bmap->ref > 0)
1379 return NULL;
1381 isl_ctx_deref(bmap->ctx);
1382 free(bmap->div);
1383 isl_blk_free(bmap->ctx, bmap->block2);
1384 free(bmap->ineq);
1385 isl_blk_free(bmap->ctx, bmap->block);
1386 isl_vec_free(bmap->sample);
1387 isl_space_free(bmap->dim);
1388 free(bmap);
1390 return NULL;
1393 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1395 return isl_basic_map_free(bset_to_bmap(bset));
1398 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1400 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1403 /* Check that "bset" does not involve any parameters.
1405 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1407 if (!bset)
1408 return isl_stat_error;
1409 if (isl_basic_set_dim(bset, isl_dim_param) != 0)
1410 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1411 "basic set should not have any parameters",
1412 return isl_stat_error);
1413 return isl_stat_ok;
1416 /* Check that "bset" does not involve any local variables.
1418 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1420 if (!bset)
1421 return isl_stat_error;
1422 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
1423 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1424 "basic set should not have any local variables",
1425 return isl_stat_error);
1426 return isl_stat_ok;
1429 /* Check that "map" has only named parameters, reporting an error
1430 * if it does not.
1432 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1434 return isl_space_check_named_params(isl_map_peek_space(map));
1437 /* Check that "bmap" has only named parameters, reporting an error
1438 * if it does not.
1440 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1442 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1445 /* Check that "bmap1" and "bmap2" have the same parameters,
1446 * reporting an error if they do not.
1448 static isl_stat isl_basic_map_check_equal_params(
1449 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1451 isl_bool match;
1453 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1454 if (match < 0)
1455 return isl_stat_error;
1456 if (!match)
1457 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1458 "parameters don't match", return isl_stat_error);
1459 return isl_stat_ok;
1462 __isl_give isl_map *isl_map_align_params_map_map_and(
1463 __isl_take isl_map *map1, __isl_take isl_map *map2,
1464 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1465 __isl_take isl_map *map2))
1467 if (!map1 || !map2)
1468 goto error;
1469 if (isl_map_has_equal_params(map1, map2))
1470 return fn(map1, map2);
1471 if (isl_map_check_named_params(map1) < 0)
1472 goto error;
1473 if (isl_map_check_named_params(map2) < 0)
1474 goto error;
1475 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1476 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1477 return fn(map1, map2);
1478 error:
1479 isl_map_free(map1);
1480 isl_map_free(map2);
1481 return NULL;
1484 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1485 __isl_keep isl_map *map2,
1486 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1488 isl_bool r;
1490 if (!map1 || !map2)
1491 return isl_bool_error;
1492 if (isl_map_has_equal_params(map1, map2))
1493 return fn(map1, map2);
1494 if (isl_map_check_named_params(map1) < 0)
1495 return isl_bool_error;
1496 if (isl_map_check_named_params(map2) < 0)
1497 return isl_bool_error;
1498 map1 = isl_map_copy(map1);
1499 map2 = isl_map_copy(map2);
1500 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1501 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1502 r = fn(map1, map2);
1503 isl_map_free(map1);
1504 isl_map_free(map2);
1505 return r;
1508 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1510 struct isl_ctx *ctx;
1511 if (!bmap)
1512 return -1;
1513 ctx = bmap->ctx;
1514 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1515 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1516 return -1);
1517 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1518 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1519 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1520 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1521 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1522 isl_int *t;
1523 int j = isl_basic_map_alloc_inequality(bmap);
1524 if (j < 0)
1525 return -1;
1526 t = bmap->ineq[j];
1527 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1528 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1529 bmap->eq[-1] = t;
1530 bmap->n_eq++;
1531 bmap->n_ineq--;
1532 bmap->eq--;
1533 return 0;
1535 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1536 bmap->extra - bmap->n_div);
1537 return bmap->n_eq++;
1540 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1542 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1545 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1547 if (!bmap)
1548 return -1;
1549 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1550 bmap->n_eq -= n;
1551 return 0;
1554 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1556 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1559 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1561 isl_int *t;
1562 if (!bmap)
1563 return -1;
1564 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1566 if (pos != bmap->n_eq - 1) {
1567 t = bmap->eq[pos];
1568 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1569 bmap->eq[bmap->n_eq - 1] = t;
1571 bmap->n_eq--;
1572 return 0;
1575 /* Turn inequality "pos" of "bmap" into an equality.
1577 * In particular, we move the inequality in front of the equalities
1578 * and move the last inequality in the position of the moved inequality.
1579 * Note that isl_tab_make_equalities_explicit depends on this particular
1580 * change in the ordering of the constraints.
1582 void isl_basic_map_inequality_to_equality(
1583 struct isl_basic_map *bmap, unsigned pos)
1585 isl_int *t;
1587 t = bmap->ineq[pos];
1588 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1589 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1590 bmap->eq[-1] = t;
1591 bmap->n_eq++;
1592 bmap->n_ineq--;
1593 bmap->eq--;
1594 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1595 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1596 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1597 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1600 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1602 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1605 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1607 struct isl_ctx *ctx;
1608 if (!bmap)
1609 return -1;
1610 ctx = bmap->ctx;
1611 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1612 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1613 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1614 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1615 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1616 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1617 1 + isl_basic_map_total_dim(bmap),
1618 bmap->extra - bmap->n_div);
1619 return bmap->n_ineq++;
1622 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1624 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1627 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1629 if (!bmap)
1630 return -1;
1631 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1632 bmap->n_ineq -= n;
1633 return 0;
1636 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1638 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1641 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1643 isl_int *t;
1644 if (!bmap)
1645 return -1;
1646 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1648 if (pos != bmap->n_ineq - 1) {
1649 t = bmap->ineq[pos];
1650 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1651 bmap->ineq[bmap->n_ineq - 1] = t;
1652 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1654 bmap->n_ineq--;
1655 return 0;
1658 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1660 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1663 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1664 isl_int *eq)
1666 isl_bool empty;
1667 int k;
1669 empty = isl_basic_map_plain_is_empty(bmap);
1670 if (empty < 0)
1671 return isl_basic_map_free(bmap);
1672 if (empty)
1673 return bmap;
1675 bmap = isl_basic_map_cow(bmap);
1676 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1677 if (!bmap)
1678 return NULL;
1679 k = isl_basic_map_alloc_equality(bmap);
1680 if (k < 0)
1681 goto error;
1682 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1683 return bmap;
1684 error:
1685 isl_basic_map_free(bmap);
1686 return NULL;
1689 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1690 isl_int *eq)
1692 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1695 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1696 isl_int *ineq)
1698 int k;
1700 bmap = isl_basic_map_cow(bmap);
1701 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1702 if (!bmap)
1703 return NULL;
1704 k = isl_basic_map_alloc_inequality(bmap);
1705 if (k < 0)
1706 goto error;
1707 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1708 return bmap;
1709 error:
1710 isl_basic_map_free(bmap);
1711 return NULL;
1714 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1715 isl_int *ineq)
1717 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1720 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1722 if (!bmap)
1723 return -1;
1724 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1725 isl_seq_clr(bmap->div[bmap->n_div] +
1726 1 + 1 + isl_basic_map_total_dim(bmap),
1727 bmap->extra - bmap->n_div);
1728 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1729 return bmap->n_div++;
1732 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1734 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1737 #undef TYPE
1738 #define TYPE isl_basic_map
1739 #include "check_type_range_templ.c"
1741 /* Check that there are "n" dimensions of type "type" starting at "first"
1742 * in "bset".
1744 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1745 enum isl_dim_type type, unsigned first, unsigned n)
1747 return isl_basic_map_check_range(bset_to_bmap(bset),
1748 type, first, n);
1751 /* Insert an extra integer division, prescribed by "div", to "bmap"
1752 * at (integer division) position "pos".
1754 * The integer division is first added at the end and then moved
1755 * into the right position.
1757 __isl_give isl_basic_map *isl_basic_map_insert_div(
1758 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1760 int i, k;
1762 bmap = isl_basic_map_cow(bmap);
1763 if (!bmap || !div)
1764 return isl_basic_map_free(bmap);
1766 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1767 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1768 "unexpected size", return isl_basic_map_free(bmap));
1769 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1770 return isl_basic_map_free(bmap);
1772 bmap = isl_basic_map_extend_space(bmap,
1773 isl_basic_map_get_space(bmap), 1, 0, 2);
1774 k = isl_basic_map_alloc_div(bmap);
1775 if (k < 0)
1776 return isl_basic_map_free(bmap);
1777 isl_seq_cpy(bmap->div[k], div->el, div->size);
1778 isl_int_set_si(bmap->div[k][div->size], 0);
1780 for (i = k; i > pos; --i)
1781 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1783 return bmap;
1786 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1788 if (!bmap)
1789 return isl_stat_error;
1790 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1791 bmap->n_div -= n;
1792 return isl_stat_ok;
1795 static __isl_give isl_basic_map *add_constraints(
1796 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1797 unsigned i_pos, unsigned o_pos)
1799 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1800 isl_ctx *ctx;
1801 isl_space *space;
1802 struct isl_dim_map *dim_map;
1804 space = isl_basic_map_peek_space(bmap2);
1805 if (!bmap1 || !space)
1806 goto error;
1808 total = isl_basic_map_dim(bmap1, isl_dim_all);
1809 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1810 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1811 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1812 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1813 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1814 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1815 ctx = isl_basic_map_get_ctx(bmap1);
1816 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1817 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1818 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1819 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1820 isl_dim_map_div(dim_map, bmap2, total);
1822 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1823 error:
1824 isl_basic_map_free(bmap1);
1825 isl_basic_map_free(bmap2);
1826 return NULL;
1829 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1830 struct isl_basic_set *bset2, unsigned pos)
1832 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1833 bset_to_bmap(bset2), 0, pos));
1836 __isl_give isl_basic_map *isl_basic_map_extend_space(
1837 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1838 unsigned extra, unsigned n_eq, unsigned n_ineq)
1840 struct isl_basic_map *ext;
1841 unsigned flags;
1842 int dims_ok;
1844 if (!space)
1845 goto error;
1847 if (!base)
1848 goto error;
1850 dims_ok = isl_space_is_equal(base->dim, space) &&
1851 base->extra >= base->n_div + extra;
1853 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1854 room_for_ineq(base, n_ineq)) {
1855 isl_space_free(space);
1856 return base;
1859 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1860 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1861 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1862 extra += base->extra;
1863 n_eq += base->n_eq;
1864 n_ineq += base->n_ineq;
1866 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1867 space = NULL;
1868 if (!ext)
1869 goto error;
1871 if (dims_ok)
1872 ext->sample = isl_vec_copy(base->sample);
1873 flags = base->flags;
1874 ext = add_constraints(ext, base, 0, 0);
1875 if (ext) {
1876 ext->flags = flags;
1877 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1880 return ext;
1882 error:
1883 isl_space_free(space);
1884 isl_basic_map_free(base);
1885 return NULL;
1888 __isl_give isl_basic_set *isl_basic_set_extend_space(
1889 __isl_take isl_basic_set *base,
1890 __isl_take isl_space *dim, unsigned extra,
1891 unsigned n_eq, unsigned n_ineq)
1893 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1894 dim, extra, n_eq, n_ineq));
1897 struct isl_basic_map *isl_basic_map_extend_constraints(
1898 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1900 if (!base)
1901 return NULL;
1902 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1903 0, n_eq, n_ineq);
1906 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1907 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1908 unsigned n_eq, unsigned n_ineq)
1910 struct isl_basic_map *bmap;
1911 isl_space *dim;
1913 if (!base)
1914 return NULL;
1915 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1916 if (!dim)
1917 goto error;
1919 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1920 return bmap;
1921 error:
1922 isl_basic_map_free(base);
1923 return NULL;
1926 struct isl_basic_set *isl_basic_set_extend_constraints(
1927 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1929 isl_basic_map *bmap = bset_to_bmap(base);
1930 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1931 return bset_from_bmap(bmap);
1934 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1936 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1939 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1941 if (!bmap)
1942 return NULL;
1944 if (bmap->ref > 1) {
1945 bmap->ref--;
1946 bmap = isl_basic_map_dup(bmap);
1948 if (bmap) {
1949 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1950 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1952 return bmap;
1955 /* Clear all cached information in "map", either because it is about
1956 * to be modified or because it is being freed.
1957 * Always return the same pointer that is passed in.
1958 * This is needed for the use in isl_map_free.
1960 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1962 isl_basic_map_free(map->cached_simple_hull[0]);
1963 isl_basic_map_free(map->cached_simple_hull[1]);
1964 map->cached_simple_hull[0] = NULL;
1965 map->cached_simple_hull[1] = NULL;
1966 return map;
1969 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1971 return isl_map_cow(set);
1974 /* Return an isl_map that is equal to "map" and that has only
1975 * a single reference.
1977 * If the original input already has only one reference, then
1978 * simply return it, but clear all cached information, since
1979 * it may be rendered invalid by the operations that will be
1980 * performed on the result.
1982 * Otherwise, create a duplicate (without any cached information).
1984 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1986 if (!map)
1987 return NULL;
1989 if (map->ref == 1)
1990 return clear_caches(map);
1991 map->ref--;
1992 return isl_map_dup(map);
1995 static void swap_vars(struct isl_blk blk, isl_int *a,
1996 unsigned a_len, unsigned b_len)
1998 isl_seq_cpy(blk.data, a+a_len, b_len);
1999 isl_seq_cpy(blk.data+b_len, a, a_len);
2000 isl_seq_cpy(a, blk.data, b_len+a_len);
2003 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2004 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2006 int i;
2007 struct isl_blk blk;
2009 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
2010 goto error;
2012 if (n1 == 0 || n2 == 0)
2013 return bmap;
2015 bmap = isl_basic_map_cow(bmap);
2016 if (!bmap)
2017 return NULL;
2019 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2020 if (isl_blk_is_error(blk))
2021 goto error;
2023 for (i = 0; i < bmap->n_eq; ++i)
2024 swap_vars(blk,
2025 bmap->eq[i] + pos, n1, n2);
2027 for (i = 0; i < bmap->n_ineq; ++i)
2028 swap_vars(blk,
2029 bmap->ineq[i] + pos, n1, n2);
2031 for (i = 0; i < bmap->n_div; ++i)
2032 swap_vars(blk,
2033 bmap->div[i]+1 + pos, n1, n2);
2035 isl_blk_free(bmap->ctx, blk);
2037 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2038 bmap = isl_basic_map_gauss(bmap, NULL);
2039 return isl_basic_map_finalize(bmap);
2040 error:
2041 isl_basic_map_free(bmap);
2042 return NULL;
2045 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2046 __isl_take isl_basic_map *bmap)
2048 int i = 0;
2049 unsigned total;
2050 if (!bmap)
2051 goto error;
2052 total = isl_basic_map_total_dim(bmap);
2053 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2054 return isl_basic_map_free(bmap);
2055 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2056 if (bmap->n_eq > 0)
2057 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2058 else {
2059 i = isl_basic_map_alloc_equality(bmap);
2060 if (i < 0)
2061 goto error;
2063 isl_int_set_si(bmap->eq[i][0], 1);
2064 isl_seq_clr(bmap->eq[i]+1, total);
2065 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2066 isl_vec_free(bmap->sample);
2067 bmap->sample = NULL;
2068 return isl_basic_map_finalize(bmap);
2069 error:
2070 isl_basic_map_free(bmap);
2071 return NULL;
2074 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2075 __isl_take isl_basic_set *bset)
2077 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2080 __isl_give isl_basic_map *isl_basic_map_set_rational(
2081 __isl_take isl_basic_map *bmap)
2083 if (!bmap)
2084 return NULL;
2086 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2087 return bmap;
2089 bmap = isl_basic_map_cow(bmap);
2090 if (!bmap)
2091 return NULL;
2093 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2095 return isl_basic_map_finalize(bmap);
2098 __isl_give isl_basic_set *isl_basic_set_set_rational(
2099 __isl_take isl_basic_set *bset)
2101 return isl_basic_map_set_rational(bset);
2104 __isl_give isl_basic_set *isl_basic_set_set_integral(
2105 __isl_take isl_basic_set *bset)
2107 if (!bset)
2108 return NULL;
2110 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2111 return bset;
2113 bset = isl_basic_set_cow(bset);
2114 if (!bset)
2115 return NULL;
2117 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2119 return isl_basic_set_finalize(bset);
2122 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2124 int i;
2126 map = isl_map_cow(map);
2127 if (!map)
2128 return NULL;
2129 for (i = 0; i < map->n; ++i) {
2130 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2131 if (!map->p[i])
2132 goto error;
2134 return map;
2135 error:
2136 isl_map_free(map);
2137 return NULL;
2140 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2142 return isl_map_set_rational(set);
2145 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2146 * of "bmap").
2148 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2150 isl_int *t = bmap->div[a];
2151 bmap->div[a] = bmap->div[b];
2152 bmap->div[b] = t;
2155 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2156 * div definitions accordingly.
2158 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2159 int a, int b)
2161 int i;
2162 int off;
2164 off = isl_basic_map_var_offset(bmap, isl_dim_div);
2165 if (off < 0)
2166 return isl_basic_map_free(bmap);
2168 swap_div(bmap, a, b);
2170 for (i = 0; i < bmap->n_eq; ++i)
2171 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2173 for (i = 0; i < bmap->n_ineq; ++i)
2174 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2176 for (i = 0; i < bmap->n_div; ++i)
2177 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2178 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2180 return bmap;
2183 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2185 isl_seq_cpy(c, c + n, rem);
2186 isl_seq_clr(c + rem, n);
2189 /* Drop n dimensions starting at first.
2191 * In principle, this frees up some extra variables as the number
2192 * of columns remains constant, but we would have to extend
2193 * the div array too as the number of rows in this array is assumed
2194 * to be equal to extra.
2196 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2197 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2199 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2202 /* Move "n" divs starting at "first" to the end of the list of divs.
2204 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2205 unsigned first, unsigned n)
2207 isl_int **div;
2208 int i;
2210 if (first + n == bmap->n_div)
2211 return bmap;
2213 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2214 if (!div)
2215 goto error;
2216 for (i = 0; i < n; ++i)
2217 div[i] = bmap->div[first + i];
2218 for (i = 0; i < bmap->n_div - first - n; ++i)
2219 bmap->div[first + i] = bmap->div[first + n + i];
2220 for (i = 0; i < n; ++i)
2221 bmap->div[bmap->n_div - n + i] = div[i];
2222 free(div);
2223 return bmap;
2224 error:
2225 isl_basic_map_free(bmap);
2226 return NULL;
2229 #undef TYPE
2230 #define TYPE isl_map
2231 static
2232 #include "check_type_range_templ.c"
2234 /* Check that there are "n" dimensions of type "type" starting at "first"
2235 * in "set".
2237 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2238 enum isl_dim_type type, unsigned first, unsigned n)
2240 return isl_map_check_range(set_to_map(set), type, first, n);
2243 /* Drop "n" dimensions of type "type" starting at "first".
2244 * Perform the core computation, without cowing or
2245 * simplifying and finalizing the result.
2247 * In principle, this frees up some extra variables as the number
2248 * of columns remains constant, but we would have to extend
2249 * the div array too as the number of rows in this array is assumed
2250 * to be equal to extra.
2252 __isl_give isl_basic_map *isl_basic_map_drop_core(
2253 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2254 unsigned first, unsigned n)
2256 int i;
2257 unsigned offset;
2258 unsigned left;
2260 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2261 return isl_basic_map_free(bmap);
2263 offset = isl_basic_map_offset(bmap, type) + first;
2264 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2265 for (i = 0; i < bmap->n_eq; ++i)
2266 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2268 for (i = 0; i < bmap->n_ineq; ++i)
2269 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2271 for (i = 0; i < bmap->n_div; ++i)
2272 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2274 if (type == isl_dim_div) {
2275 bmap = move_divs_last(bmap, first, n);
2276 if (!bmap)
2277 return NULL;
2278 if (isl_basic_map_free_div(bmap, n) < 0)
2279 return isl_basic_map_free(bmap);
2280 } else
2281 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2282 if (!bmap->dim)
2283 return isl_basic_map_free(bmap);
2285 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2286 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2287 return bmap;
2290 /* Drop "n" dimensions of type "type" starting at "first".
2292 * In principle, this frees up some extra variables as the number
2293 * of columns remains constant, but we would have to extend
2294 * the div array too as the number of rows in this array is assumed
2295 * to be equal to extra.
2297 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2298 enum isl_dim_type type, unsigned first, unsigned n)
2300 if (!bmap)
2301 return NULL;
2302 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2303 return bmap;
2305 bmap = isl_basic_map_cow(bmap);
2306 if (!bmap)
2307 return NULL;
2309 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2311 bmap = isl_basic_map_simplify(bmap);
2312 return isl_basic_map_finalize(bmap);
2315 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2316 enum isl_dim_type type, unsigned first, unsigned n)
2318 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2319 type, first, n));
2322 /* No longer consider "map" to be normalized.
2324 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2326 if (!map)
2327 return NULL;
2328 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2329 return map;
2332 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2333 enum isl_dim_type type, unsigned first, unsigned n)
2335 int i;
2337 if (isl_map_check_range(map, type, first, n) < 0)
2338 return isl_map_free(map);
2340 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2341 return map;
2342 map = isl_map_cow(map);
2343 if (!map)
2344 goto error;
2345 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2346 if (!map->dim)
2347 goto error;
2349 for (i = 0; i < map->n; ++i) {
2350 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2351 if (!map->p[i])
2352 goto error;
2354 map = isl_map_unmark_normalized(map);
2356 return map;
2357 error:
2358 isl_map_free(map);
2359 return NULL;
2362 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2363 enum isl_dim_type type, unsigned first, unsigned n)
2365 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2368 /* Drop the integer division at position "div", which is assumed
2369 * not to appear in any of the constraints or
2370 * in any of the other integer divisions.
2372 * Since the integer division is redundant, there is no need to cow.
2374 __isl_give isl_basic_map *isl_basic_map_drop_div(
2375 __isl_take isl_basic_map *bmap, unsigned div)
2377 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2380 /* Eliminate the specified n dimensions starting at first from the
2381 * constraints, without removing the dimensions from the space.
2382 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2384 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2385 enum isl_dim_type type, unsigned first, unsigned n)
2387 int i;
2389 if (n == 0)
2390 return map;
2392 if (isl_map_check_range(map, type, first, n) < 0)
2393 return isl_map_free(map);
2395 map = isl_map_cow(map);
2396 if (!map)
2397 return NULL;
2399 for (i = 0; i < map->n; ++i) {
2400 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2401 if (!map->p[i])
2402 goto error;
2404 return map;
2405 error:
2406 isl_map_free(map);
2407 return NULL;
2410 /* Eliminate the specified n dimensions starting at first from the
2411 * constraints, without removing the dimensions from the space.
2412 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2414 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2415 enum isl_dim_type type, unsigned first, unsigned n)
2417 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2420 /* Eliminate the specified n dimensions starting at first from the
2421 * constraints, without removing the dimensions from the space.
2422 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2424 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2425 unsigned first, unsigned n)
2427 return isl_set_eliminate(set, isl_dim_set, first, n);
2430 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2431 __isl_take isl_basic_map *bmap)
2433 int v_div;
2435 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2436 if (v_div < 0)
2437 return isl_basic_map_free(bmap);
2438 bmap = isl_basic_map_eliminate_vars(bmap, v_div, bmap->n_div);
2439 if (!bmap)
2440 return NULL;
2441 bmap->n_div = 0;
2442 return isl_basic_map_finalize(bmap);
2445 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2446 __isl_take isl_basic_set *bset)
2448 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2451 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2453 int i;
2455 if (!map)
2456 return NULL;
2457 if (map->n == 0)
2458 return map;
2460 map = isl_map_cow(map);
2461 if (!map)
2462 return NULL;
2464 for (i = 0; i < map->n; ++i) {
2465 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2466 if (!map->p[i])
2467 goto error;
2469 return map;
2470 error:
2471 isl_map_free(map);
2472 return NULL;
2475 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2477 return isl_map_remove_divs(set);
2480 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2481 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2482 unsigned first, unsigned n)
2484 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2485 return isl_basic_map_free(bmap);
2486 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2487 return bmap;
2488 bmap = isl_basic_map_eliminate_vars(bmap,
2489 isl_basic_map_offset(bmap, type) - 1 + first, n);
2490 if (!bmap)
2491 return bmap;
2492 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2493 return bmap;
2494 bmap = isl_basic_map_drop(bmap, type, first, n);
2495 return bmap;
2498 /* Return true if the definition of the given div (recursively) involves
2499 * any of the given variables.
2501 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2502 unsigned first, unsigned n)
2504 int i;
2505 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2507 if (isl_int_is_zero(bmap->div[div][0]))
2508 return isl_bool_false;
2509 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2510 return isl_bool_true;
2512 for (i = bmap->n_div - 1; i >= 0; --i) {
2513 isl_bool involves;
2515 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2516 continue;
2517 involves = div_involves_vars(bmap, i, first, n);
2518 if (involves < 0 || involves)
2519 return involves;
2522 return isl_bool_false;
2525 /* Try and add a lower and/or upper bound on "div" to "bmap"
2526 * based on inequality "i".
2527 * "total" is the total number of variables (excluding the divs).
2528 * "v" is a temporary object that can be used during the calculations.
2529 * If "lb" is set, then a lower bound should be constructed.
2530 * If "ub" is set, then an upper bound should be constructed.
2532 * The calling function has already checked that the inequality does not
2533 * reference "div", but we still need to check that the inequality is
2534 * of the right form. We'll consider the case where we want to construct
2535 * a lower bound. The construction of upper bounds is similar.
2537 * Let "div" be of the form
2539 * q = floor((a + f(x))/d)
2541 * We essentially check if constraint "i" is of the form
2543 * b + f(x) >= 0
2545 * so that we can use it to derive a lower bound on "div".
2546 * However, we allow a slightly more general form
2548 * b + g(x) >= 0
2550 * with the condition that the coefficients of g(x) - f(x) are all
2551 * divisible by d.
2552 * Rewriting this constraint as
2554 * 0 >= -b - g(x)
2556 * adding a + f(x) to both sides and dividing by d, we obtain
2558 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2560 * Taking the floor on both sides, we obtain
2562 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2564 * or
2566 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2568 * In the case of an upper bound, we construct the constraint
2570 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2573 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2574 __isl_take isl_basic_map *bmap, int div, int i,
2575 unsigned total, isl_int v, int lb, int ub)
2577 int j;
2579 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2580 if (lb) {
2581 isl_int_sub(v, bmap->ineq[i][1 + j],
2582 bmap->div[div][1 + 1 + j]);
2583 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2585 if (ub) {
2586 isl_int_add(v, bmap->ineq[i][1 + j],
2587 bmap->div[div][1 + 1 + j]);
2588 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2591 if (!lb && !ub)
2592 return bmap;
2594 bmap = isl_basic_map_cow(bmap);
2595 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2596 if (lb) {
2597 int k = isl_basic_map_alloc_inequality(bmap);
2598 if (k < 0)
2599 goto error;
2600 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2601 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2602 bmap->div[div][1 + j]);
2603 isl_int_cdiv_q(bmap->ineq[k][j],
2604 bmap->ineq[k][j], bmap->div[div][0]);
2606 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2608 if (ub) {
2609 int k = isl_basic_map_alloc_inequality(bmap);
2610 if (k < 0)
2611 goto error;
2612 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2613 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2614 bmap->div[div][1 + j]);
2615 isl_int_fdiv_q(bmap->ineq[k][j],
2616 bmap->ineq[k][j], bmap->div[div][0]);
2618 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2621 return bmap;
2622 error:
2623 isl_basic_map_free(bmap);
2624 return NULL;
2627 /* This function is called right before "div" is eliminated from "bmap"
2628 * using Fourier-Motzkin.
2629 * Look through the constraints of "bmap" for constraints on the argument
2630 * of the integer division and use them to construct constraints on the
2631 * integer division itself. These constraints can then be combined
2632 * during the Fourier-Motzkin elimination.
2633 * Note that it is only useful to introduce lower bounds on "div"
2634 * if "bmap" already contains upper bounds on "div" as the newly
2635 * introduce lower bounds can then be combined with the pre-existing
2636 * upper bounds. Similarly for upper bounds.
2637 * We therefore first check if "bmap" contains any lower and/or upper bounds
2638 * on "div".
2640 * It is interesting to note that the introduction of these constraints
2641 * can indeed lead to more accurate results, even when compared to
2642 * deriving constraints on the argument of "div" from constraints on "div".
2643 * Consider, for example, the set
2645 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2647 * The second constraint can be rewritten as
2649 * 2 * [(-i-2j+3)/4] + k >= 0
2651 * from which we can derive
2653 * -i - 2j + 3 >= -2k
2655 * or
2657 * i + 2j <= 3 + 2k
2659 * Combined with the first constraint, we obtain
2661 * -3 <= 3 + 2k or k >= -3
2663 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2664 * the first constraint, we obtain
2666 * [(i + 2j)/4] >= [-3/4] = -1
2668 * Combining this constraint with the second constraint, we obtain
2670 * k >= -2
2672 static __isl_give isl_basic_map *insert_bounds_on_div(
2673 __isl_take isl_basic_map *bmap, int div)
2675 int i;
2676 int check_lb, check_ub;
2677 isl_int v;
2678 int v_div;
2680 if (!bmap)
2681 return NULL;
2683 if (isl_int_is_zero(bmap->div[div][0]))
2684 return bmap;
2686 v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
2687 if (v_div < 0)
2688 return isl_basic_map_free(bmap);
2690 check_lb = 0;
2691 check_ub = 0;
2692 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2693 int s = isl_int_sgn(bmap->ineq[i][1 + v_div + div]);
2694 if (s > 0)
2695 check_ub = 1;
2696 if (s < 0)
2697 check_lb = 1;
2700 if (!check_lb && !check_ub)
2701 return bmap;
2703 isl_int_init(v);
2705 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2706 if (!isl_int_is_zero(bmap->ineq[i][1 + v_div + div]))
2707 continue;
2709 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, v_div, v,
2710 check_lb, check_ub);
2713 isl_int_clear(v);
2715 return bmap;
2718 /* Remove all divs (recursively) involving any of the given dimensions
2719 * in their definitions.
2721 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2722 __isl_take isl_basic_map *bmap,
2723 enum isl_dim_type type, unsigned first, unsigned n)
2725 int i;
2727 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2728 return isl_basic_map_free(bmap);
2729 first += isl_basic_map_offset(bmap, type);
2731 for (i = bmap->n_div - 1; i >= 0; --i) {
2732 isl_bool involves;
2734 involves = div_involves_vars(bmap, i, first, n);
2735 if (involves < 0)
2736 return isl_basic_map_free(bmap);
2737 if (!involves)
2738 continue;
2739 bmap = insert_bounds_on_div(bmap, i);
2740 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2741 if (!bmap)
2742 return NULL;
2743 i = bmap->n_div;
2746 return bmap;
2749 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2750 __isl_take isl_basic_set *bset,
2751 enum isl_dim_type type, unsigned first, unsigned n)
2753 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2756 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2757 enum isl_dim_type type, unsigned first, unsigned n)
2759 int i;
2761 if (!map)
2762 return NULL;
2763 if (map->n == 0)
2764 return map;
2766 map = isl_map_cow(map);
2767 if (!map)
2768 return NULL;
2770 for (i = 0; i < map->n; ++i) {
2771 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2772 type, first, n);
2773 if (!map->p[i])
2774 goto error;
2776 return map;
2777 error:
2778 isl_map_free(map);
2779 return NULL;
2782 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2783 enum isl_dim_type type, unsigned first, unsigned n)
2785 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2786 type, first, n));
2789 /* Does the description of "bmap" depend on the specified dimensions?
2790 * We also check whether the dimensions appear in any of the div definitions.
2791 * In principle there is no need for this check. If the dimensions appear
2792 * in a div definition, they also appear in the defining constraints of that
2793 * div.
2795 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2796 enum isl_dim_type type, unsigned first, unsigned n)
2798 int i;
2800 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2801 return isl_bool_error;
2803 first += isl_basic_map_offset(bmap, type);
2804 for (i = 0; i < bmap->n_eq; ++i)
2805 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2806 return isl_bool_true;
2807 for (i = 0; i < bmap->n_ineq; ++i)
2808 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2809 return isl_bool_true;
2810 for (i = 0; i < bmap->n_div; ++i) {
2811 if (isl_int_is_zero(bmap->div[i][0]))
2812 continue;
2813 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2814 return isl_bool_true;
2817 return isl_bool_false;
2820 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2821 enum isl_dim_type type, unsigned first, unsigned n)
2823 int i;
2825 if (isl_map_check_range(map, type, first, n) < 0)
2826 return isl_bool_error;
2828 for (i = 0; i < map->n; ++i) {
2829 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2830 type, first, n);
2831 if (involves < 0 || involves)
2832 return involves;
2835 return isl_bool_false;
2838 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2839 enum isl_dim_type type, unsigned first, unsigned n)
2841 return isl_basic_map_involves_dims(bset, type, first, n);
2844 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2845 enum isl_dim_type type, unsigned first, unsigned n)
2847 return isl_map_involves_dims(set, type, first, n);
2850 /* Drop all constraints in bmap that involve any of the dimensions
2851 * first to first+n-1.
2852 * This function only performs the actual removal of constraints.
2854 * This function should not call finalize since it is used by
2855 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2857 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2858 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2860 int i;
2862 if (n == 0)
2863 return bmap;
2865 bmap = isl_basic_map_cow(bmap);
2867 if (!bmap)
2868 return NULL;
2870 for (i = bmap->n_eq - 1; i >= 0; --i) {
2871 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2872 continue;
2873 isl_basic_map_drop_equality(bmap, i);
2876 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2877 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2878 continue;
2879 isl_basic_map_drop_inequality(bmap, i);
2882 return bmap;
2885 /* Drop all constraints in bset that involve any of the dimensions
2886 * first to first+n-1.
2887 * This function only performs the actual removal of constraints.
2889 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2890 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2892 return isl_basic_map_drop_constraints_involving(bset, first, n);
2895 /* Drop all constraints in bmap that do not involve any of the dimensions
2896 * first to first + n - 1 of the given type.
2898 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2899 __isl_take isl_basic_map *bmap,
2900 enum isl_dim_type type, unsigned first, unsigned n)
2902 int i;
2904 if (n == 0) {
2905 isl_space *space = isl_basic_map_get_space(bmap);
2906 isl_basic_map_free(bmap);
2907 return isl_basic_map_universe(space);
2909 bmap = isl_basic_map_cow(bmap);
2910 if (!bmap)
2911 return NULL;
2913 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2914 return isl_basic_map_free(bmap);
2916 first += isl_basic_map_offset(bmap, type) - 1;
2918 for (i = bmap->n_eq - 1; i >= 0; --i) {
2919 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2920 continue;
2921 isl_basic_map_drop_equality(bmap, i);
2924 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2925 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2926 continue;
2927 isl_basic_map_drop_inequality(bmap, i);
2930 bmap = isl_basic_map_add_known_div_constraints(bmap);
2931 return bmap;
2934 /* Drop all constraints in bset that do not involve any of the dimensions
2935 * first to first + n - 1 of the given type.
2937 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2938 __isl_take isl_basic_set *bset,
2939 enum isl_dim_type type, unsigned first, unsigned n)
2941 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2942 type, first, n);
2945 /* Drop all constraints in bmap that involve any of the dimensions
2946 * first to first + n - 1 of the given type.
2948 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2949 __isl_take isl_basic_map *bmap,
2950 enum isl_dim_type type, unsigned first, unsigned n)
2952 if (!bmap)
2953 return NULL;
2954 if (n == 0)
2955 return bmap;
2957 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2958 return isl_basic_map_free(bmap);
2960 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2961 first += isl_basic_map_offset(bmap, type) - 1;
2962 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2963 bmap = isl_basic_map_add_known_div_constraints(bmap);
2964 return bmap;
2967 /* Drop all constraints in bset that involve any of the dimensions
2968 * first to first + n - 1 of the given type.
2970 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2971 __isl_take isl_basic_set *bset,
2972 enum isl_dim_type type, unsigned first, unsigned n)
2974 return isl_basic_map_drop_constraints_involving_dims(bset,
2975 type, first, n);
2978 /* Drop constraints from "map" by applying "drop" to each basic map.
2980 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2981 enum isl_dim_type type, unsigned first, unsigned n,
2982 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2983 enum isl_dim_type type, unsigned first, unsigned n))
2985 int i;
2987 if (isl_map_check_range(map, type, first, n) < 0)
2988 return isl_map_free(map);
2990 map = isl_map_cow(map);
2991 if (!map)
2992 return NULL;
2994 for (i = 0; i < map->n; ++i) {
2995 map->p[i] = drop(map->p[i], type, first, n);
2996 if (!map->p[i])
2997 return isl_map_free(map);
3000 if (map->n > 1)
3001 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3003 return map;
3006 /* Drop all constraints in map that involve any of the dimensions
3007 * first to first + n - 1 of the given type.
3009 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3010 __isl_take isl_map *map,
3011 enum isl_dim_type type, unsigned first, unsigned n)
3013 if (n == 0)
3014 return map;
3015 return drop_constraints(map, type, first, n,
3016 &isl_basic_map_drop_constraints_involving_dims);
3019 /* Drop all constraints in "map" that do not involve any of the dimensions
3020 * first to first + n - 1 of the given type.
3022 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3023 __isl_take isl_map *map,
3024 enum isl_dim_type type, unsigned first, unsigned n)
3026 if (n == 0) {
3027 isl_space *space = isl_map_get_space(map);
3028 isl_map_free(map);
3029 return isl_map_universe(space);
3031 return drop_constraints(map, type, first, n,
3032 &isl_basic_map_drop_constraints_not_involving_dims);
3035 /* Drop all constraints in set that involve any of the dimensions
3036 * first to first + n - 1 of the given type.
3038 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3039 __isl_take isl_set *set,
3040 enum isl_dim_type type, unsigned first, unsigned n)
3042 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3045 /* Drop all constraints in "set" that do not involve any of the dimensions
3046 * first to first + n - 1 of the given type.
3048 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3049 __isl_take isl_set *set,
3050 enum isl_dim_type type, unsigned first, unsigned n)
3052 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3055 /* Does local variable "div" of "bmap" have a complete explicit representation?
3056 * Having a complete explicit representation requires not only
3057 * an explicit representation, but also that all local variables
3058 * that appear in this explicit representation in turn have
3059 * a complete explicit representation.
3061 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3063 int i;
3064 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3065 isl_bool marked;
3067 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3068 if (marked < 0 || marked)
3069 return isl_bool_not(marked);
3071 for (i = bmap->n_div - 1; i >= 0; --i) {
3072 isl_bool known;
3074 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3075 continue;
3076 known = isl_basic_map_div_is_known(bmap, i);
3077 if (known < 0 || !known)
3078 return known;
3081 return isl_bool_true;
3084 /* Remove all divs that are unknown or defined in terms of unknown divs.
3086 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3087 __isl_take isl_basic_map *bmap)
3089 int i;
3091 if (!bmap)
3092 return NULL;
3094 for (i = bmap->n_div - 1; i >= 0; --i) {
3095 if (isl_basic_map_div_is_known(bmap, i))
3096 continue;
3097 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3098 if (!bmap)
3099 return NULL;
3100 i = bmap->n_div;
3103 return bmap;
3106 /* Remove all divs that are unknown or defined in terms of unknown divs.
3108 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3109 __isl_take isl_basic_set *bset)
3111 return isl_basic_map_remove_unknown_divs(bset);
3114 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3116 int i;
3118 if (!map)
3119 return NULL;
3120 if (map->n == 0)
3121 return map;
3123 map = isl_map_cow(map);
3124 if (!map)
3125 return NULL;
3127 for (i = 0; i < map->n; ++i) {
3128 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3129 if (!map->p[i])
3130 goto error;
3132 return map;
3133 error:
3134 isl_map_free(map);
3135 return NULL;
3138 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3140 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3143 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3144 __isl_take isl_basic_set *bset,
3145 enum isl_dim_type type, unsigned first, unsigned n)
3147 isl_basic_map *bmap = bset_to_bmap(bset);
3148 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3149 return bset_from_bmap(bmap);
3152 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3153 enum isl_dim_type type, unsigned first, unsigned n)
3155 int i;
3157 if (n == 0)
3158 return map;
3160 map = isl_map_cow(map);
3161 if (isl_map_check_range(map, type, first, n) < 0)
3162 return isl_map_free(map);
3164 for (i = 0; i < map->n; ++i) {
3165 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3166 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3167 if (!map->p[i])
3168 goto error;
3170 map = isl_map_drop(map, type, first, n);
3171 return map;
3172 error:
3173 isl_map_free(map);
3174 return NULL;
3177 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3178 enum isl_dim_type type, unsigned first, unsigned n)
3180 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3181 type, first, n));
3184 /* Project out n inputs starting at first using Fourier-Motzkin */
3185 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3186 unsigned first, unsigned n)
3188 return isl_map_remove_dims(map, isl_dim_in, first, n);
3191 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3192 FILE *out, int indent)
3194 isl_printer *p;
3196 if (!bset) {
3197 fprintf(out, "null basic set\n");
3198 return;
3201 fprintf(out, "%*s", indent, "");
3202 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3203 bset->ref, bset->dim->nparam, bset->dim->n_out,
3204 bset->extra, bset->flags);
3206 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3207 p = isl_printer_set_dump(p, 1);
3208 p = isl_printer_set_indent(p, indent);
3209 p = isl_printer_start_line(p);
3210 p = isl_printer_print_basic_set(p, bset);
3211 p = isl_printer_end_line(p);
3212 isl_printer_free(p);
3215 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3216 FILE *out, int indent)
3218 isl_printer *p;
3220 if (!bmap) {
3221 fprintf(out, "null basic map\n");
3222 return;
3225 fprintf(out, "%*s", indent, "");
3226 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3227 "flags: %x, n_name: %d\n",
3228 bmap->ref,
3229 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3230 bmap->extra, bmap->flags, bmap->dim->n_id);
3232 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3233 p = isl_printer_set_dump(p, 1);
3234 p = isl_printer_set_indent(p, indent);
3235 p = isl_printer_start_line(p);
3236 p = isl_printer_print_basic_map(p, bmap);
3237 p = isl_printer_end_line(p);
3238 isl_printer_free(p);
3241 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3242 unsigned pos)
3244 unsigned total;
3246 if (!bmap)
3247 return NULL;
3248 total = isl_basic_map_total_dim(bmap);
3249 if (pos >= bmap->n_ineq)
3250 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3251 "invalid position", return isl_basic_map_free(bmap));
3252 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3253 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3254 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3255 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3256 return bmap;
3259 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3260 unsigned flags)
3262 if (isl_space_check_is_set(space) < 0)
3263 goto error;
3264 return isl_map_alloc_space(space, n, flags);
3265 error:
3266 isl_space_free(space);
3267 return NULL;
3270 /* Make sure "map" has room for at least "n" more basic maps.
3272 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3274 int i;
3275 struct isl_map *grown = NULL;
3277 if (!map)
3278 return NULL;
3279 isl_assert(map->ctx, n >= 0, goto error);
3280 if (map->n + n <= map->size)
3281 return map;
3282 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3283 if (!grown)
3284 goto error;
3285 for (i = 0; i < map->n; ++i) {
3286 grown->p[i] = isl_basic_map_copy(map->p[i]);
3287 if (!grown->p[i])
3288 goto error;
3289 grown->n++;
3291 isl_map_free(map);
3292 return grown;
3293 error:
3294 isl_map_free(grown);
3295 isl_map_free(map);
3296 return NULL;
3299 /* Make sure "set" has room for at least "n" more basic sets.
3301 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3303 return set_from_map(isl_map_grow(set_to_map(set), n));
3306 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3308 return isl_map_from_basic_map(bset);
3311 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3313 struct isl_map *map;
3315 if (!bmap)
3316 return NULL;
3318 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3319 return isl_map_add_basic_map(map, bmap);
3322 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3323 __isl_take isl_basic_set *bset)
3325 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3326 bset_to_bmap(bset)));
3329 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3331 return isl_map_free(set);
3334 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3336 int i;
3338 if (!set) {
3339 fprintf(out, "null set\n");
3340 return;
3343 fprintf(out, "%*s", indent, "");
3344 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3345 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3346 set->flags);
3347 for (i = 0; i < set->n; ++i) {
3348 fprintf(out, "%*s", indent, "");
3349 fprintf(out, "basic set %d:\n", i);
3350 isl_basic_set_print_internal(set->p[i], out, indent+4);
3354 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3356 int i;
3358 if (!map) {
3359 fprintf(out, "null map\n");
3360 return;
3363 fprintf(out, "%*s", indent, "");
3364 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3365 "flags: %x, n_name: %d\n",
3366 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3367 map->dim->n_out, map->flags, map->dim->n_id);
3368 for (i = 0; i < map->n; ++i) {
3369 fprintf(out, "%*s", indent, "");
3370 fprintf(out, "basic map %d:\n", i);
3371 isl_basic_map_print_internal(map->p[i], out, indent+4);
3375 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3376 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3378 struct isl_basic_map *bmap_domain;
3380 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3381 goto error;
3383 if (isl_basic_set_dim(bset, isl_dim_set) != 0)
3384 isl_assert(bset->ctx,
3385 isl_basic_map_compatible_domain(bmap, bset), goto error);
3387 bmap = isl_basic_map_cow(bmap);
3388 if (!bmap)
3389 goto error;
3390 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3391 bset->n_div, bset->n_eq, bset->n_ineq);
3392 bmap_domain = isl_basic_map_from_domain(bset);
3393 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3395 bmap = isl_basic_map_simplify(bmap);
3396 return isl_basic_map_finalize(bmap);
3397 error:
3398 isl_basic_map_free(bmap);
3399 isl_basic_set_free(bset);
3400 return NULL;
3403 /* Check that the space of "bset" is the same as that of the range of "bmap".
3405 static isl_stat isl_basic_map_check_compatible_range(
3406 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3408 isl_bool ok;
3410 ok = isl_basic_map_compatible_range(bmap, bset);
3411 if (ok < 0)
3412 return isl_stat_error;
3413 if (!ok)
3414 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3415 "incompatible spaces", return isl_stat_error);
3417 return isl_stat_ok;
3420 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3421 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3423 struct isl_basic_map *bmap_range;
3425 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3426 goto error;
3428 if (isl_basic_set_dim(bset, isl_dim_set) != 0 &&
3429 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3430 goto error;
3432 if (isl_basic_set_plain_is_universe(bset)) {
3433 isl_basic_set_free(bset);
3434 return bmap;
3437 bmap = isl_basic_map_cow(bmap);
3438 if (!bmap)
3439 goto error;
3440 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3441 bset->n_div, bset->n_eq, bset->n_ineq);
3442 bmap_range = bset_to_bmap(bset);
3443 bmap = add_constraints(bmap, bmap_range, 0, 0);
3445 bmap = isl_basic_map_simplify(bmap);
3446 return isl_basic_map_finalize(bmap);
3447 error:
3448 isl_basic_map_free(bmap);
3449 isl_basic_set_free(bset);
3450 return NULL;
3453 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3454 __isl_keep isl_vec *vec)
3456 int i;
3457 unsigned total;
3458 isl_int s;
3460 if (!bmap || !vec)
3461 return isl_bool_error;
3463 total = 1 + isl_basic_map_total_dim(bmap);
3464 if (total != vec->size)
3465 return isl_bool_false;
3467 isl_int_init(s);
3469 for (i = 0; i < bmap->n_eq; ++i) {
3470 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3471 if (!isl_int_is_zero(s)) {
3472 isl_int_clear(s);
3473 return isl_bool_false;
3477 for (i = 0; i < bmap->n_ineq; ++i) {
3478 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3479 if (isl_int_is_neg(s)) {
3480 isl_int_clear(s);
3481 return isl_bool_false;
3485 isl_int_clear(s);
3487 return isl_bool_true;
3490 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3491 __isl_keep isl_vec *vec)
3493 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3496 __isl_give isl_basic_map *isl_basic_map_intersect(
3497 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3499 struct isl_vec *sample = NULL;
3500 isl_space *space1, *space2;
3502 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3503 goto error;
3504 space1 = isl_basic_map_peek_space(bmap1);
3505 space2 = isl_basic_map_peek_space(bmap2);
3506 if (isl_space_dim(space1, isl_dim_all) ==
3507 isl_space_dim(space1, isl_dim_param) &&
3508 isl_space_dim(space2, isl_dim_all) !=
3509 isl_space_dim(space2, isl_dim_param))
3510 return isl_basic_map_intersect(bmap2, bmap1);
3512 if (isl_space_dim(space2, isl_dim_all) !=
3513 isl_space_dim(space2, isl_dim_param))
3514 isl_assert(bmap1->ctx,
3515 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3517 if (isl_basic_map_plain_is_empty(bmap1)) {
3518 isl_basic_map_free(bmap2);
3519 return bmap1;
3521 if (isl_basic_map_plain_is_empty(bmap2)) {
3522 isl_basic_map_free(bmap1);
3523 return bmap2;
3526 if (bmap1->sample &&
3527 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3528 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3529 sample = isl_vec_copy(bmap1->sample);
3530 else if (bmap2->sample &&
3531 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3532 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3533 sample = isl_vec_copy(bmap2->sample);
3535 bmap1 = isl_basic_map_cow(bmap1);
3536 if (!bmap1)
3537 goto error;
3538 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3539 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3540 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3542 if (!bmap1)
3543 isl_vec_free(sample);
3544 else if (sample) {
3545 isl_vec_free(bmap1->sample);
3546 bmap1->sample = sample;
3549 bmap1 = isl_basic_map_simplify(bmap1);
3550 return isl_basic_map_finalize(bmap1);
3551 error:
3552 if (sample)
3553 isl_vec_free(sample);
3554 isl_basic_map_free(bmap1);
3555 isl_basic_map_free(bmap2);
3556 return NULL;
3559 struct isl_basic_set *isl_basic_set_intersect(
3560 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3562 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3563 bset_to_bmap(bset2)));
3566 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3567 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3569 return isl_basic_set_intersect(bset1, bset2);
3572 /* Special case of isl_map_intersect, where both map1 and map2
3573 * are convex, without any divs and such that either map1 or map2
3574 * contains a single constraint. This constraint is then simply
3575 * added to the other map.
3577 static __isl_give isl_map *map_intersect_add_constraint(
3578 __isl_take isl_map *map1, __isl_take isl_map *map2)
3580 isl_assert(map1->ctx, map1->n == 1, goto error);
3581 isl_assert(map2->ctx, map1->n == 1, goto error);
3582 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3583 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3585 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3586 return isl_map_intersect(map2, map1);
3588 map1 = isl_map_cow(map1);
3589 if (!map1)
3590 goto error;
3591 if (isl_map_plain_is_empty(map1)) {
3592 isl_map_free(map2);
3593 return map1;
3595 if (map2->p[0]->n_eq == 1)
3596 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3597 else
3598 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3599 map2->p[0]->ineq[0]);
3601 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3602 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3603 if (!map1->p[0])
3604 goto error;
3606 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3607 isl_basic_map_free(map1->p[0]);
3608 map1->n = 0;
3611 isl_map_free(map2);
3613 map1 = isl_map_unmark_normalized(map1);
3614 return map1;
3615 error:
3616 isl_map_free(map1);
3617 isl_map_free(map2);
3618 return NULL;
3621 /* map2 may be either a parameter domain or a map living in the same
3622 * space as map1.
3624 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3625 __isl_take isl_map *map2)
3627 unsigned flags = 0;
3628 isl_bool equal;
3629 isl_map *result;
3630 int i, j;
3632 if (!map1 || !map2)
3633 goto error;
3635 if ((isl_map_plain_is_empty(map1) ||
3636 isl_map_plain_is_universe(map2)) &&
3637 isl_space_is_equal(map1->dim, map2->dim)) {
3638 isl_map_free(map2);
3639 return map1;
3641 if ((isl_map_plain_is_empty(map2) ||
3642 isl_map_plain_is_universe(map1)) &&
3643 isl_space_is_equal(map1->dim, map2->dim)) {
3644 isl_map_free(map1);
3645 return map2;
3648 if (map1->n == 1 && map2->n == 1 &&
3649 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3650 isl_space_is_equal(map1->dim, map2->dim) &&
3651 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3652 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3653 return map_intersect_add_constraint(map1, map2);
3655 equal = isl_map_plain_is_equal(map1, map2);
3656 if (equal < 0)
3657 goto error;
3658 if (equal) {
3659 isl_map_free(map2);
3660 return map1;
3663 if (isl_map_dim(map2, isl_dim_all) != isl_map_dim(map2, isl_dim_param))
3664 isl_assert(map1->ctx,
3665 isl_space_is_equal(map1->dim, map2->dim), goto error);
3667 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3668 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3669 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3671 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3672 map1->n * map2->n, flags);
3673 if (!result)
3674 goto error;
3675 for (i = 0; i < map1->n; ++i)
3676 for (j = 0; j < map2->n; ++j) {
3677 struct isl_basic_map *part;
3678 part = isl_basic_map_intersect(
3679 isl_basic_map_copy(map1->p[i]),
3680 isl_basic_map_copy(map2->p[j]));
3681 if (isl_basic_map_is_empty(part) < 0)
3682 part = isl_basic_map_free(part);
3683 result = isl_map_add_basic_map(result, part);
3684 if (!result)
3685 goto error;
3687 isl_map_free(map1);
3688 isl_map_free(map2);
3689 return result;
3690 error:
3691 isl_map_free(map1);
3692 isl_map_free(map2);
3693 return NULL;
3696 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3697 __isl_take isl_map *map2)
3699 if (!map1 || !map2)
3700 goto error;
3701 if (!isl_space_is_equal(map1->dim, map2->dim))
3702 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3703 "spaces don't match", goto error);
3704 return map_intersect_internal(map1, map2);
3705 error:
3706 isl_map_free(map1);
3707 isl_map_free(map2);
3708 return NULL;
3711 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3712 __isl_take isl_map *map2)
3714 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3717 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3719 return set_from_map(isl_map_intersect(set_to_map(set1),
3720 set_to_map(set2)));
3723 /* map_intersect_internal accepts intersections
3724 * with parameter domains, so we can just call that function.
3726 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3727 __isl_take isl_set *params)
3729 return map_intersect_internal(map, params);
3732 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3733 __isl_take isl_map *map2)
3735 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3738 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3739 __isl_take isl_set *params)
3741 return isl_map_intersect_params(set, params);
3744 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3746 isl_space *space;
3747 unsigned pos, n1, n2;
3749 if (!bmap)
3750 return NULL;
3751 bmap = isl_basic_map_cow(bmap);
3752 if (!bmap)
3753 return NULL;
3754 space = isl_space_reverse(isl_space_copy(bmap->dim));
3755 pos = isl_basic_map_offset(bmap, isl_dim_in);
3756 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3757 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3758 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3759 return isl_basic_map_reset_space(bmap, space);
3762 static __isl_give isl_basic_map *basic_map_space_reset(
3763 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3765 isl_space *space;
3767 if (!bmap)
3768 return NULL;
3769 if (!isl_space_is_named_or_nested(bmap->dim, type))
3770 return bmap;
3772 space = isl_basic_map_get_space(bmap);
3773 space = isl_space_reset(space, type);
3774 bmap = isl_basic_map_reset_space(bmap, space);
3775 return bmap;
3778 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3779 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3780 unsigned pos, unsigned n)
3782 isl_bool rational, is_empty;
3783 isl_space *res_space;
3784 struct isl_basic_map *res;
3785 struct isl_dim_map *dim_map;
3786 unsigned total, off;
3787 enum isl_dim_type t;
3789 if (n == 0)
3790 return basic_map_space_reset(bmap, type);
3792 is_empty = isl_basic_map_plain_is_empty(bmap);
3793 if (is_empty < 0)
3794 return isl_basic_map_free(bmap);
3795 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3796 type, pos, n);
3797 if (!res_space)
3798 return isl_basic_map_free(bmap);
3799 if (is_empty) {
3800 isl_basic_map_free(bmap);
3801 return isl_basic_map_empty(res_space);
3804 total = isl_basic_map_total_dim(bmap) + n;
3805 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3806 off = 0;
3807 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3808 if (t != type) {
3809 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3810 } else {
3811 unsigned size = isl_basic_map_dim(bmap, t);
3812 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3813 0, pos, off);
3814 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3815 pos, size - pos, off + pos + n);
3817 off += isl_space_dim(res_space, t);
3819 isl_dim_map_div(dim_map, bmap, off);
3821 res = isl_basic_map_alloc_space(res_space,
3822 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3823 rational = isl_basic_map_is_rational(bmap);
3824 if (rational < 0)
3825 res = isl_basic_map_free(res);
3826 if (rational)
3827 res = isl_basic_map_set_rational(res);
3828 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3829 return isl_basic_map_finalize(res);
3832 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3833 __isl_take isl_basic_set *bset,
3834 enum isl_dim_type type, unsigned pos, unsigned n)
3836 return isl_basic_map_insert_dims(bset, type, pos, n);
3839 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3840 enum isl_dim_type type, unsigned n)
3842 if (!bmap)
3843 return NULL;
3844 return isl_basic_map_insert_dims(bmap, type,
3845 isl_basic_map_dim(bmap, type), n);
3848 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3849 enum isl_dim_type type, unsigned n)
3851 if (!bset)
3852 return NULL;
3853 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3854 return isl_basic_map_add_dims(bset, type, n);
3855 error:
3856 isl_basic_set_free(bset);
3857 return NULL;
3860 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3861 enum isl_dim_type type)
3863 isl_space *space;
3865 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3866 return map;
3868 space = isl_map_get_space(map);
3869 space = isl_space_reset(space, type);
3870 map = isl_map_reset_space(map, space);
3871 return map;
3874 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3875 enum isl_dim_type type, unsigned pos, unsigned n)
3877 int i;
3879 if (n == 0)
3880 return map_space_reset(map, type);
3882 map = isl_map_cow(map);
3883 if (!map)
3884 return NULL;
3886 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3887 if (!map->dim)
3888 goto error;
3890 for (i = 0; i < map->n; ++i) {
3891 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3892 if (!map->p[i])
3893 goto error;
3896 return map;
3897 error:
3898 isl_map_free(map);
3899 return NULL;
3902 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3903 enum isl_dim_type type, unsigned pos, unsigned n)
3905 return isl_map_insert_dims(set, type, pos, n);
3908 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3909 enum isl_dim_type type, unsigned n)
3911 if (!map)
3912 return NULL;
3913 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3916 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3917 enum isl_dim_type type, unsigned n)
3919 if (!set)
3920 return NULL;
3921 isl_assert(set->ctx, type != isl_dim_in, goto error);
3922 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3923 error:
3924 isl_set_free(set);
3925 return NULL;
3928 __isl_give isl_basic_map *isl_basic_map_move_dims(
3929 __isl_take isl_basic_map *bmap,
3930 enum isl_dim_type dst_type, unsigned dst_pos,
3931 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3933 isl_space *space;
3934 struct isl_dim_map *dim_map;
3935 struct isl_basic_map *res;
3936 enum isl_dim_type t;
3937 unsigned total, off;
3939 if (!bmap)
3940 return NULL;
3941 if (n == 0) {
3942 bmap = isl_basic_map_reset(bmap, src_type);
3943 bmap = isl_basic_map_reset(bmap, dst_type);
3944 return bmap;
3947 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3948 return isl_basic_map_free(bmap);
3950 if (dst_type == src_type && dst_pos == src_pos)
3951 return bmap;
3953 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3955 if (pos(bmap->dim, dst_type) + dst_pos ==
3956 pos(bmap->dim, src_type) + src_pos +
3957 ((src_type < dst_type) ? n : 0)) {
3958 bmap = isl_basic_map_cow(bmap);
3959 if (!bmap)
3960 return NULL;
3962 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3963 src_type, src_pos, n);
3964 if (!bmap->dim)
3965 goto error;
3967 bmap = isl_basic_map_finalize(bmap);
3969 return bmap;
3972 total = isl_basic_map_total_dim(bmap);
3973 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3975 off = 0;
3976 space = isl_basic_map_peek_space(bmap);
3977 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3978 unsigned size = isl_space_dim(space, t);
3979 if (t == dst_type) {
3980 isl_dim_map_dim_range(dim_map, space, t,
3981 0, dst_pos, off);
3982 off += dst_pos;
3983 isl_dim_map_dim_range(dim_map, space, src_type,
3984 src_pos, n, off);
3985 off += n;
3986 isl_dim_map_dim_range(dim_map, space, t,
3987 dst_pos, size - dst_pos, off);
3988 off += size - dst_pos;
3989 } else if (t == src_type) {
3990 isl_dim_map_dim_range(dim_map, space, t,
3991 0, src_pos, off);
3992 off += src_pos;
3993 isl_dim_map_dim_range(dim_map, space, t,
3994 src_pos + n, size - src_pos - n, off);
3995 off += size - src_pos - n;
3996 } else {
3997 isl_dim_map_dim(dim_map, space, t, off);
3998 off += size;
4001 isl_dim_map_div(dim_map, bmap, off);
4003 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4004 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4005 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4006 if (!bmap)
4007 goto error;
4009 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4010 src_type, src_pos, n);
4011 if (!bmap->dim)
4012 goto error;
4014 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4015 bmap = isl_basic_map_gauss(bmap, NULL);
4016 bmap = isl_basic_map_finalize(bmap);
4018 return bmap;
4019 error:
4020 isl_basic_map_free(bmap);
4021 return NULL;
4024 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4025 enum isl_dim_type dst_type, unsigned dst_pos,
4026 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4028 isl_basic_map *bmap = bset_to_bmap(bset);
4029 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4030 src_type, src_pos, n);
4031 return bset_from_bmap(bmap);
4034 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4035 enum isl_dim_type dst_type, unsigned dst_pos,
4036 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4038 if (!set)
4039 return NULL;
4040 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4041 return set_from_map(isl_map_move_dims(set_to_map(set),
4042 dst_type, dst_pos, src_type, src_pos, n));
4043 error:
4044 isl_set_free(set);
4045 return NULL;
4048 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4049 enum isl_dim_type dst_type, unsigned dst_pos,
4050 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4052 int i;
4054 if (n == 0) {
4055 map = isl_map_reset(map, src_type);
4056 map = isl_map_reset(map, dst_type);
4057 return map;
4060 if (isl_map_check_range(map, src_type, src_pos, n))
4061 return isl_map_free(map);
4063 if (dst_type == src_type && dst_pos == src_pos)
4064 return map;
4066 isl_assert(map->ctx, dst_type != src_type, goto error);
4068 map = isl_map_cow(map);
4069 if (!map)
4070 return NULL;
4072 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4073 if (!map->dim)
4074 goto error;
4076 for (i = 0; i < map->n; ++i) {
4077 map->p[i] = isl_basic_map_move_dims(map->p[i],
4078 dst_type, dst_pos,
4079 src_type, src_pos, n);
4080 if (!map->p[i])
4081 goto error;
4084 return map;
4085 error:
4086 isl_map_free(map);
4087 return NULL;
4090 /* Move the specified dimensions to the last columns right before
4091 * the divs. Don't change the dimension specification of bmap.
4092 * That's the responsibility of the caller.
4094 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4095 enum isl_dim_type type, unsigned first, unsigned n)
4097 isl_space *space;
4098 struct isl_dim_map *dim_map;
4099 struct isl_basic_map *res;
4100 enum isl_dim_type t;
4101 unsigned total, off;
4103 if (!bmap)
4104 return NULL;
4105 if (isl_basic_map_offset(bmap, type) + first + n ==
4106 isl_basic_map_offset(bmap, isl_dim_div))
4107 return bmap;
4109 total = isl_basic_map_total_dim(bmap);
4110 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4112 off = 0;
4113 space = isl_basic_map_peek_space(bmap);
4114 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4115 unsigned size = isl_space_dim(space, t);
4116 if (t == type) {
4117 isl_dim_map_dim_range(dim_map, space, t,
4118 0, first, off);
4119 off += first;
4120 isl_dim_map_dim_range(dim_map, space, t,
4121 first, n, total - bmap->n_div - n);
4122 isl_dim_map_dim_range(dim_map, space, t,
4123 first + n, size - (first + n), off);
4124 off += size - (first + n);
4125 } else {
4126 isl_dim_map_dim(dim_map, space, t, off);
4127 off += size;
4130 isl_dim_map_div(dim_map, bmap, off + n);
4132 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4133 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4134 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4135 return res;
4138 /* Insert "n" rows in the divs of "bmap".
4140 * The number of columns is not changed, which means that the last
4141 * dimensions of "bmap" are being reintepreted as the new divs.
4142 * The space of "bmap" is not adjusted, however, which means
4143 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4144 * from the space of "bmap" is the responsibility of the caller.
4146 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4147 int n)
4149 int i;
4150 size_t row_size;
4151 isl_int **new_div;
4152 isl_int *old;
4154 bmap = isl_basic_map_cow(bmap);
4155 if (!bmap)
4156 return NULL;
4158 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4159 old = bmap->block2.data;
4160 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4161 (bmap->extra + n) * (1 + row_size));
4162 if (!bmap->block2.data)
4163 return isl_basic_map_free(bmap);
4164 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4165 if (!new_div)
4166 return isl_basic_map_free(bmap);
4167 for (i = 0; i < n; ++i) {
4168 new_div[i] = bmap->block2.data +
4169 (bmap->extra + i) * (1 + row_size);
4170 isl_seq_clr(new_div[i], 1 + row_size);
4172 for (i = 0; i < bmap->extra; ++i)
4173 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4174 free(bmap->div);
4175 bmap->div = new_div;
4176 bmap->n_div += n;
4177 bmap->extra += n;
4179 return bmap;
4182 /* Drop constraints from "bmap" that only involve the variables
4183 * of "type" in the range [first, first + n] that are not related
4184 * to any of the variables outside that interval.
4185 * These constraints cannot influence the values for the variables
4186 * outside the interval, except in case they cause "bmap" to be empty.
4187 * Only drop the constraints if "bmap" is known to be non-empty.
4189 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4190 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4191 unsigned first, unsigned n)
4193 int i;
4194 int *groups;
4195 unsigned dim, n_div;
4196 isl_bool non_empty;
4198 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4199 if (non_empty < 0)
4200 return isl_basic_map_free(bmap);
4201 if (!non_empty)
4202 return bmap;
4204 dim = isl_basic_map_dim(bmap, isl_dim_all);
4205 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4206 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4207 if (!groups)
4208 return isl_basic_map_free(bmap);
4209 first += isl_basic_map_offset(bmap, type) - 1;
4210 for (i = 0; i < first; ++i)
4211 groups[i] = -1;
4212 for (i = first + n; i < dim - n_div; ++i)
4213 groups[i] = -1;
4215 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4217 return bmap;
4220 /* Turn the n dimensions of type type, starting at first
4221 * into existentially quantified variables.
4223 * If a subset of the projected out variables are unrelated
4224 * to any of the variables that remain, then the constraints
4225 * involving this subset are simply dropped first.
4227 __isl_give isl_basic_map *isl_basic_map_project_out(
4228 __isl_take isl_basic_map *bmap,
4229 enum isl_dim_type type, unsigned first, unsigned n)
4231 isl_bool empty;
4233 if (n == 0)
4234 return basic_map_space_reset(bmap, type);
4235 if (type == isl_dim_div)
4236 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4237 "cannot project out existentially quantified variables",
4238 return isl_basic_map_free(bmap));
4240 empty = isl_basic_map_plain_is_empty(bmap);
4241 if (empty < 0)
4242 return isl_basic_map_free(bmap);
4243 if (empty)
4244 bmap = isl_basic_map_set_to_empty(bmap);
4246 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4247 if (!bmap)
4248 return NULL;
4250 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4251 return isl_basic_map_remove_dims(bmap, type, first, n);
4253 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4254 return isl_basic_map_free(bmap);
4256 bmap = move_last(bmap, type, first, n);
4257 bmap = isl_basic_map_cow(bmap);
4258 bmap = insert_div_rows(bmap, n);
4259 if (!bmap)
4260 return NULL;
4262 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4263 if (!bmap->dim)
4264 goto error;
4265 bmap = isl_basic_map_simplify(bmap);
4266 bmap = isl_basic_map_drop_redundant_divs(bmap);
4267 return isl_basic_map_finalize(bmap);
4268 error:
4269 isl_basic_map_free(bmap);
4270 return NULL;
4273 /* Turn the n dimensions of type type, starting at first
4274 * into existentially quantified variables.
4276 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4277 enum isl_dim_type type, unsigned first, unsigned n)
4279 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4280 type, first, n));
4283 /* Turn the n dimensions of type type, starting at first
4284 * into existentially quantified variables.
4286 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4287 enum isl_dim_type type, unsigned first, unsigned n)
4289 int i;
4291 if (n == 0)
4292 return map_space_reset(map, type);
4294 if (isl_map_check_range(map, type, first, n) < 0)
4295 return isl_map_free(map);
4297 map = isl_map_cow(map);
4298 if (!map)
4299 return NULL;
4301 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4302 if (!map->dim)
4303 goto error;
4305 for (i = 0; i < map->n; ++i) {
4306 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4307 if (!map->p[i])
4308 goto error;
4311 return map;
4312 error:
4313 isl_map_free(map);
4314 return NULL;
4317 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4318 * into existentially quantified variables.
4320 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4321 enum isl_dim_type type, unsigned first, unsigned n)
4323 unsigned dim;
4325 if (isl_map_check_range(map, type, first, n) < 0)
4326 return isl_map_free(map);
4327 dim = isl_map_dim(map, type);
4328 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4329 map = isl_map_project_out(map, type, 0, first);
4330 return map;
4333 /* Turn the n dimensions of type type, starting at first
4334 * into existentially quantified variables.
4336 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4337 enum isl_dim_type type, unsigned first, unsigned n)
4339 return set_from_map(isl_map_project_out(set_to_map(set),
4340 type, first, n));
4343 /* Return a map that projects the elements in "set" onto their
4344 * "n" set dimensions starting at "first".
4345 * "type" should be equal to isl_dim_set.
4347 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4348 enum isl_dim_type type, unsigned first, unsigned n)
4350 int i;
4351 isl_map *map;
4353 if (type != isl_dim_set)
4354 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4355 "only set dimensions can be projected out", goto error);
4356 if (isl_set_check_range(set, type, first, n) < 0)
4357 return isl_set_free(set);
4359 map = isl_map_from_domain(set);
4360 map = isl_map_add_dims(map, isl_dim_out, n);
4361 for (i = 0; i < n; ++i)
4362 map = isl_map_equate(map, isl_dim_in, first + i,
4363 isl_dim_out, i);
4364 return map;
4365 error:
4366 isl_set_free(set);
4367 return NULL;
4370 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4371 unsigned n)
4373 int i, j;
4375 for (i = 0; i < n; ++i) {
4376 j = isl_basic_map_alloc_div(bmap);
4377 if (j < 0)
4378 goto error;
4379 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4381 return bmap;
4382 error:
4383 isl_basic_map_free(bmap);
4384 return NULL;
4387 struct isl_basic_map *isl_basic_map_apply_range(
4388 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4390 isl_space *space_result = NULL;
4391 struct isl_basic_map *bmap;
4392 unsigned n_in, n_out, n, nparam, total, pos;
4393 struct isl_dim_map *dim_map1, *dim_map2;
4395 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4396 goto error;
4397 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4398 bmap2->dim, isl_dim_in))
4399 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4400 "spaces don't match", goto error);
4402 space_result = isl_space_join(isl_basic_map_get_space(bmap1),
4403 isl_basic_map_get_space(bmap2));
4405 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4406 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4407 n = isl_basic_map_dim(bmap1, isl_dim_out);
4408 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4410 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4411 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4412 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4413 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4414 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4415 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4416 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4417 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4418 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4419 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4420 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4422 bmap = isl_basic_map_alloc_space(space_result,
4423 bmap1->n_div + bmap2->n_div + n,
4424 bmap1->n_eq + bmap2->n_eq,
4425 bmap1->n_ineq + bmap2->n_ineq);
4426 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4427 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4428 bmap = add_divs(bmap, n);
4429 bmap = isl_basic_map_simplify(bmap);
4430 bmap = isl_basic_map_drop_redundant_divs(bmap);
4431 return isl_basic_map_finalize(bmap);
4432 error:
4433 isl_basic_map_free(bmap1);
4434 isl_basic_map_free(bmap2);
4435 return NULL;
4438 struct isl_basic_set *isl_basic_set_apply(
4439 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4441 if (!bset || !bmap)
4442 goto error;
4444 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4445 goto error);
4447 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4448 bmap));
4449 error:
4450 isl_basic_set_free(bset);
4451 isl_basic_map_free(bmap);
4452 return NULL;
4455 struct isl_basic_map *isl_basic_map_apply_domain(
4456 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4458 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4459 goto error;
4460 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4461 bmap2->dim, isl_dim_in))
4462 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4463 "spaces don't match", goto error);
4465 bmap1 = isl_basic_map_reverse(bmap1);
4466 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4467 return isl_basic_map_reverse(bmap1);
4468 error:
4469 isl_basic_map_free(bmap1);
4470 isl_basic_map_free(bmap2);
4471 return NULL;
4474 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4475 * A \cap B -> f(A) + f(B)
4477 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4478 __isl_take isl_basic_map *bmap2)
4480 unsigned n_in, n_out, nparam, total, pos;
4481 struct isl_basic_map *bmap = NULL;
4482 struct isl_dim_map *dim_map1, *dim_map2;
4483 int i;
4485 if (!bmap1 || !bmap2)
4486 goto error;
4488 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4489 goto error);
4491 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4492 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4493 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4495 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4496 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4497 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4498 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4499 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4500 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4501 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4502 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4503 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4504 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4505 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4507 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4508 bmap1->n_div + bmap2->n_div + 2 * n_out,
4509 bmap1->n_eq + bmap2->n_eq + n_out,
4510 bmap1->n_ineq + bmap2->n_ineq);
4511 for (i = 0; i < n_out; ++i) {
4512 int j = isl_basic_map_alloc_equality(bmap);
4513 if (j < 0)
4514 goto error;
4515 isl_seq_clr(bmap->eq[j], 1+total);
4516 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4517 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4518 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4520 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4521 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4522 bmap = add_divs(bmap, 2 * n_out);
4524 bmap = isl_basic_map_simplify(bmap);
4525 return isl_basic_map_finalize(bmap);
4526 error:
4527 isl_basic_map_free(bmap);
4528 isl_basic_map_free(bmap1);
4529 isl_basic_map_free(bmap2);
4530 return NULL;
4533 /* Given two maps A -> f(A) and B -> g(B), construct a map
4534 * A \cap B -> f(A) + f(B)
4536 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4537 __isl_take isl_map *map2)
4539 struct isl_map *result;
4540 int i, j;
4542 if (!map1 || !map2)
4543 goto error;
4545 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4547 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4548 map1->n * map2->n, 0);
4549 if (!result)
4550 goto error;
4551 for (i = 0; i < map1->n; ++i)
4552 for (j = 0; j < map2->n; ++j) {
4553 struct isl_basic_map *part;
4554 part = isl_basic_map_sum(
4555 isl_basic_map_copy(map1->p[i]),
4556 isl_basic_map_copy(map2->p[j]));
4557 if (isl_basic_map_is_empty(part))
4558 isl_basic_map_free(part);
4559 else
4560 result = isl_map_add_basic_map(result, part);
4561 if (!result)
4562 goto error;
4564 isl_map_free(map1);
4565 isl_map_free(map2);
4566 return result;
4567 error:
4568 isl_map_free(map1);
4569 isl_map_free(map2);
4570 return NULL;
4573 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4574 __isl_take isl_set *set2)
4576 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4579 /* Given a basic map A -> f(A), construct A -> -f(A).
4581 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4583 int i, j;
4584 unsigned off, n;
4586 bmap = isl_basic_map_cow(bmap);
4587 if (!bmap)
4588 return NULL;
4590 n = isl_basic_map_dim(bmap, isl_dim_out);
4591 off = isl_basic_map_offset(bmap, isl_dim_out);
4592 for (i = 0; i < bmap->n_eq; ++i)
4593 for (j = 0; j < n; ++j)
4594 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4595 for (i = 0; i < bmap->n_ineq; ++i)
4596 for (j = 0; j < n; ++j)
4597 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4598 for (i = 0; i < bmap->n_div; ++i)
4599 for (j = 0; j < n; ++j)
4600 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4601 bmap = isl_basic_map_gauss(bmap, NULL);
4602 return isl_basic_map_finalize(bmap);
4605 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4607 return isl_basic_map_neg(bset);
4610 /* Given a map A -> f(A), construct A -> -f(A).
4612 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4614 int i;
4616 map = isl_map_cow(map);
4617 if (!map)
4618 return NULL;
4620 for (i = 0; i < map->n; ++i) {
4621 map->p[i] = isl_basic_map_neg(map->p[i]);
4622 if (!map->p[i])
4623 goto error;
4626 return map;
4627 error:
4628 isl_map_free(map);
4629 return NULL;
4632 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4634 return set_from_map(isl_map_neg(set_to_map(set)));
4637 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4638 * A -> floor(f(A)/d).
4640 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4641 isl_int d)
4643 unsigned n_in, n_out, nparam, total, pos;
4644 struct isl_basic_map *result = NULL;
4645 struct isl_dim_map *dim_map;
4646 int i;
4648 if (!bmap)
4649 return NULL;
4651 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4652 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4653 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4655 total = nparam + n_in + n_out + bmap->n_div + n_out;
4656 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4657 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4658 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4659 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4660 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4662 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4663 bmap->n_div + n_out,
4664 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4665 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4666 result = add_divs(result, n_out);
4667 for (i = 0; i < n_out; ++i) {
4668 int j;
4669 j = isl_basic_map_alloc_inequality(result);
4670 if (j < 0)
4671 goto error;
4672 isl_seq_clr(result->ineq[j], 1+total);
4673 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4674 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4675 j = isl_basic_map_alloc_inequality(result);
4676 if (j < 0)
4677 goto error;
4678 isl_seq_clr(result->ineq[j], 1+total);
4679 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4680 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4681 isl_int_sub_ui(result->ineq[j][0], d, 1);
4684 result = isl_basic_map_simplify(result);
4685 return isl_basic_map_finalize(result);
4686 error:
4687 isl_basic_map_free(result);
4688 return NULL;
4691 /* Given a map A -> f(A) and an integer d, construct a map
4692 * A -> floor(f(A)/d).
4694 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4696 int i;
4698 map = isl_map_cow(map);
4699 if (!map)
4700 return NULL;
4702 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4703 for (i = 0; i < map->n; ++i) {
4704 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4705 if (!map->p[i])
4706 goto error;
4708 map = isl_map_unmark_normalized(map);
4710 return map;
4711 error:
4712 isl_map_free(map);
4713 return NULL;
4716 /* Given a map A -> f(A) and an integer d, construct a map
4717 * A -> floor(f(A)/d).
4719 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4720 __isl_take isl_val *d)
4722 if (!map || !d)
4723 goto error;
4724 if (!isl_val_is_int(d))
4725 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4726 "expecting integer denominator", goto error);
4727 map = isl_map_floordiv(map, d->n);
4728 isl_val_free(d);
4729 return map;
4730 error:
4731 isl_map_free(map);
4732 isl_val_free(d);
4733 return NULL;
4736 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4737 unsigned pos)
4739 int i;
4740 unsigned nparam;
4741 unsigned n_in;
4743 i = isl_basic_map_alloc_equality(bmap);
4744 if (i < 0)
4745 goto error;
4746 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4747 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4748 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4749 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4750 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4751 return isl_basic_map_finalize(bmap);
4752 error:
4753 isl_basic_map_free(bmap);
4754 return NULL;
4757 /* Add a constraint to "bmap" expressing i_pos < o_pos
4759 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4760 unsigned pos)
4762 int i;
4763 unsigned nparam;
4764 unsigned n_in;
4766 i = isl_basic_map_alloc_inequality(bmap);
4767 if (i < 0)
4768 goto error;
4769 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4770 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4771 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4772 isl_int_set_si(bmap->ineq[i][0], -1);
4773 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4774 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4775 return isl_basic_map_finalize(bmap);
4776 error:
4777 isl_basic_map_free(bmap);
4778 return NULL;
4781 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4783 static __isl_give isl_basic_map *var_less_or_equal(
4784 __isl_take isl_basic_map *bmap, unsigned pos)
4786 int i;
4787 unsigned nparam;
4788 unsigned n_in;
4790 i = isl_basic_map_alloc_inequality(bmap);
4791 if (i < 0)
4792 goto error;
4793 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4794 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4795 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4796 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4797 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4798 return isl_basic_map_finalize(bmap);
4799 error:
4800 isl_basic_map_free(bmap);
4801 return NULL;
4804 /* Add a constraint to "bmap" expressing i_pos > o_pos
4806 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4807 unsigned pos)
4809 int i;
4810 unsigned nparam;
4811 unsigned n_in;
4813 i = isl_basic_map_alloc_inequality(bmap);
4814 if (i < 0)
4815 goto error;
4816 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4817 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4818 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4819 isl_int_set_si(bmap->ineq[i][0], -1);
4820 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4821 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4822 return isl_basic_map_finalize(bmap);
4823 error:
4824 isl_basic_map_free(bmap);
4825 return NULL;
4828 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4830 static __isl_give isl_basic_map *var_more_or_equal(
4831 __isl_take isl_basic_map *bmap, unsigned pos)
4833 int i;
4834 unsigned nparam;
4835 unsigned n_in;
4837 i = isl_basic_map_alloc_inequality(bmap);
4838 if (i < 0)
4839 goto error;
4840 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4841 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4842 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4843 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4844 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4845 return isl_basic_map_finalize(bmap);
4846 error:
4847 isl_basic_map_free(bmap);
4848 return NULL;
4851 __isl_give isl_basic_map *isl_basic_map_equal(
4852 __isl_take isl_space *space, unsigned n_equal)
4854 int i;
4855 struct isl_basic_map *bmap;
4856 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4857 if (!bmap)
4858 return NULL;
4859 for (i = 0; i < n_equal && bmap; ++i)
4860 bmap = var_equal(bmap, i);
4861 return isl_basic_map_finalize(bmap);
4864 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4866 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4867 unsigned pos)
4869 int i;
4870 struct isl_basic_map *bmap;
4871 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4872 if (!bmap)
4873 return NULL;
4874 for (i = 0; i < pos && bmap; ++i)
4875 bmap = var_equal(bmap, i);
4876 if (bmap)
4877 bmap = var_less(bmap, pos);
4878 return isl_basic_map_finalize(bmap);
4881 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4883 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4884 __isl_take isl_space *space, unsigned pos)
4886 int i;
4887 isl_basic_map *bmap;
4889 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4890 for (i = 0; i < pos; ++i)
4891 bmap = var_equal(bmap, i);
4892 bmap = var_less_or_equal(bmap, pos);
4893 return isl_basic_map_finalize(bmap);
4896 /* Return a relation on "space" expressing i_pos > o_pos
4898 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4899 unsigned pos)
4901 int i;
4902 struct isl_basic_map *bmap;
4903 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4904 if (!bmap)
4905 return NULL;
4906 for (i = 0; i < pos && bmap; ++i)
4907 bmap = var_equal(bmap, i);
4908 if (bmap)
4909 bmap = var_more(bmap, pos);
4910 return isl_basic_map_finalize(bmap);
4913 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4915 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4916 __isl_take isl_space *space, unsigned pos)
4918 int i;
4919 isl_basic_map *bmap;
4921 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4922 for (i = 0; i < pos; ++i)
4923 bmap = var_equal(bmap, i);
4924 bmap = var_more_or_equal(bmap, pos);
4925 return isl_basic_map_finalize(bmap);
4928 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4929 unsigned n, int equal)
4931 struct isl_map *map;
4932 int i;
4934 if (n == 0 && equal)
4935 return isl_map_universe(space);
4937 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4939 for (i = 0; i + 1 < n; ++i)
4940 map = isl_map_add_basic_map(map,
4941 isl_basic_map_less_at(isl_space_copy(space), i));
4942 if (n > 0) {
4943 if (equal)
4944 map = isl_map_add_basic_map(map,
4945 isl_basic_map_less_or_equal_at(space, n - 1));
4946 else
4947 map = isl_map_add_basic_map(map,
4948 isl_basic_map_less_at(space, n - 1));
4949 } else
4950 isl_space_free(space);
4952 return map;
4955 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
4957 if (!space)
4958 return NULL;
4959 return map_lex_lte_first(space, space->n_out, equal);
4962 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4964 return map_lex_lte_first(dim, n, 0);
4967 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4969 return map_lex_lte_first(dim, n, 1);
4972 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4974 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4977 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4979 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4982 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
4983 unsigned n, int equal)
4985 struct isl_map *map;
4986 int i;
4988 if (n == 0 && equal)
4989 return isl_map_universe(space);
4991 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4993 for (i = 0; i + 1 < n; ++i)
4994 map = isl_map_add_basic_map(map,
4995 isl_basic_map_more_at(isl_space_copy(space), i));
4996 if (n > 0) {
4997 if (equal)
4998 map = isl_map_add_basic_map(map,
4999 isl_basic_map_more_or_equal_at(space, n - 1));
5000 else
5001 map = isl_map_add_basic_map(map,
5002 isl_basic_map_more_at(space, n - 1));
5003 } else
5004 isl_space_free(space);
5006 return map;
5009 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5011 if (!space)
5012 return NULL;
5013 return map_lex_gte_first(space, space->n_out, equal);
5016 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5018 return map_lex_gte_first(dim, n, 0);
5021 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5023 return map_lex_gte_first(dim, n, 1);
5026 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5028 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5031 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5033 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5036 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5037 __isl_take isl_set *set2)
5039 isl_map *map;
5040 map = isl_map_lex_le(isl_set_get_space(set1));
5041 map = isl_map_intersect_domain(map, set1);
5042 map = isl_map_intersect_range(map, set2);
5043 return map;
5046 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5047 __isl_take isl_set *set2)
5049 isl_map *map;
5050 map = isl_map_lex_lt(isl_set_get_space(set1));
5051 map = isl_map_intersect_domain(map, set1);
5052 map = isl_map_intersect_range(map, set2);
5053 return map;
5056 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5057 __isl_take isl_set *set2)
5059 isl_map *map;
5060 map = isl_map_lex_ge(isl_set_get_space(set1));
5061 map = isl_map_intersect_domain(map, set1);
5062 map = isl_map_intersect_range(map, set2);
5063 return map;
5066 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5067 __isl_take isl_set *set2)
5069 isl_map *map;
5070 map = isl_map_lex_gt(isl_set_get_space(set1));
5071 map = isl_map_intersect_domain(map, set1);
5072 map = isl_map_intersect_range(map, set2);
5073 return map;
5076 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5077 __isl_take isl_map *map2)
5079 isl_map *map;
5080 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5081 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5082 map = isl_map_apply_range(map, isl_map_reverse(map2));
5083 return map;
5086 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5087 __isl_take isl_map *map2)
5089 isl_map *map;
5090 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5091 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5092 map = isl_map_apply_range(map, isl_map_reverse(map2));
5093 return map;
5096 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5097 __isl_take isl_map *map2)
5099 isl_map *map;
5100 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5101 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5102 map = isl_map_apply_range(map, isl_map_reverse(map2));
5103 return map;
5106 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5107 __isl_take isl_map *map2)
5109 isl_map *map;
5110 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5111 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5112 map = isl_map_apply_range(map, isl_map_reverse(map2));
5113 return map;
5116 /* For the div d = floor(f/m) at position "div", add the constraint
5118 * f - m d >= 0
5120 static __isl_give isl_basic_map *add_upper_div_constraint(
5121 __isl_take isl_basic_map *bmap, unsigned div)
5123 int i;
5124 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5125 unsigned n_div, pos;
5127 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5128 if (v_div < 0)
5129 return isl_basic_map_free(bmap);
5130 pos = v_div + div;
5131 i = isl_basic_map_alloc_inequality(bmap);
5132 if (i < 0)
5133 return isl_basic_map_free(bmap);
5134 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5135 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5137 return bmap;
5140 /* For the div d = floor(f/m) at position "div", add the constraint
5142 * -(f-(m-1)) + m d >= 0
5144 static __isl_give isl_basic_map *add_lower_div_constraint(
5145 __isl_take isl_basic_map *bmap, unsigned div)
5147 int i;
5148 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5149 unsigned n_div, pos;
5151 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5152 if (v_div < 0)
5153 return isl_basic_map_free(bmap);
5154 pos = v_div + div;
5155 i = isl_basic_map_alloc_inequality(bmap);
5156 if (i < 0)
5157 return isl_basic_map_free(bmap);
5158 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5159 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5160 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5161 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5163 return bmap;
5166 /* For the div d = floor(f/m) at position "pos", add the constraints
5168 * f - m d >= 0
5169 * -(f-(m-1)) + m d >= 0
5171 * Note that the second constraint is the negation of
5173 * f - m d >= m
5175 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5176 __isl_take isl_basic_map *bmap, unsigned pos)
5178 bmap = add_upper_div_constraint(bmap, pos);
5179 bmap = add_lower_div_constraint(bmap, pos);
5180 return bmap;
5183 /* For each known div d = floor(f/m), add the constraints
5185 * f - m d >= 0
5186 * -(f-(m-1)) + m d >= 0
5188 * Remove duplicate constraints in case of some these div constraints
5189 * already appear in "bmap".
5191 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5192 __isl_take isl_basic_map *bmap)
5194 unsigned n_div;
5196 if (!bmap)
5197 return NULL;
5198 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5199 if (n_div == 0)
5200 return bmap;
5202 bmap = add_known_div_constraints(bmap);
5203 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5204 bmap = isl_basic_map_finalize(bmap);
5205 return bmap;
5208 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5210 * In particular, if this div is of the form d = floor(f/m),
5211 * then add the constraint
5213 * f - m d >= 0
5215 * if sign < 0 or the constraint
5217 * -(f-(m-1)) + m d >= 0
5219 * if sign > 0.
5221 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5222 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5224 if (sign < 0)
5225 return add_upper_div_constraint(bmap, div);
5226 else
5227 return add_lower_div_constraint(bmap, div);
5230 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5231 __isl_take isl_basic_map *bmap)
5233 if (!bmap)
5234 goto error;
5235 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5236 bmap->n_div == 0 &&
5237 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5238 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5239 return bset_from_bmap(bmap);
5240 bmap = isl_basic_map_cow(bmap);
5241 if (!bmap)
5242 goto error;
5243 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5244 if (!bmap->dim)
5245 goto error;
5246 bmap->extra -= bmap->n_div;
5247 bmap->n_div = 0;
5248 bmap = isl_basic_map_finalize(bmap);
5249 return bset_from_bmap(bmap);
5250 error:
5251 isl_basic_map_free(bmap);
5252 return NULL;
5255 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5256 __isl_take isl_basic_set *bset)
5258 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5261 /* Replace each element in "list" by the result of applying
5262 * isl_basic_map_underlying_set to the element.
5264 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5265 __isl_take isl_basic_map_list *list)
5267 int i, n;
5269 if (!list)
5270 return NULL;
5272 n = isl_basic_map_list_n_basic_map(list);
5273 for (i = 0; i < n; ++i) {
5274 isl_basic_map *bmap;
5275 isl_basic_set *bset;
5277 bmap = isl_basic_map_list_get_basic_map(list, i);
5278 bset = isl_basic_set_underlying_set(bmap);
5279 list = isl_basic_set_list_set_basic_set(list, i, bset);
5282 return list;
5285 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5286 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5288 struct isl_basic_map *bmap;
5289 struct isl_ctx *ctx;
5290 unsigned dim, total;
5291 int i;
5293 if (!bset || !like)
5294 goto error;
5295 ctx = bset->ctx;
5296 if (isl_basic_set_check_no_params(bset) < 0 ||
5297 isl_basic_set_check_no_locals(bset) < 0)
5298 goto error;
5299 dim = isl_basic_set_dim(bset, isl_dim_set);
5300 isl_assert(ctx, dim == isl_basic_map_total_dim(like), goto error);
5301 if (like->n_div == 0) {
5302 isl_space *space = isl_basic_map_get_space(like);
5303 isl_basic_map_free(like);
5304 return isl_basic_map_reset_space(bset, space);
5306 bset = isl_basic_set_cow(bset);
5307 if (!bset)
5308 goto error;
5309 total = dim + bset->extra;
5310 bmap = bset_to_bmap(bset);
5311 isl_space_free(bmap->dim);
5312 bmap->dim = isl_space_copy(like->dim);
5313 if (!bmap->dim)
5314 goto error;
5315 bmap->n_div = like->n_div;
5316 bmap->extra += like->n_div;
5317 if (bmap->extra) {
5318 unsigned ltotal;
5319 isl_int **div;
5320 ltotal = total - bmap->extra + like->extra;
5321 if (ltotal > total)
5322 ltotal = total;
5323 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5324 bmap->extra * (1 + 1 + total));
5325 if (isl_blk_is_error(bmap->block2))
5326 goto error;
5327 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5328 if (!div)
5329 goto error;
5330 bmap->div = div;
5331 for (i = 0; i < bmap->extra; ++i)
5332 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5333 for (i = 0; i < like->n_div; ++i) {
5334 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5335 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5337 bmap = isl_basic_map_add_known_div_constraints(bmap);
5339 isl_basic_map_free(like);
5340 bmap = isl_basic_map_simplify(bmap);
5341 bmap = isl_basic_map_finalize(bmap);
5342 return bmap;
5343 error:
5344 isl_basic_map_free(like);
5345 isl_basic_set_free(bset);
5346 return NULL;
5349 struct isl_basic_set *isl_basic_set_from_underlying_set(
5350 struct isl_basic_set *bset, struct isl_basic_set *like)
5352 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5353 bset_to_bmap(like)));
5356 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5358 int i;
5360 map = isl_map_cow(map);
5361 if (!map)
5362 return NULL;
5363 map->dim = isl_space_cow(map->dim);
5364 if (!map->dim)
5365 goto error;
5367 for (i = 1; i < map->n; ++i)
5368 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5369 goto error);
5370 for (i = 0; i < map->n; ++i) {
5371 map->p[i] = bset_to_bmap(
5372 isl_basic_map_underlying_set(map->p[i]));
5373 if (!map->p[i])
5374 goto error;
5376 if (map->n == 0)
5377 map->dim = isl_space_underlying(map->dim, 0);
5378 else {
5379 isl_space_free(map->dim);
5380 map->dim = isl_space_copy(map->p[0]->dim);
5382 if (!map->dim)
5383 goto error;
5384 return set_from_map(map);
5385 error:
5386 isl_map_free(map);
5387 return NULL;
5390 /* Replace the space of "bmap" by "space".
5392 * If the space of "bmap" is identical to "space" (including the identifiers
5393 * of the input and output dimensions), then simply return the original input.
5395 __isl_give isl_basic_map *isl_basic_map_reset_space(
5396 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5398 isl_bool equal;
5399 isl_space *bmap_space;
5401 bmap_space = isl_basic_map_peek_space(bmap);
5402 equal = isl_space_is_equal(bmap_space, space);
5403 if (equal >= 0 && equal)
5404 equal = isl_space_has_equal_ids(bmap_space, space);
5405 if (equal < 0)
5406 goto error;
5407 if (equal) {
5408 isl_space_free(space);
5409 return bmap;
5411 bmap = isl_basic_map_cow(bmap);
5412 if (!bmap || !space)
5413 goto error;
5415 isl_space_free(bmap->dim);
5416 bmap->dim = space;
5418 bmap = isl_basic_map_finalize(bmap);
5420 return bmap;
5421 error:
5422 isl_basic_map_free(bmap);
5423 isl_space_free(space);
5424 return NULL;
5427 __isl_give isl_basic_set *isl_basic_set_reset_space(
5428 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5430 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5431 dim));
5434 /* Check that the total dimensions of "map" and "space" are the same.
5436 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5437 __isl_keep isl_space *space)
5439 unsigned dim1, dim2;
5441 if (!map || !space)
5442 return isl_stat_error;
5443 dim1 = isl_map_dim(map, isl_dim_all);
5444 dim2 = isl_space_dim(space, isl_dim_all);
5445 if (dim1 == dim2)
5446 return isl_stat_ok;
5447 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5448 "total dimensions do not match", return isl_stat_error);
5451 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5452 __isl_take isl_space *space)
5454 int i;
5456 map = isl_map_cow(map);
5457 if (!map || !space)
5458 goto error;
5460 for (i = 0; i < map->n; ++i) {
5461 map->p[i] = isl_basic_map_reset_space(map->p[i],
5462 isl_space_copy(space));
5463 if (!map->p[i])
5464 goto error;
5466 isl_space_free(map->dim);
5467 map->dim = space;
5469 return map;
5470 error:
5471 isl_map_free(map);
5472 isl_space_free(space);
5473 return NULL;
5476 /* Replace the space of "map" by "space", without modifying
5477 * the dimension of "map".
5479 * If the space of "map" is identical to "space" (including the identifiers
5480 * of the input and output dimensions), then simply return the original input.
5482 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5483 __isl_take isl_space *space)
5485 isl_bool equal;
5486 isl_space *map_space;
5488 map_space = isl_map_peek_space(map);
5489 equal = isl_space_is_equal(map_space, space);
5490 if (equal >= 0 && equal)
5491 equal = isl_space_has_equal_ids(map_space, space);
5492 if (equal < 0)
5493 goto error;
5494 if (equal) {
5495 isl_space_free(space);
5496 return map;
5498 if (check_map_space_equal_total_dim(map, space) < 0)
5499 goto error;
5500 return isl_map_reset_space(map, space);
5501 error:
5502 isl_map_free(map);
5503 isl_space_free(space);
5504 return NULL;
5507 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5508 __isl_take isl_space *dim)
5510 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5513 /* Compute the parameter domain of the given basic set.
5515 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5517 isl_bool is_params;
5518 isl_space *space;
5519 unsigned n;
5521 is_params = isl_basic_set_is_params(bset);
5522 if (is_params < 0)
5523 return isl_basic_set_free(bset);
5524 if (is_params)
5525 return bset;
5527 n = isl_basic_set_dim(bset, isl_dim_set);
5528 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5529 space = isl_basic_set_get_space(bset);
5530 space = isl_space_params(space);
5531 bset = isl_basic_set_reset_space(bset, space);
5532 return bset;
5535 /* Construct a zero-dimensional basic set with the given parameter domain.
5537 __isl_give isl_basic_set *isl_basic_set_from_params(
5538 __isl_take isl_basic_set *bset)
5540 isl_space *space;
5541 space = isl_basic_set_get_space(bset);
5542 space = isl_space_set_from_params(space);
5543 bset = isl_basic_set_reset_space(bset, space);
5544 return bset;
5547 /* Compute the parameter domain of the given set.
5549 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5551 isl_space *space;
5552 unsigned n;
5554 if (isl_set_is_params(set))
5555 return set;
5557 n = isl_set_dim(set, isl_dim_set);
5558 set = isl_set_project_out(set, isl_dim_set, 0, n);
5559 space = isl_set_get_space(set);
5560 space = isl_space_params(space);
5561 set = isl_set_reset_space(set, space);
5562 return set;
5565 /* Construct a zero-dimensional set with the given parameter domain.
5567 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5569 isl_space *space;
5570 space = isl_set_get_space(set);
5571 space = isl_space_set_from_params(space);
5572 set = isl_set_reset_space(set, space);
5573 return set;
5576 /* Compute the parameter domain of the given map.
5578 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5580 isl_space *space;
5581 unsigned n;
5583 n = isl_map_dim(map, isl_dim_in);
5584 map = isl_map_project_out(map, isl_dim_in, 0, n);
5585 n = isl_map_dim(map, isl_dim_out);
5586 map = isl_map_project_out(map, isl_dim_out, 0, n);
5587 space = isl_map_get_space(map);
5588 space = isl_space_params(space);
5589 map = isl_map_reset_space(map, space);
5590 return map;
5593 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5595 isl_space *space;
5596 unsigned n_out;
5598 if (!bmap)
5599 return NULL;
5600 space = isl_space_domain(isl_basic_map_get_space(bmap));
5602 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5603 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5605 return isl_basic_map_reset_space(bmap, space);
5608 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5610 if (!bmap)
5611 return isl_bool_error;
5612 return isl_space_may_be_set(bmap->dim);
5615 /* Is this basic map actually a set?
5616 * Users should never call this function. Outside of isl,
5617 * the type should indicate whether something is a set or a map.
5619 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5621 if (!bmap)
5622 return isl_bool_error;
5623 return isl_space_is_set(bmap->dim);
5626 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5628 isl_bool is_set;
5630 is_set = isl_basic_map_is_set(bmap);
5631 if (is_set < 0)
5632 goto error;
5633 if (is_set)
5634 return bmap;
5635 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5636 error:
5637 isl_basic_map_free(bmap);
5638 return NULL;
5641 __isl_give isl_basic_map *isl_basic_map_domain_map(
5642 __isl_take isl_basic_map *bmap)
5644 int i;
5645 isl_space *space;
5646 isl_basic_map *domain;
5647 int nparam, n_in, n_out;
5649 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5650 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5651 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5653 space = isl_basic_map_get_space(bmap);
5654 space = isl_space_from_range(isl_space_domain(space));
5655 domain = isl_basic_map_universe(space);
5657 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5658 bmap = isl_basic_map_apply_range(bmap, domain);
5659 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5661 for (i = 0; i < n_in; ++i)
5662 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5663 isl_dim_out, i);
5665 bmap = isl_basic_map_gauss(bmap, NULL);
5666 return isl_basic_map_finalize(bmap);
5669 __isl_give isl_basic_map *isl_basic_map_range_map(
5670 __isl_take isl_basic_map *bmap)
5672 int i;
5673 isl_space *space;
5674 isl_basic_map *range;
5675 int nparam, n_in, n_out;
5677 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5678 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5679 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5681 space = isl_basic_map_get_space(bmap);
5682 space = isl_space_from_range(isl_space_range(space));
5683 range = isl_basic_map_universe(space);
5685 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5686 bmap = isl_basic_map_apply_range(bmap, range);
5687 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5689 for (i = 0; i < n_out; ++i)
5690 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5691 isl_dim_out, i);
5693 bmap = isl_basic_map_gauss(bmap, NULL);
5694 return isl_basic_map_finalize(bmap);
5697 int isl_map_may_be_set(__isl_keep isl_map *map)
5699 if (!map)
5700 return -1;
5701 return isl_space_may_be_set(map->dim);
5704 /* Is this map actually a set?
5705 * Users should never call this function. Outside of isl,
5706 * the type should indicate whether something is a set or a map.
5708 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5710 if (!map)
5711 return isl_bool_error;
5712 return isl_space_is_set(map->dim);
5715 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5717 int i;
5718 isl_bool is_set;
5719 struct isl_set *set;
5721 is_set = isl_map_is_set(map);
5722 if (is_set < 0)
5723 goto error;
5724 if (is_set)
5725 return set_from_map(map);
5727 map = isl_map_cow(map);
5728 if (!map)
5729 goto error;
5731 set = set_from_map(map);
5732 set->dim = isl_space_range(set->dim);
5733 if (!set->dim)
5734 goto error;
5735 for (i = 0; i < map->n; ++i) {
5736 set->p[i] = isl_basic_map_range(map->p[i]);
5737 if (!set->p[i])
5738 goto error;
5740 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5741 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5742 return set;
5743 error:
5744 isl_map_free(map);
5745 return NULL;
5748 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5750 int i;
5752 map = isl_map_cow(map);
5753 if (!map)
5754 return NULL;
5756 map->dim = isl_space_domain_map(map->dim);
5757 if (!map->dim)
5758 goto error;
5759 for (i = 0; i < map->n; ++i) {
5760 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5761 if (!map->p[i])
5762 goto error;
5764 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5765 map = isl_map_unmark_normalized(map);
5766 return map;
5767 error:
5768 isl_map_free(map);
5769 return NULL;
5772 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5774 int i;
5775 isl_space *range_dim;
5777 map = isl_map_cow(map);
5778 if (!map)
5779 return NULL;
5781 range_dim = isl_space_range(isl_map_get_space(map));
5782 range_dim = isl_space_from_range(range_dim);
5783 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5784 map->dim = isl_space_join(map->dim, range_dim);
5785 if (!map->dim)
5786 goto error;
5787 for (i = 0; i < map->n; ++i) {
5788 map->p[i] = isl_basic_map_range_map(map->p[i]);
5789 if (!map->p[i])
5790 goto error;
5792 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5793 map = isl_map_unmark_normalized(map);
5794 return map;
5795 error:
5796 isl_map_free(map);
5797 return NULL;
5800 /* Given a wrapped map of the form A[B -> C],
5801 * return the map A[B -> C] -> B.
5803 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5805 isl_id *id;
5806 isl_map *map;
5808 if (!set)
5809 return NULL;
5810 if (!isl_set_has_tuple_id(set))
5811 return isl_map_domain_map(isl_set_unwrap(set));
5813 id = isl_set_get_tuple_id(set);
5814 map = isl_map_domain_map(isl_set_unwrap(set));
5815 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5817 return map;
5820 __isl_give isl_basic_map *isl_basic_map_from_domain(
5821 __isl_take isl_basic_set *bset)
5823 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5826 __isl_give isl_basic_map *isl_basic_map_from_range(
5827 __isl_take isl_basic_set *bset)
5829 isl_space *space;
5830 space = isl_basic_set_get_space(bset);
5831 space = isl_space_from_range(space);
5832 bset = isl_basic_set_reset_space(bset, space);
5833 return bset_to_bmap(bset);
5836 /* Create a relation with the given set as range.
5837 * The domain of the created relation is a zero-dimensional
5838 * flat anonymous space.
5840 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5842 isl_space *space;
5843 space = isl_set_get_space(set);
5844 space = isl_space_from_range(space);
5845 set = isl_set_reset_space(set, space);
5846 return set_to_map(set);
5849 /* Create a relation with the given set as domain.
5850 * The range of the created relation is a zero-dimensional
5851 * flat anonymous space.
5853 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5855 return isl_map_reverse(isl_map_from_range(set));
5858 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5859 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5861 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5864 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5865 __isl_take isl_set *range)
5867 return isl_map_apply_range(isl_map_reverse(domain), range);
5870 /* Return a newly allocated isl_map with given space and flags and
5871 * room for "n" basic maps.
5872 * Make sure that all cached information is cleared.
5874 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5875 unsigned flags)
5877 struct isl_map *map;
5879 if (!space)
5880 return NULL;
5881 if (n < 0)
5882 isl_die(space->ctx, isl_error_internal,
5883 "negative number of basic maps", goto error);
5884 map = isl_calloc(space->ctx, struct isl_map,
5885 sizeof(struct isl_map) +
5886 (n - 1) * sizeof(struct isl_basic_map *));
5887 if (!map)
5888 goto error;
5890 map->ctx = space->ctx;
5891 isl_ctx_ref(map->ctx);
5892 map->ref = 1;
5893 map->size = n;
5894 map->n = 0;
5895 map->dim = space;
5896 map->flags = flags;
5897 return map;
5898 error:
5899 isl_space_free(space);
5900 return NULL;
5903 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5905 struct isl_basic_map *bmap;
5906 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5907 bmap = isl_basic_map_set_to_empty(bmap);
5908 return bmap;
5911 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5913 struct isl_basic_set *bset;
5914 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5915 bset = isl_basic_set_set_to_empty(bset);
5916 return bset;
5919 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5921 struct isl_basic_map *bmap;
5922 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5923 bmap = isl_basic_map_finalize(bmap);
5924 return bmap;
5927 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5929 struct isl_basic_set *bset;
5930 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5931 bset = isl_basic_set_finalize(bset);
5932 return bset;
5935 __isl_give isl_basic_map *isl_basic_map_nat_universe(
5936 __isl_take isl_space *space)
5938 int i;
5939 unsigned total = isl_space_dim(space, isl_dim_all);
5940 isl_basic_map *bmap;
5942 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
5943 for (i = 0; i < total; ++i) {
5944 int k = isl_basic_map_alloc_inequality(bmap);
5945 if (k < 0)
5946 goto error;
5947 isl_seq_clr(bmap->ineq[k], 1 + total);
5948 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5950 return bmap;
5951 error:
5952 isl_basic_map_free(bmap);
5953 return NULL;
5956 __isl_give isl_basic_set *isl_basic_set_nat_universe(
5957 __isl_take isl_space *space)
5959 return isl_basic_map_nat_universe(space);
5962 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5964 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5967 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5969 return isl_map_nat_universe(dim);
5972 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
5974 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
5977 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
5979 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
5982 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
5984 struct isl_map *map;
5985 if (!space)
5986 return NULL;
5987 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5988 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
5989 return map;
5992 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
5994 struct isl_set *set;
5995 if (!space)
5996 return NULL;
5997 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5998 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
5999 return set;
6002 struct isl_map *isl_map_dup(struct isl_map *map)
6004 int i;
6005 struct isl_map *dup;
6007 if (!map)
6008 return NULL;
6009 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6010 for (i = 0; i < map->n; ++i)
6011 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6012 return dup;
6015 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6016 __isl_take isl_basic_map *bmap)
6018 if (!bmap || !map)
6019 goto error;
6020 if (isl_basic_map_plain_is_empty(bmap)) {
6021 isl_basic_map_free(bmap);
6022 return map;
6024 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6025 isl_assert(map->ctx, map->n < map->size, goto error);
6026 map->p[map->n] = bmap;
6027 map->n++;
6028 map = isl_map_unmark_normalized(map);
6029 return map;
6030 error:
6031 if (map)
6032 isl_map_free(map);
6033 if (bmap)
6034 isl_basic_map_free(bmap);
6035 return NULL;
6038 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6040 int i;
6042 if (!map)
6043 return NULL;
6045 if (--map->ref > 0)
6046 return NULL;
6048 clear_caches(map);
6049 isl_ctx_deref(map->ctx);
6050 for (i = 0; i < map->n; ++i)
6051 isl_basic_map_free(map->p[i]);
6052 isl_space_free(map->dim);
6053 free(map);
6055 return NULL;
6058 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6059 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6061 int j;
6063 bmap = isl_basic_map_cow(bmap);
6064 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6065 j = isl_basic_map_alloc_equality(bmap);
6066 if (j < 0)
6067 goto error;
6068 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6069 isl_int_set_si(bmap->eq[j][pos], -1);
6070 isl_int_set_si(bmap->eq[j][0], value);
6071 bmap = isl_basic_map_simplify(bmap);
6072 return isl_basic_map_finalize(bmap);
6073 error:
6074 isl_basic_map_free(bmap);
6075 return NULL;
6078 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6079 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6081 int j;
6083 bmap = isl_basic_map_cow(bmap);
6084 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6085 j = isl_basic_map_alloc_equality(bmap);
6086 if (j < 0)
6087 goto error;
6088 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6089 isl_int_set_si(bmap->eq[j][pos], -1);
6090 isl_int_set(bmap->eq[j][0], value);
6091 bmap = isl_basic_map_simplify(bmap);
6092 return isl_basic_map_finalize(bmap);
6093 error:
6094 isl_basic_map_free(bmap);
6095 return NULL;
6098 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6099 enum isl_dim_type type, unsigned pos, int value)
6101 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6102 return isl_basic_map_free(bmap);
6103 return isl_basic_map_fix_pos_si(bmap,
6104 isl_basic_map_offset(bmap, type) + pos, value);
6107 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6108 enum isl_dim_type type, unsigned pos, isl_int value)
6110 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6111 return isl_basic_map_free(bmap);
6112 return isl_basic_map_fix_pos(bmap,
6113 isl_basic_map_offset(bmap, type) + pos, value);
6116 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6117 * to be equal to "v".
6119 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6120 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6122 if (!bmap || !v)
6123 goto error;
6124 if (!isl_val_is_int(v))
6125 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6126 "expecting integer value", goto error);
6127 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6128 goto error;
6129 pos += isl_basic_map_offset(bmap, type);
6130 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6131 isl_val_free(v);
6132 return bmap;
6133 error:
6134 isl_basic_map_free(bmap);
6135 isl_val_free(v);
6136 return NULL;
6139 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6140 * to be equal to "v".
6142 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6143 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6145 return isl_basic_map_fix_val(bset, type, pos, v);
6148 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6149 enum isl_dim_type type, unsigned pos, int value)
6151 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6152 type, pos, value));
6155 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6156 enum isl_dim_type type, unsigned pos, isl_int value)
6158 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6159 type, pos, value));
6162 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6163 unsigned input, int value)
6165 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6168 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6169 unsigned dim, int value)
6171 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6172 isl_dim_set, dim, value));
6175 /* Remove the basic map at position "i" from "map" if this basic map
6176 * is (obviously) empty.
6178 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6180 isl_bool empty;
6182 if (!map)
6183 return NULL;
6185 empty = isl_basic_map_plain_is_empty(map->p[i]);
6186 if (empty < 0)
6187 return isl_map_free(map);
6188 if (!empty)
6189 return map;
6191 isl_basic_map_free(map->p[i]);
6192 map->n--;
6193 if (i != map->n) {
6194 map->p[i] = map->p[map->n];
6195 map = isl_map_unmark_normalized(map);
6199 return map;
6202 /* Perform "fn" on each basic map of "map", where we may not be holding
6203 * the only reference to "map".
6204 * In particular, "fn" should be a semantics preserving operation
6205 * that we want to apply to all copies of "map". We therefore need
6206 * to be careful not to modify "map" in a way that breaks "map"
6207 * in case anything goes wrong.
6209 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6210 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6212 struct isl_basic_map *bmap;
6213 int i;
6215 if (!map)
6216 return NULL;
6218 for (i = map->n - 1; i >= 0; --i) {
6219 bmap = isl_basic_map_copy(map->p[i]);
6220 bmap = fn(bmap);
6221 if (!bmap)
6222 goto error;
6223 isl_basic_map_free(map->p[i]);
6224 map->p[i] = bmap;
6225 map = remove_if_empty(map, i);
6226 if (!map)
6227 return NULL;
6230 return map;
6231 error:
6232 isl_map_free(map);
6233 return NULL;
6236 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6237 enum isl_dim_type type, unsigned pos, int value)
6239 int i;
6241 map = isl_map_cow(map);
6242 if (isl_map_check_range(map, type, pos, 1) < 0)
6243 return isl_map_free(map);
6244 for (i = map->n - 1; i >= 0; --i) {
6245 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6246 map = remove_if_empty(map, i);
6247 if (!map)
6248 return NULL;
6250 map = isl_map_unmark_normalized(map);
6251 return map;
6254 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6255 enum isl_dim_type type, unsigned pos, int value)
6257 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6260 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6261 enum isl_dim_type type, unsigned pos, isl_int value)
6263 int i;
6265 map = isl_map_cow(map);
6266 if (isl_map_check_range(map, type, pos, 1) < 0)
6267 return isl_map_free(map);
6268 for (i = 0; i < map->n; ++i) {
6269 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6270 if (!map->p[i])
6271 goto error;
6273 map = isl_map_unmark_normalized(map);
6274 return map;
6275 error:
6276 isl_map_free(map);
6277 return NULL;
6280 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6281 enum isl_dim_type type, unsigned pos, isl_int value)
6283 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6286 /* Fix the value of the variable at position "pos" of type "type" of "map"
6287 * to be equal to "v".
6289 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6290 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6292 int i;
6294 map = isl_map_cow(map);
6295 if (!map || !v)
6296 goto error;
6298 if (!isl_val_is_int(v))
6299 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6300 "expecting integer value", goto error);
6301 if (isl_map_check_range(map, type, pos, 1) < 0)
6302 goto error;
6303 for (i = map->n - 1; i >= 0; --i) {
6304 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6305 isl_val_copy(v));
6306 map = remove_if_empty(map, i);
6307 if (!map)
6308 goto error;
6310 map = isl_map_unmark_normalized(map);
6311 isl_val_free(v);
6312 return map;
6313 error:
6314 isl_map_free(map);
6315 isl_val_free(v);
6316 return NULL;
6319 /* Fix the value of the variable at position "pos" of type "type" of "set"
6320 * to be equal to "v".
6322 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6323 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6325 return isl_map_fix_val(set, type, pos, v);
6328 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6329 unsigned input, int value)
6331 return isl_map_fix_si(map, isl_dim_in, input, value);
6334 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6336 return set_from_map(isl_map_fix_si(set_to_map(set),
6337 isl_dim_set, dim, value));
6340 static __isl_give isl_basic_map *basic_map_bound_si(
6341 __isl_take isl_basic_map *bmap,
6342 enum isl_dim_type type, unsigned pos, int value, int upper)
6344 int j;
6346 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6347 return isl_basic_map_free(bmap);
6348 pos += isl_basic_map_offset(bmap, type);
6349 bmap = isl_basic_map_cow(bmap);
6350 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6351 j = isl_basic_map_alloc_inequality(bmap);
6352 if (j < 0)
6353 goto error;
6354 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6355 if (upper) {
6356 isl_int_set_si(bmap->ineq[j][pos], -1);
6357 isl_int_set_si(bmap->ineq[j][0], value);
6358 } else {
6359 isl_int_set_si(bmap->ineq[j][pos], 1);
6360 isl_int_set_si(bmap->ineq[j][0], -value);
6362 bmap = isl_basic_map_simplify(bmap);
6363 return isl_basic_map_finalize(bmap);
6364 error:
6365 isl_basic_map_free(bmap);
6366 return NULL;
6369 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6370 __isl_take isl_basic_map *bmap,
6371 enum isl_dim_type type, unsigned pos, int value)
6373 return basic_map_bound_si(bmap, type, pos, value, 0);
6376 /* Constrain the values of the given dimension to be no greater than "value".
6378 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6379 __isl_take isl_basic_map *bmap,
6380 enum isl_dim_type type, unsigned pos, int value)
6382 return basic_map_bound_si(bmap, type, pos, value, 1);
6385 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6386 enum isl_dim_type type, unsigned pos, int value, int upper)
6388 int i;
6390 map = isl_map_cow(map);
6391 if (isl_map_check_range(map, type, pos, 1) < 0)
6392 return isl_map_free(map);
6393 for (i = 0; i < map->n; ++i) {
6394 map->p[i] = basic_map_bound_si(map->p[i],
6395 type, pos, value, upper);
6396 if (!map->p[i])
6397 goto error;
6399 map = isl_map_unmark_normalized(map);
6400 return map;
6401 error:
6402 isl_map_free(map);
6403 return NULL;
6406 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6407 enum isl_dim_type type, unsigned pos, int value)
6409 return map_bound_si(map, type, pos, value, 0);
6412 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6413 enum isl_dim_type type, unsigned pos, int value)
6415 return map_bound_si(map, type, pos, value, 1);
6418 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6419 enum isl_dim_type type, unsigned pos, int value)
6421 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6422 type, pos, value));
6425 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6426 enum isl_dim_type type, unsigned pos, int value)
6428 return isl_map_upper_bound_si(set, type, pos, value);
6431 /* Bound the given variable of "bmap" from below (or above is "upper"
6432 * is set) to "value".
6434 static __isl_give isl_basic_map *basic_map_bound(
6435 __isl_take isl_basic_map *bmap,
6436 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6438 int j;
6440 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6441 return isl_basic_map_free(bmap);
6442 pos += isl_basic_map_offset(bmap, type);
6443 bmap = isl_basic_map_cow(bmap);
6444 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6445 j = isl_basic_map_alloc_inequality(bmap);
6446 if (j < 0)
6447 goto error;
6448 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6449 if (upper) {
6450 isl_int_set_si(bmap->ineq[j][pos], -1);
6451 isl_int_set(bmap->ineq[j][0], value);
6452 } else {
6453 isl_int_set_si(bmap->ineq[j][pos], 1);
6454 isl_int_neg(bmap->ineq[j][0], value);
6456 bmap = isl_basic_map_simplify(bmap);
6457 return isl_basic_map_finalize(bmap);
6458 error:
6459 isl_basic_map_free(bmap);
6460 return NULL;
6463 /* Bound the given variable of "map" from below (or above is "upper"
6464 * is set) to "value".
6466 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6467 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6469 int i;
6471 map = isl_map_cow(map);
6472 if (isl_map_check_range(map, type, pos, 1) < 0)
6473 return isl_map_free(map);
6474 for (i = map->n - 1; i >= 0; --i) {
6475 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6476 map = remove_if_empty(map, i);
6477 if (!map)
6478 return NULL;
6480 map = isl_map_unmark_normalized(map);
6481 return map;
6484 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6485 enum isl_dim_type type, unsigned pos, isl_int value)
6487 return map_bound(map, type, pos, value, 0);
6490 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6491 enum isl_dim_type type, unsigned pos, isl_int value)
6493 return map_bound(map, type, pos, value, 1);
6496 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6497 enum isl_dim_type type, unsigned pos, isl_int value)
6499 return isl_map_lower_bound(set, type, pos, value);
6502 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6503 enum isl_dim_type type, unsigned pos, isl_int value)
6505 return isl_map_upper_bound(set, type, pos, value);
6508 /* Force the values of the variable at position "pos" of type "type" of "set"
6509 * to be no smaller than "value".
6511 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6512 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6514 if (!value)
6515 goto error;
6516 if (!isl_val_is_int(value))
6517 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6518 "expecting integer value", goto error);
6519 set = isl_set_lower_bound(set, type, pos, value->n);
6520 isl_val_free(value);
6521 return set;
6522 error:
6523 isl_val_free(value);
6524 isl_set_free(set);
6525 return NULL;
6528 /* Force the values of the variable at position "pos" of type "type" of "set"
6529 * to be no greater than "value".
6531 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6532 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6534 if (!value)
6535 goto error;
6536 if (!isl_val_is_int(value))
6537 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6538 "expecting integer value", goto error);
6539 set = isl_set_upper_bound(set, type, pos, value->n);
6540 isl_val_free(value);
6541 return set;
6542 error:
6543 isl_val_free(value);
6544 isl_set_free(set);
6545 return NULL;
6548 /* Bound the given variable of "bset" from below (or above is "upper"
6549 * is set) to "value".
6551 static __isl_give isl_basic_set *isl_basic_set_bound(
6552 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6553 isl_int value, int upper)
6555 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6556 type, pos, value, upper));
6559 /* Bound the given variable of "bset" from below (or above is "upper"
6560 * is set) to "value".
6562 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6563 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6564 __isl_take isl_val *value, int upper)
6566 if (!value)
6567 goto error;
6568 if (!isl_val_is_int(value))
6569 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6570 "expecting integer value", goto error);
6571 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6572 isl_val_free(value);
6573 return bset;
6574 error:
6575 isl_val_free(value);
6576 isl_basic_set_free(bset);
6577 return NULL;
6580 /* Bound the given variable of "bset" from below to "value".
6582 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6583 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6584 __isl_take isl_val *value)
6586 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6589 /* Bound the given variable of "bset" from above to "value".
6591 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6592 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6593 __isl_take isl_val *value)
6595 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6598 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6600 int i;
6602 map = isl_map_cow(map);
6603 if (!map)
6604 return NULL;
6606 map->dim = isl_space_reverse(map->dim);
6607 if (!map->dim)
6608 goto error;
6609 for (i = 0; i < map->n; ++i) {
6610 map->p[i] = isl_basic_map_reverse(map->p[i]);
6611 if (!map->p[i])
6612 goto error;
6614 map = isl_map_unmark_normalized(map);
6615 return map;
6616 error:
6617 isl_map_free(map);
6618 return NULL;
6621 #undef TYPE
6622 #define TYPE isl_pw_multi_aff
6623 #undef SUFFIX
6624 #define SUFFIX _pw_multi_aff
6625 #undef EMPTY
6626 #define EMPTY isl_pw_multi_aff_empty
6627 #undef ADD
6628 #define ADD isl_pw_multi_aff_union_add
6629 #include "isl_map_lexopt_templ.c"
6631 /* Given a map "map", compute the lexicographically minimal
6632 * (or maximal) image element for each domain element in dom,
6633 * in the form of an isl_pw_multi_aff.
6634 * If "empty" is not NULL, then set *empty to those elements in dom that
6635 * do not have an image element.
6636 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6637 * should be computed over the domain of "map". "empty" is also NULL
6638 * in this case.
6640 * We first compute the lexicographically minimal or maximal element
6641 * in the first basic map. This results in a partial solution "res"
6642 * and a subset "todo" of dom that still need to be handled.
6643 * We then consider each of the remaining maps in "map" and successively
6644 * update both "res" and "todo".
6645 * If "empty" is NULL, then the todo sets are not needed and therefore
6646 * also not computed.
6648 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6649 __isl_take isl_map *map, __isl_take isl_set *dom,
6650 __isl_give isl_set **empty, unsigned flags)
6652 int i;
6653 int full;
6654 isl_pw_multi_aff *res;
6655 isl_set *todo;
6657 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6658 if (!map || (!full && !dom))
6659 goto error;
6661 if (isl_map_plain_is_empty(map)) {
6662 if (empty)
6663 *empty = dom;
6664 else
6665 isl_set_free(dom);
6666 return isl_pw_multi_aff_from_map(map);
6669 res = basic_map_partial_lexopt_pw_multi_aff(
6670 isl_basic_map_copy(map->p[0]),
6671 isl_set_copy(dom), empty, flags);
6673 if (empty)
6674 todo = *empty;
6675 for (i = 1; i < map->n; ++i) {
6676 isl_pw_multi_aff *res_i;
6678 res_i = basic_map_partial_lexopt_pw_multi_aff(
6679 isl_basic_map_copy(map->p[i]),
6680 isl_set_copy(dom), empty, flags);
6682 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6683 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6684 else
6685 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6687 if (empty)
6688 todo = isl_set_intersect(todo, *empty);
6691 isl_set_free(dom);
6692 isl_map_free(map);
6694 if (empty)
6695 *empty = todo;
6697 return res;
6698 error:
6699 if (empty)
6700 *empty = NULL;
6701 isl_set_free(dom);
6702 isl_map_free(map);
6703 return NULL;
6706 #undef TYPE
6707 #define TYPE isl_map
6708 #undef SUFFIX
6709 #define SUFFIX
6710 #undef EMPTY
6711 #define EMPTY isl_map_empty
6712 #undef ADD
6713 #define ADD isl_map_union_disjoint
6714 #include "isl_map_lexopt_templ.c"
6716 /* Given a map "map", compute the lexicographically minimal
6717 * (or maximal) image element for each domain element in "dom",
6718 * in the form of an isl_map.
6719 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6720 * do not have an image element.
6721 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6722 * should be computed over the domain of "map". "empty" is also NULL
6723 * in this case.
6725 * If the input consists of more than one disjunct, then first
6726 * compute the desired result in the form of an isl_pw_multi_aff and
6727 * then convert that into an isl_map.
6729 * This function used to have an explicit implementation in terms
6730 * of isl_maps, but it would continually intersect the domains of
6731 * partial results with the complement of the domain of the next
6732 * partial solution, potentially leading to an explosion in the number
6733 * of disjuncts if there are several disjuncts in the input.
6734 * An even earlier implementation of this function would look for
6735 * better results in the domain of the partial result and for extra
6736 * results in the complement of this domain, which would lead to
6737 * even more splintering.
6739 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6740 __isl_take isl_map *map, __isl_take isl_set *dom,
6741 __isl_give isl_set **empty, unsigned flags)
6743 int full;
6744 struct isl_map *res;
6745 isl_pw_multi_aff *pma;
6747 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6748 if (!map || (!full && !dom))
6749 goto error;
6751 if (isl_map_plain_is_empty(map)) {
6752 if (empty)
6753 *empty = dom;
6754 else
6755 isl_set_free(dom);
6756 return map;
6759 if (map->n == 1) {
6760 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6761 dom, empty, flags);
6762 isl_map_free(map);
6763 return res;
6766 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6767 flags);
6768 return isl_map_from_pw_multi_aff(pma);
6769 error:
6770 if (empty)
6771 *empty = NULL;
6772 isl_set_free(dom);
6773 isl_map_free(map);
6774 return NULL;
6777 __isl_give isl_map *isl_map_partial_lexmax(
6778 __isl_take isl_map *map, __isl_take isl_set *dom,
6779 __isl_give isl_set **empty)
6781 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6784 __isl_give isl_map *isl_map_partial_lexmin(
6785 __isl_take isl_map *map, __isl_take isl_set *dom,
6786 __isl_give isl_set **empty)
6788 return isl_map_partial_lexopt(map, dom, empty, 0);
6791 __isl_give isl_set *isl_set_partial_lexmin(
6792 __isl_take isl_set *set, __isl_take isl_set *dom,
6793 __isl_give isl_set **empty)
6795 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6796 dom, empty));
6799 __isl_give isl_set *isl_set_partial_lexmax(
6800 __isl_take isl_set *set, __isl_take isl_set *dom,
6801 __isl_give isl_set **empty)
6803 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6804 dom, empty));
6807 /* Compute the lexicographic minimum (or maximum if "flags" includes
6808 * ISL_OPT_MAX) of "bset" over its parametric domain.
6810 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6811 unsigned flags)
6813 return isl_basic_map_lexopt(bset, flags);
6816 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6818 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6821 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6823 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6826 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6828 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6831 /* Compute the lexicographic minimum of "bset" over its parametric domain
6832 * for the purpose of quantifier elimination.
6833 * That is, find an explicit representation for all the existentially
6834 * quantified variables in "bset" by computing their lexicographic
6835 * minimum.
6837 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6838 __isl_take isl_basic_set *bset)
6840 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6843 /* Given a basic map with one output dimension, compute the minimum or
6844 * maximum of that dimension as an isl_pw_aff.
6846 * Compute the optimum as a lexicographic optimum over the single
6847 * output dimension and extract the single isl_pw_aff from the result.
6849 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6850 int max)
6852 isl_pw_multi_aff *pma;
6853 isl_pw_aff *pwaff;
6855 bmap = isl_basic_map_copy(bmap);
6856 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6857 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6858 isl_pw_multi_aff_free(pma);
6860 return pwaff;
6863 /* Compute the minimum or maximum of the given output dimension
6864 * as a function of the parameters and the input dimensions,
6865 * but independently of the other output dimensions.
6867 * We first project out the other output dimension and then compute
6868 * the "lexicographic" maximum in each basic map, combining the results
6869 * using isl_pw_aff_union_max.
6871 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6872 int max)
6874 int i;
6875 isl_pw_aff *pwaff;
6876 unsigned n_out;
6878 n_out = isl_map_dim(map, isl_dim_out);
6879 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6880 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6881 if (!map)
6882 return NULL;
6884 if (map->n == 0) {
6885 isl_space *space = isl_map_get_space(map);
6886 isl_map_free(map);
6887 return isl_pw_aff_empty(space);
6890 pwaff = basic_map_dim_opt(map->p[0], max);
6891 for (i = 1; i < map->n; ++i) {
6892 isl_pw_aff *pwaff_i;
6894 pwaff_i = basic_map_dim_opt(map->p[i], max);
6895 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6898 isl_map_free(map);
6900 return pwaff;
6903 /* Compute the minimum of the given output dimension as a function of the
6904 * parameters and input dimensions, but independently of
6905 * the other output dimensions.
6907 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6909 return map_dim_opt(map, pos, 0);
6912 /* Compute the maximum of the given output dimension as a function of the
6913 * parameters and input dimensions, but independently of
6914 * the other output dimensions.
6916 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6918 return map_dim_opt(map, pos, 1);
6921 /* Compute the minimum or maximum of the given set dimension
6922 * as a function of the parameters,
6923 * but independently of the other set dimensions.
6925 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6926 int max)
6928 return map_dim_opt(set, pos, max);
6931 /* Compute the maximum of the given set dimension as a function of the
6932 * parameters, but independently of the other set dimensions.
6934 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6936 return set_dim_opt(set, pos, 1);
6939 /* Compute the minimum of the given set dimension as a function of the
6940 * parameters, but independently of the other set dimensions.
6942 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6944 return set_dim_opt(set, pos, 0);
6947 /* Apply a preimage specified by "mat" on the parameters of "bset".
6948 * bset is assumed to have only parameters and divs.
6950 static __isl_give isl_basic_set *basic_set_parameter_preimage(
6951 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
6953 unsigned nparam;
6955 if (!bset || !mat)
6956 goto error;
6958 bset->dim = isl_space_cow(bset->dim);
6959 if (!bset->dim)
6960 goto error;
6962 nparam = isl_basic_set_dim(bset, isl_dim_param);
6964 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6966 bset->dim->nparam = 0;
6967 bset->dim->n_out = nparam;
6968 bset = isl_basic_set_preimage(bset, mat);
6969 if (bset) {
6970 bset->dim->nparam = bset->dim->n_out;
6971 bset->dim->n_out = 0;
6973 return bset;
6974 error:
6975 isl_mat_free(mat);
6976 isl_basic_set_free(bset);
6977 return NULL;
6980 /* Apply a preimage specified by "mat" on the parameters of "set".
6981 * set is assumed to have only parameters and divs.
6983 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6984 __isl_take isl_mat *mat)
6986 isl_space *space;
6987 unsigned nparam;
6989 if (!set || !mat)
6990 goto error;
6992 nparam = isl_set_dim(set, isl_dim_param);
6994 if (mat->n_row != 1 + nparam)
6995 isl_die(isl_set_get_ctx(set), isl_error_internal,
6996 "unexpected number of rows", goto error);
6998 space = isl_set_get_space(set);
6999 space = isl_space_move_dims(space, isl_dim_set, 0,
7000 isl_dim_param, 0, nparam);
7001 set = isl_set_reset_space(set, space);
7002 set = isl_set_preimage(set, mat);
7003 nparam = isl_set_dim(set, isl_dim_out);
7004 space = isl_set_get_space(set);
7005 space = isl_space_move_dims(space, isl_dim_param, 0,
7006 isl_dim_out, 0, nparam);
7007 set = isl_set_reset_space(set, space);
7008 return set;
7009 error:
7010 isl_mat_free(mat);
7011 isl_set_free(set);
7012 return NULL;
7015 /* Intersect the basic set "bset" with the affine space specified by the
7016 * equalities in "eq".
7018 static __isl_give isl_basic_set *basic_set_append_equalities(
7019 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7021 int i, k;
7022 unsigned len;
7024 if (!bset || !eq)
7025 goto error;
7027 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7028 eq->n_row, 0);
7029 if (!bset)
7030 goto error;
7032 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7033 for (i = 0; i < eq->n_row; ++i) {
7034 k = isl_basic_set_alloc_equality(bset);
7035 if (k < 0)
7036 goto error;
7037 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7038 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7040 isl_mat_free(eq);
7042 bset = isl_basic_set_gauss(bset, NULL);
7043 bset = isl_basic_set_finalize(bset);
7045 return bset;
7046 error:
7047 isl_mat_free(eq);
7048 isl_basic_set_free(bset);
7049 return NULL;
7052 /* Intersect the set "set" with the affine space specified by the
7053 * equalities in "eq".
7055 static struct isl_set *set_append_equalities(struct isl_set *set,
7056 struct isl_mat *eq)
7058 int i;
7060 if (!set || !eq)
7061 goto error;
7063 for (i = 0; i < set->n; ++i) {
7064 set->p[i] = basic_set_append_equalities(set->p[i],
7065 isl_mat_copy(eq));
7066 if (!set->p[i])
7067 goto error;
7069 isl_mat_free(eq);
7070 return set;
7071 error:
7072 isl_mat_free(eq);
7073 isl_set_free(set);
7074 return NULL;
7077 /* Given a basic set "bset" that only involves parameters and existentially
7078 * quantified variables, return the index of the first equality
7079 * that only involves parameters. If there is no such equality then
7080 * return bset->n_eq.
7082 * This function assumes that isl_basic_set_gauss has been called on "bset".
7084 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7086 int i, j;
7087 unsigned nparam, n_div;
7089 if (!bset)
7090 return -1;
7092 nparam = isl_basic_set_dim(bset, isl_dim_param);
7093 n_div = isl_basic_set_dim(bset, isl_dim_div);
7095 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7096 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7097 ++i;
7100 return i;
7103 /* Compute an explicit representation for the existentially quantified
7104 * variables in "bset" by computing the "minimal value" of the set
7105 * variables. Since there are no set variables, the computation of
7106 * the minimal value essentially computes an explicit representation
7107 * of the non-empty part(s) of "bset".
7109 * The input only involves parameters and existentially quantified variables.
7110 * All equalities among parameters have been removed.
7112 * Since the existentially quantified variables in the result are in general
7113 * going to be different from those in the input, we first replace
7114 * them by the minimal number of variables based on their equalities.
7115 * This should simplify the parametric integer programming.
7117 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7119 isl_morph *morph1, *morph2;
7120 isl_set *set;
7121 unsigned n;
7123 if (!bset)
7124 return NULL;
7125 if (bset->n_eq == 0)
7126 return isl_basic_set_lexmin_compute_divs(bset);
7128 morph1 = isl_basic_set_parameter_compression(bset);
7129 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7130 bset = isl_basic_set_lift(bset);
7131 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7132 bset = isl_morph_basic_set(morph2, bset);
7133 n = isl_basic_set_dim(bset, isl_dim_set);
7134 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7136 set = isl_basic_set_lexmin_compute_divs(bset);
7138 set = isl_morph_set(isl_morph_inverse(morph1), set);
7140 return set;
7143 /* Project the given basic set onto its parameter domain, possibly introducing
7144 * new, explicit, existential variables in the constraints.
7145 * The input has parameters and (possibly implicit) existential variables.
7146 * The output has the same parameters, but only
7147 * explicit existentially quantified variables.
7149 * The actual projection is performed by pip, but pip doesn't seem
7150 * to like equalities very much, so we first remove the equalities
7151 * among the parameters by performing a variable compression on
7152 * the parameters. Afterward, an inverse transformation is performed
7153 * and the equalities among the parameters are inserted back in.
7155 * The variable compression on the parameters may uncover additional
7156 * equalities that were only implicit before. We therefore check
7157 * if there are any new parameter equalities in the result and
7158 * if so recurse. The removal of parameter equalities is required
7159 * for the parameter compression performed by base_compute_divs.
7161 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7163 int i;
7164 struct isl_mat *eq;
7165 struct isl_mat *T, *T2;
7166 struct isl_set *set;
7167 unsigned nparam;
7169 bset = isl_basic_set_cow(bset);
7170 if (!bset)
7171 return NULL;
7173 if (bset->n_eq == 0)
7174 return base_compute_divs(bset);
7176 bset = isl_basic_set_gauss(bset, NULL);
7177 if (!bset)
7178 return NULL;
7179 if (isl_basic_set_plain_is_empty(bset))
7180 return isl_set_from_basic_set(bset);
7182 i = first_parameter_equality(bset);
7183 if (i == bset->n_eq)
7184 return base_compute_divs(bset);
7186 nparam = isl_basic_set_dim(bset, isl_dim_param);
7187 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7188 0, 1 + nparam);
7189 eq = isl_mat_cow(eq);
7190 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7191 if (T && T->n_col == 0) {
7192 isl_mat_free(T);
7193 isl_mat_free(T2);
7194 isl_mat_free(eq);
7195 bset = isl_basic_set_set_to_empty(bset);
7196 return isl_set_from_basic_set(bset);
7198 bset = basic_set_parameter_preimage(bset, T);
7200 i = first_parameter_equality(bset);
7201 if (!bset)
7202 set = NULL;
7203 else if (i == bset->n_eq)
7204 set = base_compute_divs(bset);
7205 else
7206 set = parameter_compute_divs(bset);
7207 set = set_parameter_preimage(set, T2);
7208 set = set_append_equalities(set, eq);
7209 return set;
7212 /* Insert the divs from "ls" before those of "bmap".
7214 * The number of columns is not changed, which means that the last
7215 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7216 * The caller is responsible for removing the same number of dimensions
7217 * from the space of "bmap".
7219 static __isl_give isl_basic_map *insert_divs_from_local_space(
7220 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7222 int i;
7223 int n_div;
7224 int old_n_div;
7226 n_div = isl_local_space_dim(ls, isl_dim_div);
7227 if (n_div == 0)
7228 return bmap;
7230 old_n_div = bmap->n_div;
7231 bmap = insert_div_rows(bmap, n_div);
7232 if (!bmap)
7233 return NULL;
7235 for (i = 0; i < n_div; ++i) {
7236 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7237 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7240 return bmap;
7243 /* Replace the space of "bmap" by the space and divs of "ls".
7245 * If "ls" has any divs, then we simplify the result since we may
7246 * have discovered some additional equalities that could simplify
7247 * the div expressions.
7249 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7250 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7252 int n_div;
7254 bmap = isl_basic_map_cow(bmap);
7255 if (!bmap || !ls)
7256 goto error;
7258 n_div = isl_local_space_dim(ls, isl_dim_div);
7259 bmap = insert_divs_from_local_space(bmap, ls);
7260 if (!bmap)
7261 goto error;
7263 isl_space_free(bmap->dim);
7264 bmap->dim = isl_local_space_get_space(ls);
7265 if (!bmap->dim)
7266 goto error;
7268 isl_local_space_free(ls);
7269 if (n_div > 0)
7270 bmap = isl_basic_map_simplify(bmap);
7271 bmap = isl_basic_map_finalize(bmap);
7272 return bmap;
7273 error:
7274 isl_basic_map_free(bmap);
7275 isl_local_space_free(ls);
7276 return NULL;
7279 /* Replace the space of "map" by the space and divs of "ls".
7281 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7282 __isl_take isl_local_space *ls)
7284 int i;
7286 map = isl_map_cow(map);
7287 if (!map || !ls)
7288 goto error;
7290 for (i = 0; i < map->n; ++i) {
7291 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7292 isl_local_space_copy(ls));
7293 if (!map->p[i])
7294 goto error;
7296 isl_space_free(map->dim);
7297 map->dim = isl_local_space_get_space(ls);
7298 if (!map->dim)
7299 goto error;
7301 isl_local_space_free(ls);
7302 return map;
7303 error:
7304 isl_local_space_free(ls);
7305 isl_map_free(map);
7306 return NULL;
7309 /* Compute an explicit representation for the existentially
7310 * quantified variables for which do not know any explicit representation yet.
7312 * We first sort the existentially quantified variables so that the
7313 * existentially quantified variables for which we already have an explicit
7314 * representation are placed before those for which we do not.
7315 * The input dimensions, the output dimensions and the existentially
7316 * quantified variables for which we already have an explicit
7317 * representation are then turned into parameters.
7318 * compute_divs returns a map with the same parameters and
7319 * no input or output dimensions and the dimension specification
7320 * is reset to that of the input, including the existentially quantified
7321 * variables for which we already had an explicit representation.
7323 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7325 struct isl_basic_set *bset;
7326 struct isl_set *set;
7327 struct isl_map *map;
7328 isl_space *space;
7329 isl_local_space *ls;
7330 unsigned nparam;
7331 unsigned n_in;
7332 unsigned n_out;
7333 int n_known;
7334 int i;
7336 bmap = isl_basic_map_sort_divs(bmap);
7337 bmap = isl_basic_map_cow(bmap);
7338 if (!bmap)
7339 return NULL;
7341 n_known = isl_basic_map_first_unknown_div(bmap);
7342 if (n_known < 0)
7343 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7345 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7346 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7347 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7348 space = isl_space_set_alloc(bmap->ctx,
7349 nparam + n_in + n_out + n_known, 0);
7350 if (!space)
7351 goto error;
7353 ls = isl_basic_map_get_local_space(bmap);
7354 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7355 n_known, bmap->n_div - n_known);
7356 if (n_known > 0) {
7357 for (i = n_known; i < bmap->n_div; ++i)
7358 swap_div(bmap, i - n_known, i);
7359 bmap->n_div -= n_known;
7360 bmap->extra -= n_known;
7362 bmap = isl_basic_map_reset_space(bmap, space);
7363 bset = bset_from_bmap(bmap);
7365 set = parameter_compute_divs(bset);
7366 map = set_to_map(set);
7367 map = replace_space_by_local_space(map, ls);
7369 return map;
7370 error:
7371 isl_basic_map_free(bmap);
7372 return NULL;
7375 /* Remove the explicit representation of local variable "div",
7376 * if there is any.
7378 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7379 __isl_take isl_basic_map *bmap, int div)
7381 isl_bool unknown;
7383 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7384 if (unknown < 0)
7385 return isl_basic_map_free(bmap);
7386 if (unknown)
7387 return bmap;
7389 bmap = isl_basic_map_cow(bmap);
7390 if (!bmap)
7391 return NULL;
7392 isl_int_set_si(bmap->div[div][0], 0);
7393 return bmap;
7396 /* Is local variable "div" of "bmap" marked as not having an explicit
7397 * representation?
7398 * Note that even if "div" is not marked in this way and therefore
7399 * has an explicit representation, this representation may still
7400 * depend (indirectly) on other local variables that do not
7401 * have an explicit representation.
7403 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7404 int div)
7406 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7407 return isl_bool_error;
7408 return isl_int_is_zero(bmap->div[div][0]);
7411 /* Return the position of the first local variable that does not
7412 * have an explicit representation.
7413 * Return the total number of local variables if they all have
7414 * an explicit representation.
7415 * Return -1 on error.
7417 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7419 int i;
7421 if (!bmap)
7422 return -1;
7424 for (i = 0; i < bmap->n_div; ++i) {
7425 if (!isl_basic_map_div_is_known(bmap, i))
7426 return i;
7428 return bmap->n_div;
7431 /* Return the position of the first local variable that does not
7432 * have an explicit representation.
7433 * Return the total number of local variables if they all have
7434 * an explicit representation.
7435 * Return -1 on error.
7437 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7439 return isl_basic_map_first_unknown_div(bset);
7442 /* Does "bmap" have an explicit representation for all local variables?
7444 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7446 int first, n;
7448 n = isl_basic_map_dim(bmap, isl_dim_div);
7449 first = isl_basic_map_first_unknown_div(bmap);
7450 if (first < 0)
7451 return isl_bool_error;
7452 return first == n;
7455 /* Do all basic maps in "map" have an explicit representation
7456 * for all local variables?
7458 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7460 int i;
7462 if (!map)
7463 return isl_bool_error;
7465 for (i = 0; i < map->n; ++i) {
7466 int known = isl_basic_map_divs_known(map->p[i]);
7467 if (known <= 0)
7468 return known;
7471 return isl_bool_true;
7474 /* If bmap contains any unknown divs, then compute explicit
7475 * expressions for them. However, this computation may be
7476 * quite expensive, so first try to remove divs that aren't
7477 * strictly needed.
7479 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7481 int known;
7482 struct isl_map *map;
7484 known = isl_basic_map_divs_known(bmap);
7485 if (known < 0)
7486 goto error;
7487 if (known)
7488 return isl_map_from_basic_map(bmap);
7490 bmap = isl_basic_map_drop_redundant_divs(bmap);
7492 known = isl_basic_map_divs_known(bmap);
7493 if (known < 0)
7494 goto error;
7495 if (known)
7496 return isl_map_from_basic_map(bmap);
7498 map = compute_divs(bmap);
7499 return map;
7500 error:
7501 isl_basic_map_free(bmap);
7502 return NULL;
7505 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7507 int i;
7508 int known;
7509 struct isl_map *res;
7511 if (!map)
7512 return NULL;
7513 if (map->n == 0)
7514 return map;
7516 known = isl_map_divs_known(map);
7517 if (known < 0) {
7518 isl_map_free(map);
7519 return NULL;
7521 if (known)
7522 return map;
7524 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7525 for (i = 1 ; i < map->n; ++i) {
7526 struct isl_map *r2;
7527 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7528 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7529 res = isl_map_union_disjoint(res, r2);
7530 else
7531 res = isl_map_union(res, r2);
7533 isl_map_free(map);
7535 return res;
7538 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7540 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7543 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7545 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7548 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7550 int i;
7551 struct isl_set *set;
7553 if (!map)
7554 goto error;
7556 map = isl_map_cow(map);
7557 if (!map)
7558 return NULL;
7560 set = set_from_map(map);
7561 set->dim = isl_space_domain(set->dim);
7562 if (!set->dim)
7563 goto error;
7564 for (i = 0; i < map->n; ++i) {
7565 set->p[i] = isl_basic_map_domain(map->p[i]);
7566 if (!set->p[i])
7567 goto error;
7569 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7570 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7571 return set;
7572 error:
7573 isl_map_free(map);
7574 return NULL;
7577 /* Return the union of "map1" and "map2", where we assume for now that
7578 * "map1" and "map2" are disjoint. Note that the basic maps inside
7579 * "map1" or "map2" may not be disjoint from each other.
7580 * Also note that this function is also called from isl_map_union,
7581 * which takes care of handling the situation where "map1" and "map2"
7582 * may not be disjoint.
7584 * If one of the inputs is empty, we can simply return the other input.
7585 * Similarly, if one of the inputs is universal, then it is equal to the union.
7587 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7588 __isl_take isl_map *map2)
7590 int i;
7591 unsigned flags = 0;
7592 struct isl_map *map = NULL;
7593 int is_universe;
7595 if (!map1 || !map2)
7596 goto error;
7598 if (!isl_space_is_equal(map1->dim, map2->dim))
7599 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7600 "spaces don't match", goto error);
7602 if (map1->n == 0) {
7603 isl_map_free(map1);
7604 return map2;
7606 if (map2->n == 0) {
7607 isl_map_free(map2);
7608 return map1;
7611 is_universe = isl_map_plain_is_universe(map1);
7612 if (is_universe < 0)
7613 goto error;
7614 if (is_universe) {
7615 isl_map_free(map2);
7616 return map1;
7619 is_universe = isl_map_plain_is_universe(map2);
7620 if (is_universe < 0)
7621 goto error;
7622 if (is_universe) {
7623 isl_map_free(map1);
7624 return map2;
7627 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7628 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7629 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7631 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7632 map1->n + map2->n, flags);
7633 if (!map)
7634 goto error;
7635 for (i = 0; i < map1->n; ++i) {
7636 map = isl_map_add_basic_map(map,
7637 isl_basic_map_copy(map1->p[i]));
7638 if (!map)
7639 goto error;
7641 for (i = 0; i < map2->n; ++i) {
7642 map = isl_map_add_basic_map(map,
7643 isl_basic_map_copy(map2->p[i]));
7644 if (!map)
7645 goto error;
7647 isl_map_free(map1);
7648 isl_map_free(map2);
7649 return map;
7650 error:
7651 isl_map_free(map);
7652 isl_map_free(map1);
7653 isl_map_free(map2);
7654 return NULL;
7657 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7658 * guaranteed to be disjoint by the caller.
7660 * Note that this functions is called from within isl_map_make_disjoint,
7661 * so we have to be careful not to touch the constraints of the inputs
7662 * in any way.
7664 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7665 __isl_take isl_map *map2)
7667 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7670 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7671 * not be disjoint. The parameters are assumed to have been aligned.
7673 * We currently simply call map_union_disjoint, the internal operation
7674 * of which does not really depend on the inputs being disjoint.
7675 * If the result contains more than one basic map, then we clear
7676 * the disjoint flag since the result may contain basic maps from
7677 * both inputs and these are not guaranteed to be disjoint.
7679 * As a special case, if "map1" and "map2" are obviously equal,
7680 * then we simply return "map1".
7682 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7683 __isl_take isl_map *map2)
7685 int equal;
7687 if (!map1 || !map2)
7688 goto error;
7690 equal = isl_map_plain_is_equal(map1, map2);
7691 if (equal < 0)
7692 goto error;
7693 if (equal) {
7694 isl_map_free(map2);
7695 return map1;
7698 map1 = map_union_disjoint(map1, map2);
7699 if (!map1)
7700 return NULL;
7701 if (map1->n > 1)
7702 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7703 return map1;
7704 error:
7705 isl_map_free(map1);
7706 isl_map_free(map2);
7707 return NULL;
7710 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7711 * not be disjoint.
7713 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7714 __isl_take isl_map *map2)
7716 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7719 __isl_give isl_set *isl_set_union_disjoint(
7720 __isl_take isl_set *set1, __isl_take isl_set *set2)
7722 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7723 set_to_map(set2)));
7726 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7728 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7731 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7732 * the results.
7734 * "map" and "set" are assumed to be compatible and non-NULL.
7736 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7737 __isl_take isl_set *set,
7738 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7739 __isl_take isl_basic_set *bset))
7741 unsigned flags = 0;
7742 struct isl_map *result;
7743 int i, j;
7745 if (isl_set_plain_is_universe(set)) {
7746 isl_set_free(set);
7747 return map;
7750 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7751 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7752 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7754 result = isl_map_alloc_space(isl_space_copy(map->dim),
7755 map->n * set->n, flags);
7756 for (i = 0; result && i < map->n; ++i)
7757 for (j = 0; j < set->n; ++j) {
7758 result = isl_map_add_basic_map(result,
7759 fn(isl_basic_map_copy(map->p[i]),
7760 isl_basic_set_copy(set->p[j])));
7761 if (!result)
7762 break;
7765 isl_map_free(map);
7766 isl_set_free(set);
7767 return result;
7770 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7771 __isl_take isl_set *set)
7773 isl_bool ok;
7775 ok = isl_map_compatible_range(map, set);
7776 if (ok < 0)
7777 goto error;
7778 if (!ok)
7779 isl_die(set->ctx, isl_error_invalid,
7780 "incompatible spaces", goto error);
7782 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7783 error:
7784 isl_map_free(map);
7785 isl_set_free(set);
7786 return NULL;
7789 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7790 __isl_take isl_set *set)
7792 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7795 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7796 __isl_take isl_set *set)
7798 isl_bool ok;
7800 ok = isl_map_compatible_domain(map, set);
7801 if (ok < 0)
7802 goto error;
7803 if (!ok)
7804 isl_die(set->ctx, isl_error_invalid,
7805 "incompatible spaces", goto error);
7807 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7808 error:
7809 isl_map_free(map);
7810 isl_set_free(set);
7811 return NULL;
7814 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7815 __isl_take isl_set *set)
7817 return isl_map_align_params_map_map_and(map, set,
7818 &map_intersect_domain);
7821 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7822 * in the space B -> C, return the intersection.
7823 * The parameters are assumed to have been aligned.
7825 * The map "factor" is first extended to a map living in the space
7826 * [A -> B] -> C and then a regular intersection is computed.
7828 static __isl_give isl_map *map_intersect_domain_factor_range(
7829 __isl_take isl_map *map, __isl_take isl_map *factor)
7831 isl_space *space;
7832 isl_map *ext_factor;
7834 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7835 ext_factor = isl_map_universe(space);
7836 ext_factor = isl_map_domain_product(ext_factor, factor);
7837 return map_intersect(map, ext_factor);
7840 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7841 * in the space B -> C, return the intersection.
7843 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7844 __isl_take isl_map *map, __isl_take isl_map *factor)
7846 return isl_map_align_params_map_map_and(map, factor,
7847 &map_intersect_domain_factor_range);
7850 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7851 * in the space A -> C, return the intersection.
7853 * The map "factor" is first extended to a map living in the space
7854 * A -> [B -> C] and then a regular intersection is computed.
7856 static __isl_give isl_map *map_intersect_range_factor_range(
7857 __isl_take isl_map *map, __isl_take isl_map *factor)
7859 isl_space *space;
7860 isl_map *ext_factor;
7862 space = isl_space_range_factor_domain(isl_map_get_space(map));
7863 ext_factor = isl_map_universe(space);
7864 ext_factor = isl_map_range_product(ext_factor, factor);
7865 return isl_map_intersect(map, ext_factor);
7868 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7869 * in the space A -> C, return the intersection.
7871 __isl_give isl_map *isl_map_intersect_range_factor_range(
7872 __isl_take isl_map *map, __isl_take isl_map *factor)
7874 return isl_map_align_params_map_map_and(map, factor,
7875 &map_intersect_range_factor_range);
7878 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7879 __isl_take isl_map *map2)
7881 if (!map1 || !map2)
7882 goto error;
7883 map1 = isl_map_reverse(map1);
7884 map1 = isl_map_apply_range(map1, map2);
7885 return isl_map_reverse(map1);
7886 error:
7887 isl_map_free(map1);
7888 isl_map_free(map2);
7889 return NULL;
7892 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7893 __isl_take isl_map *map2)
7895 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7898 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7899 __isl_take isl_map *map2)
7901 isl_space *space;
7902 struct isl_map *result;
7903 int i, j;
7905 if (!map1 || !map2)
7906 goto error;
7908 space = isl_space_join(isl_space_copy(map1->dim),
7909 isl_space_copy(map2->dim));
7911 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7912 if (!result)
7913 goto error;
7914 for (i = 0; i < map1->n; ++i)
7915 for (j = 0; j < map2->n; ++j) {
7916 result = isl_map_add_basic_map(result,
7917 isl_basic_map_apply_range(
7918 isl_basic_map_copy(map1->p[i]),
7919 isl_basic_map_copy(map2->p[j])));
7920 if (!result)
7921 goto error;
7923 isl_map_free(map1);
7924 isl_map_free(map2);
7925 if (result && result->n <= 1)
7926 ISL_F_SET(result, ISL_MAP_DISJOINT);
7927 return result;
7928 error:
7929 isl_map_free(map1);
7930 isl_map_free(map2);
7931 return NULL;
7934 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7935 __isl_take isl_map *map2)
7937 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7941 * returns range - domain
7943 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
7945 isl_space *target_space;
7946 struct isl_basic_set *bset;
7947 unsigned dim;
7948 unsigned nparam;
7949 int i;
7951 if (!bmap)
7952 goto error;
7953 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7954 bmap->dim, isl_dim_out),
7955 goto error);
7956 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7957 dim = isl_basic_map_dim(bmap, isl_dim_in);
7958 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7959 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7960 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7961 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7962 for (i = 0; i < dim; ++i) {
7963 int j = isl_basic_map_alloc_equality(bmap);
7964 if (j < 0) {
7965 bmap = isl_basic_map_free(bmap);
7966 break;
7968 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7969 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7970 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7971 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7973 bset = isl_basic_map_domain(bmap);
7974 bset = isl_basic_set_reset_space(bset, target_space);
7975 return bset;
7976 error:
7977 isl_basic_map_free(bmap);
7978 return NULL;
7981 /* Check that domain and range of "map" are the same.
7983 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
7985 isl_space *space;
7986 isl_bool equal;
7988 space = isl_map_peek_space(map);
7989 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
7990 if (equal < 0)
7991 return isl_stat_error;
7992 if (!equal)
7993 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7994 "domain and range don't match", return isl_stat_error);
7995 return isl_stat_ok;
7999 * returns range - domain
8001 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8003 int i;
8004 isl_space *dim;
8005 struct isl_set *result;
8007 if (!map)
8008 return NULL;
8010 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8011 map->dim, isl_dim_out),
8012 goto error);
8013 dim = isl_map_get_space(map);
8014 dim = isl_space_domain(dim);
8015 result = isl_set_alloc_space(dim, map->n, 0);
8016 if (!result)
8017 goto error;
8018 for (i = 0; i < map->n; ++i)
8019 result = isl_set_add_basic_set(result,
8020 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8021 isl_map_free(map);
8022 return result;
8023 error:
8024 isl_map_free(map);
8025 return NULL;
8029 * returns [domain -> range] -> range - domain
8031 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8032 __isl_take isl_basic_map *bmap)
8034 int i, k;
8035 isl_space *space;
8036 isl_basic_map *domain;
8037 int nparam, n;
8038 unsigned total;
8040 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8041 bmap->dim, isl_dim_out))
8042 isl_die(bmap->ctx, isl_error_invalid,
8043 "domain and range don't match", goto error);
8045 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8046 n = isl_basic_map_dim(bmap, isl_dim_in);
8048 space = isl_basic_map_get_space(bmap);
8049 space = isl_space_from_range(isl_space_domain(space));
8050 domain = isl_basic_map_universe(space);
8052 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8053 bmap = isl_basic_map_apply_range(bmap, domain);
8054 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8056 total = isl_basic_map_total_dim(bmap);
8058 for (i = 0; i < n; ++i) {
8059 k = isl_basic_map_alloc_equality(bmap);
8060 if (k < 0)
8061 goto error;
8062 isl_seq_clr(bmap->eq[k], 1 + total);
8063 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8064 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8065 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8068 bmap = isl_basic_map_gauss(bmap, NULL);
8069 return isl_basic_map_finalize(bmap);
8070 error:
8071 isl_basic_map_free(bmap);
8072 return NULL;
8076 * returns [domain -> range] -> range - domain
8078 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8080 int i;
8081 isl_space *domain_space;
8083 if (isl_map_check_equal_tuples(map) < 0)
8084 return isl_map_free(map);
8086 map = isl_map_cow(map);
8087 if (!map)
8088 return NULL;
8090 domain_space = isl_space_domain(isl_map_get_space(map));
8091 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8092 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8093 if (!map->dim)
8094 goto error;
8095 for (i = 0; i < map->n; ++i) {
8096 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8097 if (!map->p[i])
8098 goto error;
8100 map = isl_map_unmark_normalized(map);
8101 return map;
8102 error:
8103 isl_map_free(map);
8104 return NULL;
8107 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8109 unsigned n_in, n_out;
8111 if (!space)
8112 return NULL;
8113 n_in = isl_space_dim(space, isl_dim_in);
8114 n_out = isl_space_dim(space, isl_dim_out);
8115 if (n_in != n_out)
8116 isl_die(space->ctx, isl_error_invalid,
8117 "number of input and output dimensions needs to be "
8118 "the same", goto error);
8119 return isl_basic_map_equal(space, n_in);
8120 error:
8121 isl_space_free(space);
8122 return NULL;
8125 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8127 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8130 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8132 isl_space *dim = isl_set_get_space(set);
8133 isl_map *id;
8134 id = isl_map_identity(isl_space_map_from_set(dim));
8135 return isl_map_intersect_range(id, set);
8138 /* Construct a basic set with all set dimensions having only non-negative
8139 * values.
8141 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8142 __isl_take isl_space *space)
8144 int i;
8145 unsigned nparam;
8146 unsigned dim;
8147 struct isl_basic_set *bset;
8149 if (!space)
8150 return NULL;
8151 nparam = isl_space_dim(space, isl_dim_param);
8152 dim = isl_space_dim(space, isl_dim_set);
8153 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8154 if (!bset)
8155 return NULL;
8156 for (i = 0; i < dim; ++i) {
8157 int k = isl_basic_set_alloc_inequality(bset);
8158 if (k < 0)
8159 goto error;
8160 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8161 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8163 return bset;
8164 error:
8165 isl_basic_set_free(bset);
8166 return NULL;
8169 /* Construct the half-space x_pos >= 0.
8171 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8172 int pos)
8174 int k;
8175 isl_basic_set *nonneg;
8177 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8178 k = isl_basic_set_alloc_inequality(nonneg);
8179 if (k < 0)
8180 goto error;
8181 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8182 isl_int_set_si(nonneg->ineq[k][pos], 1);
8184 return isl_basic_set_finalize(nonneg);
8185 error:
8186 isl_basic_set_free(nonneg);
8187 return NULL;
8190 /* Construct the half-space x_pos <= -1.
8192 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8193 int pos)
8195 int k;
8196 isl_basic_set *neg;
8198 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8199 k = isl_basic_set_alloc_inequality(neg);
8200 if (k < 0)
8201 goto error;
8202 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8203 isl_int_set_si(neg->ineq[k][0], -1);
8204 isl_int_set_si(neg->ineq[k][pos], -1);
8206 return isl_basic_set_finalize(neg);
8207 error:
8208 isl_basic_set_free(neg);
8209 return NULL;
8212 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8213 enum isl_dim_type type, unsigned first, unsigned n)
8215 int i;
8216 unsigned offset;
8217 isl_basic_set *nonneg;
8218 isl_basic_set *neg;
8220 if (n == 0)
8221 return set;
8223 if (isl_set_check_range(set, type, first, n) < 0)
8224 return isl_set_free(set);
8226 offset = pos(set->dim, type);
8227 for (i = 0; i < n; ++i) {
8228 nonneg = nonneg_halfspace(isl_set_get_space(set),
8229 offset + first + i);
8230 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8232 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8235 return set;
8238 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8239 int len,
8240 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8241 void *user)
8243 isl_set *half;
8245 if (!set)
8246 return isl_stat_error;
8247 if (isl_set_plain_is_empty(set)) {
8248 isl_set_free(set);
8249 return isl_stat_ok;
8251 if (first == len)
8252 return fn(set, signs, user);
8254 signs[first] = 1;
8255 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8256 1 + first));
8257 half = isl_set_intersect(half, isl_set_copy(set));
8258 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8259 goto error;
8261 signs[first] = -1;
8262 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8263 1 + first));
8264 half = isl_set_intersect(half, set);
8265 return foreach_orthant(half, signs, first + 1, len, fn, user);
8266 error:
8267 isl_set_free(set);
8268 return isl_stat_error;
8271 /* Call "fn" on the intersections of "set" with each of the orthants
8272 * (except for obviously empty intersections). The orthant is identified
8273 * by the signs array, with each entry having value 1 or -1 according
8274 * to the sign of the corresponding variable.
8276 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8277 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8278 void *user)
8280 unsigned nparam;
8281 unsigned nvar;
8282 int *signs;
8283 isl_stat r;
8285 if (!set)
8286 return isl_stat_error;
8287 if (isl_set_plain_is_empty(set))
8288 return isl_stat_ok;
8290 nparam = isl_set_dim(set, isl_dim_param);
8291 nvar = isl_set_dim(set, isl_dim_set);
8293 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8295 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8296 fn, user);
8298 free(signs);
8300 return r;
8303 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8305 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8308 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8309 __isl_keep isl_basic_map *bmap2)
8311 isl_bool is_subset;
8312 struct isl_map *map1;
8313 struct isl_map *map2;
8315 if (!bmap1 || !bmap2)
8316 return isl_bool_error;
8318 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8319 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8321 is_subset = isl_map_is_subset(map1, map2);
8323 isl_map_free(map1);
8324 isl_map_free(map2);
8326 return is_subset;
8329 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8330 __isl_keep isl_basic_set *bset2)
8332 return isl_basic_map_is_subset(bset1, bset2);
8335 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8336 __isl_keep isl_basic_map *bmap2)
8338 isl_bool is_subset;
8340 if (!bmap1 || !bmap2)
8341 return isl_bool_error;
8342 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8343 if (is_subset != isl_bool_true)
8344 return is_subset;
8345 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8346 return is_subset;
8349 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8350 __isl_keep isl_basic_set *bset2)
8352 return isl_basic_map_is_equal(
8353 bset_to_bmap(bset1), bset_to_bmap(bset2));
8356 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8358 int i;
8359 int is_empty;
8361 if (!map)
8362 return isl_bool_error;
8363 for (i = 0; i < map->n; ++i) {
8364 is_empty = isl_basic_map_is_empty(map->p[i]);
8365 if (is_empty < 0)
8366 return isl_bool_error;
8367 if (!is_empty)
8368 return isl_bool_false;
8370 return isl_bool_true;
8373 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8375 return map ? map->n == 0 : isl_bool_error;
8378 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8380 return set ? set->n == 0 : isl_bool_error;
8383 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8385 return isl_map_is_empty(set_to_map(set));
8388 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8389 __isl_keep isl_map *map2)
8391 if (!map1 || !map2)
8392 return isl_bool_error;
8394 return isl_space_is_equal(map1->dim, map2->dim);
8397 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8398 __isl_keep isl_set *set2)
8400 if (!set1 || !set2)
8401 return isl_bool_error;
8403 return isl_space_is_equal(set1->dim, set2->dim);
8406 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8408 isl_bool is_subset;
8410 if (!map1 || !map2)
8411 return isl_bool_error;
8412 is_subset = isl_map_is_subset(map1, map2);
8413 if (is_subset != isl_bool_true)
8414 return is_subset;
8415 is_subset = isl_map_is_subset(map2, map1);
8416 return is_subset;
8419 /* Is "map1" equal to "map2"?
8421 * First check if they are obviously equal.
8422 * If not, then perform a more detailed analysis.
8424 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8426 isl_bool equal;
8428 equal = isl_map_plain_is_equal(map1, map2);
8429 if (equal < 0 || equal)
8430 return equal;
8431 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8434 isl_bool isl_basic_map_is_strict_subset(
8435 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8437 isl_bool is_subset;
8439 if (!bmap1 || !bmap2)
8440 return isl_bool_error;
8441 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8442 if (is_subset != isl_bool_true)
8443 return is_subset;
8444 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8445 return isl_bool_not(is_subset);
8448 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8449 __isl_keep isl_map *map2)
8451 isl_bool is_subset;
8453 if (!map1 || !map2)
8454 return isl_bool_error;
8455 is_subset = isl_map_is_subset(map1, map2);
8456 if (is_subset != isl_bool_true)
8457 return is_subset;
8458 is_subset = isl_map_is_subset(map2, map1);
8459 return isl_bool_not(is_subset);
8462 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8463 __isl_keep isl_set *set2)
8465 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8468 /* Is "bmap" obviously equal to the universe with the same space?
8470 * That is, does it not have any constraints?
8472 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8474 if (!bmap)
8475 return isl_bool_error;
8476 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8479 /* Is "bset" obviously equal to the universe with the same space?
8481 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8483 return isl_basic_map_plain_is_universe(bset);
8486 /* If "c" does not involve any existentially quantified variables,
8487 * then set *univ to false and abort
8489 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8491 isl_bool *univ = user;
8492 unsigned n;
8494 n = isl_constraint_dim(c, isl_dim_div);
8495 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8496 isl_constraint_free(c);
8497 if (*univ < 0 || !*univ)
8498 return isl_stat_error;
8499 return isl_stat_ok;
8502 /* Is "bmap" equal to the universe with the same space?
8504 * First check if it is obviously equal to the universe.
8505 * If not and if there are any constraints not involving
8506 * existentially quantified variables, then it is certainly
8507 * not equal to the universe.
8508 * Otherwise, check if the universe is a subset of "bmap".
8510 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8512 isl_bool univ;
8513 isl_basic_map *test;
8515 univ = isl_basic_map_plain_is_universe(bmap);
8516 if (univ < 0 || univ)
8517 return univ;
8518 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8519 return isl_bool_false;
8520 univ = isl_bool_true;
8521 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8522 univ)
8523 return isl_bool_error;
8524 if (univ < 0 || !univ)
8525 return univ;
8526 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8527 univ = isl_basic_map_is_subset(test, bmap);
8528 isl_basic_map_free(test);
8529 return univ;
8532 /* Is "bset" equal to the universe with the same space?
8534 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8536 return isl_basic_map_is_universe(bset);
8539 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8541 int i;
8543 if (!map)
8544 return isl_bool_error;
8546 for (i = 0; i < map->n; ++i) {
8547 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8548 if (r < 0 || r)
8549 return r;
8552 return isl_bool_false;
8555 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8557 return isl_map_plain_is_universe(set_to_map(set));
8560 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8562 struct isl_basic_set *bset = NULL;
8563 struct isl_vec *sample = NULL;
8564 isl_bool empty, non_empty;
8566 if (!bmap)
8567 return isl_bool_error;
8569 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8570 return isl_bool_true;
8572 if (isl_basic_map_plain_is_universe(bmap))
8573 return isl_bool_false;
8575 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8576 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8577 copy = isl_basic_map_remove_redundancies(copy);
8578 empty = isl_basic_map_plain_is_empty(copy);
8579 isl_basic_map_free(copy);
8580 return empty;
8583 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8584 if (non_empty < 0)
8585 return isl_bool_error;
8586 if (non_empty)
8587 return isl_bool_false;
8588 isl_vec_free(bmap->sample);
8589 bmap->sample = NULL;
8590 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8591 if (!bset)
8592 return isl_bool_error;
8593 sample = isl_basic_set_sample_vec(bset);
8594 if (!sample)
8595 return isl_bool_error;
8596 empty = sample->size == 0;
8597 isl_vec_free(bmap->sample);
8598 bmap->sample = sample;
8599 if (empty)
8600 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8602 return empty;
8605 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8607 if (!bmap)
8608 return isl_bool_error;
8609 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8612 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8614 if (!bset)
8615 return isl_bool_error;
8616 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8619 /* Is "bmap" known to be non-empty?
8621 * That is, is the cached sample still valid?
8623 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8625 unsigned total;
8627 if (!bmap)
8628 return isl_bool_error;
8629 if (!bmap->sample)
8630 return isl_bool_false;
8631 total = 1 + isl_basic_map_total_dim(bmap);
8632 if (bmap->sample->size != total)
8633 return isl_bool_false;
8634 return isl_basic_map_contains(bmap, bmap->sample);
8637 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8639 return isl_basic_map_is_empty(bset_to_bmap(bset));
8642 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8643 __isl_take isl_basic_map *bmap2)
8645 struct isl_map *map;
8646 if (!bmap1 || !bmap2)
8647 goto error;
8649 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8651 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8652 if (!map)
8653 goto error;
8654 map = isl_map_add_basic_map(map, bmap1);
8655 map = isl_map_add_basic_map(map, bmap2);
8656 return map;
8657 error:
8658 isl_basic_map_free(bmap1);
8659 isl_basic_map_free(bmap2);
8660 return NULL;
8663 struct isl_set *isl_basic_set_union(
8664 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8666 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8667 bset_to_bmap(bset2)));
8670 /* Order divs such that any div only depends on previous divs */
8671 __isl_give isl_basic_map *isl_basic_map_order_divs(
8672 __isl_take isl_basic_map *bmap)
8674 int i;
8675 int off;
8677 off = isl_basic_map_var_offset(bmap, isl_dim_div);
8678 if (off < 0)
8679 return isl_basic_map_free(bmap);
8681 for (i = 0; i < bmap->n_div; ++i) {
8682 int pos;
8683 if (isl_int_is_zero(bmap->div[i][0]))
8684 continue;
8685 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8686 bmap->n_div-i);
8687 if (pos == -1)
8688 continue;
8689 if (pos == 0)
8690 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8691 "integer division depends on itself",
8692 return isl_basic_map_free(bmap));
8693 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8694 if (!bmap)
8695 return NULL;
8696 --i;
8698 return bmap;
8701 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8703 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8706 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8708 int i;
8710 if (!map)
8711 return 0;
8713 for (i = 0; i < map->n; ++i) {
8714 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8715 if (!map->p[i])
8716 goto error;
8719 return map;
8720 error:
8721 isl_map_free(map);
8722 return NULL;
8725 /* Sort the local variables of "bset".
8727 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8728 __isl_take isl_basic_set *bset)
8730 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8733 /* Apply the expansion computed by isl_merge_divs.
8734 * The expansion itself is given by "exp" while the resulting
8735 * list of divs is given by "div".
8737 * Move the integer divisions of "bmap" into the right position
8738 * according to "exp" and then introduce the additional integer
8739 * divisions, adding div constraints.
8740 * The moving should be done first to avoid moving coefficients
8741 * in the definitions of the extra integer divisions.
8743 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8744 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8746 int i, j;
8747 int n_div;
8749 bmap = isl_basic_map_cow(bmap);
8750 if (!bmap || !div)
8751 goto error;
8753 if (div->n_row < bmap->n_div)
8754 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8755 "not an expansion", goto error);
8757 n_div = bmap->n_div;
8758 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8759 div->n_row - n_div, 0,
8760 2 * (div->n_row - n_div));
8762 for (i = n_div; i < div->n_row; ++i)
8763 if (isl_basic_map_alloc_div(bmap) < 0)
8764 goto error;
8766 for (j = n_div - 1; j >= 0; --j) {
8767 if (exp[j] == j)
8768 break;
8769 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8770 if (!bmap)
8771 goto error;
8773 j = 0;
8774 for (i = 0; i < div->n_row; ++i) {
8775 if (j < n_div && exp[j] == i) {
8776 j++;
8777 } else {
8778 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8779 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8780 continue;
8781 bmap = isl_basic_map_add_div_constraints(bmap, i);
8782 if (!bmap)
8783 goto error;
8787 isl_mat_free(div);
8788 return bmap;
8789 error:
8790 isl_basic_map_free(bmap);
8791 isl_mat_free(div);
8792 return NULL;
8795 /* Apply the expansion computed by isl_merge_divs.
8796 * The expansion itself is given by "exp" while the resulting
8797 * list of divs is given by "div".
8799 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8800 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8802 return isl_basic_map_expand_divs(bset, div, exp);
8805 /* Look for a div in dst that corresponds to the div "div" in src.
8806 * The divs before "div" in src and dst are assumed to be the same.
8808 * Return the position of the corresponding div in dst
8809 * if there is one. Otherwise, return a position beyond the integer divisions.
8810 * Return -1 on error.
8812 static int find_div(__isl_keep isl_basic_map *dst,
8813 __isl_keep isl_basic_map *src, unsigned div)
8815 int i;
8816 unsigned n_div;
8817 int v_div;
8819 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8820 if (!dst || v_div < 0)
8821 return -1;
8823 n_div = isl_basic_map_dim(dst, isl_dim_div);
8824 isl_assert(dst->ctx, div <= n_div, return -1);
8825 for (i = div; i < n_div; ++i)
8826 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
8827 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
8828 n_div - div) == -1)
8829 return i;
8830 return n_div;
8833 /* Align the divs of "dst" to those of "src", adding divs from "src"
8834 * if needed. That is, make sure that the first src->n_div divs
8835 * of the result are equal to those of src.
8837 * The result is not finalized as by design it will have redundant
8838 * divs if any divs from "src" were copied.
8840 __isl_give isl_basic_map *isl_basic_map_align_divs(
8841 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8843 int i;
8844 isl_bool known;
8845 int extended;
8846 int v_div;
8847 unsigned dst_n_div;
8849 if (!dst || !src)
8850 return isl_basic_map_free(dst);
8852 if (src->n_div == 0)
8853 return dst;
8855 known = isl_basic_map_divs_known(src);
8856 if (known < 0)
8857 return isl_basic_map_free(dst);
8858 if (!known)
8859 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8860 "some src divs are unknown",
8861 return isl_basic_map_free(dst));
8863 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8864 if (v_div < 0)
8865 return isl_basic_map_free(dst);
8867 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8868 if (!src)
8869 return isl_basic_map_free(dst);
8871 extended = 0;
8872 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
8873 for (i = 0; i < src->n_div; ++i) {
8874 int j = find_div(dst, src, i);
8875 if (j < 0)
8876 dst = isl_basic_map_free(dst);
8877 if (j == dst_n_div) {
8878 if (!extended) {
8879 int extra = src->n_div - i;
8880 dst = isl_basic_map_cow(dst);
8881 if (!dst)
8882 goto error;
8883 dst = isl_basic_map_extend_space(dst,
8884 isl_space_copy(dst->dim),
8885 extra, 0, 2 * extra);
8886 extended = 1;
8888 j = isl_basic_map_alloc_div(dst);
8889 if (j < 0)
8890 goto error;
8891 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
8892 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
8893 dst_n_div++;
8894 dst = isl_basic_map_add_div_constraints(dst, j);
8895 if (!dst)
8896 goto error;
8898 if (j != i)
8899 dst = isl_basic_map_swap_div(dst, i, j);
8900 if (!dst)
8901 goto error;
8903 isl_basic_map_free(src);
8904 return dst;
8905 error:
8906 isl_basic_map_free(src);
8907 isl_basic_map_free(dst);
8908 return NULL;
8911 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8913 int i;
8915 if (!map)
8916 return NULL;
8917 if (map->n == 0)
8918 return map;
8919 map = isl_map_compute_divs(map);
8920 map = isl_map_cow(map);
8921 if (!map)
8922 return NULL;
8924 for (i = 1; i < map->n; ++i)
8925 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8926 for (i = 1; i < map->n; ++i) {
8927 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8928 if (!map->p[i])
8929 return isl_map_free(map);
8932 map = isl_map_unmark_normalized(map);
8933 return map;
8936 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8938 return isl_map_align_divs_internal(map);
8941 struct isl_set *isl_set_align_divs(struct isl_set *set)
8943 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8946 /* Align the divs of the basic maps in "map" to those
8947 * of the basic maps in "list", as well as to the other basic maps in "map".
8948 * The elements in "list" are assumed to have known divs.
8950 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8951 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8953 int i, n;
8955 map = isl_map_compute_divs(map);
8956 map = isl_map_cow(map);
8957 if (!map || !list)
8958 return isl_map_free(map);
8959 if (map->n == 0)
8960 return map;
8962 n = isl_basic_map_list_n_basic_map(list);
8963 for (i = 0; i < n; ++i) {
8964 isl_basic_map *bmap;
8966 bmap = isl_basic_map_list_get_basic_map(list, i);
8967 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8968 isl_basic_map_free(bmap);
8970 if (!map->p[0])
8971 return isl_map_free(map);
8973 return isl_map_align_divs_internal(map);
8976 /* Align the divs of each element of "list" to those of "bmap".
8977 * Both "bmap" and the elements of "list" are assumed to have known divs.
8979 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8980 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8982 int i, n;
8984 if (!list || !bmap)
8985 return isl_basic_map_list_free(list);
8987 n = isl_basic_map_list_n_basic_map(list);
8988 for (i = 0; i < n; ++i) {
8989 isl_basic_map *bmap_i;
8991 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8992 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8993 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8996 return list;
8999 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9000 __isl_take isl_map *map)
9002 isl_bool ok;
9004 ok = isl_map_compatible_domain(map, set);
9005 if (ok < 0)
9006 goto error;
9007 if (!ok)
9008 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9009 "incompatible spaces", goto error);
9010 map = isl_map_intersect_domain(map, set);
9011 set = isl_map_range(map);
9012 return set;
9013 error:
9014 isl_set_free(set);
9015 isl_map_free(map);
9016 return NULL;
9019 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9020 __isl_take isl_map *map)
9022 return isl_map_align_params_map_map_and(set, map, &set_apply);
9025 /* There is no need to cow as removing empty parts doesn't change
9026 * the meaning of the set.
9028 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9030 int i;
9032 if (!map)
9033 return NULL;
9035 for (i = map->n - 1; i >= 0; --i)
9036 map = remove_if_empty(map, i);
9038 return map;
9041 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9043 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9046 /* Create a binary relation that maps the shared initial "pos" dimensions
9047 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9049 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9050 __isl_keep isl_basic_set *bset2, int pos)
9052 isl_basic_map *bmap1;
9053 isl_basic_map *bmap2;
9055 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9056 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9057 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9058 isl_dim_out, 0, pos);
9059 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9060 isl_dim_out, 0, pos);
9061 return isl_basic_map_range_product(bmap1, bmap2);
9064 /* Given two basic sets bset1 and bset2, compute the maximal difference
9065 * between the values of dimension pos in bset1 and those in bset2
9066 * for any common value of the parameters and dimensions preceding pos.
9068 static enum isl_lp_result basic_set_maximal_difference_at(
9069 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9070 int pos, isl_int *opt)
9072 isl_basic_map *bmap1;
9073 struct isl_ctx *ctx;
9074 struct isl_vec *obj;
9075 unsigned total;
9076 unsigned nparam;
9077 unsigned dim1;
9078 enum isl_lp_result res;
9080 if (!bset1 || !bset2)
9081 return isl_lp_error;
9083 nparam = isl_basic_set_n_param(bset1);
9084 dim1 = isl_basic_set_n_dim(bset1);
9086 bmap1 = join_initial(bset1, bset2, pos);
9087 if (!bmap1)
9088 return isl_lp_error;
9090 total = isl_basic_map_total_dim(bmap1);
9091 ctx = bmap1->ctx;
9092 obj = isl_vec_alloc(ctx, 1 + total);
9093 if (!obj)
9094 goto error;
9095 isl_seq_clr(obj->block.data, 1 + total);
9096 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9097 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9098 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9099 opt, NULL, NULL);
9100 isl_basic_map_free(bmap1);
9101 isl_vec_free(obj);
9102 return res;
9103 error:
9104 isl_basic_map_free(bmap1);
9105 return isl_lp_error;
9108 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9109 * for any common value of the parameters and dimensions preceding pos
9110 * in both basic sets, the values of dimension pos in bset1 are
9111 * smaller or larger than those in bset2.
9113 * Returns
9114 * 1 if bset1 follows bset2
9115 * -1 if bset1 precedes bset2
9116 * 0 if bset1 and bset2 are incomparable
9117 * -2 if some error occurred.
9119 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9120 __isl_keep isl_basic_set *bset2, int pos)
9122 isl_int opt;
9123 enum isl_lp_result res;
9124 int cmp;
9126 isl_int_init(opt);
9128 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9130 if (res == isl_lp_empty)
9131 cmp = 0;
9132 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9133 res == isl_lp_unbounded)
9134 cmp = 1;
9135 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9136 cmp = -1;
9137 else
9138 cmp = -2;
9140 isl_int_clear(opt);
9141 return cmp;
9144 /* Given two basic sets bset1 and bset2, check whether
9145 * for any common value of the parameters and dimensions preceding pos
9146 * there is a value of dimension pos in bset1 that is larger
9147 * than a value of the same dimension in bset2.
9149 * Return
9150 * 1 if there exists such a pair
9151 * 0 if there is no such pair, but there is a pair of equal values
9152 * -1 otherwise
9153 * -2 if some error occurred.
9155 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9156 __isl_keep isl_basic_set *bset2, int pos)
9158 isl_bool empty;
9159 isl_basic_map *bmap;
9160 unsigned dim1;
9162 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9163 bmap = join_initial(bset1, bset2, pos);
9164 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9165 isl_dim_out, dim1 - pos);
9166 empty = isl_basic_map_is_empty(bmap);
9167 if (empty < 0)
9168 goto error;
9169 if (empty) {
9170 isl_basic_map_free(bmap);
9171 return -1;
9173 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9174 isl_dim_out, dim1 - pos);
9175 empty = isl_basic_map_is_empty(bmap);
9176 if (empty < 0)
9177 goto error;
9178 isl_basic_map_free(bmap);
9179 if (empty)
9180 return 0;
9181 return 1;
9182 error:
9183 isl_basic_map_free(bmap);
9184 return -2;
9187 /* Given two sets set1 and set2, check whether
9188 * for any common value of the parameters and dimensions preceding pos
9189 * there is a value of dimension pos in set1 that is larger
9190 * than a value of the same dimension in set2.
9192 * Return
9193 * 1 if there exists such a pair
9194 * 0 if there is no such pair, but there is a pair of equal values
9195 * -1 otherwise
9196 * -2 if some error occurred.
9198 int isl_set_follows_at(__isl_keep isl_set *set1,
9199 __isl_keep isl_set *set2, int pos)
9201 int i, j;
9202 int follows = -1;
9204 if (!set1 || !set2)
9205 return -2;
9207 for (i = 0; i < set1->n; ++i)
9208 for (j = 0; j < set2->n; ++j) {
9209 int f;
9210 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9211 if (f == 1 || f == -2)
9212 return f;
9213 if (f > follows)
9214 follows = f;
9217 return follows;
9220 static isl_bool isl_basic_map_plain_has_fixed_var(
9221 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9223 int i;
9224 int d;
9225 unsigned total;
9227 if (!bmap)
9228 return isl_bool_error;
9229 total = isl_basic_map_total_dim(bmap);
9230 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9231 for (; d+1 > pos; --d)
9232 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9233 break;
9234 if (d != pos)
9235 continue;
9236 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9237 return isl_bool_false;
9238 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9239 return isl_bool_false;
9240 if (!isl_int_is_one(bmap->eq[i][1+d]))
9241 return isl_bool_false;
9242 if (val)
9243 isl_int_neg(*val, bmap->eq[i][0]);
9244 return isl_bool_true;
9246 return isl_bool_false;
9249 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9250 unsigned pos, isl_int *val)
9252 int i;
9253 isl_int v;
9254 isl_int tmp;
9255 isl_bool fixed;
9257 if (!map)
9258 return isl_bool_error;
9259 if (map->n == 0)
9260 return isl_bool_false;
9261 if (map->n == 1)
9262 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9263 isl_int_init(v);
9264 isl_int_init(tmp);
9265 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9266 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9267 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9268 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9269 fixed = isl_bool_false;
9271 if (val)
9272 isl_int_set(*val, v);
9273 isl_int_clear(tmp);
9274 isl_int_clear(v);
9275 return fixed;
9278 static isl_bool isl_basic_set_plain_has_fixed_var(
9279 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9281 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9282 pos, val);
9285 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9286 enum isl_dim_type type, unsigned pos, isl_int *val)
9288 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9289 return isl_bool_error;
9290 return isl_basic_map_plain_has_fixed_var(bmap,
9291 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9294 /* If "bmap" obviously lies on a hyperplane where the given dimension
9295 * has a fixed value, then return that value.
9296 * Otherwise return NaN.
9298 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9299 __isl_keep isl_basic_map *bmap,
9300 enum isl_dim_type type, unsigned pos)
9302 isl_ctx *ctx;
9303 isl_val *v;
9304 isl_bool fixed;
9306 if (!bmap)
9307 return NULL;
9308 ctx = isl_basic_map_get_ctx(bmap);
9309 v = isl_val_alloc(ctx);
9310 if (!v)
9311 return NULL;
9312 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9313 if (fixed < 0)
9314 return isl_val_free(v);
9315 if (fixed) {
9316 isl_int_set_si(v->d, 1);
9317 return v;
9319 isl_val_free(v);
9320 return isl_val_nan(ctx);
9323 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9324 enum isl_dim_type type, unsigned pos, isl_int *val)
9326 if (isl_map_check_range(map, type, pos, 1) < 0)
9327 return isl_bool_error;
9328 return isl_map_plain_has_fixed_var(map,
9329 map_offset(map, type) - 1 + pos, val);
9332 /* If "map" obviously lies on a hyperplane where the given dimension
9333 * has a fixed value, then return that value.
9334 * Otherwise return NaN.
9336 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9337 enum isl_dim_type type, unsigned pos)
9339 isl_ctx *ctx;
9340 isl_val *v;
9341 isl_bool fixed;
9343 if (!map)
9344 return NULL;
9345 ctx = isl_map_get_ctx(map);
9346 v = isl_val_alloc(ctx);
9347 if (!v)
9348 return NULL;
9349 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9350 if (fixed < 0)
9351 return isl_val_free(v);
9352 if (fixed) {
9353 isl_int_set_si(v->d, 1);
9354 return v;
9356 isl_val_free(v);
9357 return isl_val_nan(ctx);
9360 /* If "set" obviously lies on a hyperplane where the given dimension
9361 * has a fixed value, then return that value.
9362 * Otherwise return NaN.
9364 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9365 enum isl_dim_type type, unsigned pos)
9367 return isl_map_plain_get_val_if_fixed(set, type, pos);
9370 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9371 * then return this fixed value in *val.
9373 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9374 unsigned dim, isl_int *val)
9376 return isl_basic_set_plain_has_fixed_var(bset,
9377 isl_basic_set_n_param(bset) + dim, val);
9380 /* Return -1 if the constraint "c1" should be sorted before "c2"
9381 * and 1 if it should be sorted after "c2".
9382 * Return 0 if the two constraints are the same (up to the constant term).
9384 * In particular, if a constraint involves later variables than another
9385 * then it is sorted after this other constraint.
9386 * uset_gist depends on constraints without existentially quantified
9387 * variables sorting first.
9389 * For constraints that have the same latest variable, those
9390 * with the same coefficient for this latest variable (first in absolute value
9391 * and then in actual value) are grouped together.
9392 * This is useful for detecting pairs of constraints that can
9393 * be chained in their printed representation.
9395 * Finally, within a group, constraints are sorted according to
9396 * their coefficients (excluding the constant term).
9398 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9400 isl_int **c1 = (isl_int **) p1;
9401 isl_int **c2 = (isl_int **) p2;
9402 int l1, l2;
9403 unsigned size = *(unsigned *) arg;
9404 int cmp;
9406 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9407 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9409 if (l1 != l2)
9410 return l1 - l2;
9412 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9413 if (cmp != 0)
9414 return cmp;
9415 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9416 if (cmp != 0)
9417 return -cmp;
9419 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9422 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9423 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9424 * and 0 if the two constraints are the same (up to the constant term).
9426 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9427 isl_int *c1, isl_int *c2)
9429 unsigned total;
9431 if (!bmap)
9432 return -2;
9433 total = isl_basic_map_total_dim(bmap);
9434 return sort_constraint_cmp(&c1, &c2, &total);
9437 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9438 __isl_take isl_basic_map *bmap)
9440 unsigned total;
9442 if (!bmap)
9443 return NULL;
9444 if (bmap->n_ineq == 0)
9445 return bmap;
9446 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9447 return bmap;
9448 total = isl_basic_map_total_dim(bmap);
9449 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9450 &sort_constraint_cmp, &total) < 0)
9451 return isl_basic_map_free(bmap);
9452 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9453 return bmap;
9456 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9457 __isl_take isl_basic_set *bset)
9459 isl_basic_map *bmap = bset_to_bmap(bset);
9460 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9463 __isl_give isl_basic_map *isl_basic_map_normalize(
9464 __isl_take isl_basic_map *bmap)
9466 bmap = isl_basic_map_remove_redundancies(bmap);
9467 bmap = isl_basic_map_sort_constraints(bmap);
9468 return bmap;
9470 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9471 __isl_keep isl_basic_map *bmap2)
9473 int i, cmp;
9474 unsigned total;
9475 isl_space *space1, *space2;
9477 if (!bmap1 || !bmap2)
9478 return -1;
9480 if (bmap1 == bmap2)
9481 return 0;
9482 space1 = isl_basic_map_peek_space(bmap1);
9483 space2 = isl_basic_map_peek_space(bmap2);
9484 cmp = isl_space_cmp(space1, space2);
9485 if (cmp)
9486 return cmp;
9487 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9488 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9489 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9490 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9491 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9492 return 0;
9493 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9494 return 1;
9495 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9496 return -1;
9497 if (bmap1->n_eq != bmap2->n_eq)
9498 return bmap1->n_eq - bmap2->n_eq;
9499 if (bmap1->n_ineq != bmap2->n_ineq)
9500 return bmap1->n_ineq - bmap2->n_ineq;
9501 if (bmap1->n_div != bmap2->n_div)
9502 return bmap1->n_div - bmap2->n_div;
9503 total = isl_basic_map_total_dim(bmap1);
9504 for (i = 0; i < bmap1->n_eq; ++i) {
9505 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9506 if (cmp)
9507 return cmp;
9509 for (i = 0; i < bmap1->n_ineq; ++i) {
9510 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9511 if (cmp)
9512 return cmp;
9514 for (i = 0; i < bmap1->n_div; ++i) {
9515 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9516 if (cmp)
9517 return cmp;
9519 return 0;
9522 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9523 __isl_keep isl_basic_set *bset2)
9525 return isl_basic_map_plain_cmp(bset1, bset2);
9528 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9530 int i, cmp;
9532 if (set1 == set2)
9533 return 0;
9534 if (set1->n != set2->n)
9535 return set1->n - set2->n;
9537 for (i = 0; i < set1->n; ++i) {
9538 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9539 if (cmp)
9540 return cmp;
9543 return 0;
9546 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9547 __isl_keep isl_basic_map *bmap2)
9549 if (!bmap1 || !bmap2)
9550 return isl_bool_error;
9551 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9554 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9555 __isl_keep isl_basic_set *bset2)
9557 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9558 bset_to_bmap(bset2));
9561 static int qsort_bmap_cmp(const void *p1, const void *p2)
9563 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9564 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9566 return isl_basic_map_plain_cmp(bmap1, bmap2);
9569 /* Sort the basic maps of "map" and remove duplicate basic maps.
9571 * While removing basic maps, we make sure that the basic maps remain
9572 * sorted because isl_map_normalize expects the basic maps of the result
9573 * to be sorted.
9575 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9577 int i, j;
9579 map = isl_map_remove_empty_parts(map);
9580 if (!map)
9581 return NULL;
9582 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9583 for (i = map->n - 1; i >= 1; --i) {
9584 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9585 continue;
9586 isl_basic_map_free(map->p[i-1]);
9587 for (j = i; j < map->n; ++j)
9588 map->p[j - 1] = map->p[j];
9589 map->n--;
9592 return map;
9595 /* Remove obvious duplicates among the basic maps of "map".
9597 * Unlike isl_map_normalize, this function does not remove redundant
9598 * constraints and only removes duplicates that have exactly the same
9599 * constraints in the input. It does sort the constraints and
9600 * the basic maps to ease the detection of duplicates.
9602 * If "map" has already been normalized or if the basic maps are
9603 * disjoint, then there can be no duplicates.
9605 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9607 int i;
9608 isl_basic_map *bmap;
9610 if (!map)
9611 return NULL;
9612 if (map->n <= 1)
9613 return map;
9614 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9615 return map;
9616 for (i = 0; i < map->n; ++i) {
9617 bmap = isl_basic_map_copy(map->p[i]);
9618 bmap = isl_basic_map_sort_constraints(bmap);
9619 if (!bmap)
9620 return isl_map_free(map);
9621 isl_basic_map_free(map->p[i]);
9622 map->p[i] = bmap;
9625 map = sort_and_remove_duplicates(map);
9626 return map;
9629 /* We normalize in place, but if anything goes wrong we need
9630 * to return NULL, so we need to make sure we don't change the
9631 * meaning of any possible other copies of map.
9633 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9635 int i;
9636 struct isl_basic_map *bmap;
9638 if (!map)
9639 return NULL;
9640 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9641 return map;
9642 for (i = 0; i < map->n; ++i) {
9643 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9644 if (!bmap)
9645 goto error;
9646 isl_basic_map_free(map->p[i]);
9647 map->p[i] = bmap;
9650 map = sort_and_remove_duplicates(map);
9651 if (map)
9652 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9653 return map;
9654 error:
9655 isl_map_free(map);
9656 return NULL;
9659 struct isl_set *isl_set_normalize(struct isl_set *set)
9661 return set_from_map(isl_map_normalize(set_to_map(set)));
9664 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9665 __isl_keep isl_map *map2)
9667 int i;
9668 isl_bool equal;
9670 if (!map1 || !map2)
9671 return isl_bool_error;
9673 if (map1 == map2)
9674 return isl_bool_true;
9675 if (!isl_space_is_equal(map1->dim, map2->dim))
9676 return isl_bool_false;
9678 map1 = isl_map_copy(map1);
9679 map2 = isl_map_copy(map2);
9680 map1 = isl_map_normalize(map1);
9681 map2 = isl_map_normalize(map2);
9682 if (!map1 || !map2)
9683 goto error;
9684 equal = map1->n == map2->n;
9685 for (i = 0; equal && i < map1->n; ++i) {
9686 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9687 if (equal < 0)
9688 goto error;
9690 isl_map_free(map1);
9691 isl_map_free(map2);
9692 return equal;
9693 error:
9694 isl_map_free(map1);
9695 isl_map_free(map2);
9696 return isl_bool_error;
9699 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9700 __isl_keep isl_set *set2)
9702 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9705 /* Return the basic maps in "map" as a list.
9707 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9708 __isl_keep isl_map *map)
9710 int i;
9711 isl_ctx *ctx;
9712 isl_basic_map_list *list;
9714 if (!map)
9715 return NULL;
9716 ctx = isl_map_get_ctx(map);
9717 list = isl_basic_map_list_alloc(ctx, map->n);
9719 for (i = 0; i < map->n; ++i) {
9720 isl_basic_map *bmap;
9722 bmap = isl_basic_map_copy(map->p[i]);
9723 list = isl_basic_map_list_add(list, bmap);
9726 return list;
9729 /* Return the intersection of the elements in the non-empty list "list".
9730 * All elements are assumed to live in the same space.
9732 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9733 __isl_take isl_basic_map_list *list)
9735 int i, n;
9736 isl_basic_map *bmap;
9738 if (!list)
9739 return NULL;
9740 n = isl_basic_map_list_n_basic_map(list);
9741 if (n < 1)
9742 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9743 "expecting non-empty list", goto error);
9745 bmap = isl_basic_map_list_get_basic_map(list, 0);
9746 for (i = 1; i < n; ++i) {
9747 isl_basic_map *bmap_i;
9749 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9750 bmap = isl_basic_map_intersect(bmap, bmap_i);
9753 isl_basic_map_list_free(list);
9754 return bmap;
9755 error:
9756 isl_basic_map_list_free(list);
9757 return NULL;
9760 /* Return the intersection of the elements in the non-empty list "list".
9761 * All elements are assumed to live in the same space.
9763 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9764 __isl_take isl_basic_set_list *list)
9766 return isl_basic_map_list_intersect(list);
9769 /* Return the union of the elements of "list".
9770 * The list is required to have at least one element.
9772 __isl_give isl_set *isl_basic_set_list_union(
9773 __isl_take isl_basic_set_list *list)
9775 int i, n;
9776 isl_space *space;
9777 isl_basic_set *bset;
9778 isl_set *set;
9780 if (!list)
9781 return NULL;
9782 n = isl_basic_set_list_n_basic_set(list);
9783 if (n < 1)
9784 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9785 "expecting non-empty list", goto error);
9787 bset = isl_basic_set_list_get_basic_set(list, 0);
9788 space = isl_basic_set_get_space(bset);
9789 isl_basic_set_free(bset);
9791 set = isl_set_alloc_space(space, n, 0);
9792 for (i = 0; i < n; ++i) {
9793 bset = isl_basic_set_list_get_basic_set(list, i);
9794 set = isl_set_add_basic_set(set, bset);
9797 isl_basic_set_list_free(list);
9798 return set;
9799 error:
9800 isl_basic_set_list_free(list);
9801 return NULL;
9804 /* Return the union of the elements in the non-empty list "list".
9805 * All elements are assumed to live in the same space.
9807 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9809 int i, n;
9810 isl_set *set;
9812 if (!list)
9813 return NULL;
9814 n = isl_set_list_n_set(list);
9815 if (n < 1)
9816 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9817 "expecting non-empty list", goto error);
9819 set = isl_set_list_get_set(list, 0);
9820 for (i = 1; i < n; ++i) {
9821 isl_set *set_i;
9823 set_i = isl_set_list_get_set(list, i);
9824 set = isl_set_union(set, set_i);
9827 isl_set_list_free(list);
9828 return set;
9829 error:
9830 isl_set_list_free(list);
9831 return NULL;
9834 __isl_give isl_basic_map *isl_basic_map_product(
9835 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9837 isl_space *space_result = NULL;
9838 struct isl_basic_map *bmap;
9839 unsigned in1, in2, out1, out2, nparam, total, pos;
9840 struct isl_dim_map *dim_map1, *dim_map2;
9842 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9843 goto error;
9844 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9845 isl_space_copy(bmap2->dim));
9847 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9848 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9849 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9850 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9851 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9853 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9854 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9855 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9856 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9857 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9858 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9859 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9860 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9861 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9862 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9863 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9865 bmap = isl_basic_map_alloc_space(space_result,
9866 bmap1->n_div + bmap2->n_div,
9867 bmap1->n_eq + bmap2->n_eq,
9868 bmap1->n_ineq + bmap2->n_ineq);
9869 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9870 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9871 bmap = isl_basic_map_simplify(bmap);
9872 return isl_basic_map_finalize(bmap);
9873 error:
9874 isl_basic_map_free(bmap1);
9875 isl_basic_map_free(bmap2);
9876 return NULL;
9879 __isl_give isl_basic_map *isl_basic_map_flat_product(
9880 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9882 isl_basic_map *prod;
9884 prod = isl_basic_map_product(bmap1, bmap2);
9885 prod = isl_basic_map_flatten(prod);
9886 return prod;
9889 __isl_give isl_basic_set *isl_basic_set_flat_product(
9890 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9892 return isl_basic_map_flat_range_product(bset1, bset2);
9895 __isl_give isl_basic_map *isl_basic_map_domain_product(
9896 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9898 isl_space *space1, *space2;
9899 isl_space *space_result = NULL;
9900 isl_basic_map *bmap;
9901 unsigned in1, in2, out, nparam, total, pos;
9902 struct isl_dim_map *dim_map1, *dim_map2;
9904 if (!bmap1 || !bmap2)
9905 goto error;
9907 space1 = isl_basic_map_get_space(bmap1);
9908 space2 = isl_basic_map_get_space(bmap2);
9909 space_result = isl_space_domain_product(space1, space2);
9911 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9912 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9913 out = isl_basic_map_dim(bmap1, isl_dim_out);
9914 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9916 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9917 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9918 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9919 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9920 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9921 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9922 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9923 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9924 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9925 isl_dim_map_div(dim_map1, bmap1, pos += out);
9926 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9928 bmap = isl_basic_map_alloc_space(space_result,
9929 bmap1->n_div + bmap2->n_div,
9930 bmap1->n_eq + bmap2->n_eq,
9931 bmap1->n_ineq + bmap2->n_ineq);
9932 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9933 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9934 bmap = isl_basic_map_simplify(bmap);
9935 return isl_basic_map_finalize(bmap);
9936 error:
9937 isl_basic_map_free(bmap1);
9938 isl_basic_map_free(bmap2);
9939 return NULL;
9942 __isl_give isl_basic_map *isl_basic_map_range_product(
9943 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9945 isl_bool rational;
9946 isl_space *space_result = NULL;
9947 isl_basic_map *bmap;
9948 unsigned in, out1, out2, nparam, total, pos;
9949 struct isl_dim_map *dim_map1, *dim_map2;
9951 rational = isl_basic_map_is_rational(bmap1);
9952 if (rational >= 0 && rational)
9953 rational = isl_basic_map_is_rational(bmap2);
9954 if (!bmap1 || !bmap2 || rational < 0)
9955 goto error;
9957 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9958 goto error;
9960 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9961 isl_space_copy(bmap2->dim));
9963 in = isl_basic_map_dim(bmap1, isl_dim_in);
9964 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9965 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9966 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9968 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9969 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9970 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9971 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9972 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9973 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9974 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9975 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9976 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9977 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9978 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9980 bmap = isl_basic_map_alloc_space(space_result,
9981 bmap1->n_div + bmap2->n_div,
9982 bmap1->n_eq + bmap2->n_eq,
9983 bmap1->n_ineq + bmap2->n_ineq);
9984 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9985 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9986 if (rational)
9987 bmap = isl_basic_map_set_rational(bmap);
9988 bmap = isl_basic_map_simplify(bmap);
9989 return isl_basic_map_finalize(bmap);
9990 error:
9991 isl_basic_map_free(bmap1);
9992 isl_basic_map_free(bmap2);
9993 return NULL;
9996 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9997 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9999 isl_basic_map *prod;
10001 prod = isl_basic_map_range_product(bmap1, bmap2);
10002 prod = isl_basic_map_flatten_range(prod);
10003 return prod;
10006 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10007 * and collect the results.
10008 * The result live in the space obtained by calling "space_product"
10009 * on the spaces of "map1" and "map2".
10010 * If "remove_duplicates" is set then the result may contain duplicates
10011 * (even if the inputs do not) and so we try and remove the obvious
10012 * duplicates.
10014 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10015 __isl_take isl_map *map2,
10016 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10017 __isl_take isl_space *right),
10018 __isl_give isl_basic_map *(*basic_map_product)(
10019 __isl_take isl_basic_map *left,
10020 __isl_take isl_basic_map *right),
10021 int remove_duplicates)
10023 unsigned flags = 0;
10024 struct isl_map *result;
10025 int i, j;
10026 isl_bool m;
10028 m = isl_map_has_equal_params(map1, map2);
10029 if (m < 0)
10030 goto error;
10031 if (!m)
10032 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10033 "parameters don't match", goto error);
10035 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10036 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10037 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10039 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10040 isl_space_copy(map2->dim)),
10041 map1->n * map2->n, flags);
10042 if (!result)
10043 goto error;
10044 for (i = 0; i < map1->n; ++i)
10045 for (j = 0; j < map2->n; ++j) {
10046 struct isl_basic_map *part;
10047 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10048 isl_basic_map_copy(map2->p[j]));
10049 if (isl_basic_map_is_empty(part))
10050 isl_basic_map_free(part);
10051 else
10052 result = isl_map_add_basic_map(result, part);
10053 if (!result)
10054 goto error;
10056 if (remove_duplicates)
10057 result = isl_map_remove_obvious_duplicates(result);
10058 isl_map_free(map1);
10059 isl_map_free(map2);
10060 return result;
10061 error:
10062 isl_map_free(map1);
10063 isl_map_free(map2);
10064 return NULL;
10067 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10069 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10070 __isl_take isl_map *map2)
10072 return map_product(map1, map2, &isl_space_product,
10073 &isl_basic_map_product, 0);
10076 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10077 __isl_take isl_map *map2)
10079 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10082 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10084 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10085 __isl_take isl_map *map2)
10087 isl_map *prod;
10089 prod = isl_map_product(map1, map2);
10090 prod = isl_map_flatten(prod);
10091 return prod;
10094 /* Given two set A and B, construct its Cartesian product A x B.
10096 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10098 return isl_map_range_product(set1, set2);
10101 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10102 __isl_take isl_set *set2)
10104 return isl_map_flat_range_product(set1, set2);
10107 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10109 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10110 __isl_take isl_map *map2)
10112 return map_product(map1, map2, &isl_space_domain_product,
10113 &isl_basic_map_domain_product, 1);
10116 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10118 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10119 __isl_take isl_map *map2)
10121 return map_product(map1, map2, &isl_space_range_product,
10122 &isl_basic_map_range_product, 1);
10125 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10126 __isl_take isl_map *map2)
10128 return isl_map_align_params_map_map_and(map1, map2,
10129 &map_domain_product_aligned);
10132 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10133 __isl_take isl_map *map2)
10135 return isl_map_align_params_map_map_and(map1, map2,
10136 &map_range_product_aligned);
10139 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10141 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10143 isl_space *space;
10144 int total1, keep1, total2, keep2;
10146 if (!map)
10147 return NULL;
10148 if (!isl_space_domain_is_wrapping(map->dim) ||
10149 !isl_space_range_is_wrapping(map->dim))
10150 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10151 "not a product", return isl_map_free(map));
10153 space = isl_map_get_space(map);
10154 total1 = isl_space_dim(space, isl_dim_in);
10155 total2 = isl_space_dim(space, isl_dim_out);
10156 space = isl_space_factor_domain(space);
10157 keep1 = isl_space_dim(space, isl_dim_in);
10158 keep2 = isl_space_dim(space, isl_dim_out);
10159 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10160 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10161 map = isl_map_reset_space(map, space);
10163 return map;
10166 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10168 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10170 isl_space *space;
10171 int total1, keep1, total2, keep2;
10173 if (!map)
10174 return NULL;
10175 if (!isl_space_domain_is_wrapping(map->dim) ||
10176 !isl_space_range_is_wrapping(map->dim))
10177 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10178 "not a product", return isl_map_free(map));
10180 space = isl_map_get_space(map);
10181 total1 = isl_space_dim(space, isl_dim_in);
10182 total2 = isl_space_dim(space, isl_dim_out);
10183 space = isl_space_factor_range(space);
10184 keep1 = isl_space_dim(space, isl_dim_in);
10185 keep2 = isl_space_dim(space, isl_dim_out);
10186 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10187 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10188 map = isl_map_reset_space(map, space);
10190 return map;
10193 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10195 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10197 isl_space *space;
10198 int total, keep;
10200 if (!map)
10201 return NULL;
10202 if (!isl_space_domain_is_wrapping(map->dim))
10203 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10204 "domain is not a product", return isl_map_free(map));
10206 space = isl_map_get_space(map);
10207 total = isl_space_dim(space, isl_dim_in);
10208 space = isl_space_domain_factor_domain(space);
10209 keep = isl_space_dim(space, isl_dim_in);
10210 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10211 map = isl_map_reset_space(map, space);
10213 return map;
10216 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10218 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10220 isl_space *space;
10221 int total, keep;
10223 if (!map)
10224 return NULL;
10225 if (!isl_space_domain_is_wrapping(map->dim))
10226 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10227 "domain is not a product", return isl_map_free(map));
10229 space = isl_map_get_space(map);
10230 total = isl_space_dim(space, isl_dim_in);
10231 space = isl_space_domain_factor_range(space);
10232 keep = isl_space_dim(space, isl_dim_in);
10233 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10234 map = isl_map_reset_space(map, space);
10236 return map;
10239 /* Given a map A -> [B -> C], extract the map A -> B.
10241 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10243 isl_space *space;
10244 int total, keep;
10246 if (!map)
10247 return NULL;
10248 if (!isl_space_range_is_wrapping(map->dim))
10249 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10250 "range is not a product", return isl_map_free(map));
10252 space = isl_map_get_space(map);
10253 total = isl_space_dim(space, isl_dim_out);
10254 space = isl_space_range_factor_domain(space);
10255 keep = isl_space_dim(space, isl_dim_out);
10256 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10257 map = isl_map_reset_space(map, space);
10259 return map;
10262 /* Given a map A -> [B -> C], extract the map A -> C.
10264 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10266 isl_space *space;
10267 int total, keep;
10269 if (!map)
10270 return NULL;
10271 if (!isl_space_range_is_wrapping(map->dim))
10272 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10273 "range is not a product", return isl_map_free(map));
10275 space = isl_map_get_space(map);
10276 total = isl_space_dim(space, isl_dim_out);
10277 space = isl_space_range_factor_range(space);
10278 keep = isl_space_dim(space, isl_dim_out);
10279 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10280 map = isl_map_reset_space(map, space);
10282 return map;
10285 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10287 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10288 __isl_take isl_map *map2)
10290 isl_map *prod;
10292 prod = isl_map_domain_product(map1, map2);
10293 prod = isl_map_flatten_domain(prod);
10294 return prod;
10297 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10299 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10300 __isl_take isl_map *map2)
10302 isl_map *prod;
10304 prod = isl_map_range_product(map1, map2);
10305 prod = isl_map_flatten_range(prod);
10306 return prod;
10309 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10311 int i;
10312 uint32_t hash = isl_hash_init();
10313 unsigned total;
10315 if (!bmap)
10316 return 0;
10317 bmap = isl_basic_map_copy(bmap);
10318 bmap = isl_basic_map_normalize(bmap);
10319 if (!bmap)
10320 return 0;
10321 total = isl_basic_map_total_dim(bmap);
10322 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10323 for (i = 0; i < bmap->n_eq; ++i) {
10324 uint32_t c_hash;
10325 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10326 isl_hash_hash(hash, c_hash);
10328 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10329 for (i = 0; i < bmap->n_ineq; ++i) {
10330 uint32_t c_hash;
10331 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10332 isl_hash_hash(hash, c_hash);
10334 isl_hash_byte(hash, bmap->n_div & 0xFF);
10335 for (i = 0; i < bmap->n_div; ++i) {
10336 uint32_t c_hash;
10337 if (isl_int_is_zero(bmap->div[i][0]))
10338 continue;
10339 isl_hash_byte(hash, i & 0xFF);
10340 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10341 isl_hash_hash(hash, c_hash);
10343 isl_basic_map_free(bmap);
10344 return hash;
10347 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10349 return isl_basic_map_get_hash(bset_to_bmap(bset));
10352 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10354 int i;
10355 uint32_t hash;
10357 if (!map)
10358 return 0;
10359 map = isl_map_copy(map);
10360 map = isl_map_normalize(map);
10361 if (!map)
10362 return 0;
10364 hash = isl_hash_init();
10365 for (i = 0; i < map->n; ++i) {
10366 uint32_t bmap_hash;
10367 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10368 isl_hash_hash(hash, bmap_hash);
10371 isl_map_free(map);
10373 return hash;
10376 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10378 return isl_map_get_hash(set_to_map(set));
10381 /* Return the number of basic maps in the (current) representation of "map".
10383 int isl_map_n_basic_map(__isl_keep isl_map *map)
10385 return map ? map->n : 0;
10388 int isl_set_n_basic_set(__isl_keep isl_set *set)
10390 return set ? set->n : 0;
10393 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10394 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10396 int i;
10398 if (!map)
10399 return isl_stat_error;
10401 for (i = 0; i < map->n; ++i)
10402 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10403 return isl_stat_error;
10405 return isl_stat_ok;
10408 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10409 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10411 int i;
10413 if (!set)
10414 return isl_stat_error;
10416 for (i = 0; i < set->n; ++i)
10417 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10418 return isl_stat_error;
10420 return isl_stat_ok;
10423 /* Return a list of basic sets, the union of which is equal to "set".
10425 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10426 __isl_keep isl_set *set)
10428 int i;
10429 isl_basic_set_list *list;
10431 if (!set)
10432 return NULL;
10434 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10435 for (i = 0; i < set->n; ++i) {
10436 isl_basic_set *bset;
10438 bset = isl_basic_set_copy(set->p[i]);
10439 list = isl_basic_set_list_add(list, bset);
10442 return list;
10445 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10447 isl_space *space;
10449 if (!bset)
10450 return NULL;
10452 bset = isl_basic_set_cow(bset);
10453 if (!bset)
10454 return NULL;
10456 space = isl_basic_set_get_space(bset);
10457 space = isl_space_lift(space, bset->n_div);
10458 if (!space)
10459 goto error;
10460 isl_space_free(bset->dim);
10461 bset->dim = space;
10462 bset->extra -= bset->n_div;
10463 bset->n_div = 0;
10465 bset = isl_basic_set_finalize(bset);
10467 return bset;
10468 error:
10469 isl_basic_set_free(bset);
10470 return NULL;
10473 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10475 int i;
10476 isl_space *space;
10477 unsigned n_div;
10479 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10481 if (!set)
10482 return NULL;
10484 set = isl_set_cow(set);
10485 if (!set)
10486 return NULL;
10488 n_div = set->p[0]->n_div;
10489 space = isl_set_get_space(set);
10490 space = isl_space_lift(space, n_div);
10491 if (!space)
10492 goto error;
10493 isl_space_free(set->dim);
10494 set->dim = space;
10496 for (i = 0; i < set->n; ++i) {
10497 set->p[i] = isl_basic_set_lift(set->p[i]);
10498 if (!set->p[i])
10499 goto error;
10502 return set;
10503 error:
10504 isl_set_free(set);
10505 return NULL;
10508 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10510 unsigned dim;
10511 int size = 0;
10513 if (!bset)
10514 return -1;
10516 dim = isl_basic_set_total_dim(bset);
10517 size += bset->n_eq * (1 + dim);
10518 size += bset->n_ineq * (1 + dim);
10519 size += bset->n_div * (2 + dim);
10521 return size;
10524 int isl_set_size(__isl_keep isl_set *set)
10526 int i;
10527 int size = 0;
10529 if (!set)
10530 return -1;
10532 for (i = 0; i < set->n; ++i)
10533 size += isl_basic_set_size(set->p[i]);
10535 return size;
10538 /* Check if there is any lower bound (if lower == 0) and/or upper
10539 * bound (if upper == 0) on the specified dim.
10541 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10542 enum isl_dim_type type, unsigned pos, int lower, int upper)
10544 int i;
10546 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10547 return isl_bool_error;
10549 pos += isl_basic_map_offset(bmap, type);
10551 for (i = 0; i < bmap->n_div; ++i) {
10552 if (isl_int_is_zero(bmap->div[i][0]))
10553 continue;
10554 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10555 return isl_bool_true;
10558 for (i = 0; i < bmap->n_eq; ++i)
10559 if (!isl_int_is_zero(bmap->eq[i][pos]))
10560 return isl_bool_true;
10562 for (i = 0; i < bmap->n_ineq; ++i) {
10563 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10564 if (sgn > 0)
10565 lower = 1;
10566 if (sgn < 0)
10567 upper = 1;
10570 return lower && upper;
10573 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10574 enum isl_dim_type type, unsigned pos)
10576 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10579 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10580 enum isl_dim_type type, unsigned pos)
10582 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10585 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10586 enum isl_dim_type type, unsigned pos)
10588 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10591 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10592 enum isl_dim_type type, unsigned pos)
10594 int i;
10596 if (!map)
10597 return isl_bool_error;
10599 for (i = 0; i < map->n; ++i) {
10600 isl_bool bounded;
10601 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10602 if (bounded < 0 || !bounded)
10603 return bounded;
10606 return isl_bool_true;
10609 /* Return true if the specified dim is involved in both an upper bound
10610 * and a lower bound.
10612 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10613 enum isl_dim_type type, unsigned pos)
10615 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10618 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10620 static isl_bool has_any_bound(__isl_keep isl_map *map,
10621 enum isl_dim_type type, unsigned pos,
10622 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10623 enum isl_dim_type type, unsigned pos))
10625 int i;
10627 if (!map)
10628 return isl_bool_error;
10630 for (i = 0; i < map->n; ++i) {
10631 isl_bool bounded;
10632 bounded = fn(map->p[i], type, pos);
10633 if (bounded < 0 || bounded)
10634 return bounded;
10637 return isl_bool_false;
10640 /* Return 1 if the specified dim is involved in any lower bound.
10642 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10643 enum isl_dim_type type, unsigned pos)
10645 return has_any_bound(set, type, pos,
10646 &isl_basic_map_dim_has_lower_bound);
10649 /* Return 1 if the specified dim is involved in any upper bound.
10651 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10652 enum isl_dim_type type, unsigned pos)
10654 return has_any_bound(set, type, pos,
10655 &isl_basic_map_dim_has_upper_bound);
10658 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10660 static isl_bool has_bound(__isl_keep isl_map *map,
10661 enum isl_dim_type type, unsigned pos,
10662 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10663 enum isl_dim_type type, unsigned pos))
10665 int i;
10667 if (!map)
10668 return isl_bool_error;
10670 for (i = 0; i < map->n; ++i) {
10671 isl_bool bounded;
10672 bounded = fn(map->p[i], type, pos);
10673 if (bounded < 0 || !bounded)
10674 return bounded;
10677 return isl_bool_true;
10680 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10682 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10683 enum isl_dim_type type, unsigned pos)
10685 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10688 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10690 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10691 enum isl_dim_type type, unsigned pos)
10693 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10696 /* For each of the "n" variables starting at "first", determine
10697 * the sign of the variable and put the results in the first "n"
10698 * elements of the array "signs".
10699 * Sign
10700 * 1 means that the variable is non-negative
10701 * -1 means that the variable is non-positive
10702 * 0 means the variable attains both positive and negative values.
10704 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10705 unsigned first, unsigned n, int *signs)
10707 isl_vec *bound = NULL;
10708 struct isl_tab *tab = NULL;
10709 struct isl_tab_undo *snap;
10710 int i;
10712 if (!bset || !signs)
10713 return isl_stat_error;
10715 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10716 tab = isl_tab_from_basic_set(bset, 0);
10717 if (!bound || !tab)
10718 goto error;
10720 isl_seq_clr(bound->el, bound->size);
10721 isl_int_set_si(bound->el[0], -1);
10723 snap = isl_tab_snap(tab);
10724 for (i = 0; i < n; ++i) {
10725 int empty;
10727 isl_int_set_si(bound->el[1 + first + i], -1);
10728 if (isl_tab_add_ineq(tab, bound->el) < 0)
10729 goto error;
10730 empty = tab->empty;
10731 isl_int_set_si(bound->el[1 + first + i], 0);
10732 if (isl_tab_rollback(tab, snap) < 0)
10733 goto error;
10735 if (empty) {
10736 signs[i] = 1;
10737 continue;
10740 isl_int_set_si(bound->el[1 + first + i], 1);
10741 if (isl_tab_add_ineq(tab, bound->el) < 0)
10742 goto error;
10743 empty = tab->empty;
10744 isl_int_set_si(bound->el[1 + first + i], 0);
10745 if (isl_tab_rollback(tab, snap) < 0)
10746 goto error;
10748 signs[i] = empty ? -1 : 0;
10751 isl_tab_free(tab);
10752 isl_vec_free(bound);
10753 return isl_stat_ok;
10754 error:
10755 isl_tab_free(tab);
10756 isl_vec_free(bound);
10757 return isl_stat_error;
10760 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10761 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10763 if (!bset || !signs)
10764 return isl_stat_error;
10765 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10766 return isl_stat_error;
10768 first += pos(bset->dim, type) - 1;
10769 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10772 /* Is it possible for the integer division "div" to depend (possibly
10773 * indirectly) on any output dimensions?
10775 * If the div is undefined, then we conservatively assume that it
10776 * may depend on them.
10777 * Otherwise, we check if it actually depends on them or on any integer
10778 * divisions that may depend on them.
10780 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10782 int i;
10783 unsigned n_out, o_out;
10784 unsigned n_div, o_div;
10786 if (isl_int_is_zero(bmap->div[div][0]))
10787 return isl_bool_true;
10789 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10790 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10792 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10793 return isl_bool_true;
10795 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10796 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10798 for (i = 0; i < n_div; ++i) {
10799 isl_bool may_involve;
10801 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10802 continue;
10803 may_involve = div_may_involve_output(bmap, i);
10804 if (may_involve < 0 || may_involve)
10805 return may_involve;
10808 return isl_bool_false;
10811 /* Return the first integer division of "bmap" in the range
10812 * [first, first + n[ that may depend on any output dimensions and
10813 * that has a non-zero coefficient in "c" (where the first coefficient
10814 * in "c" corresponds to integer division "first").
10816 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10817 isl_int *c, int first, int n)
10819 int k;
10821 if (!bmap)
10822 return -1;
10824 for (k = first; k < first + n; ++k) {
10825 isl_bool may_involve;
10827 if (isl_int_is_zero(c[k]))
10828 continue;
10829 may_involve = div_may_involve_output(bmap, k);
10830 if (may_involve < 0)
10831 return -1;
10832 if (may_involve)
10833 return k;
10836 return first + n;
10839 /* Look for a pair of inequality constraints in "bmap" of the form
10841 * -l + i >= 0 or i >= l
10842 * and
10843 * n + l - i >= 0 or i <= l + n
10845 * with n < "m" and i the output dimension at position "pos".
10846 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10847 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10848 * and earlier output dimensions, as well as integer divisions that do
10849 * not involve any of the output dimensions.
10851 * Return the index of the first inequality constraint or bmap->n_ineq
10852 * if no such pair can be found.
10854 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10855 int pos, isl_int m)
10857 int i, j;
10858 isl_ctx *ctx;
10859 unsigned total;
10860 unsigned n_div, o_div;
10861 unsigned n_out, o_out;
10862 int less;
10864 if (!bmap)
10865 return -1;
10867 ctx = isl_basic_map_get_ctx(bmap);
10868 total = isl_basic_map_total_dim(bmap);
10869 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10870 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10871 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10872 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10873 for (i = 0; i < bmap->n_ineq; ++i) {
10874 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10875 continue;
10876 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10877 n_out - (pos + 1)) != -1)
10878 continue;
10879 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10880 0, n_div) < n_div)
10881 continue;
10882 for (j = i + 1; j < bmap->n_ineq; ++j) {
10883 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10884 ctx->one))
10885 continue;
10886 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10887 bmap->ineq[j] + 1, total))
10888 continue;
10889 break;
10891 if (j >= bmap->n_ineq)
10892 continue;
10893 isl_int_add(bmap->ineq[i][0],
10894 bmap->ineq[i][0], bmap->ineq[j][0]);
10895 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10896 isl_int_sub(bmap->ineq[i][0],
10897 bmap->ineq[i][0], bmap->ineq[j][0]);
10898 if (!less)
10899 continue;
10900 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10901 return i;
10902 else
10903 return j;
10906 return bmap->n_ineq;
10909 /* Return the index of the equality of "bmap" that defines
10910 * the output dimension "pos" in terms of earlier dimensions.
10911 * The equality may also involve integer divisions, as long
10912 * as those integer divisions are defined in terms of
10913 * parameters or input dimensions.
10914 * In this case, *div is set to the number of integer divisions and
10915 * *ineq is set to the number of inequality constraints (provided
10916 * div and ineq are not NULL).
10918 * The equality may also involve a single integer division involving
10919 * the output dimensions (typically only output dimension "pos") as
10920 * long as the coefficient of output dimension "pos" is 1 or -1 and
10921 * there is a pair of constraints i >= l and i <= l + n, with i referring
10922 * to output dimension "pos", l an expression involving only earlier
10923 * dimensions and n smaller than the coefficient of the integer division
10924 * in the equality. In this case, the output dimension can be defined
10925 * in terms of a modulo expression that does not involve the integer division.
10926 * *div is then set to this single integer division and
10927 * *ineq is set to the index of constraint i >= l.
10929 * Return bmap->n_eq if there is no such equality.
10930 * Return -1 on error.
10932 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10933 int pos, int *div, int *ineq)
10935 int j, k, l;
10936 unsigned n_out, o_out;
10937 unsigned n_div, o_div;
10939 if (!bmap)
10940 return -1;
10942 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10943 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10944 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10945 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10947 if (ineq)
10948 *ineq = bmap->n_ineq;
10949 if (div)
10950 *div = n_div;
10951 for (j = 0; j < bmap->n_eq; ++j) {
10952 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10953 continue;
10954 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10955 n_out - (pos + 1)) != -1)
10956 continue;
10957 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10958 0, n_div);
10959 if (k >= n_div)
10960 return j;
10961 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10962 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10963 continue;
10964 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10965 k + 1, n_div - (k+1)) < n_div)
10966 continue;
10967 l = find_modulo_constraint_pair(bmap, pos,
10968 bmap->eq[j][o_div + k]);
10969 if (l < 0)
10970 return -1;
10971 if (l >= bmap->n_ineq)
10972 continue;
10973 if (div)
10974 *div = k;
10975 if (ineq)
10976 *ineq = l;
10977 return j;
10980 return bmap->n_eq;
10983 /* Check if the given basic map is obviously single-valued.
10984 * In particular, for each output dimension, check that there is
10985 * an equality that defines the output dimension in terms of
10986 * earlier dimensions.
10988 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10990 int i;
10991 unsigned n_out;
10993 if (!bmap)
10994 return isl_bool_error;
10996 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10998 for (i = 0; i < n_out; ++i) {
10999 int eq;
11001 eq = isl_basic_map_output_defining_equality(bmap, i,
11002 NULL, NULL);
11003 if (eq < 0)
11004 return isl_bool_error;
11005 if (eq >= bmap->n_eq)
11006 return isl_bool_false;
11009 return isl_bool_true;
11012 /* Check if the given basic map is single-valued.
11013 * We simply compute
11015 * M \circ M^-1
11017 * and check if the result is a subset of the identity mapping.
11019 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11021 isl_space *space;
11022 isl_basic_map *test;
11023 isl_basic_map *id;
11024 isl_bool sv;
11026 sv = isl_basic_map_plain_is_single_valued(bmap);
11027 if (sv < 0 || sv)
11028 return sv;
11030 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11031 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11033 space = isl_basic_map_get_space(bmap);
11034 space = isl_space_map_from_set(isl_space_range(space));
11035 id = isl_basic_map_identity(space);
11037 sv = isl_basic_map_is_subset(test, id);
11039 isl_basic_map_free(test);
11040 isl_basic_map_free(id);
11042 return sv;
11045 /* Check if the given map is obviously single-valued.
11047 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11049 if (!map)
11050 return isl_bool_error;
11051 if (map->n == 0)
11052 return isl_bool_true;
11053 if (map->n >= 2)
11054 return isl_bool_false;
11056 return isl_basic_map_plain_is_single_valued(map->p[0]);
11059 /* Check if the given map is single-valued.
11060 * We simply compute
11062 * M \circ M^-1
11064 * and check if the result is a subset of the identity mapping.
11066 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11068 isl_space *dim;
11069 isl_map *test;
11070 isl_map *id;
11071 isl_bool sv;
11073 sv = isl_map_plain_is_single_valued(map);
11074 if (sv < 0 || sv)
11075 return sv;
11077 test = isl_map_reverse(isl_map_copy(map));
11078 test = isl_map_apply_range(test, isl_map_copy(map));
11080 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11081 id = isl_map_identity(dim);
11083 sv = isl_map_is_subset(test, id);
11085 isl_map_free(test);
11086 isl_map_free(id);
11088 return sv;
11091 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11093 isl_bool in;
11095 map = isl_map_copy(map);
11096 map = isl_map_reverse(map);
11097 in = isl_map_is_single_valued(map);
11098 isl_map_free(map);
11100 return in;
11103 /* Check if the given map is obviously injective.
11105 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11107 isl_bool in;
11109 map = isl_map_copy(map);
11110 map = isl_map_reverse(map);
11111 in = isl_map_plain_is_single_valued(map);
11112 isl_map_free(map);
11114 return in;
11117 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11119 isl_bool sv;
11121 sv = isl_map_is_single_valued(map);
11122 if (sv < 0 || !sv)
11123 return sv;
11125 return isl_map_is_injective(map);
11128 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11130 return isl_map_is_single_valued(set_to_map(set));
11133 /* Does "map" only map elements to themselves?
11135 * If the domain and range spaces are different, then "map"
11136 * is considered not to be an identity relation, even if it is empty.
11137 * Otherwise, construct the maximal identity relation and
11138 * check whether "map" is a subset of this relation.
11140 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11142 isl_space *space;
11143 isl_map *id;
11144 isl_bool equal, is_identity;
11146 space = isl_map_get_space(map);
11147 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11148 isl_space_free(space);
11149 if (equal < 0 || !equal)
11150 return equal;
11152 id = isl_map_identity(isl_map_get_space(map));
11153 is_identity = isl_map_is_subset(map, id);
11154 isl_map_free(id);
11156 return is_identity;
11159 int isl_map_is_translation(__isl_keep isl_map *map)
11161 int ok;
11162 isl_set *delta;
11164 delta = isl_map_deltas(isl_map_copy(map));
11165 ok = isl_set_is_singleton(delta);
11166 isl_set_free(delta);
11168 return ok;
11171 static int unique(isl_int *p, unsigned pos, unsigned len)
11173 if (isl_seq_first_non_zero(p, pos) != -1)
11174 return 0;
11175 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11176 return 0;
11177 return 1;
11180 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11182 int i, j;
11183 unsigned nvar;
11184 unsigned ovar;
11186 if (!bset)
11187 return isl_bool_error;
11189 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11190 return isl_bool_false;
11192 nvar = isl_basic_set_dim(bset, isl_dim_set);
11193 ovar = isl_space_offset(bset->dim, isl_dim_set);
11194 for (j = 0; j < nvar; ++j) {
11195 int lower = 0, upper = 0;
11196 for (i = 0; i < bset->n_eq; ++i) {
11197 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11198 continue;
11199 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11200 return isl_bool_false;
11201 break;
11203 if (i < bset->n_eq)
11204 continue;
11205 for (i = 0; i < bset->n_ineq; ++i) {
11206 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11207 continue;
11208 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11209 return isl_bool_false;
11210 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11211 lower = 1;
11212 else
11213 upper = 1;
11215 if (!lower || !upper)
11216 return isl_bool_false;
11219 return isl_bool_true;
11222 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11224 if (!set)
11225 return isl_bool_error;
11226 if (set->n != 1)
11227 return isl_bool_false;
11229 return isl_basic_set_is_box(set->p[0]);
11232 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11234 if (!bset)
11235 return isl_bool_error;
11237 return isl_space_is_wrapping(bset->dim);
11240 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11242 if (!set)
11243 return isl_bool_error;
11245 return isl_space_is_wrapping(set->dim);
11248 /* Modify the space of "map" through a call to "change".
11249 * If "can_change" is set (not NULL), then first call it to check
11250 * if the modification is allowed, printing the error message "cannot_change"
11251 * if it is not.
11253 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11254 isl_bool (*can_change)(__isl_keep isl_map *map),
11255 const char *cannot_change,
11256 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11258 isl_bool ok;
11259 isl_space *space;
11261 if (!map)
11262 return NULL;
11264 ok = can_change ? can_change(map) : isl_bool_true;
11265 if (ok < 0)
11266 return isl_map_free(map);
11267 if (!ok)
11268 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11269 return isl_map_free(map));
11271 space = change(isl_map_get_space(map));
11272 map = isl_map_reset_space(map, space);
11274 return map;
11277 /* Is the domain of "map" a wrapped relation?
11279 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11281 if (!map)
11282 return isl_bool_error;
11284 return isl_space_domain_is_wrapping(map->dim);
11287 /* Does "map" have a wrapped relation in both domain and range?
11289 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11291 return isl_space_is_product(isl_map_peek_space(map));
11294 /* Is the range of "map" a wrapped relation?
11296 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11298 if (!map)
11299 return isl_bool_error;
11301 return isl_space_range_is_wrapping(map->dim);
11304 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11306 bmap = isl_basic_map_cow(bmap);
11307 if (!bmap)
11308 return NULL;
11310 bmap->dim = isl_space_wrap(bmap->dim);
11311 if (!bmap->dim)
11312 goto error;
11314 bmap = isl_basic_map_finalize(bmap);
11316 return bset_from_bmap(bmap);
11317 error:
11318 isl_basic_map_free(bmap);
11319 return NULL;
11322 /* Given a map A -> B, return the set (A -> B).
11324 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11326 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11329 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11331 bset = isl_basic_set_cow(bset);
11332 if (!bset)
11333 return NULL;
11335 bset->dim = isl_space_unwrap(bset->dim);
11336 if (!bset->dim)
11337 goto error;
11339 bset = isl_basic_set_finalize(bset);
11341 return bset_to_bmap(bset);
11342 error:
11343 isl_basic_set_free(bset);
11344 return NULL;
11347 /* Given a set (A -> B), return the map A -> B.
11348 * Error out if "set" is not of the form (A -> B).
11350 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11352 return isl_map_change_space(set, &isl_set_is_wrapping,
11353 "not a wrapping set", &isl_space_unwrap);
11356 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11357 enum isl_dim_type type)
11359 if (!bmap)
11360 return NULL;
11362 if (!isl_space_is_named_or_nested(bmap->dim, type))
11363 return bmap;
11365 bmap = isl_basic_map_cow(bmap);
11366 if (!bmap)
11367 return NULL;
11369 bmap->dim = isl_space_reset(bmap->dim, type);
11370 if (!bmap->dim)
11371 goto error;
11373 bmap = isl_basic_map_finalize(bmap);
11375 return bmap;
11376 error:
11377 isl_basic_map_free(bmap);
11378 return NULL;
11381 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11382 enum isl_dim_type type)
11384 int i;
11386 if (!map)
11387 return NULL;
11389 if (!isl_space_is_named_or_nested(map->dim, type))
11390 return map;
11392 map = isl_map_cow(map);
11393 if (!map)
11394 return NULL;
11396 for (i = 0; i < map->n; ++i) {
11397 map->p[i] = isl_basic_map_reset(map->p[i], type);
11398 if (!map->p[i])
11399 goto error;
11401 map->dim = isl_space_reset(map->dim, type);
11402 if (!map->dim)
11403 goto error;
11405 return map;
11406 error:
11407 isl_map_free(map);
11408 return NULL;
11411 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11413 if (!bmap)
11414 return NULL;
11416 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11417 return bmap;
11419 bmap = isl_basic_map_cow(bmap);
11420 if (!bmap)
11421 return NULL;
11423 bmap->dim = isl_space_flatten(bmap->dim);
11424 if (!bmap->dim)
11425 goto error;
11427 bmap = isl_basic_map_finalize(bmap);
11429 return bmap;
11430 error:
11431 isl_basic_map_free(bmap);
11432 return NULL;
11435 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11437 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11440 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11441 __isl_take isl_basic_map *bmap)
11443 if (!bmap)
11444 return NULL;
11446 if (!bmap->dim->nested[0])
11447 return bmap;
11449 bmap = isl_basic_map_cow(bmap);
11450 if (!bmap)
11451 return NULL;
11453 bmap->dim = isl_space_flatten_domain(bmap->dim);
11454 if (!bmap->dim)
11455 goto error;
11457 bmap = isl_basic_map_finalize(bmap);
11459 return bmap;
11460 error:
11461 isl_basic_map_free(bmap);
11462 return NULL;
11465 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11466 __isl_take isl_basic_map *bmap)
11468 if (!bmap)
11469 return NULL;
11471 if (!bmap->dim->nested[1])
11472 return bmap;
11474 bmap = isl_basic_map_cow(bmap);
11475 if (!bmap)
11476 return NULL;
11478 bmap->dim = isl_space_flatten_range(bmap->dim);
11479 if (!bmap->dim)
11480 goto error;
11482 bmap = isl_basic_map_finalize(bmap);
11484 return bmap;
11485 error:
11486 isl_basic_map_free(bmap);
11487 return NULL;
11490 /* Remove any internal structure from the spaces of domain and range of "map".
11492 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11494 if (!map)
11495 return NULL;
11497 if (!map->dim->nested[0] && !map->dim->nested[1])
11498 return map;
11500 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11503 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11505 return set_from_map(isl_map_flatten(set_to_map(set)));
11508 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11510 isl_space *space, *flat_space;
11511 isl_map *map;
11513 space = isl_set_get_space(set);
11514 flat_space = isl_space_flatten(isl_space_copy(space));
11515 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11516 flat_space));
11517 map = isl_map_intersect_domain(map, set);
11519 return map;
11522 /* Remove any internal structure from the space of the domain of "map".
11524 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11526 if (!map)
11527 return NULL;
11529 if (!map->dim->nested[0])
11530 return map;
11532 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11535 /* Remove any internal structure from the space of the range of "map".
11537 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11539 if (!map)
11540 return NULL;
11542 if (!map->dim->nested[1])
11543 return map;
11545 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11548 /* Reorder the dimensions of "bmap" according to the given dim_map
11549 * and set the dimension specification to "space" and
11550 * perform Gaussian elimination on the result.
11552 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11553 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11555 isl_basic_map *res;
11556 unsigned flags;
11557 unsigned n_div;
11559 if (!bmap || !space || !dim_map)
11560 goto error;
11562 flags = bmap->flags;
11563 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11564 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11565 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11566 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11567 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11568 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11569 if (res)
11570 res->flags = flags;
11571 res = isl_basic_map_gauss(res, NULL);
11572 res = isl_basic_map_finalize(res);
11573 return res;
11574 error:
11575 isl_dim_map_free(dim_map);
11576 isl_basic_map_free(bmap);
11577 isl_space_free(space);
11578 return NULL;
11581 /* Reorder the dimensions of "map" according to given reordering.
11583 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11584 __isl_take isl_reordering *r)
11586 int i;
11587 struct isl_dim_map *dim_map;
11589 map = isl_map_cow(map);
11590 dim_map = isl_dim_map_from_reordering(r);
11591 if (!map || !r || !dim_map)
11592 goto error;
11594 for (i = 0; i < map->n; ++i) {
11595 struct isl_dim_map *dim_map_i;
11596 isl_space *space;
11598 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11600 space = isl_reordering_get_space(r);
11601 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11603 if (!map->p[i])
11604 goto error;
11607 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11608 map = isl_map_unmark_normalized(map);
11610 isl_reordering_free(r);
11611 isl_dim_map_free(dim_map);
11612 return map;
11613 error:
11614 isl_dim_map_free(dim_map);
11615 isl_map_free(map);
11616 isl_reordering_free(r);
11617 return NULL;
11620 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11621 __isl_take isl_reordering *r)
11623 return set_from_map(isl_map_realign(set_to_map(set), r));
11626 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11627 __isl_take isl_space *model)
11629 isl_ctx *ctx;
11630 isl_bool aligned;
11632 if (!map || !model)
11633 goto error;
11635 ctx = isl_space_get_ctx(model);
11636 if (!isl_space_has_named_params(model))
11637 isl_die(ctx, isl_error_invalid,
11638 "model has unnamed parameters", goto error);
11639 if (isl_map_check_named_params(map) < 0)
11640 goto error;
11641 aligned = isl_map_space_has_equal_params(map, model);
11642 if (aligned < 0)
11643 goto error;
11644 if (!aligned) {
11645 isl_reordering *exp;
11647 exp = isl_parameter_alignment_reordering(map->dim, model);
11648 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11649 map = isl_map_realign(map, exp);
11652 isl_space_free(model);
11653 return map;
11654 error:
11655 isl_space_free(model);
11656 isl_map_free(map);
11657 return NULL;
11660 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11661 __isl_take isl_space *model)
11663 return isl_map_align_params(set, model);
11666 /* Align the parameters of "bmap" to those of "model", introducing
11667 * additional parameters if needed.
11669 __isl_give isl_basic_map *isl_basic_map_align_params(
11670 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11672 isl_ctx *ctx;
11673 isl_bool equal_params;
11675 if (!bmap || !model)
11676 goto error;
11678 ctx = isl_space_get_ctx(model);
11679 if (!isl_space_has_named_params(model))
11680 isl_die(ctx, isl_error_invalid,
11681 "model has unnamed parameters", goto error);
11682 if (isl_basic_map_check_named_params(bmap) < 0)
11683 goto error;
11684 equal_params = isl_space_has_equal_params(bmap->dim, model);
11685 if (equal_params < 0)
11686 goto error;
11687 if (!equal_params) {
11688 isl_reordering *exp;
11689 struct isl_dim_map *dim_map;
11691 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11692 exp = isl_reordering_extend_space(exp,
11693 isl_basic_map_get_space(bmap));
11694 dim_map = isl_dim_map_from_reordering(exp);
11695 bmap = isl_basic_map_realign(bmap,
11696 isl_reordering_get_space(exp),
11697 isl_dim_map_extend(dim_map, bmap));
11698 isl_reordering_free(exp);
11699 isl_dim_map_free(dim_map);
11702 isl_space_free(model);
11703 return bmap;
11704 error:
11705 isl_space_free(model);
11706 isl_basic_map_free(bmap);
11707 return NULL;
11710 /* Do "bset" and "space" have the same parameters?
11712 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11713 __isl_keep isl_space *space)
11715 isl_space *bset_space;
11717 bset_space = isl_basic_set_peek_space(bset);
11718 return isl_space_has_equal_params(bset_space, space);
11721 /* Do "map" and "space" have the same parameters?
11723 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11724 __isl_keep isl_space *space)
11726 isl_space *map_space;
11728 map_space = isl_map_peek_space(map);
11729 return isl_space_has_equal_params(map_space, space);
11732 /* Do "set" and "space" have the same parameters?
11734 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11735 __isl_keep isl_space *space)
11737 return isl_map_space_has_equal_params(set_to_map(set), space);
11740 /* Align the parameters of "bset" to those of "model", introducing
11741 * additional parameters if needed.
11743 __isl_give isl_basic_set *isl_basic_set_align_params(
11744 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11746 return isl_basic_map_align_params(bset, model);
11749 /* Drop all parameters not referenced by "map".
11751 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11753 int i;
11755 if (isl_map_check_named_params(map) < 0)
11756 return isl_map_free(map);
11758 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11759 isl_bool involves;
11761 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11762 if (involves < 0)
11763 return isl_map_free(map);
11764 if (!involves)
11765 map = isl_map_project_out(map, isl_dim_param, i, 1);
11768 return map;
11771 /* Drop all parameters not referenced by "set".
11773 __isl_give isl_set *isl_set_drop_unused_params(
11774 __isl_take isl_set *set)
11776 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11779 /* Drop all parameters not referenced by "bmap".
11781 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11782 __isl_take isl_basic_map *bmap)
11784 int i;
11786 if (isl_basic_map_check_named_params(bmap) < 0)
11787 return isl_basic_map_free(bmap);
11789 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11790 isl_bool involves;
11792 involves = isl_basic_map_involves_dims(bmap,
11793 isl_dim_param, i, 1);
11794 if (involves < 0)
11795 return isl_basic_map_free(bmap);
11796 if (!involves)
11797 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11800 return bmap;
11803 /* Drop all parameters not referenced by "bset".
11805 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11806 __isl_take isl_basic_set *bset)
11808 return bset_from_bmap(isl_basic_map_drop_unused_params(
11809 bset_to_bmap(bset)));
11812 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11813 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11814 enum isl_dim_type c2, enum isl_dim_type c3,
11815 enum isl_dim_type c4, enum isl_dim_type c5)
11817 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11818 struct isl_mat *mat;
11819 int i, j, k;
11820 int pos;
11822 if (!bmap)
11823 return NULL;
11824 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11825 isl_basic_map_total_dim(bmap) + 1);
11826 if (!mat)
11827 return NULL;
11828 for (i = 0; i < bmap->n_eq; ++i)
11829 for (j = 0, pos = 0; j < 5; ++j) {
11830 int off = isl_basic_map_offset(bmap, c[j]);
11831 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11832 isl_int_set(mat->row[i][pos],
11833 bmap->eq[i][off + k]);
11834 ++pos;
11838 return mat;
11841 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11842 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11843 enum isl_dim_type c2, enum isl_dim_type c3,
11844 enum isl_dim_type c4, enum isl_dim_type c5)
11846 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11847 struct isl_mat *mat;
11848 int i, j, k;
11849 int pos;
11851 if (!bmap)
11852 return NULL;
11853 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11854 isl_basic_map_total_dim(bmap) + 1);
11855 if (!mat)
11856 return NULL;
11857 for (i = 0; i < bmap->n_ineq; ++i)
11858 for (j = 0, pos = 0; j < 5; ++j) {
11859 int off = isl_basic_map_offset(bmap, c[j]);
11860 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11861 isl_int_set(mat->row[i][pos],
11862 bmap->ineq[i][off + k]);
11863 ++pos;
11867 return mat;
11870 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11871 __isl_take isl_space *space,
11872 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11873 enum isl_dim_type c2, enum isl_dim_type c3,
11874 enum isl_dim_type c4, enum isl_dim_type c5)
11876 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11877 isl_basic_map *bmap = NULL;
11878 unsigned total;
11879 unsigned extra;
11880 int i, j, k, l;
11881 int pos;
11883 if (!space || !eq || !ineq)
11884 goto error;
11886 if (eq->n_col != ineq->n_col)
11887 isl_die(space->ctx, isl_error_invalid,
11888 "equalities and inequalities matrices should have "
11889 "same number of columns", goto error);
11891 total = 1 + isl_space_dim(space, isl_dim_all);
11893 if (eq->n_col < total)
11894 isl_die(space->ctx, isl_error_invalid,
11895 "number of columns too small", goto error);
11897 extra = eq->n_col - total;
11899 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11900 eq->n_row, ineq->n_row);
11901 if (!bmap)
11902 goto error;
11903 for (i = 0; i < extra; ++i) {
11904 k = isl_basic_map_alloc_div(bmap);
11905 if (k < 0)
11906 goto error;
11907 isl_int_set_si(bmap->div[k][0], 0);
11909 for (i = 0; i < eq->n_row; ++i) {
11910 l = isl_basic_map_alloc_equality(bmap);
11911 if (l < 0)
11912 goto error;
11913 for (j = 0, pos = 0; j < 5; ++j) {
11914 int off = isl_basic_map_offset(bmap, c[j]);
11915 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11916 isl_int_set(bmap->eq[l][off + k],
11917 eq->row[i][pos]);
11918 ++pos;
11922 for (i = 0; i < ineq->n_row; ++i) {
11923 l = isl_basic_map_alloc_inequality(bmap);
11924 if (l < 0)
11925 goto error;
11926 for (j = 0, pos = 0; j < 5; ++j) {
11927 int off = isl_basic_map_offset(bmap, c[j]);
11928 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11929 isl_int_set(bmap->ineq[l][off + k],
11930 ineq->row[i][pos]);
11931 ++pos;
11936 isl_space_free(space);
11937 isl_mat_free(eq);
11938 isl_mat_free(ineq);
11940 bmap = isl_basic_map_simplify(bmap);
11941 return isl_basic_map_finalize(bmap);
11942 error:
11943 isl_space_free(space);
11944 isl_mat_free(eq);
11945 isl_mat_free(ineq);
11946 isl_basic_map_free(bmap);
11947 return NULL;
11950 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11951 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11952 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11954 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11955 c1, c2, c3, c4, isl_dim_in);
11958 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11959 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11960 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11962 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11963 c1, c2, c3, c4, isl_dim_in);
11966 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11967 __isl_take isl_space *dim,
11968 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11969 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11971 isl_basic_map *bmap;
11972 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11973 c1, c2, c3, c4, isl_dim_in);
11974 return bset_from_bmap(bmap);
11977 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11979 if (!bmap)
11980 return isl_bool_error;
11982 return isl_space_can_zip(bmap->dim);
11985 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11987 if (!map)
11988 return isl_bool_error;
11990 return isl_space_can_zip(map->dim);
11993 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11994 * (A -> C) -> (B -> D).
11996 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11998 unsigned pos;
11999 unsigned n1;
12000 unsigned n2;
12002 if (!bmap)
12003 return NULL;
12005 if (!isl_basic_map_can_zip(bmap))
12006 isl_die(bmap->ctx, isl_error_invalid,
12007 "basic map cannot be zipped", goto error);
12008 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12009 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12010 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12011 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12012 bmap = isl_basic_map_cow(bmap);
12013 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12014 if (!bmap)
12015 return NULL;
12016 bmap->dim = isl_space_zip(bmap->dim);
12017 if (!bmap->dim)
12018 goto error;
12019 bmap = isl_basic_map_mark_final(bmap);
12020 return bmap;
12021 error:
12022 isl_basic_map_free(bmap);
12023 return NULL;
12026 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12027 * (A -> C) -> (B -> D).
12029 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12031 int i;
12033 if (!map)
12034 return NULL;
12036 if (!isl_map_can_zip(map))
12037 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12038 goto error);
12040 map = isl_map_cow(map);
12041 if (!map)
12042 return NULL;
12044 for (i = 0; i < map->n; ++i) {
12045 map->p[i] = isl_basic_map_zip(map->p[i]);
12046 if (!map->p[i])
12047 goto error;
12050 map->dim = isl_space_zip(map->dim);
12051 if (!map->dim)
12052 goto error;
12054 return map;
12055 error:
12056 isl_map_free(map);
12057 return NULL;
12060 /* Can we apply isl_basic_map_curry to "bmap"?
12061 * That is, does it have a nested relation in its domain?
12063 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12065 if (!bmap)
12066 return isl_bool_error;
12068 return isl_space_can_curry(bmap->dim);
12071 /* Can we apply isl_map_curry to "map"?
12072 * That is, does it have a nested relation in its domain?
12074 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12076 if (!map)
12077 return isl_bool_error;
12079 return isl_space_can_curry(map->dim);
12082 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12083 * A -> (B -> C).
12085 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12088 if (!bmap)
12089 return NULL;
12091 if (!isl_basic_map_can_curry(bmap))
12092 isl_die(bmap->ctx, isl_error_invalid,
12093 "basic map cannot be curried", goto error);
12094 bmap = isl_basic_map_cow(bmap);
12095 if (!bmap)
12096 return NULL;
12097 bmap->dim = isl_space_curry(bmap->dim);
12098 if (!bmap->dim)
12099 goto error;
12100 bmap = isl_basic_map_mark_final(bmap);
12101 return bmap;
12102 error:
12103 isl_basic_map_free(bmap);
12104 return NULL;
12107 /* Given a map (A -> B) -> C, return the corresponding map
12108 * A -> (B -> C).
12110 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12112 return isl_map_change_space(map, &isl_map_can_curry,
12113 "map cannot be curried", &isl_space_curry);
12116 /* Can isl_map_range_curry be applied to "map"?
12117 * That is, does it have a nested relation in its range,
12118 * the domain of which is itself a nested relation?
12120 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12122 if (!map)
12123 return isl_bool_error;
12125 return isl_space_can_range_curry(map->dim);
12128 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12129 * A -> (B -> (C -> D)).
12131 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12133 return isl_map_change_space(map, &isl_map_can_range_curry,
12134 "map range cannot be curried",
12135 &isl_space_range_curry);
12138 /* Can we apply isl_basic_map_uncurry to "bmap"?
12139 * That is, does it have a nested relation in its domain?
12141 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12143 if (!bmap)
12144 return isl_bool_error;
12146 return isl_space_can_uncurry(bmap->dim);
12149 /* Can we apply isl_map_uncurry to "map"?
12150 * That is, does it have a nested relation in its domain?
12152 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12154 if (!map)
12155 return isl_bool_error;
12157 return isl_space_can_uncurry(map->dim);
12160 /* Given a basic map A -> (B -> C), return the corresponding basic map
12161 * (A -> B) -> C.
12163 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12166 if (!bmap)
12167 return NULL;
12169 if (!isl_basic_map_can_uncurry(bmap))
12170 isl_die(bmap->ctx, isl_error_invalid,
12171 "basic map cannot be uncurried",
12172 return isl_basic_map_free(bmap));
12173 bmap = isl_basic_map_cow(bmap);
12174 if (!bmap)
12175 return NULL;
12176 bmap->dim = isl_space_uncurry(bmap->dim);
12177 if (!bmap->dim)
12178 return isl_basic_map_free(bmap);
12179 bmap = isl_basic_map_mark_final(bmap);
12180 return bmap;
12183 /* Given a map A -> (B -> C), return the corresponding map
12184 * (A -> B) -> C.
12186 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12188 return isl_map_change_space(map, &isl_map_can_uncurry,
12189 "map cannot be uncurried", &isl_space_uncurry);
12192 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12193 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12195 return isl_map_equate(set, type1, pos1, type2, pos2);
12198 /* Construct a basic map where the given dimensions are equal to each other.
12200 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12201 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12203 isl_basic_map *bmap = NULL;
12204 int i;
12206 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12207 isl_space_check_range(space, type2, pos2, 1) < 0)
12208 goto error;
12210 if (type1 == type2 && pos1 == pos2)
12211 return isl_basic_map_universe(space);
12213 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12214 i = isl_basic_map_alloc_equality(bmap);
12215 if (i < 0)
12216 goto error;
12217 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12218 pos1 += isl_basic_map_offset(bmap, type1);
12219 pos2 += isl_basic_map_offset(bmap, type2);
12220 isl_int_set_si(bmap->eq[i][pos1], -1);
12221 isl_int_set_si(bmap->eq[i][pos2], 1);
12222 bmap = isl_basic_map_finalize(bmap);
12223 isl_space_free(space);
12224 return bmap;
12225 error:
12226 isl_space_free(space);
12227 isl_basic_map_free(bmap);
12228 return NULL;
12231 /* Add a constraint imposing that the given two dimensions are equal.
12233 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12234 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12236 isl_basic_map *eq;
12238 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12240 bmap = isl_basic_map_intersect(bmap, eq);
12242 return bmap;
12245 /* Add a constraint imposing that the given two dimensions are equal.
12247 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12248 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12250 isl_basic_map *bmap;
12252 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12254 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12256 return map;
12259 /* Add a constraint imposing that the given two dimensions have opposite values.
12261 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12262 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12264 isl_basic_map *bmap = NULL;
12265 int i;
12267 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12268 return isl_map_free(map);
12269 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12270 return isl_map_free(map);
12272 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12273 i = isl_basic_map_alloc_equality(bmap);
12274 if (i < 0)
12275 goto error;
12276 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12277 pos1 += isl_basic_map_offset(bmap, type1);
12278 pos2 += isl_basic_map_offset(bmap, type2);
12279 isl_int_set_si(bmap->eq[i][pos1], 1);
12280 isl_int_set_si(bmap->eq[i][pos2], 1);
12281 bmap = isl_basic_map_finalize(bmap);
12283 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12285 return map;
12286 error:
12287 isl_basic_map_free(bmap);
12288 isl_map_free(map);
12289 return NULL;
12292 /* Construct a constraint imposing that the value of the first dimension is
12293 * greater than or equal to that of the second.
12295 static __isl_give isl_constraint *constraint_order_ge(
12296 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12297 enum isl_dim_type type2, int pos2)
12299 isl_constraint *c;
12301 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12302 isl_space_check_range(space, type2, pos2, 1) < 0)
12303 space = isl_space_free(space);
12304 if (!space)
12305 return NULL;
12307 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12309 if (type1 == type2 && pos1 == pos2)
12310 return c;
12312 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12313 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12315 return c;
12318 /* Add a constraint imposing that the value of the first dimension is
12319 * greater than or equal to that of the second.
12321 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12322 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12324 isl_constraint *c;
12325 isl_space *space;
12327 if (type1 == type2 && pos1 == pos2)
12328 return bmap;
12329 space = isl_basic_map_get_space(bmap);
12330 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12331 bmap = isl_basic_map_add_constraint(bmap, c);
12333 return bmap;
12336 /* Add a constraint imposing that the value of the first dimension is
12337 * greater than or equal to that of the second.
12339 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12340 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12342 isl_constraint *c;
12343 isl_space *space;
12345 if (type1 == type2 && pos1 == pos2)
12346 return map;
12347 space = isl_map_get_space(map);
12348 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12349 map = isl_map_add_constraint(map, c);
12351 return map;
12354 /* Add a constraint imposing that the value of the first dimension is
12355 * less than or equal to that of the second.
12357 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12358 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12360 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12363 /* Construct a basic map where the value of the first dimension is
12364 * greater than that of the second.
12366 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12367 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12369 isl_basic_map *bmap = NULL;
12370 int i;
12372 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12373 isl_space_check_range(space, type2, pos2, 1) < 0)
12374 goto error;
12376 if (type1 == type2 && pos1 == pos2)
12377 return isl_basic_map_empty(space);
12379 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12380 i = isl_basic_map_alloc_inequality(bmap);
12381 if (i < 0)
12382 return isl_basic_map_free(bmap);
12383 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12384 pos1 += isl_basic_map_offset(bmap, type1);
12385 pos2 += isl_basic_map_offset(bmap, type2);
12386 isl_int_set_si(bmap->ineq[i][pos1], 1);
12387 isl_int_set_si(bmap->ineq[i][pos2], -1);
12388 isl_int_set_si(bmap->ineq[i][0], -1);
12389 bmap = isl_basic_map_finalize(bmap);
12391 return bmap;
12392 error:
12393 isl_space_free(space);
12394 isl_basic_map_free(bmap);
12395 return NULL;
12398 /* Add a constraint imposing that the value of the first dimension is
12399 * greater than that of the second.
12401 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12402 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12404 isl_basic_map *gt;
12406 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12408 bmap = isl_basic_map_intersect(bmap, gt);
12410 return bmap;
12413 /* Add a constraint imposing that the value of the first dimension is
12414 * greater than that of the second.
12416 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12417 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12419 isl_basic_map *bmap;
12421 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12423 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12425 return map;
12428 /* Add a constraint imposing that the value of the first dimension is
12429 * smaller than that of the second.
12431 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12432 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12434 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12437 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12438 int pos)
12440 isl_aff *div;
12441 isl_local_space *ls;
12443 if (!bmap)
12444 return NULL;
12446 if (!isl_basic_map_divs_known(bmap))
12447 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12448 "some divs are unknown", return NULL);
12450 ls = isl_basic_map_get_local_space(bmap);
12451 div = isl_local_space_get_div(ls, pos);
12452 isl_local_space_free(ls);
12454 return div;
12457 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12458 int pos)
12460 return isl_basic_map_get_div(bset, pos);
12463 /* Plug in "subs" for dimension "type", "pos" of "bset".
12465 * Let i be the dimension to replace and let "subs" be of the form
12467 * f/d
12469 * Any integer division with a non-zero coefficient for i,
12471 * floor((a i + g)/m)
12473 * is replaced by
12475 * floor((a f + d g)/(m d))
12477 * Constraints of the form
12479 * a i + g
12481 * are replaced by
12483 * a f + d g
12485 * We currently require that "subs" is an integral expression.
12486 * Handling rational expressions may require us to add stride constraints
12487 * as we do in isl_basic_set_preimage_multi_aff.
12489 __isl_give isl_basic_set *isl_basic_set_substitute(
12490 __isl_take isl_basic_set *bset,
12491 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12493 int i;
12494 isl_int v;
12495 isl_ctx *ctx;
12497 if (bset && isl_basic_set_plain_is_empty(bset))
12498 return bset;
12500 bset = isl_basic_set_cow(bset);
12501 if (!bset || !subs)
12502 goto error;
12504 ctx = isl_basic_set_get_ctx(bset);
12505 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12506 isl_die(ctx, isl_error_invalid,
12507 "spaces don't match", goto error);
12508 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12509 isl_die(ctx, isl_error_unsupported,
12510 "cannot handle divs yet", goto error);
12511 if (!isl_int_is_one(subs->v->el[0]))
12512 isl_die(ctx, isl_error_invalid,
12513 "can only substitute integer expressions", goto error);
12515 pos += isl_basic_set_offset(bset, type);
12517 isl_int_init(v);
12519 for (i = 0; i < bset->n_eq; ++i) {
12520 if (isl_int_is_zero(bset->eq[i][pos]))
12521 continue;
12522 isl_int_set(v, bset->eq[i][pos]);
12523 isl_int_set_si(bset->eq[i][pos], 0);
12524 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12525 v, subs->v->el + 1, subs->v->size - 1);
12528 for (i = 0; i < bset->n_ineq; ++i) {
12529 if (isl_int_is_zero(bset->ineq[i][pos]))
12530 continue;
12531 isl_int_set(v, bset->ineq[i][pos]);
12532 isl_int_set_si(bset->ineq[i][pos], 0);
12533 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12534 v, subs->v->el + 1, subs->v->size - 1);
12537 for (i = 0; i < bset->n_div; ++i) {
12538 if (isl_int_is_zero(bset->div[i][1 + pos]))
12539 continue;
12540 isl_int_set(v, bset->div[i][1 + pos]);
12541 isl_int_set_si(bset->div[i][1 + pos], 0);
12542 isl_seq_combine(bset->div[i] + 1,
12543 subs->v->el[0], bset->div[i] + 1,
12544 v, subs->v->el + 1, subs->v->size - 1);
12545 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12548 isl_int_clear(v);
12550 bset = isl_basic_set_simplify(bset);
12551 return isl_basic_set_finalize(bset);
12552 error:
12553 isl_basic_set_free(bset);
12554 return NULL;
12557 /* Plug in "subs" for dimension "type", "pos" of "set".
12559 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12560 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12562 int i;
12564 if (set && isl_set_plain_is_empty(set))
12565 return set;
12567 set = isl_set_cow(set);
12568 if (!set || !subs)
12569 goto error;
12571 for (i = set->n - 1; i >= 0; --i) {
12572 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12573 set = set_from_map(remove_if_empty(set_to_map(set), i));
12574 if (!set)
12575 return NULL;
12578 return set;
12579 error:
12580 isl_set_free(set);
12581 return NULL;
12584 /* Check if the range of "ma" is compatible with the domain or range
12585 * (depending on "type") of "bmap".
12587 static isl_stat check_basic_map_compatible_range_multi_aff(
12588 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12589 __isl_keep isl_multi_aff *ma)
12591 isl_bool m;
12592 isl_space *ma_space;
12594 ma_space = isl_multi_aff_get_space(ma);
12596 m = isl_space_has_equal_params(bmap->dim, ma_space);
12597 if (m < 0)
12598 goto error;
12599 if (!m)
12600 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12601 "parameters don't match", goto error);
12602 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12603 if (m < 0)
12604 goto error;
12605 if (!m)
12606 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12607 "spaces don't match", goto error);
12609 isl_space_free(ma_space);
12610 return isl_stat_ok;
12611 error:
12612 isl_space_free(ma_space);
12613 return isl_stat_error;
12616 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12617 * coefficients before the transformed range of dimensions,
12618 * the "n_after" coefficients after the transformed range of dimensions
12619 * and the coefficients of the other divs in "bmap".
12621 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12622 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12624 int i;
12625 int n_param;
12626 int n_set;
12627 isl_local_space *ls;
12629 if (n_div == 0)
12630 return bmap;
12632 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12633 if (!ls)
12634 return isl_basic_map_free(bmap);
12636 n_param = isl_local_space_dim(ls, isl_dim_param);
12637 n_set = isl_local_space_dim(ls, isl_dim_set);
12638 for (i = 0; i < n_div; ++i) {
12639 int o_bmap = 0, o_ls = 0;
12641 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12642 o_bmap += 1 + 1 + n_param;
12643 o_ls += 1 + 1 + n_param;
12644 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12645 o_bmap += n_before;
12646 isl_seq_cpy(bmap->div[i] + o_bmap,
12647 ls->div->row[i] + o_ls, n_set);
12648 o_bmap += n_set;
12649 o_ls += n_set;
12650 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12651 o_bmap += n_after;
12652 isl_seq_cpy(bmap->div[i] + o_bmap,
12653 ls->div->row[i] + o_ls, n_div);
12654 o_bmap += n_div;
12655 o_ls += n_div;
12656 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12657 bmap = isl_basic_map_add_div_constraints(bmap, i);
12658 if (!bmap)
12659 goto error;
12662 isl_local_space_free(ls);
12663 return bmap;
12664 error:
12665 isl_local_space_free(ls);
12666 return isl_basic_map_free(bmap);
12669 /* How many stride constraints does "ma" enforce?
12670 * That is, how many of the affine expressions have a denominator
12671 * different from one?
12673 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12675 int i;
12676 int strides = 0;
12678 for (i = 0; i < ma->n; ++i)
12679 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12680 strides++;
12682 return strides;
12685 /* For each affine expression in ma of the form
12687 * x_i = (f_i y + h_i)/m_i
12689 * with m_i different from one, add a constraint to "bmap"
12690 * of the form
12692 * f_i y + h_i = m_i alpha_i
12694 * with alpha_i an additional existentially quantified variable.
12696 * The input variables of "ma" correspond to a subset of the variables
12697 * of "bmap". There are "n_before" variables in "bmap" before this
12698 * subset and "n_after" variables after this subset.
12699 * The integer divisions of the affine expressions in "ma" are assumed
12700 * to have been aligned. There are "n_div_ma" of them and
12701 * they appear first in "bmap", straight after the "n_after" variables.
12703 static __isl_give isl_basic_map *add_ma_strides(
12704 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12705 int n_before, int n_after, int n_div_ma)
12707 int i, k;
12708 int div;
12709 int total;
12710 int n_param;
12711 int n_in;
12713 total = isl_basic_map_total_dim(bmap);
12714 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12715 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12716 for (i = 0; i < ma->n; ++i) {
12717 int o_bmap = 0, o_ma = 1;
12719 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12720 continue;
12721 div = isl_basic_map_alloc_div(bmap);
12722 k = isl_basic_map_alloc_equality(bmap);
12723 if (div < 0 || k < 0)
12724 goto error;
12725 isl_int_set_si(bmap->div[div][0], 0);
12726 isl_seq_cpy(bmap->eq[k] + o_bmap,
12727 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12728 o_bmap += 1 + n_param;
12729 o_ma += 1 + n_param;
12730 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12731 o_bmap += n_before;
12732 isl_seq_cpy(bmap->eq[k] + o_bmap,
12733 ma->u.p[i]->v->el + o_ma, n_in);
12734 o_bmap += n_in;
12735 o_ma += n_in;
12736 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12737 o_bmap += n_after;
12738 isl_seq_cpy(bmap->eq[k] + o_bmap,
12739 ma->u.p[i]->v->el + o_ma, n_div_ma);
12740 o_bmap += n_div_ma;
12741 o_ma += n_div_ma;
12742 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12743 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12744 total++;
12747 return bmap;
12748 error:
12749 isl_basic_map_free(bmap);
12750 return NULL;
12753 /* Replace the domain or range space (depending on "type) of "space" by "set".
12755 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12756 enum isl_dim_type type, __isl_take isl_space *set)
12758 if (type == isl_dim_in) {
12759 space = isl_space_range(space);
12760 space = isl_space_map_from_domain_and_range(set, space);
12761 } else {
12762 space = isl_space_domain(space);
12763 space = isl_space_map_from_domain_and_range(space, set);
12766 return space;
12769 /* Compute the preimage of the domain or range (depending on "type")
12770 * of "bmap" under the function represented by "ma".
12771 * In other words, plug in "ma" in the domain or range of "bmap".
12772 * The result is a basic map that lives in the same space as "bmap"
12773 * except that the domain or range has been replaced by
12774 * the domain space of "ma".
12776 * If bmap is represented by
12778 * A(p) + S u + B x + T v + C(divs) >= 0,
12780 * where u and x are input and output dimensions if type == isl_dim_out
12781 * while x and v are input and output dimensions if type == isl_dim_in,
12782 * and ma is represented by
12784 * x = D(p) + F(y) + G(divs')
12786 * then the result is
12788 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12790 * The divs in the input set are similarly adjusted.
12791 * In particular
12793 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12795 * becomes
12797 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12798 * B_i G(divs') + c_i(divs))/n_i)
12800 * If bmap is not a rational map and if F(y) involves any denominators
12802 * x_i = (f_i y + h_i)/m_i
12804 * then additional constraints are added to ensure that we only
12805 * map back integer points. That is we enforce
12807 * f_i y + h_i = m_i alpha_i
12809 * with alpha_i an additional existentially quantified variable.
12811 * We first copy over the divs from "ma".
12812 * Then we add the modified constraints and divs from "bmap".
12813 * Finally, we add the stride constraints, if needed.
12815 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12816 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12817 __isl_take isl_multi_aff *ma)
12819 int i, k;
12820 isl_space *space;
12821 isl_basic_map *res = NULL;
12822 int n_before, n_after, n_div_bmap, n_div_ma;
12823 isl_int f, c1, c2, g;
12824 isl_bool rational;
12825 int strides;
12827 isl_int_init(f);
12828 isl_int_init(c1);
12829 isl_int_init(c2);
12830 isl_int_init(g);
12832 ma = isl_multi_aff_align_divs(ma);
12833 if (!bmap || !ma)
12834 goto error;
12835 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12836 goto error;
12838 if (type == isl_dim_in) {
12839 n_before = 0;
12840 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12841 } else {
12842 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12843 n_after = 0;
12845 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12846 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12848 space = isl_multi_aff_get_domain_space(ma);
12849 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12850 rational = isl_basic_map_is_rational(bmap);
12851 strides = rational ? 0 : multi_aff_strides(ma);
12852 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12853 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12854 if (rational)
12855 res = isl_basic_map_set_rational(res);
12857 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12858 if (isl_basic_map_alloc_div(res) < 0)
12859 goto error;
12861 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
12862 if (!res)
12863 goto error;
12865 for (i = 0; i < bmap->n_eq; ++i) {
12866 k = isl_basic_map_alloc_equality(res);
12867 if (k < 0)
12868 goto error;
12869 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12870 n_after, n_div_ma, n_div_bmap,
12871 f, c1, c2, g, 0) < 0)
12872 goto error;
12875 for (i = 0; i < bmap->n_ineq; ++i) {
12876 k = isl_basic_map_alloc_inequality(res);
12877 if (k < 0)
12878 goto error;
12879 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12880 n_after, n_div_ma, n_div_bmap,
12881 f, c1, c2, g, 0) < 0)
12882 goto error;
12885 for (i = 0; i < bmap->n_div; ++i) {
12886 if (isl_int_is_zero(bmap->div[i][0])) {
12887 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12888 continue;
12890 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12891 n_before, n_after, n_div_ma, n_div_bmap,
12892 f, c1, c2, g, 1) < 0)
12893 goto error;
12896 if (strides)
12897 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12899 isl_int_clear(f);
12900 isl_int_clear(c1);
12901 isl_int_clear(c2);
12902 isl_int_clear(g);
12903 isl_basic_map_free(bmap);
12904 isl_multi_aff_free(ma);
12905 res = isl_basic_map_simplify(res);
12906 return isl_basic_map_finalize(res);
12907 error:
12908 isl_int_clear(f);
12909 isl_int_clear(c1);
12910 isl_int_clear(c2);
12911 isl_int_clear(g);
12912 isl_basic_map_free(bmap);
12913 isl_multi_aff_free(ma);
12914 isl_basic_map_free(res);
12915 return NULL;
12918 /* Compute the preimage of "bset" under the function represented by "ma".
12919 * In other words, plug in "ma" in "bset". The result is a basic set
12920 * that lives in the domain space of "ma".
12922 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12923 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12925 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12928 /* Compute the preimage of the domain of "bmap" under the function
12929 * represented by "ma".
12930 * In other words, plug in "ma" in the domain of "bmap".
12931 * The result is a basic map that lives in the same space as "bmap"
12932 * except that the domain has been replaced by the domain space of "ma".
12934 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12935 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12937 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12940 /* Compute the preimage of the range of "bmap" under the function
12941 * represented by "ma".
12942 * In other words, plug in "ma" in the range of "bmap".
12943 * The result is a basic map that lives in the same space as "bmap"
12944 * except that the range has been replaced by the domain space of "ma".
12946 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12947 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12949 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12952 /* Check if the range of "ma" is compatible with the domain or range
12953 * (depending on "type") of "map".
12954 * Return isl_stat_error if anything is wrong.
12956 static isl_stat check_map_compatible_range_multi_aff(
12957 __isl_keep isl_map *map, enum isl_dim_type type,
12958 __isl_keep isl_multi_aff *ma)
12960 isl_bool m;
12961 isl_space *ma_space;
12963 ma_space = isl_multi_aff_get_space(ma);
12964 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12965 isl_space_free(ma_space);
12966 if (m < 0)
12967 return isl_stat_error;
12968 if (!m)
12969 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12970 "spaces don't match", return isl_stat_error);
12971 return isl_stat_ok;
12974 /* Compute the preimage of the domain or range (depending on "type")
12975 * of "map" under the function represented by "ma".
12976 * In other words, plug in "ma" in the domain or range of "map".
12977 * The result is a map that lives in the same space as "map"
12978 * except that the domain or range has been replaced by
12979 * the domain space of "ma".
12981 * The parameters are assumed to have been aligned.
12983 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12984 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12986 int i;
12987 isl_space *space;
12989 map = isl_map_cow(map);
12990 ma = isl_multi_aff_align_divs(ma);
12991 if (!map || !ma)
12992 goto error;
12993 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12994 goto error;
12996 for (i = 0; i < map->n; ++i) {
12997 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12998 isl_multi_aff_copy(ma));
12999 if (!map->p[i])
13000 goto error;
13003 space = isl_multi_aff_get_domain_space(ma);
13004 space = isl_space_set(isl_map_get_space(map), type, space);
13006 isl_space_free(map->dim);
13007 map->dim = space;
13008 if (!map->dim)
13009 goto error;
13011 isl_multi_aff_free(ma);
13012 if (map->n > 1)
13013 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13014 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13015 return map;
13016 error:
13017 isl_multi_aff_free(ma);
13018 isl_map_free(map);
13019 return NULL;
13022 /* Compute the preimage of the domain or range (depending on "type")
13023 * of "map" under the function represented by "ma".
13024 * In other words, plug in "ma" in the domain or range of "map".
13025 * The result is a map that lives in the same space as "map"
13026 * except that the domain or range has been replaced by
13027 * the domain space of "ma".
13029 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13030 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13032 isl_bool aligned;
13034 if (!map || !ma)
13035 goto error;
13037 aligned = isl_map_space_has_equal_params(map, ma->space);
13038 if (aligned < 0)
13039 goto error;
13040 if (aligned)
13041 return map_preimage_multi_aff(map, type, ma);
13043 if (isl_map_check_named_params(map) < 0)
13044 goto error;
13045 if (!isl_space_has_named_params(ma->space))
13046 isl_die(map->ctx, isl_error_invalid,
13047 "unaligned unnamed parameters", goto error);
13048 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13049 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13051 return map_preimage_multi_aff(map, type, ma);
13052 error:
13053 isl_multi_aff_free(ma);
13054 return isl_map_free(map);
13057 /* Compute the preimage of "set" under the function represented by "ma".
13058 * In other words, plug in "ma" in "set". The result is a set
13059 * that lives in the domain space of "ma".
13061 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13062 __isl_take isl_multi_aff *ma)
13064 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13067 /* Compute the preimage of the domain of "map" under the function
13068 * represented by "ma".
13069 * In other words, plug in "ma" in the domain of "map".
13070 * The result is a map that lives in the same space as "map"
13071 * except that the domain has been replaced by the domain space of "ma".
13073 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13074 __isl_take isl_multi_aff *ma)
13076 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13079 /* Compute the preimage of the range of "map" under the function
13080 * represented by "ma".
13081 * In other words, plug in "ma" in the range of "map".
13082 * The result is a map that lives in the same space as "map"
13083 * except that the range has been replaced by the domain space of "ma".
13085 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13086 __isl_take isl_multi_aff *ma)
13088 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13091 /* Compute the preimage of "map" under the function represented by "pma".
13092 * In other words, plug in "pma" in the domain or range of "map".
13093 * The result is a map that lives in the same space as "map",
13094 * except that the space of type "type" has been replaced by
13095 * the domain space of "pma".
13097 * The parameters of "map" and "pma" are assumed to have been aligned.
13099 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13100 __isl_take isl_map *map, enum isl_dim_type type,
13101 __isl_take isl_pw_multi_aff *pma)
13103 int i;
13104 isl_map *res;
13106 if (!pma)
13107 goto error;
13109 if (pma->n == 0) {
13110 isl_pw_multi_aff_free(pma);
13111 res = isl_map_empty(isl_map_get_space(map));
13112 isl_map_free(map);
13113 return res;
13116 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13117 isl_multi_aff_copy(pma->p[0].maff));
13118 if (type == isl_dim_in)
13119 res = isl_map_intersect_domain(res,
13120 isl_map_copy(pma->p[0].set));
13121 else
13122 res = isl_map_intersect_range(res,
13123 isl_map_copy(pma->p[0].set));
13125 for (i = 1; i < pma->n; ++i) {
13126 isl_map *res_i;
13128 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13129 isl_multi_aff_copy(pma->p[i].maff));
13130 if (type == isl_dim_in)
13131 res_i = isl_map_intersect_domain(res_i,
13132 isl_map_copy(pma->p[i].set));
13133 else
13134 res_i = isl_map_intersect_range(res_i,
13135 isl_map_copy(pma->p[i].set));
13136 res = isl_map_union(res, res_i);
13139 isl_pw_multi_aff_free(pma);
13140 isl_map_free(map);
13141 return res;
13142 error:
13143 isl_pw_multi_aff_free(pma);
13144 isl_map_free(map);
13145 return NULL;
13148 /* Compute the preimage of "map" under the function represented by "pma".
13149 * In other words, plug in "pma" in the domain or range of "map".
13150 * The result is a map that lives in the same space as "map",
13151 * except that the space of type "type" has been replaced by
13152 * the domain space of "pma".
13154 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13155 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13157 isl_bool aligned;
13159 if (!map || !pma)
13160 goto error;
13162 aligned = isl_map_space_has_equal_params(map, pma->dim);
13163 if (aligned < 0)
13164 goto error;
13165 if (aligned)
13166 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13168 if (isl_map_check_named_params(map) < 0)
13169 goto error;
13170 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13171 goto error;
13172 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13173 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13175 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13176 error:
13177 isl_pw_multi_aff_free(pma);
13178 return isl_map_free(map);
13181 /* Compute the preimage of "set" under the function represented by "pma".
13182 * In other words, plug in "pma" in "set". The result is a set
13183 * that lives in the domain space of "pma".
13185 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13186 __isl_take isl_pw_multi_aff *pma)
13188 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13191 /* Compute the preimage of the domain of "map" under the function
13192 * represented by "pma".
13193 * In other words, plug in "pma" in the domain of "map".
13194 * The result is a map that lives in the same space as "map",
13195 * except that domain space has been replaced by the domain space of "pma".
13197 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13198 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13200 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13203 /* Compute the preimage of the range of "map" under the function
13204 * represented by "pma".
13205 * In other words, plug in "pma" in the range of "map".
13206 * The result is a map that lives in the same space as "map",
13207 * except that range space has been replaced by the domain space of "pma".
13209 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13210 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13212 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13215 /* Compute the preimage of "map" under the function represented by "mpa".
13216 * In other words, plug in "mpa" in the domain or range of "map".
13217 * The result is a map that lives in the same space as "map",
13218 * except that the space of type "type" has been replaced by
13219 * the domain space of "mpa".
13221 * If the map does not involve any constraints that refer to the
13222 * dimensions of the substituted space, then the only possible
13223 * effect of "mpa" on the map is to map the space to a different space.
13224 * We create a separate isl_multi_aff to effectuate this change
13225 * in order to avoid spurious splitting of the map along the pieces
13226 * of "mpa".
13227 * If "mpa" has a non-trivial explicit domain, however,
13228 * then the full substitution should be performed.
13230 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13231 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13233 int n;
13234 isl_bool full;
13235 isl_pw_multi_aff *pma;
13237 if (!map || !mpa)
13238 goto error;
13240 n = isl_map_dim(map, type);
13241 full = isl_map_involves_dims(map, type, 0, n);
13242 if (full >= 0 && !full)
13243 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13244 if (full < 0)
13245 goto error;
13246 if (!full) {
13247 isl_space *space;
13248 isl_multi_aff *ma;
13250 space = isl_multi_pw_aff_get_space(mpa);
13251 isl_multi_pw_aff_free(mpa);
13252 ma = isl_multi_aff_zero(space);
13253 return isl_map_preimage_multi_aff(map, type, ma);
13256 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13257 return isl_map_preimage_pw_multi_aff(map, type, pma);
13258 error:
13259 isl_map_free(map);
13260 isl_multi_pw_aff_free(mpa);
13261 return NULL;
13264 /* Compute the preimage of "map" under the function represented by "mpa".
13265 * In other words, plug in "mpa" in the domain "map".
13266 * The result is a map that lives in the same space as "map",
13267 * except that domain space has been replaced by the domain space of "mpa".
13269 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13270 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13272 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13275 /* Compute the preimage of "set" by the function represented by "mpa".
13276 * In other words, plug in "mpa" in "set".
13278 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13279 __isl_take isl_multi_pw_aff *mpa)
13281 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13284 /* Return a copy of the equality constraints of "bset" as a matrix.
13286 __isl_give isl_mat *isl_basic_set_extract_equalities(
13287 __isl_keep isl_basic_set *bset)
13289 isl_ctx *ctx;
13290 unsigned total;
13292 if (!bset)
13293 return NULL;
13295 ctx = isl_basic_set_get_ctx(bset);
13296 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13297 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13300 /* Are the "n" "coefficients" starting at "first" of the integer division
13301 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13302 * to each other?
13303 * The "coefficient" at position 0 is the denominator.
13304 * The "coefficient" at position 1 is the constant term.
13306 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13307 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13308 unsigned first, unsigned n)
13310 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13311 return isl_bool_error;
13312 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13313 return isl_bool_error;
13314 return isl_seq_eq(bmap1->div[pos1] + first,
13315 bmap2->div[pos2] + first, n);
13318 /* Are the integer division expressions at position "pos1" in "bmap1" and
13319 * "pos2" in "bmap2" equal to each other, except that the constant terms
13320 * are different?
13322 isl_bool isl_basic_map_equal_div_expr_except_constant(
13323 __isl_keep isl_basic_map *bmap1, int pos1,
13324 __isl_keep isl_basic_map *bmap2, int pos2)
13326 isl_bool equal;
13327 unsigned total;
13329 if (!bmap1 || !bmap2)
13330 return isl_bool_error;
13331 total = isl_basic_map_total_dim(bmap1);
13332 if (total != isl_basic_map_total_dim(bmap2))
13333 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13334 "incomparable div expressions", return isl_bool_error);
13335 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13336 0, 1);
13337 if (equal < 0 || !equal)
13338 return equal;
13339 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13340 1, 1);
13341 if (equal < 0 || equal)
13342 return isl_bool_not(equal);
13343 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13344 2, total);
13347 /* Replace the numerator of the constant term of the integer division
13348 * expression at position "div" in "bmap" by "value".
13349 * The caller guarantees that this does not change the meaning
13350 * of the input.
13352 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13353 __isl_take isl_basic_map *bmap, int div, int value)
13355 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13356 return isl_basic_map_free(bmap);
13358 isl_int_set_si(bmap->div[div][1], value);
13360 return bmap;
13363 /* Is the point "inner" internal to inequality constraint "ineq"
13364 * of "bset"?
13365 * The point is considered to be internal to the inequality constraint,
13366 * if it strictly lies on the positive side of the inequality constraint,
13367 * or if it lies on the constraint and the constraint is lexico-positive.
13369 static isl_bool is_internal(__isl_keep isl_vec *inner,
13370 __isl_keep isl_basic_set *bset, int ineq)
13372 isl_ctx *ctx;
13373 int pos;
13374 unsigned total;
13376 if (!inner || !bset)
13377 return isl_bool_error;
13379 ctx = isl_basic_set_get_ctx(bset);
13380 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13381 &ctx->normalize_gcd);
13382 if (!isl_int_is_zero(ctx->normalize_gcd))
13383 return isl_int_is_nonneg(ctx->normalize_gcd);
13385 total = isl_basic_set_dim(bset, isl_dim_all);
13386 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13387 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13390 /* Tighten the inequality constraints of "bset" that are outward with respect
13391 * to the point "vec".
13392 * That is, tighten the constraints that are not satisfied by "vec".
13394 * "vec" is a point internal to some superset S of "bset" that is used
13395 * to make the subsets of S disjoint, by tightening one half of the constraints
13396 * that separate two subsets. In particular, the constraints of S
13397 * are all satisfied by "vec" and should not be tightened.
13398 * Of the internal constraints, those that have "vec" on the outside
13399 * are tightened. The shared facet is included in the adjacent subset
13400 * with the opposite constraint.
13401 * For constraints that saturate "vec", this criterion cannot be used
13402 * to determine which of the two sides should be tightened.
13403 * Instead, the sign of the first non-zero coefficient is used
13404 * to make this choice. Note that this second criterion is never used
13405 * on the constraints of S since "vec" is interior to "S".
13407 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13408 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13410 int j;
13412 bset = isl_basic_set_cow(bset);
13413 if (!bset)
13414 return NULL;
13415 for (j = 0; j < bset->n_ineq; ++j) {
13416 isl_bool internal;
13418 internal = is_internal(vec, bset, j);
13419 if (internal < 0)
13420 return isl_basic_set_free(bset);
13421 if (internal)
13422 continue;
13423 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13426 return bset;
13429 /* Replace the variables x of type "type" starting at "first" in "bmap"
13430 * by x' with x = M x' with M the matrix trans.
13431 * That is, replace the corresponding coefficients c by c M.
13433 * The transformation matrix should be a square matrix.
13435 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13436 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13437 __isl_take isl_mat *trans)
13439 unsigned pos;
13441 bmap = isl_basic_map_cow(bmap);
13442 if (!bmap || !trans)
13443 goto error;
13445 if (trans->n_row != trans->n_col)
13446 isl_die(trans->ctx, isl_error_invalid,
13447 "expecting square transformation matrix", goto error);
13448 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13449 goto error;
13451 pos = isl_basic_map_offset(bmap, type) + first;
13453 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13454 isl_mat_copy(trans)) < 0)
13455 goto error;
13456 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13457 isl_mat_copy(trans)) < 0)
13458 goto error;
13459 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13460 isl_mat_copy(trans)) < 0)
13461 goto error;
13463 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13464 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13466 isl_mat_free(trans);
13467 return bmap;
13468 error:
13469 isl_mat_free(trans);
13470 isl_basic_map_free(bmap);
13471 return NULL;
13474 /* Replace the variables x of type "type" starting at "first" in "bset"
13475 * by x' with x = M x' with M the matrix trans.
13476 * That is, replace the corresponding coefficients c by c M.
13478 * The transformation matrix should be a square matrix.
13480 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13481 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13482 __isl_take isl_mat *trans)
13484 return isl_basic_map_transform_dims(bset, type, first, trans);