isl_basic_{set,map}_print_internal: use isl_printer_print_basic_{set,map}
[isl.git] / isl_map.c
blob9e71b5b97b202d1a6e7cecb9bf8fd8e1c03cb126
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 coefficients of the variables of the given type
150 * within the sequence of coefficients of "bmap".
152 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
153 enum isl_dim_type type)
155 switch (type) {
156 case isl_dim_cst: return 0;
157 case isl_dim_param:
158 case isl_dim_in:
159 case isl_dim_out:
160 case isl_dim_div: return 1 + isl_basic_map_var_offset(bmap, type);
161 default: return 0;
165 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
166 enum isl_dim_type type)
168 return isl_basic_map_offset(bset, type);
171 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
173 return pos(map->dim, type);
176 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
177 enum isl_dim_type type)
179 return isl_basic_map_dim(bset, type);
182 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
184 return isl_basic_set_dim(bset, isl_dim_set);
187 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
189 return isl_basic_set_dim(bset, isl_dim_param);
192 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
194 return isl_basic_map_total_dim(const_bset_to_bmap(bset));
197 unsigned isl_set_n_dim(__isl_keep isl_set *set)
199 return isl_set_dim(set, isl_dim_set);
202 unsigned isl_set_n_param(__isl_keep isl_set *set)
204 return isl_set_dim(set, isl_dim_param);
207 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
209 return bmap ? bmap->dim->n_in : 0;
212 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
214 return bmap ? bmap->dim->n_out : 0;
217 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
219 return bmap ? bmap->dim->nparam : 0;
222 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
224 return bmap ? bmap->n_div : 0;
227 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
229 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
232 unsigned isl_map_n_in(__isl_keep const isl_map *map)
234 return map ? map->dim->n_in : 0;
237 unsigned isl_map_n_out(__isl_keep const isl_map *map)
239 return map ? map->dim->n_out : 0;
242 unsigned isl_map_n_param(__isl_keep const isl_map *map)
244 return map ? map->dim->nparam : 0;
247 /* Return the number of equality constraints in the description of "bmap".
248 * Return -1 on error.
250 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
252 if (!bmap)
253 return -1;
254 return bmap->n_eq;
257 /* Return the number of equality constraints in the description of "bset".
258 * Return -1 on error.
260 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
262 return isl_basic_map_n_equality(bset_to_bmap(bset));
265 /* Return the number of inequality constraints in the description of "bmap".
266 * Return -1 on error.
268 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
270 if (!bmap)
271 return -1;
272 return bmap->n_ineq;
275 /* Return the number of inequality constraints in the description of "bset".
276 * Return -1 on error.
278 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
280 return isl_basic_map_n_inequality(bset_to_bmap(bset));
283 /* Do "bmap1" and "bmap2" have the same parameters?
285 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
286 __isl_keep isl_basic_map *bmap2)
288 isl_space *space1, *space2;
290 space1 = isl_basic_map_peek_space(bmap1);
291 space2 = isl_basic_map_peek_space(bmap2);
292 return isl_space_has_equal_params(space1, space2);
295 /* Do "map1" and "map2" have the same parameters?
297 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
298 __isl_keep isl_map *map2)
300 isl_space *space1, *space2;
302 space1 = isl_map_peek_space(map1);
303 space2 = isl_map_peek_space(map2);
304 return isl_space_has_equal_params(space1, space2);
307 /* Do "map" and "set" have the same parameters?
309 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
310 __isl_keep isl_set *set)
312 return isl_map_has_equal_params(map, set_to_map(set));
315 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
316 __isl_keep isl_set *set)
318 isl_bool m;
319 if (!map || !set)
320 return isl_bool_error;
321 m = isl_map_has_equal_params(map, set_to_map(set));
322 if (m < 0 || !m)
323 return m;
324 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
325 set->dim, isl_dim_set);
328 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
329 __isl_keep isl_basic_set *bset)
331 isl_bool m;
332 if (!bmap || !bset)
333 return isl_bool_error;
334 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
335 if (m < 0 || !m)
336 return m;
337 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
338 bset->dim, isl_dim_set);
341 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
342 __isl_keep isl_set *set)
344 isl_bool m;
345 if (!map || !set)
346 return isl_bool_error;
347 m = isl_map_has_equal_params(map, set_to_map(set));
348 if (m < 0 || !m)
349 return m;
350 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
351 set->dim, isl_dim_set);
354 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
355 __isl_keep isl_basic_set *bset)
357 isl_bool m;
358 if (!bmap || !bset)
359 return isl_bool_error;
360 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
361 if (m < 0 || !m)
362 return m;
363 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
364 bset->dim, isl_dim_set);
367 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
369 return bmap ? bmap->ctx : NULL;
372 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
374 return bset ? bset->ctx : NULL;
377 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
379 return map ? map->ctx : NULL;
382 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
384 return set ? set->ctx : NULL;
387 /* Return the space of "bmap".
389 __isl_keep isl_space *isl_basic_map_peek_space(
390 __isl_keep const isl_basic_map *bmap)
392 return bmap ? bmap->dim : NULL;
395 /* Return the space of "bset".
397 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
399 return isl_basic_map_peek_space(bset_to_bmap(bset));
402 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
404 return isl_space_copy(isl_basic_map_peek_space(bmap));
407 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
409 return isl_basic_map_get_space(bset_to_bmap(bset));
412 /* Extract the divs in "bmap" as a matrix.
414 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
416 int i;
417 isl_ctx *ctx;
418 isl_mat *div;
419 unsigned total;
420 unsigned cols;
422 if (!bmap)
423 return NULL;
425 ctx = isl_basic_map_get_ctx(bmap);
426 total = isl_space_dim(bmap->dim, isl_dim_all);
427 cols = 1 + 1 + total + bmap->n_div;
428 div = isl_mat_alloc(ctx, bmap->n_div, cols);
429 if (!div)
430 return NULL;
432 for (i = 0; i < bmap->n_div; ++i)
433 isl_seq_cpy(div->row[i], bmap->div[i], cols);
435 return div;
438 /* Extract the divs in "bset" as a matrix.
440 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
442 return isl_basic_map_get_divs(bset);
445 __isl_give isl_local_space *isl_basic_map_get_local_space(
446 __isl_keep isl_basic_map *bmap)
448 isl_mat *div;
450 if (!bmap)
451 return NULL;
453 div = isl_basic_map_get_divs(bmap);
454 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
457 __isl_give isl_local_space *isl_basic_set_get_local_space(
458 __isl_keep isl_basic_set *bset)
460 return isl_basic_map_get_local_space(bset);
463 /* For each known div d = floor(f/m), add the constraints
465 * f - m d >= 0
466 * -(f-(m-1)) + m d >= 0
468 * Do not finalize the result.
470 static __isl_give isl_basic_map *add_known_div_constraints(
471 __isl_take isl_basic_map *bmap)
473 int i;
474 unsigned n_div;
476 if (!bmap)
477 return NULL;
478 n_div = isl_basic_map_dim(bmap, isl_dim_div);
479 if (n_div == 0)
480 return bmap;
481 bmap = isl_basic_map_cow(bmap);
482 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
483 if (!bmap)
484 return NULL;
485 for (i = 0; i < n_div; ++i) {
486 if (isl_int_is_zero(bmap->div[i][0]))
487 continue;
488 bmap = isl_basic_map_add_div_constraints(bmap, i);
491 return bmap;
494 __isl_give isl_basic_map *isl_basic_map_from_local_space(
495 __isl_take isl_local_space *ls)
497 int i;
498 int n_div;
499 isl_basic_map *bmap;
501 if (!ls)
502 return NULL;
504 n_div = isl_local_space_dim(ls, isl_dim_div);
505 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
506 n_div, 0, 2 * n_div);
508 for (i = 0; i < n_div; ++i)
509 if (isl_basic_map_alloc_div(bmap) < 0)
510 goto error;
512 for (i = 0; i < n_div; ++i)
513 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
514 bmap = add_known_div_constraints(bmap);
516 isl_local_space_free(ls);
517 return bmap;
518 error:
519 isl_local_space_free(ls);
520 isl_basic_map_free(bmap);
521 return NULL;
524 __isl_give isl_basic_set *isl_basic_set_from_local_space(
525 __isl_take isl_local_space *ls)
527 return isl_basic_map_from_local_space(ls);
530 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
532 return isl_space_copy(isl_map_peek_space(map));
535 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
537 if (!set)
538 return NULL;
539 return isl_space_copy(set->dim);
542 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
543 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
545 bmap = isl_basic_map_cow(bmap);
546 if (!bmap)
547 return NULL;
548 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
549 if (!bmap->dim)
550 goto error;
551 bmap = isl_basic_map_finalize(bmap);
552 return bmap;
553 error:
554 isl_basic_map_free(bmap);
555 return NULL;
558 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
559 __isl_take isl_basic_set *bset, const char *s)
561 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
564 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
565 enum isl_dim_type type)
567 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
570 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
571 enum isl_dim_type type, const char *s)
573 int i;
575 map = isl_map_cow(map);
576 if (!map)
577 return NULL;
579 map->dim = isl_space_set_tuple_name(map->dim, type, s);
580 if (!map->dim)
581 goto error;
583 for (i = 0; i < map->n; ++i) {
584 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
585 if (!map->p[i])
586 goto error;
589 return map;
590 error:
591 isl_map_free(map);
592 return NULL;
595 /* Replace the identifier of the tuple of type "type" by "id".
597 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
598 __isl_take isl_basic_map *bmap,
599 enum isl_dim_type type, __isl_take isl_id *id)
601 bmap = isl_basic_map_cow(bmap);
602 if (!bmap)
603 goto error;
604 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
605 if (!bmap->dim)
606 return isl_basic_map_free(bmap);
607 bmap = isl_basic_map_finalize(bmap);
608 return bmap;
609 error:
610 isl_id_free(id);
611 return NULL;
614 /* Replace the identifier of the tuple by "id".
616 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
617 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
619 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
622 /* Does the input or output tuple have a name?
624 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
626 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
629 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
630 enum isl_dim_type type)
632 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
635 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
636 const char *s)
638 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
639 isl_dim_set, s));
642 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
643 enum isl_dim_type type, __isl_take isl_id *id)
645 map = isl_map_cow(map);
646 if (!map)
647 goto error;
649 map->dim = isl_space_set_tuple_id(map->dim, type, id);
651 return isl_map_reset_space(map, isl_space_copy(map->dim));
652 error:
653 isl_id_free(id);
654 return NULL;
657 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
658 __isl_take isl_id *id)
660 return isl_map_set_tuple_id(set, isl_dim_set, id);
663 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
664 enum isl_dim_type type)
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
670 map->dim = isl_space_reset_tuple_id(map->dim, type);
672 return isl_map_reset_space(map, isl_space_copy(map->dim));
675 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
677 return isl_map_reset_tuple_id(set, isl_dim_set);
680 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
682 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
685 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
686 enum isl_dim_type type)
688 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
691 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
693 return isl_map_has_tuple_id(set, isl_dim_set);
696 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
698 return isl_map_get_tuple_id(set, isl_dim_set);
701 /* Does the set tuple have a name?
703 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
705 if (!set)
706 return isl_bool_error;
707 return isl_space_has_tuple_name(set->dim, isl_dim_set);
711 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
713 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
716 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
718 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
721 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
722 enum isl_dim_type type, unsigned pos)
724 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
727 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
728 enum isl_dim_type type, unsigned pos)
730 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
733 /* Does the given dimension have a name?
735 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
736 enum isl_dim_type type, unsigned pos)
738 if (!map)
739 return isl_bool_error;
740 return isl_space_has_dim_name(map->dim, type, pos);
743 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
744 enum isl_dim_type type, unsigned pos)
746 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
749 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
750 enum isl_dim_type type, unsigned pos)
752 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
755 /* Does the given dimension have a name?
757 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
758 enum isl_dim_type type, unsigned pos)
760 if (!set)
761 return isl_bool_error;
762 return isl_space_has_dim_name(set->dim, type, pos);
765 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
766 __isl_take isl_basic_map *bmap,
767 enum isl_dim_type type, unsigned pos, const char *s)
769 bmap = isl_basic_map_cow(bmap);
770 if (!bmap)
771 return NULL;
772 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
773 if (!bmap->dim)
774 goto error;
775 return isl_basic_map_finalize(bmap);
776 error:
777 isl_basic_map_free(bmap);
778 return NULL;
781 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
782 enum isl_dim_type type, unsigned pos, const char *s)
784 int i;
786 map = isl_map_cow(map);
787 if (!map)
788 return NULL;
790 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
791 if (!map->dim)
792 goto error;
794 for (i = 0; i < map->n; ++i) {
795 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
796 if (!map->p[i])
797 goto error;
800 return map;
801 error:
802 isl_map_free(map);
803 return NULL;
806 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
807 __isl_take isl_basic_set *bset,
808 enum isl_dim_type type, unsigned pos, const char *s)
810 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
811 type, pos, s));
814 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
815 enum isl_dim_type type, unsigned pos, const char *s)
817 return set_from_map(isl_map_set_dim_name(set_to_map(set),
818 type, pos, s));
821 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
822 enum isl_dim_type type, unsigned pos)
824 if (!bmap)
825 return isl_bool_error;
826 return isl_space_has_dim_id(bmap->dim, type, pos);
829 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
830 enum isl_dim_type type, unsigned pos)
832 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
835 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
836 enum isl_dim_type type, unsigned pos)
838 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
841 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
842 enum isl_dim_type type, unsigned pos)
844 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
847 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
848 enum isl_dim_type type, unsigned pos)
850 return isl_map_has_dim_id(set, type, pos);
853 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
854 enum isl_dim_type type, unsigned pos)
856 return isl_map_get_dim_id(set, type, pos);
859 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
860 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
862 map = isl_map_cow(map);
863 if (!map)
864 goto error;
866 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
868 return isl_map_reset_space(map, isl_space_copy(map->dim));
869 error:
870 isl_id_free(id);
871 return NULL;
874 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
875 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
877 return isl_map_set_dim_id(set, type, pos, id);
880 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
881 __isl_keep isl_id *id)
883 if (!map)
884 return -1;
885 return isl_space_find_dim_by_id(map->dim, type, id);
888 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
889 __isl_keep isl_id *id)
891 return isl_map_find_dim_by_id(set, type, id);
894 /* Return the position of the dimension of the given type and name
895 * in "bmap".
896 * Return -1 if no such dimension can be found.
898 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
899 enum isl_dim_type type, const char *name)
901 if (!bmap)
902 return -1;
903 return isl_space_find_dim_by_name(bmap->dim, type, name);
906 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
907 const char *name)
909 if (!map)
910 return -1;
911 return isl_space_find_dim_by_name(map->dim, type, name);
914 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
915 const char *name)
917 return isl_map_find_dim_by_name(set, type, name);
920 /* Check whether equality i of bset is a pure stride constraint
921 * on a single dimension, i.e., of the form
923 * v = k e
925 * with k a constant and e an existentially quantified variable.
927 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
929 unsigned nparam;
930 unsigned d;
931 unsigned n_div;
932 int pos1;
933 int pos2;
935 if (!bset)
936 return isl_bool_error;
938 if (!isl_int_is_zero(bset->eq[i][0]))
939 return isl_bool_false;
941 nparam = isl_basic_set_dim(bset, isl_dim_param);
942 d = isl_basic_set_dim(bset, isl_dim_set);
943 n_div = isl_basic_set_dim(bset, isl_dim_div);
945 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
946 return isl_bool_false;
947 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
948 if (pos1 == -1)
949 return isl_bool_false;
950 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
951 d - pos1 - 1) != -1)
952 return isl_bool_false;
954 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
955 if (pos2 == -1)
956 return isl_bool_false;
957 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
958 n_div - pos2 - 1) != -1)
959 return isl_bool_false;
960 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
961 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
962 return isl_bool_false;
964 return isl_bool_true;
967 /* Reset the user pointer on all identifiers of parameters and tuples
968 * of the space of "map".
970 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
972 isl_space *space;
974 space = isl_map_get_space(map);
975 space = isl_space_reset_user(space);
976 map = isl_map_reset_space(map, space);
978 return map;
981 /* Reset the user pointer on all identifiers of parameters and tuples
982 * of the space of "set".
984 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
986 return isl_map_reset_user(set);
989 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
991 if (!bmap)
992 return isl_bool_error;
993 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
996 /* Has "map" been marked as a rational map?
997 * In particular, have all basic maps in "map" been marked this way?
998 * An empty map is not considered to be rational.
999 * Maps where only some of the basic maps are marked rational
1000 * are not allowed.
1002 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
1004 int i;
1005 isl_bool rational;
1007 if (!map)
1008 return isl_bool_error;
1009 if (map->n == 0)
1010 return isl_bool_false;
1011 rational = isl_basic_map_is_rational(map->p[0]);
1012 if (rational < 0)
1013 return rational;
1014 for (i = 1; i < map->n; ++i) {
1015 isl_bool rational_i;
1017 rational_i = isl_basic_map_is_rational(map->p[i]);
1018 if (rational_i < 0)
1019 return rational_i;
1020 if (rational != rational_i)
1021 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
1022 "mixed rational and integer basic maps "
1023 "not supported", return isl_bool_error);
1026 return rational;
1029 /* Has "set" been marked as a rational set?
1030 * In particular, have all basic set in "set" been marked this way?
1031 * An empty set is not considered to be rational.
1032 * Sets where only some of the basic sets are marked rational
1033 * are not allowed.
1035 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1037 return isl_map_is_rational(set);
1040 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1042 return isl_basic_map_is_rational(bset);
1045 /* Does "bmap" contain any rational points?
1047 * If "bmap" has an equality for each dimension, equating the dimension
1048 * to an integer constant, then it has no rational points, even if it
1049 * is marked as rational.
1051 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1053 isl_bool has_rational = isl_bool_true;
1054 unsigned total;
1056 if (!bmap)
1057 return isl_bool_error;
1058 if (isl_basic_map_plain_is_empty(bmap))
1059 return isl_bool_false;
1060 if (!isl_basic_map_is_rational(bmap))
1061 return isl_bool_false;
1062 bmap = isl_basic_map_copy(bmap);
1063 bmap = isl_basic_map_implicit_equalities(bmap);
1064 if (!bmap)
1065 return isl_bool_error;
1066 total = isl_basic_map_total_dim(bmap);
1067 if (bmap->n_eq == total) {
1068 int i, j;
1069 for (i = 0; i < bmap->n_eq; ++i) {
1070 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1071 if (j < 0)
1072 break;
1073 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1074 !isl_int_is_negone(bmap->eq[i][1 + j]))
1075 break;
1076 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1077 total - j - 1);
1078 if (j >= 0)
1079 break;
1081 if (i == bmap->n_eq)
1082 has_rational = isl_bool_false;
1084 isl_basic_map_free(bmap);
1086 return has_rational;
1089 /* Does "map" contain any rational points?
1091 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1093 int i;
1094 isl_bool has_rational;
1096 if (!map)
1097 return isl_bool_error;
1098 for (i = 0; i < map->n; ++i) {
1099 has_rational = isl_basic_map_has_rational(map->p[i]);
1100 if (has_rational < 0 || has_rational)
1101 return has_rational;
1103 return isl_bool_false;
1106 /* Does "set" contain any rational points?
1108 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1110 return isl_map_has_rational(set);
1113 /* Is this basic set a parameter domain?
1115 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1117 if (!bset)
1118 return isl_bool_error;
1119 return isl_space_is_params(bset->dim);
1122 /* Is this set a parameter domain?
1124 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1126 if (!set)
1127 return isl_bool_error;
1128 return isl_space_is_params(set->dim);
1131 /* Is this map actually a parameter domain?
1132 * Users should never call this function. Outside of isl,
1133 * a map can never be a parameter domain.
1135 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1137 if (!map)
1138 return isl_bool_error;
1139 return isl_space_is_params(map->dim);
1142 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1143 __isl_take isl_basic_map *bmap, unsigned extra,
1144 unsigned n_eq, unsigned n_ineq)
1146 int i;
1147 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1149 bmap->ctx = ctx;
1150 isl_ctx_ref(ctx);
1152 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1153 if (isl_blk_is_error(bmap->block))
1154 goto error;
1156 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1157 if ((n_ineq + n_eq) && !bmap->ineq)
1158 goto error;
1160 if (extra == 0) {
1161 bmap->block2 = isl_blk_empty();
1162 bmap->div = NULL;
1163 } else {
1164 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1165 if (isl_blk_is_error(bmap->block2))
1166 goto error;
1168 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1169 if (!bmap->div)
1170 goto error;
1173 for (i = 0; i < n_ineq + n_eq; ++i)
1174 bmap->ineq[i] = bmap->block.data + i * row_size;
1176 for (i = 0; i < extra; ++i)
1177 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1179 bmap->ref = 1;
1180 bmap->flags = 0;
1181 bmap->c_size = n_eq + n_ineq;
1182 bmap->eq = bmap->ineq + n_ineq;
1183 bmap->extra = extra;
1184 bmap->n_eq = 0;
1185 bmap->n_ineq = 0;
1186 bmap->n_div = 0;
1187 bmap->sample = NULL;
1189 return bmap;
1190 error:
1191 isl_basic_map_free(bmap);
1192 return NULL;
1195 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1196 unsigned nparam, unsigned dim, unsigned extra,
1197 unsigned n_eq, unsigned n_ineq)
1199 struct isl_basic_map *bmap;
1200 isl_space *space;
1202 space = isl_space_set_alloc(ctx, nparam, dim);
1203 if (!space)
1204 return NULL;
1206 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1207 return bset_from_bmap(bmap);
1210 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1211 unsigned extra, unsigned n_eq, unsigned n_ineq)
1213 struct isl_basic_map *bmap;
1214 if (!dim)
1215 return NULL;
1216 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1217 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1218 return bset_from_bmap(bmap);
1219 error:
1220 isl_space_free(dim);
1221 return NULL;
1224 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1225 unsigned extra, unsigned n_eq, unsigned n_ineq)
1227 struct isl_basic_map *bmap;
1229 if (!space)
1230 return NULL;
1231 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1232 if (!bmap)
1233 goto error;
1234 bmap->dim = space;
1236 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1237 error:
1238 isl_space_free(space);
1239 return NULL;
1242 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1243 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1244 unsigned n_eq, unsigned n_ineq)
1246 struct isl_basic_map *bmap;
1247 isl_space *dim;
1249 dim = isl_space_alloc(ctx, nparam, in, out);
1250 if (!dim)
1251 return NULL;
1253 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1254 return bmap;
1257 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1258 __isl_keep isl_basic_map *src)
1260 int i;
1261 unsigned total = isl_basic_map_total_dim(src);
1263 if (!dst)
1264 return NULL;
1266 for (i = 0; i < src->n_eq; ++i) {
1267 int j = isl_basic_map_alloc_equality(dst);
1268 if (j < 0)
1269 return isl_basic_map_free(dst);
1270 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1273 for (i = 0; i < src->n_ineq; ++i) {
1274 int j = isl_basic_map_alloc_inequality(dst);
1275 if (j < 0)
1276 return isl_basic_map_free(dst);
1277 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1280 for (i = 0; i < src->n_div; ++i) {
1281 int j = isl_basic_map_alloc_div(dst);
1282 if (j < 0)
1283 return isl_basic_map_free(dst);
1284 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1286 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1287 return dst;
1290 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1292 struct isl_basic_map *dup;
1294 if (!bmap)
1295 return NULL;
1296 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1297 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1298 dup = dup_constraints(dup, bmap);
1299 if (!dup)
1300 return NULL;
1301 dup->flags = bmap->flags;
1302 dup->sample = isl_vec_copy(bmap->sample);
1303 return dup;
1306 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1308 struct isl_basic_map *dup;
1310 dup = isl_basic_map_dup(bset_to_bmap(bset));
1311 return bset_from_bmap(dup);
1314 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1316 if (!bset)
1317 return NULL;
1319 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1320 bset->ref++;
1321 return bset;
1323 return isl_basic_set_dup(bset);
1326 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1328 if (!set)
1329 return NULL;
1331 set->ref++;
1332 return set;
1335 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1337 if (!bmap)
1338 return NULL;
1340 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1341 bmap->ref++;
1342 return bmap;
1344 bmap = isl_basic_map_dup(bmap);
1345 if (bmap)
1346 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1347 return bmap;
1350 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1352 if (!map)
1353 return NULL;
1355 map->ref++;
1356 return map;
1359 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1361 if (!bmap)
1362 return NULL;
1364 if (--bmap->ref > 0)
1365 return NULL;
1367 isl_ctx_deref(bmap->ctx);
1368 free(bmap->div);
1369 isl_blk_free(bmap->ctx, bmap->block2);
1370 free(bmap->ineq);
1371 isl_blk_free(bmap->ctx, bmap->block);
1372 isl_vec_free(bmap->sample);
1373 isl_space_free(bmap->dim);
1374 free(bmap);
1376 return NULL;
1379 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1381 return isl_basic_map_free(bset_to_bmap(bset));
1384 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1386 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1389 /* Check that "bset" does not involve any parameters.
1391 isl_stat isl_basic_set_check_no_params(__isl_keep isl_basic_set *bset)
1393 if (!bset)
1394 return isl_stat_error;
1395 if (isl_basic_set_dim(bset, isl_dim_param) != 0)
1396 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1397 "basic set should not have any parameters",
1398 return isl_stat_error);
1399 return isl_stat_ok;
1402 /* Check that "bset" does not involve any local variables.
1404 isl_stat isl_basic_set_check_no_locals(__isl_keep isl_basic_set *bset)
1406 if (!bset)
1407 return isl_stat_error;
1408 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
1409 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
1410 "basic set should not have any local variables",
1411 return isl_stat_error);
1412 return isl_stat_ok;
1415 /* Check that "map" has only named parameters, reporting an error
1416 * if it does not.
1418 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1420 return isl_space_check_named_params(isl_map_peek_space(map));
1423 /* Check that "bmap" has only named parameters, reporting an error
1424 * if it does not.
1426 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1428 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1431 /* Check that "bmap1" and "bmap2" have the same parameters,
1432 * reporting an error if they do not.
1434 static isl_stat isl_basic_map_check_equal_params(
1435 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1437 isl_bool match;
1439 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1440 if (match < 0)
1441 return isl_stat_error;
1442 if (!match)
1443 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1444 "parameters don't match", return isl_stat_error);
1445 return isl_stat_ok;
1448 __isl_give isl_map *isl_map_align_params_map_map_and(
1449 __isl_take isl_map *map1, __isl_take isl_map *map2,
1450 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1451 __isl_take isl_map *map2))
1453 if (!map1 || !map2)
1454 goto error;
1455 if (isl_map_has_equal_params(map1, map2))
1456 return fn(map1, map2);
1457 if (isl_map_check_named_params(map1) < 0)
1458 goto error;
1459 if (isl_map_check_named_params(map2) < 0)
1460 goto error;
1461 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1462 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1463 return fn(map1, map2);
1464 error:
1465 isl_map_free(map1);
1466 isl_map_free(map2);
1467 return NULL;
1470 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1471 __isl_keep isl_map *map2,
1472 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1474 isl_bool r;
1476 if (!map1 || !map2)
1477 return isl_bool_error;
1478 if (isl_map_has_equal_params(map1, map2))
1479 return fn(map1, map2);
1480 if (isl_map_check_named_params(map1) < 0)
1481 return isl_bool_error;
1482 if (isl_map_check_named_params(map2) < 0)
1483 return isl_bool_error;
1484 map1 = isl_map_copy(map1);
1485 map2 = isl_map_copy(map2);
1486 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1487 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1488 r = fn(map1, map2);
1489 isl_map_free(map1);
1490 isl_map_free(map2);
1491 return r;
1494 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1496 struct isl_ctx *ctx;
1497 if (!bmap)
1498 return -1;
1499 ctx = bmap->ctx;
1500 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1501 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1502 return -1);
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1505 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1506 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1507 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1508 isl_int *t;
1509 int j = isl_basic_map_alloc_inequality(bmap);
1510 if (j < 0)
1511 return -1;
1512 t = bmap->ineq[j];
1513 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1514 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1515 bmap->eq[-1] = t;
1516 bmap->n_eq++;
1517 bmap->n_ineq--;
1518 bmap->eq--;
1519 return 0;
1521 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1522 bmap->extra - bmap->n_div);
1523 return bmap->n_eq++;
1526 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1528 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1531 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1533 if (!bmap)
1534 return -1;
1535 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1536 bmap->n_eq -= n;
1537 return 0;
1540 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1542 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1545 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1547 isl_int *t;
1548 if (!bmap)
1549 return -1;
1550 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1552 if (pos != bmap->n_eq - 1) {
1553 t = bmap->eq[pos];
1554 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1555 bmap->eq[bmap->n_eq - 1] = t;
1557 bmap->n_eq--;
1558 return 0;
1561 /* Turn inequality "pos" of "bmap" into an equality.
1563 * In particular, we move the inequality in front of the equalities
1564 * and move the last inequality in the position of the moved inequality.
1565 * Note that isl_tab_make_equalities_explicit depends on this particular
1566 * change in the ordering of the constraints.
1568 void isl_basic_map_inequality_to_equality(
1569 struct isl_basic_map *bmap, unsigned pos)
1571 isl_int *t;
1573 t = bmap->ineq[pos];
1574 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1575 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1576 bmap->eq[-1] = t;
1577 bmap->n_eq++;
1578 bmap->n_ineq--;
1579 bmap->eq--;
1580 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1581 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1582 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1583 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1586 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1588 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1591 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1593 struct isl_ctx *ctx;
1594 if (!bmap)
1595 return -1;
1596 ctx = bmap->ctx;
1597 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1598 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1599 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1600 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1601 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1602 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1603 1 + isl_basic_map_total_dim(bmap),
1604 bmap->extra - bmap->n_div);
1605 return bmap->n_ineq++;
1608 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1610 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1613 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1615 if (!bmap)
1616 return -1;
1617 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1618 bmap->n_ineq -= n;
1619 return 0;
1622 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1624 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1627 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1629 isl_int *t;
1630 if (!bmap)
1631 return -1;
1632 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1634 if (pos != bmap->n_ineq - 1) {
1635 t = bmap->ineq[pos];
1636 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1637 bmap->ineq[bmap->n_ineq - 1] = t;
1638 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1640 bmap->n_ineq--;
1641 return 0;
1644 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1646 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1649 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1650 isl_int *eq)
1652 isl_bool empty;
1653 int k;
1655 empty = isl_basic_map_plain_is_empty(bmap);
1656 if (empty < 0)
1657 return isl_basic_map_free(bmap);
1658 if (empty)
1659 return bmap;
1661 bmap = isl_basic_map_cow(bmap);
1662 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1663 if (!bmap)
1664 return NULL;
1665 k = isl_basic_map_alloc_equality(bmap);
1666 if (k < 0)
1667 goto error;
1668 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1669 return bmap;
1670 error:
1671 isl_basic_map_free(bmap);
1672 return NULL;
1675 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1676 isl_int *eq)
1678 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1681 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1682 isl_int *ineq)
1684 int k;
1686 bmap = isl_basic_map_cow(bmap);
1687 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1688 if (!bmap)
1689 return NULL;
1690 k = isl_basic_map_alloc_inequality(bmap);
1691 if (k < 0)
1692 goto error;
1693 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1694 return bmap;
1695 error:
1696 isl_basic_map_free(bmap);
1697 return NULL;
1700 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1701 isl_int *ineq)
1703 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1706 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1708 if (!bmap)
1709 return -1;
1710 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1711 isl_seq_clr(bmap->div[bmap->n_div] +
1712 1 + 1 + isl_basic_map_total_dim(bmap),
1713 bmap->extra - bmap->n_div);
1714 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1715 return bmap->n_div++;
1718 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1720 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1723 #undef TYPE
1724 #define TYPE isl_basic_map
1725 #include "check_type_range_templ.c"
1727 /* Check that there are "n" dimensions of type "type" starting at "first"
1728 * in "bset".
1730 isl_stat isl_basic_set_check_range(__isl_keep isl_basic_set *bset,
1731 enum isl_dim_type type, unsigned first, unsigned n)
1733 return isl_basic_map_check_range(bset_to_bmap(bset),
1734 type, first, n);
1737 /* Insert an extra integer division, prescribed by "div", to "bmap"
1738 * at (integer division) position "pos".
1740 * The integer division is first added at the end and then moved
1741 * into the right position.
1743 __isl_give isl_basic_map *isl_basic_map_insert_div(
1744 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1746 int i, k;
1748 bmap = isl_basic_map_cow(bmap);
1749 if (!bmap || !div)
1750 return isl_basic_map_free(bmap);
1752 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1753 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1754 "unexpected size", return isl_basic_map_free(bmap));
1755 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1756 return isl_basic_map_free(bmap);
1758 bmap = isl_basic_map_extend_space(bmap,
1759 isl_basic_map_get_space(bmap), 1, 0, 2);
1760 k = isl_basic_map_alloc_div(bmap);
1761 if (k < 0)
1762 return isl_basic_map_free(bmap);
1763 isl_seq_cpy(bmap->div[k], div->el, div->size);
1764 isl_int_set_si(bmap->div[k][div->size], 0);
1766 for (i = k; i > pos; --i)
1767 bmap = isl_basic_map_swap_div(bmap, i, i - 1);
1769 return bmap;
1772 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1774 if (!bmap)
1775 return isl_stat_error;
1776 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1777 bmap->n_div -= n;
1778 return isl_stat_ok;
1781 static __isl_give isl_basic_map *add_constraints(
1782 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1783 unsigned i_pos, unsigned o_pos)
1785 unsigned total, n_param, n_in, o_in, n_out, o_out, n_div;
1786 isl_ctx *ctx;
1787 isl_space *space;
1788 struct isl_dim_map *dim_map;
1790 space = isl_basic_map_peek_space(bmap2);
1791 if (!bmap1 || !space)
1792 goto error;
1794 total = isl_basic_map_dim(bmap1, isl_dim_all);
1795 n_param = isl_basic_map_dim(bmap2, isl_dim_param);
1796 n_in = isl_basic_map_dim(bmap2, isl_dim_in);
1797 o_in = isl_basic_map_offset(bmap1, isl_dim_in) - 1 + i_pos;
1798 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
1799 o_out = isl_basic_map_offset(bmap1, isl_dim_out) - 1 + o_pos;
1800 n_div = isl_basic_map_dim(bmap2, isl_dim_div);
1801 ctx = isl_basic_map_get_ctx(bmap1);
1802 dim_map = isl_dim_map_alloc(ctx, total + n_div);
1803 isl_dim_map_dim_range(dim_map, space, isl_dim_param, 0, n_param, 0);
1804 isl_dim_map_dim_range(dim_map, space, isl_dim_in, 0, n_in, o_in);
1805 isl_dim_map_dim_range(dim_map, space, isl_dim_out, 0, n_out, o_out);
1806 isl_dim_map_div(dim_map, bmap2, total);
1808 return isl_basic_map_add_constraints_dim_map(bmap1, bmap2, dim_map);
1809 error:
1810 isl_basic_map_free(bmap1);
1811 isl_basic_map_free(bmap2);
1812 return NULL;
1815 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1816 struct isl_basic_set *bset2, unsigned pos)
1818 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1819 bset_to_bmap(bset2), 0, pos));
1822 __isl_give isl_basic_map *isl_basic_map_extend_space(
1823 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1824 unsigned extra, unsigned n_eq, unsigned n_ineq)
1826 struct isl_basic_map *ext;
1827 unsigned flags;
1828 int dims_ok;
1830 if (!space)
1831 goto error;
1833 if (!base)
1834 goto error;
1836 dims_ok = isl_space_is_equal(base->dim, space) &&
1837 base->extra >= base->n_div + extra;
1839 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1840 room_for_ineq(base, n_ineq)) {
1841 isl_space_free(space);
1842 return base;
1845 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1846 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1847 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1848 extra += base->extra;
1849 n_eq += base->n_eq;
1850 n_ineq += base->n_ineq;
1852 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1853 space = NULL;
1854 if (!ext)
1855 goto error;
1857 if (dims_ok)
1858 ext->sample = isl_vec_copy(base->sample);
1859 flags = base->flags;
1860 ext = add_constraints(ext, base, 0, 0);
1861 if (ext) {
1862 ext->flags = flags;
1863 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1866 return ext;
1868 error:
1869 isl_space_free(space);
1870 isl_basic_map_free(base);
1871 return NULL;
1874 __isl_give isl_basic_set *isl_basic_set_extend_space(
1875 __isl_take isl_basic_set *base,
1876 __isl_take isl_space *dim, unsigned extra,
1877 unsigned n_eq, unsigned n_ineq)
1879 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1880 dim, extra, n_eq, n_ineq));
1883 struct isl_basic_map *isl_basic_map_extend_constraints(
1884 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1886 if (!base)
1887 return NULL;
1888 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1889 0, n_eq, n_ineq);
1892 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1893 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1894 unsigned n_eq, unsigned n_ineq)
1896 struct isl_basic_map *bmap;
1897 isl_space *dim;
1899 if (!base)
1900 return NULL;
1901 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1902 if (!dim)
1903 goto error;
1905 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1906 return bmap;
1907 error:
1908 isl_basic_map_free(base);
1909 return NULL;
1912 struct isl_basic_set *isl_basic_set_extend_constraints(
1913 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1915 isl_basic_map *bmap = bset_to_bmap(base);
1916 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1917 return bset_from_bmap(bmap);
1920 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1922 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1925 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1927 if (!bmap)
1928 return NULL;
1930 if (bmap->ref > 1) {
1931 bmap->ref--;
1932 bmap = isl_basic_map_dup(bmap);
1934 if (bmap) {
1935 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1936 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1938 return bmap;
1941 /* Clear all cached information in "map", either because it is about
1942 * to be modified or because it is being freed.
1943 * Always return the same pointer that is passed in.
1944 * This is needed for the use in isl_map_free.
1946 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1948 isl_basic_map_free(map->cached_simple_hull[0]);
1949 isl_basic_map_free(map->cached_simple_hull[1]);
1950 map->cached_simple_hull[0] = NULL;
1951 map->cached_simple_hull[1] = NULL;
1952 return map;
1955 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1957 return isl_map_cow(set);
1960 /* Return an isl_map that is equal to "map" and that has only
1961 * a single reference.
1963 * If the original input already has only one reference, then
1964 * simply return it, but clear all cached information, since
1965 * it may be rendered invalid by the operations that will be
1966 * performed on the result.
1968 * Otherwise, create a duplicate (without any cached information).
1970 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1972 if (!map)
1973 return NULL;
1975 if (map->ref == 1)
1976 return clear_caches(map);
1977 map->ref--;
1978 return isl_map_dup(map);
1981 static void swap_vars(struct isl_blk blk, isl_int *a,
1982 unsigned a_len, unsigned b_len)
1984 isl_seq_cpy(blk.data, a+a_len, b_len);
1985 isl_seq_cpy(blk.data+b_len, a, a_len);
1986 isl_seq_cpy(a, blk.data, b_len+a_len);
1989 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1990 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1992 int i;
1993 struct isl_blk blk;
1995 if (isl_basic_map_check_range(bmap, isl_dim_all, pos - 1, n1 + n2) < 0)
1996 goto error;
1998 if (n1 == 0 || n2 == 0)
1999 return bmap;
2001 bmap = isl_basic_map_cow(bmap);
2002 if (!bmap)
2003 return NULL;
2005 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2006 if (isl_blk_is_error(blk))
2007 goto error;
2009 for (i = 0; i < bmap->n_eq; ++i)
2010 swap_vars(blk,
2011 bmap->eq[i] + pos, n1, n2);
2013 for (i = 0; i < bmap->n_ineq; ++i)
2014 swap_vars(blk,
2015 bmap->ineq[i] + pos, n1, n2);
2017 for (i = 0; i < bmap->n_div; ++i)
2018 swap_vars(blk,
2019 bmap->div[i]+1 + pos, n1, n2);
2021 isl_blk_free(bmap->ctx, blk);
2023 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2024 bmap = isl_basic_map_gauss(bmap, NULL);
2025 return isl_basic_map_finalize(bmap);
2026 error:
2027 isl_basic_map_free(bmap);
2028 return NULL;
2031 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2032 __isl_take isl_basic_map *bmap)
2034 int i = 0;
2035 unsigned total;
2036 if (!bmap)
2037 goto error;
2038 total = isl_basic_map_total_dim(bmap);
2039 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2040 return isl_basic_map_free(bmap);
2041 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2042 if (bmap->n_eq > 0)
2043 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2044 else {
2045 i = isl_basic_map_alloc_equality(bmap);
2046 if (i < 0)
2047 goto error;
2049 isl_int_set_si(bmap->eq[i][0], 1);
2050 isl_seq_clr(bmap->eq[i]+1, total);
2051 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2052 isl_vec_free(bmap->sample);
2053 bmap->sample = NULL;
2054 return isl_basic_map_finalize(bmap);
2055 error:
2056 isl_basic_map_free(bmap);
2057 return NULL;
2060 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2061 __isl_take isl_basic_set *bset)
2063 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2066 __isl_give isl_basic_map *isl_basic_map_set_rational(
2067 __isl_take isl_basic_map *bmap)
2069 if (!bmap)
2070 return NULL;
2072 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2073 return bmap;
2075 bmap = isl_basic_map_cow(bmap);
2076 if (!bmap)
2077 return NULL;
2079 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2081 return isl_basic_map_finalize(bmap);
2084 __isl_give isl_basic_set *isl_basic_set_set_rational(
2085 __isl_take isl_basic_set *bset)
2087 return isl_basic_map_set_rational(bset);
2090 __isl_give isl_basic_set *isl_basic_set_set_integral(
2091 __isl_take isl_basic_set *bset)
2093 if (!bset)
2094 return NULL;
2096 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2097 return bset;
2099 bset = isl_basic_set_cow(bset);
2100 if (!bset)
2101 return NULL;
2103 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2105 return isl_basic_set_finalize(bset);
2108 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2110 int i;
2112 map = isl_map_cow(map);
2113 if (!map)
2114 return NULL;
2115 for (i = 0; i < map->n; ++i) {
2116 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2117 if (!map->p[i])
2118 goto error;
2120 return map;
2121 error:
2122 isl_map_free(map);
2123 return NULL;
2126 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2128 return isl_map_set_rational(set);
2131 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2132 * of "bmap").
2134 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2136 isl_int *t = bmap->div[a];
2137 bmap->div[a] = bmap->div[b];
2138 bmap->div[b] = t;
2141 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2142 * div definitions accordingly.
2144 __isl_give isl_basic_map *isl_basic_map_swap_div(__isl_take isl_basic_map *bmap,
2145 int a, int b)
2147 int i;
2148 unsigned off;
2150 if (!bmap)
2151 return NULL;
2153 off = isl_space_dim(bmap->dim, isl_dim_all);
2154 swap_div(bmap, a, b);
2156 for (i = 0; i < bmap->n_eq; ++i)
2157 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2159 for (i = 0; i < bmap->n_ineq; ++i)
2160 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2162 for (i = 0; i < bmap->n_div; ++i)
2163 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2164 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2166 return bmap;
2169 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2171 isl_seq_cpy(c, c + n, rem);
2172 isl_seq_clr(c + rem, n);
2175 /* Drop n dimensions starting at first.
2177 * In principle, this frees up some extra variables as the number
2178 * of columns remains constant, but we would have to extend
2179 * the div array too as the number of rows in this array is assumed
2180 * to be equal to extra.
2182 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2183 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2185 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2188 /* Move "n" divs starting at "first" to the end of the list of divs.
2190 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2191 unsigned first, unsigned n)
2193 isl_int **div;
2194 int i;
2196 if (first + n == bmap->n_div)
2197 return bmap;
2199 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2200 if (!div)
2201 goto error;
2202 for (i = 0; i < n; ++i)
2203 div[i] = bmap->div[first + i];
2204 for (i = 0; i < bmap->n_div - first - n; ++i)
2205 bmap->div[first + i] = bmap->div[first + n + i];
2206 for (i = 0; i < n; ++i)
2207 bmap->div[bmap->n_div - n + i] = div[i];
2208 free(div);
2209 return bmap;
2210 error:
2211 isl_basic_map_free(bmap);
2212 return NULL;
2215 #undef TYPE
2216 #define TYPE isl_map
2217 static
2218 #include "check_type_range_templ.c"
2220 /* Check that there are "n" dimensions of type "type" starting at "first"
2221 * in "set".
2223 static isl_stat isl_set_check_range(__isl_keep isl_set *set,
2224 enum isl_dim_type type, unsigned first, unsigned n)
2226 return isl_map_check_range(set_to_map(set), type, first, n);
2229 /* Drop "n" dimensions of type "type" starting at "first".
2230 * Perform the core computation, without cowing or
2231 * simplifying and finalizing the result.
2233 * In principle, this frees up some extra variables as the number
2234 * of columns remains constant, but we would have to extend
2235 * the div array too as the number of rows in this array is assumed
2236 * to be equal to extra.
2238 __isl_give isl_basic_map *isl_basic_map_drop_core(
2239 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2240 unsigned first, unsigned n)
2242 int i;
2243 unsigned offset;
2244 unsigned left;
2246 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2247 return isl_basic_map_free(bmap);
2249 offset = isl_basic_map_offset(bmap, type) + first;
2250 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2251 for (i = 0; i < bmap->n_eq; ++i)
2252 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2254 for (i = 0; i < bmap->n_ineq; ++i)
2255 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2257 for (i = 0; i < bmap->n_div; ++i)
2258 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2260 if (type == isl_dim_div) {
2261 bmap = move_divs_last(bmap, first, n);
2262 if (!bmap)
2263 return NULL;
2264 if (isl_basic_map_free_div(bmap, n) < 0)
2265 return isl_basic_map_free(bmap);
2266 } else
2267 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2268 if (!bmap->dim)
2269 return isl_basic_map_free(bmap);
2271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2272 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2273 return bmap;
2276 /* Drop "n" dimensions of type "type" starting at "first".
2278 * In principle, this frees up some extra variables as the number
2279 * of columns remains constant, but we would have to extend
2280 * the div array too as the number of rows in this array is assumed
2281 * to be equal to extra.
2283 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2284 enum isl_dim_type type, unsigned first, unsigned n)
2286 if (!bmap)
2287 return NULL;
2288 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2289 return bmap;
2291 bmap = isl_basic_map_cow(bmap);
2292 if (!bmap)
2293 return NULL;
2295 bmap = isl_basic_map_drop_core(bmap, type, first, n);
2297 bmap = isl_basic_map_simplify(bmap);
2298 return isl_basic_map_finalize(bmap);
2301 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2302 enum isl_dim_type type, unsigned first, unsigned n)
2304 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2305 type, first, n));
2308 /* No longer consider "map" to be normalized.
2310 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2312 if (!map)
2313 return NULL;
2314 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2315 return map;
2318 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2319 enum isl_dim_type type, unsigned first, unsigned n)
2321 int i;
2323 if (isl_map_check_range(map, type, first, n) < 0)
2324 return isl_map_free(map);
2326 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2327 return map;
2328 map = isl_map_cow(map);
2329 if (!map)
2330 goto error;
2331 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2332 if (!map->dim)
2333 goto error;
2335 for (i = 0; i < map->n; ++i) {
2336 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2337 if (!map->p[i])
2338 goto error;
2340 map = isl_map_unmark_normalized(map);
2342 return map;
2343 error:
2344 isl_map_free(map);
2345 return NULL;
2348 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2349 enum isl_dim_type type, unsigned first, unsigned n)
2351 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2354 /* Drop the integer division at position "div", which is assumed
2355 * not to appear in any of the constraints or
2356 * in any of the other integer divisions.
2358 * Since the integer division is redundant, there is no need to cow.
2360 __isl_give isl_basic_map *isl_basic_map_drop_div(
2361 __isl_take isl_basic_map *bmap, unsigned div)
2363 return isl_basic_map_drop_core(bmap, isl_dim_div, div, 1);
2366 /* Eliminate the specified n dimensions starting at first from the
2367 * constraints, without removing the dimensions from the space.
2368 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2370 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2371 enum isl_dim_type type, unsigned first, unsigned n)
2373 int i;
2375 if (n == 0)
2376 return map;
2378 if (isl_map_check_range(map, type, first, n) < 0)
2379 return isl_map_free(map);
2381 map = isl_map_cow(map);
2382 if (!map)
2383 return NULL;
2385 for (i = 0; i < map->n; ++i) {
2386 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2387 if (!map->p[i])
2388 goto error;
2390 return map;
2391 error:
2392 isl_map_free(map);
2393 return NULL;
2396 /* Eliminate the specified n dimensions starting at first from the
2397 * constraints, without removing the dimensions from the space.
2398 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2400 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2401 enum isl_dim_type type, unsigned first, unsigned n)
2403 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2406 /* Eliminate the specified n dimensions starting at first from the
2407 * constraints, without removing the dimensions from the space.
2408 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2410 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2411 unsigned first, unsigned n)
2413 return isl_set_eliminate(set, isl_dim_set, first, n);
2416 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2417 __isl_take isl_basic_map *bmap)
2419 if (!bmap)
2420 return NULL;
2421 bmap = isl_basic_map_eliminate_vars(bmap,
2422 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2423 if (!bmap)
2424 return NULL;
2425 bmap->n_div = 0;
2426 return isl_basic_map_finalize(bmap);
2429 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2430 __isl_take isl_basic_set *bset)
2432 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2435 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2437 int i;
2439 if (!map)
2440 return NULL;
2441 if (map->n == 0)
2442 return map;
2444 map = isl_map_cow(map);
2445 if (!map)
2446 return NULL;
2448 for (i = 0; i < map->n; ++i) {
2449 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2450 if (!map->p[i])
2451 goto error;
2453 return map;
2454 error:
2455 isl_map_free(map);
2456 return NULL;
2459 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2461 return isl_map_remove_divs(set);
2464 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2465 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2466 unsigned first, unsigned n)
2468 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2469 return isl_basic_map_free(bmap);
2470 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2471 return bmap;
2472 bmap = isl_basic_map_eliminate_vars(bmap,
2473 isl_basic_map_offset(bmap, type) - 1 + first, n);
2474 if (!bmap)
2475 return bmap;
2476 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2477 return bmap;
2478 bmap = isl_basic_map_drop(bmap, type, first, n);
2479 return bmap;
2482 /* Return true if the definition of the given div (recursively) involves
2483 * any of the given variables.
2485 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2486 unsigned first, unsigned n)
2488 int i;
2489 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2491 if (isl_int_is_zero(bmap->div[div][0]))
2492 return isl_bool_false;
2493 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2494 return isl_bool_true;
2496 for (i = bmap->n_div - 1; i >= 0; --i) {
2497 isl_bool involves;
2499 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2500 continue;
2501 involves = div_involves_vars(bmap, i, first, n);
2502 if (involves < 0 || involves)
2503 return involves;
2506 return isl_bool_false;
2509 /* Try and add a lower and/or upper bound on "div" to "bmap"
2510 * based on inequality "i".
2511 * "total" is the total number of variables (excluding the divs).
2512 * "v" is a temporary object that can be used during the calculations.
2513 * If "lb" is set, then a lower bound should be constructed.
2514 * If "ub" is set, then an upper bound should be constructed.
2516 * The calling function has already checked that the inequality does not
2517 * reference "div", but we still need to check that the inequality is
2518 * of the right form. We'll consider the case where we want to construct
2519 * a lower bound. The construction of upper bounds is similar.
2521 * Let "div" be of the form
2523 * q = floor((a + f(x))/d)
2525 * We essentially check if constraint "i" is of the form
2527 * b + f(x) >= 0
2529 * so that we can use it to derive a lower bound on "div".
2530 * However, we allow a slightly more general form
2532 * b + g(x) >= 0
2534 * with the condition that the coefficients of g(x) - f(x) are all
2535 * divisible by d.
2536 * Rewriting this constraint as
2538 * 0 >= -b - g(x)
2540 * adding a + f(x) to both sides and dividing by d, we obtain
2542 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2544 * Taking the floor on both sides, we obtain
2546 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2548 * or
2550 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2552 * In the case of an upper bound, we construct the constraint
2554 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2557 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2558 __isl_take isl_basic_map *bmap, int div, int i,
2559 unsigned total, isl_int v, int lb, int ub)
2561 int j;
2563 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2564 if (lb) {
2565 isl_int_sub(v, bmap->ineq[i][1 + j],
2566 bmap->div[div][1 + 1 + j]);
2567 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2569 if (ub) {
2570 isl_int_add(v, bmap->ineq[i][1 + j],
2571 bmap->div[div][1 + 1 + j]);
2572 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2575 if (!lb && !ub)
2576 return bmap;
2578 bmap = isl_basic_map_cow(bmap);
2579 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2580 if (lb) {
2581 int k = isl_basic_map_alloc_inequality(bmap);
2582 if (k < 0)
2583 goto error;
2584 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2585 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2586 bmap->div[div][1 + j]);
2587 isl_int_cdiv_q(bmap->ineq[k][j],
2588 bmap->ineq[k][j], bmap->div[div][0]);
2590 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2592 if (ub) {
2593 int k = isl_basic_map_alloc_inequality(bmap);
2594 if (k < 0)
2595 goto error;
2596 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2597 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2598 bmap->div[div][1 + j]);
2599 isl_int_fdiv_q(bmap->ineq[k][j],
2600 bmap->ineq[k][j], bmap->div[div][0]);
2602 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2605 return bmap;
2606 error:
2607 isl_basic_map_free(bmap);
2608 return NULL;
2611 /* This function is called right before "div" is eliminated from "bmap"
2612 * using Fourier-Motzkin.
2613 * Look through the constraints of "bmap" for constraints on the argument
2614 * of the integer division and use them to construct constraints on the
2615 * integer division itself. These constraints can then be combined
2616 * during the Fourier-Motzkin elimination.
2617 * Note that it is only useful to introduce lower bounds on "div"
2618 * if "bmap" already contains upper bounds on "div" as the newly
2619 * introduce lower bounds can then be combined with the pre-existing
2620 * upper bounds. Similarly for upper bounds.
2621 * We therefore first check if "bmap" contains any lower and/or upper bounds
2622 * on "div".
2624 * It is interesting to note that the introduction of these constraints
2625 * can indeed lead to more accurate results, even when compared to
2626 * deriving constraints on the argument of "div" from constraints on "div".
2627 * Consider, for example, the set
2629 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2631 * The second constraint can be rewritten as
2633 * 2 * [(-i-2j+3)/4] + k >= 0
2635 * from which we can derive
2637 * -i - 2j + 3 >= -2k
2639 * or
2641 * i + 2j <= 3 + 2k
2643 * Combined with the first constraint, we obtain
2645 * -3 <= 3 + 2k or k >= -3
2647 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2648 * the first constraint, we obtain
2650 * [(i + 2j)/4] >= [-3/4] = -1
2652 * Combining this constraint with the second constraint, we obtain
2654 * k >= -2
2656 static __isl_give isl_basic_map *insert_bounds_on_div(
2657 __isl_take isl_basic_map *bmap, int div)
2659 int i;
2660 int check_lb, check_ub;
2661 isl_int v;
2662 unsigned total;
2664 if (!bmap)
2665 return NULL;
2667 if (isl_int_is_zero(bmap->div[div][0]))
2668 return bmap;
2670 total = isl_space_dim(bmap->dim, isl_dim_all);
2672 check_lb = 0;
2673 check_ub = 0;
2674 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2675 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2676 if (s > 0)
2677 check_ub = 1;
2678 if (s < 0)
2679 check_lb = 1;
2682 if (!check_lb && !check_ub)
2683 return bmap;
2685 isl_int_init(v);
2687 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2688 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2689 continue;
2691 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2692 check_lb, check_ub);
2695 isl_int_clear(v);
2697 return bmap;
2700 /* Remove all divs (recursively) involving any of the given dimensions
2701 * in their definitions.
2703 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2704 __isl_take isl_basic_map *bmap,
2705 enum isl_dim_type type, unsigned first, unsigned n)
2707 int i;
2709 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2710 return isl_basic_map_free(bmap);
2711 first += isl_basic_map_offset(bmap, type);
2713 for (i = bmap->n_div - 1; i >= 0; --i) {
2714 isl_bool involves;
2716 involves = div_involves_vars(bmap, i, first, n);
2717 if (involves < 0)
2718 return isl_basic_map_free(bmap);
2719 if (!involves)
2720 continue;
2721 bmap = insert_bounds_on_div(bmap, i);
2722 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2723 if (!bmap)
2724 return NULL;
2725 i = bmap->n_div;
2728 return bmap;
2731 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2732 __isl_take isl_basic_set *bset,
2733 enum isl_dim_type type, unsigned first, unsigned n)
2735 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2738 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2739 enum isl_dim_type type, unsigned first, unsigned n)
2741 int i;
2743 if (!map)
2744 return NULL;
2745 if (map->n == 0)
2746 return map;
2748 map = isl_map_cow(map);
2749 if (!map)
2750 return NULL;
2752 for (i = 0; i < map->n; ++i) {
2753 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2754 type, first, n);
2755 if (!map->p[i])
2756 goto error;
2758 return map;
2759 error:
2760 isl_map_free(map);
2761 return NULL;
2764 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2765 enum isl_dim_type type, unsigned first, unsigned n)
2767 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2768 type, first, n));
2771 /* Does the description of "bmap" depend on the specified dimensions?
2772 * We also check whether the dimensions appear in any of the div definitions.
2773 * In principle there is no need for this check. If the dimensions appear
2774 * in a div definition, they also appear in the defining constraints of that
2775 * div.
2777 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2778 enum isl_dim_type type, unsigned first, unsigned n)
2780 int i;
2782 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2783 return isl_bool_error;
2785 first += isl_basic_map_offset(bmap, type);
2786 for (i = 0; i < bmap->n_eq; ++i)
2787 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2788 return isl_bool_true;
2789 for (i = 0; i < bmap->n_ineq; ++i)
2790 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2791 return isl_bool_true;
2792 for (i = 0; i < bmap->n_div; ++i) {
2793 if (isl_int_is_zero(bmap->div[i][0]))
2794 continue;
2795 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2796 return isl_bool_true;
2799 return isl_bool_false;
2802 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2803 enum isl_dim_type type, unsigned first, unsigned n)
2805 int i;
2807 if (isl_map_check_range(map, type, first, n) < 0)
2808 return isl_bool_error;
2810 for (i = 0; i < map->n; ++i) {
2811 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2812 type, first, n);
2813 if (involves < 0 || involves)
2814 return involves;
2817 return isl_bool_false;
2820 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2821 enum isl_dim_type type, unsigned first, unsigned n)
2823 return isl_basic_map_involves_dims(bset, type, first, n);
2826 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2827 enum isl_dim_type type, unsigned first, unsigned n)
2829 return isl_map_involves_dims(set, type, first, n);
2832 /* Drop all constraints in bmap that involve any of the dimensions
2833 * first to first+n-1.
2834 * This function only performs the actual removal of constraints.
2836 * This function should not call finalize since it is used by
2837 * remove_redundant_divs, which in turn is called by isl_basic_map_finalize.
2839 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2840 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2842 int i;
2844 if (n == 0)
2845 return bmap;
2847 bmap = isl_basic_map_cow(bmap);
2849 if (!bmap)
2850 return NULL;
2852 for (i = bmap->n_eq - 1; i >= 0; --i) {
2853 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2854 continue;
2855 isl_basic_map_drop_equality(bmap, i);
2858 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2859 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2860 continue;
2861 isl_basic_map_drop_inequality(bmap, i);
2864 return bmap;
2867 /* Drop all constraints in bset that involve any of the dimensions
2868 * first to first+n-1.
2869 * This function only performs the actual removal of constraints.
2871 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2872 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2874 return isl_basic_map_drop_constraints_involving(bset, first, n);
2877 /* Drop all constraints in bmap that do not involve any of the dimensions
2878 * first to first + n - 1 of the given type.
2880 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2881 __isl_take isl_basic_map *bmap,
2882 enum isl_dim_type type, unsigned first, unsigned n)
2884 int i;
2886 if (n == 0) {
2887 isl_space *space = isl_basic_map_get_space(bmap);
2888 isl_basic_map_free(bmap);
2889 return isl_basic_map_universe(space);
2891 bmap = isl_basic_map_cow(bmap);
2892 if (!bmap)
2893 return NULL;
2895 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2896 return isl_basic_map_free(bmap);
2898 first += isl_basic_map_offset(bmap, type) - 1;
2900 for (i = bmap->n_eq - 1; i >= 0; --i) {
2901 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2902 continue;
2903 isl_basic_map_drop_equality(bmap, i);
2906 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2907 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2908 continue;
2909 isl_basic_map_drop_inequality(bmap, i);
2912 bmap = isl_basic_map_add_known_div_constraints(bmap);
2913 return bmap;
2916 /* Drop all constraints in bset that do not involve any of the dimensions
2917 * first to first + n - 1 of the given type.
2919 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2920 __isl_take isl_basic_set *bset,
2921 enum isl_dim_type type, unsigned first, unsigned n)
2923 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2924 type, first, n);
2927 /* Drop all constraints in bmap that involve any of the dimensions
2928 * first to first + n - 1 of the given type.
2930 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2931 __isl_take isl_basic_map *bmap,
2932 enum isl_dim_type type, unsigned first, unsigned n)
2934 if (!bmap)
2935 return NULL;
2936 if (n == 0)
2937 return bmap;
2939 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2940 return isl_basic_map_free(bmap);
2942 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2943 first += isl_basic_map_offset(bmap, type) - 1;
2944 bmap = isl_basic_map_drop_constraints_involving(bmap, first, n);
2945 bmap = isl_basic_map_add_known_div_constraints(bmap);
2946 return bmap;
2949 /* Drop all constraints in bset that involve any of the dimensions
2950 * first to first + n - 1 of the given type.
2952 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2953 __isl_take isl_basic_set *bset,
2954 enum isl_dim_type type, unsigned first, unsigned n)
2956 return isl_basic_map_drop_constraints_involving_dims(bset,
2957 type, first, n);
2960 /* Drop constraints from "map" by applying "drop" to each basic map.
2962 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2963 enum isl_dim_type type, unsigned first, unsigned n,
2964 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2965 enum isl_dim_type type, unsigned first, unsigned n))
2967 int i;
2969 if (isl_map_check_range(map, type, first, n) < 0)
2970 return isl_map_free(map);
2972 map = isl_map_cow(map);
2973 if (!map)
2974 return NULL;
2976 for (i = 0; i < map->n; ++i) {
2977 map->p[i] = drop(map->p[i], type, first, n);
2978 if (!map->p[i])
2979 return isl_map_free(map);
2982 if (map->n > 1)
2983 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2985 return map;
2988 /* Drop all constraints in map that involve any of the dimensions
2989 * first to first + n - 1 of the given type.
2991 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2992 __isl_take isl_map *map,
2993 enum isl_dim_type type, unsigned first, unsigned n)
2995 if (n == 0)
2996 return map;
2997 return drop_constraints(map, type, first, n,
2998 &isl_basic_map_drop_constraints_involving_dims);
3001 /* Drop all constraints in "map" that do not involve any of the dimensions
3002 * first to first + n - 1 of the given type.
3004 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3005 __isl_take isl_map *map,
3006 enum isl_dim_type type, unsigned first, unsigned n)
3008 if (n == 0) {
3009 isl_space *space = isl_map_get_space(map);
3010 isl_map_free(map);
3011 return isl_map_universe(space);
3013 return drop_constraints(map, type, first, n,
3014 &isl_basic_map_drop_constraints_not_involving_dims);
3017 /* Drop all constraints in set that involve any of the dimensions
3018 * first to first + n - 1 of the given type.
3020 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3021 __isl_take isl_set *set,
3022 enum isl_dim_type type, unsigned first, unsigned n)
3024 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3027 /* Drop all constraints in "set" that do not involve any of the dimensions
3028 * first to first + n - 1 of the given type.
3030 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3031 __isl_take isl_set *set,
3032 enum isl_dim_type type, unsigned first, unsigned n)
3034 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3037 /* Does local variable "div" of "bmap" have a complete explicit representation?
3038 * Having a complete explicit representation requires not only
3039 * an explicit representation, but also that all local variables
3040 * that appear in this explicit representation in turn have
3041 * a complete explicit representation.
3043 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3045 int i;
3046 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3047 isl_bool marked;
3049 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3050 if (marked < 0 || marked)
3051 return isl_bool_not(marked);
3053 for (i = bmap->n_div - 1; i >= 0; --i) {
3054 isl_bool known;
3056 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3057 continue;
3058 known = isl_basic_map_div_is_known(bmap, i);
3059 if (known < 0 || !known)
3060 return known;
3063 return isl_bool_true;
3066 /* Remove all divs that are unknown or defined in terms of unknown divs.
3068 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3069 __isl_take isl_basic_map *bmap)
3071 int i;
3073 if (!bmap)
3074 return NULL;
3076 for (i = bmap->n_div - 1; i >= 0; --i) {
3077 if (isl_basic_map_div_is_known(bmap, i))
3078 continue;
3079 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3080 if (!bmap)
3081 return NULL;
3082 i = bmap->n_div;
3085 return bmap;
3088 /* Remove all divs that are unknown or defined in terms of unknown divs.
3090 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3091 __isl_take isl_basic_set *bset)
3093 return isl_basic_map_remove_unknown_divs(bset);
3096 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3098 int i;
3100 if (!map)
3101 return NULL;
3102 if (map->n == 0)
3103 return map;
3105 map = isl_map_cow(map);
3106 if (!map)
3107 return NULL;
3109 for (i = 0; i < map->n; ++i) {
3110 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3111 if (!map->p[i])
3112 goto error;
3114 return map;
3115 error:
3116 isl_map_free(map);
3117 return NULL;
3120 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3122 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3125 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3126 __isl_take isl_basic_set *bset,
3127 enum isl_dim_type type, unsigned first, unsigned n)
3129 isl_basic_map *bmap = bset_to_bmap(bset);
3130 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3131 return bset_from_bmap(bmap);
3134 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3135 enum isl_dim_type type, unsigned first, unsigned n)
3137 int i;
3139 if (n == 0)
3140 return map;
3142 map = isl_map_cow(map);
3143 if (isl_map_check_range(map, type, first, n) < 0)
3144 return isl_map_free(map);
3146 for (i = 0; i < map->n; ++i) {
3147 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3148 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3149 if (!map->p[i])
3150 goto error;
3152 map = isl_map_drop(map, type, first, n);
3153 return map;
3154 error:
3155 isl_map_free(map);
3156 return NULL;
3159 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3160 enum isl_dim_type type, unsigned first, unsigned n)
3162 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3163 type, first, n));
3166 /* Project out n inputs starting at first using Fourier-Motzkin */
3167 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3168 unsigned first, unsigned n)
3170 return isl_map_remove_dims(map, isl_dim_in, first, n);
3173 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3174 FILE *out, int indent)
3176 isl_printer *p;
3178 if (!bset) {
3179 fprintf(out, "null basic set\n");
3180 return;
3183 fprintf(out, "%*s", indent, "");
3184 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3185 bset->ref, bset->dim->nparam, bset->dim->n_out,
3186 bset->extra, bset->flags);
3188 p = isl_printer_to_file(isl_basic_set_get_ctx(bset), out);
3189 p = isl_printer_set_dump(p, 1);
3190 p = isl_printer_set_indent(p, indent);
3191 p = isl_printer_start_line(p);
3192 p = isl_printer_print_basic_set(p, bset);
3193 p = isl_printer_end_line(p);
3194 isl_printer_free(p);
3197 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3198 FILE *out, int indent)
3200 isl_printer *p;
3202 if (!bmap) {
3203 fprintf(out, "null basic map\n");
3204 return;
3207 fprintf(out, "%*s", indent, "");
3208 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3209 "flags: %x, n_name: %d\n",
3210 bmap->ref,
3211 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3212 bmap->extra, bmap->flags, bmap->dim->n_id);
3214 p = isl_printer_to_file(isl_basic_map_get_ctx(bmap), out);
3215 p = isl_printer_set_dump(p, 1);
3216 p = isl_printer_set_indent(p, indent);
3217 p = isl_printer_start_line(p);
3218 p = isl_printer_print_basic_map(p, bmap);
3219 p = isl_printer_end_line(p);
3220 isl_printer_free(p);
3223 __isl_give isl_basic_map *isl_inequality_negate(__isl_take isl_basic_map *bmap,
3224 unsigned pos)
3226 unsigned total;
3228 if (!bmap)
3229 return NULL;
3230 total = isl_basic_map_total_dim(bmap);
3231 if (pos >= bmap->n_ineq)
3232 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3233 "invalid position", return isl_basic_map_free(bmap));
3234 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3235 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3236 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3237 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3238 return bmap;
3241 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3242 unsigned flags)
3244 if (isl_space_check_is_set(space) < 0)
3245 goto error;
3246 return isl_map_alloc_space(space, n, flags);
3247 error:
3248 isl_space_free(space);
3249 return NULL;
3252 /* Make sure "map" has room for at least "n" more basic maps.
3254 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3256 int i;
3257 struct isl_map *grown = NULL;
3259 if (!map)
3260 return NULL;
3261 isl_assert(map->ctx, n >= 0, goto error);
3262 if (map->n + n <= map->size)
3263 return map;
3264 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3265 if (!grown)
3266 goto error;
3267 for (i = 0; i < map->n; ++i) {
3268 grown->p[i] = isl_basic_map_copy(map->p[i]);
3269 if (!grown->p[i])
3270 goto error;
3271 grown->n++;
3273 isl_map_free(map);
3274 return grown;
3275 error:
3276 isl_map_free(grown);
3277 isl_map_free(map);
3278 return NULL;
3281 /* Make sure "set" has room for at least "n" more basic sets.
3283 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3285 return set_from_map(isl_map_grow(set_to_map(set), n));
3288 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3290 return isl_map_from_basic_map(bset);
3293 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3295 struct isl_map *map;
3297 if (!bmap)
3298 return NULL;
3300 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3301 return isl_map_add_basic_map(map, bmap);
3304 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3305 __isl_take isl_basic_set *bset)
3307 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3308 bset_to_bmap(bset)));
3311 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3313 return isl_map_free(set);
3316 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3318 int i;
3320 if (!set) {
3321 fprintf(out, "null set\n");
3322 return;
3325 fprintf(out, "%*s", indent, "");
3326 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3327 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3328 set->flags);
3329 for (i = 0; i < set->n; ++i) {
3330 fprintf(out, "%*s", indent, "");
3331 fprintf(out, "basic set %d:\n", i);
3332 isl_basic_set_print_internal(set->p[i], out, indent+4);
3336 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3338 int i;
3340 if (!map) {
3341 fprintf(out, "null map\n");
3342 return;
3345 fprintf(out, "%*s", indent, "");
3346 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3347 "flags: %x, n_name: %d\n",
3348 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3349 map->dim->n_out, map->flags, map->dim->n_id);
3350 for (i = 0; i < map->n; ++i) {
3351 fprintf(out, "%*s", indent, "");
3352 fprintf(out, "basic map %d:\n", i);
3353 isl_basic_map_print_internal(map->p[i], out, indent+4);
3357 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3358 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3360 struct isl_basic_map *bmap_domain;
3362 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3363 goto error;
3365 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3366 isl_assert(bset->ctx,
3367 isl_basic_map_compatible_domain(bmap, bset), goto error);
3369 bmap = isl_basic_map_cow(bmap);
3370 if (!bmap)
3371 goto error;
3372 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3373 bset->n_div, bset->n_eq, bset->n_ineq);
3374 bmap_domain = isl_basic_map_from_domain(bset);
3375 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3377 bmap = isl_basic_map_simplify(bmap);
3378 return isl_basic_map_finalize(bmap);
3379 error:
3380 isl_basic_map_free(bmap);
3381 isl_basic_set_free(bset);
3382 return NULL;
3385 /* Check that the space of "bset" is the same as that of the range of "bmap".
3387 static isl_stat isl_basic_map_check_compatible_range(
3388 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3390 isl_bool ok;
3392 ok = isl_basic_map_compatible_range(bmap, bset);
3393 if (ok < 0)
3394 return isl_stat_error;
3395 if (!ok)
3396 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3397 "incompatible spaces", return isl_stat_error);
3399 return isl_stat_ok;
3402 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3403 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3405 struct isl_basic_map *bmap_range;
3407 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3408 goto error;
3410 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3411 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3412 goto error;
3414 if (isl_basic_set_plain_is_universe(bset)) {
3415 isl_basic_set_free(bset);
3416 return bmap;
3419 bmap = isl_basic_map_cow(bmap);
3420 if (!bmap)
3421 goto error;
3422 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3423 bset->n_div, bset->n_eq, bset->n_ineq);
3424 bmap_range = bset_to_bmap(bset);
3425 bmap = add_constraints(bmap, bmap_range, 0, 0);
3427 bmap = isl_basic_map_simplify(bmap);
3428 return isl_basic_map_finalize(bmap);
3429 error:
3430 isl_basic_map_free(bmap);
3431 isl_basic_set_free(bset);
3432 return NULL;
3435 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3436 __isl_keep isl_vec *vec)
3438 int i;
3439 unsigned total;
3440 isl_int s;
3442 if (!bmap || !vec)
3443 return isl_bool_error;
3445 total = 1 + isl_basic_map_total_dim(bmap);
3446 if (total != vec->size)
3447 return isl_bool_false;
3449 isl_int_init(s);
3451 for (i = 0; i < bmap->n_eq; ++i) {
3452 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3453 if (!isl_int_is_zero(s)) {
3454 isl_int_clear(s);
3455 return isl_bool_false;
3459 for (i = 0; i < bmap->n_ineq; ++i) {
3460 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3461 if (isl_int_is_neg(s)) {
3462 isl_int_clear(s);
3463 return isl_bool_false;
3467 isl_int_clear(s);
3469 return isl_bool_true;
3472 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3473 __isl_keep isl_vec *vec)
3475 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3478 __isl_give isl_basic_map *isl_basic_map_intersect(
3479 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3481 struct isl_vec *sample = NULL;
3483 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3484 goto error;
3485 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3486 isl_space_dim(bmap1->dim, isl_dim_param) &&
3487 isl_space_dim(bmap2->dim, isl_dim_all) !=
3488 isl_space_dim(bmap2->dim, isl_dim_param))
3489 return isl_basic_map_intersect(bmap2, bmap1);
3491 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3492 isl_space_dim(bmap2->dim, isl_dim_param))
3493 isl_assert(bmap1->ctx,
3494 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3496 if (isl_basic_map_plain_is_empty(bmap1)) {
3497 isl_basic_map_free(bmap2);
3498 return bmap1;
3500 if (isl_basic_map_plain_is_empty(bmap2)) {
3501 isl_basic_map_free(bmap1);
3502 return bmap2;
3505 if (bmap1->sample &&
3506 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3507 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3508 sample = isl_vec_copy(bmap1->sample);
3509 else if (bmap2->sample &&
3510 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3511 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3512 sample = isl_vec_copy(bmap2->sample);
3514 bmap1 = isl_basic_map_cow(bmap1);
3515 if (!bmap1)
3516 goto error;
3517 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3518 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3519 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3521 if (!bmap1)
3522 isl_vec_free(sample);
3523 else if (sample) {
3524 isl_vec_free(bmap1->sample);
3525 bmap1->sample = sample;
3528 bmap1 = isl_basic_map_simplify(bmap1);
3529 return isl_basic_map_finalize(bmap1);
3530 error:
3531 if (sample)
3532 isl_vec_free(sample);
3533 isl_basic_map_free(bmap1);
3534 isl_basic_map_free(bmap2);
3535 return NULL;
3538 struct isl_basic_set *isl_basic_set_intersect(
3539 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3541 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3542 bset_to_bmap(bset2)));
3545 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3546 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3548 return isl_basic_set_intersect(bset1, bset2);
3551 /* Special case of isl_map_intersect, where both map1 and map2
3552 * are convex, without any divs and such that either map1 or map2
3553 * contains a single constraint. This constraint is then simply
3554 * added to the other map.
3556 static __isl_give isl_map *map_intersect_add_constraint(
3557 __isl_take isl_map *map1, __isl_take isl_map *map2)
3559 isl_assert(map1->ctx, map1->n == 1, goto error);
3560 isl_assert(map2->ctx, map1->n == 1, goto error);
3561 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3562 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3564 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3565 return isl_map_intersect(map2, map1);
3567 map1 = isl_map_cow(map1);
3568 if (!map1)
3569 goto error;
3570 if (isl_map_plain_is_empty(map1)) {
3571 isl_map_free(map2);
3572 return map1;
3574 if (map2->p[0]->n_eq == 1)
3575 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3576 else
3577 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3578 map2->p[0]->ineq[0]);
3580 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3581 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3582 if (!map1->p[0])
3583 goto error;
3585 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3586 isl_basic_map_free(map1->p[0]);
3587 map1->n = 0;
3590 isl_map_free(map2);
3592 map1 = isl_map_unmark_normalized(map1);
3593 return map1;
3594 error:
3595 isl_map_free(map1);
3596 isl_map_free(map2);
3597 return NULL;
3600 /* map2 may be either a parameter domain or a map living in the same
3601 * space as map1.
3603 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3604 __isl_take isl_map *map2)
3606 unsigned flags = 0;
3607 isl_bool equal;
3608 isl_map *result;
3609 int i, j;
3611 if (!map1 || !map2)
3612 goto error;
3614 if ((isl_map_plain_is_empty(map1) ||
3615 isl_map_plain_is_universe(map2)) &&
3616 isl_space_is_equal(map1->dim, map2->dim)) {
3617 isl_map_free(map2);
3618 return map1;
3620 if ((isl_map_plain_is_empty(map2) ||
3621 isl_map_plain_is_universe(map1)) &&
3622 isl_space_is_equal(map1->dim, map2->dim)) {
3623 isl_map_free(map1);
3624 return map2;
3627 if (map1->n == 1 && map2->n == 1 &&
3628 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3629 isl_space_is_equal(map1->dim, map2->dim) &&
3630 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3631 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3632 return map_intersect_add_constraint(map1, map2);
3634 equal = isl_map_plain_is_equal(map1, map2);
3635 if (equal < 0)
3636 goto error;
3637 if (equal) {
3638 isl_map_free(map2);
3639 return map1;
3642 if (isl_space_dim(map2->dim, isl_dim_all) !=
3643 isl_space_dim(map2->dim, isl_dim_param))
3644 isl_assert(map1->ctx,
3645 isl_space_is_equal(map1->dim, map2->dim), goto error);
3647 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3648 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3649 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3651 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3652 map1->n * map2->n, flags);
3653 if (!result)
3654 goto error;
3655 for (i = 0; i < map1->n; ++i)
3656 for (j = 0; j < map2->n; ++j) {
3657 struct isl_basic_map *part;
3658 part = isl_basic_map_intersect(
3659 isl_basic_map_copy(map1->p[i]),
3660 isl_basic_map_copy(map2->p[j]));
3661 if (isl_basic_map_is_empty(part) < 0)
3662 part = isl_basic_map_free(part);
3663 result = isl_map_add_basic_map(result, part);
3664 if (!result)
3665 goto error;
3667 isl_map_free(map1);
3668 isl_map_free(map2);
3669 return result;
3670 error:
3671 isl_map_free(map1);
3672 isl_map_free(map2);
3673 return NULL;
3676 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3677 __isl_take isl_map *map2)
3679 if (!map1 || !map2)
3680 goto error;
3681 if (!isl_space_is_equal(map1->dim, map2->dim))
3682 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3683 "spaces don't match", goto error);
3684 return map_intersect_internal(map1, map2);
3685 error:
3686 isl_map_free(map1);
3687 isl_map_free(map2);
3688 return NULL;
3691 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3692 __isl_take isl_map *map2)
3694 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3697 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3699 return set_from_map(isl_map_intersect(set_to_map(set1),
3700 set_to_map(set2)));
3703 /* map_intersect_internal accepts intersections
3704 * with parameter domains, so we can just call that function.
3706 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3707 __isl_take isl_set *params)
3709 return map_intersect_internal(map, params);
3712 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3713 __isl_take isl_map *map2)
3715 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3718 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3719 __isl_take isl_set *params)
3721 return isl_map_intersect_params(set, params);
3724 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3726 isl_space *space;
3727 unsigned pos, n1, n2;
3729 if (!bmap)
3730 return NULL;
3731 bmap = isl_basic_map_cow(bmap);
3732 if (!bmap)
3733 return NULL;
3734 space = isl_space_reverse(isl_space_copy(bmap->dim));
3735 pos = isl_basic_map_offset(bmap, isl_dim_in);
3736 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3737 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3738 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3739 return isl_basic_map_reset_space(bmap, space);
3742 static __isl_give isl_basic_map *basic_map_space_reset(
3743 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3745 isl_space *space;
3747 if (!bmap)
3748 return NULL;
3749 if (!isl_space_is_named_or_nested(bmap->dim, type))
3750 return bmap;
3752 space = isl_basic_map_get_space(bmap);
3753 space = isl_space_reset(space, type);
3754 bmap = isl_basic_map_reset_space(bmap, space);
3755 return bmap;
3758 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3759 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3760 unsigned pos, unsigned n)
3762 isl_bool rational, is_empty;
3763 isl_space *res_space;
3764 struct isl_basic_map *res;
3765 struct isl_dim_map *dim_map;
3766 unsigned total, off;
3767 enum isl_dim_type t;
3769 if (n == 0)
3770 return basic_map_space_reset(bmap, type);
3772 is_empty = isl_basic_map_plain_is_empty(bmap);
3773 if (is_empty < 0)
3774 return isl_basic_map_free(bmap);
3775 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3776 type, pos, n);
3777 if (!res_space)
3778 return isl_basic_map_free(bmap);
3779 if (is_empty) {
3780 isl_basic_map_free(bmap);
3781 return isl_basic_map_empty(res_space);
3784 total = isl_basic_map_total_dim(bmap) + n;
3785 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3786 off = 0;
3787 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3788 if (t != type) {
3789 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3790 } else {
3791 unsigned size = isl_basic_map_dim(bmap, t);
3792 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3793 0, pos, off);
3794 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3795 pos, size - pos, off + pos + n);
3797 off += isl_space_dim(res_space, t);
3799 isl_dim_map_div(dim_map, bmap, off);
3801 res = isl_basic_map_alloc_space(res_space,
3802 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3803 rational = isl_basic_map_is_rational(bmap);
3804 if (rational < 0)
3805 res = isl_basic_map_free(res);
3806 if (rational)
3807 res = isl_basic_map_set_rational(res);
3808 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3809 return isl_basic_map_finalize(res);
3812 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3813 __isl_take isl_basic_set *bset,
3814 enum isl_dim_type type, unsigned pos, unsigned n)
3816 return isl_basic_map_insert_dims(bset, type, pos, n);
3819 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3820 enum isl_dim_type type, unsigned n)
3822 if (!bmap)
3823 return NULL;
3824 return isl_basic_map_insert_dims(bmap, type,
3825 isl_basic_map_dim(bmap, type), n);
3828 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3829 enum isl_dim_type type, unsigned n)
3831 if (!bset)
3832 return NULL;
3833 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3834 return isl_basic_map_add_dims(bset, type, n);
3835 error:
3836 isl_basic_set_free(bset);
3837 return NULL;
3840 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3841 enum isl_dim_type type)
3843 isl_space *space;
3845 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3846 return map;
3848 space = isl_map_get_space(map);
3849 space = isl_space_reset(space, type);
3850 map = isl_map_reset_space(map, space);
3851 return map;
3854 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3855 enum isl_dim_type type, unsigned pos, unsigned n)
3857 int i;
3859 if (n == 0)
3860 return map_space_reset(map, type);
3862 map = isl_map_cow(map);
3863 if (!map)
3864 return NULL;
3866 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3867 if (!map->dim)
3868 goto error;
3870 for (i = 0; i < map->n; ++i) {
3871 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3872 if (!map->p[i])
3873 goto error;
3876 return map;
3877 error:
3878 isl_map_free(map);
3879 return NULL;
3882 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3883 enum isl_dim_type type, unsigned pos, unsigned n)
3885 return isl_map_insert_dims(set, type, pos, n);
3888 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3889 enum isl_dim_type type, unsigned n)
3891 if (!map)
3892 return NULL;
3893 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3896 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3897 enum isl_dim_type type, unsigned n)
3899 if (!set)
3900 return NULL;
3901 isl_assert(set->ctx, type != isl_dim_in, goto error);
3902 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3903 error:
3904 isl_set_free(set);
3905 return NULL;
3908 __isl_give isl_basic_map *isl_basic_map_move_dims(
3909 __isl_take isl_basic_map *bmap,
3910 enum isl_dim_type dst_type, unsigned dst_pos,
3911 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3913 struct isl_dim_map *dim_map;
3914 struct isl_basic_map *res;
3915 enum isl_dim_type t;
3916 unsigned total, off;
3918 if (!bmap)
3919 return NULL;
3920 if (n == 0) {
3921 bmap = isl_basic_map_reset(bmap, src_type);
3922 bmap = isl_basic_map_reset(bmap, dst_type);
3923 return bmap;
3926 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3927 return isl_basic_map_free(bmap);
3929 if (dst_type == src_type && dst_pos == src_pos)
3930 return bmap;
3932 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3934 if (pos(bmap->dim, dst_type) + dst_pos ==
3935 pos(bmap->dim, src_type) + src_pos +
3936 ((src_type < dst_type) ? n : 0)) {
3937 bmap = isl_basic_map_cow(bmap);
3938 if (!bmap)
3939 return NULL;
3941 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3942 src_type, src_pos, n);
3943 if (!bmap->dim)
3944 goto error;
3946 bmap = isl_basic_map_finalize(bmap);
3948 return bmap;
3951 total = isl_basic_map_total_dim(bmap);
3952 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3954 off = 0;
3955 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3956 unsigned size = isl_space_dim(bmap->dim, t);
3957 if (t == dst_type) {
3958 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3959 0, dst_pos, off);
3960 off += dst_pos;
3961 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3962 src_pos, n, off);
3963 off += n;
3964 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3965 dst_pos, size - dst_pos, off);
3966 off += size - dst_pos;
3967 } else if (t == src_type) {
3968 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3969 0, src_pos, off);
3970 off += src_pos;
3971 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3972 src_pos + n, size - src_pos - n, off);
3973 off += size - src_pos - n;
3974 } else {
3975 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3976 off += size;
3979 isl_dim_map_div(dim_map, bmap, off);
3981 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3982 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3983 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3984 if (!bmap)
3985 goto error;
3987 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3988 src_type, src_pos, n);
3989 if (!bmap->dim)
3990 goto error;
3992 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3993 bmap = isl_basic_map_gauss(bmap, NULL);
3994 bmap = isl_basic_map_finalize(bmap);
3996 return bmap;
3997 error:
3998 isl_basic_map_free(bmap);
3999 return NULL;
4002 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4003 enum isl_dim_type dst_type, unsigned dst_pos,
4004 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4006 isl_basic_map *bmap = bset_to_bmap(bset);
4007 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4008 src_type, src_pos, n);
4009 return bset_from_bmap(bmap);
4012 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4013 enum isl_dim_type dst_type, unsigned dst_pos,
4014 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4016 if (!set)
4017 return NULL;
4018 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4019 return set_from_map(isl_map_move_dims(set_to_map(set),
4020 dst_type, dst_pos, src_type, src_pos, n));
4021 error:
4022 isl_set_free(set);
4023 return NULL;
4026 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4027 enum isl_dim_type dst_type, unsigned dst_pos,
4028 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4030 int i;
4032 if (n == 0) {
4033 map = isl_map_reset(map, src_type);
4034 map = isl_map_reset(map, dst_type);
4035 return map;
4038 if (isl_map_check_range(map, src_type, src_pos, n))
4039 return isl_map_free(map);
4041 if (dst_type == src_type && dst_pos == src_pos)
4042 return map;
4044 isl_assert(map->ctx, dst_type != src_type, goto error);
4046 map = isl_map_cow(map);
4047 if (!map)
4048 return NULL;
4050 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4051 if (!map->dim)
4052 goto error;
4054 for (i = 0; i < map->n; ++i) {
4055 map->p[i] = isl_basic_map_move_dims(map->p[i],
4056 dst_type, dst_pos,
4057 src_type, src_pos, n);
4058 if (!map->p[i])
4059 goto error;
4062 return map;
4063 error:
4064 isl_map_free(map);
4065 return NULL;
4068 /* Move the specified dimensions to the last columns right before
4069 * the divs. Don't change the dimension specification of bmap.
4070 * That's the responsibility of the caller.
4072 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4073 enum isl_dim_type type, unsigned first, unsigned n)
4075 struct isl_dim_map *dim_map;
4076 struct isl_basic_map *res;
4077 enum isl_dim_type t;
4078 unsigned total, off;
4080 if (!bmap)
4081 return NULL;
4082 if (pos(bmap->dim, type) + first + n ==
4083 1 + isl_space_dim(bmap->dim, isl_dim_all))
4084 return bmap;
4086 total = isl_basic_map_total_dim(bmap);
4087 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4089 off = 0;
4090 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4091 unsigned size = isl_space_dim(bmap->dim, t);
4092 if (t == type) {
4093 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4094 0, first, off);
4095 off += first;
4096 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4097 first, n, total - bmap->n_div - n);
4098 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4099 first + n, size - (first + n), off);
4100 off += size - (first + n);
4101 } else {
4102 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4103 off += size;
4106 isl_dim_map_div(dim_map, bmap, off + n);
4108 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4109 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4110 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4111 return res;
4114 /* Insert "n" rows in the divs of "bmap".
4116 * The number of columns is not changed, which means that the last
4117 * dimensions of "bmap" are being reintepreted as the new divs.
4118 * The space of "bmap" is not adjusted, however, which means
4119 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4120 * from the space of "bmap" is the responsibility of the caller.
4122 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4123 int n)
4125 int i;
4126 size_t row_size;
4127 isl_int **new_div;
4128 isl_int *old;
4130 bmap = isl_basic_map_cow(bmap);
4131 if (!bmap)
4132 return NULL;
4134 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4135 old = bmap->block2.data;
4136 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4137 (bmap->extra + n) * (1 + row_size));
4138 if (!bmap->block2.data)
4139 return isl_basic_map_free(bmap);
4140 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4141 if (!new_div)
4142 return isl_basic_map_free(bmap);
4143 for (i = 0; i < n; ++i) {
4144 new_div[i] = bmap->block2.data +
4145 (bmap->extra + i) * (1 + row_size);
4146 isl_seq_clr(new_div[i], 1 + row_size);
4148 for (i = 0; i < bmap->extra; ++i)
4149 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4150 free(bmap->div);
4151 bmap->div = new_div;
4152 bmap->n_div += n;
4153 bmap->extra += n;
4155 return bmap;
4158 /* Drop constraints from "bmap" that only involve the variables
4159 * of "type" in the range [first, first + n] that are not related
4160 * to any of the variables outside that interval.
4161 * These constraints cannot influence the values for the variables
4162 * outside the interval, except in case they cause "bmap" to be empty.
4163 * Only drop the constraints if "bmap" is known to be non-empty.
4165 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4166 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4167 unsigned first, unsigned n)
4169 int i;
4170 int *groups;
4171 unsigned dim, n_div;
4172 isl_bool non_empty;
4174 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4175 if (non_empty < 0)
4176 return isl_basic_map_free(bmap);
4177 if (!non_empty)
4178 return bmap;
4180 dim = isl_basic_map_dim(bmap, isl_dim_all);
4181 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4182 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4183 if (!groups)
4184 return isl_basic_map_free(bmap);
4185 first += isl_basic_map_offset(bmap, type) - 1;
4186 for (i = 0; i < first; ++i)
4187 groups[i] = -1;
4188 for (i = first + n; i < dim - n_div; ++i)
4189 groups[i] = -1;
4191 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4193 return bmap;
4196 /* Turn the n dimensions of type type, starting at first
4197 * into existentially quantified variables.
4199 * If a subset of the projected out variables are unrelated
4200 * to any of the variables that remain, then the constraints
4201 * involving this subset are simply dropped first.
4203 __isl_give isl_basic_map *isl_basic_map_project_out(
4204 __isl_take isl_basic_map *bmap,
4205 enum isl_dim_type type, unsigned first, unsigned n)
4207 isl_bool empty;
4209 if (n == 0)
4210 return basic_map_space_reset(bmap, type);
4211 if (type == isl_dim_div)
4212 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4213 "cannot project out existentially quantified variables",
4214 return isl_basic_map_free(bmap));
4216 empty = isl_basic_map_plain_is_empty(bmap);
4217 if (empty < 0)
4218 return isl_basic_map_free(bmap);
4219 if (empty)
4220 bmap = isl_basic_map_set_to_empty(bmap);
4222 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4223 if (!bmap)
4224 return NULL;
4226 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4227 return isl_basic_map_remove_dims(bmap, type, first, n);
4229 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4230 return isl_basic_map_free(bmap);
4232 bmap = move_last(bmap, type, first, n);
4233 bmap = isl_basic_map_cow(bmap);
4234 bmap = insert_div_rows(bmap, n);
4235 if (!bmap)
4236 return NULL;
4238 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4239 if (!bmap->dim)
4240 goto error;
4241 bmap = isl_basic_map_simplify(bmap);
4242 bmap = isl_basic_map_drop_redundant_divs(bmap);
4243 return isl_basic_map_finalize(bmap);
4244 error:
4245 isl_basic_map_free(bmap);
4246 return NULL;
4249 /* Turn the n dimensions of type type, starting at first
4250 * into existentially quantified variables.
4252 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4253 enum isl_dim_type type, unsigned first, unsigned n)
4255 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4256 type, first, n));
4259 /* Turn the n dimensions of type type, starting at first
4260 * into existentially quantified variables.
4262 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4263 enum isl_dim_type type, unsigned first, unsigned n)
4265 int i;
4267 if (n == 0)
4268 return map_space_reset(map, type);
4270 if (isl_map_check_range(map, type, first, n) < 0)
4271 return isl_map_free(map);
4273 map = isl_map_cow(map);
4274 if (!map)
4275 return NULL;
4277 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4278 if (!map->dim)
4279 goto error;
4281 for (i = 0; i < map->n; ++i) {
4282 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4283 if (!map->p[i])
4284 goto error;
4287 return map;
4288 error:
4289 isl_map_free(map);
4290 return NULL;
4293 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4294 * into existentially quantified variables.
4296 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4297 enum isl_dim_type type, unsigned first, unsigned n)
4299 unsigned dim;
4301 if (isl_map_check_range(map, type, first, n) < 0)
4302 return isl_map_free(map);
4303 dim = isl_map_dim(map, type);
4304 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4305 map = isl_map_project_out(map, type, 0, first);
4306 return map;
4309 /* Turn the n dimensions of type type, starting at first
4310 * into existentially quantified variables.
4312 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4313 enum isl_dim_type type, unsigned first, unsigned n)
4315 return set_from_map(isl_map_project_out(set_to_map(set),
4316 type, first, n));
4319 /* Return a map that projects the elements in "set" onto their
4320 * "n" set dimensions starting at "first".
4321 * "type" should be equal to isl_dim_set.
4323 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4324 enum isl_dim_type type, unsigned first, unsigned n)
4326 int i;
4327 isl_map *map;
4329 if (type != isl_dim_set)
4330 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4331 "only set dimensions can be projected out", goto error);
4332 if (isl_set_check_range(set, type, first, n) < 0)
4333 return isl_set_free(set);
4335 map = isl_map_from_domain(set);
4336 map = isl_map_add_dims(map, isl_dim_out, n);
4337 for (i = 0; i < n; ++i)
4338 map = isl_map_equate(map, isl_dim_in, first + i,
4339 isl_dim_out, i);
4340 return map;
4341 error:
4342 isl_set_free(set);
4343 return NULL;
4346 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4347 unsigned n)
4349 int i, j;
4351 for (i = 0; i < n; ++i) {
4352 j = isl_basic_map_alloc_div(bmap);
4353 if (j < 0)
4354 goto error;
4355 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4357 return bmap;
4358 error:
4359 isl_basic_map_free(bmap);
4360 return NULL;
4363 struct isl_basic_map *isl_basic_map_apply_range(
4364 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4366 isl_space *space_result = NULL;
4367 struct isl_basic_map *bmap;
4368 unsigned n_in, n_out, n, nparam, total, pos;
4369 struct isl_dim_map *dim_map1, *dim_map2;
4371 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4372 goto error;
4373 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4374 bmap2->dim, isl_dim_in))
4375 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4376 "spaces don't match", goto error);
4378 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4379 isl_space_copy(bmap2->dim));
4381 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4382 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4383 n = isl_basic_map_dim(bmap1, isl_dim_out);
4384 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4386 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4387 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4388 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4389 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4390 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4391 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4392 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4393 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4394 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4395 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4396 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4398 bmap = isl_basic_map_alloc_space(space_result,
4399 bmap1->n_div + bmap2->n_div + n,
4400 bmap1->n_eq + bmap2->n_eq,
4401 bmap1->n_ineq + bmap2->n_ineq);
4402 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4403 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4404 bmap = add_divs(bmap, n);
4405 bmap = isl_basic_map_simplify(bmap);
4406 bmap = isl_basic_map_drop_redundant_divs(bmap);
4407 return isl_basic_map_finalize(bmap);
4408 error:
4409 isl_basic_map_free(bmap1);
4410 isl_basic_map_free(bmap2);
4411 return NULL;
4414 struct isl_basic_set *isl_basic_set_apply(
4415 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4417 if (!bset || !bmap)
4418 goto error;
4420 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4421 goto error);
4423 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4424 bmap));
4425 error:
4426 isl_basic_set_free(bset);
4427 isl_basic_map_free(bmap);
4428 return NULL;
4431 struct isl_basic_map *isl_basic_map_apply_domain(
4432 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4434 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4435 goto error;
4436 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4437 bmap2->dim, isl_dim_in))
4438 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4439 "spaces don't match", goto error);
4441 bmap1 = isl_basic_map_reverse(bmap1);
4442 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4443 return isl_basic_map_reverse(bmap1);
4444 error:
4445 isl_basic_map_free(bmap1);
4446 isl_basic_map_free(bmap2);
4447 return NULL;
4450 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4451 * A \cap B -> f(A) + f(B)
4453 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4454 __isl_take isl_basic_map *bmap2)
4456 unsigned n_in, n_out, nparam, total, pos;
4457 struct isl_basic_map *bmap = NULL;
4458 struct isl_dim_map *dim_map1, *dim_map2;
4459 int i;
4461 if (!bmap1 || !bmap2)
4462 goto error;
4464 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4465 goto error);
4467 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4468 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4469 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4471 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4472 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4473 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4474 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4475 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4476 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4477 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4478 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4479 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4480 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4481 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4483 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4484 bmap1->n_div + bmap2->n_div + 2 * n_out,
4485 bmap1->n_eq + bmap2->n_eq + n_out,
4486 bmap1->n_ineq + bmap2->n_ineq);
4487 for (i = 0; i < n_out; ++i) {
4488 int j = isl_basic_map_alloc_equality(bmap);
4489 if (j < 0)
4490 goto error;
4491 isl_seq_clr(bmap->eq[j], 1+total);
4492 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4493 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4494 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4496 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4497 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4498 bmap = add_divs(bmap, 2 * n_out);
4500 bmap = isl_basic_map_simplify(bmap);
4501 return isl_basic_map_finalize(bmap);
4502 error:
4503 isl_basic_map_free(bmap);
4504 isl_basic_map_free(bmap1);
4505 isl_basic_map_free(bmap2);
4506 return NULL;
4509 /* Given two maps A -> f(A) and B -> g(B), construct a map
4510 * A \cap B -> f(A) + f(B)
4512 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4513 __isl_take isl_map *map2)
4515 struct isl_map *result;
4516 int i, j;
4518 if (!map1 || !map2)
4519 goto error;
4521 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4523 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4524 map1->n * map2->n, 0);
4525 if (!result)
4526 goto error;
4527 for (i = 0; i < map1->n; ++i)
4528 for (j = 0; j < map2->n; ++j) {
4529 struct isl_basic_map *part;
4530 part = isl_basic_map_sum(
4531 isl_basic_map_copy(map1->p[i]),
4532 isl_basic_map_copy(map2->p[j]));
4533 if (isl_basic_map_is_empty(part))
4534 isl_basic_map_free(part);
4535 else
4536 result = isl_map_add_basic_map(result, part);
4537 if (!result)
4538 goto error;
4540 isl_map_free(map1);
4541 isl_map_free(map2);
4542 return result;
4543 error:
4544 isl_map_free(map1);
4545 isl_map_free(map2);
4546 return NULL;
4549 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4550 __isl_take isl_set *set2)
4552 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4555 /* Given a basic map A -> f(A), construct A -> -f(A).
4557 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4559 int i, j;
4560 unsigned off, n;
4562 bmap = isl_basic_map_cow(bmap);
4563 if (!bmap)
4564 return NULL;
4566 n = isl_basic_map_dim(bmap, isl_dim_out);
4567 off = isl_basic_map_offset(bmap, isl_dim_out);
4568 for (i = 0; i < bmap->n_eq; ++i)
4569 for (j = 0; j < n; ++j)
4570 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4571 for (i = 0; i < bmap->n_ineq; ++i)
4572 for (j = 0; j < n; ++j)
4573 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4574 for (i = 0; i < bmap->n_div; ++i)
4575 for (j = 0; j < n; ++j)
4576 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4577 bmap = isl_basic_map_gauss(bmap, NULL);
4578 return isl_basic_map_finalize(bmap);
4581 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4583 return isl_basic_map_neg(bset);
4586 /* Given a map A -> f(A), construct A -> -f(A).
4588 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4590 int i;
4592 map = isl_map_cow(map);
4593 if (!map)
4594 return NULL;
4596 for (i = 0; i < map->n; ++i) {
4597 map->p[i] = isl_basic_map_neg(map->p[i]);
4598 if (!map->p[i])
4599 goto error;
4602 return map;
4603 error:
4604 isl_map_free(map);
4605 return NULL;
4608 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4610 return set_from_map(isl_map_neg(set_to_map(set)));
4613 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4614 * A -> floor(f(A)/d).
4616 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4617 isl_int d)
4619 unsigned n_in, n_out, nparam, total, pos;
4620 struct isl_basic_map *result = NULL;
4621 struct isl_dim_map *dim_map;
4622 int i;
4624 if (!bmap)
4625 return NULL;
4627 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4628 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4629 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4631 total = nparam + n_in + n_out + bmap->n_div + n_out;
4632 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4633 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4634 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4635 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4636 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4638 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4639 bmap->n_div + n_out,
4640 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4641 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4642 result = add_divs(result, n_out);
4643 for (i = 0; i < n_out; ++i) {
4644 int j;
4645 j = isl_basic_map_alloc_inequality(result);
4646 if (j < 0)
4647 goto error;
4648 isl_seq_clr(result->ineq[j], 1+total);
4649 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4650 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4651 j = isl_basic_map_alloc_inequality(result);
4652 if (j < 0)
4653 goto error;
4654 isl_seq_clr(result->ineq[j], 1+total);
4655 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4656 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4657 isl_int_sub_ui(result->ineq[j][0], d, 1);
4660 result = isl_basic_map_simplify(result);
4661 return isl_basic_map_finalize(result);
4662 error:
4663 isl_basic_map_free(result);
4664 return NULL;
4667 /* Given a map A -> f(A) and an integer d, construct a map
4668 * A -> floor(f(A)/d).
4670 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4672 int i;
4674 map = isl_map_cow(map);
4675 if (!map)
4676 return NULL;
4678 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4679 for (i = 0; i < map->n; ++i) {
4680 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4681 if (!map->p[i])
4682 goto error;
4684 map = isl_map_unmark_normalized(map);
4686 return map;
4687 error:
4688 isl_map_free(map);
4689 return NULL;
4692 /* Given a map A -> f(A) and an integer d, construct a map
4693 * A -> floor(f(A)/d).
4695 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4696 __isl_take isl_val *d)
4698 if (!map || !d)
4699 goto error;
4700 if (!isl_val_is_int(d))
4701 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4702 "expecting integer denominator", goto error);
4703 map = isl_map_floordiv(map, d->n);
4704 isl_val_free(d);
4705 return map;
4706 error:
4707 isl_map_free(map);
4708 isl_val_free(d);
4709 return NULL;
4712 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4713 unsigned pos)
4715 int i;
4716 unsigned nparam;
4717 unsigned n_in;
4719 i = isl_basic_map_alloc_equality(bmap);
4720 if (i < 0)
4721 goto error;
4722 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4723 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4724 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4725 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4726 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4727 return isl_basic_map_finalize(bmap);
4728 error:
4729 isl_basic_map_free(bmap);
4730 return NULL;
4733 /* Add a constraint to "bmap" expressing i_pos < o_pos
4735 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4736 unsigned pos)
4738 int i;
4739 unsigned nparam;
4740 unsigned n_in;
4742 i = isl_basic_map_alloc_inequality(bmap);
4743 if (i < 0)
4744 goto error;
4745 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4746 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4747 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4748 isl_int_set_si(bmap->ineq[i][0], -1);
4749 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4750 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4751 return isl_basic_map_finalize(bmap);
4752 error:
4753 isl_basic_map_free(bmap);
4754 return NULL;
4757 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4759 static __isl_give isl_basic_map *var_less_or_equal(
4760 __isl_take isl_basic_map *bmap, unsigned pos)
4762 int i;
4763 unsigned nparam;
4764 unsigned n_in;
4766 i = isl_basic_map_alloc_inequality(bmap);
4767 if (i < 0)
4768 goto error;
4769 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4770 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4771 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4772 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4773 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4774 return isl_basic_map_finalize(bmap);
4775 error:
4776 isl_basic_map_free(bmap);
4777 return NULL;
4780 /* Add a constraint to "bmap" expressing i_pos > o_pos
4782 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4783 unsigned pos)
4785 int i;
4786 unsigned nparam;
4787 unsigned n_in;
4789 i = isl_basic_map_alloc_inequality(bmap);
4790 if (i < 0)
4791 goto error;
4792 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4793 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4794 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4795 isl_int_set_si(bmap->ineq[i][0], -1);
4796 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4797 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4798 return isl_basic_map_finalize(bmap);
4799 error:
4800 isl_basic_map_free(bmap);
4801 return NULL;
4804 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4806 static __isl_give isl_basic_map *var_more_or_equal(
4807 __isl_take isl_basic_map *bmap, unsigned pos)
4809 int i;
4810 unsigned nparam;
4811 unsigned n_in;
4813 i = isl_basic_map_alloc_inequality(bmap);
4814 if (i < 0)
4815 goto error;
4816 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4817 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4818 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4819 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4820 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4821 return isl_basic_map_finalize(bmap);
4822 error:
4823 isl_basic_map_free(bmap);
4824 return NULL;
4827 __isl_give isl_basic_map *isl_basic_map_equal(
4828 __isl_take isl_space *space, unsigned n_equal)
4830 int i;
4831 struct isl_basic_map *bmap;
4832 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4833 if (!bmap)
4834 return NULL;
4835 for (i = 0; i < n_equal && bmap; ++i)
4836 bmap = var_equal(bmap, i);
4837 return isl_basic_map_finalize(bmap);
4840 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4842 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4843 unsigned pos)
4845 int i;
4846 struct isl_basic_map *bmap;
4847 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4848 if (!bmap)
4849 return NULL;
4850 for (i = 0; i < pos && bmap; ++i)
4851 bmap = var_equal(bmap, i);
4852 if (bmap)
4853 bmap = var_less(bmap, pos);
4854 return isl_basic_map_finalize(bmap);
4857 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4859 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4860 __isl_take isl_space *space, unsigned pos)
4862 int i;
4863 isl_basic_map *bmap;
4865 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4866 for (i = 0; i < pos; ++i)
4867 bmap = var_equal(bmap, i);
4868 bmap = var_less_or_equal(bmap, pos);
4869 return isl_basic_map_finalize(bmap);
4872 /* Return a relation on "space" expressing i_pos > o_pos
4874 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
4875 unsigned pos)
4877 int i;
4878 struct isl_basic_map *bmap;
4879 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4880 if (!bmap)
4881 return NULL;
4882 for (i = 0; i < pos && bmap; ++i)
4883 bmap = var_equal(bmap, i);
4884 if (bmap)
4885 bmap = var_more(bmap, pos);
4886 return isl_basic_map_finalize(bmap);
4889 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
4891 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4892 __isl_take isl_space *space, unsigned pos)
4894 int i;
4895 isl_basic_map *bmap;
4897 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4898 for (i = 0; i < pos; ++i)
4899 bmap = var_equal(bmap, i);
4900 bmap = var_more_or_equal(bmap, pos);
4901 return isl_basic_map_finalize(bmap);
4904 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
4905 unsigned n, int equal)
4907 struct isl_map *map;
4908 int i;
4910 if (n == 0 && equal)
4911 return isl_map_universe(space);
4913 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4915 for (i = 0; i + 1 < n; ++i)
4916 map = isl_map_add_basic_map(map,
4917 isl_basic_map_less_at(isl_space_copy(space), i));
4918 if (n > 0) {
4919 if (equal)
4920 map = isl_map_add_basic_map(map,
4921 isl_basic_map_less_or_equal_at(space, n - 1));
4922 else
4923 map = isl_map_add_basic_map(map,
4924 isl_basic_map_less_at(space, n - 1));
4925 } else
4926 isl_space_free(space);
4928 return map;
4931 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
4933 if (!space)
4934 return NULL;
4935 return map_lex_lte_first(space, space->n_out, equal);
4938 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4940 return map_lex_lte_first(dim, n, 0);
4943 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4945 return map_lex_lte_first(dim, n, 1);
4948 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4950 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4953 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4955 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4958 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
4959 unsigned n, int equal)
4961 struct isl_map *map;
4962 int i;
4964 if (n == 0 && equal)
4965 return isl_map_universe(space);
4967 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
4969 for (i = 0; i + 1 < n; ++i)
4970 map = isl_map_add_basic_map(map,
4971 isl_basic_map_more_at(isl_space_copy(space), i));
4972 if (n > 0) {
4973 if (equal)
4974 map = isl_map_add_basic_map(map,
4975 isl_basic_map_more_or_equal_at(space, n - 1));
4976 else
4977 map = isl_map_add_basic_map(map,
4978 isl_basic_map_more_at(space, n - 1));
4979 } else
4980 isl_space_free(space);
4982 return map;
4985 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
4987 if (!space)
4988 return NULL;
4989 return map_lex_gte_first(space, space->n_out, equal);
4992 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4994 return map_lex_gte_first(dim, n, 0);
4997 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4999 return map_lex_gte_first(dim, n, 1);
5002 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5004 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5007 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5009 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5012 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5013 __isl_take isl_set *set2)
5015 isl_map *map;
5016 map = isl_map_lex_le(isl_set_get_space(set1));
5017 map = isl_map_intersect_domain(map, set1);
5018 map = isl_map_intersect_range(map, set2);
5019 return map;
5022 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5023 __isl_take isl_set *set2)
5025 isl_map *map;
5026 map = isl_map_lex_lt(isl_set_get_space(set1));
5027 map = isl_map_intersect_domain(map, set1);
5028 map = isl_map_intersect_range(map, set2);
5029 return map;
5032 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5033 __isl_take isl_set *set2)
5035 isl_map *map;
5036 map = isl_map_lex_ge(isl_set_get_space(set1));
5037 map = isl_map_intersect_domain(map, set1);
5038 map = isl_map_intersect_range(map, set2);
5039 return map;
5042 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5043 __isl_take isl_set *set2)
5045 isl_map *map;
5046 map = isl_map_lex_gt(isl_set_get_space(set1));
5047 map = isl_map_intersect_domain(map, set1);
5048 map = isl_map_intersect_range(map, set2);
5049 return map;
5052 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5053 __isl_take isl_map *map2)
5055 isl_map *map;
5056 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5057 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5058 map = isl_map_apply_range(map, isl_map_reverse(map2));
5059 return map;
5062 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5063 __isl_take isl_map *map2)
5065 isl_map *map;
5066 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5067 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5068 map = isl_map_apply_range(map, isl_map_reverse(map2));
5069 return map;
5072 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5073 __isl_take isl_map *map2)
5075 isl_map *map;
5076 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5077 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5078 map = isl_map_apply_range(map, isl_map_reverse(map2));
5079 return map;
5082 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5083 __isl_take isl_map *map2)
5085 isl_map *map;
5086 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5087 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5088 map = isl_map_apply_range(map, isl_map_reverse(map2));
5089 return map;
5092 /* For the div d = floor(f/m) at position "div", add the constraint
5094 * f - m d >= 0
5096 static __isl_give isl_basic_map *add_upper_div_constraint(
5097 __isl_take isl_basic_map *bmap, unsigned div)
5099 int i;
5100 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5101 unsigned n_div, pos;
5103 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5104 if (v_div < 0)
5105 return isl_basic_map_free(bmap);
5106 pos = v_div + div;
5107 i = isl_basic_map_alloc_inequality(bmap);
5108 if (i < 0)
5109 return isl_basic_map_free(bmap);
5110 isl_seq_cpy(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5111 isl_int_neg(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5113 return bmap;
5116 /* For the div d = floor(f/m) at position "div", add the constraint
5118 * -(f-(m-1)) + m d >= 0
5120 static __isl_give isl_basic_map *add_lower_div_constraint(
5121 __isl_take isl_basic_map *bmap, unsigned div)
5123 int i;
5124 int v_div = isl_basic_map_var_offset(bmap, isl_dim_div);
5125 unsigned n_div, pos;
5127 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5128 if (v_div < 0)
5129 return isl_basic_map_free(bmap);
5130 pos = v_div + div;
5131 i = isl_basic_map_alloc_inequality(bmap);
5132 if (i < 0)
5133 return isl_basic_map_free(bmap);
5134 isl_seq_neg(bmap->ineq[i], bmap->div[div] + 1, 1 + v_div + n_div);
5135 isl_int_set(bmap->ineq[i][1 + pos], bmap->div[div][0]);
5136 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5137 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5139 return bmap;
5142 /* For the div d = floor(f/m) at position "pos", add the constraints
5144 * f - m d >= 0
5145 * -(f-(m-1)) + m d >= 0
5147 * Note that the second constraint is the negation of
5149 * f - m d >= m
5151 __isl_give isl_basic_map *isl_basic_map_add_div_constraints(
5152 __isl_take isl_basic_map *bmap, unsigned pos)
5154 bmap = add_upper_div_constraint(bmap, pos);
5155 bmap = add_lower_div_constraint(bmap, pos);
5156 return bmap;
5159 /* For each known div d = floor(f/m), add the constraints
5161 * f - m d >= 0
5162 * -(f-(m-1)) + m d >= 0
5164 * Remove duplicate constraints in case of some these div constraints
5165 * already appear in "bmap".
5167 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5168 __isl_take isl_basic_map *bmap)
5170 unsigned n_div;
5172 if (!bmap)
5173 return NULL;
5174 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5175 if (n_div == 0)
5176 return bmap;
5178 bmap = add_known_div_constraints(bmap);
5179 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5180 bmap = isl_basic_map_finalize(bmap);
5181 return bmap;
5184 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5186 * In particular, if this div is of the form d = floor(f/m),
5187 * then add the constraint
5189 * f - m d >= 0
5191 * if sign < 0 or the constraint
5193 * -(f-(m-1)) + m d >= 0
5195 * if sign > 0.
5197 __isl_give isl_basic_map *isl_basic_map_add_div_constraint(
5198 __isl_take isl_basic_map *bmap, unsigned div, int sign)
5200 if (sign < 0)
5201 return add_upper_div_constraint(bmap, div);
5202 else
5203 return add_lower_div_constraint(bmap, div);
5206 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5207 __isl_take isl_basic_map *bmap)
5209 if (!bmap)
5210 goto error;
5211 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5212 bmap->n_div == 0 &&
5213 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5214 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5215 return bset_from_bmap(bmap);
5216 bmap = isl_basic_map_cow(bmap);
5217 if (!bmap)
5218 goto error;
5219 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5220 if (!bmap->dim)
5221 goto error;
5222 bmap->extra -= bmap->n_div;
5223 bmap->n_div = 0;
5224 bmap = isl_basic_map_finalize(bmap);
5225 return bset_from_bmap(bmap);
5226 error:
5227 isl_basic_map_free(bmap);
5228 return NULL;
5231 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5232 __isl_take isl_basic_set *bset)
5234 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5237 /* Replace each element in "list" by the result of applying
5238 * isl_basic_map_underlying_set to the element.
5240 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5241 __isl_take isl_basic_map_list *list)
5243 int i, n;
5245 if (!list)
5246 return NULL;
5248 n = isl_basic_map_list_n_basic_map(list);
5249 for (i = 0; i < n; ++i) {
5250 isl_basic_map *bmap;
5251 isl_basic_set *bset;
5253 bmap = isl_basic_map_list_get_basic_map(list, i);
5254 bset = isl_basic_set_underlying_set(bmap);
5255 list = isl_basic_set_list_set_basic_set(list, i, bset);
5258 return list;
5261 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5262 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5264 struct isl_basic_map *bmap;
5265 struct isl_ctx *ctx;
5266 unsigned total;
5267 int i;
5269 if (!bset || !like)
5270 goto error;
5271 ctx = bset->ctx;
5272 if (isl_basic_set_check_no_params(bset) < 0 ||
5273 isl_basic_set_check_no_locals(bset) < 0)
5274 goto error;
5275 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5276 goto error);
5277 if (like->n_div == 0) {
5278 isl_space *space = isl_basic_map_get_space(like);
5279 isl_basic_map_free(like);
5280 return isl_basic_map_reset_space(bset, space);
5282 bset = isl_basic_set_cow(bset);
5283 if (!bset)
5284 goto error;
5285 total = bset->dim->n_out + bset->extra;
5286 bmap = bset_to_bmap(bset);
5287 isl_space_free(bmap->dim);
5288 bmap->dim = isl_space_copy(like->dim);
5289 if (!bmap->dim)
5290 goto error;
5291 bmap->n_div = like->n_div;
5292 bmap->extra += like->n_div;
5293 if (bmap->extra) {
5294 unsigned ltotal;
5295 isl_int **div;
5296 ltotal = total - bmap->extra + like->extra;
5297 if (ltotal > total)
5298 ltotal = total;
5299 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5300 bmap->extra * (1 + 1 + total));
5301 if (isl_blk_is_error(bmap->block2))
5302 goto error;
5303 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5304 if (!div)
5305 goto error;
5306 bmap->div = div;
5307 for (i = 0; i < bmap->extra; ++i)
5308 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5309 for (i = 0; i < like->n_div; ++i) {
5310 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5311 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5313 bmap = isl_basic_map_add_known_div_constraints(bmap);
5315 isl_basic_map_free(like);
5316 bmap = isl_basic_map_simplify(bmap);
5317 bmap = isl_basic_map_finalize(bmap);
5318 return bmap;
5319 error:
5320 isl_basic_map_free(like);
5321 isl_basic_set_free(bset);
5322 return NULL;
5325 struct isl_basic_set *isl_basic_set_from_underlying_set(
5326 struct isl_basic_set *bset, struct isl_basic_set *like)
5328 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5329 bset_to_bmap(like)));
5332 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5334 int i;
5336 map = isl_map_cow(map);
5337 if (!map)
5338 return NULL;
5339 map->dim = isl_space_cow(map->dim);
5340 if (!map->dim)
5341 goto error;
5343 for (i = 1; i < map->n; ++i)
5344 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5345 goto error);
5346 for (i = 0; i < map->n; ++i) {
5347 map->p[i] = bset_to_bmap(
5348 isl_basic_map_underlying_set(map->p[i]));
5349 if (!map->p[i])
5350 goto error;
5352 if (map->n == 0)
5353 map->dim = isl_space_underlying(map->dim, 0);
5354 else {
5355 isl_space_free(map->dim);
5356 map->dim = isl_space_copy(map->p[0]->dim);
5358 if (!map->dim)
5359 goto error;
5360 return set_from_map(map);
5361 error:
5362 isl_map_free(map);
5363 return NULL;
5366 /* Replace the space of "bmap" by "space".
5368 * If the space of "bmap" is identical to "space" (including the identifiers
5369 * of the input and output dimensions), then simply return the original input.
5371 __isl_give isl_basic_map *isl_basic_map_reset_space(
5372 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5374 isl_bool equal;
5375 isl_space *bmap_space;
5377 bmap_space = isl_basic_map_peek_space(bmap);
5378 equal = isl_space_is_equal(bmap_space, space);
5379 if (equal >= 0 && equal)
5380 equal = isl_space_has_equal_ids(bmap_space, space);
5381 if (equal < 0)
5382 goto error;
5383 if (equal) {
5384 isl_space_free(space);
5385 return bmap;
5387 bmap = isl_basic_map_cow(bmap);
5388 if (!bmap || !space)
5389 goto error;
5391 isl_space_free(bmap->dim);
5392 bmap->dim = space;
5394 bmap = isl_basic_map_finalize(bmap);
5396 return bmap;
5397 error:
5398 isl_basic_map_free(bmap);
5399 isl_space_free(space);
5400 return NULL;
5403 __isl_give isl_basic_set *isl_basic_set_reset_space(
5404 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5406 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5407 dim));
5410 /* Check that the total dimensions of "map" and "space" are the same.
5412 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5413 __isl_keep isl_space *space)
5415 unsigned dim1, dim2;
5417 if (!map || !space)
5418 return isl_stat_error;
5419 dim1 = isl_map_dim(map, isl_dim_all);
5420 dim2 = isl_space_dim(space, isl_dim_all);
5421 if (dim1 == dim2)
5422 return isl_stat_ok;
5423 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5424 "total dimensions do not match", return isl_stat_error);
5427 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5428 __isl_take isl_space *space)
5430 int i;
5432 map = isl_map_cow(map);
5433 if (!map || !space)
5434 goto error;
5436 for (i = 0; i < map->n; ++i) {
5437 map->p[i] = isl_basic_map_reset_space(map->p[i],
5438 isl_space_copy(space));
5439 if (!map->p[i])
5440 goto error;
5442 isl_space_free(map->dim);
5443 map->dim = space;
5445 return map;
5446 error:
5447 isl_map_free(map);
5448 isl_space_free(space);
5449 return NULL;
5452 /* Replace the space of "map" by "space", without modifying
5453 * the dimension of "map".
5455 * If the space of "map" is identical to "space" (including the identifiers
5456 * of the input and output dimensions), then simply return the original input.
5458 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5459 __isl_take isl_space *space)
5461 isl_bool equal;
5462 isl_space *map_space;
5464 map_space = isl_map_peek_space(map);
5465 equal = isl_space_is_equal(map_space, space);
5466 if (equal >= 0 && equal)
5467 equal = isl_space_has_equal_ids(map_space, space);
5468 if (equal < 0)
5469 goto error;
5470 if (equal) {
5471 isl_space_free(space);
5472 return map;
5474 if (check_map_space_equal_total_dim(map, space) < 0)
5475 goto error;
5476 return isl_map_reset_space(map, space);
5477 error:
5478 isl_map_free(map);
5479 isl_space_free(space);
5480 return NULL;
5483 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5484 __isl_take isl_space *dim)
5486 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5489 /* Compute the parameter domain of the given basic set.
5491 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5493 isl_bool is_params;
5494 isl_space *space;
5495 unsigned n;
5497 is_params = isl_basic_set_is_params(bset);
5498 if (is_params < 0)
5499 return isl_basic_set_free(bset);
5500 if (is_params)
5501 return bset;
5503 n = isl_basic_set_dim(bset, isl_dim_set);
5504 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5505 space = isl_basic_set_get_space(bset);
5506 space = isl_space_params(space);
5507 bset = isl_basic_set_reset_space(bset, space);
5508 return bset;
5511 /* Construct a zero-dimensional basic set with the given parameter domain.
5513 __isl_give isl_basic_set *isl_basic_set_from_params(
5514 __isl_take isl_basic_set *bset)
5516 isl_space *space;
5517 space = isl_basic_set_get_space(bset);
5518 space = isl_space_set_from_params(space);
5519 bset = isl_basic_set_reset_space(bset, space);
5520 return bset;
5523 /* Compute the parameter domain of the given set.
5525 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5527 isl_space *space;
5528 unsigned n;
5530 if (isl_set_is_params(set))
5531 return set;
5533 n = isl_set_dim(set, isl_dim_set);
5534 set = isl_set_project_out(set, isl_dim_set, 0, n);
5535 space = isl_set_get_space(set);
5536 space = isl_space_params(space);
5537 set = isl_set_reset_space(set, space);
5538 return set;
5541 /* Construct a zero-dimensional set with the given parameter domain.
5543 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5545 isl_space *space;
5546 space = isl_set_get_space(set);
5547 space = isl_space_set_from_params(space);
5548 set = isl_set_reset_space(set, space);
5549 return set;
5552 /* Compute the parameter domain of the given map.
5554 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5556 isl_space *space;
5557 unsigned n;
5559 n = isl_map_dim(map, isl_dim_in);
5560 map = isl_map_project_out(map, isl_dim_in, 0, n);
5561 n = isl_map_dim(map, isl_dim_out);
5562 map = isl_map_project_out(map, isl_dim_out, 0, n);
5563 space = isl_map_get_space(map);
5564 space = isl_space_params(space);
5565 map = isl_map_reset_space(map, space);
5566 return map;
5569 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5571 isl_space *space;
5572 unsigned n_out;
5574 if (!bmap)
5575 return NULL;
5576 space = isl_space_domain(isl_basic_map_get_space(bmap));
5578 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5579 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5581 return isl_basic_map_reset_space(bmap, space);
5584 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5586 if (!bmap)
5587 return isl_bool_error;
5588 return isl_space_may_be_set(bmap->dim);
5591 /* Is this basic map actually a set?
5592 * Users should never call this function. Outside of isl,
5593 * the type should indicate whether something is a set or a map.
5595 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5597 if (!bmap)
5598 return isl_bool_error;
5599 return isl_space_is_set(bmap->dim);
5602 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5604 isl_bool is_set;
5606 is_set = isl_basic_map_is_set(bmap);
5607 if (is_set < 0)
5608 goto error;
5609 if (is_set)
5610 return bmap;
5611 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5612 error:
5613 isl_basic_map_free(bmap);
5614 return NULL;
5617 __isl_give isl_basic_map *isl_basic_map_domain_map(
5618 __isl_take isl_basic_map *bmap)
5620 int i;
5621 isl_space *space;
5622 isl_basic_map *domain;
5623 int nparam, n_in, n_out;
5625 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5626 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5627 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5629 space = isl_basic_map_get_space(bmap);
5630 space = isl_space_from_range(isl_space_domain(space));
5631 domain = isl_basic_map_universe(space);
5633 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5634 bmap = isl_basic_map_apply_range(bmap, domain);
5635 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5637 for (i = 0; i < n_in; ++i)
5638 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5639 isl_dim_out, i);
5641 bmap = isl_basic_map_gauss(bmap, NULL);
5642 return isl_basic_map_finalize(bmap);
5645 __isl_give isl_basic_map *isl_basic_map_range_map(
5646 __isl_take isl_basic_map *bmap)
5648 int i;
5649 isl_space *space;
5650 isl_basic_map *range;
5651 int nparam, n_in, n_out;
5653 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5654 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5655 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5657 space = isl_basic_map_get_space(bmap);
5658 space = isl_space_from_range(isl_space_range(space));
5659 range = isl_basic_map_universe(space);
5661 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5662 bmap = isl_basic_map_apply_range(bmap, range);
5663 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5665 for (i = 0; i < n_out; ++i)
5666 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5667 isl_dim_out, i);
5669 bmap = isl_basic_map_gauss(bmap, NULL);
5670 return isl_basic_map_finalize(bmap);
5673 int isl_map_may_be_set(__isl_keep isl_map *map)
5675 if (!map)
5676 return -1;
5677 return isl_space_may_be_set(map->dim);
5680 /* Is this map actually a set?
5681 * Users should never call this function. Outside of isl,
5682 * the type should indicate whether something is a set or a map.
5684 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5686 if (!map)
5687 return isl_bool_error;
5688 return isl_space_is_set(map->dim);
5691 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5693 int i;
5694 isl_bool is_set;
5695 struct isl_set *set;
5697 is_set = isl_map_is_set(map);
5698 if (is_set < 0)
5699 goto error;
5700 if (is_set)
5701 return set_from_map(map);
5703 map = isl_map_cow(map);
5704 if (!map)
5705 goto error;
5707 set = set_from_map(map);
5708 set->dim = isl_space_range(set->dim);
5709 if (!set->dim)
5710 goto error;
5711 for (i = 0; i < map->n; ++i) {
5712 set->p[i] = isl_basic_map_range(map->p[i]);
5713 if (!set->p[i])
5714 goto error;
5716 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5717 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5718 return set;
5719 error:
5720 isl_map_free(map);
5721 return NULL;
5724 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5726 int i;
5728 map = isl_map_cow(map);
5729 if (!map)
5730 return NULL;
5732 map->dim = isl_space_domain_map(map->dim);
5733 if (!map->dim)
5734 goto error;
5735 for (i = 0; i < map->n; ++i) {
5736 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5737 if (!map->p[i])
5738 goto error;
5740 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5741 map = isl_map_unmark_normalized(map);
5742 return map;
5743 error:
5744 isl_map_free(map);
5745 return NULL;
5748 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5750 int i;
5751 isl_space *range_dim;
5753 map = isl_map_cow(map);
5754 if (!map)
5755 return NULL;
5757 range_dim = isl_space_range(isl_map_get_space(map));
5758 range_dim = isl_space_from_range(range_dim);
5759 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5760 map->dim = isl_space_join(map->dim, range_dim);
5761 if (!map->dim)
5762 goto error;
5763 for (i = 0; i < map->n; ++i) {
5764 map->p[i] = isl_basic_map_range_map(map->p[i]);
5765 if (!map->p[i])
5766 goto error;
5768 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5769 map = isl_map_unmark_normalized(map);
5770 return map;
5771 error:
5772 isl_map_free(map);
5773 return NULL;
5776 /* Given a wrapped map of the form A[B -> C],
5777 * return the map A[B -> C] -> B.
5779 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5781 isl_id *id;
5782 isl_map *map;
5784 if (!set)
5785 return NULL;
5786 if (!isl_set_has_tuple_id(set))
5787 return isl_map_domain_map(isl_set_unwrap(set));
5789 id = isl_set_get_tuple_id(set);
5790 map = isl_map_domain_map(isl_set_unwrap(set));
5791 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5793 return map;
5796 __isl_give isl_basic_map *isl_basic_map_from_domain(
5797 __isl_take isl_basic_set *bset)
5799 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5802 __isl_give isl_basic_map *isl_basic_map_from_range(
5803 __isl_take isl_basic_set *bset)
5805 isl_space *space;
5806 space = isl_basic_set_get_space(bset);
5807 space = isl_space_from_range(space);
5808 bset = isl_basic_set_reset_space(bset, space);
5809 return bset_to_bmap(bset);
5812 /* Create a relation with the given set as range.
5813 * The domain of the created relation is a zero-dimensional
5814 * flat anonymous space.
5816 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5818 isl_space *space;
5819 space = isl_set_get_space(set);
5820 space = isl_space_from_range(space);
5821 set = isl_set_reset_space(set, space);
5822 return set_to_map(set);
5825 /* Create a relation with the given set as domain.
5826 * The range of the created relation is a zero-dimensional
5827 * flat anonymous space.
5829 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5831 return isl_map_reverse(isl_map_from_range(set));
5834 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5835 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5837 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5840 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5841 __isl_take isl_set *range)
5843 return isl_map_apply_range(isl_map_reverse(domain), range);
5846 /* Return a newly allocated isl_map with given space and flags and
5847 * room for "n" basic maps.
5848 * Make sure that all cached information is cleared.
5850 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5851 unsigned flags)
5853 struct isl_map *map;
5855 if (!space)
5856 return NULL;
5857 if (n < 0)
5858 isl_die(space->ctx, isl_error_internal,
5859 "negative number of basic maps", goto error);
5860 map = isl_calloc(space->ctx, struct isl_map,
5861 sizeof(struct isl_map) +
5862 (n - 1) * sizeof(struct isl_basic_map *));
5863 if (!map)
5864 goto error;
5866 map->ctx = space->ctx;
5867 isl_ctx_ref(map->ctx);
5868 map->ref = 1;
5869 map->size = n;
5870 map->n = 0;
5871 map->dim = space;
5872 map->flags = flags;
5873 return map;
5874 error:
5875 isl_space_free(space);
5876 return NULL;
5879 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5881 struct isl_basic_map *bmap;
5882 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5883 bmap = isl_basic_map_set_to_empty(bmap);
5884 return bmap;
5887 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5889 struct isl_basic_set *bset;
5890 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5891 bset = isl_basic_set_set_to_empty(bset);
5892 return bset;
5895 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5897 struct isl_basic_map *bmap;
5898 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5899 bmap = isl_basic_map_finalize(bmap);
5900 return bmap;
5903 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5905 struct isl_basic_set *bset;
5906 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5907 bset = isl_basic_set_finalize(bset);
5908 return bset;
5911 __isl_give isl_basic_map *isl_basic_map_nat_universe(
5912 __isl_take isl_space *space)
5914 int i;
5915 unsigned total = isl_space_dim(space, isl_dim_all);
5916 isl_basic_map *bmap;
5918 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
5919 for (i = 0; i < total; ++i) {
5920 int k = isl_basic_map_alloc_inequality(bmap);
5921 if (k < 0)
5922 goto error;
5923 isl_seq_clr(bmap->ineq[k], 1 + total);
5924 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5926 return bmap;
5927 error:
5928 isl_basic_map_free(bmap);
5929 return NULL;
5932 __isl_give isl_basic_set *isl_basic_set_nat_universe(
5933 __isl_take isl_space *space)
5935 return isl_basic_map_nat_universe(space);
5938 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5940 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5943 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5945 return isl_map_nat_universe(dim);
5948 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
5950 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
5953 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
5955 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
5958 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
5960 struct isl_map *map;
5961 if (!space)
5962 return NULL;
5963 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5964 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
5965 return map;
5968 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
5970 struct isl_set *set;
5971 if (!space)
5972 return NULL;
5973 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
5974 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
5975 return set;
5978 struct isl_map *isl_map_dup(struct isl_map *map)
5980 int i;
5981 struct isl_map *dup;
5983 if (!map)
5984 return NULL;
5985 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5986 for (i = 0; i < map->n; ++i)
5987 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5988 return dup;
5991 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5992 __isl_take isl_basic_map *bmap)
5994 if (!bmap || !map)
5995 goto error;
5996 if (isl_basic_map_plain_is_empty(bmap)) {
5997 isl_basic_map_free(bmap);
5998 return map;
6000 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6001 isl_assert(map->ctx, map->n < map->size, goto error);
6002 map->p[map->n] = bmap;
6003 map->n++;
6004 map = isl_map_unmark_normalized(map);
6005 return map;
6006 error:
6007 if (map)
6008 isl_map_free(map);
6009 if (bmap)
6010 isl_basic_map_free(bmap);
6011 return NULL;
6014 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6016 int i;
6018 if (!map)
6019 return NULL;
6021 if (--map->ref > 0)
6022 return NULL;
6024 clear_caches(map);
6025 isl_ctx_deref(map->ctx);
6026 for (i = 0; i < map->n; ++i)
6027 isl_basic_map_free(map->p[i]);
6028 isl_space_free(map->dim);
6029 free(map);
6031 return NULL;
6034 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6035 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6037 int j;
6039 bmap = isl_basic_map_cow(bmap);
6040 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6041 j = isl_basic_map_alloc_equality(bmap);
6042 if (j < 0)
6043 goto error;
6044 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6045 isl_int_set_si(bmap->eq[j][pos], -1);
6046 isl_int_set_si(bmap->eq[j][0], value);
6047 bmap = isl_basic_map_simplify(bmap);
6048 return isl_basic_map_finalize(bmap);
6049 error:
6050 isl_basic_map_free(bmap);
6051 return NULL;
6054 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6055 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6057 int j;
6059 bmap = isl_basic_map_cow(bmap);
6060 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6061 j = isl_basic_map_alloc_equality(bmap);
6062 if (j < 0)
6063 goto error;
6064 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6065 isl_int_set_si(bmap->eq[j][pos], -1);
6066 isl_int_set(bmap->eq[j][0], value);
6067 bmap = isl_basic_map_simplify(bmap);
6068 return isl_basic_map_finalize(bmap);
6069 error:
6070 isl_basic_map_free(bmap);
6071 return NULL;
6074 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6075 enum isl_dim_type type, unsigned pos, int value)
6077 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6078 return isl_basic_map_free(bmap);
6079 return isl_basic_map_fix_pos_si(bmap,
6080 isl_basic_map_offset(bmap, type) + pos, value);
6083 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6084 enum isl_dim_type type, unsigned pos, isl_int value)
6086 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6087 return isl_basic_map_free(bmap);
6088 return isl_basic_map_fix_pos(bmap,
6089 isl_basic_map_offset(bmap, type) + pos, value);
6092 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6093 * to be equal to "v".
6095 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6096 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6098 if (!bmap || !v)
6099 goto error;
6100 if (!isl_val_is_int(v))
6101 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6102 "expecting integer value", goto error);
6103 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6104 goto error;
6105 pos += isl_basic_map_offset(bmap, type);
6106 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6107 isl_val_free(v);
6108 return bmap;
6109 error:
6110 isl_basic_map_free(bmap);
6111 isl_val_free(v);
6112 return NULL;
6115 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6116 * to be equal to "v".
6118 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6119 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6121 return isl_basic_map_fix_val(bset, type, pos, v);
6124 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6125 enum isl_dim_type type, unsigned pos, int value)
6127 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6128 type, pos, value));
6131 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6132 enum isl_dim_type type, unsigned pos, isl_int value)
6134 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6135 type, pos, value));
6138 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6139 unsigned input, int value)
6141 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6144 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6145 unsigned dim, int value)
6147 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6148 isl_dim_set, dim, value));
6151 /* Remove the basic map at position "i" from "map" if this basic map
6152 * is (obviously) empty.
6154 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6156 isl_bool empty;
6158 if (!map)
6159 return NULL;
6161 empty = isl_basic_map_plain_is_empty(map->p[i]);
6162 if (empty < 0)
6163 return isl_map_free(map);
6164 if (!empty)
6165 return map;
6167 isl_basic_map_free(map->p[i]);
6168 map->n--;
6169 if (i != map->n) {
6170 map->p[i] = map->p[map->n];
6171 map = isl_map_unmark_normalized(map);
6175 return map;
6178 /* Perform "fn" on each basic map of "map", where we may not be holding
6179 * the only reference to "map".
6180 * In particular, "fn" should be a semantics preserving operation
6181 * that we want to apply to all copies of "map". We therefore need
6182 * to be careful not to modify "map" in a way that breaks "map"
6183 * in case anything goes wrong.
6185 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6186 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6188 struct isl_basic_map *bmap;
6189 int i;
6191 if (!map)
6192 return NULL;
6194 for (i = map->n - 1; i >= 0; --i) {
6195 bmap = isl_basic_map_copy(map->p[i]);
6196 bmap = fn(bmap);
6197 if (!bmap)
6198 goto error;
6199 isl_basic_map_free(map->p[i]);
6200 map->p[i] = bmap;
6201 map = remove_if_empty(map, i);
6202 if (!map)
6203 return NULL;
6206 return map;
6207 error:
6208 isl_map_free(map);
6209 return NULL;
6212 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6213 enum isl_dim_type type, unsigned pos, int value)
6215 int i;
6217 map = isl_map_cow(map);
6218 if (isl_map_check_range(map, type, pos, 1) < 0)
6219 return isl_map_free(map);
6220 for (i = map->n - 1; i >= 0; --i) {
6221 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6222 map = remove_if_empty(map, i);
6223 if (!map)
6224 return NULL;
6226 map = isl_map_unmark_normalized(map);
6227 return map;
6230 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6231 enum isl_dim_type type, unsigned pos, int value)
6233 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6236 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6237 enum isl_dim_type type, unsigned pos, isl_int value)
6239 int i;
6241 map = isl_map_cow(map);
6242 if (isl_map_check_range(map, type, pos, 1) < 0)
6243 return isl_map_free(map);
6244 for (i = 0; i < map->n; ++i) {
6245 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6246 if (!map->p[i])
6247 goto error;
6249 map = isl_map_unmark_normalized(map);
6250 return map;
6251 error:
6252 isl_map_free(map);
6253 return NULL;
6256 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6257 enum isl_dim_type type, unsigned pos, isl_int value)
6259 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6262 /* Fix the value of the variable at position "pos" of type "type" of "map"
6263 * to be equal to "v".
6265 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6266 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6268 int i;
6270 map = isl_map_cow(map);
6271 if (!map || !v)
6272 goto error;
6274 if (!isl_val_is_int(v))
6275 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6276 "expecting integer value", goto error);
6277 if (isl_map_check_range(map, type, pos, 1) < 0)
6278 goto error;
6279 for (i = map->n - 1; i >= 0; --i) {
6280 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6281 isl_val_copy(v));
6282 map = remove_if_empty(map, i);
6283 if (!map)
6284 goto error;
6286 map = isl_map_unmark_normalized(map);
6287 isl_val_free(v);
6288 return map;
6289 error:
6290 isl_map_free(map);
6291 isl_val_free(v);
6292 return NULL;
6295 /* Fix the value of the variable at position "pos" of type "type" of "set"
6296 * to be equal to "v".
6298 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6299 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6301 return isl_map_fix_val(set, type, pos, v);
6304 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6305 unsigned input, int value)
6307 return isl_map_fix_si(map, isl_dim_in, input, value);
6310 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6312 return set_from_map(isl_map_fix_si(set_to_map(set),
6313 isl_dim_set, dim, value));
6316 static __isl_give isl_basic_map *basic_map_bound_si(
6317 __isl_take isl_basic_map *bmap,
6318 enum isl_dim_type type, unsigned pos, int value, int upper)
6320 int j;
6322 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6323 return isl_basic_map_free(bmap);
6324 pos += isl_basic_map_offset(bmap, type);
6325 bmap = isl_basic_map_cow(bmap);
6326 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6327 j = isl_basic_map_alloc_inequality(bmap);
6328 if (j < 0)
6329 goto error;
6330 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6331 if (upper) {
6332 isl_int_set_si(bmap->ineq[j][pos], -1);
6333 isl_int_set_si(bmap->ineq[j][0], value);
6334 } else {
6335 isl_int_set_si(bmap->ineq[j][pos], 1);
6336 isl_int_set_si(bmap->ineq[j][0], -value);
6338 bmap = isl_basic_map_simplify(bmap);
6339 return isl_basic_map_finalize(bmap);
6340 error:
6341 isl_basic_map_free(bmap);
6342 return NULL;
6345 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6346 __isl_take isl_basic_map *bmap,
6347 enum isl_dim_type type, unsigned pos, int value)
6349 return basic_map_bound_si(bmap, type, pos, value, 0);
6352 /* Constrain the values of the given dimension to be no greater than "value".
6354 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6355 __isl_take isl_basic_map *bmap,
6356 enum isl_dim_type type, unsigned pos, int value)
6358 return basic_map_bound_si(bmap, type, pos, value, 1);
6361 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6362 enum isl_dim_type type, unsigned pos, int value, int upper)
6364 int i;
6366 map = isl_map_cow(map);
6367 if (isl_map_check_range(map, type, pos, 1) < 0)
6368 return isl_map_free(map);
6369 for (i = 0; i < map->n; ++i) {
6370 map->p[i] = basic_map_bound_si(map->p[i],
6371 type, pos, value, upper);
6372 if (!map->p[i])
6373 goto error;
6375 map = isl_map_unmark_normalized(map);
6376 return map;
6377 error:
6378 isl_map_free(map);
6379 return NULL;
6382 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6383 enum isl_dim_type type, unsigned pos, int value)
6385 return map_bound_si(map, type, pos, value, 0);
6388 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6389 enum isl_dim_type type, unsigned pos, int value)
6391 return map_bound_si(map, type, pos, value, 1);
6394 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6395 enum isl_dim_type type, unsigned pos, int value)
6397 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6398 type, pos, value));
6401 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6402 enum isl_dim_type type, unsigned pos, int value)
6404 return isl_map_upper_bound_si(set, type, pos, value);
6407 /* Bound the given variable of "bmap" from below (or above is "upper"
6408 * is set) to "value".
6410 static __isl_give isl_basic_map *basic_map_bound(
6411 __isl_take isl_basic_map *bmap,
6412 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6414 int j;
6416 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6417 return isl_basic_map_free(bmap);
6418 pos += isl_basic_map_offset(bmap, type);
6419 bmap = isl_basic_map_cow(bmap);
6420 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6421 j = isl_basic_map_alloc_inequality(bmap);
6422 if (j < 0)
6423 goto error;
6424 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6425 if (upper) {
6426 isl_int_set_si(bmap->ineq[j][pos], -1);
6427 isl_int_set(bmap->ineq[j][0], value);
6428 } else {
6429 isl_int_set_si(bmap->ineq[j][pos], 1);
6430 isl_int_neg(bmap->ineq[j][0], value);
6432 bmap = isl_basic_map_simplify(bmap);
6433 return isl_basic_map_finalize(bmap);
6434 error:
6435 isl_basic_map_free(bmap);
6436 return NULL;
6439 /* Bound the given variable of "map" from below (or above is "upper"
6440 * is set) to "value".
6442 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6443 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6445 int i;
6447 map = isl_map_cow(map);
6448 if (isl_map_check_range(map, type, pos, 1) < 0)
6449 return isl_map_free(map);
6450 for (i = map->n - 1; i >= 0; --i) {
6451 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6452 map = remove_if_empty(map, i);
6453 if (!map)
6454 return NULL;
6456 map = isl_map_unmark_normalized(map);
6457 return map;
6460 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6461 enum isl_dim_type type, unsigned pos, isl_int value)
6463 return map_bound(map, type, pos, value, 0);
6466 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6467 enum isl_dim_type type, unsigned pos, isl_int value)
6469 return map_bound(map, type, pos, value, 1);
6472 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6473 enum isl_dim_type type, unsigned pos, isl_int value)
6475 return isl_map_lower_bound(set, type, pos, value);
6478 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6479 enum isl_dim_type type, unsigned pos, isl_int value)
6481 return isl_map_upper_bound(set, type, pos, value);
6484 /* Force the values of the variable at position "pos" of type "type" of "set"
6485 * to be no smaller than "value".
6487 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6488 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6490 if (!value)
6491 goto error;
6492 if (!isl_val_is_int(value))
6493 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6494 "expecting integer value", goto error);
6495 set = isl_set_lower_bound(set, type, pos, value->n);
6496 isl_val_free(value);
6497 return set;
6498 error:
6499 isl_val_free(value);
6500 isl_set_free(set);
6501 return NULL;
6504 /* Force the values of the variable at position "pos" of type "type" of "set"
6505 * to be no greater than "value".
6507 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6508 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6510 if (!value)
6511 goto error;
6512 if (!isl_val_is_int(value))
6513 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6514 "expecting integer value", goto error);
6515 set = isl_set_upper_bound(set, type, pos, value->n);
6516 isl_val_free(value);
6517 return set;
6518 error:
6519 isl_val_free(value);
6520 isl_set_free(set);
6521 return NULL;
6524 /* Bound the given variable of "bset" from below (or above is "upper"
6525 * is set) to "value".
6527 static __isl_give isl_basic_set *isl_basic_set_bound(
6528 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6529 isl_int value, int upper)
6531 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6532 type, pos, value, upper));
6535 /* Bound the given variable of "bset" from below (or above is "upper"
6536 * is set) to "value".
6538 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6539 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6540 __isl_take isl_val *value, int upper)
6542 if (!value)
6543 goto error;
6544 if (!isl_val_is_int(value))
6545 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6546 "expecting integer value", goto error);
6547 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6548 isl_val_free(value);
6549 return bset;
6550 error:
6551 isl_val_free(value);
6552 isl_basic_set_free(bset);
6553 return NULL;
6556 /* Bound the given variable of "bset" from below to "value".
6558 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6559 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6560 __isl_take isl_val *value)
6562 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6565 /* Bound the given variable of "bset" from above to "value".
6567 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6568 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6569 __isl_take isl_val *value)
6571 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6574 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6576 int i;
6578 map = isl_map_cow(map);
6579 if (!map)
6580 return NULL;
6582 map->dim = isl_space_reverse(map->dim);
6583 if (!map->dim)
6584 goto error;
6585 for (i = 0; i < map->n; ++i) {
6586 map->p[i] = isl_basic_map_reverse(map->p[i]);
6587 if (!map->p[i])
6588 goto error;
6590 map = isl_map_unmark_normalized(map);
6591 return map;
6592 error:
6593 isl_map_free(map);
6594 return NULL;
6597 #undef TYPE
6598 #define TYPE isl_pw_multi_aff
6599 #undef SUFFIX
6600 #define SUFFIX _pw_multi_aff
6601 #undef EMPTY
6602 #define EMPTY isl_pw_multi_aff_empty
6603 #undef ADD
6604 #define ADD isl_pw_multi_aff_union_add
6605 #include "isl_map_lexopt_templ.c"
6607 /* Given a map "map", compute the lexicographically minimal
6608 * (or maximal) image element for each domain element in dom,
6609 * in the form of an isl_pw_multi_aff.
6610 * If "empty" is not NULL, then set *empty to those elements in dom that
6611 * do not have an image element.
6612 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6613 * should be computed over the domain of "map". "empty" is also NULL
6614 * in this case.
6616 * We first compute the lexicographically minimal or maximal element
6617 * in the first basic map. This results in a partial solution "res"
6618 * and a subset "todo" of dom that still need to be handled.
6619 * We then consider each of the remaining maps in "map" and successively
6620 * update both "res" and "todo".
6621 * If "empty" is NULL, then the todo sets are not needed and therefore
6622 * also not computed.
6624 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6625 __isl_take isl_map *map, __isl_take isl_set *dom,
6626 __isl_give isl_set **empty, unsigned flags)
6628 int i;
6629 int full;
6630 isl_pw_multi_aff *res;
6631 isl_set *todo;
6633 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6634 if (!map || (!full && !dom))
6635 goto error;
6637 if (isl_map_plain_is_empty(map)) {
6638 if (empty)
6639 *empty = dom;
6640 else
6641 isl_set_free(dom);
6642 return isl_pw_multi_aff_from_map(map);
6645 res = basic_map_partial_lexopt_pw_multi_aff(
6646 isl_basic_map_copy(map->p[0]),
6647 isl_set_copy(dom), empty, flags);
6649 if (empty)
6650 todo = *empty;
6651 for (i = 1; i < map->n; ++i) {
6652 isl_pw_multi_aff *res_i;
6654 res_i = basic_map_partial_lexopt_pw_multi_aff(
6655 isl_basic_map_copy(map->p[i]),
6656 isl_set_copy(dom), empty, flags);
6658 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6659 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6660 else
6661 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6663 if (empty)
6664 todo = isl_set_intersect(todo, *empty);
6667 isl_set_free(dom);
6668 isl_map_free(map);
6670 if (empty)
6671 *empty = todo;
6673 return res;
6674 error:
6675 if (empty)
6676 *empty = NULL;
6677 isl_set_free(dom);
6678 isl_map_free(map);
6679 return NULL;
6682 #undef TYPE
6683 #define TYPE isl_map
6684 #undef SUFFIX
6685 #define SUFFIX
6686 #undef EMPTY
6687 #define EMPTY isl_map_empty
6688 #undef ADD
6689 #define ADD isl_map_union_disjoint
6690 #include "isl_map_lexopt_templ.c"
6692 /* Given a map "map", compute the lexicographically minimal
6693 * (or maximal) image element for each domain element in "dom",
6694 * in the form of an isl_map.
6695 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6696 * do not have an image element.
6697 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6698 * should be computed over the domain of "map". "empty" is also NULL
6699 * in this case.
6701 * If the input consists of more than one disjunct, then first
6702 * compute the desired result in the form of an isl_pw_multi_aff and
6703 * then convert that into an isl_map.
6705 * This function used to have an explicit implementation in terms
6706 * of isl_maps, but it would continually intersect the domains of
6707 * partial results with the complement of the domain of the next
6708 * partial solution, potentially leading to an explosion in the number
6709 * of disjuncts if there are several disjuncts in the input.
6710 * An even earlier implementation of this function would look for
6711 * better results in the domain of the partial result and for extra
6712 * results in the complement of this domain, which would lead to
6713 * even more splintering.
6715 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6716 __isl_take isl_map *map, __isl_take isl_set *dom,
6717 __isl_give isl_set **empty, unsigned flags)
6719 int full;
6720 struct isl_map *res;
6721 isl_pw_multi_aff *pma;
6723 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6724 if (!map || (!full && !dom))
6725 goto error;
6727 if (isl_map_plain_is_empty(map)) {
6728 if (empty)
6729 *empty = dom;
6730 else
6731 isl_set_free(dom);
6732 return map;
6735 if (map->n == 1) {
6736 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6737 dom, empty, flags);
6738 isl_map_free(map);
6739 return res;
6742 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6743 flags);
6744 return isl_map_from_pw_multi_aff(pma);
6745 error:
6746 if (empty)
6747 *empty = NULL;
6748 isl_set_free(dom);
6749 isl_map_free(map);
6750 return NULL;
6753 __isl_give isl_map *isl_map_partial_lexmax(
6754 __isl_take isl_map *map, __isl_take isl_set *dom,
6755 __isl_give isl_set **empty)
6757 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6760 __isl_give isl_map *isl_map_partial_lexmin(
6761 __isl_take isl_map *map, __isl_take isl_set *dom,
6762 __isl_give isl_set **empty)
6764 return isl_map_partial_lexopt(map, dom, empty, 0);
6767 __isl_give isl_set *isl_set_partial_lexmin(
6768 __isl_take isl_set *set, __isl_take isl_set *dom,
6769 __isl_give isl_set **empty)
6771 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6772 dom, empty));
6775 __isl_give isl_set *isl_set_partial_lexmax(
6776 __isl_take isl_set *set, __isl_take isl_set *dom,
6777 __isl_give isl_set **empty)
6779 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6780 dom, empty));
6783 /* Compute the lexicographic minimum (or maximum if "flags" includes
6784 * ISL_OPT_MAX) of "bset" over its parametric domain.
6786 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6787 unsigned flags)
6789 return isl_basic_map_lexopt(bset, flags);
6792 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6794 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6797 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6799 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6802 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6804 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6807 /* Compute the lexicographic minimum of "bset" over its parametric domain
6808 * for the purpose of quantifier elimination.
6809 * That is, find an explicit representation for all the existentially
6810 * quantified variables in "bset" by computing their lexicographic
6811 * minimum.
6813 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6814 __isl_take isl_basic_set *bset)
6816 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6819 /* Given a basic map with one output dimension, compute the minimum or
6820 * maximum of that dimension as an isl_pw_aff.
6822 * Compute the optimum as a lexicographic optimum over the single
6823 * output dimension and extract the single isl_pw_aff from the result.
6825 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6826 int max)
6828 isl_pw_multi_aff *pma;
6829 isl_pw_aff *pwaff;
6831 bmap = isl_basic_map_copy(bmap);
6832 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6833 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6834 isl_pw_multi_aff_free(pma);
6836 return pwaff;
6839 /* Compute the minimum or maximum of the given output dimension
6840 * as a function of the parameters and the input dimensions,
6841 * but independently of the other output dimensions.
6843 * We first project out the other output dimension and then compute
6844 * the "lexicographic" maximum in each basic map, combining the results
6845 * using isl_pw_aff_union_max.
6847 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6848 int max)
6850 int i;
6851 isl_pw_aff *pwaff;
6852 unsigned n_out;
6854 n_out = isl_map_dim(map, isl_dim_out);
6855 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6856 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6857 if (!map)
6858 return NULL;
6860 if (map->n == 0) {
6861 isl_space *space = isl_map_get_space(map);
6862 isl_map_free(map);
6863 return isl_pw_aff_empty(space);
6866 pwaff = basic_map_dim_opt(map->p[0], max);
6867 for (i = 1; i < map->n; ++i) {
6868 isl_pw_aff *pwaff_i;
6870 pwaff_i = basic_map_dim_opt(map->p[i], max);
6871 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6874 isl_map_free(map);
6876 return pwaff;
6879 /* Compute the minimum of the given output dimension as a function of the
6880 * parameters and input dimensions, but independently of
6881 * the other output dimensions.
6883 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6885 return map_dim_opt(map, pos, 0);
6888 /* Compute the maximum of the given output dimension as a function of the
6889 * parameters and input dimensions, but independently of
6890 * the other output dimensions.
6892 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6894 return map_dim_opt(map, pos, 1);
6897 /* Compute the minimum or maximum of the given set dimension
6898 * as a function of the parameters,
6899 * but independently of the other set dimensions.
6901 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6902 int max)
6904 return map_dim_opt(set, pos, max);
6907 /* Compute the maximum of the given set dimension as a function of the
6908 * parameters, but independently of the other set dimensions.
6910 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6912 return set_dim_opt(set, pos, 1);
6915 /* Compute the minimum of the given set dimension as a function of the
6916 * parameters, but independently of the other set dimensions.
6918 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6920 return set_dim_opt(set, pos, 0);
6923 /* Apply a preimage specified by "mat" on the parameters of "bset".
6924 * bset is assumed to have only parameters and divs.
6926 static __isl_give isl_basic_set *basic_set_parameter_preimage(
6927 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
6929 unsigned nparam;
6931 if (!bset || !mat)
6932 goto error;
6934 bset->dim = isl_space_cow(bset->dim);
6935 if (!bset->dim)
6936 goto error;
6938 nparam = isl_basic_set_dim(bset, isl_dim_param);
6940 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6942 bset->dim->nparam = 0;
6943 bset->dim->n_out = nparam;
6944 bset = isl_basic_set_preimage(bset, mat);
6945 if (bset) {
6946 bset->dim->nparam = bset->dim->n_out;
6947 bset->dim->n_out = 0;
6949 return bset;
6950 error:
6951 isl_mat_free(mat);
6952 isl_basic_set_free(bset);
6953 return NULL;
6956 /* Apply a preimage specified by "mat" on the parameters of "set".
6957 * set is assumed to have only parameters and divs.
6959 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6960 __isl_take isl_mat *mat)
6962 isl_space *space;
6963 unsigned nparam;
6965 if (!set || !mat)
6966 goto error;
6968 nparam = isl_set_dim(set, isl_dim_param);
6970 if (mat->n_row != 1 + nparam)
6971 isl_die(isl_set_get_ctx(set), isl_error_internal,
6972 "unexpected number of rows", goto error);
6974 space = isl_set_get_space(set);
6975 space = isl_space_move_dims(space, isl_dim_set, 0,
6976 isl_dim_param, 0, nparam);
6977 set = isl_set_reset_space(set, space);
6978 set = isl_set_preimage(set, mat);
6979 nparam = isl_set_dim(set, isl_dim_out);
6980 space = isl_set_get_space(set);
6981 space = isl_space_move_dims(space, isl_dim_param, 0,
6982 isl_dim_out, 0, nparam);
6983 set = isl_set_reset_space(set, space);
6984 return set;
6985 error:
6986 isl_mat_free(mat);
6987 isl_set_free(set);
6988 return NULL;
6991 /* Intersect the basic set "bset" with the affine space specified by the
6992 * equalities in "eq".
6994 static __isl_give isl_basic_set *basic_set_append_equalities(
6995 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
6997 int i, k;
6998 unsigned len;
7000 if (!bset || !eq)
7001 goto error;
7003 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7004 eq->n_row, 0);
7005 if (!bset)
7006 goto error;
7008 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7009 for (i = 0; i < eq->n_row; ++i) {
7010 k = isl_basic_set_alloc_equality(bset);
7011 if (k < 0)
7012 goto error;
7013 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7014 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7016 isl_mat_free(eq);
7018 bset = isl_basic_set_gauss(bset, NULL);
7019 bset = isl_basic_set_finalize(bset);
7021 return bset;
7022 error:
7023 isl_mat_free(eq);
7024 isl_basic_set_free(bset);
7025 return NULL;
7028 /* Intersect the set "set" with the affine space specified by the
7029 * equalities in "eq".
7031 static struct isl_set *set_append_equalities(struct isl_set *set,
7032 struct isl_mat *eq)
7034 int i;
7036 if (!set || !eq)
7037 goto error;
7039 for (i = 0; i < set->n; ++i) {
7040 set->p[i] = basic_set_append_equalities(set->p[i],
7041 isl_mat_copy(eq));
7042 if (!set->p[i])
7043 goto error;
7045 isl_mat_free(eq);
7046 return set;
7047 error:
7048 isl_mat_free(eq);
7049 isl_set_free(set);
7050 return NULL;
7053 /* Given a basic set "bset" that only involves parameters and existentially
7054 * quantified variables, return the index of the first equality
7055 * that only involves parameters. If there is no such equality then
7056 * return bset->n_eq.
7058 * This function assumes that isl_basic_set_gauss has been called on "bset".
7060 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7062 int i, j;
7063 unsigned nparam, n_div;
7065 if (!bset)
7066 return -1;
7068 nparam = isl_basic_set_dim(bset, isl_dim_param);
7069 n_div = isl_basic_set_dim(bset, isl_dim_div);
7071 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7072 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7073 ++i;
7076 return i;
7079 /* Compute an explicit representation for the existentially quantified
7080 * variables in "bset" by computing the "minimal value" of the set
7081 * variables. Since there are no set variables, the computation of
7082 * the minimal value essentially computes an explicit representation
7083 * of the non-empty part(s) of "bset".
7085 * The input only involves parameters and existentially quantified variables.
7086 * All equalities among parameters have been removed.
7088 * Since the existentially quantified variables in the result are in general
7089 * going to be different from those in the input, we first replace
7090 * them by the minimal number of variables based on their equalities.
7091 * This should simplify the parametric integer programming.
7093 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7095 isl_morph *morph1, *morph2;
7096 isl_set *set;
7097 unsigned n;
7099 if (!bset)
7100 return NULL;
7101 if (bset->n_eq == 0)
7102 return isl_basic_set_lexmin_compute_divs(bset);
7104 morph1 = isl_basic_set_parameter_compression(bset);
7105 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7106 bset = isl_basic_set_lift(bset);
7107 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7108 bset = isl_morph_basic_set(morph2, bset);
7109 n = isl_basic_set_dim(bset, isl_dim_set);
7110 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7112 set = isl_basic_set_lexmin_compute_divs(bset);
7114 set = isl_morph_set(isl_morph_inverse(morph1), set);
7116 return set;
7119 /* Project the given basic set onto its parameter domain, possibly introducing
7120 * new, explicit, existential variables in the constraints.
7121 * The input has parameters and (possibly implicit) existential variables.
7122 * The output has the same parameters, but only
7123 * explicit existentially quantified variables.
7125 * The actual projection is performed by pip, but pip doesn't seem
7126 * to like equalities very much, so we first remove the equalities
7127 * among the parameters by performing a variable compression on
7128 * the parameters. Afterward, an inverse transformation is performed
7129 * and the equalities among the parameters are inserted back in.
7131 * The variable compression on the parameters may uncover additional
7132 * equalities that were only implicit before. We therefore check
7133 * if there are any new parameter equalities in the result and
7134 * if so recurse. The removal of parameter equalities is required
7135 * for the parameter compression performed by base_compute_divs.
7137 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7139 int i;
7140 struct isl_mat *eq;
7141 struct isl_mat *T, *T2;
7142 struct isl_set *set;
7143 unsigned nparam;
7145 bset = isl_basic_set_cow(bset);
7146 if (!bset)
7147 return NULL;
7149 if (bset->n_eq == 0)
7150 return base_compute_divs(bset);
7152 bset = isl_basic_set_gauss(bset, NULL);
7153 if (!bset)
7154 return NULL;
7155 if (isl_basic_set_plain_is_empty(bset))
7156 return isl_set_from_basic_set(bset);
7158 i = first_parameter_equality(bset);
7159 if (i == bset->n_eq)
7160 return base_compute_divs(bset);
7162 nparam = isl_basic_set_dim(bset, isl_dim_param);
7163 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7164 0, 1 + nparam);
7165 eq = isl_mat_cow(eq);
7166 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7167 if (T && T->n_col == 0) {
7168 isl_mat_free(T);
7169 isl_mat_free(T2);
7170 isl_mat_free(eq);
7171 bset = isl_basic_set_set_to_empty(bset);
7172 return isl_set_from_basic_set(bset);
7174 bset = basic_set_parameter_preimage(bset, T);
7176 i = first_parameter_equality(bset);
7177 if (!bset)
7178 set = NULL;
7179 else if (i == bset->n_eq)
7180 set = base_compute_divs(bset);
7181 else
7182 set = parameter_compute_divs(bset);
7183 set = set_parameter_preimage(set, T2);
7184 set = set_append_equalities(set, eq);
7185 return set;
7188 /* Insert the divs from "ls" before those of "bmap".
7190 * The number of columns is not changed, which means that the last
7191 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7192 * The caller is responsible for removing the same number of dimensions
7193 * from the space of "bmap".
7195 static __isl_give isl_basic_map *insert_divs_from_local_space(
7196 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7198 int i;
7199 int n_div;
7200 int old_n_div;
7202 n_div = isl_local_space_dim(ls, isl_dim_div);
7203 if (n_div == 0)
7204 return bmap;
7206 old_n_div = bmap->n_div;
7207 bmap = insert_div_rows(bmap, n_div);
7208 if (!bmap)
7209 return NULL;
7211 for (i = 0; i < n_div; ++i) {
7212 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7213 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7216 return bmap;
7219 /* Replace the space of "bmap" by the space and divs of "ls".
7221 * If "ls" has any divs, then we simplify the result since we may
7222 * have discovered some additional equalities that could simplify
7223 * the div expressions.
7225 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7226 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7228 int n_div;
7230 bmap = isl_basic_map_cow(bmap);
7231 if (!bmap || !ls)
7232 goto error;
7234 n_div = isl_local_space_dim(ls, isl_dim_div);
7235 bmap = insert_divs_from_local_space(bmap, ls);
7236 if (!bmap)
7237 goto error;
7239 isl_space_free(bmap->dim);
7240 bmap->dim = isl_local_space_get_space(ls);
7241 if (!bmap->dim)
7242 goto error;
7244 isl_local_space_free(ls);
7245 if (n_div > 0)
7246 bmap = isl_basic_map_simplify(bmap);
7247 bmap = isl_basic_map_finalize(bmap);
7248 return bmap;
7249 error:
7250 isl_basic_map_free(bmap);
7251 isl_local_space_free(ls);
7252 return NULL;
7255 /* Replace the space of "map" by the space and divs of "ls".
7257 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7258 __isl_take isl_local_space *ls)
7260 int i;
7262 map = isl_map_cow(map);
7263 if (!map || !ls)
7264 goto error;
7266 for (i = 0; i < map->n; ++i) {
7267 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7268 isl_local_space_copy(ls));
7269 if (!map->p[i])
7270 goto error;
7272 isl_space_free(map->dim);
7273 map->dim = isl_local_space_get_space(ls);
7274 if (!map->dim)
7275 goto error;
7277 isl_local_space_free(ls);
7278 return map;
7279 error:
7280 isl_local_space_free(ls);
7281 isl_map_free(map);
7282 return NULL;
7285 /* Compute an explicit representation for the existentially
7286 * quantified variables for which do not know any explicit representation yet.
7288 * We first sort the existentially quantified variables so that the
7289 * existentially quantified variables for which we already have an explicit
7290 * representation are placed before those for which we do not.
7291 * The input dimensions, the output dimensions and the existentially
7292 * quantified variables for which we already have an explicit
7293 * representation are then turned into parameters.
7294 * compute_divs returns a map with the same parameters and
7295 * no input or output dimensions and the dimension specification
7296 * is reset to that of the input, including the existentially quantified
7297 * variables for which we already had an explicit representation.
7299 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7301 struct isl_basic_set *bset;
7302 struct isl_set *set;
7303 struct isl_map *map;
7304 isl_space *space;
7305 isl_local_space *ls;
7306 unsigned nparam;
7307 unsigned n_in;
7308 unsigned n_out;
7309 int n_known;
7310 int i;
7312 bmap = isl_basic_map_sort_divs(bmap);
7313 bmap = isl_basic_map_cow(bmap);
7314 if (!bmap)
7315 return NULL;
7317 n_known = isl_basic_map_first_unknown_div(bmap);
7318 if (n_known < 0)
7319 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7321 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7322 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7323 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7324 space = isl_space_set_alloc(bmap->ctx,
7325 nparam + n_in + n_out + n_known, 0);
7326 if (!space)
7327 goto error;
7329 ls = isl_basic_map_get_local_space(bmap);
7330 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7331 n_known, bmap->n_div - n_known);
7332 if (n_known > 0) {
7333 for (i = n_known; i < bmap->n_div; ++i)
7334 swap_div(bmap, i - n_known, i);
7335 bmap->n_div -= n_known;
7336 bmap->extra -= n_known;
7338 bmap = isl_basic_map_reset_space(bmap, space);
7339 bset = bset_from_bmap(bmap);
7341 set = parameter_compute_divs(bset);
7342 map = set_to_map(set);
7343 map = replace_space_by_local_space(map, ls);
7345 return map;
7346 error:
7347 isl_basic_map_free(bmap);
7348 return NULL;
7351 /* Remove the explicit representation of local variable "div",
7352 * if there is any.
7354 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7355 __isl_take isl_basic_map *bmap, int div)
7357 isl_bool unknown;
7359 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7360 if (unknown < 0)
7361 return isl_basic_map_free(bmap);
7362 if (unknown)
7363 return bmap;
7365 bmap = isl_basic_map_cow(bmap);
7366 if (!bmap)
7367 return NULL;
7368 isl_int_set_si(bmap->div[div][0], 0);
7369 return bmap;
7372 /* Is local variable "div" of "bmap" marked as not having an explicit
7373 * representation?
7374 * Note that even if "div" is not marked in this way and therefore
7375 * has an explicit representation, this representation may still
7376 * depend (indirectly) on other local variables that do not
7377 * have an explicit representation.
7379 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7380 int div)
7382 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7383 return isl_bool_error;
7384 return isl_int_is_zero(bmap->div[div][0]);
7387 /* Return the position of the first local variable that does not
7388 * have an explicit representation.
7389 * Return the total number of local variables if they all have
7390 * an explicit representation.
7391 * Return -1 on error.
7393 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7395 int i;
7397 if (!bmap)
7398 return -1;
7400 for (i = 0; i < bmap->n_div; ++i) {
7401 if (!isl_basic_map_div_is_known(bmap, i))
7402 return i;
7404 return bmap->n_div;
7407 /* Return the position of the first local variable that does not
7408 * have an explicit representation.
7409 * Return the total number of local variables if they all have
7410 * an explicit representation.
7411 * Return -1 on error.
7413 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7415 return isl_basic_map_first_unknown_div(bset);
7418 /* Does "bmap" have an explicit representation for all local variables?
7420 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7422 int first, n;
7424 n = isl_basic_map_dim(bmap, isl_dim_div);
7425 first = isl_basic_map_first_unknown_div(bmap);
7426 if (first < 0)
7427 return isl_bool_error;
7428 return first == n;
7431 /* Do all basic maps in "map" have an explicit representation
7432 * for all local variables?
7434 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7436 int i;
7438 if (!map)
7439 return isl_bool_error;
7441 for (i = 0; i < map->n; ++i) {
7442 int known = isl_basic_map_divs_known(map->p[i]);
7443 if (known <= 0)
7444 return known;
7447 return isl_bool_true;
7450 /* If bmap contains any unknown divs, then compute explicit
7451 * expressions for them. However, this computation may be
7452 * quite expensive, so first try to remove divs that aren't
7453 * strictly needed.
7455 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7457 int known;
7458 struct isl_map *map;
7460 known = isl_basic_map_divs_known(bmap);
7461 if (known < 0)
7462 goto error;
7463 if (known)
7464 return isl_map_from_basic_map(bmap);
7466 bmap = isl_basic_map_drop_redundant_divs(bmap);
7468 known = isl_basic_map_divs_known(bmap);
7469 if (known < 0)
7470 goto error;
7471 if (known)
7472 return isl_map_from_basic_map(bmap);
7474 map = compute_divs(bmap);
7475 return map;
7476 error:
7477 isl_basic_map_free(bmap);
7478 return NULL;
7481 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7483 int i;
7484 int known;
7485 struct isl_map *res;
7487 if (!map)
7488 return NULL;
7489 if (map->n == 0)
7490 return map;
7492 known = isl_map_divs_known(map);
7493 if (known < 0) {
7494 isl_map_free(map);
7495 return NULL;
7497 if (known)
7498 return map;
7500 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7501 for (i = 1 ; i < map->n; ++i) {
7502 struct isl_map *r2;
7503 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7504 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7505 res = isl_map_union_disjoint(res, r2);
7506 else
7507 res = isl_map_union(res, r2);
7509 isl_map_free(map);
7511 return res;
7514 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7516 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7519 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7521 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7524 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7526 int i;
7527 struct isl_set *set;
7529 if (!map)
7530 goto error;
7532 map = isl_map_cow(map);
7533 if (!map)
7534 return NULL;
7536 set = set_from_map(map);
7537 set->dim = isl_space_domain(set->dim);
7538 if (!set->dim)
7539 goto error;
7540 for (i = 0; i < map->n; ++i) {
7541 set->p[i] = isl_basic_map_domain(map->p[i]);
7542 if (!set->p[i])
7543 goto error;
7545 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7546 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7547 return set;
7548 error:
7549 isl_map_free(map);
7550 return NULL;
7553 /* Return the union of "map1" and "map2", where we assume for now that
7554 * "map1" and "map2" are disjoint. Note that the basic maps inside
7555 * "map1" or "map2" may not be disjoint from each other.
7556 * Also note that this function is also called from isl_map_union,
7557 * which takes care of handling the situation where "map1" and "map2"
7558 * may not be disjoint.
7560 * If one of the inputs is empty, we can simply return the other input.
7561 * Similarly, if one of the inputs is universal, then it is equal to the union.
7563 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7564 __isl_take isl_map *map2)
7566 int i;
7567 unsigned flags = 0;
7568 struct isl_map *map = NULL;
7569 int is_universe;
7571 if (!map1 || !map2)
7572 goto error;
7574 if (!isl_space_is_equal(map1->dim, map2->dim))
7575 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7576 "spaces don't match", goto error);
7578 if (map1->n == 0) {
7579 isl_map_free(map1);
7580 return map2;
7582 if (map2->n == 0) {
7583 isl_map_free(map2);
7584 return map1;
7587 is_universe = isl_map_plain_is_universe(map1);
7588 if (is_universe < 0)
7589 goto error;
7590 if (is_universe) {
7591 isl_map_free(map2);
7592 return map1;
7595 is_universe = isl_map_plain_is_universe(map2);
7596 if (is_universe < 0)
7597 goto error;
7598 if (is_universe) {
7599 isl_map_free(map1);
7600 return map2;
7603 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7604 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7605 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7607 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7608 map1->n + map2->n, flags);
7609 if (!map)
7610 goto error;
7611 for (i = 0; i < map1->n; ++i) {
7612 map = isl_map_add_basic_map(map,
7613 isl_basic_map_copy(map1->p[i]));
7614 if (!map)
7615 goto error;
7617 for (i = 0; i < map2->n; ++i) {
7618 map = isl_map_add_basic_map(map,
7619 isl_basic_map_copy(map2->p[i]));
7620 if (!map)
7621 goto error;
7623 isl_map_free(map1);
7624 isl_map_free(map2);
7625 return map;
7626 error:
7627 isl_map_free(map);
7628 isl_map_free(map1);
7629 isl_map_free(map2);
7630 return NULL;
7633 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7634 * guaranteed to be disjoint by the caller.
7636 * Note that this functions is called from within isl_map_make_disjoint,
7637 * so we have to be careful not to touch the constraints of the inputs
7638 * in any way.
7640 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7641 __isl_take isl_map *map2)
7643 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7646 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7647 * not be disjoint. The parameters are assumed to have been aligned.
7649 * We currently simply call map_union_disjoint, the internal operation
7650 * of which does not really depend on the inputs being disjoint.
7651 * If the result contains more than one basic map, then we clear
7652 * the disjoint flag since the result may contain basic maps from
7653 * both inputs and these are not guaranteed to be disjoint.
7655 * As a special case, if "map1" and "map2" are obviously equal,
7656 * then we simply return "map1".
7658 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7659 __isl_take isl_map *map2)
7661 int equal;
7663 if (!map1 || !map2)
7664 goto error;
7666 equal = isl_map_plain_is_equal(map1, map2);
7667 if (equal < 0)
7668 goto error;
7669 if (equal) {
7670 isl_map_free(map2);
7671 return map1;
7674 map1 = map_union_disjoint(map1, map2);
7675 if (!map1)
7676 return NULL;
7677 if (map1->n > 1)
7678 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7679 return map1;
7680 error:
7681 isl_map_free(map1);
7682 isl_map_free(map2);
7683 return NULL;
7686 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7687 * not be disjoint.
7689 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7690 __isl_take isl_map *map2)
7692 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7695 __isl_give isl_set *isl_set_union_disjoint(
7696 __isl_take isl_set *set1, __isl_take isl_set *set2)
7698 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7699 set_to_map(set2)));
7702 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7704 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7707 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7708 * the results.
7710 * "map" and "set" are assumed to be compatible and non-NULL.
7712 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7713 __isl_take isl_set *set,
7714 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7715 __isl_take isl_basic_set *bset))
7717 unsigned flags = 0;
7718 struct isl_map *result;
7719 int i, j;
7721 if (isl_set_plain_is_universe(set)) {
7722 isl_set_free(set);
7723 return map;
7726 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7727 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7728 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7730 result = isl_map_alloc_space(isl_space_copy(map->dim),
7731 map->n * set->n, flags);
7732 for (i = 0; result && i < map->n; ++i)
7733 for (j = 0; j < set->n; ++j) {
7734 result = isl_map_add_basic_map(result,
7735 fn(isl_basic_map_copy(map->p[i]),
7736 isl_basic_set_copy(set->p[j])));
7737 if (!result)
7738 break;
7741 isl_map_free(map);
7742 isl_set_free(set);
7743 return result;
7746 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7747 __isl_take isl_set *set)
7749 isl_bool ok;
7751 ok = isl_map_compatible_range(map, set);
7752 if (ok < 0)
7753 goto error;
7754 if (!ok)
7755 isl_die(set->ctx, isl_error_invalid,
7756 "incompatible spaces", goto error);
7758 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7759 error:
7760 isl_map_free(map);
7761 isl_set_free(set);
7762 return NULL;
7765 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7766 __isl_take isl_set *set)
7768 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7771 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7772 __isl_take isl_set *set)
7774 isl_bool ok;
7776 ok = isl_map_compatible_domain(map, set);
7777 if (ok < 0)
7778 goto error;
7779 if (!ok)
7780 isl_die(set->ctx, isl_error_invalid,
7781 "incompatible spaces", goto error);
7783 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7784 error:
7785 isl_map_free(map);
7786 isl_set_free(set);
7787 return NULL;
7790 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7791 __isl_take isl_set *set)
7793 return isl_map_align_params_map_map_and(map, set,
7794 &map_intersect_domain);
7797 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7798 * in the space B -> C, return the intersection.
7799 * The parameters are assumed to have been aligned.
7801 * The map "factor" is first extended to a map living in the space
7802 * [A -> B] -> C and then a regular intersection is computed.
7804 static __isl_give isl_map *map_intersect_domain_factor_range(
7805 __isl_take isl_map *map, __isl_take isl_map *factor)
7807 isl_space *space;
7808 isl_map *ext_factor;
7810 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7811 ext_factor = isl_map_universe(space);
7812 ext_factor = isl_map_domain_product(ext_factor, factor);
7813 return map_intersect(map, ext_factor);
7816 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7817 * in the space B -> C, return the intersection.
7819 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7820 __isl_take isl_map *map, __isl_take isl_map *factor)
7822 return isl_map_align_params_map_map_and(map, factor,
7823 &map_intersect_domain_factor_range);
7826 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7827 * in the space A -> C, return the intersection.
7829 * The map "factor" is first extended to a map living in the space
7830 * A -> [B -> C] and then a regular intersection is computed.
7832 static __isl_give isl_map *map_intersect_range_factor_range(
7833 __isl_take isl_map *map, __isl_take isl_map *factor)
7835 isl_space *space;
7836 isl_map *ext_factor;
7838 space = isl_space_range_factor_domain(isl_map_get_space(map));
7839 ext_factor = isl_map_universe(space);
7840 ext_factor = isl_map_range_product(ext_factor, factor);
7841 return isl_map_intersect(map, ext_factor);
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 __isl_give isl_map *isl_map_intersect_range_factor_range(
7848 __isl_take isl_map *map, __isl_take isl_map *factor)
7850 return isl_map_align_params_map_map_and(map, factor,
7851 &map_intersect_range_factor_range);
7854 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7855 __isl_take isl_map *map2)
7857 if (!map1 || !map2)
7858 goto error;
7859 map1 = isl_map_reverse(map1);
7860 map1 = isl_map_apply_range(map1, map2);
7861 return isl_map_reverse(map1);
7862 error:
7863 isl_map_free(map1);
7864 isl_map_free(map2);
7865 return NULL;
7868 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7869 __isl_take isl_map *map2)
7871 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7874 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7875 __isl_take isl_map *map2)
7877 isl_space *space;
7878 struct isl_map *result;
7879 int i, j;
7881 if (!map1 || !map2)
7882 goto error;
7884 space = isl_space_join(isl_space_copy(map1->dim),
7885 isl_space_copy(map2->dim));
7887 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
7888 if (!result)
7889 goto error;
7890 for (i = 0; i < map1->n; ++i)
7891 for (j = 0; j < map2->n; ++j) {
7892 result = isl_map_add_basic_map(result,
7893 isl_basic_map_apply_range(
7894 isl_basic_map_copy(map1->p[i]),
7895 isl_basic_map_copy(map2->p[j])));
7896 if (!result)
7897 goto error;
7899 isl_map_free(map1);
7900 isl_map_free(map2);
7901 if (result && result->n <= 1)
7902 ISL_F_SET(result, ISL_MAP_DISJOINT);
7903 return result;
7904 error:
7905 isl_map_free(map1);
7906 isl_map_free(map2);
7907 return NULL;
7910 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7911 __isl_take isl_map *map2)
7913 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7917 * returns range - domain
7919 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
7921 isl_space *target_space;
7922 struct isl_basic_set *bset;
7923 unsigned dim;
7924 unsigned nparam;
7925 int i;
7927 if (!bmap)
7928 goto error;
7929 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7930 bmap->dim, isl_dim_out),
7931 goto error);
7932 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7933 dim = isl_basic_map_dim(bmap, isl_dim_in);
7934 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7935 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7936 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7937 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7938 for (i = 0; i < dim; ++i) {
7939 int j = isl_basic_map_alloc_equality(bmap);
7940 if (j < 0) {
7941 bmap = isl_basic_map_free(bmap);
7942 break;
7944 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7945 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7946 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7947 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7949 bset = isl_basic_map_domain(bmap);
7950 bset = isl_basic_set_reset_space(bset, target_space);
7951 return bset;
7952 error:
7953 isl_basic_map_free(bmap);
7954 return NULL;
7957 /* Check that domain and range of "map" are the same.
7959 isl_stat isl_map_check_equal_tuples(__isl_keep isl_map *map)
7961 isl_space *space;
7962 isl_bool equal;
7964 space = isl_map_peek_space(map);
7965 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
7966 if (equal < 0)
7967 return isl_stat_error;
7968 if (!equal)
7969 isl_die(isl_map_get_ctx(map), isl_error_invalid,
7970 "domain and range don't match", return isl_stat_error);
7971 return isl_stat_ok;
7975 * returns range - domain
7977 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7979 int i;
7980 isl_space *dim;
7981 struct isl_set *result;
7983 if (!map)
7984 return NULL;
7986 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7987 map->dim, isl_dim_out),
7988 goto error);
7989 dim = isl_map_get_space(map);
7990 dim = isl_space_domain(dim);
7991 result = isl_set_alloc_space(dim, map->n, 0);
7992 if (!result)
7993 goto error;
7994 for (i = 0; i < map->n; ++i)
7995 result = isl_set_add_basic_set(result,
7996 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7997 isl_map_free(map);
7998 return result;
7999 error:
8000 isl_map_free(map);
8001 return NULL;
8005 * returns [domain -> range] -> range - domain
8007 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8008 __isl_take isl_basic_map *bmap)
8010 int i, k;
8011 isl_space *space;
8012 isl_basic_map *domain;
8013 int nparam, n;
8014 unsigned total;
8016 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8017 bmap->dim, isl_dim_out))
8018 isl_die(bmap->ctx, isl_error_invalid,
8019 "domain and range don't match", goto error);
8021 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8022 n = isl_basic_map_dim(bmap, isl_dim_in);
8024 space = isl_basic_map_get_space(bmap);
8025 space = isl_space_from_range(isl_space_domain(space));
8026 domain = isl_basic_map_universe(space);
8028 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8029 bmap = isl_basic_map_apply_range(bmap, domain);
8030 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8032 total = isl_basic_map_total_dim(bmap);
8034 for (i = 0; i < n; ++i) {
8035 k = isl_basic_map_alloc_equality(bmap);
8036 if (k < 0)
8037 goto error;
8038 isl_seq_clr(bmap->eq[k], 1 + total);
8039 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8040 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8041 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8044 bmap = isl_basic_map_gauss(bmap, NULL);
8045 return isl_basic_map_finalize(bmap);
8046 error:
8047 isl_basic_map_free(bmap);
8048 return NULL;
8052 * returns [domain -> range] -> range - domain
8054 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8056 int i;
8057 isl_space *domain_space;
8059 if (isl_map_check_equal_tuples(map) < 0)
8060 return isl_map_free(map);
8062 map = isl_map_cow(map);
8063 if (!map)
8064 return NULL;
8066 domain_space = isl_space_domain(isl_map_get_space(map));
8067 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8068 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8069 if (!map->dim)
8070 goto error;
8071 for (i = 0; i < map->n; ++i) {
8072 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8073 if (!map->p[i])
8074 goto error;
8076 map = isl_map_unmark_normalized(map);
8077 return map;
8078 error:
8079 isl_map_free(map);
8080 return NULL;
8083 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8085 unsigned n_in, n_out;
8087 if (!space)
8088 return NULL;
8089 n_in = isl_space_dim(space, isl_dim_in);
8090 n_out = isl_space_dim(space, isl_dim_out);
8091 if (n_in != n_out)
8092 isl_die(space->ctx, isl_error_invalid,
8093 "number of input and output dimensions needs to be "
8094 "the same", goto error);
8095 return isl_basic_map_equal(space, n_in);
8096 error:
8097 isl_space_free(space);
8098 return NULL;
8101 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8103 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8106 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8108 isl_space *dim = isl_set_get_space(set);
8109 isl_map *id;
8110 id = isl_map_identity(isl_space_map_from_set(dim));
8111 return isl_map_intersect_range(id, set);
8114 /* Construct a basic set with all set dimensions having only non-negative
8115 * values.
8117 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8118 __isl_take isl_space *space)
8120 int i;
8121 unsigned nparam;
8122 unsigned dim;
8123 struct isl_basic_set *bset;
8125 if (!space)
8126 return NULL;
8127 nparam = space->nparam;
8128 dim = space->n_out;
8129 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8130 if (!bset)
8131 return NULL;
8132 for (i = 0; i < dim; ++i) {
8133 int k = isl_basic_set_alloc_inequality(bset);
8134 if (k < 0)
8135 goto error;
8136 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8137 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8139 return bset;
8140 error:
8141 isl_basic_set_free(bset);
8142 return NULL;
8145 /* Construct the half-space x_pos >= 0.
8147 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8148 int pos)
8150 int k;
8151 isl_basic_set *nonneg;
8153 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8154 k = isl_basic_set_alloc_inequality(nonneg);
8155 if (k < 0)
8156 goto error;
8157 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8158 isl_int_set_si(nonneg->ineq[k][pos], 1);
8160 return isl_basic_set_finalize(nonneg);
8161 error:
8162 isl_basic_set_free(nonneg);
8163 return NULL;
8166 /* Construct the half-space x_pos <= -1.
8168 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8169 int pos)
8171 int k;
8172 isl_basic_set *neg;
8174 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8175 k = isl_basic_set_alloc_inequality(neg);
8176 if (k < 0)
8177 goto error;
8178 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8179 isl_int_set_si(neg->ineq[k][0], -1);
8180 isl_int_set_si(neg->ineq[k][pos], -1);
8182 return isl_basic_set_finalize(neg);
8183 error:
8184 isl_basic_set_free(neg);
8185 return NULL;
8188 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8189 enum isl_dim_type type, unsigned first, unsigned n)
8191 int i;
8192 unsigned offset;
8193 isl_basic_set *nonneg;
8194 isl_basic_set *neg;
8196 if (n == 0)
8197 return set;
8199 if (isl_set_check_range(set, type, first, n) < 0)
8200 return isl_set_free(set);
8202 offset = pos(set->dim, type);
8203 for (i = 0; i < n; ++i) {
8204 nonneg = nonneg_halfspace(isl_set_get_space(set),
8205 offset + first + i);
8206 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8208 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8211 return set;
8214 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8215 int len,
8216 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8217 void *user)
8219 isl_set *half;
8221 if (!set)
8222 return isl_stat_error;
8223 if (isl_set_plain_is_empty(set)) {
8224 isl_set_free(set);
8225 return isl_stat_ok;
8227 if (first == len)
8228 return fn(set, signs, user);
8230 signs[first] = 1;
8231 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8232 1 + first));
8233 half = isl_set_intersect(half, isl_set_copy(set));
8234 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8235 goto error;
8237 signs[first] = -1;
8238 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8239 1 + first));
8240 half = isl_set_intersect(half, set);
8241 return foreach_orthant(half, signs, first + 1, len, fn, user);
8242 error:
8243 isl_set_free(set);
8244 return isl_stat_error;
8247 /* Call "fn" on the intersections of "set" with each of the orthants
8248 * (except for obviously empty intersections). The orthant is identified
8249 * by the signs array, with each entry having value 1 or -1 according
8250 * to the sign of the corresponding variable.
8252 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8253 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8254 void *user)
8256 unsigned nparam;
8257 unsigned nvar;
8258 int *signs;
8259 isl_stat r;
8261 if (!set)
8262 return isl_stat_error;
8263 if (isl_set_plain_is_empty(set))
8264 return isl_stat_ok;
8266 nparam = isl_set_dim(set, isl_dim_param);
8267 nvar = isl_set_dim(set, isl_dim_set);
8269 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8271 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8272 fn, user);
8274 free(signs);
8276 return r;
8279 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8281 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8284 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8285 __isl_keep isl_basic_map *bmap2)
8287 isl_bool is_subset;
8288 struct isl_map *map1;
8289 struct isl_map *map2;
8291 if (!bmap1 || !bmap2)
8292 return isl_bool_error;
8294 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8295 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8297 is_subset = isl_map_is_subset(map1, map2);
8299 isl_map_free(map1);
8300 isl_map_free(map2);
8302 return is_subset;
8305 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8306 __isl_keep isl_basic_set *bset2)
8308 return isl_basic_map_is_subset(bset1, bset2);
8311 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8312 __isl_keep isl_basic_map *bmap2)
8314 isl_bool is_subset;
8316 if (!bmap1 || !bmap2)
8317 return isl_bool_error;
8318 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8319 if (is_subset != isl_bool_true)
8320 return is_subset;
8321 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8322 return is_subset;
8325 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8326 __isl_keep isl_basic_set *bset2)
8328 return isl_basic_map_is_equal(
8329 bset_to_bmap(bset1), bset_to_bmap(bset2));
8332 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8334 int i;
8335 int is_empty;
8337 if (!map)
8338 return isl_bool_error;
8339 for (i = 0; i < map->n; ++i) {
8340 is_empty = isl_basic_map_is_empty(map->p[i]);
8341 if (is_empty < 0)
8342 return isl_bool_error;
8343 if (!is_empty)
8344 return isl_bool_false;
8346 return isl_bool_true;
8349 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8351 return map ? map->n == 0 : isl_bool_error;
8354 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8356 return set ? set->n == 0 : isl_bool_error;
8359 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8361 return isl_map_is_empty(set_to_map(set));
8364 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8365 __isl_keep isl_map *map2)
8367 if (!map1 || !map2)
8368 return isl_bool_error;
8370 return isl_space_is_equal(map1->dim, map2->dim);
8373 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8374 __isl_keep isl_set *set2)
8376 if (!set1 || !set2)
8377 return isl_bool_error;
8379 return isl_space_is_equal(set1->dim, set2->dim);
8382 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8384 isl_bool is_subset;
8386 if (!map1 || !map2)
8387 return isl_bool_error;
8388 is_subset = isl_map_is_subset(map1, map2);
8389 if (is_subset != isl_bool_true)
8390 return is_subset;
8391 is_subset = isl_map_is_subset(map2, map1);
8392 return is_subset;
8395 /* Is "map1" equal to "map2"?
8397 * First check if they are obviously equal.
8398 * If not, then perform a more detailed analysis.
8400 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8402 isl_bool equal;
8404 equal = isl_map_plain_is_equal(map1, map2);
8405 if (equal < 0 || equal)
8406 return equal;
8407 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8410 isl_bool isl_basic_map_is_strict_subset(
8411 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8413 isl_bool is_subset;
8415 if (!bmap1 || !bmap2)
8416 return isl_bool_error;
8417 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8418 if (is_subset != isl_bool_true)
8419 return is_subset;
8420 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8421 return isl_bool_not(is_subset);
8424 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8425 __isl_keep isl_map *map2)
8427 isl_bool is_subset;
8429 if (!map1 || !map2)
8430 return isl_bool_error;
8431 is_subset = isl_map_is_subset(map1, map2);
8432 if (is_subset != isl_bool_true)
8433 return is_subset;
8434 is_subset = isl_map_is_subset(map2, map1);
8435 return isl_bool_not(is_subset);
8438 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8439 __isl_keep isl_set *set2)
8441 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8444 /* Is "bmap" obviously equal to the universe with the same space?
8446 * That is, does it not have any constraints?
8448 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8450 if (!bmap)
8451 return isl_bool_error;
8452 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8455 /* Is "bset" obviously equal to the universe with the same space?
8457 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8459 return isl_basic_map_plain_is_universe(bset);
8462 /* If "c" does not involve any existentially quantified variables,
8463 * then set *univ to false and abort
8465 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8467 isl_bool *univ = user;
8468 unsigned n;
8470 n = isl_constraint_dim(c, isl_dim_div);
8471 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8472 isl_constraint_free(c);
8473 if (*univ < 0 || !*univ)
8474 return isl_stat_error;
8475 return isl_stat_ok;
8478 /* Is "bmap" equal to the universe with the same space?
8480 * First check if it is obviously equal to the universe.
8481 * If not and if there are any constraints not involving
8482 * existentially quantified variables, then it is certainly
8483 * not equal to the universe.
8484 * Otherwise, check if the universe is a subset of "bmap".
8486 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8488 isl_bool univ;
8489 isl_basic_map *test;
8491 univ = isl_basic_map_plain_is_universe(bmap);
8492 if (univ < 0 || univ)
8493 return univ;
8494 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8495 return isl_bool_false;
8496 univ = isl_bool_true;
8497 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8498 univ)
8499 return isl_bool_error;
8500 if (univ < 0 || !univ)
8501 return univ;
8502 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8503 univ = isl_basic_map_is_subset(test, bmap);
8504 isl_basic_map_free(test);
8505 return univ;
8508 /* Is "bset" equal to the universe with the same space?
8510 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8512 return isl_basic_map_is_universe(bset);
8515 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8517 int i;
8519 if (!map)
8520 return isl_bool_error;
8522 for (i = 0; i < map->n; ++i) {
8523 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8524 if (r < 0 || r)
8525 return r;
8528 return isl_bool_false;
8531 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8533 return isl_map_plain_is_universe(set_to_map(set));
8536 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8538 struct isl_basic_set *bset = NULL;
8539 struct isl_vec *sample = NULL;
8540 isl_bool empty, non_empty;
8542 if (!bmap)
8543 return isl_bool_error;
8545 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8546 return isl_bool_true;
8548 if (isl_basic_map_plain_is_universe(bmap))
8549 return isl_bool_false;
8551 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8552 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8553 copy = isl_basic_map_remove_redundancies(copy);
8554 empty = isl_basic_map_plain_is_empty(copy);
8555 isl_basic_map_free(copy);
8556 return empty;
8559 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8560 if (non_empty < 0)
8561 return isl_bool_error;
8562 if (non_empty)
8563 return isl_bool_false;
8564 isl_vec_free(bmap->sample);
8565 bmap->sample = NULL;
8566 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8567 if (!bset)
8568 return isl_bool_error;
8569 sample = isl_basic_set_sample_vec(bset);
8570 if (!sample)
8571 return isl_bool_error;
8572 empty = sample->size == 0;
8573 isl_vec_free(bmap->sample);
8574 bmap->sample = sample;
8575 if (empty)
8576 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8578 return empty;
8581 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8583 if (!bmap)
8584 return isl_bool_error;
8585 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8588 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8590 if (!bset)
8591 return isl_bool_error;
8592 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8595 /* Is "bmap" known to be non-empty?
8597 * That is, is the cached sample still valid?
8599 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8601 unsigned total;
8603 if (!bmap)
8604 return isl_bool_error;
8605 if (!bmap->sample)
8606 return isl_bool_false;
8607 total = 1 + isl_basic_map_total_dim(bmap);
8608 if (bmap->sample->size != total)
8609 return isl_bool_false;
8610 return isl_basic_map_contains(bmap, bmap->sample);
8613 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8615 return isl_basic_map_is_empty(bset_to_bmap(bset));
8618 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8619 __isl_take isl_basic_map *bmap2)
8621 struct isl_map *map;
8622 if (!bmap1 || !bmap2)
8623 goto error;
8625 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8627 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8628 if (!map)
8629 goto error;
8630 map = isl_map_add_basic_map(map, bmap1);
8631 map = isl_map_add_basic_map(map, bmap2);
8632 return map;
8633 error:
8634 isl_basic_map_free(bmap1);
8635 isl_basic_map_free(bmap2);
8636 return NULL;
8639 struct isl_set *isl_basic_set_union(
8640 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8642 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8643 bset_to_bmap(bset2)));
8646 /* Order divs such that any div only depends on previous divs */
8647 __isl_give isl_basic_map *isl_basic_map_order_divs(
8648 __isl_take isl_basic_map *bmap)
8650 int i;
8651 unsigned off;
8653 if (!bmap)
8654 return NULL;
8656 off = isl_space_dim(bmap->dim, isl_dim_all);
8658 for (i = 0; i < bmap->n_div; ++i) {
8659 int pos;
8660 if (isl_int_is_zero(bmap->div[i][0]))
8661 continue;
8662 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8663 bmap->n_div-i);
8664 if (pos == -1)
8665 continue;
8666 if (pos == 0)
8667 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8668 "integer division depends on itself",
8669 return isl_basic_map_free(bmap));
8670 bmap = isl_basic_map_swap_div(bmap, i, i + pos);
8671 if (!bmap)
8672 return NULL;
8673 --i;
8675 return bmap;
8678 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8680 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8683 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8685 int i;
8687 if (!map)
8688 return 0;
8690 for (i = 0; i < map->n; ++i) {
8691 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8692 if (!map->p[i])
8693 goto error;
8696 return map;
8697 error:
8698 isl_map_free(map);
8699 return NULL;
8702 /* Sort the local variables of "bset".
8704 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8705 __isl_take isl_basic_set *bset)
8707 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8710 /* Apply the expansion computed by isl_merge_divs.
8711 * The expansion itself is given by "exp" while the resulting
8712 * list of divs is given by "div".
8714 * Move the integer divisions of "bmap" into the right position
8715 * according to "exp" and then introduce the additional integer
8716 * divisions, adding div constraints.
8717 * The moving should be done first to avoid moving coefficients
8718 * in the definitions of the extra integer divisions.
8720 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8721 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8723 int i, j;
8724 int n_div;
8726 bmap = isl_basic_map_cow(bmap);
8727 if (!bmap || !div)
8728 goto error;
8730 if (div->n_row < bmap->n_div)
8731 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8732 "not an expansion", goto error);
8734 n_div = bmap->n_div;
8735 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8736 div->n_row - n_div, 0,
8737 2 * (div->n_row - n_div));
8739 for (i = n_div; i < div->n_row; ++i)
8740 if (isl_basic_map_alloc_div(bmap) < 0)
8741 goto error;
8743 for (j = n_div - 1; j >= 0; --j) {
8744 if (exp[j] == j)
8745 break;
8746 bmap = isl_basic_map_swap_div(bmap, j, exp[j]);
8747 if (!bmap)
8748 goto error;
8750 j = 0;
8751 for (i = 0; i < div->n_row; ++i) {
8752 if (j < n_div && exp[j] == i) {
8753 j++;
8754 } else {
8755 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8756 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8757 continue;
8758 bmap = isl_basic_map_add_div_constraints(bmap, i);
8759 if (!bmap)
8760 goto error;
8764 isl_mat_free(div);
8765 return bmap;
8766 error:
8767 isl_basic_map_free(bmap);
8768 isl_mat_free(div);
8769 return NULL;
8772 /* Apply the expansion computed by isl_merge_divs.
8773 * The expansion itself is given by "exp" while the resulting
8774 * list of divs is given by "div".
8776 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8777 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8779 return isl_basic_map_expand_divs(bset, div, exp);
8782 /* Look for a div in dst that corresponds to the div "div" in src.
8783 * The divs before "div" in src and dst are assumed to be the same.
8785 * Return the position of the corresponding div in dst
8786 * if there is one. Otherwise, return a position beyond the integer divisions.
8787 * Return -1 on error.
8789 static int find_div(__isl_keep isl_basic_map *dst,
8790 __isl_keep isl_basic_map *src, unsigned div)
8792 int i;
8793 unsigned n_div;
8794 unsigned total;
8796 if (!dst || !src)
8797 return -1;
8799 total = isl_space_dim(src->dim, isl_dim_all);
8800 n_div = isl_basic_map_dim(dst, isl_dim_div);
8801 isl_assert(dst->ctx, div <= n_div, return -1);
8802 for (i = div; i < n_div; ++i)
8803 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8804 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8805 n_div - div) == -1)
8806 return i;
8807 return n_div;
8810 /* Align the divs of "dst" to those of "src", adding divs from "src"
8811 * if needed. That is, make sure that the first src->n_div divs
8812 * of the result are equal to those of src.
8814 * The result is not finalized as by design it will have redundant
8815 * divs if any divs from "src" were copied.
8817 __isl_give isl_basic_map *isl_basic_map_align_divs(
8818 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8820 int i;
8821 isl_bool known;
8822 int extended;
8823 unsigned total, dst_n_div;
8825 if (!dst || !src)
8826 return isl_basic_map_free(dst);
8828 if (src->n_div == 0)
8829 return dst;
8831 known = isl_basic_map_divs_known(src);
8832 if (known < 0)
8833 return isl_basic_map_free(dst);
8834 if (!known)
8835 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8836 "some src divs are unknown",
8837 return isl_basic_map_free(dst));
8839 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8840 if (!src)
8841 return isl_basic_map_free(dst);
8843 extended = 0;
8844 total = isl_space_dim(src->dim, isl_dim_all);
8845 dst_n_div = isl_basic_map_dim(dst, isl_dim_div);
8846 for (i = 0; i < src->n_div; ++i) {
8847 int j = find_div(dst, src, i);
8848 if (j < 0)
8849 dst = isl_basic_map_free(dst);
8850 if (j == dst_n_div) {
8851 if (!extended) {
8852 int extra = src->n_div - i;
8853 dst = isl_basic_map_cow(dst);
8854 if (!dst)
8855 goto error;
8856 dst = isl_basic_map_extend_space(dst,
8857 isl_space_copy(dst->dim),
8858 extra, 0, 2 * extra);
8859 extended = 1;
8861 j = isl_basic_map_alloc_div(dst);
8862 if (j < 0)
8863 goto error;
8864 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8865 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8866 dst_n_div++;
8867 dst = isl_basic_map_add_div_constraints(dst, j);
8868 if (!dst)
8869 goto error;
8871 if (j != i)
8872 dst = isl_basic_map_swap_div(dst, i, j);
8873 if (!dst)
8874 goto error;
8876 isl_basic_map_free(src);
8877 return dst;
8878 error:
8879 isl_basic_map_free(src);
8880 isl_basic_map_free(dst);
8881 return NULL;
8884 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8886 int i;
8888 if (!map)
8889 return NULL;
8890 if (map->n == 0)
8891 return map;
8892 map = isl_map_compute_divs(map);
8893 map = isl_map_cow(map);
8894 if (!map)
8895 return NULL;
8897 for (i = 1; i < map->n; ++i)
8898 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8899 for (i = 1; i < map->n; ++i) {
8900 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8901 if (!map->p[i])
8902 return isl_map_free(map);
8905 map = isl_map_unmark_normalized(map);
8906 return map;
8909 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8911 return isl_map_align_divs_internal(map);
8914 struct isl_set *isl_set_align_divs(struct isl_set *set)
8916 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8919 /* Align the divs of the basic maps in "map" to those
8920 * of the basic maps in "list", as well as to the other basic maps in "map".
8921 * The elements in "list" are assumed to have known divs.
8923 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8924 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8926 int i, n;
8928 map = isl_map_compute_divs(map);
8929 map = isl_map_cow(map);
8930 if (!map || !list)
8931 return isl_map_free(map);
8932 if (map->n == 0)
8933 return map;
8935 n = isl_basic_map_list_n_basic_map(list);
8936 for (i = 0; i < n; ++i) {
8937 isl_basic_map *bmap;
8939 bmap = isl_basic_map_list_get_basic_map(list, i);
8940 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8941 isl_basic_map_free(bmap);
8943 if (!map->p[0])
8944 return isl_map_free(map);
8946 return isl_map_align_divs_internal(map);
8949 /* Align the divs of each element of "list" to those of "bmap".
8950 * Both "bmap" and the elements of "list" are assumed to have known divs.
8952 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8953 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8955 int i, n;
8957 if (!list || !bmap)
8958 return isl_basic_map_list_free(list);
8960 n = isl_basic_map_list_n_basic_map(list);
8961 for (i = 0; i < n; ++i) {
8962 isl_basic_map *bmap_i;
8964 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8965 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8966 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8969 return list;
8972 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8973 __isl_take isl_map *map)
8975 isl_bool ok;
8977 ok = isl_map_compatible_domain(map, set);
8978 if (ok < 0)
8979 goto error;
8980 if (!ok)
8981 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8982 "incompatible spaces", goto error);
8983 map = isl_map_intersect_domain(map, set);
8984 set = isl_map_range(map);
8985 return set;
8986 error:
8987 isl_set_free(set);
8988 isl_map_free(map);
8989 return NULL;
8992 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8993 __isl_take isl_map *map)
8995 return isl_map_align_params_map_map_and(set, map, &set_apply);
8998 /* There is no need to cow as removing empty parts doesn't change
8999 * the meaning of the set.
9001 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9003 int i;
9005 if (!map)
9006 return NULL;
9008 for (i = map->n - 1; i >= 0; --i)
9009 map = remove_if_empty(map, i);
9011 return map;
9014 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9016 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9019 /* Create a binary relation that maps the shared initial "pos" dimensions
9020 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9022 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9023 __isl_keep isl_basic_set *bset2, int pos)
9025 isl_basic_map *bmap1;
9026 isl_basic_map *bmap2;
9028 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9029 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9030 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9031 isl_dim_out, 0, pos);
9032 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9033 isl_dim_out, 0, pos);
9034 return isl_basic_map_range_product(bmap1, bmap2);
9037 /* Given two basic sets bset1 and bset2, compute the maximal difference
9038 * between the values of dimension pos in bset1 and those in bset2
9039 * for any common value of the parameters and dimensions preceding pos.
9041 static enum isl_lp_result basic_set_maximal_difference_at(
9042 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9043 int pos, isl_int *opt)
9045 isl_basic_map *bmap1;
9046 struct isl_ctx *ctx;
9047 struct isl_vec *obj;
9048 unsigned total;
9049 unsigned nparam;
9050 unsigned dim1;
9051 enum isl_lp_result res;
9053 if (!bset1 || !bset2)
9054 return isl_lp_error;
9056 nparam = isl_basic_set_n_param(bset1);
9057 dim1 = isl_basic_set_n_dim(bset1);
9059 bmap1 = join_initial(bset1, bset2, pos);
9060 if (!bmap1)
9061 return isl_lp_error;
9063 total = isl_basic_map_total_dim(bmap1);
9064 ctx = bmap1->ctx;
9065 obj = isl_vec_alloc(ctx, 1 + total);
9066 if (!obj)
9067 goto error;
9068 isl_seq_clr(obj->block.data, 1 + total);
9069 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9070 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9071 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9072 opt, NULL, NULL);
9073 isl_basic_map_free(bmap1);
9074 isl_vec_free(obj);
9075 return res;
9076 error:
9077 isl_basic_map_free(bmap1);
9078 return isl_lp_error;
9081 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9082 * for any common value of the parameters and dimensions preceding pos
9083 * in both basic sets, the values of dimension pos in bset1 are
9084 * smaller or larger than those in bset2.
9086 * Returns
9087 * 1 if bset1 follows bset2
9088 * -1 if bset1 precedes bset2
9089 * 0 if bset1 and bset2 are incomparable
9090 * -2 if some error occurred.
9092 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9093 __isl_keep isl_basic_set *bset2, int pos)
9095 isl_int opt;
9096 enum isl_lp_result res;
9097 int cmp;
9099 isl_int_init(opt);
9101 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9103 if (res == isl_lp_empty)
9104 cmp = 0;
9105 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9106 res == isl_lp_unbounded)
9107 cmp = 1;
9108 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9109 cmp = -1;
9110 else
9111 cmp = -2;
9113 isl_int_clear(opt);
9114 return cmp;
9117 /* Given two basic sets bset1 and bset2, check whether
9118 * for any common value of the parameters and dimensions preceding pos
9119 * there is a value of dimension pos in bset1 that is larger
9120 * than a value of the same dimension in bset2.
9122 * Return
9123 * 1 if there exists such a pair
9124 * 0 if there is no such pair, but there is a pair of equal values
9125 * -1 otherwise
9126 * -2 if some error occurred.
9128 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9129 __isl_keep isl_basic_set *bset2, int pos)
9131 isl_bool empty;
9132 isl_basic_map *bmap;
9133 unsigned dim1;
9135 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9136 bmap = join_initial(bset1, bset2, pos);
9137 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9138 isl_dim_out, dim1 - pos);
9139 empty = isl_basic_map_is_empty(bmap);
9140 if (empty < 0)
9141 goto error;
9142 if (empty) {
9143 isl_basic_map_free(bmap);
9144 return -1;
9146 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9147 isl_dim_out, dim1 - pos);
9148 empty = isl_basic_map_is_empty(bmap);
9149 if (empty < 0)
9150 goto error;
9151 isl_basic_map_free(bmap);
9152 if (empty)
9153 return 0;
9154 return 1;
9155 error:
9156 isl_basic_map_free(bmap);
9157 return -2;
9160 /* Given two sets set1 and set2, check whether
9161 * for any common value of the parameters and dimensions preceding pos
9162 * there is a value of dimension pos in set1 that is larger
9163 * than a value of the same dimension in set2.
9165 * Return
9166 * 1 if there exists such a pair
9167 * 0 if there is no such pair, but there is a pair of equal values
9168 * -1 otherwise
9169 * -2 if some error occurred.
9171 int isl_set_follows_at(__isl_keep isl_set *set1,
9172 __isl_keep isl_set *set2, int pos)
9174 int i, j;
9175 int follows = -1;
9177 if (!set1 || !set2)
9178 return -2;
9180 for (i = 0; i < set1->n; ++i)
9181 for (j = 0; j < set2->n; ++j) {
9182 int f;
9183 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9184 if (f == 1 || f == -2)
9185 return f;
9186 if (f > follows)
9187 follows = f;
9190 return follows;
9193 static isl_bool isl_basic_map_plain_has_fixed_var(
9194 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9196 int i;
9197 int d;
9198 unsigned total;
9200 if (!bmap)
9201 return isl_bool_error;
9202 total = isl_basic_map_total_dim(bmap);
9203 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9204 for (; d+1 > pos; --d)
9205 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9206 break;
9207 if (d != pos)
9208 continue;
9209 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9210 return isl_bool_false;
9211 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9212 return isl_bool_false;
9213 if (!isl_int_is_one(bmap->eq[i][1+d]))
9214 return isl_bool_false;
9215 if (val)
9216 isl_int_neg(*val, bmap->eq[i][0]);
9217 return isl_bool_true;
9219 return isl_bool_false;
9222 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9223 unsigned pos, isl_int *val)
9225 int i;
9226 isl_int v;
9227 isl_int tmp;
9228 isl_bool fixed;
9230 if (!map)
9231 return isl_bool_error;
9232 if (map->n == 0)
9233 return isl_bool_false;
9234 if (map->n == 1)
9235 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9236 isl_int_init(v);
9237 isl_int_init(tmp);
9238 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9239 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9240 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9241 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9242 fixed = isl_bool_false;
9244 if (val)
9245 isl_int_set(*val, v);
9246 isl_int_clear(tmp);
9247 isl_int_clear(v);
9248 return fixed;
9251 static isl_bool isl_basic_set_plain_has_fixed_var(
9252 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9254 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9255 pos, val);
9258 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9259 enum isl_dim_type type, unsigned pos, isl_int *val)
9261 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9262 return isl_bool_error;
9263 return isl_basic_map_plain_has_fixed_var(bmap,
9264 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9267 /* If "bmap" obviously lies on a hyperplane where the given dimension
9268 * has a fixed value, then return that value.
9269 * Otherwise return NaN.
9271 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9272 __isl_keep isl_basic_map *bmap,
9273 enum isl_dim_type type, unsigned pos)
9275 isl_ctx *ctx;
9276 isl_val *v;
9277 isl_bool fixed;
9279 if (!bmap)
9280 return NULL;
9281 ctx = isl_basic_map_get_ctx(bmap);
9282 v = isl_val_alloc(ctx);
9283 if (!v)
9284 return NULL;
9285 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9286 if (fixed < 0)
9287 return isl_val_free(v);
9288 if (fixed) {
9289 isl_int_set_si(v->d, 1);
9290 return v;
9292 isl_val_free(v);
9293 return isl_val_nan(ctx);
9296 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9297 enum isl_dim_type type, unsigned pos, isl_int *val)
9299 if (isl_map_check_range(map, type, pos, 1) < 0)
9300 return isl_bool_error;
9301 return isl_map_plain_has_fixed_var(map,
9302 map_offset(map, type) - 1 + pos, val);
9305 /* If "map" obviously lies on a hyperplane where the given dimension
9306 * has a fixed value, then return that value.
9307 * Otherwise return NaN.
9309 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9310 enum isl_dim_type type, unsigned pos)
9312 isl_ctx *ctx;
9313 isl_val *v;
9314 isl_bool fixed;
9316 if (!map)
9317 return NULL;
9318 ctx = isl_map_get_ctx(map);
9319 v = isl_val_alloc(ctx);
9320 if (!v)
9321 return NULL;
9322 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9323 if (fixed < 0)
9324 return isl_val_free(v);
9325 if (fixed) {
9326 isl_int_set_si(v->d, 1);
9327 return v;
9329 isl_val_free(v);
9330 return isl_val_nan(ctx);
9333 /* If "set" obviously lies on a hyperplane where the given dimension
9334 * has a fixed value, then return that value.
9335 * Otherwise return NaN.
9337 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9338 enum isl_dim_type type, unsigned pos)
9340 return isl_map_plain_get_val_if_fixed(set, type, pos);
9343 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9344 * then return this fixed value in *val.
9346 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9347 unsigned dim, isl_int *val)
9349 return isl_basic_set_plain_has_fixed_var(bset,
9350 isl_basic_set_n_param(bset) + dim, val);
9353 /* Return -1 if the constraint "c1" should be sorted before "c2"
9354 * and 1 if it should be sorted after "c2".
9355 * Return 0 if the two constraints are the same (up to the constant term).
9357 * In particular, if a constraint involves later variables than another
9358 * then it is sorted after this other constraint.
9359 * uset_gist depends on constraints without existentially quantified
9360 * variables sorting first.
9362 * For constraints that have the same latest variable, those
9363 * with the same coefficient for this latest variable (first in absolute value
9364 * and then in actual value) are grouped together.
9365 * This is useful for detecting pairs of constraints that can
9366 * be chained in their printed representation.
9368 * Finally, within a group, constraints are sorted according to
9369 * their coefficients (excluding the constant term).
9371 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9373 isl_int **c1 = (isl_int **) p1;
9374 isl_int **c2 = (isl_int **) p2;
9375 int l1, l2;
9376 unsigned size = *(unsigned *) arg;
9377 int cmp;
9379 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9380 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9382 if (l1 != l2)
9383 return l1 - l2;
9385 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9386 if (cmp != 0)
9387 return cmp;
9388 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9389 if (cmp != 0)
9390 return -cmp;
9392 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9395 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9396 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9397 * and 0 if the two constraints are the same (up to the constant term).
9399 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9400 isl_int *c1, isl_int *c2)
9402 unsigned total;
9404 if (!bmap)
9405 return -2;
9406 total = isl_basic_map_total_dim(bmap);
9407 return sort_constraint_cmp(&c1, &c2, &total);
9410 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9411 __isl_take isl_basic_map *bmap)
9413 unsigned total;
9415 if (!bmap)
9416 return NULL;
9417 if (bmap->n_ineq == 0)
9418 return bmap;
9419 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9420 return bmap;
9421 total = isl_basic_map_total_dim(bmap);
9422 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9423 &sort_constraint_cmp, &total) < 0)
9424 return isl_basic_map_free(bmap);
9425 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9426 return bmap;
9429 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9430 __isl_take isl_basic_set *bset)
9432 isl_basic_map *bmap = bset_to_bmap(bset);
9433 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9436 __isl_give isl_basic_map *isl_basic_map_normalize(
9437 __isl_take isl_basic_map *bmap)
9439 bmap = isl_basic_map_remove_redundancies(bmap);
9440 bmap = isl_basic_map_sort_constraints(bmap);
9441 return bmap;
9443 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9444 __isl_keep isl_basic_map *bmap2)
9446 int i, cmp;
9447 unsigned total;
9448 isl_space *space1, *space2;
9450 if (!bmap1 || !bmap2)
9451 return -1;
9453 if (bmap1 == bmap2)
9454 return 0;
9455 space1 = isl_basic_map_peek_space(bmap1);
9456 space2 = isl_basic_map_peek_space(bmap2);
9457 cmp = isl_space_cmp(space1, space2);
9458 if (cmp)
9459 return cmp;
9460 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9461 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9462 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9463 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9464 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9465 return 0;
9466 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9467 return 1;
9468 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9469 return -1;
9470 if (bmap1->n_eq != bmap2->n_eq)
9471 return bmap1->n_eq - bmap2->n_eq;
9472 if (bmap1->n_ineq != bmap2->n_ineq)
9473 return bmap1->n_ineq - bmap2->n_ineq;
9474 if (bmap1->n_div != bmap2->n_div)
9475 return bmap1->n_div - bmap2->n_div;
9476 total = isl_basic_map_total_dim(bmap1);
9477 for (i = 0; i < bmap1->n_eq; ++i) {
9478 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9479 if (cmp)
9480 return cmp;
9482 for (i = 0; i < bmap1->n_ineq; ++i) {
9483 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9484 if (cmp)
9485 return cmp;
9487 for (i = 0; i < bmap1->n_div; ++i) {
9488 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9489 if (cmp)
9490 return cmp;
9492 return 0;
9495 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9496 __isl_keep isl_basic_set *bset2)
9498 return isl_basic_map_plain_cmp(bset1, bset2);
9501 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9503 int i, cmp;
9505 if (set1 == set2)
9506 return 0;
9507 if (set1->n != set2->n)
9508 return set1->n - set2->n;
9510 for (i = 0; i < set1->n; ++i) {
9511 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9512 if (cmp)
9513 return cmp;
9516 return 0;
9519 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9520 __isl_keep isl_basic_map *bmap2)
9522 if (!bmap1 || !bmap2)
9523 return isl_bool_error;
9524 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9527 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9528 __isl_keep isl_basic_set *bset2)
9530 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9531 bset_to_bmap(bset2));
9534 static int qsort_bmap_cmp(const void *p1, const void *p2)
9536 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9537 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9539 return isl_basic_map_plain_cmp(bmap1, bmap2);
9542 /* Sort the basic maps of "map" and remove duplicate basic maps.
9544 * While removing basic maps, we make sure that the basic maps remain
9545 * sorted because isl_map_normalize expects the basic maps of the result
9546 * to be sorted.
9548 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9550 int i, j;
9552 map = isl_map_remove_empty_parts(map);
9553 if (!map)
9554 return NULL;
9555 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9556 for (i = map->n - 1; i >= 1; --i) {
9557 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9558 continue;
9559 isl_basic_map_free(map->p[i-1]);
9560 for (j = i; j < map->n; ++j)
9561 map->p[j - 1] = map->p[j];
9562 map->n--;
9565 return map;
9568 /* Remove obvious duplicates among the basic maps of "map".
9570 * Unlike isl_map_normalize, this function does not remove redundant
9571 * constraints and only removes duplicates that have exactly the same
9572 * constraints in the input. It does sort the constraints and
9573 * the basic maps to ease the detection of duplicates.
9575 * If "map" has already been normalized or if the basic maps are
9576 * disjoint, then there can be no duplicates.
9578 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9580 int i;
9581 isl_basic_map *bmap;
9583 if (!map)
9584 return NULL;
9585 if (map->n <= 1)
9586 return map;
9587 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9588 return map;
9589 for (i = 0; i < map->n; ++i) {
9590 bmap = isl_basic_map_copy(map->p[i]);
9591 bmap = isl_basic_map_sort_constraints(bmap);
9592 if (!bmap)
9593 return isl_map_free(map);
9594 isl_basic_map_free(map->p[i]);
9595 map->p[i] = bmap;
9598 map = sort_and_remove_duplicates(map);
9599 return map;
9602 /* We normalize in place, but if anything goes wrong we need
9603 * to return NULL, so we need to make sure we don't change the
9604 * meaning of any possible other copies of map.
9606 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9608 int i;
9609 struct isl_basic_map *bmap;
9611 if (!map)
9612 return NULL;
9613 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9614 return map;
9615 for (i = 0; i < map->n; ++i) {
9616 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9617 if (!bmap)
9618 goto error;
9619 isl_basic_map_free(map->p[i]);
9620 map->p[i] = bmap;
9623 map = sort_and_remove_duplicates(map);
9624 if (map)
9625 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9626 return map;
9627 error:
9628 isl_map_free(map);
9629 return NULL;
9632 struct isl_set *isl_set_normalize(struct isl_set *set)
9634 return set_from_map(isl_map_normalize(set_to_map(set)));
9637 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9638 __isl_keep isl_map *map2)
9640 int i;
9641 isl_bool equal;
9643 if (!map1 || !map2)
9644 return isl_bool_error;
9646 if (map1 == map2)
9647 return isl_bool_true;
9648 if (!isl_space_is_equal(map1->dim, map2->dim))
9649 return isl_bool_false;
9651 map1 = isl_map_copy(map1);
9652 map2 = isl_map_copy(map2);
9653 map1 = isl_map_normalize(map1);
9654 map2 = isl_map_normalize(map2);
9655 if (!map1 || !map2)
9656 goto error;
9657 equal = map1->n == map2->n;
9658 for (i = 0; equal && i < map1->n; ++i) {
9659 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9660 if (equal < 0)
9661 goto error;
9663 isl_map_free(map1);
9664 isl_map_free(map2);
9665 return equal;
9666 error:
9667 isl_map_free(map1);
9668 isl_map_free(map2);
9669 return isl_bool_error;
9672 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9673 __isl_keep isl_set *set2)
9675 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9678 /* Return the basic maps in "map" as a list.
9680 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9681 __isl_keep isl_map *map)
9683 int i;
9684 isl_ctx *ctx;
9685 isl_basic_map_list *list;
9687 if (!map)
9688 return NULL;
9689 ctx = isl_map_get_ctx(map);
9690 list = isl_basic_map_list_alloc(ctx, map->n);
9692 for (i = 0; i < map->n; ++i) {
9693 isl_basic_map *bmap;
9695 bmap = isl_basic_map_copy(map->p[i]);
9696 list = isl_basic_map_list_add(list, bmap);
9699 return list;
9702 /* Return the intersection of the elements in the non-empty list "list".
9703 * All elements are assumed to live in the same space.
9705 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9706 __isl_take isl_basic_map_list *list)
9708 int i, n;
9709 isl_basic_map *bmap;
9711 if (!list)
9712 return NULL;
9713 n = isl_basic_map_list_n_basic_map(list);
9714 if (n < 1)
9715 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9716 "expecting non-empty list", goto error);
9718 bmap = isl_basic_map_list_get_basic_map(list, 0);
9719 for (i = 1; i < n; ++i) {
9720 isl_basic_map *bmap_i;
9722 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9723 bmap = isl_basic_map_intersect(bmap, bmap_i);
9726 isl_basic_map_list_free(list);
9727 return bmap;
9728 error:
9729 isl_basic_map_list_free(list);
9730 return NULL;
9733 /* Return the intersection of the elements in the non-empty list "list".
9734 * All elements are assumed to live in the same space.
9736 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9737 __isl_take isl_basic_set_list *list)
9739 return isl_basic_map_list_intersect(list);
9742 /* Return the union of the elements of "list".
9743 * The list is required to have at least one element.
9745 __isl_give isl_set *isl_basic_set_list_union(
9746 __isl_take isl_basic_set_list *list)
9748 int i, n;
9749 isl_space *space;
9750 isl_basic_set *bset;
9751 isl_set *set;
9753 if (!list)
9754 return NULL;
9755 n = isl_basic_set_list_n_basic_set(list);
9756 if (n < 1)
9757 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9758 "expecting non-empty list", goto error);
9760 bset = isl_basic_set_list_get_basic_set(list, 0);
9761 space = isl_basic_set_get_space(bset);
9762 isl_basic_set_free(bset);
9764 set = isl_set_alloc_space(space, n, 0);
9765 for (i = 0; i < n; ++i) {
9766 bset = isl_basic_set_list_get_basic_set(list, i);
9767 set = isl_set_add_basic_set(set, bset);
9770 isl_basic_set_list_free(list);
9771 return set;
9772 error:
9773 isl_basic_set_list_free(list);
9774 return NULL;
9777 /* Return the union of the elements in the non-empty list "list".
9778 * All elements are assumed to live in the same space.
9780 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9782 int i, n;
9783 isl_set *set;
9785 if (!list)
9786 return NULL;
9787 n = isl_set_list_n_set(list);
9788 if (n < 1)
9789 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9790 "expecting non-empty list", goto error);
9792 set = isl_set_list_get_set(list, 0);
9793 for (i = 1; i < n; ++i) {
9794 isl_set *set_i;
9796 set_i = isl_set_list_get_set(list, i);
9797 set = isl_set_union(set, set_i);
9800 isl_set_list_free(list);
9801 return set;
9802 error:
9803 isl_set_list_free(list);
9804 return NULL;
9807 __isl_give isl_basic_map *isl_basic_map_product(
9808 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9810 isl_space *space_result = NULL;
9811 struct isl_basic_map *bmap;
9812 unsigned in1, in2, out1, out2, nparam, total, pos;
9813 struct isl_dim_map *dim_map1, *dim_map2;
9815 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9816 goto error;
9817 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9818 isl_space_copy(bmap2->dim));
9820 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9821 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9822 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9823 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9824 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9826 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9827 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9828 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9829 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9830 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9831 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9832 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9833 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9834 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9835 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9836 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9838 bmap = isl_basic_map_alloc_space(space_result,
9839 bmap1->n_div + bmap2->n_div,
9840 bmap1->n_eq + bmap2->n_eq,
9841 bmap1->n_ineq + bmap2->n_ineq);
9842 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9843 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9844 bmap = isl_basic_map_simplify(bmap);
9845 return isl_basic_map_finalize(bmap);
9846 error:
9847 isl_basic_map_free(bmap1);
9848 isl_basic_map_free(bmap2);
9849 return NULL;
9852 __isl_give isl_basic_map *isl_basic_map_flat_product(
9853 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9855 isl_basic_map *prod;
9857 prod = isl_basic_map_product(bmap1, bmap2);
9858 prod = isl_basic_map_flatten(prod);
9859 return prod;
9862 __isl_give isl_basic_set *isl_basic_set_flat_product(
9863 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9865 return isl_basic_map_flat_range_product(bset1, bset2);
9868 __isl_give isl_basic_map *isl_basic_map_domain_product(
9869 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9871 isl_space *space_result = NULL;
9872 isl_basic_map *bmap;
9873 unsigned in1, in2, out, nparam, total, pos;
9874 struct isl_dim_map *dim_map1, *dim_map2;
9876 if (!bmap1 || !bmap2)
9877 goto error;
9879 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9880 isl_space_copy(bmap2->dim));
9882 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9883 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9884 out = isl_basic_map_dim(bmap1, isl_dim_out);
9885 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9887 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9888 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9889 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9890 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9891 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9892 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9893 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9894 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9895 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9896 isl_dim_map_div(dim_map1, bmap1, pos += out);
9897 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9899 bmap = isl_basic_map_alloc_space(space_result,
9900 bmap1->n_div + bmap2->n_div,
9901 bmap1->n_eq + bmap2->n_eq,
9902 bmap1->n_ineq + bmap2->n_ineq);
9903 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9904 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9905 bmap = isl_basic_map_simplify(bmap);
9906 return isl_basic_map_finalize(bmap);
9907 error:
9908 isl_basic_map_free(bmap1);
9909 isl_basic_map_free(bmap2);
9910 return NULL;
9913 __isl_give isl_basic_map *isl_basic_map_range_product(
9914 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9916 isl_bool rational;
9917 isl_space *space_result = NULL;
9918 isl_basic_map *bmap;
9919 unsigned in, out1, out2, nparam, total, pos;
9920 struct isl_dim_map *dim_map1, *dim_map2;
9922 rational = isl_basic_map_is_rational(bmap1);
9923 if (rational >= 0 && rational)
9924 rational = isl_basic_map_is_rational(bmap2);
9925 if (!bmap1 || !bmap2 || rational < 0)
9926 goto error;
9928 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9929 goto error;
9931 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9932 isl_space_copy(bmap2->dim));
9934 in = isl_basic_map_dim(bmap1, isl_dim_in);
9935 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9936 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9937 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9939 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9940 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9941 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9942 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9943 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9944 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9945 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9946 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9947 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9948 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9949 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9951 bmap = isl_basic_map_alloc_space(space_result,
9952 bmap1->n_div + bmap2->n_div,
9953 bmap1->n_eq + bmap2->n_eq,
9954 bmap1->n_ineq + bmap2->n_ineq);
9955 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9956 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9957 if (rational)
9958 bmap = isl_basic_map_set_rational(bmap);
9959 bmap = isl_basic_map_simplify(bmap);
9960 return isl_basic_map_finalize(bmap);
9961 error:
9962 isl_basic_map_free(bmap1);
9963 isl_basic_map_free(bmap2);
9964 return NULL;
9967 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9968 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9970 isl_basic_map *prod;
9972 prod = isl_basic_map_range_product(bmap1, bmap2);
9973 prod = isl_basic_map_flatten_range(prod);
9974 return prod;
9977 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9978 * and collect the results.
9979 * The result live in the space obtained by calling "space_product"
9980 * on the spaces of "map1" and "map2".
9981 * If "remove_duplicates" is set then the result may contain duplicates
9982 * (even if the inputs do not) and so we try and remove the obvious
9983 * duplicates.
9985 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9986 __isl_take isl_map *map2,
9987 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9988 __isl_take isl_space *right),
9989 __isl_give isl_basic_map *(*basic_map_product)(
9990 __isl_take isl_basic_map *left,
9991 __isl_take isl_basic_map *right),
9992 int remove_duplicates)
9994 unsigned flags = 0;
9995 struct isl_map *result;
9996 int i, j;
9997 isl_bool m;
9999 m = isl_map_has_equal_params(map1, map2);
10000 if (m < 0)
10001 goto error;
10002 if (!m)
10003 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10004 "parameters don't match", goto error);
10006 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10007 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10008 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10010 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10011 isl_space_copy(map2->dim)),
10012 map1->n * map2->n, flags);
10013 if (!result)
10014 goto error;
10015 for (i = 0; i < map1->n; ++i)
10016 for (j = 0; j < map2->n; ++j) {
10017 struct isl_basic_map *part;
10018 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10019 isl_basic_map_copy(map2->p[j]));
10020 if (isl_basic_map_is_empty(part))
10021 isl_basic_map_free(part);
10022 else
10023 result = isl_map_add_basic_map(result, part);
10024 if (!result)
10025 goto error;
10027 if (remove_duplicates)
10028 result = isl_map_remove_obvious_duplicates(result);
10029 isl_map_free(map1);
10030 isl_map_free(map2);
10031 return result;
10032 error:
10033 isl_map_free(map1);
10034 isl_map_free(map2);
10035 return NULL;
10038 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10040 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10041 __isl_take isl_map *map2)
10043 return map_product(map1, map2, &isl_space_product,
10044 &isl_basic_map_product, 0);
10047 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10048 __isl_take isl_map *map2)
10050 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10053 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10055 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10056 __isl_take isl_map *map2)
10058 isl_map *prod;
10060 prod = isl_map_product(map1, map2);
10061 prod = isl_map_flatten(prod);
10062 return prod;
10065 /* Given two set A and B, construct its Cartesian product A x B.
10067 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10069 return isl_map_range_product(set1, set2);
10072 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10073 __isl_take isl_set *set2)
10075 return isl_map_flat_range_product(set1, set2);
10078 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10080 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10081 __isl_take isl_map *map2)
10083 return map_product(map1, map2, &isl_space_domain_product,
10084 &isl_basic_map_domain_product, 1);
10087 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10089 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10090 __isl_take isl_map *map2)
10092 return map_product(map1, map2, &isl_space_range_product,
10093 &isl_basic_map_range_product, 1);
10096 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10097 __isl_take isl_map *map2)
10099 return isl_map_align_params_map_map_and(map1, map2,
10100 &map_domain_product_aligned);
10103 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10104 __isl_take isl_map *map2)
10106 return isl_map_align_params_map_map_and(map1, map2,
10107 &map_range_product_aligned);
10110 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10112 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10114 isl_space *space;
10115 int total1, keep1, total2, keep2;
10117 if (!map)
10118 return NULL;
10119 if (!isl_space_domain_is_wrapping(map->dim) ||
10120 !isl_space_range_is_wrapping(map->dim))
10121 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10122 "not a product", return isl_map_free(map));
10124 space = isl_map_get_space(map);
10125 total1 = isl_space_dim(space, isl_dim_in);
10126 total2 = isl_space_dim(space, isl_dim_out);
10127 space = isl_space_factor_domain(space);
10128 keep1 = isl_space_dim(space, isl_dim_in);
10129 keep2 = isl_space_dim(space, isl_dim_out);
10130 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10131 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10132 map = isl_map_reset_space(map, space);
10134 return map;
10137 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10139 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10141 isl_space *space;
10142 int total1, keep1, total2, keep2;
10144 if (!map)
10145 return NULL;
10146 if (!isl_space_domain_is_wrapping(map->dim) ||
10147 !isl_space_range_is_wrapping(map->dim))
10148 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10149 "not a product", return isl_map_free(map));
10151 space = isl_map_get_space(map);
10152 total1 = isl_space_dim(space, isl_dim_in);
10153 total2 = isl_space_dim(space, isl_dim_out);
10154 space = isl_space_factor_range(space);
10155 keep1 = isl_space_dim(space, isl_dim_in);
10156 keep2 = isl_space_dim(space, isl_dim_out);
10157 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10158 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10159 map = isl_map_reset_space(map, space);
10161 return map;
10164 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10166 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10168 isl_space *space;
10169 int total, keep;
10171 if (!map)
10172 return NULL;
10173 if (!isl_space_domain_is_wrapping(map->dim))
10174 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10175 "domain is not a product", return isl_map_free(map));
10177 space = isl_map_get_space(map);
10178 total = isl_space_dim(space, isl_dim_in);
10179 space = isl_space_domain_factor_domain(space);
10180 keep = isl_space_dim(space, isl_dim_in);
10181 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10182 map = isl_map_reset_space(map, space);
10184 return map;
10187 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10189 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10191 isl_space *space;
10192 int total, keep;
10194 if (!map)
10195 return NULL;
10196 if (!isl_space_domain_is_wrapping(map->dim))
10197 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10198 "domain is not a product", return isl_map_free(map));
10200 space = isl_map_get_space(map);
10201 total = isl_space_dim(space, isl_dim_in);
10202 space = isl_space_domain_factor_range(space);
10203 keep = isl_space_dim(space, isl_dim_in);
10204 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10205 map = isl_map_reset_space(map, space);
10207 return map;
10210 /* Given a map A -> [B -> C], extract the map A -> B.
10212 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10214 isl_space *space;
10215 int total, keep;
10217 if (!map)
10218 return NULL;
10219 if (!isl_space_range_is_wrapping(map->dim))
10220 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10221 "range is not a product", return isl_map_free(map));
10223 space = isl_map_get_space(map);
10224 total = isl_space_dim(space, isl_dim_out);
10225 space = isl_space_range_factor_domain(space);
10226 keep = isl_space_dim(space, isl_dim_out);
10227 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10228 map = isl_map_reset_space(map, space);
10230 return map;
10233 /* Given a map A -> [B -> C], extract the map A -> C.
10235 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10237 isl_space *space;
10238 int total, keep;
10240 if (!map)
10241 return NULL;
10242 if (!isl_space_range_is_wrapping(map->dim))
10243 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10244 "range is not a product", return isl_map_free(map));
10246 space = isl_map_get_space(map);
10247 total = isl_space_dim(space, isl_dim_out);
10248 space = isl_space_range_factor_range(space);
10249 keep = isl_space_dim(space, isl_dim_out);
10250 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10251 map = isl_map_reset_space(map, space);
10253 return map;
10256 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10258 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10259 __isl_take isl_map *map2)
10261 isl_map *prod;
10263 prod = isl_map_domain_product(map1, map2);
10264 prod = isl_map_flatten_domain(prod);
10265 return prod;
10268 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10270 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10271 __isl_take isl_map *map2)
10273 isl_map *prod;
10275 prod = isl_map_range_product(map1, map2);
10276 prod = isl_map_flatten_range(prod);
10277 return prod;
10280 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10282 int i;
10283 uint32_t hash = isl_hash_init();
10284 unsigned total;
10286 if (!bmap)
10287 return 0;
10288 bmap = isl_basic_map_copy(bmap);
10289 bmap = isl_basic_map_normalize(bmap);
10290 if (!bmap)
10291 return 0;
10292 total = isl_basic_map_total_dim(bmap);
10293 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10294 for (i = 0; i < bmap->n_eq; ++i) {
10295 uint32_t c_hash;
10296 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10297 isl_hash_hash(hash, c_hash);
10299 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10300 for (i = 0; i < bmap->n_ineq; ++i) {
10301 uint32_t c_hash;
10302 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10303 isl_hash_hash(hash, c_hash);
10305 isl_hash_byte(hash, bmap->n_div & 0xFF);
10306 for (i = 0; i < bmap->n_div; ++i) {
10307 uint32_t c_hash;
10308 if (isl_int_is_zero(bmap->div[i][0]))
10309 continue;
10310 isl_hash_byte(hash, i & 0xFF);
10311 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10312 isl_hash_hash(hash, c_hash);
10314 isl_basic_map_free(bmap);
10315 return hash;
10318 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10320 return isl_basic_map_get_hash(bset_to_bmap(bset));
10323 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10325 int i;
10326 uint32_t hash;
10328 if (!map)
10329 return 0;
10330 map = isl_map_copy(map);
10331 map = isl_map_normalize(map);
10332 if (!map)
10333 return 0;
10335 hash = isl_hash_init();
10336 for (i = 0; i < map->n; ++i) {
10337 uint32_t bmap_hash;
10338 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10339 isl_hash_hash(hash, bmap_hash);
10342 isl_map_free(map);
10344 return hash;
10347 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10349 return isl_map_get_hash(set_to_map(set));
10352 /* Return the number of basic maps in the (current) representation of "map".
10354 int isl_map_n_basic_map(__isl_keep isl_map *map)
10356 return map ? map->n : 0;
10359 int isl_set_n_basic_set(__isl_keep isl_set *set)
10361 return set ? set->n : 0;
10364 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10365 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10367 int i;
10369 if (!map)
10370 return isl_stat_error;
10372 for (i = 0; i < map->n; ++i)
10373 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10374 return isl_stat_error;
10376 return isl_stat_ok;
10379 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10380 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10382 int i;
10384 if (!set)
10385 return isl_stat_error;
10387 for (i = 0; i < set->n; ++i)
10388 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10389 return isl_stat_error;
10391 return isl_stat_ok;
10394 /* Return a list of basic sets, the union of which is equal to "set".
10396 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10397 __isl_keep isl_set *set)
10399 int i;
10400 isl_basic_set_list *list;
10402 if (!set)
10403 return NULL;
10405 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10406 for (i = 0; i < set->n; ++i) {
10407 isl_basic_set *bset;
10409 bset = isl_basic_set_copy(set->p[i]);
10410 list = isl_basic_set_list_add(list, bset);
10413 return list;
10416 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10418 isl_space *space;
10420 if (!bset)
10421 return NULL;
10423 bset = isl_basic_set_cow(bset);
10424 if (!bset)
10425 return NULL;
10427 space = isl_basic_set_get_space(bset);
10428 space = isl_space_lift(space, bset->n_div);
10429 if (!space)
10430 goto error;
10431 isl_space_free(bset->dim);
10432 bset->dim = space;
10433 bset->extra -= bset->n_div;
10434 bset->n_div = 0;
10436 bset = isl_basic_set_finalize(bset);
10438 return bset;
10439 error:
10440 isl_basic_set_free(bset);
10441 return NULL;
10444 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10446 int i;
10447 isl_space *space;
10448 unsigned n_div;
10450 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10452 if (!set)
10453 return NULL;
10455 set = isl_set_cow(set);
10456 if (!set)
10457 return NULL;
10459 n_div = set->p[0]->n_div;
10460 space = isl_set_get_space(set);
10461 space = isl_space_lift(space, n_div);
10462 if (!space)
10463 goto error;
10464 isl_space_free(set->dim);
10465 set->dim = space;
10467 for (i = 0; i < set->n; ++i) {
10468 set->p[i] = isl_basic_set_lift(set->p[i]);
10469 if (!set->p[i])
10470 goto error;
10473 return set;
10474 error:
10475 isl_set_free(set);
10476 return NULL;
10479 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10481 unsigned dim;
10482 int size = 0;
10484 if (!bset)
10485 return -1;
10487 dim = isl_basic_set_total_dim(bset);
10488 size += bset->n_eq * (1 + dim);
10489 size += bset->n_ineq * (1 + dim);
10490 size += bset->n_div * (2 + dim);
10492 return size;
10495 int isl_set_size(__isl_keep isl_set *set)
10497 int i;
10498 int size = 0;
10500 if (!set)
10501 return -1;
10503 for (i = 0; i < set->n; ++i)
10504 size += isl_basic_set_size(set->p[i]);
10506 return size;
10509 /* Check if there is any lower bound (if lower == 0) and/or upper
10510 * bound (if upper == 0) on the specified dim.
10512 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10513 enum isl_dim_type type, unsigned pos, int lower, int upper)
10515 int i;
10517 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10518 return isl_bool_error;
10520 pos += isl_basic_map_offset(bmap, type);
10522 for (i = 0; i < bmap->n_div; ++i) {
10523 if (isl_int_is_zero(bmap->div[i][0]))
10524 continue;
10525 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10526 return isl_bool_true;
10529 for (i = 0; i < bmap->n_eq; ++i)
10530 if (!isl_int_is_zero(bmap->eq[i][pos]))
10531 return isl_bool_true;
10533 for (i = 0; i < bmap->n_ineq; ++i) {
10534 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10535 if (sgn > 0)
10536 lower = 1;
10537 if (sgn < 0)
10538 upper = 1;
10541 return lower && upper;
10544 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10545 enum isl_dim_type type, unsigned pos)
10547 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10550 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10551 enum isl_dim_type type, unsigned pos)
10553 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10556 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10557 enum isl_dim_type type, unsigned pos)
10559 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10562 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10563 enum isl_dim_type type, unsigned pos)
10565 int i;
10567 if (!map)
10568 return isl_bool_error;
10570 for (i = 0; i < map->n; ++i) {
10571 isl_bool bounded;
10572 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10573 if (bounded < 0 || !bounded)
10574 return bounded;
10577 return isl_bool_true;
10580 /* Return true if the specified dim is involved in both an upper bound
10581 * and a lower bound.
10583 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10584 enum isl_dim_type type, unsigned pos)
10586 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10589 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10591 static isl_bool has_any_bound(__isl_keep isl_map *map,
10592 enum isl_dim_type type, unsigned pos,
10593 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10594 enum isl_dim_type type, unsigned pos))
10596 int i;
10598 if (!map)
10599 return isl_bool_error;
10601 for (i = 0; i < map->n; ++i) {
10602 isl_bool bounded;
10603 bounded = fn(map->p[i], type, pos);
10604 if (bounded < 0 || bounded)
10605 return bounded;
10608 return isl_bool_false;
10611 /* Return 1 if the specified dim is involved in any lower bound.
10613 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10614 enum isl_dim_type type, unsigned pos)
10616 return has_any_bound(set, type, pos,
10617 &isl_basic_map_dim_has_lower_bound);
10620 /* Return 1 if the specified dim is involved in any upper bound.
10622 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10623 enum isl_dim_type type, unsigned pos)
10625 return has_any_bound(set, type, pos,
10626 &isl_basic_map_dim_has_upper_bound);
10629 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10631 static isl_bool has_bound(__isl_keep isl_map *map,
10632 enum isl_dim_type type, unsigned pos,
10633 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10634 enum isl_dim_type type, unsigned pos))
10636 int i;
10638 if (!map)
10639 return isl_bool_error;
10641 for (i = 0; i < map->n; ++i) {
10642 isl_bool bounded;
10643 bounded = fn(map->p[i], type, pos);
10644 if (bounded < 0 || !bounded)
10645 return bounded;
10648 return isl_bool_true;
10651 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10653 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10654 enum isl_dim_type type, unsigned pos)
10656 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10659 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10661 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10662 enum isl_dim_type type, unsigned pos)
10664 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10667 /* For each of the "n" variables starting at "first", determine
10668 * the sign of the variable and put the results in the first "n"
10669 * elements of the array "signs".
10670 * Sign
10671 * 1 means that the variable is non-negative
10672 * -1 means that the variable is non-positive
10673 * 0 means the variable attains both positive and negative values.
10675 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10676 unsigned first, unsigned n, int *signs)
10678 isl_vec *bound = NULL;
10679 struct isl_tab *tab = NULL;
10680 struct isl_tab_undo *snap;
10681 int i;
10683 if (!bset || !signs)
10684 return isl_stat_error;
10686 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10687 tab = isl_tab_from_basic_set(bset, 0);
10688 if (!bound || !tab)
10689 goto error;
10691 isl_seq_clr(bound->el, bound->size);
10692 isl_int_set_si(bound->el[0], -1);
10694 snap = isl_tab_snap(tab);
10695 for (i = 0; i < n; ++i) {
10696 int empty;
10698 isl_int_set_si(bound->el[1 + first + i], -1);
10699 if (isl_tab_add_ineq(tab, bound->el) < 0)
10700 goto error;
10701 empty = tab->empty;
10702 isl_int_set_si(bound->el[1 + first + i], 0);
10703 if (isl_tab_rollback(tab, snap) < 0)
10704 goto error;
10706 if (empty) {
10707 signs[i] = 1;
10708 continue;
10711 isl_int_set_si(bound->el[1 + first + i], 1);
10712 if (isl_tab_add_ineq(tab, bound->el) < 0)
10713 goto error;
10714 empty = tab->empty;
10715 isl_int_set_si(bound->el[1 + first + i], 0);
10716 if (isl_tab_rollback(tab, snap) < 0)
10717 goto error;
10719 signs[i] = empty ? -1 : 0;
10722 isl_tab_free(tab);
10723 isl_vec_free(bound);
10724 return isl_stat_ok;
10725 error:
10726 isl_tab_free(tab);
10727 isl_vec_free(bound);
10728 return isl_stat_error;
10731 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10732 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10734 if (!bset || !signs)
10735 return isl_stat_error;
10736 if (isl_basic_set_check_range(bset, type, first, n) < 0)
10737 return isl_stat_error;
10739 first += pos(bset->dim, type) - 1;
10740 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10743 /* Is it possible for the integer division "div" to depend (possibly
10744 * indirectly) on any output dimensions?
10746 * If the div is undefined, then we conservatively assume that it
10747 * may depend on them.
10748 * Otherwise, we check if it actually depends on them or on any integer
10749 * divisions that may depend on them.
10751 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10753 int i;
10754 unsigned n_out, o_out;
10755 unsigned n_div, o_div;
10757 if (isl_int_is_zero(bmap->div[div][0]))
10758 return isl_bool_true;
10760 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10761 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10763 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10764 return isl_bool_true;
10766 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10767 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10769 for (i = 0; i < n_div; ++i) {
10770 isl_bool may_involve;
10772 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10773 continue;
10774 may_involve = div_may_involve_output(bmap, i);
10775 if (may_involve < 0 || may_involve)
10776 return may_involve;
10779 return isl_bool_false;
10782 /* Return the first integer division of "bmap" in the range
10783 * [first, first + n[ that may depend on any output dimensions and
10784 * that has a non-zero coefficient in "c" (where the first coefficient
10785 * in "c" corresponds to integer division "first").
10787 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10788 isl_int *c, int first, int n)
10790 int k;
10792 if (!bmap)
10793 return -1;
10795 for (k = first; k < first + n; ++k) {
10796 isl_bool may_involve;
10798 if (isl_int_is_zero(c[k]))
10799 continue;
10800 may_involve = div_may_involve_output(bmap, k);
10801 if (may_involve < 0)
10802 return -1;
10803 if (may_involve)
10804 return k;
10807 return first + n;
10810 /* Look for a pair of inequality constraints in "bmap" of the form
10812 * -l + i >= 0 or i >= l
10813 * and
10814 * n + l - i >= 0 or i <= l + n
10816 * with n < "m" and i the output dimension at position "pos".
10817 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10818 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10819 * and earlier output dimensions, as well as integer divisions that do
10820 * not involve any of the output dimensions.
10822 * Return the index of the first inequality constraint or bmap->n_ineq
10823 * if no such pair can be found.
10825 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10826 int pos, isl_int m)
10828 int i, j;
10829 isl_ctx *ctx;
10830 unsigned total;
10831 unsigned n_div, o_div;
10832 unsigned n_out, o_out;
10833 int less;
10835 if (!bmap)
10836 return -1;
10838 ctx = isl_basic_map_get_ctx(bmap);
10839 total = isl_basic_map_total_dim(bmap);
10840 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10841 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10842 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10843 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10844 for (i = 0; i < bmap->n_ineq; ++i) {
10845 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10846 continue;
10847 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10848 n_out - (pos + 1)) != -1)
10849 continue;
10850 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10851 0, n_div) < n_div)
10852 continue;
10853 for (j = i + 1; j < bmap->n_ineq; ++j) {
10854 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10855 ctx->one))
10856 continue;
10857 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10858 bmap->ineq[j] + 1, total))
10859 continue;
10860 break;
10862 if (j >= bmap->n_ineq)
10863 continue;
10864 isl_int_add(bmap->ineq[i][0],
10865 bmap->ineq[i][0], bmap->ineq[j][0]);
10866 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10867 isl_int_sub(bmap->ineq[i][0],
10868 bmap->ineq[i][0], bmap->ineq[j][0]);
10869 if (!less)
10870 continue;
10871 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10872 return i;
10873 else
10874 return j;
10877 return bmap->n_ineq;
10880 /* Return the index of the equality of "bmap" that defines
10881 * the output dimension "pos" in terms of earlier dimensions.
10882 * The equality may also involve integer divisions, as long
10883 * as those integer divisions are defined in terms of
10884 * parameters or input dimensions.
10885 * In this case, *div is set to the number of integer divisions and
10886 * *ineq is set to the number of inequality constraints (provided
10887 * div and ineq are not NULL).
10889 * The equality may also involve a single integer division involving
10890 * the output dimensions (typically only output dimension "pos") as
10891 * long as the coefficient of output dimension "pos" is 1 or -1 and
10892 * there is a pair of constraints i >= l and i <= l + n, with i referring
10893 * to output dimension "pos", l an expression involving only earlier
10894 * dimensions and n smaller than the coefficient of the integer division
10895 * in the equality. In this case, the output dimension can be defined
10896 * in terms of a modulo expression that does not involve the integer division.
10897 * *div is then set to this single integer division and
10898 * *ineq is set to the index of constraint i >= l.
10900 * Return bmap->n_eq if there is no such equality.
10901 * Return -1 on error.
10903 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10904 int pos, int *div, int *ineq)
10906 int j, k, l;
10907 unsigned n_out, o_out;
10908 unsigned n_div, o_div;
10910 if (!bmap)
10911 return -1;
10913 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10914 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10915 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10916 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10918 if (ineq)
10919 *ineq = bmap->n_ineq;
10920 if (div)
10921 *div = n_div;
10922 for (j = 0; j < bmap->n_eq; ++j) {
10923 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10924 continue;
10925 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10926 n_out - (pos + 1)) != -1)
10927 continue;
10928 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10929 0, n_div);
10930 if (k >= n_div)
10931 return j;
10932 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10933 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10934 continue;
10935 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10936 k + 1, n_div - (k+1)) < n_div)
10937 continue;
10938 l = find_modulo_constraint_pair(bmap, pos,
10939 bmap->eq[j][o_div + k]);
10940 if (l < 0)
10941 return -1;
10942 if (l >= bmap->n_ineq)
10943 continue;
10944 if (div)
10945 *div = k;
10946 if (ineq)
10947 *ineq = l;
10948 return j;
10951 return bmap->n_eq;
10954 /* Check if the given basic map is obviously single-valued.
10955 * In particular, for each output dimension, check that there is
10956 * an equality that defines the output dimension in terms of
10957 * earlier dimensions.
10959 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10961 int i;
10962 unsigned n_out;
10964 if (!bmap)
10965 return isl_bool_error;
10967 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10969 for (i = 0; i < n_out; ++i) {
10970 int eq;
10972 eq = isl_basic_map_output_defining_equality(bmap, i,
10973 NULL, NULL);
10974 if (eq < 0)
10975 return isl_bool_error;
10976 if (eq >= bmap->n_eq)
10977 return isl_bool_false;
10980 return isl_bool_true;
10983 /* Check if the given basic map is single-valued.
10984 * We simply compute
10986 * M \circ M^-1
10988 * and check if the result is a subset of the identity mapping.
10990 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10992 isl_space *space;
10993 isl_basic_map *test;
10994 isl_basic_map *id;
10995 isl_bool sv;
10997 sv = isl_basic_map_plain_is_single_valued(bmap);
10998 if (sv < 0 || sv)
10999 return sv;
11001 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11002 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11004 space = isl_basic_map_get_space(bmap);
11005 space = isl_space_map_from_set(isl_space_range(space));
11006 id = isl_basic_map_identity(space);
11008 sv = isl_basic_map_is_subset(test, id);
11010 isl_basic_map_free(test);
11011 isl_basic_map_free(id);
11013 return sv;
11016 /* Check if the given map is obviously single-valued.
11018 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11020 if (!map)
11021 return isl_bool_error;
11022 if (map->n == 0)
11023 return isl_bool_true;
11024 if (map->n >= 2)
11025 return isl_bool_false;
11027 return isl_basic_map_plain_is_single_valued(map->p[0]);
11030 /* Check if the given map is single-valued.
11031 * We simply compute
11033 * M \circ M^-1
11035 * and check if the result is a subset of the identity mapping.
11037 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11039 isl_space *dim;
11040 isl_map *test;
11041 isl_map *id;
11042 isl_bool sv;
11044 sv = isl_map_plain_is_single_valued(map);
11045 if (sv < 0 || sv)
11046 return sv;
11048 test = isl_map_reverse(isl_map_copy(map));
11049 test = isl_map_apply_range(test, isl_map_copy(map));
11051 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11052 id = isl_map_identity(dim);
11054 sv = isl_map_is_subset(test, id);
11056 isl_map_free(test);
11057 isl_map_free(id);
11059 return sv;
11062 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11064 isl_bool in;
11066 map = isl_map_copy(map);
11067 map = isl_map_reverse(map);
11068 in = isl_map_is_single_valued(map);
11069 isl_map_free(map);
11071 return in;
11074 /* Check if the given map is obviously injective.
11076 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11078 isl_bool in;
11080 map = isl_map_copy(map);
11081 map = isl_map_reverse(map);
11082 in = isl_map_plain_is_single_valued(map);
11083 isl_map_free(map);
11085 return in;
11088 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11090 isl_bool sv;
11092 sv = isl_map_is_single_valued(map);
11093 if (sv < 0 || !sv)
11094 return sv;
11096 return isl_map_is_injective(map);
11099 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11101 return isl_map_is_single_valued(set_to_map(set));
11104 /* Does "map" only map elements to themselves?
11106 * If the domain and range spaces are different, then "map"
11107 * is considered not to be an identity relation, even if it is empty.
11108 * Otherwise, construct the maximal identity relation and
11109 * check whether "map" is a subset of this relation.
11111 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11113 isl_space *space;
11114 isl_map *id;
11115 isl_bool equal, is_identity;
11117 space = isl_map_get_space(map);
11118 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11119 isl_space_free(space);
11120 if (equal < 0 || !equal)
11121 return equal;
11123 id = isl_map_identity(isl_map_get_space(map));
11124 is_identity = isl_map_is_subset(map, id);
11125 isl_map_free(id);
11127 return is_identity;
11130 int isl_map_is_translation(__isl_keep isl_map *map)
11132 int ok;
11133 isl_set *delta;
11135 delta = isl_map_deltas(isl_map_copy(map));
11136 ok = isl_set_is_singleton(delta);
11137 isl_set_free(delta);
11139 return ok;
11142 static int unique(isl_int *p, unsigned pos, unsigned len)
11144 if (isl_seq_first_non_zero(p, pos) != -1)
11145 return 0;
11146 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11147 return 0;
11148 return 1;
11151 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11153 int i, j;
11154 unsigned nvar;
11155 unsigned ovar;
11157 if (!bset)
11158 return isl_bool_error;
11160 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11161 return isl_bool_false;
11163 nvar = isl_basic_set_dim(bset, isl_dim_set);
11164 ovar = isl_space_offset(bset->dim, isl_dim_set);
11165 for (j = 0; j < nvar; ++j) {
11166 int lower = 0, upper = 0;
11167 for (i = 0; i < bset->n_eq; ++i) {
11168 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11169 continue;
11170 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11171 return isl_bool_false;
11172 break;
11174 if (i < bset->n_eq)
11175 continue;
11176 for (i = 0; i < bset->n_ineq; ++i) {
11177 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11178 continue;
11179 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11180 return isl_bool_false;
11181 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11182 lower = 1;
11183 else
11184 upper = 1;
11186 if (!lower || !upper)
11187 return isl_bool_false;
11190 return isl_bool_true;
11193 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11195 if (!set)
11196 return isl_bool_error;
11197 if (set->n != 1)
11198 return isl_bool_false;
11200 return isl_basic_set_is_box(set->p[0]);
11203 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11205 if (!bset)
11206 return isl_bool_error;
11208 return isl_space_is_wrapping(bset->dim);
11211 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11213 if (!set)
11214 return isl_bool_error;
11216 return isl_space_is_wrapping(set->dim);
11219 /* Modify the space of "map" through a call to "change".
11220 * If "can_change" is set (not NULL), then first call it to check
11221 * if the modification is allowed, printing the error message "cannot_change"
11222 * if it is not.
11224 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11225 isl_bool (*can_change)(__isl_keep isl_map *map),
11226 const char *cannot_change,
11227 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11229 isl_bool ok;
11230 isl_space *space;
11232 if (!map)
11233 return NULL;
11235 ok = can_change ? can_change(map) : isl_bool_true;
11236 if (ok < 0)
11237 return isl_map_free(map);
11238 if (!ok)
11239 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11240 return isl_map_free(map));
11242 space = change(isl_map_get_space(map));
11243 map = isl_map_reset_space(map, space);
11245 return map;
11248 /* Is the domain of "map" a wrapped relation?
11250 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11252 if (!map)
11253 return isl_bool_error;
11255 return isl_space_domain_is_wrapping(map->dim);
11258 /* Does "map" have a wrapped relation in both domain and range?
11260 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11262 return isl_space_is_product(isl_map_peek_space(map));
11265 /* Is the range of "map" a wrapped relation?
11267 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11269 if (!map)
11270 return isl_bool_error;
11272 return isl_space_range_is_wrapping(map->dim);
11275 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11277 bmap = isl_basic_map_cow(bmap);
11278 if (!bmap)
11279 return NULL;
11281 bmap->dim = isl_space_wrap(bmap->dim);
11282 if (!bmap->dim)
11283 goto error;
11285 bmap = isl_basic_map_finalize(bmap);
11287 return bset_from_bmap(bmap);
11288 error:
11289 isl_basic_map_free(bmap);
11290 return NULL;
11293 /* Given a map A -> B, return the set (A -> B).
11295 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11297 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11300 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11302 bset = isl_basic_set_cow(bset);
11303 if (!bset)
11304 return NULL;
11306 bset->dim = isl_space_unwrap(bset->dim);
11307 if (!bset->dim)
11308 goto error;
11310 bset = isl_basic_set_finalize(bset);
11312 return bset_to_bmap(bset);
11313 error:
11314 isl_basic_set_free(bset);
11315 return NULL;
11318 /* Given a set (A -> B), return the map A -> B.
11319 * Error out if "set" is not of the form (A -> B).
11321 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11323 return isl_map_change_space(set, &isl_set_is_wrapping,
11324 "not a wrapping set", &isl_space_unwrap);
11327 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11328 enum isl_dim_type type)
11330 if (!bmap)
11331 return NULL;
11333 if (!isl_space_is_named_or_nested(bmap->dim, type))
11334 return bmap;
11336 bmap = isl_basic_map_cow(bmap);
11337 if (!bmap)
11338 return NULL;
11340 bmap->dim = isl_space_reset(bmap->dim, type);
11341 if (!bmap->dim)
11342 goto error;
11344 bmap = isl_basic_map_finalize(bmap);
11346 return bmap;
11347 error:
11348 isl_basic_map_free(bmap);
11349 return NULL;
11352 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11353 enum isl_dim_type type)
11355 int i;
11357 if (!map)
11358 return NULL;
11360 if (!isl_space_is_named_or_nested(map->dim, type))
11361 return map;
11363 map = isl_map_cow(map);
11364 if (!map)
11365 return NULL;
11367 for (i = 0; i < map->n; ++i) {
11368 map->p[i] = isl_basic_map_reset(map->p[i], type);
11369 if (!map->p[i])
11370 goto error;
11372 map->dim = isl_space_reset(map->dim, type);
11373 if (!map->dim)
11374 goto error;
11376 return map;
11377 error:
11378 isl_map_free(map);
11379 return NULL;
11382 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11384 if (!bmap)
11385 return NULL;
11387 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11388 return bmap;
11390 bmap = isl_basic_map_cow(bmap);
11391 if (!bmap)
11392 return NULL;
11394 bmap->dim = isl_space_flatten(bmap->dim);
11395 if (!bmap->dim)
11396 goto error;
11398 bmap = isl_basic_map_finalize(bmap);
11400 return bmap;
11401 error:
11402 isl_basic_map_free(bmap);
11403 return NULL;
11406 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11408 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11411 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11412 __isl_take isl_basic_map *bmap)
11414 if (!bmap)
11415 return NULL;
11417 if (!bmap->dim->nested[0])
11418 return bmap;
11420 bmap = isl_basic_map_cow(bmap);
11421 if (!bmap)
11422 return NULL;
11424 bmap->dim = isl_space_flatten_domain(bmap->dim);
11425 if (!bmap->dim)
11426 goto error;
11428 bmap = isl_basic_map_finalize(bmap);
11430 return bmap;
11431 error:
11432 isl_basic_map_free(bmap);
11433 return NULL;
11436 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11437 __isl_take isl_basic_map *bmap)
11439 if (!bmap)
11440 return NULL;
11442 if (!bmap->dim->nested[1])
11443 return bmap;
11445 bmap = isl_basic_map_cow(bmap);
11446 if (!bmap)
11447 return NULL;
11449 bmap->dim = isl_space_flatten_range(bmap->dim);
11450 if (!bmap->dim)
11451 goto error;
11453 bmap = isl_basic_map_finalize(bmap);
11455 return bmap;
11456 error:
11457 isl_basic_map_free(bmap);
11458 return NULL;
11461 /* Remove any internal structure from the spaces of domain and range of "map".
11463 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11465 if (!map)
11466 return NULL;
11468 if (!map->dim->nested[0] && !map->dim->nested[1])
11469 return map;
11471 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11474 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11476 return set_from_map(isl_map_flatten(set_to_map(set)));
11479 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11481 isl_space *space, *flat_space;
11482 isl_map *map;
11484 space = isl_set_get_space(set);
11485 flat_space = isl_space_flatten(isl_space_copy(space));
11486 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11487 flat_space));
11488 map = isl_map_intersect_domain(map, set);
11490 return map;
11493 /* Remove any internal structure from the space of the domain of "map".
11495 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11497 if (!map)
11498 return NULL;
11500 if (!map->dim->nested[0])
11501 return map;
11503 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11506 /* Remove any internal structure from the space of the range of "map".
11508 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11510 if (!map)
11511 return NULL;
11513 if (!map->dim->nested[1])
11514 return map;
11516 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11519 /* Reorder the dimensions of "bmap" according to the given dim_map
11520 * and set the dimension specification to "space" and
11521 * perform Gaussian elimination on the result.
11523 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11524 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11526 isl_basic_map *res;
11527 unsigned flags;
11528 unsigned n_div;
11530 if (!bmap || !space || !dim_map)
11531 goto error;
11533 flags = bmap->flags;
11534 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11535 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11536 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11537 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11538 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11539 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11540 if (res)
11541 res->flags = flags;
11542 res = isl_basic_map_gauss(res, NULL);
11543 res = isl_basic_map_finalize(res);
11544 return res;
11545 error:
11546 isl_dim_map_free(dim_map);
11547 isl_basic_map_free(bmap);
11548 isl_space_free(space);
11549 return NULL;
11552 /* Reorder the dimensions of "map" according to given reordering.
11554 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11555 __isl_take isl_reordering *r)
11557 int i;
11558 struct isl_dim_map *dim_map;
11560 map = isl_map_cow(map);
11561 dim_map = isl_dim_map_from_reordering(r);
11562 if (!map || !r || !dim_map)
11563 goto error;
11565 for (i = 0; i < map->n; ++i) {
11566 struct isl_dim_map *dim_map_i;
11567 isl_space *space;
11569 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11571 space = isl_reordering_get_space(r);
11572 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11574 if (!map->p[i])
11575 goto error;
11578 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11579 map = isl_map_unmark_normalized(map);
11581 isl_reordering_free(r);
11582 isl_dim_map_free(dim_map);
11583 return map;
11584 error:
11585 isl_dim_map_free(dim_map);
11586 isl_map_free(map);
11587 isl_reordering_free(r);
11588 return NULL;
11591 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11592 __isl_take isl_reordering *r)
11594 return set_from_map(isl_map_realign(set_to_map(set), r));
11597 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11598 __isl_take isl_space *model)
11600 isl_ctx *ctx;
11601 isl_bool aligned;
11603 if (!map || !model)
11604 goto error;
11606 ctx = isl_space_get_ctx(model);
11607 if (!isl_space_has_named_params(model))
11608 isl_die(ctx, isl_error_invalid,
11609 "model has unnamed parameters", goto error);
11610 if (isl_map_check_named_params(map) < 0)
11611 goto error;
11612 aligned = isl_map_space_has_equal_params(map, model);
11613 if (aligned < 0)
11614 goto error;
11615 if (!aligned) {
11616 isl_reordering *exp;
11618 exp = isl_parameter_alignment_reordering(map->dim, model);
11619 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11620 map = isl_map_realign(map, exp);
11623 isl_space_free(model);
11624 return map;
11625 error:
11626 isl_space_free(model);
11627 isl_map_free(map);
11628 return NULL;
11631 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11632 __isl_take isl_space *model)
11634 return isl_map_align_params(set, model);
11637 /* Align the parameters of "bmap" to those of "model", introducing
11638 * additional parameters if needed.
11640 __isl_give isl_basic_map *isl_basic_map_align_params(
11641 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11643 isl_ctx *ctx;
11644 isl_bool equal_params;
11646 if (!bmap || !model)
11647 goto error;
11649 ctx = isl_space_get_ctx(model);
11650 if (!isl_space_has_named_params(model))
11651 isl_die(ctx, isl_error_invalid,
11652 "model has unnamed parameters", goto error);
11653 if (isl_basic_map_check_named_params(bmap) < 0)
11654 goto error;
11655 equal_params = isl_space_has_equal_params(bmap->dim, model);
11656 if (equal_params < 0)
11657 goto error;
11658 if (!equal_params) {
11659 isl_reordering *exp;
11660 struct isl_dim_map *dim_map;
11662 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11663 exp = isl_reordering_extend_space(exp,
11664 isl_basic_map_get_space(bmap));
11665 dim_map = isl_dim_map_from_reordering(exp);
11666 bmap = isl_basic_map_realign(bmap,
11667 isl_reordering_get_space(exp),
11668 isl_dim_map_extend(dim_map, bmap));
11669 isl_reordering_free(exp);
11670 isl_dim_map_free(dim_map);
11673 isl_space_free(model);
11674 return bmap;
11675 error:
11676 isl_space_free(model);
11677 isl_basic_map_free(bmap);
11678 return NULL;
11681 /* Do "bset" and "space" have the same parameters?
11683 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11684 __isl_keep isl_space *space)
11686 isl_space *bset_space;
11688 bset_space = isl_basic_set_peek_space(bset);
11689 return isl_space_has_equal_params(bset_space, space);
11692 /* Do "map" and "space" have the same parameters?
11694 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11695 __isl_keep isl_space *space)
11697 isl_space *map_space;
11699 map_space = isl_map_peek_space(map);
11700 return isl_space_has_equal_params(map_space, space);
11703 /* Do "set" and "space" have the same parameters?
11705 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11706 __isl_keep isl_space *space)
11708 return isl_map_space_has_equal_params(set_to_map(set), space);
11711 /* Align the parameters of "bset" to those of "model", introducing
11712 * additional parameters if needed.
11714 __isl_give isl_basic_set *isl_basic_set_align_params(
11715 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11717 return isl_basic_map_align_params(bset, model);
11720 /* Drop all parameters not referenced by "map".
11722 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11724 int i;
11726 if (isl_map_check_named_params(map) < 0)
11727 return isl_map_free(map);
11729 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11730 isl_bool involves;
11732 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11733 if (involves < 0)
11734 return isl_map_free(map);
11735 if (!involves)
11736 map = isl_map_project_out(map, isl_dim_param, i, 1);
11739 return map;
11742 /* Drop all parameters not referenced by "set".
11744 __isl_give isl_set *isl_set_drop_unused_params(
11745 __isl_take isl_set *set)
11747 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11750 /* Drop all parameters not referenced by "bmap".
11752 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11753 __isl_take isl_basic_map *bmap)
11755 int i;
11757 if (isl_basic_map_check_named_params(bmap) < 0)
11758 return isl_basic_map_free(bmap);
11760 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11761 isl_bool involves;
11763 involves = isl_basic_map_involves_dims(bmap,
11764 isl_dim_param, i, 1);
11765 if (involves < 0)
11766 return isl_basic_map_free(bmap);
11767 if (!involves)
11768 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11771 return bmap;
11774 /* Drop all parameters not referenced by "bset".
11776 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11777 __isl_take isl_basic_set *bset)
11779 return bset_from_bmap(isl_basic_map_drop_unused_params(
11780 bset_to_bmap(bset)));
11783 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11784 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11785 enum isl_dim_type c2, enum isl_dim_type c3,
11786 enum isl_dim_type c4, enum isl_dim_type c5)
11788 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11789 struct isl_mat *mat;
11790 int i, j, k;
11791 int pos;
11793 if (!bmap)
11794 return NULL;
11795 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11796 isl_basic_map_total_dim(bmap) + 1);
11797 if (!mat)
11798 return NULL;
11799 for (i = 0; i < bmap->n_eq; ++i)
11800 for (j = 0, pos = 0; j < 5; ++j) {
11801 int off = isl_basic_map_offset(bmap, c[j]);
11802 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11803 isl_int_set(mat->row[i][pos],
11804 bmap->eq[i][off + k]);
11805 ++pos;
11809 return mat;
11812 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11813 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11814 enum isl_dim_type c2, enum isl_dim_type c3,
11815 enum isl_dim_type c4, enum isl_dim_type c5)
11817 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11818 struct isl_mat *mat;
11819 int i, j, k;
11820 int pos;
11822 if (!bmap)
11823 return NULL;
11824 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11825 isl_basic_map_total_dim(bmap) + 1);
11826 if (!mat)
11827 return NULL;
11828 for (i = 0; i < bmap->n_ineq; ++i)
11829 for (j = 0, pos = 0; j < 5; ++j) {
11830 int off = isl_basic_map_offset(bmap, c[j]);
11831 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11832 isl_int_set(mat->row[i][pos],
11833 bmap->ineq[i][off + k]);
11834 ++pos;
11838 return mat;
11841 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11842 __isl_take isl_space *space,
11843 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11844 enum isl_dim_type c2, enum isl_dim_type c3,
11845 enum isl_dim_type c4, enum isl_dim_type c5)
11847 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11848 isl_basic_map *bmap = NULL;
11849 unsigned total;
11850 unsigned extra;
11851 int i, j, k, l;
11852 int pos;
11854 if (!space || !eq || !ineq)
11855 goto error;
11857 if (eq->n_col != ineq->n_col)
11858 isl_die(space->ctx, isl_error_invalid,
11859 "equalities and inequalities matrices should have "
11860 "same number of columns", goto error);
11862 total = 1 + isl_space_dim(space, isl_dim_all);
11864 if (eq->n_col < total)
11865 isl_die(space->ctx, isl_error_invalid,
11866 "number of columns too small", goto error);
11868 extra = eq->n_col - total;
11870 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
11871 eq->n_row, ineq->n_row);
11872 if (!bmap)
11873 goto error;
11874 for (i = 0; i < extra; ++i) {
11875 k = isl_basic_map_alloc_div(bmap);
11876 if (k < 0)
11877 goto error;
11878 isl_int_set_si(bmap->div[k][0], 0);
11880 for (i = 0; i < eq->n_row; ++i) {
11881 l = isl_basic_map_alloc_equality(bmap);
11882 if (l < 0)
11883 goto error;
11884 for (j = 0, pos = 0; j < 5; ++j) {
11885 int off = isl_basic_map_offset(bmap, c[j]);
11886 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11887 isl_int_set(bmap->eq[l][off + k],
11888 eq->row[i][pos]);
11889 ++pos;
11893 for (i = 0; i < ineq->n_row; ++i) {
11894 l = isl_basic_map_alloc_inequality(bmap);
11895 if (l < 0)
11896 goto error;
11897 for (j = 0, pos = 0; j < 5; ++j) {
11898 int off = isl_basic_map_offset(bmap, c[j]);
11899 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11900 isl_int_set(bmap->ineq[l][off + k],
11901 ineq->row[i][pos]);
11902 ++pos;
11907 isl_space_free(space);
11908 isl_mat_free(eq);
11909 isl_mat_free(ineq);
11911 bmap = isl_basic_map_simplify(bmap);
11912 return isl_basic_map_finalize(bmap);
11913 error:
11914 isl_space_free(space);
11915 isl_mat_free(eq);
11916 isl_mat_free(ineq);
11917 isl_basic_map_free(bmap);
11918 return NULL;
11921 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11922 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11923 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11925 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11926 c1, c2, c3, c4, isl_dim_in);
11929 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11930 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11931 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11933 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11934 c1, c2, c3, c4, isl_dim_in);
11937 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11938 __isl_take isl_space *dim,
11939 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11940 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11942 isl_basic_map *bmap;
11943 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11944 c1, c2, c3, c4, isl_dim_in);
11945 return bset_from_bmap(bmap);
11948 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11950 if (!bmap)
11951 return isl_bool_error;
11953 return isl_space_can_zip(bmap->dim);
11956 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11958 if (!map)
11959 return isl_bool_error;
11961 return isl_space_can_zip(map->dim);
11964 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11965 * (A -> C) -> (B -> D).
11967 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11969 unsigned pos;
11970 unsigned n1;
11971 unsigned n2;
11973 if (!bmap)
11974 return NULL;
11976 if (!isl_basic_map_can_zip(bmap))
11977 isl_die(bmap->ctx, isl_error_invalid,
11978 "basic map cannot be zipped", goto error);
11979 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11980 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11981 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11982 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11983 bmap = isl_basic_map_cow(bmap);
11984 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11985 if (!bmap)
11986 return NULL;
11987 bmap->dim = isl_space_zip(bmap->dim);
11988 if (!bmap->dim)
11989 goto error;
11990 bmap = isl_basic_map_mark_final(bmap);
11991 return bmap;
11992 error:
11993 isl_basic_map_free(bmap);
11994 return NULL;
11997 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11998 * (A -> C) -> (B -> D).
12000 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12002 int i;
12004 if (!map)
12005 return NULL;
12007 if (!isl_map_can_zip(map))
12008 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12009 goto error);
12011 map = isl_map_cow(map);
12012 if (!map)
12013 return NULL;
12015 for (i = 0; i < map->n; ++i) {
12016 map->p[i] = isl_basic_map_zip(map->p[i]);
12017 if (!map->p[i])
12018 goto error;
12021 map->dim = isl_space_zip(map->dim);
12022 if (!map->dim)
12023 goto error;
12025 return map;
12026 error:
12027 isl_map_free(map);
12028 return NULL;
12031 /* Can we apply isl_basic_map_curry to "bmap"?
12032 * That is, does it have a nested relation in its domain?
12034 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12036 if (!bmap)
12037 return isl_bool_error;
12039 return isl_space_can_curry(bmap->dim);
12042 /* Can we apply isl_map_curry to "map"?
12043 * That is, does it have a nested relation in its domain?
12045 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12047 if (!map)
12048 return isl_bool_error;
12050 return isl_space_can_curry(map->dim);
12053 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12054 * A -> (B -> C).
12056 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12059 if (!bmap)
12060 return NULL;
12062 if (!isl_basic_map_can_curry(bmap))
12063 isl_die(bmap->ctx, isl_error_invalid,
12064 "basic map cannot be curried", goto error);
12065 bmap = isl_basic_map_cow(bmap);
12066 if (!bmap)
12067 return NULL;
12068 bmap->dim = isl_space_curry(bmap->dim);
12069 if (!bmap->dim)
12070 goto error;
12071 bmap = isl_basic_map_mark_final(bmap);
12072 return bmap;
12073 error:
12074 isl_basic_map_free(bmap);
12075 return NULL;
12078 /* Given a map (A -> B) -> C, return the corresponding map
12079 * A -> (B -> C).
12081 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12083 return isl_map_change_space(map, &isl_map_can_curry,
12084 "map cannot be curried", &isl_space_curry);
12087 /* Can isl_map_range_curry be applied to "map"?
12088 * That is, does it have a nested relation in its range,
12089 * the domain of which is itself a nested relation?
12091 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12093 if (!map)
12094 return isl_bool_error;
12096 return isl_space_can_range_curry(map->dim);
12099 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12100 * A -> (B -> (C -> D)).
12102 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12104 return isl_map_change_space(map, &isl_map_can_range_curry,
12105 "map range cannot be curried",
12106 &isl_space_range_curry);
12109 /* Can we apply isl_basic_map_uncurry to "bmap"?
12110 * That is, does it have a nested relation in its domain?
12112 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12114 if (!bmap)
12115 return isl_bool_error;
12117 return isl_space_can_uncurry(bmap->dim);
12120 /* Can we apply isl_map_uncurry to "map"?
12121 * That is, does it have a nested relation in its domain?
12123 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12125 if (!map)
12126 return isl_bool_error;
12128 return isl_space_can_uncurry(map->dim);
12131 /* Given a basic map A -> (B -> C), return the corresponding basic map
12132 * (A -> B) -> C.
12134 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12137 if (!bmap)
12138 return NULL;
12140 if (!isl_basic_map_can_uncurry(bmap))
12141 isl_die(bmap->ctx, isl_error_invalid,
12142 "basic map cannot be uncurried",
12143 return isl_basic_map_free(bmap));
12144 bmap = isl_basic_map_cow(bmap);
12145 if (!bmap)
12146 return NULL;
12147 bmap->dim = isl_space_uncurry(bmap->dim);
12148 if (!bmap->dim)
12149 return isl_basic_map_free(bmap);
12150 bmap = isl_basic_map_mark_final(bmap);
12151 return bmap;
12154 /* Given a map A -> (B -> C), return the corresponding map
12155 * (A -> B) -> C.
12157 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12159 return isl_map_change_space(map, &isl_map_can_uncurry,
12160 "map cannot be uncurried", &isl_space_uncurry);
12163 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12164 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12166 return isl_map_equate(set, type1, pos1, type2, pos2);
12169 /* Construct a basic map where the given dimensions are equal to each other.
12171 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12172 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12174 isl_basic_map *bmap = NULL;
12175 int i;
12177 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12178 isl_space_check_range(space, type2, pos2, 1) < 0)
12179 goto error;
12181 if (type1 == type2 && pos1 == pos2)
12182 return isl_basic_map_universe(space);
12184 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12185 i = isl_basic_map_alloc_equality(bmap);
12186 if (i < 0)
12187 goto error;
12188 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12189 pos1 += isl_basic_map_offset(bmap, type1);
12190 pos2 += isl_basic_map_offset(bmap, type2);
12191 isl_int_set_si(bmap->eq[i][pos1], -1);
12192 isl_int_set_si(bmap->eq[i][pos2], 1);
12193 bmap = isl_basic_map_finalize(bmap);
12194 isl_space_free(space);
12195 return bmap;
12196 error:
12197 isl_space_free(space);
12198 isl_basic_map_free(bmap);
12199 return NULL;
12202 /* Add a constraint imposing that the given two dimensions are equal.
12204 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12205 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12207 isl_basic_map *eq;
12209 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12211 bmap = isl_basic_map_intersect(bmap, eq);
12213 return bmap;
12216 /* Add a constraint imposing that the given two dimensions are equal.
12218 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12219 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12221 isl_basic_map *bmap;
12223 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12225 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12227 return map;
12230 /* Add a constraint imposing that the given two dimensions have opposite values.
12232 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12233 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12235 isl_basic_map *bmap = NULL;
12236 int i;
12238 if (isl_map_check_range(map, type1, pos1, 1) < 0)
12239 return isl_map_free(map);
12240 if (isl_map_check_range(map, type2, pos2, 1) < 0)
12241 return isl_map_free(map);
12243 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12244 i = isl_basic_map_alloc_equality(bmap);
12245 if (i < 0)
12246 goto error;
12247 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12248 pos1 += isl_basic_map_offset(bmap, type1);
12249 pos2 += isl_basic_map_offset(bmap, type2);
12250 isl_int_set_si(bmap->eq[i][pos1], 1);
12251 isl_int_set_si(bmap->eq[i][pos2], 1);
12252 bmap = isl_basic_map_finalize(bmap);
12254 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12256 return map;
12257 error:
12258 isl_basic_map_free(bmap);
12259 isl_map_free(map);
12260 return NULL;
12263 /* Construct a constraint imposing that the value of the first dimension is
12264 * greater than or equal to that of the second.
12266 static __isl_give isl_constraint *constraint_order_ge(
12267 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12268 enum isl_dim_type type2, int pos2)
12270 isl_constraint *c;
12272 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12273 isl_space_check_range(space, type2, pos2, 1) < 0)
12274 space = isl_space_free(space);
12275 if (!space)
12276 return NULL;
12278 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12280 if (type1 == type2 && pos1 == pos2)
12281 return c;
12283 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12284 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12286 return c;
12289 /* Add a constraint imposing that the value of the first dimension is
12290 * greater than or equal to that of the second.
12292 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12293 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12295 isl_constraint *c;
12296 isl_space *space;
12298 if (type1 == type2 && pos1 == pos2)
12299 return bmap;
12300 space = isl_basic_map_get_space(bmap);
12301 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12302 bmap = isl_basic_map_add_constraint(bmap, c);
12304 return bmap;
12307 /* Add a constraint imposing that the value of the first dimension is
12308 * greater than or equal to that of the second.
12310 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12311 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12313 isl_constraint *c;
12314 isl_space *space;
12316 if (type1 == type2 && pos1 == pos2)
12317 return map;
12318 space = isl_map_get_space(map);
12319 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12320 map = isl_map_add_constraint(map, c);
12322 return map;
12325 /* Add a constraint imposing that the value of the first dimension is
12326 * less than or equal to that of the second.
12328 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12329 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12331 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12334 /* Construct a basic map where the value of the first dimension is
12335 * greater than that of the second.
12337 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12338 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12340 isl_basic_map *bmap = NULL;
12341 int i;
12343 if (isl_space_check_range(space, type1, pos1, 1) < 0 ||
12344 isl_space_check_range(space, type2, pos2, 1) < 0)
12345 goto error;
12347 if (type1 == type2 && pos1 == pos2)
12348 return isl_basic_map_empty(space);
12350 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12351 i = isl_basic_map_alloc_inequality(bmap);
12352 if (i < 0)
12353 return isl_basic_map_free(bmap);
12354 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12355 pos1 += isl_basic_map_offset(bmap, type1);
12356 pos2 += isl_basic_map_offset(bmap, type2);
12357 isl_int_set_si(bmap->ineq[i][pos1], 1);
12358 isl_int_set_si(bmap->ineq[i][pos2], -1);
12359 isl_int_set_si(bmap->ineq[i][0], -1);
12360 bmap = isl_basic_map_finalize(bmap);
12362 return bmap;
12363 error:
12364 isl_space_free(space);
12365 isl_basic_map_free(bmap);
12366 return NULL;
12369 /* Add a constraint imposing that the value of the first dimension is
12370 * greater than that of the second.
12372 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12373 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12375 isl_basic_map *gt;
12377 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12379 bmap = isl_basic_map_intersect(bmap, gt);
12381 return bmap;
12384 /* Add a constraint imposing that the value of the first dimension is
12385 * greater than that of the second.
12387 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12388 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12390 isl_basic_map *bmap;
12392 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12394 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12396 return map;
12399 /* Add a constraint imposing that the value of the first dimension is
12400 * smaller than that of the second.
12402 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12403 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12405 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12408 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12409 int pos)
12411 isl_aff *div;
12412 isl_local_space *ls;
12414 if (!bmap)
12415 return NULL;
12417 if (!isl_basic_map_divs_known(bmap))
12418 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12419 "some divs are unknown", return NULL);
12421 ls = isl_basic_map_get_local_space(bmap);
12422 div = isl_local_space_get_div(ls, pos);
12423 isl_local_space_free(ls);
12425 return div;
12428 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12429 int pos)
12431 return isl_basic_map_get_div(bset, pos);
12434 /* Plug in "subs" for dimension "type", "pos" of "bset".
12436 * Let i be the dimension to replace and let "subs" be of the form
12438 * f/d
12440 * Any integer division with a non-zero coefficient for i,
12442 * floor((a i + g)/m)
12444 * is replaced by
12446 * floor((a f + d g)/(m d))
12448 * Constraints of the form
12450 * a i + g
12452 * are replaced by
12454 * a f + d g
12456 * We currently require that "subs" is an integral expression.
12457 * Handling rational expressions may require us to add stride constraints
12458 * as we do in isl_basic_set_preimage_multi_aff.
12460 __isl_give isl_basic_set *isl_basic_set_substitute(
12461 __isl_take isl_basic_set *bset,
12462 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12464 int i;
12465 isl_int v;
12466 isl_ctx *ctx;
12468 if (bset && isl_basic_set_plain_is_empty(bset))
12469 return bset;
12471 bset = isl_basic_set_cow(bset);
12472 if (!bset || !subs)
12473 goto error;
12475 ctx = isl_basic_set_get_ctx(bset);
12476 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12477 isl_die(ctx, isl_error_invalid,
12478 "spaces don't match", goto error);
12479 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12480 isl_die(ctx, isl_error_unsupported,
12481 "cannot handle divs yet", goto error);
12482 if (!isl_int_is_one(subs->v->el[0]))
12483 isl_die(ctx, isl_error_invalid,
12484 "can only substitute integer expressions", goto error);
12486 pos += isl_basic_set_offset(bset, type);
12488 isl_int_init(v);
12490 for (i = 0; i < bset->n_eq; ++i) {
12491 if (isl_int_is_zero(bset->eq[i][pos]))
12492 continue;
12493 isl_int_set(v, bset->eq[i][pos]);
12494 isl_int_set_si(bset->eq[i][pos], 0);
12495 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12496 v, subs->v->el + 1, subs->v->size - 1);
12499 for (i = 0; i < bset->n_ineq; ++i) {
12500 if (isl_int_is_zero(bset->ineq[i][pos]))
12501 continue;
12502 isl_int_set(v, bset->ineq[i][pos]);
12503 isl_int_set_si(bset->ineq[i][pos], 0);
12504 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12505 v, subs->v->el + 1, subs->v->size - 1);
12508 for (i = 0; i < bset->n_div; ++i) {
12509 if (isl_int_is_zero(bset->div[i][1 + pos]))
12510 continue;
12511 isl_int_set(v, bset->div[i][1 + pos]);
12512 isl_int_set_si(bset->div[i][1 + pos], 0);
12513 isl_seq_combine(bset->div[i] + 1,
12514 subs->v->el[0], bset->div[i] + 1,
12515 v, subs->v->el + 1, subs->v->size - 1);
12516 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12519 isl_int_clear(v);
12521 bset = isl_basic_set_simplify(bset);
12522 return isl_basic_set_finalize(bset);
12523 error:
12524 isl_basic_set_free(bset);
12525 return NULL;
12528 /* Plug in "subs" for dimension "type", "pos" of "set".
12530 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12531 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12533 int i;
12535 if (set && isl_set_plain_is_empty(set))
12536 return set;
12538 set = isl_set_cow(set);
12539 if (!set || !subs)
12540 goto error;
12542 for (i = set->n - 1; i >= 0; --i) {
12543 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12544 set = set_from_map(remove_if_empty(set_to_map(set), i));
12545 if (!set)
12546 return NULL;
12549 return set;
12550 error:
12551 isl_set_free(set);
12552 return NULL;
12555 /* Check if the range of "ma" is compatible with the domain or range
12556 * (depending on "type") of "bmap".
12558 static isl_stat check_basic_map_compatible_range_multi_aff(
12559 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12560 __isl_keep isl_multi_aff *ma)
12562 isl_bool m;
12563 isl_space *ma_space;
12565 ma_space = isl_multi_aff_get_space(ma);
12567 m = isl_space_has_equal_params(bmap->dim, ma_space);
12568 if (m < 0)
12569 goto error;
12570 if (!m)
12571 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12572 "parameters don't match", goto error);
12573 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12574 if (m < 0)
12575 goto error;
12576 if (!m)
12577 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12578 "spaces don't match", goto error);
12580 isl_space_free(ma_space);
12581 return isl_stat_ok;
12582 error:
12583 isl_space_free(ma_space);
12584 return isl_stat_error;
12587 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12588 * coefficients before the transformed range of dimensions,
12589 * the "n_after" coefficients after the transformed range of dimensions
12590 * and the coefficients of the other divs in "bmap".
12592 static __isl_give isl_basic_map *set_ma_divs(__isl_take isl_basic_map *bmap,
12593 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12595 int i;
12596 int n_param;
12597 int n_set;
12598 isl_local_space *ls;
12600 if (n_div == 0)
12601 return bmap;
12603 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12604 if (!ls)
12605 return isl_basic_map_free(bmap);
12607 n_param = isl_local_space_dim(ls, isl_dim_param);
12608 n_set = isl_local_space_dim(ls, isl_dim_set);
12609 for (i = 0; i < n_div; ++i) {
12610 int o_bmap = 0, o_ls = 0;
12612 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12613 o_bmap += 1 + 1 + n_param;
12614 o_ls += 1 + 1 + n_param;
12615 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12616 o_bmap += n_before;
12617 isl_seq_cpy(bmap->div[i] + o_bmap,
12618 ls->div->row[i] + o_ls, n_set);
12619 o_bmap += n_set;
12620 o_ls += n_set;
12621 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12622 o_bmap += n_after;
12623 isl_seq_cpy(bmap->div[i] + o_bmap,
12624 ls->div->row[i] + o_ls, n_div);
12625 o_bmap += n_div;
12626 o_ls += n_div;
12627 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12628 bmap = isl_basic_map_add_div_constraints(bmap, i);
12629 if (!bmap)
12630 goto error;
12633 isl_local_space_free(ls);
12634 return bmap;
12635 error:
12636 isl_local_space_free(ls);
12637 return isl_basic_map_free(bmap);
12640 /* How many stride constraints does "ma" enforce?
12641 * That is, how many of the affine expressions have a denominator
12642 * different from one?
12644 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12646 int i;
12647 int strides = 0;
12649 for (i = 0; i < ma->n; ++i)
12650 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12651 strides++;
12653 return strides;
12656 /* For each affine expression in ma of the form
12658 * x_i = (f_i y + h_i)/m_i
12660 * with m_i different from one, add a constraint to "bmap"
12661 * of the form
12663 * f_i y + h_i = m_i alpha_i
12665 * with alpha_i an additional existentially quantified variable.
12667 * The input variables of "ma" correspond to a subset of the variables
12668 * of "bmap". There are "n_before" variables in "bmap" before this
12669 * subset and "n_after" variables after this subset.
12670 * The integer divisions of the affine expressions in "ma" are assumed
12671 * to have been aligned. There are "n_div_ma" of them and
12672 * they appear first in "bmap", straight after the "n_after" variables.
12674 static __isl_give isl_basic_map *add_ma_strides(
12675 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12676 int n_before, int n_after, int n_div_ma)
12678 int i, k;
12679 int div;
12680 int total;
12681 int n_param;
12682 int n_in;
12684 total = isl_basic_map_total_dim(bmap);
12685 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12686 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12687 for (i = 0; i < ma->n; ++i) {
12688 int o_bmap = 0, o_ma = 1;
12690 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12691 continue;
12692 div = isl_basic_map_alloc_div(bmap);
12693 k = isl_basic_map_alloc_equality(bmap);
12694 if (div < 0 || k < 0)
12695 goto error;
12696 isl_int_set_si(bmap->div[div][0], 0);
12697 isl_seq_cpy(bmap->eq[k] + o_bmap,
12698 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12699 o_bmap += 1 + n_param;
12700 o_ma += 1 + n_param;
12701 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12702 o_bmap += n_before;
12703 isl_seq_cpy(bmap->eq[k] + o_bmap,
12704 ma->u.p[i]->v->el + o_ma, n_in);
12705 o_bmap += n_in;
12706 o_ma += n_in;
12707 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12708 o_bmap += n_after;
12709 isl_seq_cpy(bmap->eq[k] + o_bmap,
12710 ma->u.p[i]->v->el + o_ma, n_div_ma);
12711 o_bmap += n_div_ma;
12712 o_ma += n_div_ma;
12713 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12714 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12715 total++;
12718 return bmap;
12719 error:
12720 isl_basic_map_free(bmap);
12721 return NULL;
12724 /* Replace the domain or range space (depending on "type) of "space" by "set".
12726 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12727 enum isl_dim_type type, __isl_take isl_space *set)
12729 if (type == isl_dim_in) {
12730 space = isl_space_range(space);
12731 space = isl_space_map_from_domain_and_range(set, space);
12732 } else {
12733 space = isl_space_domain(space);
12734 space = isl_space_map_from_domain_and_range(space, set);
12737 return space;
12740 /* Compute the preimage of the domain or range (depending on "type")
12741 * of "bmap" under the function represented by "ma".
12742 * In other words, plug in "ma" in the domain or range of "bmap".
12743 * The result is a basic map that lives in the same space as "bmap"
12744 * except that the domain or range has been replaced by
12745 * the domain space of "ma".
12747 * If bmap is represented by
12749 * A(p) + S u + B x + T v + C(divs) >= 0,
12751 * where u and x are input and output dimensions if type == isl_dim_out
12752 * while x and v are input and output dimensions if type == isl_dim_in,
12753 * and ma is represented by
12755 * x = D(p) + F(y) + G(divs')
12757 * then the result is
12759 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12761 * The divs in the input set are similarly adjusted.
12762 * In particular
12764 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12766 * becomes
12768 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12769 * B_i G(divs') + c_i(divs))/n_i)
12771 * If bmap is not a rational map and if F(y) involves any denominators
12773 * x_i = (f_i y + h_i)/m_i
12775 * then additional constraints are added to ensure that we only
12776 * map back integer points. That is we enforce
12778 * f_i y + h_i = m_i alpha_i
12780 * with alpha_i an additional existentially quantified variable.
12782 * We first copy over the divs from "ma".
12783 * Then we add the modified constraints and divs from "bmap".
12784 * Finally, we add the stride constraints, if needed.
12786 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12787 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12788 __isl_take isl_multi_aff *ma)
12790 int i, k;
12791 isl_space *space;
12792 isl_basic_map *res = NULL;
12793 int n_before, n_after, n_div_bmap, n_div_ma;
12794 isl_int f, c1, c2, g;
12795 isl_bool rational;
12796 int strides;
12798 isl_int_init(f);
12799 isl_int_init(c1);
12800 isl_int_init(c2);
12801 isl_int_init(g);
12803 ma = isl_multi_aff_align_divs(ma);
12804 if (!bmap || !ma)
12805 goto error;
12806 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12807 goto error;
12809 if (type == isl_dim_in) {
12810 n_before = 0;
12811 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12812 } else {
12813 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12814 n_after = 0;
12816 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12817 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12819 space = isl_multi_aff_get_domain_space(ma);
12820 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12821 rational = isl_basic_map_is_rational(bmap);
12822 strides = rational ? 0 : multi_aff_strides(ma);
12823 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12824 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12825 if (rational)
12826 res = isl_basic_map_set_rational(res);
12828 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12829 if (isl_basic_map_alloc_div(res) < 0)
12830 goto error;
12832 res = set_ma_divs(res, ma, n_before, n_after, n_div_ma);
12833 if (!res)
12834 goto error;
12836 for (i = 0; i < bmap->n_eq; ++i) {
12837 k = isl_basic_map_alloc_equality(res);
12838 if (k < 0)
12839 goto error;
12840 if (isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12841 n_after, n_div_ma, n_div_bmap,
12842 f, c1, c2, g, 0) < 0)
12843 goto error;
12846 for (i = 0; i < bmap->n_ineq; ++i) {
12847 k = isl_basic_map_alloc_inequality(res);
12848 if (k < 0)
12849 goto error;
12850 if (isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12851 n_after, n_div_ma, n_div_bmap,
12852 f, c1, c2, g, 0) < 0)
12853 goto error;
12856 for (i = 0; i < bmap->n_div; ++i) {
12857 if (isl_int_is_zero(bmap->div[i][0])) {
12858 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12859 continue;
12861 if (isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12862 n_before, n_after, n_div_ma, n_div_bmap,
12863 f, c1, c2, g, 1) < 0)
12864 goto error;
12867 if (strides)
12868 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12870 isl_int_clear(f);
12871 isl_int_clear(c1);
12872 isl_int_clear(c2);
12873 isl_int_clear(g);
12874 isl_basic_map_free(bmap);
12875 isl_multi_aff_free(ma);
12876 res = isl_basic_map_simplify(res);
12877 return isl_basic_map_finalize(res);
12878 error:
12879 isl_int_clear(f);
12880 isl_int_clear(c1);
12881 isl_int_clear(c2);
12882 isl_int_clear(g);
12883 isl_basic_map_free(bmap);
12884 isl_multi_aff_free(ma);
12885 isl_basic_map_free(res);
12886 return NULL;
12889 /* Compute the preimage of "bset" under the function represented by "ma".
12890 * In other words, plug in "ma" in "bset". The result is a basic set
12891 * that lives in the domain space of "ma".
12893 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12894 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12896 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12899 /* Compute the preimage of the domain of "bmap" under the function
12900 * represented by "ma".
12901 * In other words, plug in "ma" in the domain of "bmap".
12902 * The result is a basic map that lives in the same space as "bmap"
12903 * except that the domain has been replaced by the domain space of "ma".
12905 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12906 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12908 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12911 /* Compute the preimage of the range of "bmap" under the function
12912 * represented by "ma".
12913 * In other words, plug in "ma" in the range of "bmap".
12914 * The result is a basic map that lives in the same space as "bmap"
12915 * except that the range has been replaced by the domain space of "ma".
12917 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12918 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12920 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12923 /* Check if the range of "ma" is compatible with the domain or range
12924 * (depending on "type") of "map".
12925 * Return isl_stat_error if anything is wrong.
12927 static isl_stat check_map_compatible_range_multi_aff(
12928 __isl_keep isl_map *map, enum isl_dim_type type,
12929 __isl_keep isl_multi_aff *ma)
12931 isl_bool m;
12932 isl_space *ma_space;
12934 ma_space = isl_multi_aff_get_space(ma);
12935 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12936 isl_space_free(ma_space);
12937 if (m < 0)
12938 return isl_stat_error;
12939 if (!m)
12940 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12941 "spaces don't match", return isl_stat_error);
12942 return isl_stat_ok;
12945 /* Compute the preimage of the domain or range (depending on "type")
12946 * of "map" under the function represented by "ma".
12947 * In other words, plug in "ma" in the domain or range of "map".
12948 * The result is a map that lives in the same space as "map"
12949 * except that the domain or range has been replaced by
12950 * the domain space of "ma".
12952 * The parameters are assumed to have been aligned.
12954 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12955 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12957 int i;
12958 isl_space *space;
12960 map = isl_map_cow(map);
12961 ma = isl_multi_aff_align_divs(ma);
12962 if (!map || !ma)
12963 goto error;
12964 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12965 goto error;
12967 for (i = 0; i < map->n; ++i) {
12968 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12969 isl_multi_aff_copy(ma));
12970 if (!map->p[i])
12971 goto error;
12974 space = isl_multi_aff_get_domain_space(ma);
12975 space = isl_space_set(isl_map_get_space(map), type, space);
12977 isl_space_free(map->dim);
12978 map->dim = space;
12979 if (!map->dim)
12980 goto error;
12982 isl_multi_aff_free(ma);
12983 if (map->n > 1)
12984 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12985 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12986 return map;
12987 error:
12988 isl_multi_aff_free(ma);
12989 isl_map_free(map);
12990 return NULL;
12993 /* Compute the preimage of the domain or range (depending on "type")
12994 * of "map" under the function represented by "ma".
12995 * In other words, plug in "ma" in the domain or range of "map".
12996 * The result is a map that lives in the same space as "map"
12997 * except that the domain or range has been replaced by
12998 * the domain space of "ma".
13000 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13001 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13003 isl_bool aligned;
13005 if (!map || !ma)
13006 goto error;
13008 aligned = isl_map_space_has_equal_params(map, ma->space);
13009 if (aligned < 0)
13010 goto error;
13011 if (aligned)
13012 return map_preimage_multi_aff(map, type, ma);
13014 if (isl_map_check_named_params(map) < 0)
13015 goto error;
13016 if (!isl_space_has_named_params(ma->space))
13017 isl_die(map->ctx, isl_error_invalid,
13018 "unaligned unnamed parameters", goto error);
13019 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13020 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13022 return map_preimage_multi_aff(map, type, ma);
13023 error:
13024 isl_multi_aff_free(ma);
13025 return isl_map_free(map);
13028 /* Compute the preimage of "set" under the function represented by "ma".
13029 * In other words, plug in "ma" in "set". The result is a set
13030 * that lives in the domain space of "ma".
13032 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13033 __isl_take isl_multi_aff *ma)
13035 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13038 /* Compute the preimage of the domain of "map" under the function
13039 * represented by "ma".
13040 * In other words, plug in "ma" in the domain of "map".
13041 * The result is a map that lives in the same space as "map"
13042 * except that the domain has been replaced by the domain space of "ma".
13044 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13045 __isl_take isl_multi_aff *ma)
13047 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13050 /* Compute the preimage of the range of "map" under the function
13051 * represented by "ma".
13052 * In other words, plug in "ma" in the range of "map".
13053 * The result is a map that lives in the same space as "map"
13054 * except that the range has been replaced by the domain space of "ma".
13056 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13057 __isl_take isl_multi_aff *ma)
13059 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13062 /* Compute the preimage of "map" under the function represented by "pma".
13063 * In other words, plug in "pma" in the domain or range of "map".
13064 * The result is a map that lives in the same space as "map",
13065 * except that the space of type "type" has been replaced by
13066 * the domain space of "pma".
13068 * The parameters of "map" and "pma" are assumed to have been aligned.
13070 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13071 __isl_take isl_map *map, enum isl_dim_type type,
13072 __isl_take isl_pw_multi_aff *pma)
13074 int i;
13075 isl_map *res;
13077 if (!pma)
13078 goto error;
13080 if (pma->n == 0) {
13081 isl_pw_multi_aff_free(pma);
13082 res = isl_map_empty(isl_map_get_space(map));
13083 isl_map_free(map);
13084 return res;
13087 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13088 isl_multi_aff_copy(pma->p[0].maff));
13089 if (type == isl_dim_in)
13090 res = isl_map_intersect_domain(res,
13091 isl_map_copy(pma->p[0].set));
13092 else
13093 res = isl_map_intersect_range(res,
13094 isl_map_copy(pma->p[0].set));
13096 for (i = 1; i < pma->n; ++i) {
13097 isl_map *res_i;
13099 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13100 isl_multi_aff_copy(pma->p[i].maff));
13101 if (type == isl_dim_in)
13102 res_i = isl_map_intersect_domain(res_i,
13103 isl_map_copy(pma->p[i].set));
13104 else
13105 res_i = isl_map_intersect_range(res_i,
13106 isl_map_copy(pma->p[i].set));
13107 res = isl_map_union(res, res_i);
13110 isl_pw_multi_aff_free(pma);
13111 isl_map_free(map);
13112 return res;
13113 error:
13114 isl_pw_multi_aff_free(pma);
13115 isl_map_free(map);
13116 return NULL;
13119 /* Compute the preimage of "map" under the function represented by "pma".
13120 * In other words, plug in "pma" in the domain or range of "map".
13121 * The result is a map that lives in the same space as "map",
13122 * except that the space of type "type" has been replaced by
13123 * the domain space of "pma".
13125 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13126 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13128 isl_bool aligned;
13130 if (!map || !pma)
13131 goto error;
13133 aligned = isl_map_space_has_equal_params(map, pma->dim);
13134 if (aligned < 0)
13135 goto error;
13136 if (aligned)
13137 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13139 if (isl_map_check_named_params(map) < 0)
13140 goto error;
13141 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13142 goto error;
13143 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13144 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13146 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13147 error:
13148 isl_pw_multi_aff_free(pma);
13149 return isl_map_free(map);
13152 /* Compute the preimage of "set" under the function represented by "pma".
13153 * In other words, plug in "pma" in "set". The result is a set
13154 * that lives in the domain space of "pma".
13156 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13157 __isl_take isl_pw_multi_aff *pma)
13159 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13162 /* Compute the preimage of the domain of "map" under the function
13163 * represented by "pma".
13164 * In other words, plug in "pma" in the domain of "map".
13165 * The result is a map that lives in the same space as "map",
13166 * except that domain space has been replaced by the domain space of "pma".
13168 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13169 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13171 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13174 /* Compute the preimage of the range of "map" under the function
13175 * represented by "pma".
13176 * In other words, plug in "pma" in the range of "map".
13177 * The result is a map that lives in the same space as "map",
13178 * except that range space has been replaced by the domain space of "pma".
13180 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13181 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13183 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13186 /* Compute the preimage of "map" under the function represented by "mpa".
13187 * In other words, plug in "mpa" in the domain or range of "map".
13188 * The result is a map that lives in the same space as "map",
13189 * except that the space of type "type" has been replaced by
13190 * the domain space of "mpa".
13192 * If the map does not involve any constraints that refer to the
13193 * dimensions of the substituted space, then the only possible
13194 * effect of "mpa" on the map is to map the space to a different space.
13195 * We create a separate isl_multi_aff to effectuate this change
13196 * in order to avoid spurious splitting of the map along the pieces
13197 * of "mpa".
13198 * If "mpa" has a non-trivial explicit domain, however,
13199 * then the full substitution should be performed.
13201 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13202 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13204 int n;
13205 isl_bool full;
13206 isl_pw_multi_aff *pma;
13208 if (!map || !mpa)
13209 goto error;
13211 n = isl_map_dim(map, type);
13212 full = isl_map_involves_dims(map, type, 0, n);
13213 if (full >= 0 && !full)
13214 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13215 if (full < 0)
13216 goto error;
13217 if (!full) {
13218 isl_space *space;
13219 isl_multi_aff *ma;
13221 space = isl_multi_pw_aff_get_space(mpa);
13222 isl_multi_pw_aff_free(mpa);
13223 ma = isl_multi_aff_zero(space);
13224 return isl_map_preimage_multi_aff(map, type, ma);
13227 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13228 return isl_map_preimage_pw_multi_aff(map, type, pma);
13229 error:
13230 isl_map_free(map);
13231 isl_multi_pw_aff_free(mpa);
13232 return NULL;
13235 /* Compute the preimage of "map" under the function represented by "mpa".
13236 * In other words, plug in "mpa" in the domain "map".
13237 * The result is a map that lives in the same space as "map",
13238 * except that domain space has been replaced by the domain space of "mpa".
13240 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13241 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13243 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13246 /* Compute the preimage of "set" by the function represented by "mpa".
13247 * In other words, plug in "mpa" in "set".
13249 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13250 __isl_take isl_multi_pw_aff *mpa)
13252 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13255 /* Return a copy of the equality constraints of "bset" as a matrix.
13257 __isl_give isl_mat *isl_basic_set_extract_equalities(
13258 __isl_keep isl_basic_set *bset)
13260 isl_ctx *ctx;
13261 unsigned total;
13263 if (!bset)
13264 return NULL;
13266 ctx = isl_basic_set_get_ctx(bset);
13267 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13268 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13271 /* Are the "n" "coefficients" starting at "first" of the integer division
13272 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13273 * to each other?
13274 * The "coefficient" at position 0 is the denominator.
13275 * The "coefficient" at position 1 is the constant term.
13277 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13278 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13279 unsigned first, unsigned n)
13281 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13282 return isl_bool_error;
13283 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13284 return isl_bool_error;
13285 return isl_seq_eq(bmap1->div[pos1] + first,
13286 bmap2->div[pos2] + first, n);
13289 /* Are the integer division expressions at position "pos1" in "bmap1" and
13290 * "pos2" in "bmap2" equal to each other, except that the constant terms
13291 * are different?
13293 isl_bool isl_basic_map_equal_div_expr_except_constant(
13294 __isl_keep isl_basic_map *bmap1, int pos1,
13295 __isl_keep isl_basic_map *bmap2, int pos2)
13297 isl_bool equal;
13298 unsigned total;
13300 if (!bmap1 || !bmap2)
13301 return isl_bool_error;
13302 total = isl_basic_map_total_dim(bmap1);
13303 if (total != isl_basic_map_total_dim(bmap2))
13304 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13305 "incomparable div expressions", return isl_bool_error);
13306 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13307 0, 1);
13308 if (equal < 0 || !equal)
13309 return equal;
13310 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13311 1, 1);
13312 if (equal < 0 || equal)
13313 return isl_bool_not(equal);
13314 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13315 2, total);
13318 /* Replace the numerator of the constant term of the integer division
13319 * expression at position "div" in "bmap" by "value".
13320 * The caller guarantees that this does not change the meaning
13321 * of the input.
13323 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13324 __isl_take isl_basic_map *bmap, int div, int value)
13326 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13327 return isl_basic_map_free(bmap);
13329 isl_int_set_si(bmap->div[div][1], value);
13331 return bmap;
13334 /* Is the point "inner" internal to inequality constraint "ineq"
13335 * of "bset"?
13336 * The point is considered to be internal to the inequality constraint,
13337 * if it strictly lies on the positive side of the inequality constraint,
13338 * or if it lies on the constraint and the constraint is lexico-positive.
13340 static isl_bool is_internal(__isl_keep isl_vec *inner,
13341 __isl_keep isl_basic_set *bset, int ineq)
13343 isl_ctx *ctx;
13344 int pos;
13345 unsigned total;
13347 if (!inner || !bset)
13348 return isl_bool_error;
13350 ctx = isl_basic_set_get_ctx(bset);
13351 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13352 &ctx->normalize_gcd);
13353 if (!isl_int_is_zero(ctx->normalize_gcd))
13354 return isl_int_is_nonneg(ctx->normalize_gcd);
13356 total = isl_basic_set_dim(bset, isl_dim_all);
13357 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13358 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13361 /* Tighten the inequality constraints of "bset" that are outward with respect
13362 * to the point "vec".
13363 * That is, tighten the constraints that are not satisfied by "vec".
13365 * "vec" is a point internal to some superset S of "bset" that is used
13366 * to make the subsets of S disjoint, by tightening one half of the constraints
13367 * that separate two subsets. In particular, the constraints of S
13368 * are all satisfied by "vec" and should not be tightened.
13369 * Of the internal constraints, those that have "vec" on the outside
13370 * are tightened. The shared facet is included in the adjacent subset
13371 * with the opposite constraint.
13372 * For constraints that saturate "vec", this criterion cannot be used
13373 * to determine which of the two sides should be tightened.
13374 * Instead, the sign of the first non-zero coefficient is used
13375 * to make this choice. Note that this second criterion is never used
13376 * on the constraints of S since "vec" is interior to "S".
13378 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13379 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13381 int j;
13383 bset = isl_basic_set_cow(bset);
13384 if (!bset)
13385 return NULL;
13386 for (j = 0; j < bset->n_ineq; ++j) {
13387 isl_bool internal;
13389 internal = is_internal(vec, bset, j);
13390 if (internal < 0)
13391 return isl_basic_set_free(bset);
13392 if (internal)
13393 continue;
13394 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13397 return bset;
13400 /* Replace the variables x of type "type" starting at "first" in "bmap"
13401 * by x' with x = M x' with M the matrix trans.
13402 * That is, replace the corresponding coefficients c by c M.
13404 * The transformation matrix should be a square matrix.
13406 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13407 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13408 __isl_take isl_mat *trans)
13410 unsigned pos;
13412 bmap = isl_basic_map_cow(bmap);
13413 if (!bmap || !trans)
13414 goto error;
13416 if (trans->n_row != trans->n_col)
13417 isl_die(trans->ctx, isl_error_invalid,
13418 "expecting square transformation matrix", goto error);
13419 if (isl_basic_map_check_range(bmap, type, first, trans->n_row) < 0)
13420 goto error;
13422 pos = isl_basic_map_offset(bmap, type) + first;
13424 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13425 isl_mat_copy(trans)) < 0)
13426 goto error;
13427 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13428 isl_mat_copy(trans)) < 0)
13429 goto error;
13430 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13431 isl_mat_copy(trans)) < 0)
13432 goto error;
13434 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13435 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13437 isl_mat_free(trans);
13438 return bmap;
13439 error:
13440 isl_mat_free(trans);
13441 isl_basic_map_free(bmap);
13442 return NULL;
13445 /* Replace the variables x of type "type" starting at "first" in "bset"
13446 * by x' with x = M x' with M the matrix trans.
13447 * That is, replace the corresponding coefficients c by c M.
13449 * The transformation matrix should be a square matrix.
13451 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13452 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13453 __isl_take isl_mat *trans)
13455 return isl_basic_map_transform_dims(bset, type, first, trans);