isl_qpolynomial_drop_dims: use isl_qpolynomial_domain_var_offset
[isl.git] / isl_map.c
bloba8ac11fdaf76918ce7a9eac54d42130b8f422093
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_space_dim(bset->dim, 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_space_dim(bset->dim, 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;
3501 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3502 goto error;
3503 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3504 isl_space_dim(bmap1->dim, isl_dim_param) &&
3505 isl_space_dim(bmap2->dim, isl_dim_all) !=
3506 isl_space_dim(bmap2->dim, isl_dim_param))
3507 return isl_basic_map_intersect(bmap2, bmap1);
3509 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3510 isl_space_dim(bmap2->dim, isl_dim_param))
3511 isl_assert(bmap1->ctx,
3512 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3514 if (isl_basic_map_plain_is_empty(bmap1)) {
3515 isl_basic_map_free(bmap2);
3516 return bmap1;
3518 if (isl_basic_map_plain_is_empty(bmap2)) {
3519 isl_basic_map_free(bmap1);
3520 return bmap2;
3523 if (bmap1->sample &&
3524 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3525 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3526 sample = isl_vec_copy(bmap1->sample);
3527 else if (bmap2->sample &&
3528 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3529 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3530 sample = isl_vec_copy(bmap2->sample);
3532 bmap1 = isl_basic_map_cow(bmap1);
3533 if (!bmap1)
3534 goto error;
3535 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3536 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3537 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3539 if (!bmap1)
3540 isl_vec_free(sample);
3541 else if (sample) {
3542 isl_vec_free(bmap1->sample);
3543 bmap1->sample = sample;
3546 bmap1 = isl_basic_map_simplify(bmap1);
3547 return isl_basic_map_finalize(bmap1);
3548 error:
3549 if (sample)
3550 isl_vec_free(sample);
3551 isl_basic_map_free(bmap1);
3552 isl_basic_map_free(bmap2);
3553 return NULL;
3556 struct isl_basic_set *isl_basic_set_intersect(
3557 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3559 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3560 bset_to_bmap(bset2)));
3563 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3564 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3566 return isl_basic_set_intersect(bset1, bset2);
3569 /* Special case of isl_map_intersect, where both map1 and map2
3570 * are convex, without any divs and such that either map1 or map2
3571 * contains a single constraint. This constraint is then simply
3572 * added to the other map.
3574 static __isl_give isl_map *map_intersect_add_constraint(
3575 __isl_take isl_map *map1, __isl_take isl_map *map2)
3577 isl_assert(map1->ctx, map1->n == 1, goto error);
3578 isl_assert(map2->ctx, map1->n == 1, goto error);
3579 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3580 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3582 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3583 return isl_map_intersect(map2, map1);
3585 map1 = isl_map_cow(map1);
3586 if (!map1)
3587 goto error;
3588 if (isl_map_plain_is_empty(map1)) {
3589 isl_map_free(map2);
3590 return map1;
3592 if (map2->p[0]->n_eq == 1)
3593 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3594 else
3595 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3596 map2->p[0]->ineq[0]);
3598 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3599 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3600 if (!map1->p[0])
3601 goto error;
3603 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3604 isl_basic_map_free(map1->p[0]);
3605 map1->n = 0;
3608 isl_map_free(map2);
3610 map1 = isl_map_unmark_normalized(map1);
3611 return map1;
3612 error:
3613 isl_map_free(map1);
3614 isl_map_free(map2);
3615 return NULL;
3618 /* map2 may be either a parameter domain or a map living in the same
3619 * space as map1.
3621 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3622 __isl_take isl_map *map2)
3624 unsigned flags = 0;
3625 isl_bool equal;
3626 isl_map *result;
3627 int i, j;
3629 if (!map1 || !map2)
3630 goto error;
3632 if ((isl_map_plain_is_empty(map1) ||
3633 isl_map_plain_is_universe(map2)) &&
3634 isl_space_is_equal(map1->dim, map2->dim)) {
3635 isl_map_free(map2);
3636 return map1;
3638 if ((isl_map_plain_is_empty(map2) ||
3639 isl_map_plain_is_universe(map1)) &&
3640 isl_space_is_equal(map1->dim, map2->dim)) {
3641 isl_map_free(map1);
3642 return map2;
3645 if (map1->n == 1 && map2->n == 1 &&
3646 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3647 isl_space_is_equal(map1->dim, map2->dim) &&
3648 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3649 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3650 return map_intersect_add_constraint(map1, map2);
3652 equal = isl_map_plain_is_equal(map1, map2);
3653 if (equal < 0)
3654 goto error;
3655 if (equal) {
3656 isl_map_free(map2);
3657 return map1;
3660 if (isl_space_dim(map2->dim, isl_dim_all) !=
3661 isl_space_dim(map2->dim, isl_dim_param))
3662 isl_assert(map1->ctx,
3663 isl_space_is_equal(map1->dim, map2->dim), goto error);
3665 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3666 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3667 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3669 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3670 map1->n * map2->n, flags);
3671 if (!result)
3672 goto error;
3673 for (i = 0; i < map1->n; ++i)
3674 for (j = 0; j < map2->n; ++j) {
3675 struct isl_basic_map *part;
3676 part = isl_basic_map_intersect(
3677 isl_basic_map_copy(map1->p[i]),
3678 isl_basic_map_copy(map2->p[j]));
3679 if (isl_basic_map_is_empty(part) < 0)
3680 part = isl_basic_map_free(part);
3681 result = isl_map_add_basic_map(result, part);
3682 if (!result)
3683 goto error;
3685 isl_map_free(map1);
3686 isl_map_free(map2);
3687 return result;
3688 error:
3689 isl_map_free(map1);
3690 isl_map_free(map2);
3691 return NULL;
3694 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3695 __isl_take isl_map *map2)
3697 if (!map1 || !map2)
3698 goto error;
3699 if (!isl_space_is_equal(map1->dim, map2->dim))
3700 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3701 "spaces don't match", goto error);
3702 return map_intersect_internal(map1, map2);
3703 error:
3704 isl_map_free(map1);
3705 isl_map_free(map2);
3706 return NULL;
3709 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3710 __isl_take isl_map *map2)
3712 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3715 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3717 return set_from_map(isl_map_intersect(set_to_map(set1),
3718 set_to_map(set2)));
3721 /* map_intersect_internal accepts intersections
3722 * with parameter domains, so we can just call that function.
3724 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3725 __isl_take isl_set *params)
3727 return map_intersect_internal(map, params);
3730 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3731 __isl_take isl_map *map2)
3733 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3736 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3737 __isl_take isl_set *params)
3739 return isl_map_intersect_params(set, params);
3742 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3744 isl_space *space;
3745 unsigned pos, n1, n2;
3747 if (!bmap)
3748 return NULL;
3749 bmap = isl_basic_map_cow(bmap);
3750 if (!bmap)
3751 return NULL;
3752 space = isl_space_reverse(isl_space_copy(bmap->dim));
3753 pos = isl_basic_map_offset(bmap, isl_dim_in);
3754 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3755 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3756 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3757 return isl_basic_map_reset_space(bmap, space);
3760 static __isl_give isl_basic_map *basic_map_space_reset(
3761 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3763 isl_space *space;
3765 if (!bmap)
3766 return NULL;
3767 if (!isl_space_is_named_or_nested(bmap->dim, type))
3768 return bmap;
3770 space = isl_basic_map_get_space(bmap);
3771 space = isl_space_reset(space, type);
3772 bmap = isl_basic_map_reset_space(bmap, space);
3773 return bmap;
3776 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3777 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3778 unsigned pos, unsigned n)
3780 isl_bool rational, is_empty;
3781 isl_space *res_space;
3782 struct isl_basic_map *res;
3783 struct isl_dim_map *dim_map;
3784 unsigned total, off;
3785 enum isl_dim_type t;
3787 if (n == 0)
3788 return basic_map_space_reset(bmap, type);
3790 is_empty = isl_basic_map_plain_is_empty(bmap);
3791 if (is_empty < 0)
3792 return isl_basic_map_free(bmap);
3793 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3794 type, pos, n);
3795 if (!res_space)
3796 return isl_basic_map_free(bmap);
3797 if (is_empty) {
3798 isl_basic_map_free(bmap);
3799 return isl_basic_map_empty(res_space);
3802 total = isl_basic_map_total_dim(bmap) + n;
3803 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3804 off = 0;
3805 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3806 if (t != type) {
3807 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3808 } else {
3809 unsigned size = isl_basic_map_dim(bmap, t);
3810 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3811 0, pos, off);
3812 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3813 pos, size - pos, off + pos + n);
3815 off += isl_space_dim(res_space, t);
3817 isl_dim_map_div(dim_map, bmap, off);
3819 res = isl_basic_map_alloc_space(res_space,
3820 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3821 rational = isl_basic_map_is_rational(bmap);
3822 if (rational < 0)
3823 res = isl_basic_map_free(res);
3824 if (rational)
3825 res = isl_basic_map_set_rational(res);
3826 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3827 return isl_basic_map_finalize(res);
3830 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3831 __isl_take isl_basic_set *bset,
3832 enum isl_dim_type type, unsigned pos, unsigned n)
3834 return isl_basic_map_insert_dims(bset, type, pos, n);
3837 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3838 enum isl_dim_type type, unsigned n)
3840 if (!bmap)
3841 return NULL;
3842 return isl_basic_map_insert_dims(bmap, type,
3843 isl_basic_map_dim(bmap, type), n);
3846 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3847 enum isl_dim_type type, unsigned n)
3849 if (!bset)
3850 return NULL;
3851 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3852 return isl_basic_map_add_dims(bset, type, n);
3853 error:
3854 isl_basic_set_free(bset);
3855 return NULL;
3858 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3859 enum isl_dim_type type)
3861 isl_space *space;
3863 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3864 return map;
3866 space = isl_map_get_space(map);
3867 space = isl_space_reset(space, type);
3868 map = isl_map_reset_space(map, space);
3869 return map;
3872 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3873 enum isl_dim_type type, unsigned pos, unsigned n)
3875 int i;
3877 if (n == 0)
3878 return map_space_reset(map, type);
3880 map = isl_map_cow(map);
3881 if (!map)
3882 return NULL;
3884 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3885 if (!map->dim)
3886 goto error;
3888 for (i = 0; i < map->n; ++i) {
3889 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3890 if (!map->p[i])
3891 goto error;
3894 return map;
3895 error:
3896 isl_map_free(map);
3897 return NULL;
3900 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3901 enum isl_dim_type type, unsigned pos, unsigned n)
3903 return isl_map_insert_dims(set, type, pos, n);
3906 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3907 enum isl_dim_type type, unsigned n)
3909 if (!map)
3910 return NULL;
3911 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3914 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3915 enum isl_dim_type type, unsigned n)
3917 if (!set)
3918 return NULL;
3919 isl_assert(set->ctx, type != isl_dim_in, goto error);
3920 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3921 error:
3922 isl_set_free(set);
3923 return NULL;
3926 __isl_give isl_basic_map *isl_basic_map_move_dims(
3927 __isl_take isl_basic_map *bmap,
3928 enum isl_dim_type dst_type, unsigned dst_pos,
3929 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3931 struct isl_dim_map *dim_map;
3932 struct isl_basic_map *res;
3933 enum isl_dim_type t;
3934 unsigned total, off;
3936 if (!bmap)
3937 return NULL;
3938 if (n == 0) {
3939 bmap = isl_basic_map_reset(bmap, src_type);
3940 bmap = isl_basic_map_reset(bmap, dst_type);
3941 return bmap;
3944 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3945 return isl_basic_map_free(bmap);
3947 if (dst_type == src_type && dst_pos == src_pos)
3948 return bmap;
3950 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3952 if (pos(bmap->dim, dst_type) + dst_pos ==
3953 pos(bmap->dim, src_type) + src_pos +
3954 ((src_type < dst_type) ? n : 0)) {
3955 bmap = isl_basic_map_cow(bmap);
3956 if (!bmap)
3957 return NULL;
3959 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3960 src_type, src_pos, n);
3961 if (!bmap->dim)
3962 goto error;
3964 bmap = isl_basic_map_finalize(bmap);
3966 return bmap;
3969 total = isl_basic_map_total_dim(bmap);
3970 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3972 off = 0;
3973 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3974 unsigned size = isl_space_dim(bmap->dim, t);
3975 if (t == dst_type) {
3976 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3977 0, dst_pos, off);
3978 off += dst_pos;
3979 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3980 src_pos, n, off);
3981 off += n;
3982 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3983 dst_pos, size - dst_pos, off);
3984 off += size - dst_pos;
3985 } else if (t == src_type) {
3986 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3987 0, src_pos, off);
3988 off += src_pos;
3989 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3990 src_pos + n, size - src_pos - n, off);
3991 off += size - src_pos - n;
3992 } else {
3993 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3994 off += size;
3997 isl_dim_map_div(dim_map, bmap, off);
3999 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4000 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4001 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4002 if (!bmap)
4003 goto error;
4005 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4006 src_type, src_pos, n);
4007 if (!bmap->dim)
4008 goto error;
4010 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4011 bmap = isl_basic_map_gauss(bmap, NULL);
4012 bmap = isl_basic_map_finalize(bmap);
4014 return bmap;
4015 error:
4016 isl_basic_map_free(bmap);
4017 return NULL;
4020 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4021 enum isl_dim_type dst_type, unsigned dst_pos,
4022 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4024 isl_basic_map *bmap = bset_to_bmap(bset);
4025 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4026 src_type, src_pos, n);
4027 return bset_from_bmap(bmap);
4030 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4031 enum isl_dim_type dst_type, unsigned dst_pos,
4032 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4034 if (!set)
4035 return NULL;
4036 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4037 return set_from_map(isl_map_move_dims(set_to_map(set),
4038 dst_type, dst_pos, src_type, src_pos, n));
4039 error:
4040 isl_set_free(set);
4041 return NULL;
4044 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4045 enum isl_dim_type dst_type, unsigned dst_pos,
4046 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4048 int i;
4050 if (n == 0) {
4051 map = isl_map_reset(map, src_type);
4052 map = isl_map_reset(map, dst_type);
4053 return map;
4056 if (isl_map_check_range(map, src_type, src_pos, n))
4057 return isl_map_free(map);
4059 if (dst_type == src_type && dst_pos == src_pos)
4060 return map;
4062 isl_assert(map->ctx, dst_type != src_type, goto error);
4064 map = isl_map_cow(map);
4065 if (!map)
4066 return NULL;
4068 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4069 if (!map->dim)
4070 goto error;
4072 for (i = 0; i < map->n; ++i) {
4073 map->p[i] = isl_basic_map_move_dims(map->p[i],
4074 dst_type, dst_pos,
4075 src_type, src_pos, n);
4076 if (!map->p[i])
4077 goto error;
4080 return map;
4081 error:
4082 isl_map_free(map);
4083 return NULL;
4086 /* Move the specified dimensions to the last columns right before
4087 * the divs. Don't change the dimension specification of bmap.
4088 * That's the responsibility of the caller.
4090 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4091 enum isl_dim_type type, unsigned first, unsigned n)
4093 struct isl_dim_map *dim_map;
4094 struct isl_basic_map *res;
4095 enum isl_dim_type t;
4096 unsigned total, off;
4098 if (!bmap)
4099 return NULL;
4100 if (pos(bmap->dim, type) + first + n ==
4101 1 + isl_space_dim(bmap->dim, isl_dim_all))
4102 return bmap;
4104 total = isl_basic_map_total_dim(bmap);
4105 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4107 off = 0;
4108 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4109 unsigned size = isl_space_dim(bmap->dim, t);
4110 if (t == type) {
4111 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4112 0, first, off);
4113 off += first;
4114 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4115 first, n, total - bmap->n_div - n);
4116 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4117 first + n, size - (first + n), off);
4118 off += size - (first + n);
4119 } else {
4120 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4121 off += size;
4124 isl_dim_map_div(dim_map, bmap, off + n);
4126 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4127 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4128 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4129 return res;
4132 /* Insert "n" rows in the divs of "bmap".
4134 * The number of columns is not changed, which means that the last
4135 * dimensions of "bmap" are being reintepreted as the new divs.
4136 * The space of "bmap" is not adjusted, however, which means
4137 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4138 * from the space of "bmap" is the responsibility of the caller.
4140 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4141 int n)
4143 int i;
4144 size_t row_size;
4145 isl_int **new_div;
4146 isl_int *old;
4148 bmap = isl_basic_map_cow(bmap);
4149 if (!bmap)
4150 return NULL;
4152 row_size = isl_basic_map_offset(bmap, isl_dim_div) + bmap->extra;
4153 old = bmap->block2.data;
4154 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4155 (bmap->extra + n) * (1 + row_size));
4156 if (!bmap->block2.data)
4157 return isl_basic_map_free(bmap);
4158 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4159 if (!new_div)
4160 return isl_basic_map_free(bmap);
4161 for (i = 0; i < n; ++i) {
4162 new_div[i] = bmap->block2.data +
4163 (bmap->extra + i) * (1 + row_size);
4164 isl_seq_clr(new_div[i], 1 + row_size);
4166 for (i = 0; i < bmap->extra; ++i)
4167 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4168 free(bmap->div);
4169 bmap->div = new_div;
4170 bmap->n_div += n;
4171 bmap->extra += n;
4173 return bmap;
4176 /* Drop constraints from "bmap" that only involve the variables
4177 * of "type" in the range [first, first + n] that are not related
4178 * to any of the variables outside that interval.
4179 * These constraints cannot influence the values for the variables
4180 * outside the interval, except in case they cause "bmap" to be empty.
4181 * Only drop the constraints if "bmap" is known to be non-empty.
4183 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4184 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4185 unsigned first, unsigned n)
4187 int i;
4188 int *groups;
4189 unsigned dim, n_div;
4190 isl_bool non_empty;
4192 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4193 if (non_empty < 0)
4194 return isl_basic_map_free(bmap);
4195 if (!non_empty)
4196 return bmap;
4198 dim = isl_basic_map_dim(bmap, isl_dim_all);
4199 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4200 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4201 if (!groups)
4202 return isl_basic_map_free(bmap);
4203 first += isl_basic_map_offset(bmap, type) - 1;
4204 for (i = 0; i < first; ++i)
4205 groups[i] = -1;
4206 for (i = first + n; i < dim - n_div; ++i)
4207 groups[i] = -1;
4209 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4211 return bmap;
4214 /* Turn the n dimensions of type type, starting at first
4215 * into existentially quantified variables.
4217 * If a subset of the projected out variables are unrelated
4218 * to any of the variables that remain, then the constraints
4219 * involving this subset are simply dropped first.
4221 __isl_give isl_basic_map *isl_basic_map_project_out(
4222 __isl_take isl_basic_map *bmap,
4223 enum isl_dim_type type, unsigned first, unsigned n)
4225 isl_bool empty;
4227 if (n == 0)
4228 return basic_map_space_reset(bmap, type);
4229 if (type == isl_dim_div)
4230 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4231 "cannot project out existentially quantified variables",
4232 return isl_basic_map_free(bmap));
4234 empty = isl_basic_map_plain_is_empty(bmap);
4235 if (empty < 0)
4236 return isl_basic_map_free(bmap);
4237 if (empty)
4238 bmap = isl_basic_map_set_to_empty(bmap);
4240 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4241 if (!bmap)
4242 return NULL;
4244 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4245 return isl_basic_map_remove_dims(bmap, type, first, n);
4247 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4248 return isl_basic_map_free(bmap);
4250 bmap = move_last(bmap, type, first, n);
4251 bmap = isl_basic_map_cow(bmap);
4252 bmap = insert_div_rows(bmap, n);
4253 if (!bmap)
4254 return NULL;
4256 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4257 if (!bmap->dim)
4258 goto error;
4259 bmap = isl_basic_map_simplify(bmap);
4260 bmap = isl_basic_map_drop_redundant_divs(bmap);
4261 return isl_basic_map_finalize(bmap);
4262 error:
4263 isl_basic_map_free(bmap);
4264 return NULL;
4267 /* Turn the n dimensions of type type, starting at first
4268 * into existentially quantified variables.
4270 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4271 enum isl_dim_type type, unsigned first, unsigned n)
4273 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4274 type, first, n));
4277 /* Turn the n dimensions of type type, starting at first
4278 * into existentially quantified variables.
4280 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4281 enum isl_dim_type type, unsigned first, unsigned n)
4283 int i;
4285 if (n == 0)
4286 return map_space_reset(map, type);
4288 if (isl_map_check_range(map, type, first, n) < 0)
4289 return isl_map_free(map);
4291 map = isl_map_cow(map);
4292 if (!map)
4293 return NULL;
4295 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4296 if (!map->dim)
4297 goto error;
4299 for (i = 0; i < map->n; ++i) {
4300 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4301 if (!map->p[i])
4302 goto error;
4305 return map;
4306 error:
4307 isl_map_free(map);
4308 return NULL;
4311 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4312 * into existentially quantified variables.
4314 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4315 enum isl_dim_type type, unsigned first, unsigned n)
4317 unsigned dim;
4319 if (isl_map_check_range(map, type, first, n) < 0)
4320 return isl_map_free(map);
4321 dim = isl_map_dim(map, type);
4322 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4323 map = isl_map_project_out(map, type, 0, first);
4324 return map;
4327 /* Turn the n dimensions of type type, starting at first
4328 * into existentially quantified variables.
4330 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4331 enum isl_dim_type type, unsigned first, unsigned n)
4333 return set_from_map(isl_map_project_out(set_to_map(set),
4334 type, first, n));
4337 /* Return a map that projects the elements in "set" onto their
4338 * "n" set dimensions starting at "first".
4339 * "type" should be equal to isl_dim_set.
4341 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4342 enum isl_dim_type type, unsigned first, unsigned n)
4344 int i;
4345 isl_map *map;
4347 if (type != isl_dim_set)
4348 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4349 "only set dimensions can be projected out", goto error);
4350 if (isl_set_check_range(set, type, first, n) < 0)
4351 return isl_set_free(set);
4353 map = isl_map_from_domain(set);
4354 map = isl_map_add_dims(map, isl_dim_out, n);
4355 for (i = 0; i < n; ++i)
4356 map = isl_map_equate(map, isl_dim_in, first + i,
4357 isl_dim_out, i);
4358 return map;
4359 error:
4360 isl_set_free(set);
4361 return NULL;
4364 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4365 unsigned n)
4367 int i, j;
4369 for (i = 0; i < n; ++i) {
4370 j = isl_basic_map_alloc_div(bmap);
4371 if (j < 0)
4372 goto error;
4373 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4375 return bmap;
4376 error:
4377 isl_basic_map_free(bmap);
4378 return NULL;
4381 struct isl_basic_map *isl_basic_map_apply_range(
4382 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4384 isl_space *space_result = NULL;
4385 struct isl_basic_map *bmap;
4386 unsigned n_in, n_out, n, nparam, total, pos;
4387 struct isl_dim_map *dim_map1, *dim_map2;
4389 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4390 goto error;
4391 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4392 bmap2->dim, isl_dim_in))
4393 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4394 "spaces don't match", goto error);
4396 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4397 isl_space_copy(bmap2->dim));
4399 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4400 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4401 n = isl_basic_map_dim(bmap1, isl_dim_out);
4402 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4404 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4405 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4406 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4407 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4408 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4409 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4410 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4411 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4412 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4413 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4414 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4416 bmap = isl_basic_map_alloc_space(space_result,
4417 bmap1->n_div + bmap2->n_div + n,
4418 bmap1->n_eq + bmap2->n_eq,
4419 bmap1->n_ineq + bmap2->n_ineq);
4420 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4421 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4422 bmap = add_divs(bmap, n);
4423 bmap = isl_basic_map_simplify(bmap);
4424 bmap = isl_basic_map_drop_redundant_divs(bmap);
4425 return isl_basic_map_finalize(bmap);
4426 error:
4427 isl_basic_map_free(bmap1);
4428 isl_basic_map_free(bmap2);
4429 return NULL;
4432 struct isl_basic_set *isl_basic_set_apply(
4433 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4435 if (!bset || !bmap)
4436 goto error;
4438 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4439 goto error);
4441 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4442 bmap));
4443 error:
4444 isl_basic_set_free(bset);
4445 isl_basic_map_free(bmap);
4446 return NULL;
4449 struct isl_basic_map *isl_basic_map_apply_domain(
4450 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4452 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4453 goto error;
4454 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4455 bmap2->dim, isl_dim_in))
4456 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4457 "spaces don't match", goto error);
4459 bmap1 = isl_basic_map_reverse(bmap1);
4460 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4461 return isl_basic_map_reverse(bmap1);
4462 error:
4463 isl_basic_map_free(bmap1);
4464 isl_basic_map_free(bmap2);
4465 return NULL;
4468 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4469 * A \cap B -> f(A) + f(B)
4471 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4472 __isl_take isl_basic_map *bmap2)
4474 unsigned n_in, n_out, nparam, total, pos;
4475 struct isl_basic_map *bmap = NULL;
4476 struct isl_dim_map *dim_map1, *dim_map2;
4477 int i;
4479 if (!bmap1 || !bmap2)
4480 goto error;
4482 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4483 goto error);
4485 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4486 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4487 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4489 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4490 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4491 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4492 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4493 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4494 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4495 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4496 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4497 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4498 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4499 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4501 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4502 bmap1->n_div + bmap2->n_div + 2 * n_out,
4503 bmap1->n_eq + bmap2->n_eq + n_out,
4504 bmap1->n_ineq + bmap2->n_ineq);
4505 for (i = 0; i < n_out; ++i) {
4506 int j = isl_basic_map_alloc_equality(bmap);
4507 if (j < 0)
4508 goto error;
4509 isl_seq_clr(bmap->eq[j], 1+total);
4510 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4511 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4512 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4514 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4515 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4516 bmap = add_divs(bmap, 2 * n_out);
4518 bmap = isl_basic_map_simplify(bmap);
4519 return isl_basic_map_finalize(bmap);
4520 error:
4521 isl_basic_map_free(bmap);
4522 isl_basic_map_free(bmap1);
4523 isl_basic_map_free(bmap2);
4524 return NULL;
4527 /* Given two maps A -> f(A) and B -> g(B), construct a map
4528 * A \cap B -> f(A) + f(B)
4530 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4531 __isl_take isl_map *map2)
4533 struct isl_map *result;
4534 int i, j;
4536 if (!map1 || !map2)
4537 goto error;
4539 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4541 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4542 map1->n * map2->n, 0);
4543 if (!result)
4544 goto error;
4545 for (i = 0; i < map1->n; ++i)
4546 for (j = 0; j < map2->n; ++j) {
4547 struct isl_basic_map *part;
4548 part = isl_basic_map_sum(
4549 isl_basic_map_copy(map1->p[i]),
4550 isl_basic_map_copy(map2->p[j]));
4551 if (isl_basic_map_is_empty(part))
4552 isl_basic_map_free(part);
4553 else
4554 result = isl_map_add_basic_map(result, part);
4555 if (!result)
4556 goto error;
4558 isl_map_free(map1);
4559 isl_map_free(map2);
4560 return result;
4561 error:
4562 isl_map_free(map1);
4563 isl_map_free(map2);
4564 return NULL;
4567 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4568 __isl_take isl_set *set2)
4570 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4573 /* Given a basic map A -> f(A), construct A -> -f(A).
4575 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4577 int i, j;
4578 unsigned off, n;
4580 bmap = isl_basic_map_cow(bmap);
4581 if (!bmap)
4582 return NULL;
4584 n = isl_basic_map_dim(bmap, isl_dim_out);
4585 off = isl_basic_map_offset(bmap, isl_dim_out);
4586 for (i = 0; i < bmap->n_eq; ++i)
4587 for (j = 0; j < n; ++j)
4588 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4589 for (i = 0; i < bmap->n_ineq; ++i)
4590 for (j = 0; j < n; ++j)
4591 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4592 for (i = 0; i < bmap->n_div; ++i)
4593 for (j = 0; j < n; ++j)
4594 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4595 bmap = isl_basic_map_gauss(bmap, NULL);
4596 return isl_basic_map_finalize(bmap);
4599 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4601 return isl_basic_map_neg(bset);
4604 /* Given a map A -> f(A), construct A -> -f(A).
4606 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4608 int i;
4610 map = isl_map_cow(map);
4611 if (!map)
4612 return NULL;
4614 for (i = 0; i < map->n; ++i) {
4615 map->p[i] = isl_basic_map_neg(map->p[i]);
4616 if (!map->p[i])
4617 goto error;
4620 return map;
4621 error:
4622 isl_map_free(map);
4623 return NULL;
4626 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4628 return set_from_map(isl_map_neg(set_to_map(set)));
4631 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4632 * A -> floor(f(A)/d).
4634 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4635 isl_int d)
4637 unsigned n_in, n_out, nparam, total, pos;
4638 struct isl_basic_map *result = NULL;
4639 struct isl_dim_map *dim_map;
4640 int i;
4642 if (!bmap)
4643 return NULL;
4645 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4646 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4647 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4649 total = nparam + n_in + n_out + bmap->n_div + n_out;
4650 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4651 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4652 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4653 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4654 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4656 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4657 bmap->n_div + n_out,
4658 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4659 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4660 result = add_divs(result, n_out);
4661 for (i = 0; i < n_out; ++i) {
4662 int j;
4663 j = isl_basic_map_alloc_inequality(result);
4664 if (j < 0)
4665 goto error;
4666 isl_seq_clr(result->ineq[j], 1+total);
4667 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4668 isl_int_set_si(result->ineq[j][1+pos+i], 1);
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_set(result->ineq[j][1+nparam+n_in+i], d);
4674 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4675 isl_int_sub_ui(result->ineq[j][0], d, 1);
4678 result = isl_basic_map_simplify(result);
4679 return isl_basic_map_finalize(result);
4680 error:
4681 isl_basic_map_free(result);
4682 return NULL;
4685 /* Given a map A -> f(A) and an integer d, construct a map
4686 * A -> floor(f(A)/d).
4688 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4690 int i;
4692 map = isl_map_cow(map);
4693 if (!map)
4694 return NULL;
4696 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4697 for (i = 0; i < map->n; ++i) {
4698 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4699 if (!map->p[i])
4700 goto error;
4702 map = isl_map_unmark_normalized(map);
4704 return map;
4705 error:
4706 isl_map_free(map);
4707 return NULL;
4710 /* Given a map A -> f(A) and an integer d, construct a map
4711 * A -> floor(f(A)/d).
4713 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4714 __isl_take isl_val *d)
4716 if (!map || !d)
4717 goto error;
4718 if (!isl_val_is_int(d))
4719 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4720 "expecting integer denominator", goto error);
4721 map = isl_map_floordiv(map, d->n);
4722 isl_val_free(d);
4723 return map;
4724 error:
4725 isl_map_free(map);
4726 isl_val_free(d);
4727 return NULL;
4730 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4731 unsigned pos)
4733 int i;
4734 unsigned nparam;
4735 unsigned n_in;
4737 i = isl_basic_map_alloc_equality(bmap);
4738 if (i < 0)
4739 goto error;
4740 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4741 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4742 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4743 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4744 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4745 return isl_basic_map_finalize(bmap);
4746 error:
4747 isl_basic_map_free(bmap);
4748 return NULL;
4751 /* Add a constraint to "bmap" expressing i_pos < o_pos
4753 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4754 unsigned pos)
4756 int i;
4757 unsigned nparam;
4758 unsigned n_in;
4760 i = isl_basic_map_alloc_inequality(bmap);
4761 if (i < 0)
4762 goto error;
4763 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4764 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4765 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4766 isl_int_set_si(bmap->ineq[i][0], -1);
4767 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4768 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4769 return isl_basic_map_finalize(bmap);
4770 error:
4771 isl_basic_map_free(bmap);
4772 return NULL;
4775 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4777 static __isl_give isl_basic_map *var_less_or_equal(
4778 __isl_take isl_basic_map *bmap, unsigned pos)
4780 int i;
4781 unsigned nparam;
4782 unsigned n_in;
4784 i = isl_basic_map_alloc_inequality(bmap);
4785 if (i < 0)
4786 goto error;
4787 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4788 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4789 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4790 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4791 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4792 return isl_basic_map_finalize(bmap);
4793 error:
4794 isl_basic_map_free(bmap);
4795 return NULL;
4798 /* Add a constraint to "bmap" expressing i_pos > o_pos
4800 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4801 unsigned pos)
4803 int i;
4804 unsigned nparam;
4805 unsigned n_in;
4807 i = isl_basic_map_alloc_inequality(bmap);
4808 if (i < 0)
4809 goto error;
4810 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4811 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4812 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4813 isl_int_set_si(bmap->ineq[i][0], -1);
4814 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4815 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4816 return isl_basic_map_finalize(bmap);
4817 error:
4818 isl_basic_map_free(bmap);
4819 return NULL;
4822 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4824 static __isl_give isl_basic_map *var_more_or_equal(
4825 __isl_take isl_basic_map *bmap, unsigned pos)
4827 int i;
4828 unsigned nparam;
4829 unsigned n_in;
4831 i = isl_basic_map_alloc_inequality(bmap);
4832 if (i < 0)
4833 goto error;
4834 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4835 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4836 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4837 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4838 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4839 return isl_basic_map_finalize(bmap);
4840 error:
4841 isl_basic_map_free(bmap);
4842 return NULL;
4845 __isl_give isl_basic_map *isl_basic_map_equal(
4846 __isl_take isl_space *space, unsigned n_equal)
4848 int i;
4849 struct isl_basic_map *bmap;
4850 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4851 if (!bmap)
4852 return NULL;
4853 for (i = 0; i < n_equal && bmap; ++i)
4854 bmap = var_equal(bmap, i);
4855 return isl_basic_map_finalize(bmap);
4858 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4860 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4861 unsigned pos)
4863 int i;
4864 struct isl_basic_map *bmap;
4865 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4866 if (!bmap)
4867 return NULL;
4868 for (i = 0; i < pos && bmap; ++i)
4869 bmap = var_equal(bmap, i);
4870 if (bmap)
4871 bmap = var_less(bmap, pos);
4872 return isl_basic_map_finalize(bmap);
4875 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4877 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4878 __isl_take isl_space *space, unsigned pos)
4880 int i;
4881 isl_basic_map *bmap;
4883 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4884 for (i = 0; i < pos; ++i)
4885 bmap = var_equal(bmap, i);
4886 bmap = var_less_or_equal(bmap, pos);
4887 return isl_basic_map_finalize(bmap);
4890 /* Return a relation on "space" expressing i_pos > o_pos
4892 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4893 unsigned pos)
4895 int i;
4896 struct isl_basic_map *bmap;
4897 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4898 if (!bmap)
4899 return NULL;
4900 for (i = 0; i < pos && bmap; ++i)
4901 bmap = var_equal(bmap, i);
4902 if (bmap)
4903 bmap = var_more(bmap, pos);
4904 return isl_basic_map_finalize(bmap);
4907 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4909 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4910 __isl_take isl_space *space, unsigned pos)
4912 int i;
4913 isl_basic_map *bmap;
4915 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4916 for (i = 0; i < pos; ++i)
4917 bmap = var_equal(bmap, i);
4918 bmap = var_more_or_equal(bmap, pos);
4919 return isl_basic_map_finalize(bmap);
4922 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4923 unsigned n, int equal)
4925 struct isl_map *map;
4926 int i;
4928 if (n == 0 && equal)
4929 return isl_map_universe(space);
4931 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4933 for (i = 0; i + 1 < n; ++i)
4934 map = isl_map_add_basic_map(map,
4935 isl_basic_map_less_at(isl_space_copy(space), i));
4936 if (n > 0) {
4937 if (equal)
4938 map = isl_map_add_basic_map(map,
4939 isl_basic_map_less_or_equal_at(space, n - 1));
4940 else
4941 map = isl_map_add_basic_map(map,
4942 isl_basic_map_less_at(space, n - 1));
4943 } else
4944 isl_space_free(space);
4946 return map;
4949 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
4951 if (!space)
4952 return NULL;
4953 return map_lex_lte_first(space, space->n_out, equal);
4956 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4958 return map_lex_lte_first(dim, n, 0);
4961 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4963 return map_lex_lte_first(dim, n, 1);
4966 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4968 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4971 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4973 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4976 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
4977 unsigned n, int equal)
4979 struct isl_map *map;
4980 int i;
4982 if (n == 0 && equal)
4983 return isl_map_universe(space);
4985 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4987 for (i = 0; i + 1 < n; ++i)
4988 map = isl_map_add_basic_map(map,
4989 isl_basic_map_more_at(isl_space_copy(space), i));
4990 if (n > 0) {
4991 if (equal)
4992 map = isl_map_add_basic_map(map,
4993 isl_basic_map_more_or_equal_at(space, n - 1));
4994 else
4995 map = isl_map_add_basic_map(map,
4996 isl_basic_map_more_at(space, n - 1));
4997 } else
4998 isl_space_free(space);
5000 return map;
5003 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5005 if (!space)
5006 return NULL;
5007 return map_lex_gte_first(space, space->n_out, equal);
5010 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5012 return map_lex_gte_first(dim, n, 0);
5015 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5017 return map_lex_gte_first(dim, n, 1);
5020 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5022 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5025 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5027 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5030 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5031 __isl_take isl_set *set2)
5033 isl_map *map;
5034 map = isl_map_lex_le(isl_set_get_space(set1));
5035 map = isl_map_intersect_domain(map, set1);
5036 map = isl_map_intersect_range(map, set2);
5037 return map;
5040 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5041 __isl_take isl_set *set2)
5043 isl_map *map;
5044 map = isl_map_lex_lt(isl_set_get_space(set1));
5045 map = isl_map_intersect_domain(map, set1);
5046 map = isl_map_intersect_range(map, set2);
5047 return map;
5050 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5051 __isl_take isl_set *set2)
5053 isl_map *map;
5054 map = isl_map_lex_ge(isl_set_get_space(set1));
5055 map = isl_map_intersect_domain(map, set1);
5056 map = isl_map_intersect_range(map, set2);
5057 return map;
5060 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5061 __isl_take isl_set *set2)
5063 isl_map *map;
5064 map = isl_map_lex_gt(isl_set_get_space(set1));
5065 map = isl_map_intersect_domain(map, set1);
5066 map = isl_map_intersect_range(map, set2);
5067 return map;
5070 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5071 __isl_take isl_map *map2)
5073 isl_map *map;
5074 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5075 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5076 map = isl_map_apply_range(map, isl_map_reverse(map2));
5077 return map;
5080 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5081 __isl_take isl_map *map2)
5083 isl_map *map;
5084 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5085 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5086 map = isl_map_apply_range(map, isl_map_reverse(map2));
5087 return map;
5090 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5091 __isl_take isl_map *map2)
5093 isl_map *map;
5094 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5095 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5096 map = isl_map_apply_range(map, isl_map_reverse(map2));
5097 return map;
5100 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5101 __isl_take isl_map *map2)
5103 isl_map *map;
5104 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5105 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5106 map = isl_map_apply_range(map, isl_map_reverse(map2));
5107 return map;
5110 /* For the div d = floor(f/m) at position "div", add the constraint
5112 * f - m d >= 0
5114 static __isl_give isl_basic_map *add_upper_div_constraint(
5115 __isl_take isl_basic_map *bmap, unsigned div)
5117 int i;
5118 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5119 unsigned n_div, pos;
5121 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5122 if (v_div < 0)
5123 return isl_basic_map_free(bmap);
5124 pos = v_div + div;
5125 i = isl_basic_map_alloc_inequality(bmap);
5126 if (i < 0)
5127 return isl_basic_map_free(bmap);
5128 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5129 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5131 return bmap;
5134 /* For the div d = floor(f/m) at position "div", add the constraint
5136 * -(f-(m-1)) + m d >= 0
5138 static __isl_give isl_basic_map *add_lower_div_constraint(
5139 __isl_take isl_basic_map *bmap, unsigned div)
5141 int i;
5142 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5143 unsigned n_div, pos;
5145 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5146 if (v_div < 0)
5147 return isl_basic_map_free(bmap);
5148 pos = v_div + div;
5149 i = isl_basic_map_alloc_inequality(bmap);
5150 if (i < 0)
5151 return isl_basic_map_free(bmap);
5152 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5153 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5154 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5155 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5157 return bmap;
5160 /* For the div d = floor(f/m) at position "pos", add the constraints
5162 * f - m d >= 0
5163 * -(f-(m-1)) + m d >= 0
5165 * Note that the second constraint is the negation of
5167 * f - m d >= m
5169 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5170 __isl_take isl_basic_map *bmap, unsigned pos)
5172 bmap = add_upper_div_constraint(bmap, pos);
5173 bmap = add_lower_div_constraint(bmap, pos);
5174 return bmap;
5177 /* For each known div d = floor(f/m), add the constraints
5179 * f - m d >= 0
5180 * -(f-(m-1)) + m d >= 0
5182 * Remove duplicate constraints in case of some these div constraints
5183 * already appear in "bmap".
5185 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5186 __isl_take isl_basic_map *bmap)
5188 unsigned n_div;
5190 if (!bmap)
5191 return NULL;
5192 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5193 if (n_div == 0)
5194 return bmap;
5196 bmap = add_known_div_constraints(bmap);
5197 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5198 bmap = isl_basic_map_finalize(bmap);
5199 return bmap;
5202 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5204 * In particular, if this div is of the form d = floor(f/m),
5205 * then add the constraint
5207 * f - m d >= 0
5209 * if sign < 0 or the constraint
5211 * -(f-(m-1)) + m d >= 0
5213 * if sign > 0.
5215 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5216 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5218 if (sign < 0)
5219 return add_upper_div_constraint(bmap, div);
5220 else
5221 return add_lower_div_constraint(bmap, div);
5224 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5225 __isl_take isl_basic_map *bmap)
5227 if (!bmap)
5228 goto error;
5229 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5230 bmap->n_div == 0 &&
5231 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5232 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5233 return bset_from_bmap(bmap);
5234 bmap = isl_basic_map_cow(bmap);
5235 if (!bmap)
5236 goto error;
5237 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5238 if (!bmap->dim)
5239 goto error;
5240 bmap->extra -= bmap->n_div;
5241 bmap->n_div = 0;
5242 bmap = isl_basic_map_finalize(bmap);
5243 return bset_from_bmap(bmap);
5244 error:
5245 isl_basic_map_free(bmap);
5246 return NULL;
5249 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5250 __isl_take isl_basic_set *bset)
5252 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5255 /* Replace each element in "list" by the result of applying
5256 * isl_basic_map_underlying_set to the element.
5258 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5259 __isl_take isl_basic_map_list *list)
5261 int i, n;
5263 if (!list)
5264 return NULL;
5266 n = isl_basic_map_list_n_basic_map(list);
5267 for (i = 0; i < n; ++i) {
5268 isl_basic_map *bmap;
5269 isl_basic_set *bset;
5271 bmap = isl_basic_map_list_get_basic_map(list, i);
5272 bset = isl_basic_set_underlying_set(bmap);
5273 list = isl_basic_set_list_set_basic_set(list, i, bset);
5276 return list;
5279 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5280 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5282 struct isl_basic_map *bmap;
5283 struct isl_ctx *ctx;
5284 unsigned total;
5285 int i;
5287 if (!bset || !like)
5288 goto error;
5289 ctx = bset->ctx;
5290 if (isl_basic_set_check_no_params(bset) < 0 ||
5291 isl_basic_set_check_no_locals(bset) < 0)
5292 goto error;
5293 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5294 goto error);
5295 if (like->n_div == 0) {
5296 isl_space *space = isl_basic_map_get_space(like);
5297 isl_basic_map_free(like);
5298 return isl_basic_map_reset_space(bset, space);
5300 bset = isl_basic_set_cow(bset);
5301 if (!bset)
5302 goto error;
5303 total = bset->dim->n_out + bset->extra;
5304 bmap = bset_to_bmap(bset);
5305 isl_space_free(bmap->dim);
5306 bmap->dim = isl_space_copy(like->dim);
5307 if (!bmap->dim)
5308 goto error;
5309 bmap->n_div = like->n_div;
5310 bmap->extra += like->n_div;
5311 if (bmap->extra) {
5312 unsigned ltotal;
5313 isl_int **div;
5314 ltotal = total - bmap->extra + like->extra;
5315 if (ltotal > total)
5316 ltotal = total;
5317 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5318 bmap->extra * (1 + 1 + total));
5319 if (isl_blk_is_error(bmap->block2))
5320 goto error;
5321 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5322 if (!div)
5323 goto error;
5324 bmap->div = div;
5325 for (i = 0; i < bmap->extra; ++i)
5326 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5327 for (i = 0; i < like->n_div; ++i) {
5328 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5329 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5331 bmap = isl_basic_map_add_known_div_constraints(bmap);
5333 isl_basic_map_free(like);
5334 bmap = isl_basic_map_simplify(bmap);
5335 bmap = isl_basic_map_finalize(bmap);
5336 return bmap;
5337 error:
5338 isl_basic_map_free(like);
5339 isl_basic_set_free(bset);
5340 return NULL;
5343 struct isl_basic_set *isl_basic_set_from_underlying_set(
5344 struct isl_basic_set *bset, struct isl_basic_set *like)
5346 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5347 bset_to_bmap(like)));
5350 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5352 int i;
5354 map = isl_map_cow(map);
5355 if (!map)
5356 return NULL;
5357 map->dim = isl_space_cow(map->dim);
5358 if (!map->dim)
5359 goto error;
5361 for (i = 1; i < map->n; ++i)
5362 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5363 goto error);
5364 for (i = 0; i < map->n; ++i) {
5365 map->p[i] = bset_to_bmap(
5366 isl_basic_map_underlying_set(map->p[i]));
5367 if (!map->p[i])
5368 goto error;
5370 if (map->n == 0)
5371 map->dim = isl_space_underlying(map->dim, 0);
5372 else {
5373 isl_space_free(map->dim);
5374 map->dim = isl_space_copy(map->p[0]->dim);
5376 if (!map->dim)
5377 goto error;
5378 return set_from_map(map);
5379 error:
5380 isl_map_free(map);
5381 return NULL;
5384 /* Replace the space of "bmap" by "space".
5386 * If the space of "bmap" is identical to "space" (including the identifiers
5387 * of the input and output dimensions), then simply return the original input.
5389 __isl_give isl_basic_map *isl_basic_map_reset_space(
5390 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5392 isl_bool equal;
5393 isl_space *bmap_space;
5395 bmap_space = isl_basic_map_peek_space(bmap);
5396 equal = isl_space_is_equal(bmap_space, space);
5397 if (equal >= 0 && equal)
5398 equal = isl_space_has_equal_ids(bmap_space, space);
5399 if (equal < 0)
5400 goto error;
5401 if (equal) {
5402 isl_space_free(space);
5403 return bmap;
5405 bmap = isl_basic_map_cow(bmap);
5406 if (!bmap || !space)
5407 goto error;
5409 isl_space_free(bmap->dim);
5410 bmap->dim = space;
5412 bmap = isl_basic_map_finalize(bmap);
5414 return bmap;
5415 error:
5416 isl_basic_map_free(bmap);
5417 isl_space_free(space);
5418 return NULL;
5421 __isl_give isl_basic_set *isl_basic_set_reset_space(
5422 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5424 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5425 dim));
5428 /* Check that the total dimensions of "map" and "space" are the same.
5430 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5431 __isl_keep isl_space *space)
5433 unsigned dim1, dim2;
5435 if (!map || !space)
5436 return isl_stat_error;
5437 dim1 = isl_map_dim(map, isl_dim_all);
5438 dim2 = isl_space_dim(space, isl_dim_all);
5439 if (dim1 == dim2)
5440 return isl_stat_ok;
5441 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5442 "total dimensions do not match", return isl_stat_error);
5445 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5446 __isl_take isl_space *space)
5448 int i;
5450 map = isl_map_cow(map);
5451 if (!map || !space)
5452 goto error;
5454 for (i = 0; i < map->n; ++i) {
5455 map->p[i] = isl_basic_map_reset_space(map->p[i],
5456 isl_space_copy(space));
5457 if (!map->p[i])
5458 goto error;
5460 isl_space_free(map->dim);
5461 map->dim = space;
5463 return map;
5464 error:
5465 isl_map_free(map);
5466 isl_space_free(space);
5467 return NULL;
5470 /* Replace the space of "map" by "space", without modifying
5471 * the dimension of "map".
5473 * If the space of "map" is identical to "space" (including the identifiers
5474 * of the input and output dimensions), then simply return the original input.
5476 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5477 __isl_take isl_space *space)
5479 isl_bool equal;
5480 isl_space *map_space;
5482 map_space = isl_map_peek_space(map);
5483 equal = isl_space_is_equal(map_space, space);
5484 if (equal >= 0 && equal)
5485 equal = isl_space_has_equal_ids(map_space, space);
5486 if (equal < 0)
5487 goto error;
5488 if (equal) {
5489 isl_space_free(space);
5490 return map;
5492 if (check_map_space_equal_total_dim(map, space) < 0)
5493 goto error;
5494 return isl_map_reset_space(map, space);
5495 error:
5496 isl_map_free(map);
5497 isl_space_free(space);
5498 return NULL;
5501 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5502 __isl_take isl_space *dim)
5504 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5507 /* Compute the parameter domain of the given basic set.
5509 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5511 isl_bool is_params;
5512 isl_space *space;
5513 unsigned n;
5515 is_params = isl_basic_set_is_params(bset);
5516 if (is_params < 0)
5517 return isl_basic_set_free(bset);
5518 if (is_params)
5519 return bset;
5521 n = isl_basic_set_dim(bset, isl_dim_set);
5522 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5523 space = isl_basic_set_get_space(bset);
5524 space = isl_space_params(space);
5525 bset = isl_basic_set_reset_space(bset, space);
5526 return bset;
5529 /* Construct a zero-dimensional basic set with the given parameter domain.
5531 __isl_give isl_basic_set *isl_basic_set_from_params(
5532 __isl_take isl_basic_set *bset)
5534 isl_space *space;
5535 space = isl_basic_set_get_space(bset);
5536 space = isl_space_set_from_params(space);
5537 bset = isl_basic_set_reset_space(bset, space);
5538 return bset;
5541 /* Compute the parameter domain of the given set.
5543 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5545 isl_space *space;
5546 unsigned n;
5548 if (isl_set_is_params(set))
5549 return set;
5551 n = isl_set_dim(set, isl_dim_set);
5552 set = isl_set_project_out(set, isl_dim_set, 0, n);
5553 space = isl_set_get_space(set);
5554 space = isl_space_params(space);
5555 set = isl_set_reset_space(set, space);
5556 return set;
5559 /* Construct a zero-dimensional set with the given parameter domain.
5561 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5563 isl_space *space;
5564 space = isl_set_get_space(set);
5565 space = isl_space_set_from_params(space);
5566 set = isl_set_reset_space(set, space);
5567 return set;
5570 /* Compute the parameter domain of the given map.
5572 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5574 isl_space *space;
5575 unsigned n;
5577 n = isl_map_dim(map, isl_dim_in);
5578 map = isl_map_project_out(map, isl_dim_in, 0, n);
5579 n = isl_map_dim(map, isl_dim_out);
5580 map = isl_map_project_out(map, isl_dim_out, 0, n);
5581 space = isl_map_get_space(map);
5582 space = isl_space_params(space);
5583 map = isl_map_reset_space(map, space);
5584 return map;
5587 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5589 isl_space *space;
5590 unsigned n_out;
5592 if (!bmap)
5593 return NULL;
5594 space = isl_space_domain(isl_basic_map_get_space(bmap));
5596 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5597 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5599 return isl_basic_map_reset_space(bmap, space);
5602 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5604 if (!bmap)
5605 return isl_bool_error;
5606 return isl_space_may_be_set(bmap->dim);
5609 /* Is this basic map actually a set?
5610 * Users should never call this function. Outside of isl,
5611 * the type should indicate whether something is a set or a map.
5613 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5615 if (!bmap)
5616 return isl_bool_error;
5617 return isl_space_is_set(bmap->dim);
5620 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5622 isl_bool is_set;
5624 is_set = isl_basic_map_is_set(bmap);
5625 if (is_set < 0)
5626 goto error;
5627 if (is_set)
5628 return bmap;
5629 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5630 error:
5631 isl_basic_map_free(bmap);
5632 return NULL;
5635 __isl_give isl_basic_map *isl_basic_map_domain_map(
5636 __isl_take isl_basic_map *bmap)
5638 int i;
5639 isl_space *space;
5640 isl_basic_map *domain;
5641 int nparam, n_in, n_out;
5643 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5644 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5645 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5647 space = isl_basic_map_get_space(bmap);
5648 space = isl_space_from_range(isl_space_domain(space));
5649 domain = isl_basic_map_universe(space);
5651 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5652 bmap = isl_basic_map_apply_range(bmap, domain);
5653 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5655 for (i = 0; i < n_in; ++i)
5656 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5657 isl_dim_out, i);
5659 bmap = isl_basic_map_gauss(bmap, NULL);
5660 return isl_basic_map_finalize(bmap);
5663 __isl_give isl_basic_map *isl_basic_map_range_map(
5664 __isl_take isl_basic_map *bmap)
5666 int i;
5667 isl_space *space;
5668 isl_basic_map *range;
5669 int nparam, n_in, n_out;
5671 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5672 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5673 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5675 space = isl_basic_map_get_space(bmap);
5676 space = isl_space_from_range(isl_space_range(space));
5677 range = isl_basic_map_universe(space);
5679 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5680 bmap = isl_basic_map_apply_range(bmap, range);
5681 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5683 for (i = 0; i < n_out; ++i)
5684 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5685 isl_dim_out, i);
5687 bmap = isl_basic_map_gauss(bmap, NULL);
5688 return isl_basic_map_finalize(bmap);
5691 int isl_map_may_be_set(__isl_keep isl_map *map)
5693 if (!map)
5694 return -1;
5695 return isl_space_may_be_set(map->dim);
5698 /* Is this map actually a set?
5699 * Users should never call this function. Outside of isl,
5700 * the type should indicate whether something is a set or a map.
5702 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5704 if (!map)
5705 return isl_bool_error;
5706 return isl_space_is_set(map->dim);
5709 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5711 int i;
5712 isl_bool is_set;
5713 struct isl_set *set;
5715 is_set = isl_map_is_set(map);
5716 if (is_set < 0)
5717 goto error;
5718 if (is_set)
5719 return set_from_map(map);
5721 map = isl_map_cow(map);
5722 if (!map)
5723 goto error;
5725 set = set_from_map(map);
5726 set->dim = isl_space_range(set->dim);
5727 if (!set->dim)
5728 goto error;
5729 for (i = 0; i < map->n; ++i) {
5730 set->p[i] = isl_basic_map_range(map->p[i]);
5731 if (!set->p[i])
5732 goto error;
5734 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5735 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5736 return set;
5737 error:
5738 isl_map_free(map);
5739 return NULL;
5742 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5744 int i;
5746 map = isl_map_cow(map);
5747 if (!map)
5748 return NULL;
5750 map->dim = isl_space_domain_map(map->dim);
5751 if (!map->dim)
5752 goto error;
5753 for (i = 0; i < map->n; ++i) {
5754 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5755 if (!map->p[i])
5756 goto error;
5758 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5759 map = isl_map_unmark_normalized(map);
5760 return map;
5761 error:
5762 isl_map_free(map);
5763 return NULL;
5766 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5768 int i;
5769 isl_space *range_dim;
5771 map = isl_map_cow(map);
5772 if (!map)
5773 return NULL;
5775 range_dim = isl_space_range(isl_map_get_space(map));
5776 range_dim = isl_space_from_range(range_dim);
5777 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5778 map->dim = isl_space_join(map->dim, range_dim);
5779 if (!map->dim)
5780 goto error;
5781 for (i = 0; i < map->n; ++i) {
5782 map->p[i] = isl_basic_map_range_map(map->p[i]);
5783 if (!map->p[i])
5784 goto error;
5786 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5787 map = isl_map_unmark_normalized(map);
5788 return map;
5789 error:
5790 isl_map_free(map);
5791 return NULL;
5794 /* Given a wrapped map of the form A[B -> C],
5795 * return the map A[B -> C] -> B.
5797 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5799 isl_id *id;
5800 isl_map *map;
5802 if (!set)
5803 return NULL;
5804 if (!isl_set_has_tuple_id(set))
5805 return isl_map_domain_map(isl_set_unwrap(set));
5807 id = isl_set_get_tuple_id(set);
5808 map = isl_map_domain_map(isl_set_unwrap(set));
5809 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5811 return map;
5814 __isl_give isl_basic_map *isl_basic_map_from_domain(
5815 __isl_take isl_basic_set *bset)
5817 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5820 __isl_give isl_basic_map *isl_basic_map_from_range(
5821 __isl_take isl_basic_set *bset)
5823 isl_space *space;
5824 space = isl_basic_set_get_space(bset);
5825 space = isl_space_from_range(space);
5826 bset = isl_basic_set_reset_space(bset, space);
5827 return bset_to_bmap(bset);
5830 /* Create a relation with the given set as range.
5831 * The domain of the created relation is a zero-dimensional
5832 * flat anonymous space.
5834 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5836 isl_space *space;
5837 space = isl_set_get_space(set);
5838 space = isl_space_from_range(space);
5839 set = isl_set_reset_space(set, space);
5840 return set_to_map(set);
5843 /* Create a relation with the given set as domain.
5844 * The range of the created relation is a zero-dimensional
5845 * flat anonymous space.
5847 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5849 return isl_map_reverse(isl_map_from_range(set));
5852 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5853 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5855 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5858 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5859 __isl_take isl_set *range)
5861 return isl_map_apply_range(isl_map_reverse(domain), range);
5864 /* Return a newly allocated isl_map with given space and flags and
5865 * room for "n" basic maps.
5866 * Make sure that all cached information is cleared.
5868 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5869 unsigned flags)
5871 struct isl_map *map;
5873 if (!space)
5874 return NULL;
5875 if (n < 0)
5876 isl_die(space->ctx, isl_error_internal,
5877 "negative number of basic maps", goto error);
5878 map = isl_calloc(space->ctx, struct isl_map,
5879 sizeof(struct isl_map) +
5880 (n - 1) * sizeof(struct isl_basic_map *));
5881 if (!map)
5882 goto error;
5884 map->ctx = space->ctx;
5885 isl_ctx_ref(map->ctx);
5886 map->ref = 1;
5887 map->size = n;
5888 map->n = 0;
5889 map->dim = space;
5890 map->flags = flags;
5891 return map;
5892 error:
5893 isl_space_free(space);
5894 return NULL;
5897 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5899 struct isl_basic_map *bmap;
5900 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5901 bmap = isl_basic_map_set_to_empty(bmap);
5902 return bmap;
5905 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5907 struct isl_basic_set *bset;
5908 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5909 bset = isl_basic_set_set_to_empty(bset);
5910 return bset;
5913 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5915 struct isl_basic_map *bmap;
5916 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5917 bmap = isl_basic_map_finalize(bmap);
5918 return bmap;
5921 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5923 struct isl_basic_set *bset;
5924 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5925 bset = isl_basic_set_finalize(bset);
5926 return bset;
5929 __isl_give isl_basic_map *isl_basic_map_nat_universe(
5930 __isl_take isl_space *space)
5932 int i;
5933 unsigned total = isl_space_dim(space, isl_dim_all);
5934 isl_basic_map *bmap;
5936 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
5937 for (i = 0; i < total; ++i) {
5938 int k = isl_basic_map_alloc_inequality(bmap);
5939 if (k < 0)
5940 goto error;
5941 isl_seq_clr(bmap->ineq[k], 1 + total);
5942 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5944 return bmap;
5945 error:
5946 isl_basic_map_free(bmap);
5947 return NULL;
5950 __isl_give isl_basic_set *isl_basic_set_nat_universe(
5951 __isl_take isl_space *space)
5953 return isl_basic_map_nat_universe(space);
5956 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5958 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5961 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5963 return isl_map_nat_universe(dim);
5966 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
5968 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
5971 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
5973 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
5976 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
5978 struct isl_map *map;
5979 if (!space)
5980 return NULL;
5981 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5982 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
5983 return map;
5986 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
5988 struct isl_set *set;
5989 if (!space)
5990 return NULL;
5991 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5992 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
5993 return set;
5996 struct isl_map *isl_map_dup(struct isl_map *map)
5998 int i;
5999 struct isl_map *dup;
6001 if (!map)
6002 return NULL;
6003 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6004 for (i = 0; i < map->n; ++i)
6005 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6006 return dup;
6009 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6010 __isl_take isl_basic_map *bmap)
6012 if (!bmap || !map)
6013 goto error;
6014 if (isl_basic_map_plain_is_empty(bmap)) {
6015 isl_basic_map_free(bmap);
6016 return map;
6018 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6019 isl_assert(map->ctx, map->n < map->size, goto error);
6020 map->p[map->n] = bmap;
6021 map->n++;
6022 map = isl_map_unmark_normalized(map);
6023 return map;
6024 error:
6025 if (map)
6026 isl_map_free(map);
6027 if (bmap)
6028 isl_basic_map_free(bmap);
6029 return NULL;
6032 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6034 int i;
6036 if (!map)
6037 return NULL;
6039 if (--map->ref > 0)
6040 return NULL;
6042 clear_caches(map);
6043 isl_ctx_deref(map->ctx);
6044 for (i = 0; i < map->n; ++i)
6045 isl_basic_map_free(map->p[i]);
6046 isl_space_free(map->dim);
6047 free(map);
6049 return NULL;
6052 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6053 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6055 int j;
6057 bmap = isl_basic_map_cow(bmap);
6058 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6059 j = isl_basic_map_alloc_equality(bmap);
6060 if (j < 0)
6061 goto error;
6062 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6063 isl_int_set_si(bmap->eq[j][pos], -1);
6064 isl_int_set_si(bmap->eq[j][0], value);
6065 bmap = isl_basic_map_simplify(bmap);
6066 return isl_basic_map_finalize(bmap);
6067 error:
6068 isl_basic_map_free(bmap);
6069 return NULL;
6072 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6073 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6075 int j;
6077 bmap = isl_basic_map_cow(bmap);
6078 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6079 j = isl_basic_map_alloc_equality(bmap);
6080 if (j < 0)
6081 goto error;
6082 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6083 isl_int_set_si(bmap->eq[j][pos], -1);
6084 isl_int_set(bmap->eq[j][0], value);
6085 bmap = isl_basic_map_simplify(bmap);
6086 return isl_basic_map_finalize(bmap);
6087 error:
6088 isl_basic_map_free(bmap);
6089 return NULL;
6092 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6093 enum isl_dim_type type, unsigned pos, int value)
6095 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6096 return isl_basic_map_free(bmap);
6097 return isl_basic_map_fix_pos_si(bmap,
6098 isl_basic_map_offset(bmap, type) + pos, value);
6101 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6102 enum isl_dim_type type, unsigned pos, isl_int value)
6104 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6105 return isl_basic_map_free(bmap);
6106 return isl_basic_map_fix_pos(bmap,
6107 isl_basic_map_offset(bmap, type) + pos, value);
6110 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6111 * to be equal to "v".
6113 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6114 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6116 if (!bmap || !v)
6117 goto error;
6118 if (!isl_val_is_int(v))
6119 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6120 "expecting integer value", goto error);
6121 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6122 goto error;
6123 pos += isl_basic_map_offset(bmap, type);
6124 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6125 isl_val_free(v);
6126 return bmap;
6127 error:
6128 isl_basic_map_free(bmap);
6129 isl_val_free(v);
6130 return NULL;
6133 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6134 * to be equal to "v".
6136 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6137 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6139 return isl_basic_map_fix_val(bset, type, pos, v);
6142 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6143 enum isl_dim_type type, unsigned pos, int value)
6145 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6146 type, pos, value));
6149 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6150 enum isl_dim_type type, unsigned pos, isl_int value)
6152 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6153 type, pos, value));
6156 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6157 unsigned input, int value)
6159 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6162 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6163 unsigned dim, int value)
6165 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6166 isl_dim_set, dim, value));
6169 /* Remove the basic map at position "i" from "map" if this basic map
6170 * is (obviously) empty.
6172 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6174 isl_bool empty;
6176 if (!map)
6177 return NULL;
6179 empty = isl_basic_map_plain_is_empty(map->p[i]);
6180 if (empty < 0)
6181 return isl_map_free(map);
6182 if (!empty)
6183 return map;
6185 isl_basic_map_free(map->p[i]);
6186 map->n--;
6187 if (i != map->n) {
6188 map->p[i] = map->p[map->n];
6189 map = isl_map_unmark_normalized(map);
6193 return map;
6196 /* Perform "fn" on each basic map of "map", where we may not be holding
6197 * the only reference to "map".
6198 * In particular, "fn" should be a semantics preserving operation
6199 * that we want to apply to all copies of "map". We therefore need
6200 * to be careful not to modify "map" in a way that breaks "map"
6201 * in case anything goes wrong.
6203 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6204 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6206 struct isl_basic_map *bmap;
6207 int i;
6209 if (!map)
6210 return NULL;
6212 for (i = map->n - 1; i >= 0; --i) {
6213 bmap = isl_basic_map_copy(map->p[i]);
6214 bmap = fn(bmap);
6215 if (!bmap)
6216 goto error;
6217 isl_basic_map_free(map->p[i]);
6218 map->p[i] = bmap;
6219 map = remove_if_empty(map, i);
6220 if (!map)
6221 return NULL;
6224 return map;
6225 error:
6226 isl_map_free(map);
6227 return NULL;
6230 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6231 enum isl_dim_type type, unsigned pos, int value)
6233 int i;
6235 map = isl_map_cow(map);
6236 if (isl_map_check_range(map, type, pos, 1) < 0)
6237 return isl_map_free(map);
6238 for (i = map->n - 1; i >= 0; --i) {
6239 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6240 map = remove_if_empty(map, i);
6241 if (!map)
6242 return NULL;
6244 map = isl_map_unmark_normalized(map);
6245 return map;
6248 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6249 enum isl_dim_type type, unsigned pos, int value)
6251 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6254 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6255 enum isl_dim_type type, unsigned pos, isl_int value)
6257 int i;
6259 map = isl_map_cow(map);
6260 if (isl_map_check_range(map, type, pos, 1) < 0)
6261 return isl_map_free(map);
6262 for (i = 0; i < map->n; ++i) {
6263 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6264 if (!map->p[i])
6265 goto error;
6267 map = isl_map_unmark_normalized(map);
6268 return map;
6269 error:
6270 isl_map_free(map);
6271 return NULL;
6274 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6275 enum isl_dim_type type, unsigned pos, isl_int value)
6277 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6280 /* Fix the value of the variable at position "pos" of type "type" of "map"
6281 * to be equal to "v".
6283 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6284 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6286 int i;
6288 map = isl_map_cow(map);
6289 if (!map || !v)
6290 goto error;
6292 if (!isl_val_is_int(v))
6293 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6294 "expecting integer value", goto error);
6295 if (isl_map_check_range(map, type, pos, 1) < 0)
6296 goto error;
6297 for (i = map->n - 1; i >= 0; --i) {
6298 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6299 isl_val_copy(v));
6300 map = remove_if_empty(map, i);
6301 if (!map)
6302 goto error;
6304 map = isl_map_unmark_normalized(map);
6305 isl_val_free(v);
6306 return map;
6307 error:
6308 isl_map_free(map);
6309 isl_val_free(v);
6310 return NULL;
6313 /* Fix the value of the variable at position "pos" of type "type" of "set"
6314 * to be equal to "v".
6316 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6317 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6319 return isl_map_fix_val(set, type, pos, v);
6322 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6323 unsigned input, int value)
6325 return isl_map_fix_si(map, isl_dim_in, input, value);
6328 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6330 return set_from_map(isl_map_fix_si(set_to_map(set),
6331 isl_dim_set, dim, value));
6334 static __isl_give isl_basic_map *basic_map_bound_si(
6335 __isl_take isl_basic_map *bmap,
6336 enum isl_dim_type type, unsigned pos, int value, int upper)
6338 int j;
6340 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6341 return isl_basic_map_free(bmap);
6342 pos += isl_basic_map_offset(bmap, type);
6343 bmap = isl_basic_map_cow(bmap);
6344 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6345 j = isl_basic_map_alloc_inequality(bmap);
6346 if (j < 0)
6347 goto error;
6348 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6349 if (upper) {
6350 isl_int_set_si(bmap->ineq[j][pos], -1);
6351 isl_int_set_si(bmap->ineq[j][0], value);
6352 } else {
6353 isl_int_set_si(bmap->ineq[j][pos], 1);
6354 isl_int_set_si(bmap->ineq[j][0], -value);
6356 bmap = isl_basic_map_simplify(bmap);
6357 return isl_basic_map_finalize(bmap);
6358 error:
6359 isl_basic_map_free(bmap);
6360 return NULL;
6363 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6364 __isl_take isl_basic_map *bmap,
6365 enum isl_dim_type type, unsigned pos, int value)
6367 return basic_map_bound_si(bmap, type, pos, value, 0);
6370 /* Constrain the values of the given dimension to be no greater than "value".
6372 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6373 __isl_take isl_basic_map *bmap,
6374 enum isl_dim_type type, unsigned pos, int value)
6376 return basic_map_bound_si(bmap, type, pos, value, 1);
6379 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6380 enum isl_dim_type type, unsigned pos, int value, int upper)
6382 int i;
6384 map = isl_map_cow(map);
6385 if (isl_map_check_range(map, type, pos, 1) < 0)
6386 return isl_map_free(map);
6387 for (i = 0; i < map->n; ++i) {
6388 map->p[i] = basic_map_bound_si(map->p[i],
6389 type, pos, value, upper);
6390 if (!map->p[i])
6391 goto error;
6393 map = isl_map_unmark_normalized(map);
6394 return map;
6395 error:
6396 isl_map_free(map);
6397 return NULL;
6400 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6401 enum isl_dim_type type, unsigned pos, int value)
6403 return map_bound_si(map, type, pos, value, 0);
6406 __isl_give isl_map *isl_map_upper_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, 1);
6412 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6413 enum isl_dim_type type, unsigned pos, int value)
6415 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6416 type, pos, value));
6419 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6420 enum isl_dim_type type, unsigned pos, int value)
6422 return isl_map_upper_bound_si(set, type, pos, value);
6425 /* Bound the given variable of "bmap" from below (or above is "upper"
6426 * is set) to "value".
6428 static __isl_give isl_basic_map *basic_map_bound(
6429 __isl_take isl_basic_map *bmap,
6430 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6432 int j;
6434 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6435 return isl_basic_map_free(bmap);
6436 pos += isl_basic_map_offset(bmap, type);
6437 bmap = isl_basic_map_cow(bmap);
6438 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6439 j = isl_basic_map_alloc_inequality(bmap);
6440 if (j < 0)
6441 goto error;
6442 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6443 if (upper) {
6444 isl_int_set_si(bmap->ineq[j][pos], -1);
6445 isl_int_set(bmap->ineq[j][0], value);
6446 } else {
6447 isl_int_set_si(bmap->ineq[j][pos], 1);
6448 isl_int_neg(bmap->ineq[j][0], value);
6450 bmap = isl_basic_map_simplify(bmap);
6451 return isl_basic_map_finalize(bmap);
6452 error:
6453 isl_basic_map_free(bmap);
6454 return NULL;
6457 /* Bound the given variable of "map" from below (or above is "upper"
6458 * is set) to "value".
6460 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6461 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6463 int i;
6465 map = isl_map_cow(map);
6466 if (isl_map_check_range(map, type, pos, 1) < 0)
6467 return isl_map_free(map);
6468 for (i = map->n - 1; i >= 0; --i) {
6469 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6470 map = remove_if_empty(map, i);
6471 if (!map)
6472 return NULL;
6474 map = isl_map_unmark_normalized(map);
6475 return map;
6478 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6479 enum isl_dim_type type, unsigned pos, isl_int value)
6481 return map_bound(map, type, pos, value, 0);
6484 __isl_give isl_map *isl_map_upper_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, 1);
6490 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6491 enum isl_dim_type type, unsigned pos, isl_int value)
6493 return isl_map_lower_bound(set, type, pos, value);
6496 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6497 enum isl_dim_type type, unsigned pos, isl_int value)
6499 return isl_map_upper_bound(set, type, pos, value);
6502 /* Force the values of the variable at position "pos" of type "type" of "set"
6503 * to be no smaller than "value".
6505 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6506 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6508 if (!value)
6509 goto error;
6510 if (!isl_val_is_int(value))
6511 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6512 "expecting integer value", goto error);
6513 set = isl_set_lower_bound(set, type, pos, value->n);
6514 isl_val_free(value);
6515 return set;
6516 error:
6517 isl_val_free(value);
6518 isl_set_free(set);
6519 return NULL;
6522 /* Force the values of the variable at position "pos" of type "type" of "set"
6523 * to be no greater than "value".
6525 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6526 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6528 if (!value)
6529 goto error;
6530 if (!isl_val_is_int(value))
6531 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6532 "expecting integer value", goto error);
6533 set = isl_set_upper_bound(set, type, pos, value->n);
6534 isl_val_free(value);
6535 return set;
6536 error:
6537 isl_val_free(value);
6538 isl_set_free(set);
6539 return NULL;
6542 /* Bound the given variable of "bset" from below (or above is "upper"
6543 * is set) to "value".
6545 static __isl_give isl_basic_set *isl_basic_set_bound(
6546 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6547 isl_int value, int upper)
6549 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6550 type, pos, value, upper));
6553 /* Bound the given variable of "bset" from below (or above is "upper"
6554 * is set) to "value".
6556 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6557 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6558 __isl_take isl_val *value, int upper)
6560 if (!value)
6561 goto error;
6562 if (!isl_val_is_int(value))
6563 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6564 "expecting integer value", goto error);
6565 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6566 isl_val_free(value);
6567 return bset;
6568 error:
6569 isl_val_free(value);
6570 isl_basic_set_free(bset);
6571 return NULL;
6574 /* Bound the given variable of "bset" from below to "value".
6576 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6577 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6578 __isl_take isl_val *value)
6580 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6583 /* Bound the given variable of "bset" from above to "value".
6585 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6586 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6587 __isl_take isl_val *value)
6589 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6592 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6594 int i;
6596 map = isl_map_cow(map);
6597 if (!map)
6598 return NULL;
6600 map->dim = isl_space_reverse(map->dim);
6601 if (!map->dim)
6602 goto error;
6603 for (i = 0; i < map->n; ++i) {
6604 map->p[i] = isl_basic_map_reverse(map->p[i]);
6605 if (!map->p[i])
6606 goto error;
6608 map = isl_map_unmark_normalized(map);
6609 return map;
6610 error:
6611 isl_map_free(map);
6612 return NULL;
6615 #undef TYPE
6616 #define TYPE isl_pw_multi_aff
6617 #undef SUFFIX
6618 #define SUFFIX _pw_multi_aff
6619 #undef EMPTY
6620 #define EMPTY isl_pw_multi_aff_empty
6621 #undef ADD
6622 #define ADD isl_pw_multi_aff_union_add
6623 #include "isl_map_lexopt_templ.c"
6625 /* Given a map "map", compute the lexicographically minimal
6626 * (or maximal) image element for each domain element in dom,
6627 * in the form of an isl_pw_multi_aff.
6628 * If "empty" is not NULL, then set *empty to those elements in dom that
6629 * do not have an image element.
6630 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6631 * should be computed over the domain of "map". "empty" is also NULL
6632 * in this case.
6634 * We first compute the lexicographically minimal or maximal element
6635 * in the first basic map. This results in a partial solution "res"
6636 * and a subset "todo" of dom that still need to be handled.
6637 * We then consider each of the remaining maps in "map" and successively
6638 * update both "res" and "todo".
6639 * If "empty" is NULL, then the todo sets are not needed and therefore
6640 * also not computed.
6642 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6643 __isl_take isl_map *map, __isl_take isl_set *dom,
6644 __isl_give isl_set **empty, unsigned flags)
6646 int i;
6647 int full;
6648 isl_pw_multi_aff *res;
6649 isl_set *todo;
6651 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6652 if (!map || (!full && !dom))
6653 goto error;
6655 if (isl_map_plain_is_empty(map)) {
6656 if (empty)
6657 *empty = dom;
6658 else
6659 isl_set_free(dom);
6660 return isl_pw_multi_aff_from_map(map);
6663 res = basic_map_partial_lexopt_pw_multi_aff(
6664 isl_basic_map_copy(map->p[0]),
6665 isl_set_copy(dom), empty, flags);
6667 if (empty)
6668 todo = *empty;
6669 for (i = 1; i < map->n; ++i) {
6670 isl_pw_multi_aff *res_i;
6672 res_i = basic_map_partial_lexopt_pw_multi_aff(
6673 isl_basic_map_copy(map->p[i]),
6674 isl_set_copy(dom), empty, flags);
6676 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6677 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6678 else
6679 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6681 if (empty)
6682 todo = isl_set_intersect(todo, *empty);
6685 isl_set_free(dom);
6686 isl_map_free(map);
6688 if (empty)
6689 *empty = todo;
6691 return res;
6692 error:
6693 if (empty)
6694 *empty = NULL;
6695 isl_set_free(dom);
6696 isl_map_free(map);
6697 return NULL;
6700 #undef TYPE
6701 #define TYPE isl_map
6702 #undef SUFFIX
6703 #define SUFFIX
6704 #undef EMPTY
6705 #define EMPTY isl_map_empty
6706 #undef ADD
6707 #define ADD isl_map_union_disjoint
6708 #include "isl_map_lexopt_templ.c"
6710 /* Given a map "map", compute the lexicographically minimal
6711 * (or maximal) image element for each domain element in "dom",
6712 * in the form of an isl_map.
6713 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6714 * do not have an image element.
6715 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6716 * should be computed over the domain of "map". "empty" is also NULL
6717 * in this case.
6719 * If the input consists of more than one disjunct, then first
6720 * compute the desired result in the form of an isl_pw_multi_aff and
6721 * then convert that into an isl_map.
6723 * This function used to have an explicit implementation in terms
6724 * of isl_maps, but it would continually intersect the domains of
6725 * partial results with the complement of the domain of the next
6726 * partial solution, potentially leading to an explosion in the number
6727 * of disjuncts if there are several disjuncts in the input.
6728 * An even earlier implementation of this function would look for
6729 * better results in the domain of the partial result and for extra
6730 * results in the complement of this domain, which would lead to
6731 * even more splintering.
6733 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6734 __isl_take isl_map *map, __isl_take isl_set *dom,
6735 __isl_give isl_set **empty, unsigned flags)
6737 int full;
6738 struct isl_map *res;
6739 isl_pw_multi_aff *pma;
6741 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6742 if (!map || (!full && !dom))
6743 goto error;
6745 if (isl_map_plain_is_empty(map)) {
6746 if (empty)
6747 *empty = dom;
6748 else
6749 isl_set_free(dom);
6750 return map;
6753 if (map->n == 1) {
6754 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6755 dom, empty, flags);
6756 isl_map_free(map);
6757 return res;
6760 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6761 flags);
6762 return isl_map_from_pw_multi_aff(pma);
6763 error:
6764 if (empty)
6765 *empty = NULL;
6766 isl_set_free(dom);
6767 isl_map_free(map);
6768 return NULL;
6771 __isl_give isl_map *isl_map_partial_lexmax(
6772 __isl_take isl_map *map, __isl_take isl_set *dom,
6773 __isl_give isl_set **empty)
6775 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6778 __isl_give isl_map *isl_map_partial_lexmin(
6779 __isl_take isl_map *map, __isl_take isl_set *dom,
6780 __isl_give isl_set **empty)
6782 return isl_map_partial_lexopt(map, dom, empty, 0);
6785 __isl_give isl_set *isl_set_partial_lexmin(
6786 __isl_take isl_set *set, __isl_take isl_set *dom,
6787 __isl_give isl_set **empty)
6789 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6790 dom, empty));
6793 __isl_give isl_set *isl_set_partial_lexmax(
6794 __isl_take isl_set *set, __isl_take isl_set *dom,
6795 __isl_give isl_set **empty)
6797 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6798 dom, empty));
6801 /* Compute the lexicographic minimum (or maximum if "flags" includes
6802 * ISL_OPT_MAX) of "bset" over its parametric domain.
6804 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6805 unsigned flags)
6807 return isl_basic_map_lexopt(bset, flags);
6810 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6812 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6815 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6817 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6820 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6822 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6825 /* Compute the lexicographic minimum of "bset" over its parametric domain
6826 * for the purpose of quantifier elimination.
6827 * That is, find an explicit representation for all the existentially
6828 * quantified variables in "bset" by computing their lexicographic
6829 * minimum.
6831 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6832 __isl_take isl_basic_set *bset)
6834 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6837 /* Given a basic map with one output dimension, compute the minimum or
6838 * maximum of that dimension as an isl_pw_aff.
6840 * Compute the optimum as a lexicographic optimum over the single
6841 * output dimension and extract the single isl_pw_aff from the result.
6843 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6844 int max)
6846 isl_pw_multi_aff *pma;
6847 isl_pw_aff *pwaff;
6849 bmap = isl_basic_map_copy(bmap);
6850 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6851 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6852 isl_pw_multi_aff_free(pma);
6854 return pwaff;
6857 /* Compute the minimum or maximum of the given output dimension
6858 * as a function of the parameters and the input dimensions,
6859 * but independently of the other output dimensions.
6861 * We first project out the other output dimension and then compute
6862 * the "lexicographic" maximum in each basic map, combining the results
6863 * using isl_pw_aff_union_max.
6865 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6866 int max)
6868 int i;
6869 isl_pw_aff *pwaff;
6870 unsigned n_out;
6872 n_out = isl_map_dim(map, isl_dim_out);
6873 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6874 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6875 if (!map)
6876 return NULL;
6878 if (map->n == 0) {
6879 isl_space *space = isl_map_get_space(map);
6880 isl_map_free(map);
6881 return isl_pw_aff_empty(space);
6884 pwaff = basic_map_dim_opt(map->p[0], max);
6885 for (i = 1; i < map->n; ++i) {
6886 isl_pw_aff *pwaff_i;
6888 pwaff_i = basic_map_dim_opt(map->p[i], max);
6889 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6892 isl_map_free(map);
6894 return pwaff;
6897 /* Compute the minimum of the given output dimension as a function of the
6898 * parameters and input dimensions, but independently of
6899 * the other output dimensions.
6901 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6903 return map_dim_opt(map, pos, 0);
6906 /* Compute the maximum of the given output dimension as a function of the
6907 * parameters and input dimensions, but independently of
6908 * the other output dimensions.
6910 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6912 return map_dim_opt(map, pos, 1);
6915 /* Compute the minimum or maximum of the given set dimension
6916 * as a function of the parameters,
6917 * but independently of the other set dimensions.
6919 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6920 int max)
6922 return map_dim_opt(set, pos, max);
6925 /* Compute the maximum of the given set dimension as a function of the
6926 * parameters, but independently of the other set dimensions.
6928 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6930 return set_dim_opt(set, pos, 1);
6933 /* Compute the minimum of the given set dimension as a function of the
6934 * parameters, but independently of the other set dimensions.
6936 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6938 return set_dim_opt(set, pos, 0);
6941 /* Apply a preimage specified by "mat" on the parameters of "bset".
6942 * bset is assumed to have only parameters and divs.
6944 static __isl_give isl_basic_set *basic_set_parameter_preimage(
6945 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
6947 unsigned nparam;
6949 if (!bset || !mat)
6950 goto error;
6952 bset->dim = isl_space_cow(bset->dim);
6953 if (!bset->dim)
6954 goto error;
6956 nparam = isl_basic_set_dim(bset, isl_dim_param);
6958 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6960 bset->dim->nparam = 0;
6961 bset->dim->n_out = nparam;
6962 bset = isl_basic_set_preimage(bset, mat);
6963 if (bset) {
6964 bset->dim->nparam = bset->dim->n_out;
6965 bset->dim->n_out = 0;
6967 return bset;
6968 error:
6969 isl_mat_free(mat);
6970 isl_basic_set_free(bset);
6971 return NULL;
6974 /* Apply a preimage specified by "mat" on the parameters of "set".
6975 * set is assumed to have only parameters and divs.
6977 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6978 __isl_take isl_mat *mat)
6980 isl_space *space;
6981 unsigned nparam;
6983 if (!set || !mat)
6984 goto error;
6986 nparam = isl_set_dim(set, isl_dim_param);
6988 if (mat->n_row != 1 + nparam)
6989 isl_die(isl_set_get_ctx(set), isl_error_internal,
6990 "unexpected number of rows", goto error);
6992 space = isl_set_get_space(set);
6993 space = isl_space_move_dims(space, isl_dim_set, 0,
6994 isl_dim_param, 0, nparam);
6995 set = isl_set_reset_space(set, space);
6996 set = isl_set_preimage(set, mat);
6997 nparam = isl_set_dim(set, isl_dim_out);
6998 space = isl_set_get_space(set);
6999 space = isl_space_move_dims(space, isl_dim_param, 0,
7000 isl_dim_out, 0, nparam);
7001 set = isl_set_reset_space(set, space);
7002 return set;
7003 error:
7004 isl_mat_free(mat);
7005 isl_set_free(set);
7006 return NULL;
7009 /* Intersect the basic set "bset" with the affine space specified by the
7010 * equalities in "eq".
7012 static __isl_give isl_basic_set *basic_set_append_equalities(
7013 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7015 int i, k;
7016 unsigned len;
7018 if (!bset || !eq)
7019 goto error;
7021 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7022 eq->n_row, 0);
7023 if (!bset)
7024 goto error;
7026 len = isl_basic_set_offset(bset, isl_dim_div) + bset->extra;
7027 for (i = 0; i < eq->n_row; ++i) {
7028 k = isl_basic_set_alloc_equality(bset);
7029 if (k < 0)
7030 goto error;
7031 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7032 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7034 isl_mat_free(eq);
7036 bset = isl_basic_set_gauss(bset, NULL);
7037 bset = isl_basic_set_finalize(bset);
7039 return bset;
7040 error:
7041 isl_mat_free(eq);
7042 isl_basic_set_free(bset);
7043 return NULL;
7046 /* Intersect the set "set" with the affine space specified by the
7047 * equalities in "eq".
7049 static struct isl_set *set_append_equalities(struct isl_set *set,
7050 struct isl_mat *eq)
7052 int i;
7054 if (!set || !eq)
7055 goto error;
7057 for (i = 0; i < set->n; ++i) {
7058 set->p[i] = basic_set_append_equalities(set->p[i],
7059 isl_mat_copy(eq));
7060 if (!set->p[i])
7061 goto error;
7063 isl_mat_free(eq);
7064 return set;
7065 error:
7066 isl_mat_free(eq);
7067 isl_set_free(set);
7068 return NULL;
7071 /* Given a basic set "bset" that only involves parameters and existentially
7072 * quantified variables, return the index of the first equality
7073 * that only involves parameters. If there is no such equality then
7074 * return bset->n_eq.
7076 * This function assumes that isl_basic_set_gauss has been called on "bset".
7078 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7080 int i, j;
7081 unsigned nparam, n_div;
7083 if (!bset)
7084 return -1;
7086 nparam = isl_basic_set_dim(bset, isl_dim_param);
7087 n_div = isl_basic_set_dim(bset, isl_dim_div);
7089 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7090 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7091 ++i;
7094 return i;
7097 /* Compute an explicit representation for the existentially quantified
7098 * variables in "bset" by computing the "minimal value" of the set
7099 * variables. Since there are no set variables, the computation of
7100 * the minimal value essentially computes an explicit representation
7101 * of the non-empty part(s) of "bset".
7103 * The input only involves parameters and existentially quantified variables.
7104 * All equalities among parameters have been removed.
7106 * Since the existentially quantified variables in the result are in general
7107 * going to be different from those in the input, we first replace
7108 * them by the minimal number of variables based on their equalities.
7109 * This should simplify the parametric integer programming.
7111 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7113 isl_morph *morph1, *morph2;
7114 isl_set *set;
7115 unsigned n;
7117 if (!bset)
7118 return NULL;
7119 if (bset->n_eq == 0)
7120 return isl_basic_set_lexmin_compute_divs(bset);
7122 morph1 = isl_basic_set_parameter_compression(bset);
7123 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7124 bset = isl_basic_set_lift(bset);
7125 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7126 bset = isl_morph_basic_set(morph2, bset);
7127 n = isl_basic_set_dim(bset, isl_dim_set);
7128 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7130 set = isl_basic_set_lexmin_compute_divs(bset);
7132 set = isl_morph_set(isl_morph_inverse(morph1), set);
7134 return set;
7137 /* Project the given basic set onto its parameter domain, possibly introducing
7138 * new, explicit, existential variables in the constraints.
7139 * The input has parameters and (possibly implicit) existential variables.
7140 * The output has the same parameters, but only
7141 * explicit existentially quantified variables.
7143 * The actual projection is performed by pip, but pip doesn't seem
7144 * to like equalities very much, so we first remove the equalities
7145 * among the parameters by performing a variable compression on
7146 * the parameters. Afterward, an inverse transformation is performed
7147 * and the equalities among the parameters are inserted back in.
7149 * The variable compression on the parameters may uncover additional
7150 * equalities that were only implicit before. We therefore check
7151 * if there are any new parameter equalities in the result and
7152 * if so recurse. The removal of parameter equalities is required
7153 * for the parameter compression performed by base_compute_divs.
7155 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7157 int i;
7158 struct isl_mat *eq;
7159 struct isl_mat *T, *T2;
7160 struct isl_set *set;
7161 unsigned nparam;
7163 bset = isl_basic_set_cow(bset);
7164 if (!bset)
7165 return NULL;
7167 if (bset->n_eq == 0)
7168 return base_compute_divs(bset);
7170 bset = isl_basic_set_gauss(bset, NULL);
7171 if (!bset)
7172 return NULL;
7173 if (isl_basic_set_plain_is_empty(bset))
7174 return isl_set_from_basic_set(bset);
7176 i = first_parameter_equality(bset);
7177 if (i == bset->n_eq)
7178 return base_compute_divs(bset);
7180 nparam = isl_basic_set_dim(bset, isl_dim_param);
7181 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7182 0, 1 + nparam);
7183 eq = isl_mat_cow(eq);
7184 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7185 if (T && T->n_col == 0) {
7186 isl_mat_free(T);
7187 isl_mat_free(T2);
7188 isl_mat_free(eq);
7189 bset = isl_basic_set_set_to_empty(bset);
7190 return isl_set_from_basic_set(bset);
7192 bset = basic_set_parameter_preimage(bset, T);
7194 i = first_parameter_equality(bset);
7195 if (!bset)
7196 set = NULL;
7197 else if (i == bset->n_eq)
7198 set = base_compute_divs(bset);
7199 else
7200 set = parameter_compute_divs(bset);
7201 set = set_parameter_preimage(set, T2);
7202 set = set_append_equalities(set, eq);
7203 return set;
7206 /* Insert the divs from "ls" before those of "bmap".
7208 * The number of columns is not changed, which means that the last
7209 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7210 * The caller is responsible for removing the same number of dimensions
7211 * from the space of "bmap".
7213 static __isl_give isl_basic_map *insert_divs_from_local_space(
7214 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7216 int i;
7217 int n_div;
7218 int old_n_div;
7220 n_div = isl_local_space_dim(ls, isl_dim_div);
7221 if (n_div == 0)
7222 return bmap;
7224 old_n_div = bmap->n_div;
7225 bmap = insert_div_rows(bmap, n_div);
7226 if (!bmap)
7227 return NULL;
7229 for (i = 0; i < n_div; ++i) {
7230 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7231 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7234 return bmap;
7237 /* Replace the space of "bmap" by the space and divs of "ls".
7239 * If "ls" has any divs, then we simplify the result since we may
7240 * have discovered some additional equalities that could simplify
7241 * the div expressions.
7243 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7244 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7246 int n_div;
7248 bmap = isl_basic_map_cow(bmap);
7249 if (!bmap || !ls)
7250 goto error;
7252 n_div = isl_local_space_dim(ls, isl_dim_div);
7253 bmap = insert_divs_from_local_space(bmap, ls);
7254 if (!bmap)
7255 goto error;
7257 isl_space_free(bmap->dim);
7258 bmap->dim = isl_local_space_get_space(ls);
7259 if (!bmap->dim)
7260 goto error;
7262 isl_local_space_free(ls);
7263 if (n_div > 0)
7264 bmap = isl_basic_map_simplify(bmap);
7265 bmap = isl_basic_map_finalize(bmap);
7266 return bmap;
7267 error:
7268 isl_basic_map_free(bmap);
7269 isl_local_space_free(ls);
7270 return NULL;
7273 /* Replace the space of "map" by the space and divs of "ls".
7275 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7276 __isl_take isl_local_space *ls)
7278 int i;
7280 map = isl_map_cow(map);
7281 if (!map || !ls)
7282 goto error;
7284 for (i = 0; i < map->n; ++i) {
7285 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7286 isl_local_space_copy(ls));
7287 if (!map->p[i])
7288 goto error;
7290 isl_space_free(map->dim);
7291 map->dim = isl_local_space_get_space(ls);
7292 if (!map->dim)
7293 goto error;
7295 isl_local_space_free(ls);
7296 return map;
7297 error:
7298 isl_local_space_free(ls);
7299 isl_map_free(map);
7300 return NULL;
7303 /* Compute an explicit representation for the existentially
7304 * quantified variables for which do not know any explicit representation yet.
7306 * We first sort the existentially quantified variables so that the
7307 * existentially quantified variables for which we already have an explicit
7308 * representation are placed before those for which we do not.
7309 * The input dimensions, the output dimensions and the existentially
7310 * quantified variables for which we already have an explicit
7311 * representation are then turned into parameters.
7312 * compute_divs returns a map with the same parameters and
7313 * no input or output dimensions and the dimension specification
7314 * is reset to that of the input, including the existentially quantified
7315 * variables for which we already had an explicit representation.
7317 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7319 struct isl_basic_set *bset;
7320 struct isl_set *set;
7321 struct isl_map *map;
7322 isl_space *space;
7323 isl_local_space *ls;
7324 unsigned nparam;
7325 unsigned n_in;
7326 unsigned n_out;
7327 int n_known;
7328 int i;
7330 bmap = isl_basic_map_sort_divs(bmap);
7331 bmap = isl_basic_map_cow(bmap);
7332 if (!bmap)
7333 return NULL;
7335 n_known = isl_basic_map_first_unknown_div(bmap);
7336 if (n_known < 0)
7337 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7339 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7340 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7341 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7342 space = isl_space_set_alloc(bmap->ctx,
7343 nparam + n_in + n_out + n_known, 0);
7344 if (!space)
7345 goto error;
7347 ls = isl_basic_map_get_local_space(bmap);
7348 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7349 n_known, bmap->n_div - n_known);
7350 if (n_known > 0) {
7351 for (i = n_known; i < bmap->n_div; ++i)
7352 swap_div(bmap, i - n_known, i);
7353 bmap->n_div -= n_known;
7354 bmap->extra -= n_known;
7356 bmap = isl_basic_map_reset_space(bmap, space);
7357 bset = bset_from_bmap(bmap);
7359 set = parameter_compute_divs(bset);
7360 map = set_to_map(set);
7361 map = replace_space_by_local_space(map, ls);
7363 return map;
7364 error:
7365 isl_basic_map_free(bmap);
7366 return NULL;
7369 /* Remove the explicit representation of local variable "div",
7370 * if there is any.
7372 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7373 __isl_take isl_basic_map *bmap, int div)
7375 isl_bool unknown;
7377 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7378 if (unknown < 0)
7379 return isl_basic_map_free(bmap);
7380 if (unknown)
7381 return bmap;
7383 bmap = isl_basic_map_cow(bmap);
7384 if (!bmap)
7385 return NULL;
7386 isl_int_set_si(bmap->div[div][0], 0);
7387 return bmap;
7390 /* Is local variable "div" of "bmap" marked as not having an explicit
7391 * representation?
7392 * Note that even if "div" is not marked in this way and therefore
7393 * has an explicit representation, this representation may still
7394 * depend (indirectly) on other local variables that do not
7395 * have an explicit representation.
7397 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7398 int div)
7400 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7401 return isl_bool_error;
7402 return isl_int_is_zero(bmap->div[div][0]);
7405 /* Return the position of the first local variable that does not
7406 * have an explicit representation.
7407 * Return the total number of local variables if they all have
7408 * an explicit representation.
7409 * Return -1 on error.
7411 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7413 int i;
7415 if (!bmap)
7416 return -1;
7418 for (i = 0; i < bmap->n_div; ++i) {
7419 if (!isl_basic_map_div_is_known(bmap, i))
7420 return i;
7422 return bmap->n_div;
7425 /* Return the position of the first local variable that does not
7426 * have an explicit representation.
7427 * Return the total number of local variables if they all have
7428 * an explicit representation.
7429 * Return -1 on error.
7431 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7433 return isl_basic_map_first_unknown_div(bset);
7436 /* Does "bmap" have an explicit representation for all local variables?
7438 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7440 int first, n;
7442 n = isl_basic_map_dim(bmap, isl_dim_div);
7443 first = isl_basic_map_first_unknown_div(bmap);
7444 if (first < 0)
7445 return isl_bool_error;
7446 return first == n;
7449 /* Do all basic maps in "map" have an explicit representation
7450 * for all local variables?
7452 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7454 int i;
7456 if (!map)
7457 return isl_bool_error;
7459 for (i = 0; i < map->n; ++i) {
7460 int known = isl_basic_map_divs_known(map->p[i]);
7461 if (known <= 0)
7462 return known;
7465 return isl_bool_true;
7468 /* If bmap contains any unknown divs, then compute explicit
7469 * expressions for them. However, this computation may be
7470 * quite expensive, so first try to remove divs that aren't
7471 * strictly needed.
7473 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7475 int known;
7476 struct isl_map *map;
7478 known = isl_basic_map_divs_known(bmap);
7479 if (known < 0)
7480 goto error;
7481 if (known)
7482 return isl_map_from_basic_map(bmap);
7484 bmap = isl_basic_map_drop_redundant_divs(bmap);
7486 known = isl_basic_map_divs_known(bmap);
7487 if (known < 0)
7488 goto error;
7489 if (known)
7490 return isl_map_from_basic_map(bmap);
7492 map = compute_divs(bmap);
7493 return map;
7494 error:
7495 isl_basic_map_free(bmap);
7496 return NULL;
7499 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7501 int i;
7502 int known;
7503 struct isl_map *res;
7505 if (!map)
7506 return NULL;
7507 if (map->n == 0)
7508 return map;
7510 known = isl_map_divs_known(map);
7511 if (known < 0) {
7512 isl_map_free(map);
7513 return NULL;
7515 if (known)
7516 return map;
7518 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7519 for (i = 1 ; i < map->n; ++i) {
7520 struct isl_map *r2;
7521 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7522 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7523 res = isl_map_union_disjoint(res, r2);
7524 else
7525 res = isl_map_union(res, r2);
7527 isl_map_free(map);
7529 return res;
7532 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7534 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7537 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7539 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7542 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7544 int i;
7545 struct isl_set *set;
7547 if (!map)
7548 goto error;
7550 map = isl_map_cow(map);
7551 if (!map)
7552 return NULL;
7554 set = set_from_map(map);
7555 set->dim = isl_space_domain(set->dim);
7556 if (!set->dim)
7557 goto error;
7558 for (i = 0; i < map->n; ++i) {
7559 set->p[i] = isl_basic_map_domain(map->p[i]);
7560 if (!set->p[i])
7561 goto error;
7563 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7564 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7565 return set;
7566 error:
7567 isl_map_free(map);
7568 return NULL;
7571 /* Return the union of "map1" and "map2", where we assume for now that
7572 * "map1" and "map2" are disjoint. Note that the basic maps inside
7573 * "map1" or "map2" may not be disjoint from each other.
7574 * Also note that this function is also called from isl_map_union,
7575 * which takes care of handling the situation where "map1" and "map2"
7576 * may not be disjoint.
7578 * If one of the inputs is empty, we can simply return the other input.
7579 * Similarly, if one of the inputs is universal, then it is equal to the union.
7581 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7582 __isl_take isl_map *map2)
7584 int i;
7585 unsigned flags = 0;
7586 struct isl_map *map = NULL;
7587 int is_universe;
7589 if (!map1 || !map2)
7590 goto error;
7592 if (!isl_space_is_equal(map1->dim, map2->dim))
7593 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7594 "spaces don't match", goto error);
7596 if (map1->n == 0) {
7597 isl_map_free(map1);
7598 return map2;
7600 if (map2->n == 0) {
7601 isl_map_free(map2);
7602 return map1;
7605 is_universe = isl_map_plain_is_universe(map1);
7606 if (is_universe < 0)
7607 goto error;
7608 if (is_universe) {
7609 isl_map_free(map2);
7610 return map1;
7613 is_universe = isl_map_plain_is_universe(map2);
7614 if (is_universe < 0)
7615 goto error;
7616 if (is_universe) {
7617 isl_map_free(map1);
7618 return map2;
7621 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7622 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7623 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7625 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7626 map1->n + map2->n, flags);
7627 if (!map)
7628 goto error;
7629 for (i = 0; i < map1->n; ++i) {
7630 map = isl_map_add_basic_map(map,
7631 isl_basic_map_copy(map1->p[i]));
7632 if (!map)
7633 goto error;
7635 for (i = 0; i < map2->n; ++i) {
7636 map = isl_map_add_basic_map(map,
7637 isl_basic_map_copy(map2->p[i]));
7638 if (!map)
7639 goto error;
7641 isl_map_free(map1);
7642 isl_map_free(map2);
7643 return map;
7644 error:
7645 isl_map_free(map);
7646 isl_map_free(map1);
7647 isl_map_free(map2);
7648 return NULL;
7651 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7652 * guaranteed to be disjoint by the caller.
7654 * Note that this functions is called from within isl_map_make_disjoint,
7655 * so we have to be careful not to touch the constraints of the inputs
7656 * in any way.
7658 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7659 __isl_take isl_map *map2)
7661 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7664 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7665 * not be disjoint. The parameters are assumed to have been aligned.
7667 * We currently simply call map_union_disjoint, the internal operation
7668 * of which does not really depend on the inputs being disjoint.
7669 * If the result contains more than one basic map, then we clear
7670 * the disjoint flag since the result may contain basic maps from
7671 * both inputs and these are not guaranteed to be disjoint.
7673 * As a special case, if "map1" and "map2" are obviously equal,
7674 * then we simply return "map1".
7676 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7677 __isl_take isl_map *map2)
7679 int equal;
7681 if (!map1 || !map2)
7682 goto error;
7684 equal = isl_map_plain_is_equal(map1, map2);
7685 if (equal < 0)
7686 goto error;
7687 if (equal) {
7688 isl_map_free(map2);
7689 return map1;
7692 map1 = map_union_disjoint(map1, map2);
7693 if (!map1)
7694 return NULL;
7695 if (map1->n > 1)
7696 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7697 return map1;
7698 error:
7699 isl_map_free(map1);
7700 isl_map_free(map2);
7701 return NULL;
7704 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7705 * not be disjoint.
7707 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7708 __isl_take isl_map *map2)
7710 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7713 __isl_give isl_set *isl_set_union_disjoint(
7714 __isl_take isl_set *set1, __isl_take isl_set *set2)
7716 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7717 set_to_map(set2)));
7720 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7722 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7725 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7726 * the results.
7728 * "map" and "set" are assumed to be compatible and non-NULL.
7730 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7731 __isl_take isl_set *set,
7732 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7733 __isl_take isl_basic_set *bset))
7735 unsigned flags = 0;
7736 struct isl_map *result;
7737 int i, j;
7739 if (isl_set_plain_is_universe(set)) {
7740 isl_set_free(set);
7741 return map;
7744 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7745 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7746 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7748 result = isl_map_alloc_space(isl_space_copy(map->dim),
7749 map->n * set->n, flags);
7750 for (i = 0; result && i < map->n; ++i)
7751 for (j = 0; j < set->n; ++j) {
7752 result = isl_map_add_basic_map(result,
7753 fn(isl_basic_map_copy(map->p[i]),
7754 isl_basic_set_copy(set->p[j])));
7755 if (!result)
7756 break;
7759 isl_map_free(map);
7760 isl_set_free(set);
7761 return result;
7764 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7765 __isl_take isl_set *set)
7767 isl_bool ok;
7769 ok = isl_map_compatible_range(map, set);
7770 if (ok < 0)
7771 goto error;
7772 if (!ok)
7773 isl_die(set->ctx, isl_error_invalid,
7774 "incompatible spaces", goto error);
7776 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7777 error:
7778 isl_map_free(map);
7779 isl_set_free(set);
7780 return NULL;
7783 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7784 __isl_take isl_set *set)
7786 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7789 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7790 __isl_take isl_set *set)
7792 isl_bool ok;
7794 ok = isl_map_compatible_domain(map, set);
7795 if (ok < 0)
7796 goto error;
7797 if (!ok)
7798 isl_die(set->ctx, isl_error_invalid,
7799 "incompatible spaces", goto error);
7801 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7802 error:
7803 isl_map_free(map);
7804 isl_set_free(set);
7805 return NULL;
7808 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7809 __isl_take isl_set *set)
7811 return isl_map_align_params_map_map_and(map, set,
7812 &map_intersect_domain);
7815 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7816 * in the space B -> C, return the intersection.
7817 * The parameters are assumed to have been aligned.
7819 * The map "factor" is first extended to a map living in the space
7820 * [A -> B] -> C and then a regular intersection is computed.
7822 static __isl_give isl_map *map_intersect_domain_factor_range(
7823 __isl_take isl_map *map, __isl_take isl_map *factor)
7825 isl_space *space;
7826 isl_map *ext_factor;
7828 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7829 ext_factor = isl_map_universe(space);
7830 ext_factor = isl_map_domain_product(ext_factor, factor);
7831 return map_intersect(map, ext_factor);
7834 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7835 * in the space B -> C, return the intersection.
7837 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7838 __isl_take isl_map *map, __isl_take isl_map *factor)
7840 return isl_map_align_params_map_map_and(map, factor,
7841 &map_intersect_domain_factor_range);
7844 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7845 * in the space A -> C, return the intersection.
7847 * The map "factor" is first extended to a map living in the space
7848 * A -> [B -> C] and then a regular intersection is computed.
7850 static __isl_give isl_map *map_intersect_range_factor_range(
7851 __isl_take isl_map *map, __isl_take isl_map *factor)
7853 isl_space *space;
7854 isl_map *ext_factor;
7856 space = isl_space_range_factor_domain(isl_map_get_space(map));
7857 ext_factor = isl_map_universe(space);
7858 ext_factor = isl_map_range_product(ext_factor, factor);
7859 return isl_map_intersect(map, ext_factor);
7862 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7863 * in the space A -> C, return the intersection.
7865 __isl_give isl_map *isl_map_intersect_range_factor_range(
7866 __isl_take isl_map *map, __isl_take isl_map *factor)
7868 return isl_map_align_params_map_map_and(map, factor,
7869 &map_intersect_range_factor_range);
7872 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7873 __isl_take isl_map *map2)
7875 if (!map1 || !map2)
7876 goto error;
7877 map1 = isl_map_reverse(map1);
7878 map1 = isl_map_apply_range(map1, map2);
7879 return isl_map_reverse(map1);
7880 error:
7881 isl_map_free(map1);
7882 isl_map_free(map2);
7883 return NULL;
7886 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7887 __isl_take isl_map *map2)
7889 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7892 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7893 __isl_take isl_map *map2)
7895 isl_space *space;
7896 struct isl_map *result;
7897 int i, j;
7899 if (!map1 || !map2)
7900 goto error;
7902 space = isl_space_join(isl_space_copy(map1->dim),
7903 isl_space_copy(map2->dim));
7905 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7906 if (!result)
7907 goto error;
7908 for (i = 0; i < map1->n; ++i)
7909 for (j = 0; j < map2->n; ++j) {
7910 result = isl_map_add_basic_map(result,
7911 isl_basic_map_apply_range(
7912 isl_basic_map_copy(map1->p[i]),
7913 isl_basic_map_copy(map2->p[j])));
7914 if (!result)
7915 goto error;
7917 isl_map_free(map1);
7918 isl_map_free(map2);
7919 if (result && result->n <= 1)
7920 ISL_F_SET(result, ISL_MAP_DISJOINT);
7921 return result;
7922 error:
7923 isl_map_free(map1);
7924 isl_map_free(map2);
7925 return NULL;
7928 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7929 __isl_take isl_map *map2)
7931 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7935 * returns range - domain
7937 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
7939 isl_space *target_space;
7940 struct isl_basic_set *bset;
7941 unsigned dim;
7942 unsigned nparam;
7943 int i;
7945 if (!bmap)
7946 goto error;
7947 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7948 bmap->dim, isl_dim_out),
7949 goto error);
7950 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7951 dim = isl_basic_map_dim(bmap, isl_dim_in);
7952 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7953 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7954 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7955 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7956 for (i = 0; i < dim; ++i) {
7957 int j = isl_basic_map_alloc_equality(bmap);
7958 if (j < 0) {
7959 bmap = isl_basic_map_free(bmap);
7960 break;
7962 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7963 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7964 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7965 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7967 bset = isl_basic_map_domain(bmap);
7968 bset = isl_basic_set_reset_space(bset, target_space);
7969 return bset;
7970 error:
7971 isl_basic_map_free(bmap);
7972 return NULL;
7975 /* Check that domain and range of "map" are the same.
7977 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
7979 isl_space *space;
7980 isl_bool equal;
7982 space = isl_map_peek_space(map);
7983 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
7984 if (equal < 0)
7985 return isl_stat_error;
7986 if (!equal)
7987 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7988 "domain and range don't match", return isl_stat_error);
7989 return isl_stat_ok;
7993 * returns range - domain
7995 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7997 int i;
7998 isl_space *dim;
7999 struct isl_set *result;
8001 if (!map)
8002 return NULL;
8004 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8005 map->dim, isl_dim_out),
8006 goto error);
8007 dim = isl_map_get_space(map);
8008 dim = isl_space_domain(dim);
8009 result = isl_set_alloc_space(dim, map->n, 0);
8010 if (!result)
8011 goto error;
8012 for (i = 0; i < map->n; ++i)
8013 result = isl_set_add_basic_set(result,
8014 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8015 isl_map_free(map);
8016 return result;
8017 error:
8018 isl_map_free(map);
8019 return NULL;
8023 * returns [domain -> range] -> range - domain
8025 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8026 __isl_take isl_basic_map *bmap)
8028 int i, k;
8029 isl_space *space;
8030 isl_basic_map *domain;
8031 int nparam, n;
8032 unsigned total;
8034 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8035 bmap->dim, isl_dim_out))
8036 isl_die(bmap->ctx, isl_error_invalid,
8037 "domain and range don't match", goto error);
8039 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8040 n = isl_basic_map_dim(bmap, isl_dim_in);
8042 space = isl_basic_map_get_space(bmap);
8043 space = isl_space_from_range(isl_space_domain(space));
8044 domain = isl_basic_map_universe(space);
8046 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8047 bmap = isl_basic_map_apply_range(bmap, domain);
8048 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8050 total = isl_basic_map_total_dim(bmap);
8052 for (i = 0; i < n; ++i) {
8053 k = isl_basic_map_alloc_equality(bmap);
8054 if (k < 0)
8055 goto error;
8056 isl_seq_clr(bmap->eq[k], 1 + total);
8057 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8058 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8059 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8062 bmap = isl_basic_map_gauss(bmap, NULL);
8063 return isl_basic_map_finalize(bmap);
8064 error:
8065 isl_basic_map_free(bmap);
8066 return NULL;
8070 * returns [domain -> range] -> range - domain
8072 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8074 int i;
8075 isl_space *domain_space;
8077 if (isl_map_check_equal_tuples(map) < 0)
8078 return isl_map_free(map);
8080 map = isl_map_cow(map);
8081 if (!map)
8082 return NULL;
8084 domain_space = isl_space_domain(isl_map_get_space(map));
8085 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8086 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8087 if (!map->dim)
8088 goto error;
8089 for (i = 0; i < map->n; ++i) {
8090 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8091 if (!map->p[i])
8092 goto error;
8094 map = isl_map_unmark_normalized(map);
8095 return map;
8096 error:
8097 isl_map_free(map);
8098 return NULL;
8101 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8103 unsigned n_in, n_out;
8105 if (!space)
8106 return NULL;
8107 n_in = isl_space_dim(space, isl_dim_in);
8108 n_out = isl_space_dim(space, isl_dim_out);
8109 if (n_in != n_out)
8110 isl_die(space->ctx, isl_error_invalid,
8111 "number of input and output dimensions needs to be "
8112 "the same", goto error);
8113 return isl_basic_map_equal(space, n_in);
8114 error:
8115 isl_space_free(space);
8116 return NULL;
8119 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8121 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8124 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8126 isl_space *dim = isl_set_get_space(set);
8127 isl_map *id;
8128 id = isl_map_identity(isl_space_map_from_set(dim));
8129 return isl_map_intersect_range(id, set);
8132 /* Construct a basic set with all set dimensions having only non-negative
8133 * values.
8135 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8136 __isl_take isl_space *space)
8138 int i;
8139 unsigned nparam;
8140 unsigned dim;
8141 struct isl_basic_set *bset;
8143 if (!space)
8144 return NULL;
8145 nparam = isl_space_dim(space, isl_dim_param);
8146 dim = isl_space_dim(space, isl_dim_set);
8147 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8148 if (!bset)
8149 return NULL;
8150 for (i = 0; i < dim; ++i) {
8151 int k = isl_basic_set_alloc_inequality(bset);
8152 if (k < 0)
8153 goto error;
8154 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8155 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8157 return bset;
8158 error:
8159 isl_basic_set_free(bset);
8160 return NULL;
8163 /* Construct the half-space x_pos >= 0.
8165 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8166 int pos)
8168 int k;
8169 isl_basic_set *nonneg;
8171 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8172 k = isl_basic_set_alloc_inequality(nonneg);
8173 if (k < 0)
8174 goto error;
8175 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8176 isl_int_set_si(nonneg->ineq[k][pos], 1);
8178 return isl_basic_set_finalize(nonneg);
8179 error:
8180 isl_basic_set_free(nonneg);
8181 return NULL;
8184 /* Construct the half-space x_pos <= -1.
8186 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8187 int pos)
8189 int k;
8190 isl_basic_set *neg;
8192 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8193 k = isl_basic_set_alloc_inequality(neg);
8194 if (k < 0)
8195 goto error;
8196 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8197 isl_int_set_si(neg->ineq[k][0], -1);
8198 isl_int_set_si(neg->ineq[k][pos], -1);
8200 return isl_basic_set_finalize(neg);
8201 error:
8202 isl_basic_set_free(neg);
8203 return NULL;
8206 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8207 enum isl_dim_type type, unsigned first, unsigned n)
8209 int i;
8210 unsigned offset;
8211 isl_basic_set *nonneg;
8212 isl_basic_set *neg;
8214 if (n == 0)
8215 return set;
8217 if (isl_set_check_range(set, type, first, n) < 0)
8218 return isl_set_free(set);
8220 offset = pos(set->dim, type);
8221 for (i = 0; i < n; ++i) {
8222 nonneg = nonneg_halfspace(isl_set_get_space(set),
8223 offset + first + i);
8224 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8226 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8229 return set;
8232 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8233 int len,
8234 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8235 void *user)
8237 isl_set *half;
8239 if (!set)
8240 return isl_stat_error;
8241 if (isl_set_plain_is_empty(set)) {
8242 isl_set_free(set);
8243 return isl_stat_ok;
8245 if (first == len)
8246 return fn(set, signs, user);
8248 signs[first] = 1;
8249 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8250 1 + first));
8251 half = isl_set_intersect(half, isl_set_copy(set));
8252 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8253 goto error;
8255 signs[first] = -1;
8256 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8257 1 + first));
8258 half = isl_set_intersect(half, set);
8259 return foreach_orthant(half, signs, first + 1, len, fn, user);
8260 error:
8261 isl_set_free(set);
8262 return isl_stat_error;
8265 /* Call "fn" on the intersections of "set" with each of the orthants
8266 * (except for obviously empty intersections). The orthant is identified
8267 * by the signs array, with each entry having value 1 or -1 according
8268 * to the sign of the corresponding variable.
8270 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8271 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8272 void *user)
8274 unsigned nparam;
8275 unsigned nvar;
8276 int *signs;
8277 isl_stat r;
8279 if (!set)
8280 return isl_stat_error;
8281 if (isl_set_plain_is_empty(set))
8282 return isl_stat_ok;
8284 nparam = isl_set_dim(set, isl_dim_param);
8285 nvar = isl_set_dim(set, isl_dim_set);
8287 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8289 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8290 fn, user);
8292 free(signs);
8294 return r;
8297 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8299 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8302 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8303 __isl_keep isl_basic_map *bmap2)
8305 isl_bool is_subset;
8306 struct isl_map *map1;
8307 struct isl_map *map2;
8309 if (!bmap1 || !bmap2)
8310 return isl_bool_error;
8312 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8313 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8315 is_subset = isl_map_is_subset(map1, map2);
8317 isl_map_free(map1);
8318 isl_map_free(map2);
8320 return is_subset;
8323 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8324 __isl_keep isl_basic_set *bset2)
8326 return isl_basic_map_is_subset(bset1, bset2);
8329 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8330 __isl_keep isl_basic_map *bmap2)
8332 isl_bool is_subset;
8334 if (!bmap1 || !bmap2)
8335 return isl_bool_error;
8336 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8337 if (is_subset != isl_bool_true)
8338 return is_subset;
8339 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8340 return is_subset;
8343 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8344 __isl_keep isl_basic_set *bset2)
8346 return isl_basic_map_is_equal(
8347 bset_to_bmap(bset1), bset_to_bmap(bset2));
8350 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8352 int i;
8353 int is_empty;
8355 if (!map)
8356 return isl_bool_error;
8357 for (i = 0; i < map->n; ++i) {
8358 is_empty = isl_basic_map_is_empty(map->p[i]);
8359 if (is_empty < 0)
8360 return isl_bool_error;
8361 if (!is_empty)
8362 return isl_bool_false;
8364 return isl_bool_true;
8367 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8369 return map ? map->n == 0 : isl_bool_error;
8372 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8374 return set ? set->n == 0 : isl_bool_error;
8377 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8379 return isl_map_is_empty(set_to_map(set));
8382 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8383 __isl_keep isl_map *map2)
8385 if (!map1 || !map2)
8386 return isl_bool_error;
8388 return isl_space_is_equal(map1->dim, map2->dim);
8391 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8392 __isl_keep isl_set *set2)
8394 if (!set1 || !set2)
8395 return isl_bool_error;
8397 return isl_space_is_equal(set1->dim, set2->dim);
8400 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8402 isl_bool is_subset;
8404 if (!map1 || !map2)
8405 return isl_bool_error;
8406 is_subset = isl_map_is_subset(map1, map2);
8407 if (is_subset != isl_bool_true)
8408 return is_subset;
8409 is_subset = isl_map_is_subset(map2, map1);
8410 return is_subset;
8413 /* Is "map1" equal to "map2"?
8415 * First check if they are obviously equal.
8416 * If not, then perform a more detailed analysis.
8418 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8420 isl_bool equal;
8422 equal = isl_map_plain_is_equal(map1, map2);
8423 if (equal < 0 || equal)
8424 return equal;
8425 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8428 isl_bool isl_basic_map_is_strict_subset(
8429 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8431 isl_bool is_subset;
8433 if (!bmap1 || !bmap2)
8434 return isl_bool_error;
8435 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8436 if (is_subset != isl_bool_true)
8437 return is_subset;
8438 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8439 return isl_bool_not(is_subset);
8442 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8443 __isl_keep isl_map *map2)
8445 isl_bool is_subset;
8447 if (!map1 || !map2)
8448 return isl_bool_error;
8449 is_subset = isl_map_is_subset(map1, map2);
8450 if (is_subset != isl_bool_true)
8451 return is_subset;
8452 is_subset = isl_map_is_subset(map2, map1);
8453 return isl_bool_not(is_subset);
8456 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8457 __isl_keep isl_set *set2)
8459 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8462 /* Is "bmap" obviously equal to the universe with the same space?
8464 * That is, does it not have any constraints?
8466 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8468 if (!bmap)
8469 return isl_bool_error;
8470 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8473 /* Is "bset" obviously equal to the universe with the same space?
8475 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8477 return isl_basic_map_plain_is_universe(bset);
8480 /* If "c" does not involve any existentially quantified variables,
8481 * then set *univ to false and abort
8483 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8485 isl_bool *univ = user;
8486 unsigned n;
8488 n = isl_constraint_dim(c, isl_dim_div);
8489 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8490 isl_constraint_free(c);
8491 if (*univ < 0 || !*univ)
8492 return isl_stat_error;
8493 return isl_stat_ok;
8496 /* Is "bmap" equal to the universe with the same space?
8498 * First check if it is obviously equal to the universe.
8499 * If not and if there are any constraints not involving
8500 * existentially quantified variables, then it is certainly
8501 * not equal to the universe.
8502 * Otherwise, check if the universe is a subset of "bmap".
8504 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8506 isl_bool univ;
8507 isl_basic_map *test;
8509 univ = isl_basic_map_plain_is_universe(bmap);
8510 if (univ < 0 || univ)
8511 return univ;
8512 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8513 return isl_bool_false;
8514 univ = isl_bool_true;
8515 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8516 univ)
8517 return isl_bool_error;
8518 if (univ < 0 || !univ)
8519 return univ;
8520 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8521 univ = isl_basic_map_is_subset(test, bmap);
8522 isl_basic_map_free(test);
8523 return univ;
8526 /* Is "bset" equal to the universe with the same space?
8528 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8530 return isl_basic_map_is_universe(bset);
8533 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8535 int i;
8537 if (!map)
8538 return isl_bool_error;
8540 for (i = 0; i < map->n; ++i) {
8541 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8542 if (r < 0 || r)
8543 return r;
8546 return isl_bool_false;
8549 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8551 return isl_map_plain_is_universe(set_to_map(set));
8554 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8556 struct isl_basic_set *bset = NULL;
8557 struct isl_vec *sample = NULL;
8558 isl_bool empty, non_empty;
8560 if (!bmap)
8561 return isl_bool_error;
8563 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8564 return isl_bool_true;
8566 if (isl_basic_map_plain_is_universe(bmap))
8567 return isl_bool_false;
8569 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8570 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8571 copy = isl_basic_map_remove_redundancies(copy);
8572 empty = isl_basic_map_plain_is_empty(copy);
8573 isl_basic_map_free(copy);
8574 return empty;
8577 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8578 if (non_empty < 0)
8579 return isl_bool_error;
8580 if (non_empty)
8581 return isl_bool_false;
8582 isl_vec_free(bmap->sample);
8583 bmap->sample = NULL;
8584 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8585 if (!bset)
8586 return isl_bool_error;
8587 sample = isl_basic_set_sample_vec(bset);
8588 if (!sample)
8589 return isl_bool_error;
8590 empty = sample->size == 0;
8591 isl_vec_free(bmap->sample);
8592 bmap->sample = sample;
8593 if (empty)
8594 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8596 return empty;
8599 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8601 if (!bmap)
8602 return isl_bool_error;
8603 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8606 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8608 if (!bset)
8609 return isl_bool_error;
8610 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8613 /* Is "bmap" known to be non-empty?
8615 * That is, is the cached sample still valid?
8617 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8619 unsigned total;
8621 if (!bmap)
8622 return isl_bool_error;
8623 if (!bmap->sample)
8624 return isl_bool_false;
8625 total = 1 + isl_basic_map_total_dim(bmap);
8626 if (bmap->sample->size != total)
8627 return isl_bool_false;
8628 return isl_basic_map_contains(bmap, bmap->sample);
8631 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8633 return isl_basic_map_is_empty(bset_to_bmap(bset));
8636 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8637 __isl_take isl_basic_map *bmap2)
8639 struct isl_map *map;
8640 if (!bmap1 || !bmap2)
8641 goto error;
8643 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8645 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8646 if (!map)
8647 goto error;
8648 map = isl_map_add_basic_map(map, bmap1);
8649 map = isl_map_add_basic_map(map, bmap2);
8650 return map;
8651 error:
8652 isl_basic_map_free(bmap1);
8653 isl_basic_map_free(bmap2);
8654 return NULL;
8657 struct isl_set *isl_basic_set_union(
8658 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8660 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8661 bset_to_bmap(bset2)));
8664 /* Order divs such that any div only depends on previous divs */
8665 __isl_give isl_basic_map *isl_basic_map_order_divs(
8666 __isl_take isl_basic_map *bmap)
8668 int i;
8669 int off;
8671 off = isl_basic_map_var_offset(bmap, isl_dim_div);
8672 if (off < 0)
8673 return isl_basic_map_free(bmap);
8675 for (i = 0; i < bmap->n_div; ++i) {
8676 int pos;
8677 if (isl_int_is_zero(bmap->div[i][0]))
8678 continue;
8679 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8680 bmap->n_div-i);
8681 if (pos == -1)
8682 continue;
8683 if (pos == 0)
8684 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8685 "integer division depends on itself",
8686 return isl_basic_map_free(bmap));
8687 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8688 if (!bmap)
8689 return NULL;
8690 --i;
8692 return bmap;
8695 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8697 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8700 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8702 int i;
8704 if (!map)
8705 return 0;
8707 for (i = 0; i < map->n; ++i) {
8708 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8709 if (!map->p[i])
8710 goto error;
8713 return map;
8714 error:
8715 isl_map_free(map);
8716 return NULL;
8719 /* Sort the local variables of "bset".
8721 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8722 __isl_take isl_basic_set *bset)
8724 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8727 /* Apply the expansion computed by isl_merge_divs.
8728 * The expansion itself is given by "exp" while the resulting
8729 * list of divs is given by "div".
8731 * Move the integer divisions of "bmap" into the right position
8732 * according to "exp" and then introduce the additional integer
8733 * divisions, adding div constraints.
8734 * The moving should be done first to avoid moving coefficients
8735 * in the definitions of the extra integer divisions.
8737 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8738 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8740 int i, j;
8741 int n_div;
8743 bmap = isl_basic_map_cow(bmap);
8744 if (!bmap || !div)
8745 goto error;
8747 if (div->n_row < bmap->n_div)
8748 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8749 "not an expansion", goto error);
8751 n_div = bmap->n_div;
8752 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8753 div->n_row - n_div, 0,
8754 2 * (div->n_row - n_div));
8756 for (i = n_div; i < div->n_row; ++i)
8757 if (isl_basic_map_alloc_div(bmap) < 0)
8758 goto error;
8760 for (j = n_div - 1; j >= 0; --j) {
8761 if (exp[j] == j)
8762 break;
8763 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8764 if (!bmap)
8765 goto error;
8767 j = 0;
8768 for (i = 0; i < div->n_row; ++i) {
8769 if (j < n_div && exp[j] == i) {
8770 j++;
8771 } else {
8772 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8773 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8774 continue;
8775 bmap = isl_basic_map_add_div_constraints(bmap, i);
8776 if (!bmap)
8777 goto error;
8781 isl_mat_free(div);
8782 return bmap;
8783 error:
8784 isl_basic_map_free(bmap);
8785 isl_mat_free(div);
8786 return NULL;
8789 /* Apply the expansion computed by isl_merge_divs.
8790 * The expansion itself is given by "exp" while the resulting
8791 * list of divs is given by "div".
8793 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8794 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8796 return isl_basic_map_expand_divs(bset, div, exp);
8799 /* Look for a div in dst that corresponds to the div "div" in src.
8800 * The divs before "div" in src and dst are assumed to be the same.
8802 * Return the position of the corresponding div in dst
8803 * if there is one. Otherwise, return a position beyond the integer divisions.
8804 * Return -1 on error.
8806 static int find_div(__isl_keep isl_basic_map *dst,
8807 __isl_keep isl_basic_map *src, unsigned div)
8809 int i;
8810 unsigned n_div;
8811 int v_div;
8813 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8814 if (!dst || v_div < 0)
8815 return -1;
8817 n_div = isl_basic_map_dim(dst, isl_dim_div);
8818 isl_assert(dst->ctx, div <= n_div, return -1);
8819 for (i = div; i < n_div; ++i)
8820 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+v_div+div) &&
8821 isl_seq_first_non_zero(dst->div[i] + 1 + 1 + v_div + div,
8822 n_div - div) == -1)
8823 return i;
8824 return n_div;
8827 /* Align the divs of "dst" to those of "src", adding divs from "src"
8828 * if needed. That is, make sure that the first src->n_div divs
8829 * of the result are equal to those of src.
8831 * The result is not finalized as by design it will have redundant
8832 * divs if any divs from "src" were copied.
8834 __isl_give isl_basic_map *isl_basic_map_align_divs(
8835 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8837 int i;
8838 isl_bool known;
8839 int extended;
8840 int v_div;
8841 unsigned dst_n_div;
8843 if (!dst || !src)
8844 return isl_basic_map_free(dst);
8846 if (src->n_div == 0)
8847 return dst;
8849 known = isl_basic_map_divs_known(src);
8850 if (known < 0)
8851 return isl_basic_map_free(dst);
8852 if (!known)
8853 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8854 "some src divs are unknown",
8855 return isl_basic_map_free(dst));
8857 v_div = isl_basic_map_var_offset(src, isl_dim_div);
8858 if (v_div < 0)
8859 return isl_basic_map_free(dst);
8861 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8862 if (!src)
8863 return isl_basic_map_free(dst);
8865 extended = 0;
8866 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
8867 for (i = 0; i < src->n_div; ++i) {
8868 int j = find_div(dst, src, i);
8869 if (j < 0)
8870 dst = isl_basic_map_free(dst);
8871 if (j == dst_n_div) {
8872 if (!extended) {
8873 int extra = src->n_div - i;
8874 dst = isl_basic_map_cow(dst);
8875 if (!dst)
8876 goto error;
8877 dst = isl_basic_map_extend_space(dst,
8878 isl_space_copy(dst->dim),
8879 extra, 0, 2 * extra);
8880 extended = 1;
8882 j = isl_basic_map_alloc_div(dst);
8883 if (j < 0)
8884 goto error;
8885 isl_seq_cpy(dst->div[j], src->div[i], 1+1+v_div+i);
8886 isl_seq_clr(dst->div[j]+1+1+v_div+i, dst->n_div - i);
8887 dst_n_div++;
8888 dst = isl_basic_map_add_div_constraints(dst, j);
8889 if (!dst)
8890 goto error;
8892 if (j != i)
8893 dst = isl_basic_map_swap_div(dst, i, j);
8894 if (!dst)
8895 goto error;
8897 isl_basic_map_free(src);
8898 return dst;
8899 error:
8900 isl_basic_map_free(src);
8901 isl_basic_map_free(dst);
8902 return NULL;
8905 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8907 int i;
8909 if (!map)
8910 return NULL;
8911 if (map->n == 0)
8912 return map;
8913 map = isl_map_compute_divs(map);
8914 map = isl_map_cow(map);
8915 if (!map)
8916 return NULL;
8918 for (i = 1; i < map->n; ++i)
8919 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8920 for (i = 1; i < map->n; ++i) {
8921 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8922 if (!map->p[i])
8923 return isl_map_free(map);
8926 map = isl_map_unmark_normalized(map);
8927 return map;
8930 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8932 return isl_map_align_divs_internal(map);
8935 struct isl_set *isl_set_align_divs(struct isl_set *set)
8937 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8940 /* Align the divs of the basic maps in "map" to those
8941 * of the basic maps in "list", as well as to the other basic maps in "map".
8942 * The elements in "list" are assumed to have known divs.
8944 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8945 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8947 int i, n;
8949 map = isl_map_compute_divs(map);
8950 map = isl_map_cow(map);
8951 if (!map || !list)
8952 return isl_map_free(map);
8953 if (map->n == 0)
8954 return map;
8956 n = isl_basic_map_list_n_basic_map(list);
8957 for (i = 0; i < n; ++i) {
8958 isl_basic_map *bmap;
8960 bmap = isl_basic_map_list_get_basic_map(list, i);
8961 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8962 isl_basic_map_free(bmap);
8964 if (!map->p[0])
8965 return isl_map_free(map);
8967 return isl_map_align_divs_internal(map);
8970 /* Align the divs of each element of "list" to those of "bmap".
8971 * Both "bmap" and the elements of "list" are assumed to have known divs.
8973 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8974 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8976 int i, n;
8978 if (!list || !bmap)
8979 return isl_basic_map_list_free(list);
8981 n = isl_basic_map_list_n_basic_map(list);
8982 for (i = 0; i < n; ++i) {
8983 isl_basic_map *bmap_i;
8985 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8986 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8987 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8990 return list;
8993 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8994 __isl_take isl_map *map)
8996 isl_bool ok;
8998 ok = isl_map_compatible_domain(map, set);
8999 if (ok < 0)
9000 goto error;
9001 if (!ok)
9002 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9003 "incompatible spaces", goto error);
9004 map = isl_map_intersect_domain(map, set);
9005 set = isl_map_range(map);
9006 return set;
9007 error:
9008 isl_set_free(set);
9009 isl_map_free(map);
9010 return NULL;
9013 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9014 __isl_take isl_map *map)
9016 return isl_map_align_params_map_map_and(set, map, &set_apply);
9019 /* There is no need to cow as removing empty parts doesn't change
9020 * the meaning of the set.
9022 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9024 int i;
9026 if (!map)
9027 return NULL;
9029 for (i = map->n - 1; i >= 0; --i)
9030 map = remove_if_empty(map, i);
9032 return map;
9035 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9037 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9040 /* Create a binary relation that maps the shared initial "pos" dimensions
9041 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9043 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9044 __isl_keep isl_basic_set *bset2, int pos)
9046 isl_basic_map *bmap1;
9047 isl_basic_map *bmap2;
9049 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9050 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9051 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9052 isl_dim_out, 0, pos);
9053 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9054 isl_dim_out, 0, pos);
9055 return isl_basic_map_range_product(bmap1, bmap2);
9058 /* Given two basic sets bset1 and bset2, compute the maximal difference
9059 * between the values of dimension pos in bset1 and those in bset2
9060 * for any common value of the parameters and dimensions preceding pos.
9062 static enum isl_lp_result basic_set_maximal_difference_at(
9063 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9064 int pos, isl_int *opt)
9066 isl_basic_map *bmap1;
9067 struct isl_ctx *ctx;
9068 struct isl_vec *obj;
9069 unsigned total;
9070 unsigned nparam;
9071 unsigned dim1;
9072 enum isl_lp_result res;
9074 if (!bset1 || !bset2)
9075 return isl_lp_error;
9077 nparam = isl_basic_set_n_param(bset1);
9078 dim1 = isl_basic_set_n_dim(bset1);
9080 bmap1 = join_initial(bset1, bset2, pos);
9081 if (!bmap1)
9082 return isl_lp_error;
9084 total = isl_basic_map_total_dim(bmap1);
9085 ctx = bmap1->ctx;
9086 obj = isl_vec_alloc(ctx, 1 + total);
9087 if (!obj)
9088 goto error;
9089 isl_seq_clr(obj->block.data, 1 + total);
9090 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9091 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9092 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9093 opt, NULL, NULL);
9094 isl_basic_map_free(bmap1);
9095 isl_vec_free(obj);
9096 return res;
9097 error:
9098 isl_basic_map_free(bmap1);
9099 return isl_lp_error;
9102 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9103 * for any common value of the parameters and dimensions preceding pos
9104 * in both basic sets, the values of dimension pos in bset1 are
9105 * smaller or larger than those in bset2.
9107 * Returns
9108 * 1 if bset1 follows bset2
9109 * -1 if bset1 precedes bset2
9110 * 0 if bset1 and bset2 are incomparable
9111 * -2 if some error occurred.
9113 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9114 __isl_keep isl_basic_set *bset2, int pos)
9116 isl_int opt;
9117 enum isl_lp_result res;
9118 int cmp;
9120 isl_int_init(opt);
9122 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9124 if (res == isl_lp_empty)
9125 cmp = 0;
9126 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9127 res == isl_lp_unbounded)
9128 cmp = 1;
9129 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9130 cmp = -1;
9131 else
9132 cmp = -2;
9134 isl_int_clear(opt);
9135 return cmp;
9138 /* Given two basic sets bset1 and bset2, check whether
9139 * for any common value of the parameters and dimensions preceding pos
9140 * there is a value of dimension pos in bset1 that is larger
9141 * than a value of the same dimension in bset2.
9143 * Return
9144 * 1 if there exists such a pair
9145 * 0 if there is no such pair, but there is a pair of equal values
9146 * -1 otherwise
9147 * -2 if some error occurred.
9149 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9150 __isl_keep isl_basic_set *bset2, int pos)
9152 isl_bool empty;
9153 isl_basic_map *bmap;
9154 unsigned dim1;
9156 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9157 bmap = join_initial(bset1, bset2, pos);
9158 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9159 isl_dim_out, dim1 - pos);
9160 empty = isl_basic_map_is_empty(bmap);
9161 if (empty < 0)
9162 goto error;
9163 if (empty) {
9164 isl_basic_map_free(bmap);
9165 return -1;
9167 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9168 isl_dim_out, dim1 - pos);
9169 empty = isl_basic_map_is_empty(bmap);
9170 if (empty < 0)
9171 goto error;
9172 isl_basic_map_free(bmap);
9173 if (empty)
9174 return 0;
9175 return 1;
9176 error:
9177 isl_basic_map_free(bmap);
9178 return -2;
9181 /* Given two sets set1 and set2, check whether
9182 * for any common value of the parameters and dimensions preceding pos
9183 * there is a value of dimension pos in set1 that is larger
9184 * than a value of the same dimension in set2.
9186 * Return
9187 * 1 if there exists such a pair
9188 * 0 if there is no such pair, but there is a pair of equal values
9189 * -1 otherwise
9190 * -2 if some error occurred.
9192 int isl_set_follows_at(__isl_keep isl_set *set1,
9193 __isl_keep isl_set *set2, int pos)
9195 int i, j;
9196 int follows = -1;
9198 if (!set1 || !set2)
9199 return -2;
9201 for (i = 0; i < set1->n; ++i)
9202 for (j = 0; j < set2->n; ++j) {
9203 int f;
9204 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9205 if (f == 1 || f == -2)
9206 return f;
9207 if (f > follows)
9208 follows = f;
9211 return follows;
9214 static isl_bool isl_basic_map_plain_has_fixed_var(
9215 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9217 int i;
9218 int d;
9219 unsigned total;
9221 if (!bmap)
9222 return isl_bool_error;
9223 total = isl_basic_map_total_dim(bmap);
9224 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9225 for (; d+1 > pos; --d)
9226 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9227 break;
9228 if (d != pos)
9229 continue;
9230 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9231 return isl_bool_false;
9232 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9233 return isl_bool_false;
9234 if (!isl_int_is_one(bmap->eq[i][1+d]))
9235 return isl_bool_false;
9236 if (val)
9237 isl_int_neg(*val, bmap->eq[i][0]);
9238 return isl_bool_true;
9240 return isl_bool_false;
9243 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9244 unsigned pos, isl_int *val)
9246 int i;
9247 isl_int v;
9248 isl_int tmp;
9249 isl_bool fixed;
9251 if (!map)
9252 return isl_bool_error;
9253 if (map->n == 0)
9254 return isl_bool_false;
9255 if (map->n == 1)
9256 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9257 isl_int_init(v);
9258 isl_int_init(tmp);
9259 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9260 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9261 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9262 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9263 fixed = isl_bool_false;
9265 if (val)
9266 isl_int_set(*val, v);
9267 isl_int_clear(tmp);
9268 isl_int_clear(v);
9269 return fixed;
9272 static isl_bool isl_basic_set_plain_has_fixed_var(
9273 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9275 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9276 pos, val);
9279 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9280 enum isl_dim_type type, unsigned pos, isl_int *val)
9282 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9283 return isl_bool_error;
9284 return isl_basic_map_plain_has_fixed_var(bmap,
9285 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9288 /* If "bmap" obviously lies on a hyperplane where the given dimension
9289 * has a fixed value, then return that value.
9290 * Otherwise return NaN.
9292 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9293 __isl_keep isl_basic_map *bmap,
9294 enum isl_dim_type type, unsigned pos)
9296 isl_ctx *ctx;
9297 isl_val *v;
9298 isl_bool fixed;
9300 if (!bmap)
9301 return NULL;
9302 ctx = isl_basic_map_get_ctx(bmap);
9303 v = isl_val_alloc(ctx);
9304 if (!v)
9305 return NULL;
9306 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9307 if (fixed < 0)
9308 return isl_val_free(v);
9309 if (fixed) {
9310 isl_int_set_si(v->d, 1);
9311 return v;
9313 isl_val_free(v);
9314 return isl_val_nan(ctx);
9317 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9318 enum isl_dim_type type, unsigned pos, isl_int *val)
9320 if (isl_map_check_range(map, type, pos, 1) < 0)
9321 return isl_bool_error;
9322 return isl_map_plain_has_fixed_var(map,
9323 map_offset(map, type) - 1 + pos, val);
9326 /* If "map" obviously lies on a hyperplane where the given dimension
9327 * has a fixed value, then return that value.
9328 * Otherwise return NaN.
9330 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9331 enum isl_dim_type type, unsigned pos)
9333 isl_ctx *ctx;
9334 isl_val *v;
9335 isl_bool fixed;
9337 if (!map)
9338 return NULL;
9339 ctx = isl_map_get_ctx(map);
9340 v = isl_val_alloc(ctx);
9341 if (!v)
9342 return NULL;
9343 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9344 if (fixed < 0)
9345 return isl_val_free(v);
9346 if (fixed) {
9347 isl_int_set_si(v->d, 1);
9348 return v;
9350 isl_val_free(v);
9351 return isl_val_nan(ctx);
9354 /* If "set" obviously lies on a hyperplane where the given dimension
9355 * has a fixed value, then return that value.
9356 * Otherwise return NaN.
9358 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9359 enum isl_dim_type type, unsigned pos)
9361 return isl_map_plain_get_val_if_fixed(set, type, pos);
9364 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9365 * then return this fixed value in *val.
9367 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9368 unsigned dim, isl_int *val)
9370 return isl_basic_set_plain_has_fixed_var(bset,
9371 isl_basic_set_n_param(bset) + dim, val);
9374 /* Return -1 if the constraint "c1" should be sorted before "c2"
9375 * and 1 if it should be sorted after "c2".
9376 * Return 0 if the two constraints are the same (up to the constant term).
9378 * In particular, if a constraint involves later variables than another
9379 * then it is sorted after this other constraint.
9380 * uset_gist depends on constraints without existentially quantified
9381 * variables sorting first.
9383 * For constraints that have the same latest variable, those
9384 * with the same coefficient for this latest variable (first in absolute value
9385 * and then in actual value) are grouped together.
9386 * This is useful for detecting pairs of constraints that can
9387 * be chained in their printed representation.
9389 * Finally, within a group, constraints are sorted according to
9390 * their coefficients (excluding the constant term).
9392 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9394 isl_int **c1 = (isl_int **) p1;
9395 isl_int **c2 = (isl_int **) p2;
9396 int l1, l2;
9397 unsigned size = *(unsigned *) arg;
9398 int cmp;
9400 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9401 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9403 if (l1 != l2)
9404 return l1 - l2;
9406 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9407 if (cmp != 0)
9408 return cmp;
9409 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9410 if (cmp != 0)
9411 return -cmp;
9413 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9416 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9417 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9418 * and 0 if the two constraints are the same (up to the constant term).
9420 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9421 isl_int *c1, isl_int *c2)
9423 unsigned total;
9425 if (!bmap)
9426 return -2;
9427 total = isl_basic_map_total_dim(bmap);
9428 return sort_constraint_cmp(&c1, &c2, &total);
9431 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9432 __isl_take isl_basic_map *bmap)
9434 unsigned total;
9436 if (!bmap)
9437 return NULL;
9438 if (bmap->n_ineq == 0)
9439 return bmap;
9440 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9441 return bmap;
9442 total = isl_basic_map_total_dim(bmap);
9443 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9444 &sort_constraint_cmp, &total) < 0)
9445 return isl_basic_map_free(bmap);
9446 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9447 return bmap;
9450 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9451 __isl_take isl_basic_set *bset)
9453 isl_basic_map *bmap = bset_to_bmap(bset);
9454 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9457 __isl_give isl_basic_map *isl_basic_map_normalize(
9458 __isl_take isl_basic_map *bmap)
9460 bmap = isl_basic_map_remove_redundancies(bmap);
9461 bmap = isl_basic_map_sort_constraints(bmap);
9462 return bmap;
9464 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9465 __isl_keep isl_basic_map *bmap2)
9467 int i, cmp;
9468 unsigned total;
9469 isl_space *space1, *space2;
9471 if (!bmap1 || !bmap2)
9472 return -1;
9474 if (bmap1 == bmap2)
9475 return 0;
9476 space1 = isl_basic_map_peek_space(bmap1);
9477 space2 = isl_basic_map_peek_space(bmap2);
9478 cmp = isl_space_cmp(space1, space2);
9479 if (cmp)
9480 return cmp;
9481 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9482 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9483 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9484 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9485 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9486 return 0;
9487 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9488 return 1;
9489 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9490 return -1;
9491 if (bmap1->n_eq != bmap2->n_eq)
9492 return bmap1->n_eq - bmap2->n_eq;
9493 if (bmap1->n_ineq != bmap2->n_ineq)
9494 return bmap1->n_ineq - bmap2->n_ineq;
9495 if (bmap1->n_div != bmap2->n_div)
9496 return bmap1->n_div - bmap2->n_div;
9497 total = isl_basic_map_total_dim(bmap1);
9498 for (i = 0; i < bmap1->n_eq; ++i) {
9499 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9500 if (cmp)
9501 return cmp;
9503 for (i = 0; i < bmap1->n_ineq; ++i) {
9504 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9505 if (cmp)
9506 return cmp;
9508 for (i = 0; i < bmap1->n_div; ++i) {
9509 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9510 if (cmp)
9511 return cmp;
9513 return 0;
9516 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9517 __isl_keep isl_basic_set *bset2)
9519 return isl_basic_map_plain_cmp(bset1, bset2);
9522 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9524 int i, cmp;
9526 if (set1 == set2)
9527 return 0;
9528 if (set1->n != set2->n)
9529 return set1->n - set2->n;
9531 for (i = 0; i < set1->n; ++i) {
9532 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9533 if (cmp)
9534 return cmp;
9537 return 0;
9540 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9541 __isl_keep isl_basic_map *bmap2)
9543 if (!bmap1 || !bmap2)
9544 return isl_bool_error;
9545 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9548 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9549 __isl_keep isl_basic_set *bset2)
9551 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9552 bset_to_bmap(bset2));
9555 static int qsort_bmap_cmp(const void *p1, const void *p2)
9557 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9558 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9560 return isl_basic_map_plain_cmp(bmap1, bmap2);
9563 /* Sort the basic maps of "map" and remove duplicate basic maps.
9565 * While removing basic maps, we make sure that the basic maps remain
9566 * sorted because isl_map_normalize expects the basic maps of the result
9567 * to be sorted.
9569 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9571 int i, j;
9573 map = isl_map_remove_empty_parts(map);
9574 if (!map)
9575 return NULL;
9576 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9577 for (i = map->n - 1; i >= 1; --i) {
9578 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9579 continue;
9580 isl_basic_map_free(map->p[i-1]);
9581 for (j = i; j < map->n; ++j)
9582 map->p[j - 1] = map->p[j];
9583 map->n--;
9586 return map;
9589 /* Remove obvious duplicates among the basic maps of "map".
9591 * Unlike isl_map_normalize, this function does not remove redundant
9592 * constraints and only removes duplicates that have exactly the same
9593 * constraints in the input. It does sort the constraints and
9594 * the basic maps to ease the detection of duplicates.
9596 * If "map" has already been normalized or if the basic maps are
9597 * disjoint, then there can be no duplicates.
9599 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9601 int i;
9602 isl_basic_map *bmap;
9604 if (!map)
9605 return NULL;
9606 if (map->n <= 1)
9607 return map;
9608 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9609 return map;
9610 for (i = 0; i < map->n; ++i) {
9611 bmap = isl_basic_map_copy(map->p[i]);
9612 bmap = isl_basic_map_sort_constraints(bmap);
9613 if (!bmap)
9614 return isl_map_free(map);
9615 isl_basic_map_free(map->p[i]);
9616 map->p[i] = bmap;
9619 map = sort_and_remove_duplicates(map);
9620 return map;
9623 /* We normalize in place, but if anything goes wrong we need
9624 * to return NULL, so we need to make sure we don't change the
9625 * meaning of any possible other copies of map.
9627 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9629 int i;
9630 struct isl_basic_map *bmap;
9632 if (!map)
9633 return NULL;
9634 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9635 return map;
9636 for (i = 0; i < map->n; ++i) {
9637 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9638 if (!bmap)
9639 goto error;
9640 isl_basic_map_free(map->p[i]);
9641 map->p[i] = bmap;
9644 map = sort_and_remove_duplicates(map);
9645 if (map)
9646 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9647 return map;
9648 error:
9649 isl_map_free(map);
9650 return NULL;
9653 struct isl_set *isl_set_normalize(struct isl_set *set)
9655 return set_from_map(isl_map_normalize(set_to_map(set)));
9658 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9659 __isl_keep isl_map *map2)
9661 int i;
9662 isl_bool equal;
9664 if (!map1 || !map2)
9665 return isl_bool_error;
9667 if (map1 == map2)
9668 return isl_bool_true;
9669 if (!isl_space_is_equal(map1->dim, map2->dim))
9670 return isl_bool_false;
9672 map1 = isl_map_copy(map1);
9673 map2 = isl_map_copy(map2);
9674 map1 = isl_map_normalize(map1);
9675 map2 = isl_map_normalize(map2);
9676 if (!map1 || !map2)
9677 goto error;
9678 equal = map1->n == map2->n;
9679 for (i = 0; equal && i < map1->n; ++i) {
9680 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9681 if (equal < 0)
9682 goto error;
9684 isl_map_free(map1);
9685 isl_map_free(map2);
9686 return equal;
9687 error:
9688 isl_map_free(map1);
9689 isl_map_free(map2);
9690 return isl_bool_error;
9693 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9694 __isl_keep isl_set *set2)
9696 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9699 /* Return the basic maps in "map" as a list.
9701 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9702 __isl_keep isl_map *map)
9704 int i;
9705 isl_ctx *ctx;
9706 isl_basic_map_list *list;
9708 if (!map)
9709 return NULL;
9710 ctx = isl_map_get_ctx(map);
9711 list = isl_basic_map_list_alloc(ctx, map->n);
9713 for (i = 0; i < map->n; ++i) {
9714 isl_basic_map *bmap;
9716 bmap = isl_basic_map_copy(map->p[i]);
9717 list = isl_basic_map_list_add(list, bmap);
9720 return list;
9723 /* Return the intersection of the elements in the non-empty list "list".
9724 * All elements are assumed to live in the same space.
9726 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9727 __isl_take isl_basic_map_list *list)
9729 int i, n;
9730 isl_basic_map *bmap;
9732 if (!list)
9733 return NULL;
9734 n = isl_basic_map_list_n_basic_map(list);
9735 if (n < 1)
9736 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9737 "expecting non-empty list", goto error);
9739 bmap = isl_basic_map_list_get_basic_map(list, 0);
9740 for (i = 1; i < n; ++i) {
9741 isl_basic_map *bmap_i;
9743 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9744 bmap = isl_basic_map_intersect(bmap, bmap_i);
9747 isl_basic_map_list_free(list);
9748 return bmap;
9749 error:
9750 isl_basic_map_list_free(list);
9751 return NULL;
9754 /* Return the intersection of the elements in the non-empty list "list".
9755 * All elements are assumed to live in the same space.
9757 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9758 __isl_take isl_basic_set_list *list)
9760 return isl_basic_map_list_intersect(list);
9763 /* Return the union of the elements of "list".
9764 * The list is required to have at least one element.
9766 __isl_give isl_set *isl_basic_set_list_union(
9767 __isl_take isl_basic_set_list *list)
9769 int i, n;
9770 isl_space *space;
9771 isl_basic_set *bset;
9772 isl_set *set;
9774 if (!list)
9775 return NULL;
9776 n = isl_basic_set_list_n_basic_set(list);
9777 if (n < 1)
9778 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9779 "expecting non-empty list", goto error);
9781 bset = isl_basic_set_list_get_basic_set(list, 0);
9782 space = isl_basic_set_get_space(bset);
9783 isl_basic_set_free(bset);
9785 set = isl_set_alloc_space(space, n, 0);
9786 for (i = 0; i < n; ++i) {
9787 bset = isl_basic_set_list_get_basic_set(list, i);
9788 set = isl_set_add_basic_set(set, bset);
9791 isl_basic_set_list_free(list);
9792 return set;
9793 error:
9794 isl_basic_set_list_free(list);
9795 return NULL;
9798 /* Return the union of the elements in the non-empty list "list".
9799 * All elements are assumed to live in the same space.
9801 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9803 int i, n;
9804 isl_set *set;
9806 if (!list)
9807 return NULL;
9808 n = isl_set_list_n_set(list);
9809 if (n < 1)
9810 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9811 "expecting non-empty list", goto error);
9813 set = isl_set_list_get_set(list, 0);
9814 for (i = 1; i < n; ++i) {
9815 isl_set *set_i;
9817 set_i = isl_set_list_get_set(list, i);
9818 set = isl_set_union(set, set_i);
9821 isl_set_list_free(list);
9822 return set;
9823 error:
9824 isl_set_list_free(list);
9825 return NULL;
9828 __isl_give isl_basic_map *isl_basic_map_product(
9829 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9831 isl_space *space_result = NULL;
9832 struct isl_basic_map *bmap;
9833 unsigned in1, in2, out1, out2, nparam, total, pos;
9834 struct isl_dim_map *dim_map1, *dim_map2;
9836 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9837 goto error;
9838 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9839 isl_space_copy(bmap2->dim));
9841 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9842 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9843 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9844 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9845 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9847 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9848 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9849 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9850 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9851 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9852 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9853 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9854 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9855 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9856 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9857 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9859 bmap = isl_basic_map_alloc_space(space_result,
9860 bmap1->n_div + bmap2->n_div,
9861 bmap1->n_eq + bmap2->n_eq,
9862 bmap1->n_ineq + bmap2->n_ineq);
9863 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9864 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9865 bmap = isl_basic_map_simplify(bmap);
9866 return isl_basic_map_finalize(bmap);
9867 error:
9868 isl_basic_map_free(bmap1);
9869 isl_basic_map_free(bmap2);
9870 return NULL;
9873 __isl_give isl_basic_map *isl_basic_map_flat_product(
9874 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9876 isl_basic_map *prod;
9878 prod = isl_basic_map_product(bmap1, bmap2);
9879 prod = isl_basic_map_flatten(prod);
9880 return prod;
9883 __isl_give isl_basic_set *isl_basic_set_flat_product(
9884 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9886 return isl_basic_map_flat_range_product(bset1, bset2);
9889 __isl_give isl_basic_map *isl_basic_map_domain_product(
9890 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9892 isl_space *space_result = NULL;
9893 isl_basic_map *bmap;
9894 unsigned in1, in2, out, nparam, total, pos;
9895 struct isl_dim_map *dim_map1, *dim_map2;
9897 if (!bmap1 || !bmap2)
9898 goto error;
9900 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9901 isl_space_copy(bmap2->dim));
9903 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9904 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9905 out = isl_basic_map_dim(bmap1, isl_dim_out);
9906 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9908 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9909 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9910 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9911 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9912 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9913 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9914 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9915 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9916 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9917 isl_dim_map_div(dim_map1, bmap1, pos += out);
9918 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9920 bmap = isl_basic_map_alloc_space(space_result,
9921 bmap1->n_div + bmap2->n_div,
9922 bmap1->n_eq + bmap2->n_eq,
9923 bmap1->n_ineq + bmap2->n_ineq);
9924 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9925 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9926 bmap = isl_basic_map_simplify(bmap);
9927 return isl_basic_map_finalize(bmap);
9928 error:
9929 isl_basic_map_free(bmap1);
9930 isl_basic_map_free(bmap2);
9931 return NULL;
9934 __isl_give isl_basic_map *isl_basic_map_range_product(
9935 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9937 isl_bool rational;
9938 isl_space *space_result = NULL;
9939 isl_basic_map *bmap;
9940 unsigned in, out1, out2, nparam, total, pos;
9941 struct isl_dim_map *dim_map1, *dim_map2;
9943 rational = isl_basic_map_is_rational(bmap1);
9944 if (rational >= 0 && rational)
9945 rational = isl_basic_map_is_rational(bmap2);
9946 if (!bmap1 || !bmap2 || rational < 0)
9947 goto error;
9949 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9950 goto error;
9952 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9953 isl_space_copy(bmap2->dim));
9955 in = isl_basic_map_dim(bmap1, isl_dim_in);
9956 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9957 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9958 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9960 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9961 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9962 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9963 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9964 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9965 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9966 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9967 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9968 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9969 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9970 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9972 bmap = isl_basic_map_alloc_space(space_result,
9973 bmap1->n_div + bmap2->n_div,
9974 bmap1->n_eq + bmap2->n_eq,
9975 bmap1->n_ineq + bmap2->n_ineq);
9976 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9977 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9978 if (rational)
9979 bmap = isl_basic_map_set_rational(bmap);
9980 bmap = isl_basic_map_simplify(bmap);
9981 return isl_basic_map_finalize(bmap);
9982 error:
9983 isl_basic_map_free(bmap1);
9984 isl_basic_map_free(bmap2);
9985 return NULL;
9988 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9989 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9991 isl_basic_map *prod;
9993 prod = isl_basic_map_range_product(bmap1, bmap2);
9994 prod = isl_basic_map_flatten_range(prod);
9995 return prod;
9998 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9999 * and collect the results.
10000 * The result live in the space obtained by calling "space_product"
10001 * on the spaces of "map1" and "map2".
10002 * If "remove_duplicates" is set then the result may contain duplicates
10003 * (even if the inputs do not) and so we try and remove the obvious
10004 * duplicates.
10006 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10007 __isl_take isl_map *map2,
10008 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10009 __isl_take isl_space *right),
10010 __isl_give isl_basic_map *(*basic_map_product)(
10011 __isl_take isl_basic_map *left,
10012 __isl_take isl_basic_map *right),
10013 int remove_duplicates)
10015 unsigned flags = 0;
10016 struct isl_map *result;
10017 int i, j;
10018 isl_bool m;
10020 m = isl_map_has_equal_params(map1, map2);
10021 if (m < 0)
10022 goto error;
10023 if (!m)
10024 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10025 "parameters don't match", goto error);
10027 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10028 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10029 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10031 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10032 isl_space_copy(map2->dim)),
10033 map1->n * map2->n, flags);
10034 if (!result)
10035 goto error;
10036 for (i = 0; i < map1->n; ++i)
10037 for (j = 0; j < map2->n; ++j) {
10038 struct isl_basic_map *part;
10039 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10040 isl_basic_map_copy(map2->p[j]));
10041 if (isl_basic_map_is_empty(part))
10042 isl_basic_map_free(part);
10043 else
10044 result = isl_map_add_basic_map(result, part);
10045 if (!result)
10046 goto error;
10048 if (remove_duplicates)
10049 result = isl_map_remove_obvious_duplicates(result);
10050 isl_map_free(map1);
10051 isl_map_free(map2);
10052 return result;
10053 error:
10054 isl_map_free(map1);
10055 isl_map_free(map2);
10056 return NULL;
10059 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10061 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10062 __isl_take isl_map *map2)
10064 return map_product(map1, map2, &isl_space_product,
10065 &isl_basic_map_product, 0);
10068 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10069 __isl_take isl_map *map2)
10071 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10074 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10076 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10077 __isl_take isl_map *map2)
10079 isl_map *prod;
10081 prod = isl_map_product(map1, map2);
10082 prod = isl_map_flatten(prod);
10083 return prod;
10086 /* Given two set A and B, construct its Cartesian product A x B.
10088 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10090 return isl_map_range_product(set1, set2);
10093 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10094 __isl_take isl_set *set2)
10096 return isl_map_flat_range_product(set1, set2);
10099 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10101 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10102 __isl_take isl_map *map2)
10104 return map_product(map1, map2, &isl_space_domain_product,
10105 &isl_basic_map_domain_product, 1);
10108 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10110 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10111 __isl_take isl_map *map2)
10113 return map_product(map1, map2, &isl_space_range_product,
10114 &isl_basic_map_range_product, 1);
10117 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10118 __isl_take isl_map *map2)
10120 return isl_map_align_params_map_map_and(map1, map2,
10121 &map_domain_product_aligned);
10124 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10125 __isl_take isl_map *map2)
10127 return isl_map_align_params_map_map_and(map1, map2,
10128 &map_range_product_aligned);
10131 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10133 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10135 isl_space *space;
10136 int total1, keep1, total2, keep2;
10138 if (!map)
10139 return NULL;
10140 if (!isl_space_domain_is_wrapping(map->dim) ||
10141 !isl_space_range_is_wrapping(map->dim))
10142 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10143 "not a product", return isl_map_free(map));
10145 space = isl_map_get_space(map);
10146 total1 = isl_space_dim(space, isl_dim_in);
10147 total2 = isl_space_dim(space, isl_dim_out);
10148 space = isl_space_factor_domain(space);
10149 keep1 = isl_space_dim(space, isl_dim_in);
10150 keep2 = isl_space_dim(space, isl_dim_out);
10151 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10152 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10153 map = isl_map_reset_space(map, space);
10155 return map;
10158 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10160 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10162 isl_space *space;
10163 int total1, keep1, total2, keep2;
10165 if (!map)
10166 return NULL;
10167 if (!isl_space_domain_is_wrapping(map->dim) ||
10168 !isl_space_range_is_wrapping(map->dim))
10169 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10170 "not a product", return isl_map_free(map));
10172 space = isl_map_get_space(map);
10173 total1 = isl_space_dim(space, isl_dim_in);
10174 total2 = isl_space_dim(space, isl_dim_out);
10175 space = isl_space_factor_range(space);
10176 keep1 = isl_space_dim(space, isl_dim_in);
10177 keep2 = isl_space_dim(space, isl_dim_out);
10178 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10179 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10180 map = isl_map_reset_space(map, space);
10182 return map;
10185 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10187 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10189 isl_space *space;
10190 int total, keep;
10192 if (!map)
10193 return NULL;
10194 if (!isl_space_domain_is_wrapping(map->dim))
10195 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10196 "domain is not a product", return isl_map_free(map));
10198 space = isl_map_get_space(map);
10199 total = isl_space_dim(space, isl_dim_in);
10200 space = isl_space_domain_factor_domain(space);
10201 keep = isl_space_dim(space, isl_dim_in);
10202 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10203 map = isl_map_reset_space(map, space);
10205 return map;
10208 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10210 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10212 isl_space *space;
10213 int total, keep;
10215 if (!map)
10216 return NULL;
10217 if (!isl_space_domain_is_wrapping(map->dim))
10218 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10219 "domain is not a product", return isl_map_free(map));
10221 space = isl_map_get_space(map);
10222 total = isl_space_dim(space, isl_dim_in);
10223 space = isl_space_domain_factor_range(space);
10224 keep = isl_space_dim(space, isl_dim_in);
10225 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10226 map = isl_map_reset_space(map, space);
10228 return map;
10231 /* Given a map A -> [B -> C], extract the map A -> B.
10233 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10235 isl_space *space;
10236 int total, keep;
10238 if (!map)
10239 return NULL;
10240 if (!isl_space_range_is_wrapping(map->dim))
10241 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10242 "range is not a product", return isl_map_free(map));
10244 space = isl_map_get_space(map);
10245 total = isl_space_dim(space, isl_dim_out);
10246 space = isl_space_range_factor_domain(space);
10247 keep = isl_space_dim(space, isl_dim_out);
10248 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10249 map = isl_map_reset_space(map, space);
10251 return map;
10254 /* Given a map A -> [B -> C], extract the map A -> C.
10256 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10258 isl_space *space;
10259 int total, keep;
10261 if (!map)
10262 return NULL;
10263 if (!isl_space_range_is_wrapping(map->dim))
10264 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10265 "range is not a product", return isl_map_free(map));
10267 space = isl_map_get_space(map);
10268 total = isl_space_dim(space, isl_dim_out);
10269 space = isl_space_range_factor_range(space);
10270 keep = isl_space_dim(space, isl_dim_out);
10271 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10272 map = isl_map_reset_space(map, space);
10274 return map;
10277 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10279 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10280 __isl_take isl_map *map2)
10282 isl_map *prod;
10284 prod = isl_map_domain_product(map1, map2);
10285 prod = isl_map_flatten_domain(prod);
10286 return prod;
10289 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10291 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10292 __isl_take isl_map *map2)
10294 isl_map *prod;
10296 prod = isl_map_range_product(map1, map2);
10297 prod = isl_map_flatten_range(prod);
10298 return prod;
10301 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10303 int i;
10304 uint32_t hash = isl_hash_init();
10305 unsigned total;
10307 if (!bmap)
10308 return 0;
10309 bmap = isl_basic_map_copy(bmap);
10310 bmap = isl_basic_map_normalize(bmap);
10311 if (!bmap)
10312 return 0;
10313 total = isl_basic_map_total_dim(bmap);
10314 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10315 for (i = 0; i < bmap->n_eq; ++i) {
10316 uint32_t c_hash;
10317 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10318 isl_hash_hash(hash, c_hash);
10320 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10321 for (i = 0; i < bmap->n_ineq; ++i) {
10322 uint32_t c_hash;
10323 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10324 isl_hash_hash(hash, c_hash);
10326 isl_hash_byte(hash, bmap->n_div & 0xFF);
10327 for (i = 0; i < bmap->n_div; ++i) {
10328 uint32_t c_hash;
10329 if (isl_int_is_zero(bmap->div[i][0]))
10330 continue;
10331 isl_hash_byte(hash, i & 0xFF);
10332 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10333 isl_hash_hash(hash, c_hash);
10335 isl_basic_map_free(bmap);
10336 return hash;
10339 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10341 return isl_basic_map_get_hash(bset_to_bmap(bset));
10344 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10346 int i;
10347 uint32_t hash;
10349 if (!map)
10350 return 0;
10351 map = isl_map_copy(map);
10352 map = isl_map_normalize(map);
10353 if (!map)
10354 return 0;
10356 hash = isl_hash_init();
10357 for (i = 0; i < map->n; ++i) {
10358 uint32_t bmap_hash;
10359 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10360 isl_hash_hash(hash, bmap_hash);
10363 isl_map_free(map);
10365 return hash;
10368 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10370 return isl_map_get_hash(set_to_map(set));
10373 /* Return the number of basic maps in the (current) representation of "map".
10375 int isl_map_n_basic_map(__isl_keep isl_map *map)
10377 return map ? map->n : 0;
10380 int isl_set_n_basic_set(__isl_keep isl_set *set)
10382 return set ? set->n : 0;
10385 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10386 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10388 int i;
10390 if (!map)
10391 return isl_stat_error;
10393 for (i = 0; i < map->n; ++i)
10394 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10395 return isl_stat_error;
10397 return isl_stat_ok;
10400 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10401 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10403 int i;
10405 if (!set)
10406 return isl_stat_error;
10408 for (i = 0; i < set->n; ++i)
10409 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10410 return isl_stat_error;
10412 return isl_stat_ok;
10415 /* Return a list of basic sets, the union of which is equal to "set".
10417 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10418 __isl_keep isl_set *set)
10420 int i;
10421 isl_basic_set_list *list;
10423 if (!set)
10424 return NULL;
10426 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10427 for (i = 0; i < set->n; ++i) {
10428 isl_basic_set *bset;
10430 bset = isl_basic_set_copy(set->p[i]);
10431 list = isl_basic_set_list_add(list, bset);
10434 return list;
10437 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10439 isl_space *space;
10441 if (!bset)
10442 return NULL;
10444 bset = isl_basic_set_cow(bset);
10445 if (!bset)
10446 return NULL;
10448 space = isl_basic_set_get_space(bset);
10449 space = isl_space_lift(space, bset->n_div);
10450 if (!space)
10451 goto error;
10452 isl_space_free(bset->dim);
10453 bset->dim = space;
10454 bset->extra -= bset->n_div;
10455 bset->n_div = 0;
10457 bset = isl_basic_set_finalize(bset);
10459 return bset;
10460 error:
10461 isl_basic_set_free(bset);
10462 return NULL;
10465 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10467 int i;
10468 isl_space *space;
10469 unsigned n_div;
10471 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10473 if (!set)
10474 return NULL;
10476 set = isl_set_cow(set);
10477 if (!set)
10478 return NULL;
10480 n_div = set->p[0]->n_div;
10481 space = isl_set_get_space(set);
10482 space = isl_space_lift(space, n_div);
10483 if (!space)
10484 goto error;
10485 isl_space_free(set->dim);
10486 set->dim = space;
10488 for (i = 0; i < set->n; ++i) {
10489 set->p[i] = isl_basic_set_lift(set->p[i]);
10490 if (!set->p[i])
10491 goto error;
10494 return set;
10495 error:
10496 isl_set_free(set);
10497 return NULL;
10500 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10502 unsigned dim;
10503 int size = 0;
10505 if (!bset)
10506 return -1;
10508 dim = isl_basic_set_total_dim(bset);
10509 size += bset->n_eq * (1 + dim);
10510 size += bset->n_ineq * (1 + dim);
10511 size += bset->n_div * (2 + dim);
10513 return size;
10516 int isl_set_size(__isl_keep isl_set *set)
10518 int i;
10519 int size = 0;
10521 if (!set)
10522 return -1;
10524 for (i = 0; i < set->n; ++i)
10525 size += isl_basic_set_size(set->p[i]);
10527 return size;
10530 /* Check if there is any lower bound (if lower == 0) and/or upper
10531 * bound (if upper == 0) on the specified dim.
10533 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10534 enum isl_dim_type type, unsigned pos, int lower, int upper)
10536 int i;
10538 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10539 return isl_bool_error;
10541 pos += isl_basic_map_offset(bmap, type);
10543 for (i = 0; i < bmap->n_div; ++i) {
10544 if (isl_int_is_zero(bmap->div[i][0]))
10545 continue;
10546 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10547 return isl_bool_true;
10550 for (i = 0; i < bmap->n_eq; ++i)
10551 if (!isl_int_is_zero(bmap->eq[i][pos]))
10552 return isl_bool_true;
10554 for (i = 0; i < bmap->n_ineq; ++i) {
10555 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10556 if (sgn > 0)
10557 lower = 1;
10558 if (sgn < 0)
10559 upper = 1;
10562 return lower && upper;
10565 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10566 enum isl_dim_type type, unsigned pos)
10568 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10571 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10572 enum isl_dim_type type, unsigned pos)
10574 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10577 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10578 enum isl_dim_type type, unsigned pos)
10580 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10583 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10584 enum isl_dim_type type, unsigned pos)
10586 int i;
10588 if (!map)
10589 return isl_bool_error;
10591 for (i = 0; i < map->n; ++i) {
10592 isl_bool bounded;
10593 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10594 if (bounded < 0 || !bounded)
10595 return bounded;
10598 return isl_bool_true;
10601 /* Return true if the specified dim is involved in both an upper bound
10602 * and a lower bound.
10604 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10605 enum isl_dim_type type, unsigned pos)
10607 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10610 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10612 static isl_bool has_any_bound(__isl_keep isl_map *map,
10613 enum isl_dim_type type, unsigned pos,
10614 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10615 enum isl_dim_type type, unsigned pos))
10617 int i;
10619 if (!map)
10620 return isl_bool_error;
10622 for (i = 0; i < map->n; ++i) {
10623 isl_bool bounded;
10624 bounded = fn(map->p[i], type, pos);
10625 if (bounded < 0 || bounded)
10626 return bounded;
10629 return isl_bool_false;
10632 /* Return 1 if the specified dim is involved in any lower bound.
10634 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10635 enum isl_dim_type type, unsigned pos)
10637 return has_any_bound(set, type, pos,
10638 &isl_basic_map_dim_has_lower_bound);
10641 /* Return 1 if the specified dim is involved in any upper bound.
10643 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10644 enum isl_dim_type type, unsigned pos)
10646 return has_any_bound(set, type, pos,
10647 &isl_basic_map_dim_has_upper_bound);
10650 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10652 static isl_bool has_bound(__isl_keep isl_map *map,
10653 enum isl_dim_type type, unsigned pos,
10654 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10655 enum isl_dim_type type, unsigned pos))
10657 int i;
10659 if (!map)
10660 return isl_bool_error;
10662 for (i = 0; i < map->n; ++i) {
10663 isl_bool bounded;
10664 bounded = fn(map->p[i], type, pos);
10665 if (bounded < 0 || !bounded)
10666 return bounded;
10669 return isl_bool_true;
10672 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10674 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10675 enum isl_dim_type type, unsigned pos)
10677 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10680 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10682 isl_bool isl_set_dim_has_upper_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_upper_bound);
10688 /* For each of the "n" variables starting at "first", determine
10689 * the sign of the variable and put the results in the first "n"
10690 * elements of the array "signs".
10691 * Sign
10692 * 1 means that the variable is non-negative
10693 * -1 means that the variable is non-positive
10694 * 0 means the variable attains both positive and negative values.
10696 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10697 unsigned first, unsigned n, int *signs)
10699 isl_vec *bound = NULL;
10700 struct isl_tab *tab = NULL;
10701 struct isl_tab_undo *snap;
10702 int i;
10704 if (!bset || !signs)
10705 return isl_stat_error;
10707 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10708 tab = isl_tab_from_basic_set(bset, 0);
10709 if (!bound || !tab)
10710 goto error;
10712 isl_seq_clr(bound->el, bound->size);
10713 isl_int_set_si(bound->el[0], -1);
10715 snap = isl_tab_snap(tab);
10716 for (i = 0; i < n; ++i) {
10717 int empty;
10719 isl_int_set_si(bound->el[1 + first + i], -1);
10720 if (isl_tab_add_ineq(tab, bound->el) < 0)
10721 goto error;
10722 empty = tab->empty;
10723 isl_int_set_si(bound->el[1 + first + i], 0);
10724 if (isl_tab_rollback(tab, snap) < 0)
10725 goto error;
10727 if (empty) {
10728 signs[i] = 1;
10729 continue;
10732 isl_int_set_si(bound->el[1 + first + i], 1);
10733 if (isl_tab_add_ineq(tab, bound->el) < 0)
10734 goto error;
10735 empty = tab->empty;
10736 isl_int_set_si(bound->el[1 + first + i], 0);
10737 if (isl_tab_rollback(tab, snap) < 0)
10738 goto error;
10740 signs[i] = empty ? -1 : 0;
10743 isl_tab_free(tab);
10744 isl_vec_free(bound);
10745 return isl_stat_ok;
10746 error:
10747 isl_tab_free(tab);
10748 isl_vec_free(bound);
10749 return isl_stat_error;
10752 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10753 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10755 if (!bset || !signs)
10756 return isl_stat_error;
10757 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10758 return isl_stat_error;
10760 first += pos(bset->dim, type) - 1;
10761 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10764 /* Is it possible for the integer division "div" to depend (possibly
10765 * indirectly) on any output dimensions?
10767 * If the div is undefined, then we conservatively assume that it
10768 * may depend on them.
10769 * Otherwise, we check if it actually depends on them or on any integer
10770 * divisions that may depend on them.
10772 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10774 int i;
10775 unsigned n_out, o_out;
10776 unsigned n_div, o_div;
10778 if (isl_int_is_zero(bmap->div[div][0]))
10779 return isl_bool_true;
10781 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10782 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10784 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10785 return isl_bool_true;
10787 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10788 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10790 for (i = 0; i < n_div; ++i) {
10791 isl_bool may_involve;
10793 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10794 continue;
10795 may_involve = div_may_involve_output(bmap, i);
10796 if (may_involve < 0 || may_involve)
10797 return may_involve;
10800 return isl_bool_false;
10803 /* Return the first integer division of "bmap" in the range
10804 * [first, first + n[ that may depend on any output dimensions and
10805 * that has a non-zero coefficient in "c" (where the first coefficient
10806 * in "c" corresponds to integer division "first").
10808 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10809 isl_int *c, int first, int n)
10811 int k;
10813 if (!bmap)
10814 return -1;
10816 for (k = first; k < first + n; ++k) {
10817 isl_bool may_involve;
10819 if (isl_int_is_zero(c[k]))
10820 continue;
10821 may_involve = div_may_involve_output(bmap, k);
10822 if (may_involve < 0)
10823 return -1;
10824 if (may_involve)
10825 return k;
10828 return first + n;
10831 /* Look for a pair of inequality constraints in "bmap" of the form
10833 * -l + i >= 0 or i >= l
10834 * and
10835 * n + l - i >= 0 or i <= l + n
10837 * with n < "m" and i the output dimension at position "pos".
10838 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10839 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10840 * and earlier output dimensions, as well as integer divisions that do
10841 * not involve any of the output dimensions.
10843 * Return the index of the first inequality constraint or bmap->n_ineq
10844 * if no such pair can be found.
10846 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10847 int pos, isl_int m)
10849 int i, j;
10850 isl_ctx *ctx;
10851 unsigned total;
10852 unsigned n_div, o_div;
10853 unsigned n_out, o_out;
10854 int less;
10856 if (!bmap)
10857 return -1;
10859 ctx = isl_basic_map_get_ctx(bmap);
10860 total = isl_basic_map_total_dim(bmap);
10861 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10862 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10863 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10864 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10865 for (i = 0; i < bmap->n_ineq; ++i) {
10866 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10867 continue;
10868 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10869 n_out - (pos + 1)) != -1)
10870 continue;
10871 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10872 0, n_div) < n_div)
10873 continue;
10874 for (j = i + 1; j < bmap->n_ineq; ++j) {
10875 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10876 ctx->one))
10877 continue;
10878 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10879 bmap->ineq[j] + 1, total))
10880 continue;
10881 break;
10883 if (j >= bmap->n_ineq)
10884 continue;
10885 isl_int_add(bmap->ineq[i][0],
10886 bmap->ineq[i][0], bmap->ineq[j][0]);
10887 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10888 isl_int_sub(bmap->ineq[i][0],
10889 bmap->ineq[i][0], bmap->ineq[j][0]);
10890 if (!less)
10891 continue;
10892 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10893 return i;
10894 else
10895 return j;
10898 return bmap->n_ineq;
10901 /* Return the index of the equality of "bmap" that defines
10902 * the output dimension "pos" in terms of earlier dimensions.
10903 * The equality may also involve integer divisions, as long
10904 * as those integer divisions are defined in terms of
10905 * parameters or input dimensions.
10906 * In this case, *div is set to the number of integer divisions and
10907 * *ineq is set to the number of inequality constraints (provided
10908 * div and ineq are not NULL).
10910 * The equality may also involve a single integer division involving
10911 * the output dimensions (typically only output dimension "pos") as
10912 * long as the coefficient of output dimension "pos" is 1 or -1 and
10913 * there is a pair of constraints i >= l and i <= l + n, with i referring
10914 * to output dimension "pos", l an expression involving only earlier
10915 * dimensions and n smaller than the coefficient of the integer division
10916 * in the equality. In this case, the output dimension can be defined
10917 * in terms of a modulo expression that does not involve the integer division.
10918 * *div is then set to this single integer division and
10919 * *ineq is set to the index of constraint i >= l.
10921 * Return bmap->n_eq if there is no such equality.
10922 * Return -1 on error.
10924 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10925 int pos, int *div, int *ineq)
10927 int j, k, l;
10928 unsigned n_out, o_out;
10929 unsigned n_div, o_div;
10931 if (!bmap)
10932 return -1;
10934 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10935 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10936 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10937 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10939 if (ineq)
10940 *ineq = bmap->n_ineq;
10941 if (div)
10942 *div = n_div;
10943 for (j = 0; j < bmap->n_eq; ++j) {
10944 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10945 continue;
10946 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10947 n_out - (pos + 1)) != -1)
10948 continue;
10949 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10950 0, n_div);
10951 if (k >= n_div)
10952 return j;
10953 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10954 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10955 continue;
10956 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10957 k + 1, n_div - (k+1)) < n_div)
10958 continue;
10959 l = find_modulo_constraint_pair(bmap, pos,
10960 bmap->eq[j][o_div + k]);
10961 if (l < 0)
10962 return -1;
10963 if (l >= bmap->n_ineq)
10964 continue;
10965 if (div)
10966 *div = k;
10967 if (ineq)
10968 *ineq = l;
10969 return j;
10972 return bmap->n_eq;
10975 /* Check if the given basic map is obviously single-valued.
10976 * In particular, for each output dimension, check that there is
10977 * an equality that defines the output dimension in terms of
10978 * earlier dimensions.
10980 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10982 int i;
10983 unsigned n_out;
10985 if (!bmap)
10986 return isl_bool_error;
10988 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10990 for (i = 0; i < n_out; ++i) {
10991 int eq;
10993 eq = isl_basic_map_output_defining_equality(bmap, i,
10994 NULL, NULL);
10995 if (eq < 0)
10996 return isl_bool_error;
10997 if (eq >= bmap->n_eq)
10998 return isl_bool_false;
11001 return isl_bool_true;
11004 /* Check if the given basic map is single-valued.
11005 * We simply compute
11007 * M \circ M^-1
11009 * and check if the result is a subset of the identity mapping.
11011 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11013 isl_space *space;
11014 isl_basic_map *test;
11015 isl_basic_map *id;
11016 isl_bool sv;
11018 sv = isl_basic_map_plain_is_single_valued(bmap);
11019 if (sv < 0 || sv)
11020 return sv;
11022 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11023 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11025 space = isl_basic_map_get_space(bmap);
11026 space = isl_space_map_from_set(isl_space_range(space));
11027 id = isl_basic_map_identity(space);
11029 sv = isl_basic_map_is_subset(test, id);
11031 isl_basic_map_free(test);
11032 isl_basic_map_free(id);
11034 return sv;
11037 /* Check if the given map is obviously single-valued.
11039 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11041 if (!map)
11042 return isl_bool_error;
11043 if (map->n == 0)
11044 return isl_bool_true;
11045 if (map->n >= 2)
11046 return isl_bool_false;
11048 return isl_basic_map_plain_is_single_valued(map->p[0]);
11051 /* Check if the given map is single-valued.
11052 * We simply compute
11054 * M \circ M^-1
11056 * and check if the result is a subset of the identity mapping.
11058 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11060 isl_space *dim;
11061 isl_map *test;
11062 isl_map *id;
11063 isl_bool sv;
11065 sv = isl_map_plain_is_single_valued(map);
11066 if (sv < 0 || sv)
11067 return sv;
11069 test = isl_map_reverse(isl_map_copy(map));
11070 test = isl_map_apply_range(test, isl_map_copy(map));
11072 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11073 id = isl_map_identity(dim);
11075 sv = isl_map_is_subset(test, id);
11077 isl_map_free(test);
11078 isl_map_free(id);
11080 return sv;
11083 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11085 isl_bool in;
11087 map = isl_map_copy(map);
11088 map = isl_map_reverse(map);
11089 in = isl_map_is_single_valued(map);
11090 isl_map_free(map);
11092 return in;
11095 /* Check if the given map is obviously injective.
11097 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11099 isl_bool in;
11101 map = isl_map_copy(map);
11102 map = isl_map_reverse(map);
11103 in = isl_map_plain_is_single_valued(map);
11104 isl_map_free(map);
11106 return in;
11109 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11111 isl_bool sv;
11113 sv = isl_map_is_single_valued(map);
11114 if (sv < 0 || !sv)
11115 return sv;
11117 return isl_map_is_injective(map);
11120 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11122 return isl_map_is_single_valued(set_to_map(set));
11125 /* Does "map" only map elements to themselves?
11127 * If the domain and range spaces are different, then "map"
11128 * is considered not to be an identity relation, even if it is empty.
11129 * Otherwise, construct the maximal identity relation and
11130 * check whether "map" is a subset of this relation.
11132 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11134 isl_space *space;
11135 isl_map *id;
11136 isl_bool equal, is_identity;
11138 space = isl_map_get_space(map);
11139 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11140 isl_space_free(space);
11141 if (equal < 0 || !equal)
11142 return equal;
11144 id = isl_map_identity(isl_map_get_space(map));
11145 is_identity = isl_map_is_subset(map, id);
11146 isl_map_free(id);
11148 return is_identity;
11151 int isl_map_is_translation(__isl_keep isl_map *map)
11153 int ok;
11154 isl_set *delta;
11156 delta = isl_map_deltas(isl_map_copy(map));
11157 ok = isl_set_is_singleton(delta);
11158 isl_set_free(delta);
11160 return ok;
11163 static int unique(isl_int *p, unsigned pos, unsigned len)
11165 if (isl_seq_first_non_zero(p, pos) != -1)
11166 return 0;
11167 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11168 return 0;
11169 return 1;
11172 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11174 int i, j;
11175 unsigned nvar;
11176 unsigned ovar;
11178 if (!bset)
11179 return isl_bool_error;
11181 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11182 return isl_bool_false;
11184 nvar = isl_basic_set_dim(bset, isl_dim_set);
11185 ovar = isl_space_offset(bset->dim, isl_dim_set);
11186 for (j = 0; j < nvar; ++j) {
11187 int lower = 0, upper = 0;
11188 for (i = 0; i < bset->n_eq; ++i) {
11189 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11190 continue;
11191 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11192 return isl_bool_false;
11193 break;
11195 if (i < bset->n_eq)
11196 continue;
11197 for (i = 0; i < bset->n_ineq; ++i) {
11198 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11199 continue;
11200 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11201 return isl_bool_false;
11202 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11203 lower = 1;
11204 else
11205 upper = 1;
11207 if (!lower || !upper)
11208 return isl_bool_false;
11211 return isl_bool_true;
11214 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11216 if (!set)
11217 return isl_bool_error;
11218 if (set->n != 1)
11219 return isl_bool_false;
11221 return isl_basic_set_is_box(set->p[0]);
11224 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11226 if (!bset)
11227 return isl_bool_error;
11229 return isl_space_is_wrapping(bset->dim);
11232 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11234 if (!set)
11235 return isl_bool_error;
11237 return isl_space_is_wrapping(set->dim);
11240 /* Modify the space of "map" through a call to "change".
11241 * If "can_change" is set (not NULL), then first call it to check
11242 * if the modification is allowed, printing the error message "cannot_change"
11243 * if it is not.
11245 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11246 isl_bool (*can_change)(__isl_keep isl_map *map),
11247 const char *cannot_change,
11248 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11250 isl_bool ok;
11251 isl_space *space;
11253 if (!map)
11254 return NULL;
11256 ok = can_change ? can_change(map) : isl_bool_true;
11257 if (ok < 0)
11258 return isl_map_free(map);
11259 if (!ok)
11260 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11261 return isl_map_free(map));
11263 space = change(isl_map_get_space(map));
11264 map = isl_map_reset_space(map, space);
11266 return map;
11269 /* Is the domain of "map" a wrapped relation?
11271 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11273 if (!map)
11274 return isl_bool_error;
11276 return isl_space_domain_is_wrapping(map->dim);
11279 /* Does "map" have a wrapped relation in both domain and range?
11281 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11283 return isl_space_is_product(isl_map_peek_space(map));
11286 /* Is the range of "map" a wrapped relation?
11288 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11290 if (!map)
11291 return isl_bool_error;
11293 return isl_space_range_is_wrapping(map->dim);
11296 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11298 bmap = isl_basic_map_cow(bmap);
11299 if (!bmap)
11300 return NULL;
11302 bmap->dim = isl_space_wrap(bmap->dim);
11303 if (!bmap->dim)
11304 goto error;
11306 bmap = isl_basic_map_finalize(bmap);
11308 return bset_from_bmap(bmap);
11309 error:
11310 isl_basic_map_free(bmap);
11311 return NULL;
11314 /* Given a map A -> B, return the set (A -> B).
11316 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11318 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11321 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11323 bset = isl_basic_set_cow(bset);
11324 if (!bset)
11325 return NULL;
11327 bset->dim = isl_space_unwrap(bset->dim);
11328 if (!bset->dim)
11329 goto error;
11331 bset = isl_basic_set_finalize(bset);
11333 return bset_to_bmap(bset);
11334 error:
11335 isl_basic_set_free(bset);
11336 return NULL;
11339 /* Given a set (A -> B), return the map A -> B.
11340 * Error out if "set" is not of the form (A -> B).
11342 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11344 return isl_map_change_space(set, &isl_set_is_wrapping,
11345 "not a wrapping set", &isl_space_unwrap);
11348 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11349 enum isl_dim_type type)
11351 if (!bmap)
11352 return NULL;
11354 if (!isl_space_is_named_or_nested(bmap->dim, type))
11355 return bmap;
11357 bmap = isl_basic_map_cow(bmap);
11358 if (!bmap)
11359 return NULL;
11361 bmap->dim = isl_space_reset(bmap->dim, type);
11362 if (!bmap->dim)
11363 goto error;
11365 bmap = isl_basic_map_finalize(bmap);
11367 return bmap;
11368 error:
11369 isl_basic_map_free(bmap);
11370 return NULL;
11373 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11374 enum isl_dim_type type)
11376 int i;
11378 if (!map)
11379 return NULL;
11381 if (!isl_space_is_named_or_nested(map->dim, type))
11382 return map;
11384 map = isl_map_cow(map);
11385 if (!map)
11386 return NULL;
11388 for (i = 0; i < map->n; ++i) {
11389 map->p[i] = isl_basic_map_reset(map->p[i], type);
11390 if (!map->p[i])
11391 goto error;
11393 map->dim = isl_space_reset(map->dim, type);
11394 if (!map->dim)
11395 goto error;
11397 return map;
11398 error:
11399 isl_map_free(map);
11400 return NULL;
11403 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11405 if (!bmap)
11406 return NULL;
11408 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11409 return bmap;
11411 bmap = isl_basic_map_cow(bmap);
11412 if (!bmap)
11413 return NULL;
11415 bmap->dim = isl_space_flatten(bmap->dim);
11416 if (!bmap->dim)
11417 goto error;
11419 bmap = isl_basic_map_finalize(bmap);
11421 return bmap;
11422 error:
11423 isl_basic_map_free(bmap);
11424 return NULL;
11427 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11429 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11432 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11433 __isl_take isl_basic_map *bmap)
11435 if (!bmap)
11436 return NULL;
11438 if (!bmap->dim->nested[0])
11439 return bmap;
11441 bmap = isl_basic_map_cow(bmap);
11442 if (!bmap)
11443 return NULL;
11445 bmap->dim = isl_space_flatten_domain(bmap->dim);
11446 if (!bmap->dim)
11447 goto error;
11449 bmap = isl_basic_map_finalize(bmap);
11451 return bmap;
11452 error:
11453 isl_basic_map_free(bmap);
11454 return NULL;
11457 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11458 __isl_take isl_basic_map *bmap)
11460 if (!bmap)
11461 return NULL;
11463 if (!bmap->dim->nested[1])
11464 return bmap;
11466 bmap = isl_basic_map_cow(bmap);
11467 if (!bmap)
11468 return NULL;
11470 bmap->dim = isl_space_flatten_range(bmap->dim);
11471 if (!bmap->dim)
11472 goto error;
11474 bmap = isl_basic_map_finalize(bmap);
11476 return bmap;
11477 error:
11478 isl_basic_map_free(bmap);
11479 return NULL;
11482 /* Remove any internal structure from the spaces of domain and range of "map".
11484 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11486 if (!map)
11487 return NULL;
11489 if (!map->dim->nested[0] && !map->dim->nested[1])
11490 return map;
11492 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11495 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11497 return set_from_map(isl_map_flatten(set_to_map(set)));
11500 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11502 isl_space *space, *flat_space;
11503 isl_map *map;
11505 space = isl_set_get_space(set);
11506 flat_space = isl_space_flatten(isl_space_copy(space));
11507 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11508 flat_space));
11509 map = isl_map_intersect_domain(map, set);
11511 return map;
11514 /* Remove any internal structure from the space of the domain of "map".
11516 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11518 if (!map)
11519 return NULL;
11521 if (!map->dim->nested[0])
11522 return map;
11524 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11527 /* Remove any internal structure from the space of the range of "map".
11529 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11531 if (!map)
11532 return NULL;
11534 if (!map->dim->nested[1])
11535 return map;
11537 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11540 /* Reorder the dimensions of "bmap" according to the given dim_map
11541 * and set the dimension specification to "space" and
11542 * perform Gaussian elimination on the result.
11544 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11545 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11547 isl_basic_map *res;
11548 unsigned flags;
11549 unsigned n_div;
11551 if (!bmap || !space || !dim_map)
11552 goto error;
11554 flags = bmap->flags;
11555 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11556 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11557 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11558 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11559 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11560 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11561 if (res)
11562 res->flags = flags;
11563 res = isl_basic_map_gauss(res, NULL);
11564 res = isl_basic_map_finalize(res);
11565 return res;
11566 error:
11567 isl_dim_map_free(dim_map);
11568 isl_basic_map_free(bmap);
11569 isl_space_free(space);
11570 return NULL;
11573 /* Reorder the dimensions of "map" according to given reordering.
11575 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11576 __isl_take isl_reordering *r)
11578 int i;
11579 struct isl_dim_map *dim_map;
11581 map = isl_map_cow(map);
11582 dim_map = isl_dim_map_from_reordering(r);
11583 if (!map || !r || !dim_map)
11584 goto error;
11586 for (i = 0; i < map->n; ++i) {
11587 struct isl_dim_map *dim_map_i;
11588 isl_space *space;
11590 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11592 space = isl_reordering_get_space(r);
11593 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11595 if (!map->p[i])
11596 goto error;
11599 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11600 map = isl_map_unmark_normalized(map);
11602 isl_reordering_free(r);
11603 isl_dim_map_free(dim_map);
11604 return map;
11605 error:
11606 isl_dim_map_free(dim_map);
11607 isl_map_free(map);
11608 isl_reordering_free(r);
11609 return NULL;
11612 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11613 __isl_take isl_reordering *r)
11615 return set_from_map(isl_map_realign(set_to_map(set), r));
11618 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11619 __isl_take isl_space *model)
11621 isl_ctx *ctx;
11622 isl_bool aligned;
11624 if (!map || !model)
11625 goto error;
11627 ctx = isl_space_get_ctx(model);
11628 if (!isl_space_has_named_params(model))
11629 isl_die(ctx, isl_error_invalid,
11630 "model has unnamed parameters", goto error);
11631 if (isl_map_check_named_params(map) < 0)
11632 goto error;
11633 aligned = isl_map_space_has_equal_params(map, model);
11634 if (aligned < 0)
11635 goto error;
11636 if (!aligned) {
11637 isl_reordering *exp;
11639 exp = isl_parameter_alignment_reordering(map->dim, model);
11640 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11641 map = isl_map_realign(map, exp);
11644 isl_space_free(model);
11645 return map;
11646 error:
11647 isl_space_free(model);
11648 isl_map_free(map);
11649 return NULL;
11652 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11653 __isl_take isl_space *model)
11655 return isl_map_align_params(set, model);
11658 /* Align the parameters of "bmap" to those of "model", introducing
11659 * additional parameters if needed.
11661 __isl_give isl_basic_map *isl_basic_map_align_params(
11662 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11664 isl_ctx *ctx;
11665 isl_bool equal_params;
11667 if (!bmap || !model)
11668 goto error;
11670 ctx = isl_space_get_ctx(model);
11671 if (!isl_space_has_named_params(model))
11672 isl_die(ctx, isl_error_invalid,
11673 "model has unnamed parameters", goto error);
11674 if (isl_basic_map_check_named_params(bmap) < 0)
11675 goto error;
11676 equal_params = isl_space_has_equal_params(bmap->dim, model);
11677 if (equal_params < 0)
11678 goto error;
11679 if (!equal_params) {
11680 isl_reordering *exp;
11681 struct isl_dim_map *dim_map;
11683 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11684 exp = isl_reordering_extend_space(exp,
11685 isl_basic_map_get_space(bmap));
11686 dim_map = isl_dim_map_from_reordering(exp);
11687 bmap = isl_basic_map_realign(bmap,
11688 isl_reordering_get_space(exp),
11689 isl_dim_map_extend(dim_map, bmap));
11690 isl_reordering_free(exp);
11691 isl_dim_map_free(dim_map);
11694 isl_space_free(model);
11695 return bmap;
11696 error:
11697 isl_space_free(model);
11698 isl_basic_map_free(bmap);
11699 return NULL;
11702 /* Do "bset" and "space" have the same parameters?
11704 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11705 __isl_keep isl_space *space)
11707 isl_space *bset_space;
11709 bset_space = isl_basic_set_peek_space(bset);
11710 return isl_space_has_equal_params(bset_space, space);
11713 /* Do "map" and "space" have the same parameters?
11715 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11716 __isl_keep isl_space *space)
11718 isl_space *map_space;
11720 map_space = isl_map_peek_space(map);
11721 return isl_space_has_equal_params(map_space, space);
11724 /* Do "set" and "space" have the same parameters?
11726 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11727 __isl_keep isl_space *space)
11729 return isl_map_space_has_equal_params(set_to_map(set), space);
11732 /* Align the parameters of "bset" to those of "model", introducing
11733 * additional parameters if needed.
11735 __isl_give isl_basic_set *isl_basic_set_align_params(
11736 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11738 return isl_basic_map_align_params(bset, model);
11741 /* Drop all parameters not referenced by "map".
11743 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11745 int i;
11747 if (isl_map_check_named_params(map) < 0)
11748 return isl_map_free(map);
11750 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11751 isl_bool involves;
11753 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11754 if (involves < 0)
11755 return isl_map_free(map);
11756 if (!involves)
11757 map = isl_map_project_out(map, isl_dim_param, i, 1);
11760 return map;
11763 /* Drop all parameters not referenced by "set".
11765 __isl_give isl_set *isl_set_drop_unused_params(
11766 __isl_take isl_set *set)
11768 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11771 /* Drop all parameters not referenced by "bmap".
11773 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11774 __isl_take isl_basic_map *bmap)
11776 int i;
11778 if (isl_basic_map_check_named_params(bmap) < 0)
11779 return isl_basic_map_free(bmap);
11781 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11782 isl_bool involves;
11784 involves = isl_basic_map_involves_dims(bmap,
11785 isl_dim_param, i, 1);
11786 if (involves < 0)
11787 return isl_basic_map_free(bmap);
11788 if (!involves)
11789 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11792 return bmap;
11795 /* Drop all parameters not referenced by "bset".
11797 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11798 __isl_take isl_basic_set *bset)
11800 return bset_from_bmap(isl_basic_map_drop_unused_params(
11801 bset_to_bmap(bset)));
11804 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11805 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11806 enum isl_dim_type c2, enum isl_dim_type c3,
11807 enum isl_dim_type c4, enum isl_dim_type c5)
11809 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11810 struct isl_mat *mat;
11811 int i, j, k;
11812 int pos;
11814 if (!bmap)
11815 return NULL;
11816 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11817 isl_basic_map_total_dim(bmap) + 1);
11818 if (!mat)
11819 return NULL;
11820 for (i = 0; i < bmap->n_eq; ++i)
11821 for (j = 0, pos = 0; j < 5; ++j) {
11822 int off = isl_basic_map_offset(bmap, c[j]);
11823 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11824 isl_int_set(mat->row[i][pos],
11825 bmap->eq[i][off + k]);
11826 ++pos;
11830 return mat;
11833 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11834 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11835 enum isl_dim_type c2, enum isl_dim_type c3,
11836 enum isl_dim_type c4, enum isl_dim_type c5)
11838 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11839 struct isl_mat *mat;
11840 int i, j, k;
11841 int pos;
11843 if (!bmap)
11844 return NULL;
11845 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11846 isl_basic_map_total_dim(bmap) + 1);
11847 if (!mat)
11848 return NULL;
11849 for (i = 0; i < bmap->n_ineq; ++i)
11850 for (j = 0, pos = 0; j < 5; ++j) {
11851 int off = isl_basic_map_offset(bmap, c[j]);
11852 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11853 isl_int_set(mat->row[i][pos],
11854 bmap->ineq[i][off + k]);
11855 ++pos;
11859 return mat;
11862 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11863 __isl_take isl_space *space,
11864 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11865 enum isl_dim_type c2, enum isl_dim_type c3,
11866 enum isl_dim_type c4, enum isl_dim_type c5)
11868 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11869 isl_basic_map *bmap = NULL;
11870 unsigned total;
11871 unsigned extra;
11872 int i, j, k, l;
11873 int pos;
11875 if (!space || !eq || !ineq)
11876 goto error;
11878 if (eq->n_col != ineq->n_col)
11879 isl_die(space->ctx, isl_error_invalid,
11880 "equalities and inequalities matrices should have "
11881 "same number of columns", goto error);
11883 total = 1 + isl_space_dim(space, isl_dim_all);
11885 if (eq->n_col < total)
11886 isl_die(space->ctx, isl_error_invalid,
11887 "number of columns too small", goto error);
11889 extra = eq->n_col - total;
11891 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11892 eq->n_row, ineq->n_row);
11893 if (!bmap)
11894 goto error;
11895 for (i = 0; i < extra; ++i) {
11896 k = isl_basic_map_alloc_div(bmap);
11897 if (k < 0)
11898 goto error;
11899 isl_int_set_si(bmap->div[k][0], 0);
11901 for (i = 0; i < eq->n_row; ++i) {
11902 l = isl_basic_map_alloc_equality(bmap);
11903 if (l < 0)
11904 goto error;
11905 for (j = 0, pos = 0; j < 5; ++j) {
11906 int off = isl_basic_map_offset(bmap, c[j]);
11907 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11908 isl_int_set(bmap->eq[l][off + k],
11909 eq->row[i][pos]);
11910 ++pos;
11914 for (i = 0; i < ineq->n_row; ++i) {
11915 l = isl_basic_map_alloc_inequality(bmap);
11916 if (l < 0)
11917 goto error;
11918 for (j = 0, pos = 0; j < 5; ++j) {
11919 int off = isl_basic_map_offset(bmap, c[j]);
11920 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11921 isl_int_set(bmap->ineq[l][off + k],
11922 ineq->row[i][pos]);
11923 ++pos;
11928 isl_space_free(space);
11929 isl_mat_free(eq);
11930 isl_mat_free(ineq);
11932 bmap = isl_basic_map_simplify(bmap);
11933 return isl_basic_map_finalize(bmap);
11934 error:
11935 isl_space_free(space);
11936 isl_mat_free(eq);
11937 isl_mat_free(ineq);
11938 isl_basic_map_free(bmap);
11939 return NULL;
11942 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11943 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11944 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11946 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11947 c1, c2, c3, c4, isl_dim_in);
11950 __isl_give isl_mat *isl_basic_set_inequalities_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_inequalities_matrix(bset_to_bmap(bset),
11955 c1, c2, c3, c4, isl_dim_in);
11958 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11959 __isl_take isl_space *dim,
11960 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11961 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11963 isl_basic_map *bmap;
11964 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11965 c1, c2, c3, c4, isl_dim_in);
11966 return bset_from_bmap(bmap);
11969 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11971 if (!bmap)
11972 return isl_bool_error;
11974 return isl_space_can_zip(bmap->dim);
11977 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11979 if (!map)
11980 return isl_bool_error;
11982 return isl_space_can_zip(map->dim);
11985 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11986 * (A -> C) -> (B -> D).
11988 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11990 unsigned pos;
11991 unsigned n1;
11992 unsigned n2;
11994 if (!bmap)
11995 return NULL;
11997 if (!isl_basic_map_can_zip(bmap))
11998 isl_die(bmap->ctx, isl_error_invalid,
11999 "basic map cannot be zipped", goto error);
12000 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12001 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12002 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12003 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12004 bmap = isl_basic_map_cow(bmap);
12005 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12006 if (!bmap)
12007 return NULL;
12008 bmap->dim = isl_space_zip(bmap->dim);
12009 if (!bmap->dim)
12010 goto error;
12011 bmap = isl_basic_map_mark_final(bmap);
12012 return bmap;
12013 error:
12014 isl_basic_map_free(bmap);
12015 return NULL;
12018 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12019 * (A -> C) -> (B -> D).
12021 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12023 int i;
12025 if (!map)
12026 return NULL;
12028 if (!isl_map_can_zip(map))
12029 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12030 goto error);
12032 map = isl_map_cow(map);
12033 if (!map)
12034 return NULL;
12036 for (i = 0; i < map->n; ++i) {
12037 map->p[i] = isl_basic_map_zip(map->p[i]);
12038 if (!map->p[i])
12039 goto error;
12042 map->dim = isl_space_zip(map->dim);
12043 if (!map->dim)
12044 goto error;
12046 return map;
12047 error:
12048 isl_map_free(map);
12049 return NULL;
12052 /* Can we apply isl_basic_map_curry to "bmap"?
12053 * That is, does it have a nested relation in its domain?
12055 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12057 if (!bmap)
12058 return isl_bool_error;
12060 return isl_space_can_curry(bmap->dim);
12063 /* Can we apply isl_map_curry to "map"?
12064 * That is, does it have a nested relation in its domain?
12066 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12068 if (!map)
12069 return isl_bool_error;
12071 return isl_space_can_curry(map->dim);
12074 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12075 * A -> (B -> C).
12077 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12080 if (!bmap)
12081 return NULL;
12083 if (!isl_basic_map_can_curry(bmap))
12084 isl_die(bmap->ctx, isl_error_invalid,
12085 "basic map cannot be curried", goto error);
12086 bmap = isl_basic_map_cow(bmap);
12087 if (!bmap)
12088 return NULL;
12089 bmap->dim = isl_space_curry(bmap->dim);
12090 if (!bmap->dim)
12091 goto error;
12092 bmap = isl_basic_map_mark_final(bmap);
12093 return bmap;
12094 error:
12095 isl_basic_map_free(bmap);
12096 return NULL;
12099 /* Given a map (A -> B) -> C, return the corresponding map
12100 * A -> (B -> C).
12102 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12104 return isl_map_change_space(map, &isl_map_can_curry,
12105 "map cannot be curried", &isl_space_curry);
12108 /* Can isl_map_range_curry be applied to "map"?
12109 * That is, does it have a nested relation in its range,
12110 * the domain of which is itself a nested relation?
12112 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12114 if (!map)
12115 return isl_bool_error;
12117 return isl_space_can_range_curry(map->dim);
12120 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12121 * A -> (B -> (C -> D)).
12123 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12125 return isl_map_change_space(map, &isl_map_can_range_curry,
12126 "map range cannot be curried",
12127 &isl_space_range_curry);
12130 /* Can we apply isl_basic_map_uncurry to "bmap"?
12131 * That is, does it have a nested relation in its domain?
12133 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12135 if (!bmap)
12136 return isl_bool_error;
12138 return isl_space_can_uncurry(bmap->dim);
12141 /* Can we apply isl_map_uncurry to "map"?
12142 * That is, does it have a nested relation in its domain?
12144 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12146 if (!map)
12147 return isl_bool_error;
12149 return isl_space_can_uncurry(map->dim);
12152 /* Given a basic map A -> (B -> C), return the corresponding basic map
12153 * (A -> B) -> C.
12155 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12158 if (!bmap)
12159 return NULL;
12161 if (!isl_basic_map_can_uncurry(bmap))
12162 isl_die(bmap->ctx, isl_error_invalid,
12163 "basic map cannot be uncurried",
12164 return isl_basic_map_free(bmap));
12165 bmap = isl_basic_map_cow(bmap);
12166 if (!bmap)
12167 return NULL;
12168 bmap->dim = isl_space_uncurry(bmap->dim);
12169 if (!bmap->dim)
12170 return isl_basic_map_free(bmap);
12171 bmap = isl_basic_map_mark_final(bmap);
12172 return bmap;
12175 /* Given a map A -> (B -> C), return the corresponding map
12176 * (A -> B) -> C.
12178 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12180 return isl_map_change_space(map, &isl_map_can_uncurry,
12181 "map cannot be uncurried", &isl_space_uncurry);
12184 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12185 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12187 return isl_map_equate(set, type1, pos1, type2, pos2);
12190 /* Construct a basic map where the given dimensions are equal to each other.
12192 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12193 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12195 isl_basic_map *bmap = NULL;
12196 int i;
12198 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12199 isl_space_check_range(space, type2, pos2, 1) < 0)
12200 goto error;
12202 if (type1 == type2 && pos1 == pos2)
12203 return isl_basic_map_universe(space);
12205 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12206 i = isl_basic_map_alloc_equality(bmap);
12207 if (i < 0)
12208 goto error;
12209 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12210 pos1 += isl_basic_map_offset(bmap, type1);
12211 pos2 += isl_basic_map_offset(bmap, type2);
12212 isl_int_set_si(bmap->eq[i][pos1], -1);
12213 isl_int_set_si(bmap->eq[i][pos2], 1);
12214 bmap = isl_basic_map_finalize(bmap);
12215 isl_space_free(space);
12216 return bmap;
12217 error:
12218 isl_space_free(space);
12219 isl_basic_map_free(bmap);
12220 return NULL;
12223 /* Add a constraint imposing that the given two dimensions are equal.
12225 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12226 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12228 isl_basic_map *eq;
12230 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12232 bmap = isl_basic_map_intersect(bmap, eq);
12234 return bmap;
12237 /* Add a constraint imposing that the given two dimensions are equal.
12239 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12240 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12242 isl_basic_map *bmap;
12244 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12246 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12248 return map;
12251 /* Add a constraint imposing that the given two dimensions have opposite values.
12253 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12254 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12256 isl_basic_map *bmap = NULL;
12257 int i;
12259 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12260 return isl_map_free(map);
12261 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12262 return isl_map_free(map);
12264 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12265 i = isl_basic_map_alloc_equality(bmap);
12266 if (i < 0)
12267 goto error;
12268 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12269 pos1 += isl_basic_map_offset(bmap, type1);
12270 pos2 += isl_basic_map_offset(bmap, type2);
12271 isl_int_set_si(bmap->eq[i][pos1], 1);
12272 isl_int_set_si(bmap->eq[i][pos2], 1);
12273 bmap = isl_basic_map_finalize(bmap);
12275 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12277 return map;
12278 error:
12279 isl_basic_map_free(bmap);
12280 isl_map_free(map);
12281 return NULL;
12284 /* Construct a constraint imposing that the value of the first dimension is
12285 * greater than or equal to that of the second.
12287 static __isl_give isl_constraint *constraint_order_ge(
12288 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12289 enum isl_dim_type type2, int pos2)
12291 isl_constraint *c;
12293 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12294 isl_space_check_range(space, type2, pos2, 1) < 0)
12295 space = isl_space_free(space);
12296 if (!space)
12297 return NULL;
12299 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12301 if (type1 == type2 && pos1 == pos2)
12302 return c;
12304 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12305 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12307 return c;
12310 /* Add a constraint imposing that the value of the first dimension is
12311 * greater than or equal to that of the second.
12313 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12314 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12316 isl_constraint *c;
12317 isl_space *space;
12319 if (type1 == type2 && pos1 == pos2)
12320 return bmap;
12321 space = isl_basic_map_get_space(bmap);
12322 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12323 bmap = isl_basic_map_add_constraint(bmap, c);
12325 return bmap;
12328 /* Add a constraint imposing that the value of the first dimension is
12329 * greater than or equal to that of the second.
12331 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12332 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12334 isl_constraint *c;
12335 isl_space *space;
12337 if (type1 == type2 && pos1 == pos2)
12338 return map;
12339 space = isl_map_get_space(map);
12340 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12341 map = isl_map_add_constraint(map, c);
12343 return map;
12346 /* Add a constraint imposing that the value of the first dimension is
12347 * less than or equal to that of the second.
12349 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12350 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12352 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12355 /* Construct a basic map where the value of the first dimension is
12356 * greater than that of the second.
12358 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12359 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12361 isl_basic_map *bmap = NULL;
12362 int i;
12364 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12365 isl_space_check_range(space, type2, pos2, 1) < 0)
12366 goto error;
12368 if (type1 == type2 && pos1 == pos2)
12369 return isl_basic_map_empty(space);
12371 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12372 i = isl_basic_map_alloc_inequality(bmap);
12373 if (i < 0)
12374 return isl_basic_map_free(bmap);
12375 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12376 pos1 += isl_basic_map_offset(bmap, type1);
12377 pos2 += isl_basic_map_offset(bmap, type2);
12378 isl_int_set_si(bmap->ineq[i][pos1], 1);
12379 isl_int_set_si(bmap->ineq[i][pos2], -1);
12380 isl_int_set_si(bmap->ineq[i][0], -1);
12381 bmap = isl_basic_map_finalize(bmap);
12383 return bmap;
12384 error:
12385 isl_space_free(space);
12386 isl_basic_map_free(bmap);
12387 return NULL;
12390 /* Add a constraint imposing that the value of the first dimension is
12391 * greater than that of the second.
12393 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12394 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12396 isl_basic_map *gt;
12398 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12400 bmap = isl_basic_map_intersect(bmap, gt);
12402 return bmap;
12405 /* Add a constraint imposing that the value of the first dimension is
12406 * greater than that of the second.
12408 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12409 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12411 isl_basic_map *bmap;
12413 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12415 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12417 return map;
12420 /* Add a constraint imposing that the value of the first dimension is
12421 * smaller than that of the second.
12423 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12424 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12426 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12429 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12430 int pos)
12432 isl_aff *div;
12433 isl_local_space *ls;
12435 if (!bmap)
12436 return NULL;
12438 if (!isl_basic_map_divs_known(bmap))
12439 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12440 "some divs are unknown", return NULL);
12442 ls = isl_basic_map_get_local_space(bmap);
12443 div = isl_local_space_get_div(ls, pos);
12444 isl_local_space_free(ls);
12446 return div;
12449 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12450 int pos)
12452 return isl_basic_map_get_div(bset, pos);
12455 /* Plug in "subs" for dimension "type", "pos" of "bset".
12457 * Let i be the dimension to replace and let "subs" be of the form
12459 * f/d
12461 * Any integer division with a non-zero coefficient for i,
12463 * floor((a i + g)/m)
12465 * is replaced by
12467 * floor((a f + d g)/(m d))
12469 * Constraints of the form
12471 * a i + g
12473 * are replaced by
12475 * a f + d g
12477 * We currently require that "subs" is an integral expression.
12478 * Handling rational expressions may require us to add stride constraints
12479 * as we do in isl_basic_set_preimage_multi_aff.
12481 __isl_give isl_basic_set *isl_basic_set_substitute(
12482 __isl_take isl_basic_set *bset,
12483 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12485 int i;
12486 isl_int v;
12487 isl_ctx *ctx;
12489 if (bset && isl_basic_set_plain_is_empty(bset))
12490 return bset;
12492 bset = isl_basic_set_cow(bset);
12493 if (!bset || !subs)
12494 goto error;
12496 ctx = isl_basic_set_get_ctx(bset);
12497 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12498 isl_die(ctx, isl_error_invalid,
12499 "spaces don't match", goto error);
12500 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12501 isl_die(ctx, isl_error_unsupported,
12502 "cannot handle divs yet", goto error);
12503 if (!isl_int_is_one(subs->v->el[0]))
12504 isl_die(ctx, isl_error_invalid,
12505 "can only substitute integer expressions", goto error);
12507 pos += isl_basic_set_offset(bset, type);
12509 isl_int_init(v);
12511 for (i = 0; i < bset->n_eq; ++i) {
12512 if (isl_int_is_zero(bset->eq[i][pos]))
12513 continue;
12514 isl_int_set(v, bset->eq[i][pos]);
12515 isl_int_set_si(bset->eq[i][pos], 0);
12516 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12517 v, subs->v->el + 1, subs->v->size - 1);
12520 for (i = 0; i < bset->n_ineq; ++i) {
12521 if (isl_int_is_zero(bset->ineq[i][pos]))
12522 continue;
12523 isl_int_set(v, bset->ineq[i][pos]);
12524 isl_int_set_si(bset->ineq[i][pos], 0);
12525 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12526 v, subs->v->el + 1, subs->v->size - 1);
12529 for (i = 0; i < bset->n_div; ++i) {
12530 if (isl_int_is_zero(bset->div[i][1 + pos]))
12531 continue;
12532 isl_int_set(v, bset->div[i][1 + pos]);
12533 isl_int_set_si(bset->div[i][1 + pos], 0);
12534 isl_seq_combine(bset->div[i] + 1,
12535 subs->v->el[0], bset->div[i] + 1,
12536 v, subs->v->el + 1, subs->v->size - 1);
12537 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12540 isl_int_clear(v);
12542 bset = isl_basic_set_simplify(bset);
12543 return isl_basic_set_finalize(bset);
12544 error:
12545 isl_basic_set_free(bset);
12546 return NULL;
12549 /* Plug in "subs" for dimension "type", "pos" of "set".
12551 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12552 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12554 int i;
12556 if (set && isl_set_plain_is_empty(set))
12557 return set;
12559 set = isl_set_cow(set);
12560 if (!set || !subs)
12561 goto error;
12563 for (i = set->n - 1; i >= 0; --i) {
12564 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12565 set = set_from_map(remove_if_empty(set_to_map(set), i));
12566 if (!set)
12567 return NULL;
12570 return set;
12571 error:
12572 isl_set_free(set);
12573 return NULL;
12576 /* Check if the range of "ma" is compatible with the domain or range
12577 * (depending on "type") of "bmap".
12579 static isl_stat check_basic_map_compatible_range_multi_aff(
12580 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12581 __isl_keep isl_multi_aff *ma)
12583 isl_bool m;
12584 isl_space *ma_space;
12586 ma_space = isl_multi_aff_get_space(ma);
12588 m = isl_space_has_equal_params(bmap->dim, ma_space);
12589 if (m < 0)
12590 goto error;
12591 if (!m)
12592 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12593 "parameters don't match", goto error);
12594 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12595 if (m < 0)
12596 goto error;
12597 if (!m)
12598 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12599 "spaces don't match", goto error);
12601 isl_space_free(ma_space);
12602 return isl_stat_ok;
12603 error:
12604 isl_space_free(ma_space);
12605 return isl_stat_error;
12608 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12609 * coefficients before the transformed range of dimensions,
12610 * the "n_after" coefficients after the transformed range of dimensions
12611 * and the coefficients of the other divs in "bmap".
12613 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12614 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12616 int i;
12617 int n_param;
12618 int n_set;
12619 isl_local_space *ls;
12621 if (n_div == 0)
12622 return bmap;
12624 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12625 if (!ls)
12626 return isl_basic_map_free(bmap);
12628 n_param = isl_local_space_dim(ls, isl_dim_param);
12629 n_set = isl_local_space_dim(ls, isl_dim_set);
12630 for (i = 0; i < n_div; ++i) {
12631 int o_bmap = 0, o_ls = 0;
12633 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12634 o_bmap += 1 + 1 + n_param;
12635 o_ls += 1 + 1 + n_param;
12636 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12637 o_bmap += n_before;
12638 isl_seq_cpy(bmap->div[i] + o_bmap,
12639 ls->div->row[i] + o_ls, n_set);
12640 o_bmap += n_set;
12641 o_ls += n_set;
12642 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12643 o_bmap += n_after;
12644 isl_seq_cpy(bmap->div[i] + o_bmap,
12645 ls->div->row[i] + o_ls, n_div);
12646 o_bmap += n_div;
12647 o_ls += n_div;
12648 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12649 bmap = isl_basic_map_add_div_constraints(bmap, i);
12650 if (!bmap)
12651 goto error;
12654 isl_local_space_free(ls);
12655 return bmap;
12656 error:
12657 isl_local_space_free(ls);
12658 return isl_basic_map_free(bmap);
12661 /* How many stride constraints does "ma" enforce?
12662 * That is, how many of the affine expressions have a denominator
12663 * different from one?
12665 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12667 int i;
12668 int strides = 0;
12670 for (i = 0; i < ma->n; ++i)
12671 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12672 strides++;
12674 return strides;
12677 /* For each affine expression in ma of the form
12679 * x_i = (f_i y + h_i)/m_i
12681 * with m_i different from one, add a constraint to "bmap"
12682 * of the form
12684 * f_i y + h_i = m_i alpha_i
12686 * with alpha_i an additional existentially quantified variable.
12688 * The input variables of "ma" correspond to a subset of the variables
12689 * of "bmap". There are "n_before" variables in "bmap" before this
12690 * subset and "n_after" variables after this subset.
12691 * The integer divisions of the affine expressions in "ma" are assumed
12692 * to have been aligned. There are "n_div_ma" of them and
12693 * they appear first in "bmap", straight after the "n_after" variables.
12695 static __isl_give isl_basic_map *add_ma_strides(
12696 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12697 int n_before, int n_after, int n_div_ma)
12699 int i, k;
12700 int div;
12701 int total;
12702 int n_param;
12703 int n_in;
12705 total = isl_basic_map_total_dim(bmap);
12706 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12707 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12708 for (i = 0; i < ma->n; ++i) {
12709 int o_bmap = 0, o_ma = 1;
12711 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12712 continue;
12713 div = isl_basic_map_alloc_div(bmap);
12714 k = isl_basic_map_alloc_equality(bmap);
12715 if (div < 0 || k < 0)
12716 goto error;
12717 isl_int_set_si(bmap->div[div][0], 0);
12718 isl_seq_cpy(bmap->eq[k] + o_bmap,
12719 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12720 o_bmap += 1 + n_param;
12721 o_ma += 1 + n_param;
12722 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12723 o_bmap += n_before;
12724 isl_seq_cpy(bmap->eq[k] + o_bmap,
12725 ma->u.p[i]->v->el + o_ma, n_in);
12726 o_bmap += n_in;
12727 o_ma += n_in;
12728 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12729 o_bmap += n_after;
12730 isl_seq_cpy(bmap->eq[k] + o_bmap,
12731 ma->u.p[i]->v->el + o_ma, n_div_ma);
12732 o_bmap += n_div_ma;
12733 o_ma += n_div_ma;
12734 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12735 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12736 total++;
12739 return bmap;
12740 error:
12741 isl_basic_map_free(bmap);
12742 return NULL;
12745 /* Replace the domain or range space (depending on "type) of "space" by "set".
12747 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12748 enum isl_dim_type type, __isl_take isl_space *set)
12750 if (type == isl_dim_in) {
12751 space = isl_space_range(space);
12752 space = isl_space_map_from_domain_and_range(set, space);
12753 } else {
12754 space = isl_space_domain(space);
12755 space = isl_space_map_from_domain_and_range(space, set);
12758 return space;
12761 /* Compute the preimage of the domain or range (depending on "type")
12762 * of "bmap" under the function represented by "ma".
12763 * In other words, plug in "ma" in the domain or range of "bmap".
12764 * The result is a basic map that lives in the same space as "bmap"
12765 * except that the domain or range has been replaced by
12766 * the domain space of "ma".
12768 * If bmap is represented by
12770 * A(p) + S u + B x + T v + C(divs) >= 0,
12772 * where u and x are input and output dimensions if type == isl_dim_out
12773 * while x and v are input and output dimensions if type == isl_dim_in,
12774 * and ma is represented by
12776 * x = D(p) + F(y) + G(divs')
12778 * then the result is
12780 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12782 * The divs in the input set are similarly adjusted.
12783 * In particular
12785 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12787 * becomes
12789 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12790 * B_i G(divs') + c_i(divs))/n_i)
12792 * If bmap is not a rational map and if F(y) involves any denominators
12794 * x_i = (f_i y + h_i)/m_i
12796 * then additional constraints are added to ensure that we only
12797 * map back integer points. That is we enforce
12799 * f_i y + h_i = m_i alpha_i
12801 * with alpha_i an additional existentially quantified variable.
12803 * We first copy over the divs from "ma".
12804 * Then we add the modified constraints and divs from "bmap".
12805 * Finally, we add the stride constraints, if needed.
12807 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12808 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12809 __isl_take isl_multi_aff *ma)
12811 int i, k;
12812 isl_space *space;
12813 isl_basic_map *res = NULL;
12814 int n_before, n_after, n_div_bmap, n_div_ma;
12815 isl_int f, c1, c2, g;
12816 isl_bool rational;
12817 int strides;
12819 isl_int_init(f);
12820 isl_int_init(c1);
12821 isl_int_init(c2);
12822 isl_int_init(g);
12824 ma = isl_multi_aff_align_divs(ma);
12825 if (!bmap || !ma)
12826 goto error;
12827 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12828 goto error;
12830 if (type == isl_dim_in) {
12831 n_before = 0;
12832 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12833 } else {
12834 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12835 n_after = 0;
12837 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12838 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12840 space = isl_multi_aff_get_domain_space(ma);
12841 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12842 rational = isl_basic_map_is_rational(bmap);
12843 strides = rational ? 0 : multi_aff_strides(ma);
12844 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12845 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12846 if (rational)
12847 res = isl_basic_map_set_rational(res);
12849 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12850 if (isl_basic_map_alloc_div(res) < 0)
12851 goto error;
12853 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
12854 if (!res)
12855 goto error;
12857 for (i = 0; i < bmap->n_eq; ++i) {
12858 k = isl_basic_map_alloc_equality(res);
12859 if (k < 0)
12860 goto error;
12861 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12862 n_after, n_div_ma, n_div_bmap,
12863 f, c1, c2, g, 0) < 0)
12864 goto error;
12867 for (i = 0; i < bmap->n_ineq; ++i) {
12868 k = isl_basic_map_alloc_inequality(res);
12869 if (k < 0)
12870 goto error;
12871 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12872 n_after, n_div_ma, n_div_bmap,
12873 f, c1, c2, g, 0) < 0)
12874 goto error;
12877 for (i = 0; i < bmap->n_div; ++i) {
12878 if (isl_int_is_zero(bmap->div[i][0])) {
12879 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12880 continue;
12882 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12883 n_before, n_after, n_div_ma, n_div_bmap,
12884 f, c1, c2, g, 1) < 0)
12885 goto error;
12888 if (strides)
12889 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12891 isl_int_clear(f);
12892 isl_int_clear(c1);
12893 isl_int_clear(c2);
12894 isl_int_clear(g);
12895 isl_basic_map_free(bmap);
12896 isl_multi_aff_free(ma);
12897 res = isl_basic_map_simplify(res);
12898 return isl_basic_map_finalize(res);
12899 error:
12900 isl_int_clear(f);
12901 isl_int_clear(c1);
12902 isl_int_clear(c2);
12903 isl_int_clear(g);
12904 isl_basic_map_free(bmap);
12905 isl_multi_aff_free(ma);
12906 isl_basic_map_free(res);
12907 return NULL;
12910 /* Compute the preimage of "bset" under the function represented by "ma".
12911 * In other words, plug in "ma" in "bset". The result is a basic set
12912 * that lives in the domain space of "ma".
12914 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12915 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12917 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12920 /* Compute the preimage of the domain of "bmap" under the function
12921 * represented by "ma".
12922 * In other words, plug in "ma" in the domain of "bmap".
12923 * The result is a basic map that lives in the same space as "bmap"
12924 * except that the domain has been replaced by the domain space of "ma".
12926 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12927 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12929 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12932 /* Compute the preimage of the range of "bmap" under the function
12933 * represented by "ma".
12934 * In other words, plug in "ma" in the range of "bmap".
12935 * The result is a basic map that lives in the same space as "bmap"
12936 * except that the range has been replaced by the domain space of "ma".
12938 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12939 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12941 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12944 /* Check if the range of "ma" is compatible with the domain or range
12945 * (depending on "type") of "map".
12946 * Return isl_stat_error if anything is wrong.
12948 static isl_stat check_map_compatible_range_multi_aff(
12949 __isl_keep isl_map *map, enum isl_dim_type type,
12950 __isl_keep isl_multi_aff *ma)
12952 isl_bool m;
12953 isl_space *ma_space;
12955 ma_space = isl_multi_aff_get_space(ma);
12956 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12957 isl_space_free(ma_space);
12958 if (m < 0)
12959 return isl_stat_error;
12960 if (!m)
12961 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12962 "spaces don't match", return isl_stat_error);
12963 return isl_stat_ok;
12966 /* Compute the preimage of the domain or range (depending on "type")
12967 * of "map" under the function represented by "ma".
12968 * In other words, plug in "ma" in the domain or range of "map".
12969 * The result is a map that lives in the same space as "map"
12970 * except that the domain or range has been replaced by
12971 * the domain space of "ma".
12973 * The parameters are assumed to have been aligned.
12975 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12976 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12978 int i;
12979 isl_space *space;
12981 map = isl_map_cow(map);
12982 ma = isl_multi_aff_align_divs(ma);
12983 if (!map || !ma)
12984 goto error;
12985 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12986 goto error;
12988 for (i = 0; i < map->n; ++i) {
12989 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12990 isl_multi_aff_copy(ma));
12991 if (!map->p[i])
12992 goto error;
12995 space = isl_multi_aff_get_domain_space(ma);
12996 space = isl_space_set(isl_map_get_space(map), type, space);
12998 isl_space_free(map->dim);
12999 map->dim = space;
13000 if (!map->dim)
13001 goto error;
13003 isl_multi_aff_free(ma);
13004 if (map->n > 1)
13005 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13006 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13007 return map;
13008 error:
13009 isl_multi_aff_free(ma);
13010 isl_map_free(map);
13011 return NULL;
13014 /* Compute the preimage of the domain or range (depending on "type")
13015 * of "map" under the function represented by "ma".
13016 * In other words, plug in "ma" in the domain or range of "map".
13017 * The result is a map that lives in the same space as "map"
13018 * except that the domain or range has been replaced by
13019 * the domain space of "ma".
13021 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13022 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13024 isl_bool aligned;
13026 if (!map || !ma)
13027 goto error;
13029 aligned = isl_map_space_has_equal_params(map, ma->space);
13030 if (aligned < 0)
13031 goto error;
13032 if (aligned)
13033 return map_preimage_multi_aff(map, type, ma);
13035 if (isl_map_check_named_params(map) < 0)
13036 goto error;
13037 if (!isl_space_has_named_params(ma->space))
13038 isl_die(map->ctx, isl_error_invalid,
13039 "unaligned unnamed parameters", goto error);
13040 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13041 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13043 return map_preimage_multi_aff(map, type, ma);
13044 error:
13045 isl_multi_aff_free(ma);
13046 return isl_map_free(map);
13049 /* Compute the preimage of "set" under the function represented by "ma".
13050 * In other words, plug in "ma" in "set". The result is a set
13051 * that lives in the domain space of "ma".
13053 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13054 __isl_take isl_multi_aff *ma)
13056 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13059 /* Compute the preimage of the domain of "map" under the function
13060 * represented by "ma".
13061 * In other words, plug in "ma" in the domain of "map".
13062 * The result is a map that lives in the same space as "map"
13063 * except that the domain has been replaced by the domain space of "ma".
13065 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13066 __isl_take isl_multi_aff *ma)
13068 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13071 /* Compute the preimage of the range of "map" under the function
13072 * represented by "ma".
13073 * In other words, plug in "ma" in the range of "map".
13074 * The result is a map that lives in the same space as "map"
13075 * except that the range has been replaced by the domain space of "ma".
13077 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13078 __isl_take isl_multi_aff *ma)
13080 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13083 /* Compute the preimage of "map" under the function represented by "pma".
13084 * In other words, plug in "pma" in the domain or range of "map".
13085 * The result is a map that lives in the same space as "map",
13086 * except that the space of type "type" has been replaced by
13087 * the domain space of "pma".
13089 * The parameters of "map" and "pma" are assumed to have been aligned.
13091 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13092 __isl_take isl_map *map, enum isl_dim_type type,
13093 __isl_take isl_pw_multi_aff *pma)
13095 int i;
13096 isl_map *res;
13098 if (!pma)
13099 goto error;
13101 if (pma->n == 0) {
13102 isl_pw_multi_aff_free(pma);
13103 res = isl_map_empty(isl_map_get_space(map));
13104 isl_map_free(map);
13105 return res;
13108 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13109 isl_multi_aff_copy(pma->p[0].maff));
13110 if (type == isl_dim_in)
13111 res = isl_map_intersect_domain(res,
13112 isl_map_copy(pma->p[0].set));
13113 else
13114 res = isl_map_intersect_range(res,
13115 isl_map_copy(pma->p[0].set));
13117 for (i = 1; i < pma->n; ++i) {
13118 isl_map *res_i;
13120 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13121 isl_multi_aff_copy(pma->p[i].maff));
13122 if (type == isl_dim_in)
13123 res_i = isl_map_intersect_domain(res_i,
13124 isl_map_copy(pma->p[i].set));
13125 else
13126 res_i = isl_map_intersect_range(res_i,
13127 isl_map_copy(pma->p[i].set));
13128 res = isl_map_union(res, res_i);
13131 isl_pw_multi_aff_free(pma);
13132 isl_map_free(map);
13133 return res;
13134 error:
13135 isl_pw_multi_aff_free(pma);
13136 isl_map_free(map);
13137 return NULL;
13140 /* Compute the preimage of "map" under the function represented by "pma".
13141 * In other words, plug in "pma" in the domain or range of "map".
13142 * The result is a map that lives in the same space as "map",
13143 * except that the space of type "type" has been replaced by
13144 * the domain space of "pma".
13146 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13147 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13149 isl_bool aligned;
13151 if (!map || !pma)
13152 goto error;
13154 aligned = isl_map_space_has_equal_params(map, pma->dim);
13155 if (aligned < 0)
13156 goto error;
13157 if (aligned)
13158 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13160 if (isl_map_check_named_params(map) < 0)
13161 goto error;
13162 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13163 goto error;
13164 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13165 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13167 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13168 error:
13169 isl_pw_multi_aff_free(pma);
13170 return isl_map_free(map);
13173 /* Compute the preimage of "set" under the function represented by "pma".
13174 * In other words, plug in "pma" in "set". The result is a set
13175 * that lives in the domain space of "pma".
13177 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13178 __isl_take isl_pw_multi_aff *pma)
13180 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13183 /* Compute the preimage of the domain of "map" under the function
13184 * represented by "pma".
13185 * In other words, plug in "pma" in the domain of "map".
13186 * The result is a map that lives in the same space as "map",
13187 * except that domain space has been replaced by the domain space of "pma".
13189 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13190 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13192 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13195 /* Compute the preimage of the range of "map" under the function
13196 * represented by "pma".
13197 * In other words, plug in "pma" in the range of "map".
13198 * The result is a map that lives in the same space as "map",
13199 * except that range space has been replaced by the domain space of "pma".
13201 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13202 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13204 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13207 /* Compute the preimage of "map" under the function represented by "mpa".
13208 * In other words, plug in "mpa" in the domain or range of "map".
13209 * The result is a map that lives in the same space as "map",
13210 * except that the space of type "type" has been replaced by
13211 * the domain space of "mpa".
13213 * If the map does not involve any constraints that refer to the
13214 * dimensions of the substituted space, then the only possible
13215 * effect of "mpa" on the map is to map the space to a different space.
13216 * We create a separate isl_multi_aff to effectuate this change
13217 * in order to avoid spurious splitting of the map along the pieces
13218 * of "mpa".
13219 * If "mpa" has a non-trivial explicit domain, however,
13220 * then the full substitution should be performed.
13222 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13223 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13225 int n;
13226 isl_bool full;
13227 isl_pw_multi_aff *pma;
13229 if (!map || !mpa)
13230 goto error;
13232 n = isl_map_dim(map, type);
13233 full = isl_map_involves_dims(map, type, 0, n);
13234 if (full >= 0 && !full)
13235 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13236 if (full < 0)
13237 goto error;
13238 if (!full) {
13239 isl_space *space;
13240 isl_multi_aff *ma;
13242 space = isl_multi_pw_aff_get_space(mpa);
13243 isl_multi_pw_aff_free(mpa);
13244 ma = isl_multi_aff_zero(space);
13245 return isl_map_preimage_multi_aff(map, type, ma);
13248 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13249 return isl_map_preimage_pw_multi_aff(map, type, pma);
13250 error:
13251 isl_map_free(map);
13252 isl_multi_pw_aff_free(mpa);
13253 return NULL;
13256 /* Compute the preimage of "map" under the function represented by "mpa".
13257 * In other words, plug in "mpa" in the domain "map".
13258 * The result is a map that lives in the same space as "map",
13259 * except that domain space has been replaced by the domain space of "mpa".
13261 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13262 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13264 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13267 /* Compute the preimage of "set" by the function represented by "mpa".
13268 * In other words, plug in "mpa" in "set".
13270 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13271 __isl_take isl_multi_pw_aff *mpa)
13273 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13276 /* Return a copy of the equality constraints of "bset" as a matrix.
13278 __isl_give isl_mat *isl_basic_set_extract_equalities(
13279 __isl_keep isl_basic_set *bset)
13281 isl_ctx *ctx;
13282 unsigned total;
13284 if (!bset)
13285 return NULL;
13287 ctx = isl_basic_set_get_ctx(bset);
13288 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13289 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13292 /* Are the "n" "coefficients" starting at "first" of the integer division
13293 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13294 * to each other?
13295 * The "coefficient" at position 0 is the denominator.
13296 * The "coefficient" at position 1 is the constant term.
13298 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13299 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13300 unsigned first, unsigned n)
13302 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13303 return isl_bool_error;
13304 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13305 return isl_bool_error;
13306 return isl_seq_eq(bmap1->div[pos1] + first,
13307 bmap2->div[pos2] + first, n);
13310 /* Are the integer division expressions at position "pos1" in "bmap1" and
13311 * "pos2" in "bmap2" equal to each other, except that the constant terms
13312 * are different?
13314 isl_bool isl_basic_map_equal_div_expr_except_constant(
13315 __isl_keep isl_basic_map *bmap1, int pos1,
13316 __isl_keep isl_basic_map *bmap2, int pos2)
13318 isl_bool equal;
13319 unsigned total;
13321 if (!bmap1 || !bmap2)
13322 return isl_bool_error;
13323 total = isl_basic_map_total_dim(bmap1);
13324 if (total != isl_basic_map_total_dim(bmap2))
13325 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13326 "incomparable div expressions", return isl_bool_error);
13327 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13328 0, 1);
13329 if (equal < 0 || !equal)
13330 return equal;
13331 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13332 1, 1);
13333 if (equal < 0 || equal)
13334 return isl_bool_not(equal);
13335 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13336 2, total);
13339 /* Replace the numerator of the constant term of the integer division
13340 * expression at position "div" in "bmap" by "value".
13341 * The caller guarantees that this does not change the meaning
13342 * of the input.
13344 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13345 __isl_take isl_basic_map *bmap, int div, int value)
13347 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13348 return isl_basic_map_free(bmap);
13350 isl_int_set_si(bmap->div[div][1], value);
13352 return bmap;
13355 /* Is the point "inner" internal to inequality constraint "ineq"
13356 * of "bset"?
13357 * The point is considered to be internal to the inequality constraint,
13358 * if it strictly lies on the positive side of the inequality constraint,
13359 * or if it lies on the constraint and the constraint is lexico-positive.
13361 static isl_bool is_internal(__isl_keep isl_vec *inner,
13362 __isl_keep isl_basic_set *bset, int ineq)
13364 isl_ctx *ctx;
13365 int pos;
13366 unsigned total;
13368 if (!inner || !bset)
13369 return isl_bool_error;
13371 ctx = isl_basic_set_get_ctx(bset);
13372 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13373 &ctx->normalize_gcd);
13374 if (!isl_int_is_zero(ctx->normalize_gcd))
13375 return isl_int_is_nonneg(ctx->normalize_gcd);
13377 total = isl_basic_set_dim(bset, isl_dim_all);
13378 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13379 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13382 /* Tighten the inequality constraints of "bset" that are outward with respect
13383 * to the point "vec".
13384 * That is, tighten the constraints that are not satisfied by "vec".
13386 * "vec" is a point internal to some superset S of "bset" that is used
13387 * to make the subsets of S disjoint, by tightening one half of the constraints
13388 * that separate two subsets. In particular, the constraints of S
13389 * are all satisfied by "vec" and should not be tightened.
13390 * Of the internal constraints, those that have "vec" on the outside
13391 * are tightened. The shared facet is included in the adjacent subset
13392 * with the opposite constraint.
13393 * For constraints that saturate "vec", this criterion cannot be used
13394 * to determine which of the two sides should be tightened.
13395 * Instead, the sign of the first non-zero coefficient is used
13396 * to make this choice. Note that this second criterion is never used
13397 * on the constraints of S since "vec" is interior to "S".
13399 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13400 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13402 int j;
13404 bset = isl_basic_set_cow(bset);
13405 if (!bset)
13406 return NULL;
13407 for (j = 0; j < bset->n_ineq; ++j) {
13408 isl_bool internal;
13410 internal = is_internal(vec, bset, j);
13411 if (internal < 0)
13412 return isl_basic_set_free(bset);
13413 if (internal)
13414 continue;
13415 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13418 return bset;
13421 /* Replace the variables x of type "type" starting at "first" in "bmap"
13422 * by x' with x = M x' with M the matrix trans.
13423 * That is, replace the corresponding coefficients c by c M.
13425 * The transformation matrix should be a square matrix.
13427 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13428 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13429 __isl_take isl_mat *trans)
13431 unsigned pos;
13433 bmap = isl_basic_map_cow(bmap);
13434 if (!bmap || !trans)
13435 goto error;
13437 if (trans->n_row != trans->n_col)
13438 isl_die(trans->ctx, isl_error_invalid,
13439 "expecting square transformation matrix", goto error);
13440 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13441 goto error;
13443 pos = isl_basic_map_offset(bmap, type) + first;
13445 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13446 isl_mat_copy(trans)) < 0)
13447 goto error;
13448 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13449 isl_mat_copy(trans)) < 0)
13450 goto error;
13451 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13452 isl_mat_copy(trans)) < 0)
13453 goto error;
13455 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13456 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13458 isl_mat_free(trans);
13459 return bmap;
13460 error:
13461 isl_mat_free(trans);
13462 isl_basic_map_free(bmap);
13463 return NULL;
13466 /* Replace the variables x of type "type" starting at "first" in "bset"
13467 * by x' with x = M x' with M the matrix trans.
13468 * That is, replace the corresponding coefficients c by c M.
13470 * The transformation matrix should be a square matrix.
13472 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13473 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13474 __isl_take isl_mat *trans)
13476 return isl_basic_map_transform_dims(bset, type, first, trans);