isl_tab_basic_set_non_trivial_lexmin: extract out init_lexmin_data
[isl.git] / isl_map.c
blobb27d4da0d70a6b60c4ee7de0663d54d23fcf4d2e
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/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl/map.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
35 #include <isl_sort.h>
36 #include "isl_tab.h"
37 #include <isl/vec.h>
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.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 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
65 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
91 /* Return the space of "map".
93 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
95 return map ? map->dim : NULL;
98 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
100 return map ? n(map->dim, type) : 0;
103 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
105 return set ? n(set->dim, type) : 0;
108 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
109 enum isl_dim_type type)
111 isl_space *space;
113 if (!bmap)
114 return 0;
116 space = bmap->dim;
117 switch (type) {
118 case isl_dim_cst: return 0;
119 case isl_dim_param: return 1;
120 case isl_dim_in: return 1 + space->nparam;
121 case isl_dim_out: return 1 + space->nparam + space->n_in;
122 case isl_dim_div: return 1 + space->nparam + space->n_in +
123 space->n_out;
124 default: return 0;
128 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
129 enum isl_dim_type type)
131 return isl_basic_map_offset(bset, type);
134 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
136 return pos(map->dim, type);
139 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
140 enum isl_dim_type type)
142 return isl_basic_map_dim(bset, type);
145 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
147 return isl_basic_set_dim(bset, isl_dim_set);
150 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
152 return isl_basic_set_dim(bset, isl_dim_param);
155 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
157 if (!bset)
158 return 0;
159 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
162 unsigned isl_set_n_dim(__isl_keep isl_set *set)
164 return isl_set_dim(set, isl_dim_set);
167 unsigned isl_set_n_param(__isl_keep isl_set *set)
169 return isl_set_dim(set, isl_dim_param);
172 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
174 return bmap ? bmap->dim->n_in : 0;
177 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
179 return bmap ? bmap->dim->n_out : 0;
182 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
184 return bmap ? bmap->dim->nparam : 0;
187 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
189 return bmap ? bmap->n_div : 0;
192 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
194 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
197 unsigned isl_map_n_in(__isl_keep const isl_map *map)
199 return map ? map->dim->n_in : 0;
202 unsigned isl_map_n_out(__isl_keep const isl_map *map)
204 return map ? map->dim->n_out : 0;
207 unsigned isl_map_n_param(__isl_keep const isl_map *map)
209 return map ? map->dim->nparam : 0;
212 /* Return the number of equality constraints in the description of "bmap".
213 * Return -1 on error.
215 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
217 if (!bmap)
218 return -1;
219 return bmap->n_eq;
222 /* Return the number of equality constraints in the description of "bset".
223 * Return -1 on error.
225 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
227 return isl_basic_map_n_equality(bset_to_bmap(bset));
230 /* Return the number of inequality constraints in the description of "bmap".
231 * Return -1 on error.
233 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
235 if (!bmap)
236 return -1;
237 return bmap->n_ineq;
240 /* Return the number of inequality constraints in the description of "bset".
241 * Return -1 on error.
243 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
245 return isl_basic_map_n_inequality(bset_to_bmap(bset));
248 /* Do "bmap1" and "bmap2" have the same parameters?
250 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
251 __isl_keep isl_basic_map *bmap2)
253 isl_space *space1, *space2;
255 space1 = isl_basic_map_peek_space(bmap1);
256 space2 = isl_basic_map_peek_space(bmap2);
257 return isl_space_has_equal_params(space1, space2);
260 /* Do "map1" and "map2" have the same parameters?
262 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
263 __isl_keep isl_map *map2)
265 isl_space *space1, *space2;
267 space1 = isl_map_peek_space(map1);
268 space2 = isl_map_peek_space(map2);
269 return isl_space_has_equal_params(space1, space2);
272 /* Do "map" and "set" have the same parameters?
274 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
275 __isl_keep isl_set *set)
277 return isl_map_has_equal_params(map, set_to_map(set));
280 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
281 __isl_keep isl_set *set)
283 isl_bool m;
284 if (!map || !set)
285 return isl_bool_error;
286 m = isl_map_has_equal_params(map, set_to_map(set));
287 if (m < 0 || !m)
288 return m;
289 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
290 set->dim, isl_dim_set);
293 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
294 __isl_keep isl_basic_set *bset)
296 isl_bool m;
297 if (!bmap || !bset)
298 return isl_bool_error;
299 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
300 if (m < 0 || !m)
301 return m;
302 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
303 bset->dim, isl_dim_set);
306 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
307 __isl_keep isl_set *set)
309 isl_bool m;
310 if (!map || !set)
311 return isl_bool_error;
312 m = isl_map_has_equal_params(map, set_to_map(set));
313 if (m < 0 || !m)
314 return m;
315 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
316 set->dim, isl_dim_set);
319 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
320 __isl_keep isl_basic_set *bset)
322 isl_bool m;
323 if (!bmap || !bset)
324 return isl_bool_error;
325 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
326 if (m < 0 || !m)
327 return m;
328 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
329 bset->dim, isl_dim_set);
332 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
334 return bmap ? bmap->ctx : NULL;
337 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
339 return bset ? bset->ctx : NULL;
342 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
344 return map ? map->ctx : NULL;
347 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
349 return set ? set->ctx : NULL;
352 /* Return the space of "bmap".
354 __isl_keep isl_space *isl_basic_map_peek_space(
355 __isl_keep const isl_basic_map *bmap)
357 return bmap ? bmap->dim : NULL;
360 /* Return the space of "bset".
362 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
364 return isl_basic_map_peek_space(bset_to_bmap(bset));
367 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
369 return isl_space_copy(isl_basic_map_peek_space(bmap));
372 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
374 return isl_basic_map_get_space(bset_to_bmap(bset));
377 /* Extract the divs in "bmap" as a matrix.
379 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
381 int i;
382 isl_ctx *ctx;
383 isl_mat *div;
384 unsigned total;
385 unsigned cols;
387 if (!bmap)
388 return NULL;
390 ctx = isl_basic_map_get_ctx(bmap);
391 total = isl_space_dim(bmap->dim, isl_dim_all);
392 cols = 1 + 1 + total + bmap->n_div;
393 div = isl_mat_alloc(ctx, bmap->n_div, cols);
394 if (!div)
395 return NULL;
397 for (i = 0; i < bmap->n_div; ++i)
398 isl_seq_cpy(div->row[i], bmap->div[i], cols);
400 return div;
403 /* Extract the divs in "bset" as a matrix.
405 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
407 return isl_basic_map_get_divs(bset);
410 __isl_give isl_local_space *isl_basic_map_get_local_space(
411 __isl_keep isl_basic_map *bmap)
413 isl_mat *div;
415 if (!bmap)
416 return NULL;
418 div = isl_basic_map_get_divs(bmap);
419 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
422 __isl_give isl_local_space *isl_basic_set_get_local_space(
423 __isl_keep isl_basic_set *bset)
425 return isl_basic_map_get_local_space(bset);
428 /* For each known div d = floor(f/m), add the constraints
430 * f - m d >= 0
431 * -(f-(m-1)) + m d >= 0
433 * Do not finalize the result.
435 static __isl_give isl_basic_map *add_known_div_constraints(
436 __isl_take isl_basic_map *bmap)
438 int i;
439 unsigned n_div;
441 if (!bmap)
442 return NULL;
443 n_div = isl_basic_map_dim(bmap, isl_dim_div);
444 if (n_div == 0)
445 return bmap;
446 bmap = isl_basic_map_cow(bmap);
447 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
448 if (!bmap)
449 return NULL;
450 for (i = 0; i < n_div; ++i) {
451 if (isl_int_is_zero(bmap->div[i][0]))
452 continue;
453 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
454 return isl_basic_map_free(bmap);
457 return bmap;
460 __isl_give isl_basic_map *isl_basic_map_from_local_space(
461 __isl_take isl_local_space *ls)
463 int i;
464 int n_div;
465 isl_basic_map *bmap;
467 if (!ls)
468 return NULL;
470 n_div = isl_local_space_dim(ls, isl_dim_div);
471 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
472 n_div, 0, 2 * n_div);
474 for (i = 0; i < n_div; ++i)
475 if (isl_basic_map_alloc_div(bmap) < 0)
476 goto error;
478 for (i = 0; i < n_div; ++i)
479 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
480 bmap = add_known_div_constraints(bmap);
482 isl_local_space_free(ls);
483 return bmap;
484 error:
485 isl_local_space_free(ls);
486 isl_basic_map_free(bmap);
487 return NULL;
490 __isl_give isl_basic_set *isl_basic_set_from_local_space(
491 __isl_take isl_local_space *ls)
493 return isl_basic_map_from_local_space(ls);
496 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
498 return isl_space_copy(isl_map_peek_space(map));
501 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
503 if (!set)
504 return NULL;
505 return isl_space_copy(set->dim);
508 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
509 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
511 bmap = isl_basic_map_cow(bmap);
512 if (!bmap)
513 return NULL;
514 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
515 if (!bmap->dim)
516 goto error;
517 bmap = isl_basic_map_finalize(bmap);
518 return bmap;
519 error:
520 isl_basic_map_free(bmap);
521 return NULL;
524 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
525 __isl_take isl_basic_set *bset, const char *s)
527 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
530 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
531 enum isl_dim_type type)
533 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
536 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
537 enum isl_dim_type type, const char *s)
539 int i;
541 map = isl_map_cow(map);
542 if (!map)
543 return NULL;
545 map->dim = isl_space_set_tuple_name(map->dim, type, s);
546 if (!map->dim)
547 goto error;
549 for (i = 0; i < map->n; ++i) {
550 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
551 if (!map->p[i])
552 goto error;
555 return map;
556 error:
557 isl_map_free(map);
558 return NULL;
561 /* Replace the identifier of the tuple of type "type" by "id".
563 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
564 __isl_take isl_basic_map *bmap,
565 enum isl_dim_type type, __isl_take isl_id *id)
567 bmap = isl_basic_map_cow(bmap);
568 if (!bmap)
569 goto error;
570 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
571 if (!bmap->dim)
572 return isl_basic_map_free(bmap);
573 bmap = isl_basic_map_finalize(bmap);
574 return bmap;
575 error:
576 isl_id_free(id);
577 return NULL;
580 /* Replace the identifier of the tuple by "id".
582 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
583 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
585 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
588 /* Does the input or output tuple have a name?
590 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
592 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
595 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
596 enum isl_dim_type type)
598 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
601 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
602 const char *s)
604 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
605 isl_dim_set, s));
608 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
609 enum isl_dim_type type, __isl_take isl_id *id)
611 map = isl_map_cow(map);
612 if (!map)
613 goto error;
615 map->dim = isl_space_set_tuple_id(map->dim, type, id);
617 return isl_map_reset_space(map, isl_space_copy(map->dim));
618 error:
619 isl_id_free(id);
620 return NULL;
623 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
624 __isl_take isl_id *id)
626 return isl_map_set_tuple_id(set, isl_dim_set, id);
629 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
630 enum isl_dim_type type)
632 map = isl_map_cow(map);
633 if (!map)
634 return NULL;
636 map->dim = isl_space_reset_tuple_id(map->dim, type);
638 return isl_map_reset_space(map, isl_space_copy(map->dim));
641 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
643 return isl_map_reset_tuple_id(set, isl_dim_set);
646 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
648 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
651 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
652 enum isl_dim_type type)
654 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
657 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
659 return isl_map_has_tuple_id(set, isl_dim_set);
662 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
664 return isl_map_get_tuple_id(set, isl_dim_set);
667 /* Does the set tuple have a name?
669 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
671 if (!set)
672 return isl_bool_error;
673 return isl_space_has_tuple_name(set->dim, isl_dim_set);
677 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
679 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
682 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
684 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
687 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
688 enum isl_dim_type type, unsigned pos)
690 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
693 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
694 enum isl_dim_type type, unsigned pos)
696 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
699 /* Does the given dimension have a name?
701 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
702 enum isl_dim_type type, unsigned pos)
704 if (!map)
705 return isl_bool_error;
706 return isl_space_has_dim_name(map->dim, type, pos);
709 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
710 enum isl_dim_type type, unsigned pos)
712 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
715 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
716 enum isl_dim_type type, unsigned pos)
718 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
721 /* Does the given dimension have a name?
723 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
724 enum isl_dim_type type, unsigned pos)
726 if (!set)
727 return isl_bool_error;
728 return isl_space_has_dim_name(set->dim, type, pos);
731 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
732 __isl_take isl_basic_map *bmap,
733 enum isl_dim_type type, unsigned pos, const char *s)
735 bmap = isl_basic_map_cow(bmap);
736 if (!bmap)
737 return NULL;
738 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
739 if (!bmap->dim)
740 goto error;
741 return isl_basic_map_finalize(bmap);
742 error:
743 isl_basic_map_free(bmap);
744 return NULL;
747 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
748 enum isl_dim_type type, unsigned pos, const char *s)
750 int i;
752 map = isl_map_cow(map);
753 if (!map)
754 return NULL;
756 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
757 if (!map->dim)
758 goto error;
760 for (i = 0; i < map->n; ++i) {
761 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
762 if (!map->p[i])
763 goto error;
766 return map;
767 error:
768 isl_map_free(map);
769 return NULL;
772 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
773 __isl_take isl_basic_set *bset,
774 enum isl_dim_type type, unsigned pos, const char *s)
776 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
777 type, pos, s));
780 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
781 enum isl_dim_type type, unsigned pos, const char *s)
783 return set_from_map(isl_map_set_dim_name(set_to_map(set),
784 type, pos, s));
787 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
788 enum isl_dim_type type, unsigned pos)
790 if (!bmap)
791 return isl_bool_error;
792 return isl_space_has_dim_id(bmap->dim, type, pos);
795 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
796 enum isl_dim_type type, unsigned pos)
798 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
801 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
802 enum isl_dim_type type, unsigned pos)
804 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
807 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
808 enum isl_dim_type type, unsigned pos)
810 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
813 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
814 enum isl_dim_type type, unsigned pos)
816 return isl_map_has_dim_id(set, type, pos);
819 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
820 enum isl_dim_type type, unsigned pos)
822 return isl_map_get_dim_id(set, type, pos);
825 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
826 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
828 map = isl_map_cow(map);
829 if (!map)
830 goto error;
832 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
834 return isl_map_reset_space(map, isl_space_copy(map->dim));
835 error:
836 isl_id_free(id);
837 return NULL;
840 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
841 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
843 return isl_map_set_dim_id(set, type, pos, id);
846 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
847 __isl_keep isl_id *id)
849 if (!map)
850 return -1;
851 return isl_space_find_dim_by_id(map->dim, type, id);
854 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
855 __isl_keep isl_id *id)
857 return isl_map_find_dim_by_id(set, type, id);
860 /* Return the position of the dimension of the given type and name
861 * in "bmap".
862 * Return -1 if no such dimension can be found.
864 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
865 enum isl_dim_type type, const char *name)
867 if (!bmap)
868 return -1;
869 return isl_space_find_dim_by_name(bmap->dim, type, name);
872 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
873 const char *name)
875 if (!map)
876 return -1;
877 return isl_space_find_dim_by_name(map->dim, type, name);
880 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
881 const char *name)
883 return isl_map_find_dim_by_name(set, type, name);
886 /* Check whether equality i of bset is a pure stride constraint
887 * on a single dimension, i.e., of the form
889 * v = k e
891 * with k a constant and e an existentially quantified variable.
893 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
895 unsigned nparam;
896 unsigned d;
897 unsigned n_div;
898 int pos1;
899 int pos2;
901 if (!bset)
902 return isl_bool_error;
904 if (!isl_int_is_zero(bset->eq[i][0]))
905 return isl_bool_false;
907 nparam = isl_basic_set_dim(bset, isl_dim_param);
908 d = isl_basic_set_dim(bset, isl_dim_set);
909 n_div = isl_basic_set_dim(bset, isl_dim_div);
911 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
912 return isl_bool_false;
913 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
914 if (pos1 == -1)
915 return isl_bool_false;
916 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
917 d - pos1 - 1) != -1)
918 return isl_bool_false;
920 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
921 if (pos2 == -1)
922 return isl_bool_false;
923 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
924 n_div - pos2 - 1) != -1)
925 return isl_bool_false;
926 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
927 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
928 return isl_bool_false;
930 return isl_bool_true;
933 /* Reset the user pointer on all identifiers of parameters and tuples
934 * of the space of "map".
936 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
938 isl_space *space;
940 space = isl_map_get_space(map);
941 space = isl_space_reset_user(space);
942 map = isl_map_reset_space(map, space);
944 return map;
947 /* Reset the user pointer on all identifiers of parameters and tuples
948 * of the space of "set".
950 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
952 return isl_map_reset_user(set);
955 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
957 if (!bmap)
958 return isl_bool_error;
959 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
962 /* Has "map" been marked as a rational map?
963 * In particular, have all basic maps in "map" been marked this way?
964 * An empty map is not considered to be rational.
965 * Maps where only some of the basic maps are marked rational
966 * are not allowed.
968 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
970 int i;
971 isl_bool rational;
973 if (!map)
974 return isl_bool_error;
975 if (map->n == 0)
976 return isl_bool_false;
977 rational = isl_basic_map_is_rational(map->p[0]);
978 if (rational < 0)
979 return rational;
980 for (i = 1; i < map->n; ++i) {
981 isl_bool rational_i;
983 rational_i = isl_basic_map_is_rational(map->p[i]);
984 if (rational_i < 0)
985 return rational_i;
986 if (rational != rational_i)
987 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
988 "mixed rational and integer basic maps "
989 "not supported", return isl_bool_error);
992 return rational;
995 /* Has "set" been marked as a rational set?
996 * In particular, have all basic set in "set" been marked this way?
997 * An empty set is not considered to be rational.
998 * Sets where only some of the basic sets are marked rational
999 * are not allowed.
1001 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1003 return isl_map_is_rational(set);
1006 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1008 return isl_basic_map_is_rational(bset);
1011 /* Does "bmap" contain any rational points?
1013 * If "bmap" has an equality for each dimension, equating the dimension
1014 * to an integer constant, then it has no rational points, even if it
1015 * is marked as rational.
1017 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1019 isl_bool has_rational = isl_bool_true;
1020 unsigned total;
1022 if (!bmap)
1023 return isl_bool_error;
1024 if (isl_basic_map_plain_is_empty(bmap))
1025 return isl_bool_false;
1026 if (!isl_basic_map_is_rational(bmap))
1027 return isl_bool_false;
1028 bmap = isl_basic_map_copy(bmap);
1029 bmap = isl_basic_map_implicit_equalities(bmap);
1030 if (!bmap)
1031 return isl_bool_error;
1032 total = isl_basic_map_total_dim(bmap);
1033 if (bmap->n_eq == total) {
1034 int i, j;
1035 for (i = 0; i < bmap->n_eq; ++i) {
1036 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1037 if (j < 0)
1038 break;
1039 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1040 !isl_int_is_negone(bmap->eq[i][1 + j]))
1041 break;
1042 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1043 total - j - 1);
1044 if (j >= 0)
1045 break;
1047 if (i == bmap->n_eq)
1048 has_rational = isl_bool_false;
1050 isl_basic_map_free(bmap);
1052 return has_rational;
1055 /* Does "map" contain any rational points?
1057 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1059 int i;
1060 isl_bool has_rational;
1062 if (!map)
1063 return isl_bool_error;
1064 for (i = 0; i < map->n; ++i) {
1065 has_rational = isl_basic_map_has_rational(map->p[i]);
1066 if (has_rational < 0 || has_rational)
1067 return has_rational;
1069 return isl_bool_false;
1072 /* Does "set" contain any rational points?
1074 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1076 return isl_map_has_rational(set);
1079 /* Is this basic set a parameter domain?
1081 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1083 if (!bset)
1084 return isl_bool_error;
1085 return isl_space_is_params(bset->dim);
1088 /* Is this set a parameter domain?
1090 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1092 if (!set)
1093 return isl_bool_error;
1094 return isl_space_is_params(set->dim);
1097 /* Is this map actually a parameter domain?
1098 * Users should never call this function. Outside of isl,
1099 * a map can never be a parameter domain.
1101 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1103 if (!map)
1104 return isl_bool_error;
1105 return isl_space_is_params(map->dim);
1108 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
1109 struct isl_basic_map *bmap, unsigned extra,
1110 unsigned n_eq, unsigned n_ineq)
1112 int i;
1113 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1115 bmap->ctx = ctx;
1116 isl_ctx_ref(ctx);
1118 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1119 if (isl_blk_is_error(bmap->block))
1120 goto error;
1122 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1123 if ((n_ineq + n_eq) && !bmap->ineq)
1124 goto error;
1126 if (extra == 0) {
1127 bmap->block2 = isl_blk_empty();
1128 bmap->div = NULL;
1129 } else {
1130 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1131 if (isl_blk_is_error(bmap->block2))
1132 goto error;
1134 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1135 if (!bmap->div)
1136 goto error;
1139 for (i = 0; i < n_ineq + n_eq; ++i)
1140 bmap->ineq[i] = bmap->block.data + i * row_size;
1142 for (i = 0; i < extra; ++i)
1143 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1145 bmap->ref = 1;
1146 bmap->flags = 0;
1147 bmap->c_size = n_eq + n_ineq;
1148 bmap->eq = bmap->ineq + n_ineq;
1149 bmap->extra = extra;
1150 bmap->n_eq = 0;
1151 bmap->n_ineq = 0;
1152 bmap->n_div = 0;
1153 bmap->sample = NULL;
1155 return bmap;
1156 error:
1157 isl_basic_map_free(bmap);
1158 return NULL;
1161 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1162 unsigned nparam, unsigned dim, unsigned extra,
1163 unsigned n_eq, unsigned n_ineq)
1165 struct isl_basic_map *bmap;
1166 isl_space *space;
1168 space = isl_space_set_alloc(ctx, nparam, dim);
1169 if (!space)
1170 return NULL;
1172 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1173 return bset_from_bmap(bmap);
1176 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1177 unsigned extra, unsigned n_eq, unsigned n_ineq)
1179 struct isl_basic_map *bmap;
1180 if (!dim)
1181 return NULL;
1182 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1183 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1184 return bset_from_bmap(bmap);
1185 error:
1186 isl_space_free(dim);
1187 return NULL;
1190 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1191 unsigned extra, unsigned n_eq, unsigned n_ineq)
1193 struct isl_basic_map *bmap;
1195 if (!dim)
1196 return NULL;
1197 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1198 if (!bmap)
1199 goto error;
1200 bmap->dim = dim;
1202 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1203 error:
1204 isl_space_free(dim);
1205 return NULL;
1208 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1209 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1210 unsigned n_eq, unsigned n_ineq)
1212 struct isl_basic_map *bmap;
1213 isl_space *dim;
1215 dim = isl_space_alloc(ctx, nparam, in, out);
1216 if (!dim)
1217 return NULL;
1219 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1220 return bmap;
1223 static void dup_constraints(
1224 struct isl_basic_map *dst, struct isl_basic_map *src)
1226 int i;
1227 unsigned total = isl_basic_map_total_dim(src);
1229 for (i = 0; i < src->n_eq; ++i) {
1230 int j = isl_basic_map_alloc_equality(dst);
1231 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1234 for (i = 0; i < src->n_ineq; ++i) {
1235 int j = isl_basic_map_alloc_inequality(dst);
1236 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1239 for (i = 0; i < src->n_div; ++i) {
1240 int j = isl_basic_map_alloc_div(dst);
1241 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1243 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1246 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1248 struct isl_basic_map *dup;
1250 if (!bmap)
1251 return NULL;
1252 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1253 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1254 if (!dup)
1255 return NULL;
1256 dup_constraints(dup, bmap);
1257 dup->flags = bmap->flags;
1258 dup->sample = isl_vec_copy(bmap->sample);
1259 return dup;
1262 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1264 struct isl_basic_map *dup;
1266 dup = isl_basic_map_dup(bset_to_bmap(bset));
1267 return bset_from_bmap(dup);
1270 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1272 if (!bset)
1273 return NULL;
1275 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1276 bset->ref++;
1277 return bset;
1279 return isl_basic_set_dup(bset);
1282 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1284 if (!set)
1285 return NULL;
1287 set->ref++;
1288 return set;
1291 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1293 if (!bmap)
1294 return NULL;
1296 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1297 bmap->ref++;
1298 return bmap;
1300 bmap = isl_basic_map_dup(bmap);
1301 if (bmap)
1302 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1303 return bmap;
1306 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1308 if (!map)
1309 return NULL;
1311 map->ref++;
1312 return map;
1315 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1317 if (!bmap)
1318 return NULL;
1320 if (--bmap->ref > 0)
1321 return NULL;
1323 isl_ctx_deref(bmap->ctx);
1324 free(bmap->div);
1325 isl_blk_free(bmap->ctx, bmap->block2);
1326 free(bmap->ineq);
1327 isl_blk_free(bmap->ctx, bmap->block);
1328 isl_vec_free(bmap->sample);
1329 isl_space_free(bmap->dim);
1330 free(bmap);
1332 return NULL;
1335 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1337 return isl_basic_map_free(bset_to_bmap(bset));
1340 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1342 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1345 /* Check that "map" has only named parameters, reporting an error
1346 * if it does not.
1348 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1350 return isl_space_check_named_params(isl_map_peek_space(map));
1353 /* Check that "bmap1" and "bmap2" have the same parameters,
1354 * reporting an error if they do not.
1356 static isl_stat isl_basic_map_check_equal_params(
1357 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1359 isl_bool match;
1361 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1362 if (match < 0)
1363 return isl_stat_error;
1364 if (!match)
1365 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1366 "parameters don't match", return isl_stat_error);
1367 return isl_stat_ok;
1370 __isl_give isl_map *isl_map_align_params_map_map_and(
1371 __isl_take isl_map *map1, __isl_take isl_map *map2,
1372 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1373 __isl_take isl_map *map2))
1375 if (!map1 || !map2)
1376 goto error;
1377 if (isl_map_has_equal_params(map1, map2))
1378 return fn(map1, map2);
1379 if (isl_map_check_named_params(map1) < 0)
1380 goto error;
1381 if (isl_map_check_named_params(map2) < 0)
1382 goto error;
1383 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1384 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1385 return fn(map1, map2);
1386 error:
1387 isl_map_free(map1);
1388 isl_map_free(map2);
1389 return NULL;
1392 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1393 __isl_keep isl_map *map2,
1394 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1396 isl_bool r;
1398 if (!map1 || !map2)
1399 return isl_bool_error;
1400 if (isl_map_has_equal_params(map1, map2))
1401 return fn(map1, map2);
1402 if (isl_map_check_named_params(map1) < 0)
1403 return isl_bool_error;
1404 if (isl_map_check_named_params(map2) < 0)
1405 return isl_bool_error;
1406 map1 = isl_map_copy(map1);
1407 map2 = isl_map_copy(map2);
1408 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1409 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1410 r = fn(map1, map2);
1411 isl_map_free(map1);
1412 isl_map_free(map2);
1413 return r;
1416 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1418 struct isl_ctx *ctx;
1419 if (!bmap)
1420 return -1;
1421 ctx = bmap->ctx;
1422 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1423 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1424 return -1);
1425 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1426 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1427 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1428 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1429 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1430 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1431 isl_int *t;
1432 int j = isl_basic_map_alloc_inequality(bmap);
1433 if (j < 0)
1434 return -1;
1435 t = bmap->ineq[j];
1436 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1437 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1438 bmap->eq[-1] = t;
1439 bmap->n_eq++;
1440 bmap->n_ineq--;
1441 bmap->eq--;
1442 return 0;
1444 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1445 bmap->extra - bmap->n_div);
1446 return bmap->n_eq++;
1449 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1451 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1454 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1456 if (!bmap)
1457 return -1;
1458 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1459 bmap->n_eq -= n;
1460 return 0;
1463 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1465 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1468 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1470 isl_int *t;
1471 if (!bmap)
1472 return -1;
1473 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1475 if (pos != bmap->n_eq - 1) {
1476 t = bmap->eq[pos];
1477 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1478 bmap->eq[bmap->n_eq - 1] = t;
1480 bmap->n_eq--;
1481 return 0;
1484 /* Turn inequality "pos" of "bmap" into an equality.
1486 * In particular, we move the inequality in front of the equalities
1487 * and move the last inequality in the position of the moved inequality.
1488 * Note that isl_tab_make_equalities_explicit depends on this particular
1489 * change in the ordering of the constraints.
1491 void isl_basic_map_inequality_to_equality(
1492 struct isl_basic_map *bmap, unsigned pos)
1494 isl_int *t;
1496 t = bmap->ineq[pos];
1497 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1498 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1499 bmap->eq[-1] = t;
1500 bmap->n_eq++;
1501 bmap->n_ineq--;
1502 bmap->eq--;
1503 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1504 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1505 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1506 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1509 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1511 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1514 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1516 struct isl_ctx *ctx;
1517 if (!bmap)
1518 return -1;
1519 ctx = bmap->ctx;
1520 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1521 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1522 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1523 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1524 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1525 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1526 1 + isl_basic_map_total_dim(bmap),
1527 bmap->extra - bmap->n_div);
1528 return bmap->n_ineq++;
1531 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1533 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1536 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1538 if (!bmap)
1539 return -1;
1540 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1541 bmap->n_ineq -= n;
1542 return 0;
1545 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1547 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1550 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1552 isl_int *t;
1553 if (!bmap)
1554 return -1;
1555 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1557 if (pos != bmap->n_ineq - 1) {
1558 t = bmap->ineq[pos];
1559 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1560 bmap->ineq[bmap->n_ineq - 1] = t;
1561 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1563 bmap->n_ineq--;
1564 return 0;
1567 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1569 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1572 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1573 isl_int *eq)
1575 int k;
1577 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1578 if (!bmap)
1579 return NULL;
1580 k = isl_basic_map_alloc_equality(bmap);
1581 if (k < 0)
1582 goto error;
1583 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1584 return bmap;
1585 error:
1586 isl_basic_map_free(bmap);
1587 return NULL;
1590 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1591 isl_int *eq)
1593 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1596 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1597 isl_int *ineq)
1599 int k;
1601 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1602 if (!bmap)
1603 return NULL;
1604 k = isl_basic_map_alloc_inequality(bmap);
1605 if (k < 0)
1606 goto error;
1607 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1608 return bmap;
1609 error:
1610 isl_basic_map_free(bmap);
1611 return NULL;
1614 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1615 isl_int *ineq)
1617 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1620 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1622 if (!bmap)
1623 return -1;
1624 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1625 isl_seq_clr(bmap->div[bmap->n_div] +
1626 1 + 1 + isl_basic_map_total_dim(bmap),
1627 bmap->extra - bmap->n_div);
1628 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1629 return bmap->n_div++;
1632 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1634 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1637 /* Check that there are "n" dimensions of type "type" starting at "first"
1638 * in "bmap".
1640 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1641 enum isl_dim_type type, unsigned first, unsigned n)
1643 unsigned dim;
1645 if (!bmap)
1646 return isl_stat_error;
1647 dim = isl_basic_map_dim(bmap, type);
1648 if (first + n > dim || first + n < first)
1649 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1650 "position or range out of bounds",
1651 return isl_stat_error);
1652 return isl_stat_ok;
1655 /* Insert an extra integer division, prescribed by "div", to "bmap"
1656 * at (integer division) position "pos".
1658 * The integer division is first added at the end and then moved
1659 * into the right position.
1661 __isl_give isl_basic_map *isl_basic_map_insert_div(
1662 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1664 int i, k;
1666 bmap = isl_basic_map_cow(bmap);
1667 if (!bmap || !div)
1668 return isl_basic_map_free(bmap);
1670 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1671 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1672 "unexpected size", return isl_basic_map_free(bmap));
1673 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1674 return isl_basic_map_free(bmap);
1676 bmap = isl_basic_map_extend_space(bmap,
1677 isl_basic_map_get_space(bmap), 1, 0, 2);
1678 k = isl_basic_map_alloc_div(bmap);
1679 if (k < 0)
1680 return isl_basic_map_free(bmap);
1681 isl_seq_cpy(bmap->div[k], div->el, div->size);
1682 isl_int_set_si(bmap->div[k][div->size], 0);
1684 for (i = k; i > pos; --i)
1685 isl_basic_map_swap_div(bmap, i, i - 1);
1687 return bmap;
1690 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1692 if (!bmap)
1693 return isl_stat_error;
1694 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1695 bmap->n_div -= n;
1696 return isl_stat_ok;
1699 /* Copy constraint from src to dst, putting the vars of src at offset
1700 * dim_off in dst and the divs of src at offset div_off in dst.
1701 * If both sets are actually map, then dim_off applies to the input
1702 * variables.
1704 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1705 struct isl_basic_map *src_map, isl_int *src,
1706 unsigned in_off, unsigned out_off, unsigned div_off)
1708 unsigned src_nparam = isl_basic_map_dim(src_map, isl_dim_param);
1709 unsigned dst_nparam = isl_basic_map_dim(dst_map, isl_dim_param);
1710 unsigned src_in = isl_basic_map_dim(src_map, isl_dim_in);
1711 unsigned dst_in = isl_basic_map_dim(dst_map, isl_dim_in);
1712 unsigned src_out = isl_basic_map_dim(src_map, isl_dim_out);
1713 unsigned dst_out = isl_basic_map_dim(dst_map, isl_dim_out);
1714 isl_int_set(dst[0], src[0]);
1715 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1716 if (dst_nparam > src_nparam)
1717 isl_seq_clr(dst+1+src_nparam,
1718 dst_nparam - src_nparam);
1719 isl_seq_clr(dst+1+dst_nparam, in_off);
1720 isl_seq_cpy(dst+1+dst_nparam+in_off,
1721 src+1+src_nparam,
1722 isl_min(dst_in-in_off, src_in));
1723 if (dst_in-in_off > src_in)
1724 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1725 dst_in - in_off - src_in);
1726 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1727 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1728 src+1+src_nparam+src_in,
1729 isl_min(dst_out-out_off, src_out));
1730 if (dst_out-out_off > src_out)
1731 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1732 dst_out - out_off - src_out);
1733 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1734 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1735 src+1+src_nparam+src_in+src_out,
1736 isl_min(dst_map->extra-div_off, src_map->n_div));
1737 if (dst_map->n_div-div_off > src_map->n_div)
1738 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1739 div_off+src_map->n_div,
1740 dst_map->n_div - div_off - src_map->n_div);
1743 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1744 struct isl_basic_map *src_map, isl_int *src,
1745 unsigned in_off, unsigned out_off, unsigned div_off)
1747 isl_int_set(dst[0], src[0]);
1748 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1751 static __isl_give isl_basic_map *add_constraints(
1752 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1753 unsigned i_pos, unsigned o_pos)
1755 int i;
1756 unsigned div_off;
1758 if (!bmap1 || !bmap2)
1759 goto error;
1761 div_off = bmap1->n_div;
1763 for (i = 0; i < bmap2->n_eq; ++i) {
1764 int i1 = isl_basic_map_alloc_equality(bmap1);
1765 if (i1 < 0)
1766 goto error;
1767 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1768 i_pos, o_pos, div_off);
1771 for (i = 0; i < bmap2->n_ineq; ++i) {
1772 int i1 = isl_basic_map_alloc_inequality(bmap1);
1773 if (i1 < 0)
1774 goto error;
1775 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1776 i_pos, o_pos, div_off);
1779 for (i = 0; i < bmap2->n_div; ++i) {
1780 int i1 = isl_basic_map_alloc_div(bmap1);
1781 if (i1 < 0)
1782 goto error;
1783 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1784 i_pos, o_pos, div_off);
1787 isl_basic_map_free(bmap2);
1789 return bmap1;
1791 error:
1792 isl_basic_map_free(bmap1);
1793 isl_basic_map_free(bmap2);
1794 return NULL;
1797 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1798 struct isl_basic_set *bset2, unsigned pos)
1800 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1801 bset_to_bmap(bset2), 0, pos));
1804 __isl_give isl_basic_map *isl_basic_map_extend_space(
1805 __isl_take isl_basic_map *base, __isl_take isl_space *dim,
1806 unsigned extra, unsigned n_eq, unsigned n_ineq)
1808 struct isl_basic_map *ext;
1809 unsigned flags;
1810 int dims_ok;
1812 if (!dim)
1813 goto error;
1815 if (!base)
1816 goto error;
1818 dims_ok = isl_space_is_equal(base->dim, dim) &&
1819 base->extra >= base->n_div + extra;
1821 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1822 room_for_ineq(base, n_ineq)) {
1823 isl_space_free(dim);
1824 return base;
1827 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1828 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1829 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1830 extra += base->extra;
1831 n_eq += base->n_eq;
1832 n_ineq += base->n_ineq;
1834 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1835 dim = NULL;
1836 if (!ext)
1837 goto error;
1839 if (dims_ok)
1840 ext->sample = isl_vec_copy(base->sample);
1841 flags = base->flags;
1842 ext = add_constraints(ext, base, 0, 0);
1843 if (ext) {
1844 ext->flags = flags;
1845 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1848 return ext;
1850 error:
1851 isl_space_free(dim);
1852 isl_basic_map_free(base);
1853 return NULL;
1856 __isl_give isl_basic_set *isl_basic_set_extend_space(
1857 __isl_take isl_basic_set *base,
1858 __isl_take isl_space *dim, unsigned extra,
1859 unsigned n_eq, unsigned n_ineq)
1861 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1862 dim, extra, n_eq, n_ineq));
1865 struct isl_basic_map *isl_basic_map_extend_constraints(
1866 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1868 if (!base)
1869 return NULL;
1870 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1871 0, n_eq, n_ineq);
1874 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1875 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1876 unsigned n_eq, unsigned n_ineq)
1878 struct isl_basic_map *bmap;
1879 isl_space *dim;
1881 if (!base)
1882 return NULL;
1883 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1884 if (!dim)
1885 goto error;
1887 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1888 return bmap;
1889 error:
1890 isl_basic_map_free(base);
1891 return NULL;
1894 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1895 unsigned nparam, unsigned dim, unsigned extra,
1896 unsigned n_eq, unsigned n_ineq)
1898 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1899 nparam, 0, dim, extra, n_eq, n_ineq));
1902 struct isl_basic_set *isl_basic_set_extend_constraints(
1903 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1905 isl_basic_map *bmap = bset_to_bmap(base);
1906 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1907 return bset_from_bmap(bmap);
1910 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1912 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1915 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1917 if (!bmap)
1918 return NULL;
1920 if (bmap->ref > 1) {
1921 bmap->ref--;
1922 bmap = isl_basic_map_dup(bmap);
1924 if (bmap) {
1925 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1926 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1928 return bmap;
1931 /* Clear all cached information in "map", either because it is about
1932 * to be modified or because it is being freed.
1933 * Always return the same pointer that is passed in.
1934 * This is needed for the use in isl_map_free.
1936 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1938 isl_basic_map_free(map->cached_simple_hull[0]);
1939 isl_basic_map_free(map->cached_simple_hull[1]);
1940 map->cached_simple_hull[0] = NULL;
1941 map->cached_simple_hull[1] = NULL;
1942 return map;
1945 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1947 return isl_map_cow(set);
1950 /* Return an isl_map that is equal to "map" and that has only
1951 * a single reference.
1953 * If the original input already has only one reference, then
1954 * simply return it, but clear all cached information, since
1955 * it may be rendered invalid by the operations that will be
1956 * performed on the result.
1958 * Otherwise, create a duplicate (without any cached information).
1960 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1962 if (!map)
1963 return NULL;
1965 if (map->ref == 1)
1966 return clear_caches(map);
1967 map->ref--;
1968 return isl_map_dup(map);
1971 static void swap_vars(struct isl_blk blk, isl_int *a,
1972 unsigned a_len, unsigned b_len)
1974 isl_seq_cpy(blk.data, a+a_len, b_len);
1975 isl_seq_cpy(blk.data+b_len, a, a_len);
1976 isl_seq_cpy(a, blk.data, b_len+a_len);
1979 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1980 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1982 int i;
1983 struct isl_blk blk;
1985 if (!bmap)
1986 goto error;
1988 isl_assert(bmap->ctx,
1989 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1991 if (n1 == 0 || n2 == 0)
1992 return bmap;
1994 bmap = isl_basic_map_cow(bmap);
1995 if (!bmap)
1996 return NULL;
1998 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1999 if (isl_blk_is_error(blk))
2000 goto error;
2002 for (i = 0; i < bmap->n_eq; ++i)
2003 swap_vars(blk,
2004 bmap->eq[i] + pos, n1, n2);
2006 for (i = 0; i < bmap->n_ineq; ++i)
2007 swap_vars(blk,
2008 bmap->ineq[i] + pos, n1, n2);
2010 for (i = 0; i < bmap->n_div; ++i)
2011 swap_vars(blk,
2012 bmap->div[i]+1 + pos, n1, n2);
2014 isl_blk_free(bmap->ctx, blk);
2016 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
2017 bmap = isl_basic_map_gauss(bmap, NULL);
2018 return isl_basic_map_finalize(bmap);
2019 error:
2020 isl_basic_map_free(bmap);
2021 return NULL;
2024 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2025 __isl_take isl_basic_map *bmap)
2027 int i = 0;
2028 unsigned total;
2029 if (!bmap)
2030 goto error;
2031 total = isl_basic_map_total_dim(bmap);
2032 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2033 return isl_basic_map_free(bmap);
2034 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2035 if (bmap->n_eq > 0)
2036 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2037 else {
2038 i = isl_basic_map_alloc_equality(bmap);
2039 if (i < 0)
2040 goto error;
2042 isl_int_set_si(bmap->eq[i][0], 1);
2043 isl_seq_clr(bmap->eq[i]+1, total);
2044 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2045 isl_vec_free(bmap->sample);
2046 bmap->sample = NULL;
2047 return isl_basic_map_finalize(bmap);
2048 error:
2049 isl_basic_map_free(bmap);
2050 return NULL;
2053 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2054 __isl_take isl_basic_set *bset)
2056 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2059 __isl_give isl_basic_map *isl_basic_map_set_rational(
2060 __isl_take isl_basic_map *bmap)
2062 if (!bmap)
2063 return NULL;
2065 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2066 return bmap;
2068 bmap = isl_basic_map_cow(bmap);
2069 if (!bmap)
2070 return NULL;
2072 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2074 return isl_basic_map_finalize(bmap);
2077 __isl_give isl_basic_set *isl_basic_set_set_rational(
2078 __isl_take isl_basic_set *bset)
2080 return isl_basic_map_set_rational(bset);
2083 __isl_give isl_basic_set *isl_basic_set_set_integral(
2084 __isl_take isl_basic_set *bset)
2086 if (!bset)
2087 return NULL;
2089 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2090 return bset;
2092 bset = isl_basic_set_cow(bset);
2093 if (!bset)
2094 return NULL;
2096 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2098 return isl_basic_set_finalize(bset);
2101 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2103 int i;
2105 map = isl_map_cow(map);
2106 if (!map)
2107 return NULL;
2108 for (i = 0; i < map->n; ++i) {
2109 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2110 if (!map->p[i])
2111 goto error;
2113 return map;
2114 error:
2115 isl_map_free(map);
2116 return NULL;
2119 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2121 return isl_map_set_rational(set);
2124 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2125 * of "bmap").
2127 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2129 isl_int *t = bmap->div[a];
2130 bmap->div[a] = bmap->div[b];
2131 bmap->div[b] = t;
2134 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2135 * div definitions accordingly.
2137 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2139 int i;
2140 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2142 swap_div(bmap, a, b);
2144 for (i = 0; i < bmap->n_eq; ++i)
2145 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2147 for (i = 0; i < bmap->n_ineq; ++i)
2148 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2150 for (i = 0; i < bmap->n_div; ++i)
2151 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2152 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2155 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
2156 * div definitions accordingly.
2158 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
2160 isl_basic_map_swap_div(bset, a, b);
2163 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2165 isl_seq_cpy(c, c + n, rem);
2166 isl_seq_clr(c + rem, n);
2169 /* Drop n dimensions starting at first.
2171 * In principle, this frees up some extra variables as the number
2172 * of columns remains constant, but we would have to extend
2173 * the div array too as the number of rows in this array is assumed
2174 * to be equal to extra.
2176 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2177 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2179 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2182 /* Move "n" divs starting at "first" to the end of the list of divs.
2184 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2185 unsigned first, unsigned n)
2187 isl_int **div;
2188 int i;
2190 if (first + n == bmap->n_div)
2191 return bmap;
2193 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2194 if (!div)
2195 goto error;
2196 for (i = 0; i < n; ++i)
2197 div[i] = bmap->div[first + i];
2198 for (i = 0; i < bmap->n_div - first - n; ++i)
2199 bmap->div[first + i] = bmap->div[first + n + i];
2200 for (i = 0; i < n; ++i)
2201 bmap->div[bmap->n_div - n + i] = div[i];
2202 free(div);
2203 return bmap;
2204 error:
2205 isl_basic_map_free(bmap);
2206 return NULL;
2209 /* Drop "n" dimensions of type "type" starting at "first".
2211 * In principle, this frees up some extra variables as the number
2212 * of columns remains constant, but we would have to extend
2213 * the div array too as the number of rows in this array is assumed
2214 * to be equal to extra.
2216 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2217 enum isl_dim_type type, unsigned first, unsigned n)
2219 int i;
2220 unsigned dim;
2221 unsigned offset;
2222 unsigned left;
2224 if (!bmap)
2225 goto error;
2227 dim = isl_basic_map_dim(bmap, type);
2228 isl_assert(bmap->ctx, first + n <= dim, goto error);
2230 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2231 return bmap;
2233 bmap = isl_basic_map_cow(bmap);
2234 if (!bmap)
2235 return NULL;
2237 offset = isl_basic_map_offset(bmap, type) + first;
2238 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2239 for (i = 0; i < bmap->n_eq; ++i)
2240 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2242 for (i = 0; i < bmap->n_ineq; ++i)
2243 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2245 for (i = 0; i < bmap->n_div; ++i)
2246 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2248 if (type == isl_dim_div) {
2249 bmap = move_divs_last(bmap, first, n);
2250 if (!bmap)
2251 goto error;
2252 if (isl_basic_map_free_div(bmap, n) < 0)
2253 return isl_basic_map_free(bmap);
2254 } else
2255 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2256 if (!bmap->dim)
2257 goto error;
2259 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2260 bmap = isl_basic_map_simplify(bmap);
2261 return isl_basic_map_finalize(bmap);
2262 error:
2263 isl_basic_map_free(bmap);
2264 return NULL;
2267 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2268 enum isl_dim_type type, unsigned first, unsigned n)
2270 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2271 type, first, n));
2274 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2275 enum isl_dim_type type, unsigned first, unsigned n)
2277 int i;
2279 if (!map)
2280 goto error;
2282 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2284 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2285 return map;
2286 map = isl_map_cow(map);
2287 if (!map)
2288 goto error;
2289 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2290 if (!map->dim)
2291 goto error;
2293 for (i = 0; i < map->n; ++i) {
2294 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2295 if (!map->p[i])
2296 goto error;
2298 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2300 return map;
2301 error:
2302 isl_map_free(map);
2303 return NULL;
2306 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2307 enum isl_dim_type type, unsigned first, unsigned n)
2309 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2313 * We don't cow, as the div is assumed to be redundant.
2315 __isl_give isl_basic_map *isl_basic_map_drop_div(
2316 __isl_take isl_basic_map *bmap, unsigned div)
2318 int i;
2319 unsigned pos;
2321 if (!bmap)
2322 goto error;
2324 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2326 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
2328 for (i = 0; i < bmap->n_eq; ++i)
2329 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2331 for (i = 0; i < bmap->n_ineq; ++i) {
2332 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
2333 isl_basic_map_drop_inequality(bmap, i);
2334 --i;
2335 continue;
2337 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2340 for (i = 0; i < bmap->n_div; ++i)
2341 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2343 if (div != bmap->n_div - 1) {
2344 int j;
2345 isl_int *t = bmap->div[div];
2347 for (j = div; j < bmap->n_div - 1; ++j)
2348 bmap->div[j] = bmap->div[j+1];
2350 bmap->div[bmap->n_div - 1] = t;
2352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2353 if (isl_basic_map_free_div(bmap, 1) < 0)
2354 return isl_basic_map_free(bmap);
2356 return bmap;
2357 error:
2358 isl_basic_map_free(bmap);
2359 return NULL;
2362 /* Eliminate the specified n dimensions starting at first from the
2363 * constraints, without removing the dimensions from the space.
2364 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2366 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2367 enum isl_dim_type type, unsigned first, unsigned n)
2369 int i;
2371 if (!map)
2372 return NULL;
2373 if (n == 0)
2374 return map;
2376 if (first + n > isl_map_dim(map, type) || first + n < first)
2377 isl_die(map->ctx, isl_error_invalid,
2378 "index out of bounds", goto error);
2380 map = isl_map_cow(map);
2381 if (!map)
2382 return NULL;
2384 for (i = 0; i < map->n; ++i) {
2385 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2386 if (!map->p[i])
2387 goto error;
2389 return map;
2390 error:
2391 isl_map_free(map);
2392 return NULL;
2395 /* Eliminate the specified n dimensions starting at first from the
2396 * constraints, without removing the dimensions from the space.
2397 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2399 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2400 enum isl_dim_type type, unsigned first, unsigned n)
2402 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2405 /* Eliminate the specified n dimensions starting at first from the
2406 * constraints, without removing the dimensions from the space.
2407 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2409 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2410 unsigned first, unsigned n)
2412 return isl_set_eliminate(set, isl_dim_set, first, n);
2415 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2416 __isl_take isl_basic_map *bmap)
2418 if (!bmap)
2419 return NULL;
2420 bmap = isl_basic_map_eliminate_vars(bmap,
2421 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2422 if (!bmap)
2423 return NULL;
2424 bmap->n_div = 0;
2425 return isl_basic_map_finalize(bmap);
2428 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2429 __isl_take isl_basic_set *bset)
2431 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2434 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2436 int i;
2438 if (!map)
2439 return NULL;
2440 if (map->n == 0)
2441 return map;
2443 map = isl_map_cow(map);
2444 if (!map)
2445 return NULL;
2447 for (i = 0; i < map->n; ++i) {
2448 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2449 if (!map->p[i])
2450 goto error;
2452 return map;
2453 error:
2454 isl_map_free(map);
2455 return NULL;
2458 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2460 return isl_map_remove_divs(set);
2463 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2464 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2465 unsigned first, unsigned n)
2467 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2468 return isl_basic_map_free(bmap);
2469 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2470 return bmap;
2471 bmap = isl_basic_map_eliminate_vars(bmap,
2472 isl_basic_map_offset(bmap, type) - 1 + first, n);
2473 if (!bmap)
2474 return bmap;
2475 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2476 return bmap;
2477 bmap = isl_basic_map_drop(bmap, type, first, n);
2478 return bmap;
2481 /* Return true if the definition of the given div (recursively) involves
2482 * any of the given variables.
2484 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2485 unsigned first, unsigned n)
2487 int i;
2488 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2490 if (isl_int_is_zero(bmap->div[div][0]))
2491 return isl_bool_false;
2492 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2493 return isl_bool_true;
2495 for (i = bmap->n_div - 1; i >= 0; --i) {
2496 isl_bool involves;
2498 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2499 continue;
2500 involves = div_involves_vars(bmap, i, first, n);
2501 if (involves < 0 || involves)
2502 return involves;
2505 return isl_bool_false;
2508 /* Try and add a lower and/or upper bound on "div" to "bmap"
2509 * based on inequality "i".
2510 * "total" is the total number of variables (excluding the divs).
2511 * "v" is a temporary object that can be used during the calculations.
2512 * If "lb" is set, then a lower bound should be constructed.
2513 * If "ub" is set, then an upper bound should be constructed.
2515 * The calling function has already checked that the inequality does not
2516 * reference "div", but we still need to check that the inequality is
2517 * of the right form. We'll consider the case where we want to construct
2518 * a lower bound. The construction of upper bounds is similar.
2520 * Let "div" be of the form
2522 * q = floor((a + f(x))/d)
2524 * We essentially check if constraint "i" is of the form
2526 * b + f(x) >= 0
2528 * so that we can use it to derive a lower bound on "div".
2529 * However, we allow a slightly more general form
2531 * b + g(x) >= 0
2533 * with the condition that the coefficients of g(x) - f(x) are all
2534 * divisible by d.
2535 * Rewriting this constraint as
2537 * 0 >= -b - g(x)
2539 * adding a + f(x) to both sides and dividing by d, we obtain
2541 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2543 * Taking the floor on both sides, we obtain
2545 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2547 * or
2549 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2551 * In the case of an upper bound, we construct the constraint
2553 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2556 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2557 __isl_take isl_basic_map *bmap, int div, int i,
2558 unsigned total, isl_int v, int lb, int ub)
2560 int j;
2562 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2563 if (lb) {
2564 isl_int_sub(v, bmap->ineq[i][1 + j],
2565 bmap->div[div][1 + 1 + j]);
2566 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2568 if (ub) {
2569 isl_int_add(v, bmap->ineq[i][1 + j],
2570 bmap->div[div][1 + 1 + j]);
2571 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2574 if (!lb && !ub)
2575 return bmap;
2577 bmap = isl_basic_map_cow(bmap);
2578 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2579 if (lb) {
2580 int k = isl_basic_map_alloc_inequality(bmap);
2581 if (k < 0)
2582 goto error;
2583 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2584 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2585 bmap->div[div][1 + j]);
2586 isl_int_cdiv_q(bmap->ineq[k][j],
2587 bmap->ineq[k][j], bmap->div[div][0]);
2589 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2591 if (ub) {
2592 int k = isl_basic_map_alloc_inequality(bmap);
2593 if (k < 0)
2594 goto error;
2595 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2596 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2597 bmap->div[div][1 + j]);
2598 isl_int_fdiv_q(bmap->ineq[k][j],
2599 bmap->ineq[k][j], bmap->div[div][0]);
2601 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2604 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
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 (!map)
2808 return isl_bool_error;
2810 if (first + n > isl_map_dim(map, type))
2811 isl_die(map->ctx, isl_error_invalid,
2812 "index out of bounds", return isl_bool_error);
2814 for (i = 0; i < map->n; ++i) {
2815 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2816 type, first, n);
2817 if (involves < 0 || involves)
2818 return involves;
2821 return isl_bool_false;
2824 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2825 enum isl_dim_type type, unsigned first, unsigned n)
2827 return isl_basic_map_involves_dims(bset, type, first, n);
2830 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2831 enum isl_dim_type type, unsigned first, unsigned n)
2833 return isl_map_involves_dims(set, type, first, n);
2836 /* Drop all constraints in bmap that involve any of the dimensions
2837 * first to first+n-1.
2839 static __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 bmap = isl_basic_map_add_known_div_constraints(bmap);
2865 return bmap;
2868 /* Drop all constraints in bset that involve any of the dimensions
2869 * first to first+n-1.
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 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2947 /* Drop all constraints in bset that involve any of the dimensions
2948 * first to first + n - 1 of the given type.
2950 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2951 __isl_take isl_basic_set *bset,
2952 enum isl_dim_type type, unsigned first, unsigned n)
2954 return isl_basic_map_drop_constraints_involving_dims(bset,
2955 type, first, n);
2958 /* Drop constraints from "map" by applying "drop" to each basic map.
2960 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2961 enum isl_dim_type type, unsigned first, unsigned n,
2962 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2963 enum isl_dim_type type, unsigned first, unsigned n))
2965 int i;
2966 unsigned dim;
2968 if (!map)
2969 return NULL;
2971 dim = isl_map_dim(map, type);
2972 if (first + n > dim || first + n < first)
2973 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2974 "index out of bounds", return isl_map_free(map));
2976 map = isl_map_cow(map);
2977 if (!map)
2978 return NULL;
2980 for (i = 0; i < map->n; ++i) {
2981 map->p[i] = drop(map->p[i], type, first, n);
2982 if (!map->p[i])
2983 return isl_map_free(map);
2986 if (map->n > 1)
2987 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2989 return map;
2992 /* Drop all constraints in map that involve any of the dimensions
2993 * first to first + n - 1 of the given type.
2995 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2996 __isl_take isl_map *map,
2997 enum isl_dim_type type, unsigned first, unsigned n)
2999 if (n == 0)
3000 return map;
3001 return drop_constraints(map, type, first, n,
3002 &isl_basic_map_drop_constraints_involving_dims);
3005 /* Drop all constraints in "map" that do not involve any of the dimensions
3006 * first to first + n - 1 of the given type.
3008 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3009 __isl_take isl_map *map,
3010 enum isl_dim_type type, unsigned first, unsigned n)
3012 if (n == 0) {
3013 isl_space *space = isl_map_get_space(map);
3014 isl_map_free(map);
3015 return isl_map_universe(space);
3017 return drop_constraints(map, type, first, n,
3018 &isl_basic_map_drop_constraints_not_involving_dims);
3021 /* Drop all constraints in set that involve any of the dimensions
3022 * first to first + n - 1 of the given type.
3024 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3025 __isl_take isl_set *set,
3026 enum isl_dim_type type, unsigned first, unsigned n)
3028 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3031 /* Drop all constraints in "set" that do not involve any of the dimensions
3032 * first to first + n - 1 of the given type.
3034 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3035 __isl_take isl_set *set,
3036 enum isl_dim_type type, unsigned first, unsigned n)
3038 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3041 /* Does local variable "div" of "bmap" have a complete explicit representation?
3042 * Having a complete explicit representation requires not only
3043 * an explicit representation, but also that all local variables
3044 * that appear in this explicit representation in turn have
3045 * a complete explicit representation.
3047 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3049 int i;
3050 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3051 isl_bool marked;
3053 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3054 if (marked < 0 || marked)
3055 return isl_bool_not(marked);
3057 for (i = bmap->n_div - 1; i >= 0; --i) {
3058 isl_bool known;
3060 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3061 continue;
3062 known = isl_basic_map_div_is_known(bmap, i);
3063 if (known < 0 || !known)
3064 return known;
3067 return isl_bool_true;
3070 /* Remove all divs that are unknown or defined in terms of unknown divs.
3072 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3073 __isl_take isl_basic_map *bmap)
3075 int i;
3077 if (!bmap)
3078 return NULL;
3080 for (i = bmap->n_div - 1; i >= 0; --i) {
3081 if (isl_basic_map_div_is_known(bmap, i))
3082 continue;
3083 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3084 if (!bmap)
3085 return NULL;
3086 i = bmap->n_div;
3089 return bmap;
3092 /* Remove all divs that are unknown or defined in terms of unknown divs.
3094 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3095 __isl_take isl_basic_set *bset)
3097 return isl_basic_map_remove_unknown_divs(bset);
3100 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3102 int i;
3104 if (!map)
3105 return NULL;
3106 if (map->n == 0)
3107 return map;
3109 map = isl_map_cow(map);
3110 if (!map)
3111 return NULL;
3113 for (i = 0; i < map->n; ++i) {
3114 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3115 if (!map->p[i])
3116 goto error;
3118 return map;
3119 error:
3120 isl_map_free(map);
3121 return NULL;
3124 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3126 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3129 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3130 __isl_take isl_basic_set *bset,
3131 enum isl_dim_type type, unsigned first, unsigned n)
3133 isl_basic_map *bmap = bset_to_bmap(bset);
3134 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3135 return bset_from_bmap(bmap);
3138 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3139 enum isl_dim_type type, unsigned first, unsigned n)
3141 int i;
3143 if (n == 0)
3144 return map;
3146 map = isl_map_cow(map);
3147 if (!map)
3148 return NULL;
3149 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3151 for (i = 0; i < map->n; ++i) {
3152 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3153 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3154 if (!map->p[i])
3155 goto error;
3157 map = isl_map_drop(map, type, first, n);
3158 return map;
3159 error:
3160 isl_map_free(map);
3161 return NULL;
3164 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3165 enum isl_dim_type type, unsigned first, unsigned n)
3167 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3168 type, first, n));
3171 /* Project out n inputs starting at first using Fourier-Motzkin */
3172 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3173 unsigned first, unsigned n)
3175 return isl_map_remove_dims(map, isl_dim_in, first, n);
3178 static void dump_term(struct isl_basic_map *bmap,
3179 isl_int c, int pos, FILE *out)
3181 const char *name;
3182 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3183 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3184 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3185 if (!pos)
3186 isl_int_print(out, c, 0);
3187 else {
3188 if (!isl_int_is_one(c))
3189 isl_int_print(out, c, 0);
3190 if (pos < 1 + nparam) {
3191 name = isl_space_get_dim_name(bmap->dim,
3192 isl_dim_param, pos - 1);
3193 if (name)
3194 fprintf(out, "%s", name);
3195 else
3196 fprintf(out, "p%d", pos - 1);
3197 } else if (pos < 1 + nparam + in)
3198 fprintf(out, "i%d", pos - 1 - nparam);
3199 else if (pos < 1 + nparam + dim)
3200 fprintf(out, "o%d", pos - 1 - nparam - in);
3201 else
3202 fprintf(out, "e%d", pos - 1 - nparam - dim);
3206 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3207 int sign, FILE *out)
3209 int i;
3210 int first;
3211 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3212 isl_int v;
3214 isl_int_init(v);
3215 for (i = 0, first = 1; i < len; ++i) {
3216 if (isl_int_sgn(c[i]) * sign <= 0)
3217 continue;
3218 if (!first)
3219 fprintf(out, " + ");
3220 first = 0;
3221 isl_int_abs(v, c[i]);
3222 dump_term(bmap, v, i, out);
3224 isl_int_clear(v);
3225 if (first)
3226 fprintf(out, "0");
3229 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3230 const char *op, FILE *out, int indent)
3232 int i;
3234 fprintf(out, "%*s", indent, "");
3236 dump_constraint_sign(bmap, c, 1, out);
3237 fprintf(out, " %s ", op);
3238 dump_constraint_sign(bmap, c, -1, out);
3240 fprintf(out, "\n");
3242 for (i = bmap->n_div; i < bmap->extra; ++i) {
3243 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3244 continue;
3245 fprintf(out, "%*s", indent, "");
3246 fprintf(out, "ERROR: unused div coefficient not zero\n");
3247 abort();
3251 static void dump_constraints(struct isl_basic_map *bmap,
3252 isl_int **c, unsigned n,
3253 const char *op, FILE *out, int indent)
3255 int i;
3257 for (i = 0; i < n; ++i)
3258 dump_constraint(bmap, c[i], op, out, indent);
3261 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3263 int j;
3264 int first = 1;
3265 unsigned total = isl_basic_map_total_dim(bmap);
3267 for (j = 0; j < 1 + total; ++j) {
3268 if (isl_int_is_zero(exp[j]))
3269 continue;
3270 if (!first && isl_int_is_pos(exp[j]))
3271 fprintf(out, "+");
3272 dump_term(bmap, exp[j], j, out);
3273 first = 0;
3277 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3279 int i;
3281 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3282 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3284 for (i = 0; i < bmap->n_div; ++i) {
3285 fprintf(out, "%*s", indent, "");
3286 fprintf(out, "e%d = [(", i);
3287 dump_affine(bmap, bmap->div[i]+1, out);
3288 fprintf(out, ")/");
3289 isl_int_print(out, bmap->div[i][0], 0);
3290 fprintf(out, "]\n");
3294 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3295 FILE *out, int indent)
3297 if (!bset) {
3298 fprintf(out, "null basic set\n");
3299 return;
3302 fprintf(out, "%*s", indent, "");
3303 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3304 bset->ref, bset->dim->nparam, bset->dim->n_out,
3305 bset->extra, bset->flags);
3306 dump(bset_to_bmap(bset), out, indent);
3309 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3310 FILE *out, int indent)
3312 if (!bmap) {
3313 fprintf(out, "null basic map\n");
3314 return;
3317 fprintf(out, "%*s", indent, "");
3318 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3319 "flags: %x, n_name: %d\n",
3320 bmap->ref,
3321 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3322 bmap->extra, bmap->flags, bmap->dim->n_id);
3323 dump(bmap, out, indent);
3326 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3328 unsigned total;
3329 if (!bmap)
3330 return -1;
3331 total = isl_basic_map_total_dim(bmap);
3332 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3333 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3334 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3335 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3336 return 0;
3339 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3340 unsigned flags)
3342 if (!space)
3343 return NULL;
3344 if (isl_space_dim(space, isl_dim_in) != 0)
3345 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3346 "set cannot have input dimensions", goto error);
3347 return isl_map_alloc_space(space, n, flags);
3348 error:
3349 isl_space_free(space);
3350 return NULL;
3353 /* Make sure "map" has room for at least "n" more basic maps.
3355 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3357 int i;
3358 struct isl_map *grown = NULL;
3360 if (!map)
3361 return NULL;
3362 isl_assert(map->ctx, n >= 0, goto error);
3363 if (map->n + n <= map->size)
3364 return map;
3365 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3366 if (!grown)
3367 goto error;
3368 for (i = 0; i < map->n; ++i) {
3369 grown->p[i] = isl_basic_map_copy(map->p[i]);
3370 if (!grown->p[i])
3371 goto error;
3372 grown->n++;
3374 isl_map_free(map);
3375 return grown;
3376 error:
3377 isl_map_free(grown);
3378 isl_map_free(map);
3379 return NULL;
3382 /* Make sure "set" has room for at least "n" more basic sets.
3384 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3386 return set_from_map(isl_map_grow(set_to_map(set), n));
3389 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3391 return isl_map_from_basic_map(bset);
3394 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3396 struct isl_map *map;
3398 if (!bmap)
3399 return NULL;
3401 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3402 return isl_map_add_basic_map(map, bmap);
3405 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3406 __isl_take isl_basic_set *bset)
3408 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3409 bset_to_bmap(bset)));
3412 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3414 return isl_map_free(set);
3417 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3419 int i;
3421 if (!set) {
3422 fprintf(out, "null set\n");
3423 return;
3426 fprintf(out, "%*s", indent, "");
3427 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3428 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3429 set->flags);
3430 for (i = 0; i < set->n; ++i) {
3431 fprintf(out, "%*s", indent, "");
3432 fprintf(out, "basic set %d:\n", i);
3433 isl_basic_set_print_internal(set->p[i], out, indent+4);
3437 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3439 int i;
3441 if (!map) {
3442 fprintf(out, "null map\n");
3443 return;
3446 fprintf(out, "%*s", indent, "");
3447 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3448 "flags: %x, n_name: %d\n",
3449 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3450 map->dim->n_out, map->flags, map->dim->n_id);
3451 for (i = 0; i < map->n; ++i) {
3452 fprintf(out, "%*s", indent, "");
3453 fprintf(out, "basic map %d:\n", i);
3454 isl_basic_map_print_internal(map->p[i], out, indent+4);
3458 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3459 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3461 struct isl_basic_map *bmap_domain;
3463 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3464 goto error;
3466 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3467 isl_assert(bset->ctx,
3468 isl_basic_map_compatible_domain(bmap, bset), goto error);
3470 bmap = isl_basic_map_cow(bmap);
3471 if (!bmap)
3472 goto error;
3473 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3474 bset->n_div, bset->n_eq, bset->n_ineq);
3475 bmap_domain = isl_basic_map_from_domain(bset);
3476 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3478 bmap = isl_basic_map_simplify(bmap);
3479 return isl_basic_map_finalize(bmap);
3480 error:
3481 isl_basic_map_free(bmap);
3482 isl_basic_set_free(bset);
3483 return NULL;
3486 /* Check that the space of "bset" is the same as that of the range of "bmap".
3488 static isl_stat isl_basic_map_check_compatible_range(
3489 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3491 isl_bool ok;
3493 ok = isl_basic_map_compatible_range(bmap, bset);
3494 if (ok < 0)
3495 return isl_stat_error;
3496 if (!ok)
3497 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3498 "incompatible spaces", return isl_stat_error);
3500 return isl_stat_ok;
3503 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3504 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3506 struct isl_basic_map *bmap_range;
3508 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3509 goto error;
3511 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3512 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3513 goto error;
3515 if (isl_basic_set_plain_is_universe(bset)) {
3516 isl_basic_set_free(bset);
3517 return bmap;
3520 bmap = isl_basic_map_cow(bmap);
3521 if (!bmap)
3522 goto error;
3523 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3524 bset->n_div, bset->n_eq, bset->n_ineq);
3525 bmap_range = bset_to_bmap(bset);
3526 bmap = add_constraints(bmap, bmap_range, 0, 0);
3528 bmap = isl_basic_map_simplify(bmap);
3529 return isl_basic_map_finalize(bmap);
3530 error:
3531 isl_basic_map_free(bmap);
3532 isl_basic_set_free(bset);
3533 return NULL;
3536 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3537 __isl_keep isl_vec *vec)
3539 int i;
3540 unsigned total;
3541 isl_int s;
3543 if (!bmap || !vec)
3544 return isl_bool_error;
3546 total = 1 + isl_basic_map_total_dim(bmap);
3547 if (total != vec->size)
3548 return isl_bool_false;
3550 isl_int_init(s);
3552 for (i = 0; i < bmap->n_eq; ++i) {
3553 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3554 if (!isl_int_is_zero(s)) {
3555 isl_int_clear(s);
3556 return isl_bool_false;
3560 for (i = 0; i < bmap->n_ineq; ++i) {
3561 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3562 if (isl_int_is_neg(s)) {
3563 isl_int_clear(s);
3564 return isl_bool_false;
3568 isl_int_clear(s);
3570 return isl_bool_true;
3573 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3574 __isl_keep isl_vec *vec)
3576 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3579 __isl_give isl_basic_map *isl_basic_map_intersect(
3580 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3582 struct isl_vec *sample = NULL;
3584 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3585 goto error;
3586 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3587 isl_space_dim(bmap1->dim, isl_dim_param) &&
3588 isl_space_dim(bmap2->dim, isl_dim_all) !=
3589 isl_space_dim(bmap2->dim, isl_dim_param))
3590 return isl_basic_map_intersect(bmap2, bmap1);
3592 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3593 isl_space_dim(bmap2->dim, isl_dim_param))
3594 isl_assert(bmap1->ctx,
3595 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3597 if (isl_basic_map_plain_is_empty(bmap1)) {
3598 isl_basic_map_free(bmap2);
3599 return bmap1;
3601 if (isl_basic_map_plain_is_empty(bmap2)) {
3602 isl_basic_map_free(bmap1);
3603 return bmap2;
3606 if (bmap1->sample &&
3607 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3608 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3609 sample = isl_vec_copy(bmap1->sample);
3610 else if (bmap2->sample &&
3611 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3612 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3613 sample = isl_vec_copy(bmap2->sample);
3615 bmap1 = isl_basic_map_cow(bmap1);
3616 if (!bmap1)
3617 goto error;
3618 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3619 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3620 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3622 if (!bmap1)
3623 isl_vec_free(sample);
3624 else if (sample) {
3625 isl_vec_free(bmap1->sample);
3626 bmap1->sample = sample;
3629 bmap1 = isl_basic_map_simplify(bmap1);
3630 return isl_basic_map_finalize(bmap1);
3631 error:
3632 if (sample)
3633 isl_vec_free(sample);
3634 isl_basic_map_free(bmap1);
3635 isl_basic_map_free(bmap2);
3636 return NULL;
3639 struct isl_basic_set *isl_basic_set_intersect(
3640 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3642 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3643 bset_to_bmap(bset2)));
3646 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3647 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3649 return isl_basic_set_intersect(bset1, bset2);
3652 /* Special case of isl_map_intersect, where both map1 and map2
3653 * are convex, without any divs and such that either map1 or map2
3654 * contains a single constraint. This constraint is then simply
3655 * added to the other map.
3657 static __isl_give isl_map *map_intersect_add_constraint(
3658 __isl_take isl_map *map1, __isl_take isl_map *map2)
3660 isl_assert(map1->ctx, map1->n == 1, goto error);
3661 isl_assert(map2->ctx, map1->n == 1, goto error);
3662 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3663 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3665 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3666 return isl_map_intersect(map2, map1);
3668 map1 = isl_map_cow(map1);
3669 if (!map1)
3670 goto error;
3671 if (isl_map_plain_is_empty(map1)) {
3672 isl_map_free(map2);
3673 return map1;
3675 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3676 if (map2->p[0]->n_eq == 1)
3677 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3678 else
3679 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3680 map2->p[0]->ineq[0]);
3682 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3683 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3684 if (!map1->p[0])
3685 goto error;
3687 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3688 isl_basic_map_free(map1->p[0]);
3689 map1->n = 0;
3692 isl_map_free(map2);
3694 return map1;
3695 error:
3696 isl_map_free(map1);
3697 isl_map_free(map2);
3698 return NULL;
3701 /* map2 may be either a parameter domain or a map living in the same
3702 * space as map1.
3704 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3705 __isl_take isl_map *map2)
3707 unsigned flags = 0;
3708 isl_map *result;
3709 int i, j;
3711 if (!map1 || !map2)
3712 goto error;
3714 if ((isl_map_plain_is_empty(map1) ||
3715 isl_map_plain_is_universe(map2)) &&
3716 isl_space_is_equal(map1->dim, map2->dim)) {
3717 isl_map_free(map2);
3718 return map1;
3720 if ((isl_map_plain_is_empty(map2) ||
3721 isl_map_plain_is_universe(map1)) &&
3722 isl_space_is_equal(map1->dim, map2->dim)) {
3723 isl_map_free(map1);
3724 return map2;
3727 if (map1->n == 1 && map2->n == 1 &&
3728 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3729 isl_space_is_equal(map1->dim, map2->dim) &&
3730 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3731 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3732 return map_intersect_add_constraint(map1, map2);
3734 if (isl_space_dim(map2->dim, isl_dim_all) !=
3735 isl_space_dim(map2->dim, isl_dim_param))
3736 isl_assert(map1->ctx,
3737 isl_space_is_equal(map1->dim, map2->dim), goto error);
3739 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3740 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3741 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3743 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3744 map1->n * map2->n, flags);
3745 if (!result)
3746 goto error;
3747 for (i = 0; i < map1->n; ++i)
3748 for (j = 0; j < map2->n; ++j) {
3749 struct isl_basic_map *part;
3750 part = isl_basic_map_intersect(
3751 isl_basic_map_copy(map1->p[i]),
3752 isl_basic_map_copy(map2->p[j]));
3753 if (isl_basic_map_is_empty(part) < 0)
3754 part = isl_basic_map_free(part);
3755 result = isl_map_add_basic_map(result, part);
3756 if (!result)
3757 goto error;
3759 isl_map_free(map1);
3760 isl_map_free(map2);
3761 return result;
3762 error:
3763 isl_map_free(map1);
3764 isl_map_free(map2);
3765 return NULL;
3768 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3769 __isl_take isl_map *map2)
3771 if (!map1 || !map2)
3772 goto error;
3773 if (!isl_space_is_equal(map1->dim, map2->dim))
3774 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3775 "spaces don't match", goto error);
3776 return map_intersect_internal(map1, map2);
3777 error:
3778 isl_map_free(map1);
3779 isl_map_free(map2);
3780 return NULL;
3783 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3784 __isl_take isl_map *map2)
3786 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3789 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3791 return set_from_map(isl_map_intersect(set_to_map(set1),
3792 set_to_map(set2)));
3795 /* map_intersect_internal accepts intersections
3796 * with parameter domains, so we can just call that function.
3798 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3799 __isl_take isl_set *params)
3801 return map_intersect_internal(map, params);
3804 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3805 __isl_take isl_map *map2)
3807 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3810 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3811 __isl_take isl_set *params)
3813 return isl_map_intersect_params(set, params);
3816 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3818 isl_space *space;
3819 unsigned pos, n1, n2;
3821 if (!bmap)
3822 return NULL;
3823 bmap = isl_basic_map_cow(bmap);
3824 if (!bmap)
3825 return NULL;
3826 space = isl_space_reverse(isl_space_copy(bmap->dim));
3827 pos = isl_basic_map_offset(bmap, isl_dim_in);
3828 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3829 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3830 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3831 return isl_basic_map_reset_space(bmap, space);
3834 static __isl_give isl_basic_map *basic_map_space_reset(
3835 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3837 isl_space *space;
3839 if (!bmap)
3840 return NULL;
3841 if (!isl_space_is_named_or_nested(bmap->dim, type))
3842 return bmap;
3844 space = isl_basic_map_get_space(bmap);
3845 space = isl_space_reset(space, type);
3846 bmap = isl_basic_map_reset_space(bmap, space);
3847 return bmap;
3850 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3851 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3852 unsigned pos, unsigned n)
3854 isl_bool rational;
3855 isl_space *res_dim;
3856 struct isl_basic_map *res;
3857 struct isl_dim_map *dim_map;
3858 unsigned total, off;
3859 enum isl_dim_type t;
3861 if (n == 0)
3862 return basic_map_space_reset(bmap, type);
3864 if (!bmap)
3865 return NULL;
3867 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3869 total = isl_basic_map_total_dim(bmap) + n;
3870 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3871 off = 0;
3872 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3873 if (t != type) {
3874 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3875 } else {
3876 unsigned size = isl_basic_map_dim(bmap, t);
3877 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3878 0, pos, off);
3879 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3880 pos, size - pos, off + pos + n);
3882 off += isl_space_dim(res_dim, t);
3884 isl_dim_map_div(dim_map, bmap, off);
3886 res = isl_basic_map_alloc_space(res_dim,
3887 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3888 rational = isl_basic_map_is_rational(bmap);
3889 if (rational < 0)
3890 res = isl_basic_map_free(res);
3891 if (rational)
3892 res = isl_basic_map_set_rational(res);
3893 if (isl_basic_map_plain_is_empty(bmap)) {
3894 isl_basic_map_free(bmap);
3895 free(dim_map);
3896 return isl_basic_map_set_to_empty(res);
3898 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3899 return isl_basic_map_finalize(res);
3902 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3903 __isl_take isl_basic_set *bset,
3904 enum isl_dim_type type, unsigned pos, unsigned n)
3906 return isl_basic_map_insert_dims(bset, type, pos, n);
3909 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3910 enum isl_dim_type type, unsigned n)
3912 if (!bmap)
3913 return NULL;
3914 return isl_basic_map_insert_dims(bmap, type,
3915 isl_basic_map_dim(bmap, type), n);
3918 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3919 enum isl_dim_type type, unsigned n)
3921 if (!bset)
3922 return NULL;
3923 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3924 return isl_basic_map_add_dims(bset, type, n);
3925 error:
3926 isl_basic_set_free(bset);
3927 return NULL;
3930 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3931 enum isl_dim_type type)
3933 isl_space *space;
3935 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3936 return map;
3938 space = isl_map_get_space(map);
3939 space = isl_space_reset(space, type);
3940 map = isl_map_reset_space(map, space);
3941 return map;
3944 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3945 enum isl_dim_type type, unsigned pos, unsigned n)
3947 int i;
3949 if (n == 0)
3950 return map_space_reset(map, type);
3952 map = isl_map_cow(map);
3953 if (!map)
3954 return NULL;
3956 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3957 if (!map->dim)
3958 goto error;
3960 for (i = 0; i < map->n; ++i) {
3961 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3962 if (!map->p[i])
3963 goto error;
3966 return map;
3967 error:
3968 isl_map_free(map);
3969 return NULL;
3972 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3973 enum isl_dim_type type, unsigned pos, unsigned n)
3975 return isl_map_insert_dims(set, type, pos, n);
3978 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3979 enum isl_dim_type type, unsigned n)
3981 if (!map)
3982 return NULL;
3983 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3986 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3987 enum isl_dim_type type, unsigned n)
3989 if (!set)
3990 return NULL;
3991 isl_assert(set->ctx, type != isl_dim_in, goto error);
3992 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3993 error:
3994 isl_set_free(set);
3995 return NULL;
3998 __isl_give isl_basic_map *isl_basic_map_move_dims(
3999 __isl_take isl_basic_map *bmap,
4000 enum isl_dim_type dst_type, unsigned dst_pos,
4001 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4003 struct isl_dim_map *dim_map;
4004 struct isl_basic_map *res;
4005 enum isl_dim_type t;
4006 unsigned total, off;
4008 if (!bmap)
4009 return NULL;
4010 if (n == 0) {
4011 bmap = isl_basic_map_reset(bmap, src_type);
4012 bmap = isl_basic_map_reset(bmap, dst_type);
4013 return bmap;
4016 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4017 return isl_basic_map_free(bmap);
4019 if (dst_type == src_type && dst_pos == src_pos)
4020 return bmap;
4022 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4024 if (pos(bmap->dim, dst_type) + dst_pos ==
4025 pos(bmap->dim, src_type) + src_pos +
4026 ((src_type < dst_type) ? n : 0)) {
4027 bmap = isl_basic_map_cow(bmap);
4028 if (!bmap)
4029 return NULL;
4031 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4032 src_type, src_pos, n);
4033 if (!bmap->dim)
4034 goto error;
4036 bmap = isl_basic_map_finalize(bmap);
4038 return bmap;
4041 total = isl_basic_map_total_dim(bmap);
4042 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4044 off = 0;
4045 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4046 unsigned size = isl_space_dim(bmap->dim, t);
4047 if (t == dst_type) {
4048 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4049 0, dst_pos, off);
4050 off += dst_pos;
4051 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4052 src_pos, n, off);
4053 off += n;
4054 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4055 dst_pos, size - dst_pos, off);
4056 off += size - dst_pos;
4057 } else if (t == src_type) {
4058 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4059 0, src_pos, off);
4060 off += src_pos;
4061 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4062 src_pos + n, size - src_pos - n, off);
4063 off += size - src_pos - n;
4064 } else {
4065 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4066 off += size;
4069 isl_dim_map_div(dim_map, bmap, off);
4071 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4072 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4073 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4074 if (!bmap)
4075 goto error;
4077 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4078 src_type, src_pos, n);
4079 if (!bmap->dim)
4080 goto error;
4082 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
4083 bmap = isl_basic_map_gauss(bmap, NULL);
4084 bmap = isl_basic_map_finalize(bmap);
4086 return bmap;
4087 error:
4088 isl_basic_map_free(bmap);
4089 return NULL;
4092 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4093 enum isl_dim_type dst_type, unsigned dst_pos,
4094 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4096 isl_basic_map *bmap = bset_to_bmap(bset);
4097 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4098 src_type, src_pos, n);
4099 return bset_from_bmap(bmap);
4102 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4103 enum isl_dim_type dst_type, unsigned dst_pos,
4104 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4106 if (!set)
4107 return NULL;
4108 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4109 return set_from_map(isl_map_move_dims(set_to_map(set),
4110 dst_type, dst_pos, src_type, src_pos, n));
4111 error:
4112 isl_set_free(set);
4113 return NULL;
4116 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4117 enum isl_dim_type dst_type, unsigned dst_pos,
4118 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4120 int i;
4122 if (!map)
4123 return NULL;
4124 if (n == 0) {
4125 map = isl_map_reset(map, src_type);
4126 map = isl_map_reset(map, dst_type);
4127 return map;
4130 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
4131 goto error);
4133 if (dst_type == src_type && dst_pos == src_pos)
4134 return map;
4136 isl_assert(map->ctx, dst_type != src_type, goto error);
4138 map = isl_map_cow(map);
4139 if (!map)
4140 return NULL;
4142 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4143 if (!map->dim)
4144 goto error;
4146 for (i = 0; i < map->n; ++i) {
4147 map->p[i] = isl_basic_map_move_dims(map->p[i],
4148 dst_type, dst_pos,
4149 src_type, src_pos, n);
4150 if (!map->p[i])
4151 goto error;
4154 return map;
4155 error:
4156 isl_map_free(map);
4157 return NULL;
4160 /* Move the specified dimensions to the last columns right before
4161 * the divs. Don't change the dimension specification of bmap.
4162 * That's the responsibility of the caller.
4164 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4165 enum isl_dim_type type, unsigned first, unsigned n)
4167 struct isl_dim_map *dim_map;
4168 struct isl_basic_map *res;
4169 enum isl_dim_type t;
4170 unsigned total, off;
4172 if (!bmap)
4173 return NULL;
4174 if (pos(bmap->dim, type) + first + n ==
4175 1 + isl_space_dim(bmap->dim, isl_dim_all))
4176 return bmap;
4178 total = isl_basic_map_total_dim(bmap);
4179 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4181 off = 0;
4182 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4183 unsigned size = isl_space_dim(bmap->dim, t);
4184 if (t == type) {
4185 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4186 0, first, off);
4187 off += first;
4188 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4189 first, n, total - bmap->n_div - n);
4190 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4191 first + n, size - (first + n), off);
4192 off += size - (first + n);
4193 } else {
4194 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4195 off += size;
4198 isl_dim_map_div(dim_map, bmap, off + n);
4200 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4201 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4202 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4203 return res;
4206 /* Insert "n" rows in the divs of "bmap".
4208 * The number of columns is not changed, which means that the last
4209 * dimensions of "bmap" are being reintepreted as the new divs.
4210 * The space of "bmap" is not adjusted, however, which means
4211 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4212 * from the space of "bmap" is the responsibility of the caller.
4214 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4215 int n)
4217 int i;
4218 size_t row_size;
4219 isl_int **new_div;
4220 isl_int *old;
4222 bmap = isl_basic_map_cow(bmap);
4223 if (!bmap)
4224 return NULL;
4226 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4227 old = bmap->block2.data;
4228 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4229 (bmap->extra + n) * (1 + row_size));
4230 if (!bmap->block2.data)
4231 return isl_basic_map_free(bmap);
4232 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4233 if (!new_div)
4234 return isl_basic_map_free(bmap);
4235 for (i = 0; i < n; ++i) {
4236 new_div[i] = bmap->block2.data +
4237 (bmap->extra + i) * (1 + row_size);
4238 isl_seq_clr(new_div[i], 1 + row_size);
4240 for (i = 0; i < bmap->extra; ++i)
4241 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4242 free(bmap->div);
4243 bmap->div = new_div;
4244 bmap->n_div += n;
4245 bmap->extra += n;
4247 return bmap;
4250 /* Drop constraints from "bmap" that only involve the variables
4251 * of "type" in the range [first, first + n] that are not related
4252 * to any of the variables outside that interval.
4253 * These constraints cannot influence the values for the variables
4254 * outside the interval, except in case they cause "bmap" to be empty.
4255 * Only drop the constraints if "bmap" is known to be non-empty.
4257 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4258 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4259 unsigned first, unsigned n)
4261 int i;
4262 int *groups;
4263 unsigned dim, n_div;
4264 isl_bool non_empty;
4266 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4267 if (non_empty < 0)
4268 return isl_basic_map_free(bmap);
4269 if (!non_empty)
4270 return bmap;
4272 dim = isl_basic_map_dim(bmap, isl_dim_all);
4273 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4274 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4275 if (!groups)
4276 return isl_basic_map_free(bmap);
4277 first += isl_basic_map_offset(bmap, type) - 1;
4278 for (i = 0; i < first; ++i)
4279 groups[i] = -1;
4280 for (i = first + n; i < dim - n_div; ++i)
4281 groups[i] = -1;
4283 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4285 return bmap;
4288 /* Turn the n dimensions of type type, starting at first
4289 * into existentially quantified variables.
4291 * If a subset of the projected out variables are unrelated
4292 * to any of the variables that remain, then the constraints
4293 * involving this subset are simply dropped first.
4295 __isl_give isl_basic_map *isl_basic_map_project_out(
4296 __isl_take isl_basic_map *bmap,
4297 enum isl_dim_type type, unsigned first, unsigned n)
4299 isl_bool empty;
4301 if (n == 0)
4302 return basic_map_space_reset(bmap, type);
4303 if (type == isl_dim_div)
4304 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4305 "cannot project out existentially quantified variables",
4306 return isl_basic_map_free(bmap));
4308 empty = isl_basic_map_plain_is_empty(bmap);
4309 if (empty < 0)
4310 return isl_basic_map_free(bmap);
4311 if (empty)
4312 bmap = isl_basic_map_set_to_empty(bmap);
4314 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4315 if (!bmap)
4316 return NULL;
4318 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4319 return isl_basic_map_remove_dims(bmap, type, first, n);
4321 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4322 return isl_basic_map_free(bmap);
4324 bmap = move_last(bmap, type, first, n);
4325 bmap = isl_basic_map_cow(bmap);
4326 bmap = insert_div_rows(bmap, n);
4327 if (!bmap)
4328 return NULL;
4330 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4331 if (!bmap->dim)
4332 goto error;
4333 bmap = isl_basic_map_simplify(bmap);
4334 bmap = isl_basic_map_drop_redundant_divs(bmap);
4335 return isl_basic_map_finalize(bmap);
4336 error:
4337 isl_basic_map_free(bmap);
4338 return NULL;
4341 /* Turn the n dimensions of type type, starting at first
4342 * into existentially quantified variables.
4344 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4345 enum isl_dim_type type, unsigned first, unsigned n)
4347 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4348 type, first, n));
4351 /* Turn the n dimensions of type type, starting at first
4352 * into existentially quantified variables.
4354 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4355 enum isl_dim_type type, unsigned first, unsigned n)
4357 int i;
4359 if (!map)
4360 return NULL;
4362 if (n == 0)
4363 return map_space_reset(map, type);
4365 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
4367 map = isl_map_cow(map);
4368 if (!map)
4369 return NULL;
4371 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4372 if (!map->dim)
4373 goto error;
4375 for (i = 0; i < map->n; ++i) {
4376 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4377 if (!map->p[i])
4378 goto error;
4381 return map;
4382 error:
4383 isl_map_free(map);
4384 return NULL;
4387 /* Turn the n dimensions of type type, starting at first
4388 * into existentially quantified variables.
4390 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4391 enum isl_dim_type type, unsigned first, unsigned n)
4393 return set_from_map(isl_map_project_out(set_to_map(set),
4394 type, first, n));
4397 /* Return a map that projects the elements in "set" onto their
4398 * "n" set dimensions starting at "first".
4399 * "type" should be equal to isl_dim_set.
4401 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4402 enum isl_dim_type type, unsigned first, unsigned n)
4404 int i;
4405 int dim;
4406 isl_map *map;
4408 if (!set)
4409 return NULL;
4410 if (type != isl_dim_set)
4411 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4412 "only set dimensions can be projected out", goto error);
4413 dim = isl_set_dim(set, isl_dim_set);
4414 if (first + n > dim || first + n < first)
4415 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4416 "index out of bounds", goto error);
4418 map = isl_map_from_domain(set);
4419 map = isl_map_add_dims(map, isl_dim_out, n);
4420 for (i = 0; i < n; ++i)
4421 map = isl_map_equate(map, isl_dim_in, first + i,
4422 isl_dim_out, i);
4423 return map;
4424 error:
4425 isl_set_free(set);
4426 return NULL;
4429 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4431 int i, j;
4433 for (i = 0; i < n; ++i) {
4434 j = isl_basic_map_alloc_div(bmap);
4435 if (j < 0)
4436 goto error;
4437 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4439 return bmap;
4440 error:
4441 isl_basic_map_free(bmap);
4442 return NULL;
4445 struct isl_basic_map *isl_basic_map_apply_range(
4446 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4448 isl_space *dim_result = NULL;
4449 struct isl_basic_map *bmap;
4450 unsigned n_in, n_out, n, nparam, total, pos;
4451 struct isl_dim_map *dim_map1, *dim_map2;
4453 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4454 goto error;
4455 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4456 bmap2->dim, isl_dim_in))
4457 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4458 "spaces don't match", goto error);
4460 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4461 isl_space_copy(bmap2->dim));
4463 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4464 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4465 n = isl_basic_map_dim(bmap1, isl_dim_out);
4466 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4468 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4469 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4470 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4471 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4472 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4473 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4474 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4475 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4476 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4477 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4478 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4480 bmap = isl_basic_map_alloc_space(dim_result,
4481 bmap1->n_div + bmap2->n_div + n,
4482 bmap1->n_eq + bmap2->n_eq,
4483 bmap1->n_ineq + bmap2->n_ineq);
4484 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4485 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4486 bmap = add_divs(bmap, n);
4487 bmap = isl_basic_map_simplify(bmap);
4488 bmap = isl_basic_map_drop_redundant_divs(bmap);
4489 return isl_basic_map_finalize(bmap);
4490 error:
4491 isl_basic_map_free(bmap1);
4492 isl_basic_map_free(bmap2);
4493 return NULL;
4496 struct isl_basic_set *isl_basic_set_apply(
4497 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4499 if (!bset || !bmap)
4500 goto error;
4502 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4503 goto error);
4505 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4506 bmap));
4507 error:
4508 isl_basic_set_free(bset);
4509 isl_basic_map_free(bmap);
4510 return NULL;
4513 struct isl_basic_map *isl_basic_map_apply_domain(
4514 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4516 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4517 goto error;
4518 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4519 bmap2->dim, isl_dim_in))
4520 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4521 "spaces don't match", goto error);
4523 bmap1 = isl_basic_map_reverse(bmap1);
4524 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4525 return isl_basic_map_reverse(bmap1);
4526 error:
4527 isl_basic_map_free(bmap1);
4528 isl_basic_map_free(bmap2);
4529 return NULL;
4532 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4533 * A \cap B -> f(A) + f(B)
4535 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4536 __isl_take isl_basic_map *bmap2)
4538 unsigned n_in, n_out, nparam, total, pos;
4539 struct isl_basic_map *bmap = NULL;
4540 struct isl_dim_map *dim_map1, *dim_map2;
4541 int i;
4543 if (!bmap1 || !bmap2)
4544 goto error;
4546 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4547 goto error);
4549 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4550 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4551 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4553 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4554 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4555 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4556 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4557 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4558 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4559 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4560 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4561 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4562 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4563 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4565 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4566 bmap1->n_div + bmap2->n_div + 2 * n_out,
4567 bmap1->n_eq + bmap2->n_eq + n_out,
4568 bmap1->n_ineq + bmap2->n_ineq);
4569 for (i = 0; i < n_out; ++i) {
4570 int j = isl_basic_map_alloc_equality(bmap);
4571 if (j < 0)
4572 goto error;
4573 isl_seq_clr(bmap->eq[j], 1+total);
4574 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4575 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4576 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4578 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4579 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4580 bmap = add_divs(bmap, 2 * n_out);
4582 bmap = isl_basic_map_simplify(bmap);
4583 return isl_basic_map_finalize(bmap);
4584 error:
4585 isl_basic_map_free(bmap);
4586 isl_basic_map_free(bmap1);
4587 isl_basic_map_free(bmap2);
4588 return NULL;
4591 /* Given two maps A -> f(A) and B -> g(B), construct a map
4592 * A \cap B -> f(A) + f(B)
4594 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4595 __isl_take isl_map *map2)
4597 struct isl_map *result;
4598 int i, j;
4600 if (!map1 || !map2)
4601 goto error;
4603 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4605 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4606 map1->n * map2->n, 0);
4607 if (!result)
4608 goto error;
4609 for (i = 0; i < map1->n; ++i)
4610 for (j = 0; j < map2->n; ++j) {
4611 struct isl_basic_map *part;
4612 part = isl_basic_map_sum(
4613 isl_basic_map_copy(map1->p[i]),
4614 isl_basic_map_copy(map2->p[j]));
4615 if (isl_basic_map_is_empty(part))
4616 isl_basic_map_free(part);
4617 else
4618 result = isl_map_add_basic_map(result, part);
4619 if (!result)
4620 goto error;
4622 isl_map_free(map1);
4623 isl_map_free(map2);
4624 return result;
4625 error:
4626 isl_map_free(map1);
4627 isl_map_free(map2);
4628 return NULL;
4631 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4632 __isl_take isl_set *set2)
4634 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4637 /* Given a basic map A -> f(A), construct A -> -f(A).
4639 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4641 int i, j;
4642 unsigned off, n;
4644 bmap = isl_basic_map_cow(bmap);
4645 if (!bmap)
4646 return NULL;
4648 n = isl_basic_map_dim(bmap, isl_dim_out);
4649 off = isl_basic_map_offset(bmap, isl_dim_out);
4650 for (i = 0; i < bmap->n_eq; ++i)
4651 for (j = 0; j < n; ++j)
4652 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4653 for (i = 0; i < bmap->n_ineq; ++i)
4654 for (j = 0; j < n; ++j)
4655 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4656 for (i = 0; i < bmap->n_div; ++i)
4657 for (j = 0; j < n; ++j)
4658 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4659 bmap = isl_basic_map_gauss(bmap, NULL);
4660 return isl_basic_map_finalize(bmap);
4663 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4665 return isl_basic_map_neg(bset);
4668 /* Given a map A -> f(A), construct A -> -f(A).
4670 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4672 int i;
4674 map = isl_map_cow(map);
4675 if (!map)
4676 return NULL;
4678 for (i = 0; i < map->n; ++i) {
4679 map->p[i] = isl_basic_map_neg(map->p[i]);
4680 if (!map->p[i])
4681 goto error;
4684 return map;
4685 error:
4686 isl_map_free(map);
4687 return NULL;
4690 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4692 return set_from_map(isl_map_neg(set_to_map(set)));
4695 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4696 * A -> floor(f(A)/d).
4698 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4699 isl_int d)
4701 unsigned n_in, n_out, nparam, total, pos;
4702 struct isl_basic_map *result = NULL;
4703 struct isl_dim_map *dim_map;
4704 int i;
4706 if (!bmap)
4707 return NULL;
4709 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4710 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4711 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4713 total = nparam + n_in + n_out + bmap->n_div + n_out;
4714 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4715 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4716 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4717 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4718 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4720 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4721 bmap->n_div + n_out,
4722 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4723 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4724 result = add_divs(result, n_out);
4725 for (i = 0; i < n_out; ++i) {
4726 int j;
4727 j = isl_basic_map_alloc_inequality(result);
4728 if (j < 0)
4729 goto error;
4730 isl_seq_clr(result->ineq[j], 1+total);
4731 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4732 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4733 j = isl_basic_map_alloc_inequality(result);
4734 if (j < 0)
4735 goto error;
4736 isl_seq_clr(result->ineq[j], 1+total);
4737 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4738 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4739 isl_int_sub_ui(result->ineq[j][0], d, 1);
4742 result = isl_basic_map_simplify(result);
4743 return isl_basic_map_finalize(result);
4744 error:
4745 isl_basic_map_free(result);
4746 return NULL;
4749 /* Given a map A -> f(A) and an integer d, construct a map
4750 * A -> floor(f(A)/d).
4752 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4754 int i;
4756 map = isl_map_cow(map);
4757 if (!map)
4758 return NULL;
4760 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4761 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4762 for (i = 0; i < map->n; ++i) {
4763 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4764 if (!map->p[i])
4765 goto error;
4768 return map;
4769 error:
4770 isl_map_free(map);
4771 return NULL;
4774 /* Given a map A -> f(A) and an integer d, construct a map
4775 * A -> floor(f(A)/d).
4777 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4778 __isl_take isl_val *d)
4780 if (!map || !d)
4781 goto error;
4782 if (!isl_val_is_int(d))
4783 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4784 "expecting integer denominator", goto error);
4785 map = isl_map_floordiv(map, d->n);
4786 isl_val_free(d);
4787 return map;
4788 error:
4789 isl_map_free(map);
4790 isl_val_free(d);
4791 return NULL;
4794 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4795 unsigned pos)
4797 int i;
4798 unsigned nparam;
4799 unsigned n_in;
4801 i = isl_basic_map_alloc_equality(bmap);
4802 if (i < 0)
4803 goto error;
4804 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4805 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4806 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4807 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4808 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4809 return isl_basic_map_finalize(bmap);
4810 error:
4811 isl_basic_map_free(bmap);
4812 return NULL;
4815 /* Add a constraint to "bmap" expressing i_pos < o_pos
4817 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4818 unsigned pos)
4820 int i;
4821 unsigned nparam;
4822 unsigned n_in;
4824 i = isl_basic_map_alloc_inequality(bmap);
4825 if (i < 0)
4826 goto error;
4827 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4828 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4829 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4830 isl_int_set_si(bmap->ineq[i][0], -1);
4831 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4832 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4833 return isl_basic_map_finalize(bmap);
4834 error:
4835 isl_basic_map_free(bmap);
4836 return NULL;
4839 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4841 static __isl_give isl_basic_map *var_less_or_equal(
4842 __isl_take isl_basic_map *bmap, unsigned pos)
4844 int i;
4845 unsigned nparam;
4846 unsigned n_in;
4848 i = isl_basic_map_alloc_inequality(bmap);
4849 if (i < 0)
4850 goto error;
4851 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4852 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4853 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4854 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4855 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4856 return isl_basic_map_finalize(bmap);
4857 error:
4858 isl_basic_map_free(bmap);
4859 return NULL;
4862 /* Add a constraint to "bmap" expressing i_pos > o_pos
4864 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4865 unsigned pos)
4867 int i;
4868 unsigned nparam;
4869 unsigned n_in;
4871 i = isl_basic_map_alloc_inequality(bmap);
4872 if (i < 0)
4873 goto error;
4874 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4875 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4876 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4877 isl_int_set_si(bmap->ineq[i][0], -1);
4878 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4879 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4880 return isl_basic_map_finalize(bmap);
4881 error:
4882 isl_basic_map_free(bmap);
4883 return NULL;
4886 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4888 static __isl_give isl_basic_map *var_more_or_equal(
4889 __isl_take isl_basic_map *bmap, unsigned pos)
4891 int i;
4892 unsigned nparam;
4893 unsigned n_in;
4895 i = isl_basic_map_alloc_inequality(bmap);
4896 if (i < 0)
4897 goto error;
4898 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4899 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4900 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4901 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4902 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4903 return isl_basic_map_finalize(bmap);
4904 error:
4905 isl_basic_map_free(bmap);
4906 return NULL;
4909 __isl_give isl_basic_map *isl_basic_map_equal(
4910 __isl_take isl_space *dim, unsigned n_equal)
4912 int i;
4913 struct isl_basic_map *bmap;
4914 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4915 if (!bmap)
4916 return NULL;
4917 for (i = 0; i < n_equal && bmap; ++i)
4918 bmap = var_equal(bmap, i);
4919 return isl_basic_map_finalize(bmap);
4922 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4924 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4925 unsigned pos)
4927 int i;
4928 struct isl_basic_map *bmap;
4929 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4930 if (!bmap)
4931 return NULL;
4932 for (i = 0; i < pos && bmap; ++i)
4933 bmap = var_equal(bmap, i);
4934 if (bmap)
4935 bmap = var_less(bmap, pos);
4936 return isl_basic_map_finalize(bmap);
4939 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4941 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4942 __isl_take isl_space *dim, unsigned pos)
4944 int i;
4945 isl_basic_map *bmap;
4947 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4948 for (i = 0; i < pos; ++i)
4949 bmap = var_equal(bmap, i);
4950 bmap = var_less_or_equal(bmap, pos);
4951 return isl_basic_map_finalize(bmap);
4954 /* Return a relation on "dim" expressing i_pos > o_pos
4956 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4957 unsigned pos)
4959 int i;
4960 struct isl_basic_map *bmap;
4961 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4962 if (!bmap)
4963 return NULL;
4964 for (i = 0; i < pos && bmap; ++i)
4965 bmap = var_equal(bmap, i);
4966 if (bmap)
4967 bmap = var_more(bmap, pos);
4968 return isl_basic_map_finalize(bmap);
4971 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4973 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4974 __isl_take isl_space *dim, unsigned pos)
4976 int i;
4977 isl_basic_map *bmap;
4979 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4980 for (i = 0; i < pos; ++i)
4981 bmap = var_equal(bmap, i);
4982 bmap = var_more_or_equal(bmap, pos);
4983 return isl_basic_map_finalize(bmap);
4986 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4987 unsigned n, int equal)
4989 struct isl_map *map;
4990 int i;
4992 if (n == 0 && equal)
4993 return isl_map_universe(dims);
4995 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4997 for (i = 0; i + 1 < n; ++i)
4998 map = isl_map_add_basic_map(map,
4999 isl_basic_map_less_at(isl_space_copy(dims), i));
5000 if (n > 0) {
5001 if (equal)
5002 map = isl_map_add_basic_map(map,
5003 isl_basic_map_less_or_equal_at(dims, n - 1));
5004 else
5005 map = isl_map_add_basic_map(map,
5006 isl_basic_map_less_at(dims, n - 1));
5007 } else
5008 isl_space_free(dims);
5010 return map;
5013 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
5015 if (!dims)
5016 return NULL;
5017 return map_lex_lte_first(dims, dims->n_out, equal);
5020 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5022 return map_lex_lte_first(dim, n, 0);
5025 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5027 return map_lex_lte_first(dim, n, 1);
5030 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5032 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5035 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5037 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5040 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
5041 unsigned n, int equal)
5043 struct isl_map *map;
5044 int i;
5046 if (n == 0 && equal)
5047 return isl_map_universe(dims);
5049 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
5051 for (i = 0; i + 1 < n; ++i)
5052 map = isl_map_add_basic_map(map,
5053 isl_basic_map_more_at(isl_space_copy(dims), i));
5054 if (n > 0) {
5055 if (equal)
5056 map = isl_map_add_basic_map(map,
5057 isl_basic_map_more_or_equal_at(dims, n - 1));
5058 else
5059 map = isl_map_add_basic_map(map,
5060 isl_basic_map_more_at(dims, n - 1));
5061 } else
5062 isl_space_free(dims);
5064 return map;
5067 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
5069 if (!dims)
5070 return NULL;
5071 return map_lex_gte_first(dims, dims->n_out, equal);
5074 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5076 return map_lex_gte_first(dim, n, 0);
5079 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5081 return map_lex_gte_first(dim, n, 1);
5084 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5086 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5089 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5091 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5094 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5095 __isl_take isl_set *set2)
5097 isl_map *map;
5098 map = isl_map_lex_le(isl_set_get_space(set1));
5099 map = isl_map_intersect_domain(map, set1);
5100 map = isl_map_intersect_range(map, set2);
5101 return map;
5104 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5105 __isl_take isl_set *set2)
5107 isl_map *map;
5108 map = isl_map_lex_lt(isl_set_get_space(set1));
5109 map = isl_map_intersect_domain(map, set1);
5110 map = isl_map_intersect_range(map, set2);
5111 return map;
5114 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5115 __isl_take isl_set *set2)
5117 isl_map *map;
5118 map = isl_map_lex_ge(isl_set_get_space(set1));
5119 map = isl_map_intersect_domain(map, set1);
5120 map = isl_map_intersect_range(map, set2);
5121 return map;
5124 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5125 __isl_take isl_set *set2)
5127 isl_map *map;
5128 map = isl_map_lex_gt(isl_set_get_space(set1));
5129 map = isl_map_intersect_domain(map, set1);
5130 map = isl_map_intersect_range(map, set2);
5131 return map;
5134 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5135 __isl_take isl_map *map2)
5137 isl_map *map;
5138 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5139 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5140 map = isl_map_apply_range(map, isl_map_reverse(map2));
5141 return map;
5144 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5145 __isl_take isl_map *map2)
5147 isl_map *map;
5148 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5149 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5150 map = isl_map_apply_range(map, isl_map_reverse(map2));
5151 return map;
5154 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5155 __isl_take isl_map *map2)
5157 isl_map *map;
5158 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5159 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5160 map = isl_map_apply_range(map, isl_map_reverse(map2));
5161 return map;
5164 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5165 __isl_take isl_map *map2)
5167 isl_map *map;
5168 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5169 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5170 map = isl_map_apply_range(map, isl_map_reverse(map2));
5171 return map;
5174 /* For a div d = floor(f/m), add the constraint
5176 * f - m d >= 0
5178 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5179 unsigned pos, isl_int *div)
5181 int i;
5182 unsigned total = isl_basic_map_total_dim(bmap);
5184 i = isl_basic_map_alloc_inequality(bmap);
5185 if (i < 0)
5186 return isl_stat_error;
5187 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5188 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5190 return isl_stat_ok;
5193 /* For a div d = floor(f/m), add the constraint
5195 * -(f-(m-1)) + m d >= 0
5197 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5198 unsigned pos, isl_int *div)
5200 int i;
5201 unsigned total = isl_basic_map_total_dim(bmap);
5203 i = isl_basic_map_alloc_inequality(bmap);
5204 if (i < 0)
5205 return isl_stat_error;
5206 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5207 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5208 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5209 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5211 return isl_stat_ok;
5214 /* For a div d = floor(f/m), add the constraints
5216 * f - m d >= 0
5217 * -(f-(m-1)) + m d >= 0
5219 * Note that the second constraint is the negation of
5221 * f - m d >= m
5223 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5224 unsigned pos, isl_int *div)
5226 if (add_upper_div_constraint(bmap, pos, div) < 0)
5227 return -1;
5228 if (add_lower_div_constraint(bmap, pos, div) < 0)
5229 return -1;
5230 return 0;
5233 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5234 unsigned pos, isl_int *div)
5236 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5237 pos, div);
5240 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
5242 unsigned total = isl_basic_map_total_dim(bmap);
5243 unsigned div_pos = total - bmap->n_div + div;
5245 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5246 bmap->div[div]);
5249 /* For each known div d = floor(f/m), add the constraints
5251 * f - m d >= 0
5252 * -(f-(m-1)) + m d >= 0
5254 * Remove duplicate constraints in case of some these div constraints
5255 * already appear in "bmap".
5257 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5258 __isl_take isl_basic_map *bmap)
5260 unsigned n_div;
5262 if (!bmap)
5263 return NULL;
5264 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5265 if (n_div == 0)
5266 return bmap;
5268 bmap = add_known_div_constraints(bmap);
5269 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5270 bmap = isl_basic_map_finalize(bmap);
5271 return bmap;
5274 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5276 * In particular, if this div is of the form d = floor(f/m),
5277 * then add the constraint
5279 * f - m d >= 0
5281 * if sign < 0 or the constraint
5283 * -(f-(m-1)) + m d >= 0
5285 * if sign > 0.
5287 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5288 unsigned div, int sign)
5290 unsigned total;
5291 unsigned div_pos;
5293 if (!bmap)
5294 return -1;
5296 total = isl_basic_map_total_dim(bmap);
5297 div_pos = total - bmap->n_div + div;
5299 if (sign < 0)
5300 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5301 else
5302 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5305 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5306 __isl_take isl_basic_map *bmap)
5308 if (!bmap)
5309 goto error;
5310 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5311 bmap->n_div == 0 &&
5312 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5313 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5314 return bset_from_bmap(bmap);
5315 bmap = isl_basic_map_cow(bmap);
5316 if (!bmap)
5317 goto error;
5318 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5319 if (!bmap->dim)
5320 goto error;
5321 bmap->extra -= bmap->n_div;
5322 bmap->n_div = 0;
5323 bmap = isl_basic_map_finalize(bmap);
5324 return bset_from_bmap(bmap);
5325 error:
5326 isl_basic_map_free(bmap);
5327 return NULL;
5330 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5331 __isl_take isl_basic_set *bset)
5333 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5336 /* Replace each element in "list" by the result of applying
5337 * isl_basic_map_underlying_set to the element.
5339 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5340 __isl_take isl_basic_map_list *list)
5342 int i, n;
5344 if (!list)
5345 return NULL;
5347 n = isl_basic_map_list_n_basic_map(list);
5348 for (i = 0; i < n; ++i) {
5349 isl_basic_map *bmap;
5350 isl_basic_set *bset;
5352 bmap = isl_basic_map_list_get_basic_map(list, i);
5353 bset = isl_basic_set_underlying_set(bmap);
5354 list = isl_basic_set_list_set_basic_set(list, i, bset);
5357 return list;
5360 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5361 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5363 struct isl_basic_map *bmap;
5364 struct isl_ctx *ctx;
5365 unsigned total;
5366 int i;
5368 if (!bset || !like)
5369 goto error;
5370 ctx = bset->ctx;
5371 isl_assert(ctx, bset->n_div == 0, goto error);
5372 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5373 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5374 goto error);
5375 if (like->n_div == 0) {
5376 isl_space *space = isl_basic_map_get_space(like);
5377 isl_basic_map_free(like);
5378 return isl_basic_map_reset_space(bset, space);
5380 bset = isl_basic_set_cow(bset);
5381 if (!bset)
5382 goto error;
5383 total = bset->dim->n_out + bset->extra;
5384 bmap = bset_to_bmap(bset);
5385 isl_space_free(bmap->dim);
5386 bmap->dim = isl_space_copy(like->dim);
5387 if (!bmap->dim)
5388 goto error;
5389 bmap->n_div = like->n_div;
5390 bmap->extra += like->n_div;
5391 if (bmap->extra) {
5392 unsigned ltotal;
5393 isl_int **div;
5394 ltotal = total - bmap->extra + like->extra;
5395 if (ltotal > total)
5396 ltotal = total;
5397 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5398 bmap->extra * (1 + 1 + total));
5399 if (isl_blk_is_error(bmap->block2))
5400 goto error;
5401 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5402 if (!div)
5403 goto error;
5404 bmap->div = div;
5405 for (i = 0; i < bmap->extra; ++i)
5406 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5407 for (i = 0; i < like->n_div; ++i) {
5408 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5409 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5411 bmap = isl_basic_map_add_known_div_constraints(bmap);
5413 isl_basic_map_free(like);
5414 bmap = isl_basic_map_simplify(bmap);
5415 bmap = isl_basic_map_finalize(bmap);
5416 return bmap;
5417 error:
5418 isl_basic_map_free(like);
5419 isl_basic_set_free(bset);
5420 return NULL;
5423 struct isl_basic_set *isl_basic_set_from_underlying_set(
5424 struct isl_basic_set *bset, struct isl_basic_set *like)
5426 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5427 bset_to_bmap(like)));
5430 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5432 int i;
5434 map = isl_map_cow(map);
5435 if (!map)
5436 return NULL;
5437 map->dim = isl_space_cow(map->dim);
5438 if (!map->dim)
5439 goto error;
5441 for (i = 1; i < map->n; ++i)
5442 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5443 goto error);
5444 for (i = 0; i < map->n; ++i) {
5445 map->p[i] = bset_to_bmap(
5446 isl_basic_map_underlying_set(map->p[i]));
5447 if (!map->p[i])
5448 goto error;
5450 if (map->n == 0)
5451 map->dim = isl_space_underlying(map->dim, 0);
5452 else {
5453 isl_space_free(map->dim);
5454 map->dim = isl_space_copy(map->p[0]->dim);
5456 if (!map->dim)
5457 goto error;
5458 return set_from_map(map);
5459 error:
5460 isl_map_free(map);
5461 return NULL;
5464 /* Replace the space of "bmap" by "space".
5466 * If the space of "bmap" is identical to "space" (including the identifiers
5467 * of the input and output dimensions), then simply return the original input.
5469 __isl_give isl_basic_map *isl_basic_map_reset_space(
5470 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5472 isl_bool equal;
5473 isl_space *bmap_space;
5475 bmap_space = isl_basic_map_peek_space(bmap);
5476 equal = isl_space_is_equal(bmap_space, space);
5477 if (equal >= 0 && equal)
5478 equal = isl_space_has_equal_ids(bmap_space, space);
5479 if (equal < 0)
5480 goto error;
5481 if (equal) {
5482 isl_space_free(space);
5483 return bmap;
5485 bmap = isl_basic_map_cow(bmap);
5486 if (!bmap || !space)
5487 goto error;
5489 isl_space_free(bmap->dim);
5490 bmap->dim = space;
5492 bmap = isl_basic_map_finalize(bmap);
5494 return bmap;
5495 error:
5496 isl_basic_map_free(bmap);
5497 isl_space_free(space);
5498 return NULL;
5501 __isl_give isl_basic_set *isl_basic_set_reset_space(
5502 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5504 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5505 dim));
5508 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5509 __isl_take isl_space *dim)
5511 int i;
5513 map = isl_map_cow(map);
5514 if (!map || !dim)
5515 goto error;
5517 for (i = 0; i < map->n; ++i) {
5518 map->p[i] = isl_basic_map_reset_space(map->p[i],
5519 isl_space_copy(dim));
5520 if (!map->p[i])
5521 goto error;
5523 isl_space_free(map->dim);
5524 map->dim = dim;
5526 return map;
5527 error:
5528 isl_map_free(map);
5529 isl_space_free(dim);
5530 return NULL;
5533 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5534 __isl_take isl_space *dim)
5536 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5539 /* Compute the parameter domain of the given basic set.
5541 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5543 isl_bool is_params;
5544 isl_space *space;
5545 unsigned n;
5547 is_params = isl_basic_set_is_params(bset);
5548 if (is_params < 0)
5549 return isl_basic_set_free(bset);
5550 if (is_params)
5551 return bset;
5553 n = isl_basic_set_dim(bset, isl_dim_set);
5554 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5555 space = isl_basic_set_get_space(bset);
5556 space = isl_space_params(space);
5557 bset = isl_basic_set_reset_space(bset, space);
5558 return bset;
5561 /* Construct a zero-dimensional basic set with the given parameter domain.
5563 __isl_give isl_basic_set *isl_basic_set_from_params(
5564 __isl_take isl_basic_set *bset)
5566 isl_space *space;
5567 space = isl_basic_set_get_space(bset);
5568 space = isl_space_set_from_params(space);
5569 bset = isl_basic_set_reset_space(bset, space);
5570 return bset;
5573 /* Compute the parameter domain of the given set.
5575 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5577 isl_space *space;
5578 unsigned n;
5580 if (isl_set_is_params(set))
5581 return set;
5583 n = isl_set_dim(set, isl_dim_set);
5584 set = isl_set_project_out(set, isl_dim_set, 0, n);
5585 space = isl_set_get_space(set);
5586 space = isl_space_params(space);
5587 set = isl_set_reset_space(set, space);
5588 return set;
5591 /* Construct a zero-dimensional set with the given parameter domain.
5593 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5595 isl_space *space;
5596 space = isl_set_get_space(set);
5597 space = isl_space_set_from_params(space);
5598 set = isl_set_reset_space(set, space);
5599 return set;
5602 /* Compute the parameter domain of the given map.
5604 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5606 isl_space *space;
5607 unsigned n;
5609 n = isl_map_dim(map, isl_dim_in);
5610 map = isl_map_project_out(map, isl_dim_in, 0, n);
5611 n = isl_map_dim(map, isl_dim_out);
5612 map = isl_map_project_out(map, isl_dim_out, 0, n);
5613 space = isl_map_get_space(map);
5614 space = isl_space_params(space);
5615 map = isl_map_reset_space(map, space);
5616 return map;
5619 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5621 isl_space *space;
5622 unsigned n_out;
5624 if (!bmap)
5625 return NULL;
5626 space = isl_space_domain(isl_basic_map_get_space(bmap));
5628 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5629 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5631 return isl_basic_map_reset_space(bmap, space);
5634 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5636 if (!bmap)
5637 return isl_bool_error;
5638 return isl_space_may_be_set(bmap->dim);
5641 /* Is this basic map actually a set?
5642 * Users should never call this function. Outside of isl,
5643 * the type should indicate whether something is a set or a map.
5645 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5647 if (!bmap)
5648 return isl_bool_error;
5649 return isl_space_is_set(bmap->dim);
5652 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5654 isl_bool is_set;
5656 is_set = isl_basic_map_is_set(bmap);
5657 if (is_set < 0)
5658 goto error;
5659 if (is_set)
5660 return bmap;
5661 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5662 error:
5663 isl_basic_map_free(bmap);
5664 return NULL;
5667 __isl_give isl_basic_map *isl_basic_map_domain_map(
5668 __isl_take isl_basic_map *bmap)
5670 int i;
5671 isl_space *dim;
5672 isl_basic_map *domain;
5673 int nparam, n_in, n_out;
5675 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5676 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5677 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5679 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5680 domain = isl_basic_map_universe(dim);
5682 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5683 bmap = isl_basic_map_apply_range(bmap, domain);
5684 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5686 for (i = 0; i < n_in; ++i)
5687 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5688 isl_dim_out, i);
5690 bmap = isl_basic_map_gauss(bmap, NULL);
5691 return isl_basic_map_finalize(bmap);
5694 __isl_give isl_basic_map *isl_basic_map_range_map(
5695 __isl_take isl_basic_map *bmap)
5697 int i;
5698 isl_space *dim;
5699 isl_basic_map *range;
5700 int nparam, n_in, n_out;
5702 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5703 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5704 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5706 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5707 range = isl_basic_map_universe(dim);
5709 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5710 bmap = isl_basic_map_apply_range(bmap, range);
5711 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5713 for (i = 0; i < n_out; ++i)
5714 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5715 isl_dim_out, i);
5717 bmap = isl_basic_map_gauss(bmap, NULL);
5718 return isl_basic_map_finalize(bmap);
5721 int isl_map_may_be_set(__isl_keep isl_map *map)
5723 if (!map)
5724 return -1;
5725 return isl_space_may_be_set(map->dim);
5728 /* Is this map actually a set?
5729 * Users should never call this function. Outside of isl,
5730 * the type should indicate whether something is a set or a map.
5732 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5734 if (!map)
5735 return isl_bool_error;
5736 return isl_space_is_set(map->dim);
5739 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5741 int i;
5742 isl_bool is_set;
5743 struct isl_set *set;
5745 is_set = isl_map_is_set(map);
5746 if (is_set < 0)
5747 goto error;
5748 if (is_set)
5749 return set_from_map(map);
5751 map = isl_map_cow(map);
5752 if (!map)
5753 goto error;
5755 set = set_from_map(map);
5756 set->dim = isl_space_range(set->dim);
5757 if (!set->dim)
5758 goto error;
5759 for (i = 0; i < map->n; ++i) {
5760 set->p[i] = isl_basic_map_range(map->p[i]);
5761 if (!set->p[i])
5762 goto error;
5764 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5765 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5766 return set;
5767 error:
5768 isl_map_free(map);
5769 return NULL;
5772 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5774 int i;
5776 map = isl_map_cow(map);
5777 if (!map)
5778 return NULL;
5780 map->dim = isl_space_domain_map(map->dim);
5781 if (!map->dim)
5782 goto error;
5783 for (i = 0; i < map->n; ++i) {
5784 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5785 if (!map->p[i])
5786 goto error;
5788 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5789 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5790 return map;
5791 error:
5792 isl_map_free(map);
5793 return NULL;
5796 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5798 int i;
5799 isl_space *range_dim;
5801 map = isl_map_cow(map);
5802 if (!map)
5803 return NULL;
5805 range_dim = isl_space_range(isl_map_get_space(map));
5806 range_dim = isl_space_from_range(range_dim);
5807 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5808 map->dim = isl_space_join(map->dim, range_dim);
5809 if (!map->dim)
5810 goto error;
5811 for (i = 0; i < map->n; ++i) {
5812 map->p[i] = isl_basic_map_range_map(map->p[i]);
5813 if (!map->p[i])
5814 goto error;
5816 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5817 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5818 return map;
5819 error:
5820 isl_map_free(map);
5821 return NULL;
5824 /* Given a wrapped map of the form A[B -> C],
5825 * return the map A[B -> C] -> B.
5827 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5829 isl_id *id;
5830 isl_map *map;
5832 if (!set)
5833 return NULL;
5834 if (!isl_set_has_tuple_id(set))
5835 return isl_map_domain_map(isl_set_unwrap(set));
5837 id = isl_set_get_tuple_id(set);
5838 map = isl_map_domain_map(isl_set_unwrap(set));
5839 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5841 return map;
5844 __isl_give isl_basic_map *isl_basic_map_from_domain(
5845 __isl_take isl_basic_set *bset)
5847 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5850 __isl_give isl_basic_map *isl_basic_map_from_range(
5851 __isl_take isl_basic_set *bset)
5853 isl_space *space;
5854 space = isl_basic_set_get_space(bset);
5855 space = isl_space_from_range(space);
5856 bset = isl_basic_set_reset_space(bset, space);
5857 return bset_to_bmap(bset);
5860 /* Create a relation with the given set as range.
5861 * The domain of the created relation is a zero-dimensional
5862 * flat anonymous space.
5864 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5866 isl_space *space;
5867 space = isl_set_get_space(set);
5868 space = isl_space_from_range(space);
5869 set = isl_set_reset_space(set, space);
5870 return set_to_map(set);
5873 /* Create a relation with the given set as domain.
5874 * The range of the created relation is a zero-dimensional
5875 * flat anonymous space.
5877 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5879 return isl_map_reverse(isl_map_from_range(set));
5882 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5883 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5885 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5888 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5889 __isl_take isl_set *range)
5891 return isl_map_apply_range(isl_map_reverse(domain), range);
5894 /* Return a newly allocated isl_map with given space and flags and
5895 * room for "n" basic maps.
5896 * Make sure that all cached information is cleared.
5898 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5899 unsigned flags)
5901 struct isl_map *map;
5903 if (!space)
5904 return NULL;
5905 if (n < 0)
5906 isl_die(space->ctx, isl_error_internal,
5907 "negative number of basic maps", goto error);
5908 map = isl_calloc(space->ctx, struct isl_map,
5909 sizeof(struct isl_map) +
5910 (n - 1) * sizeof(struct isl_basic_map *));
5911 if (!map)
5912 goto error;
5914 map->ctx = space->ctx;
5915 isl_ctx_ref(map->ctx);
5916 map->ref = 1;
5917 map->size = n;
5918 map->n = 0;
5919 map->dim = space;
5920 map->flags = flags;
5921 return map;
5922 error:
5923 isl_space_free(space);
5924 return NULL;
5927 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
5929 struct isl_basic_map *bmap;
5930 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
5931 bmap = isl_basic_map_set_to_empty(bmap);
5932 return bmap;
5935 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
5937 struct isl_basic_set *bset;
5938 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
5939 bset = isl_basic_set_set_to_empty(bset);
5940 return bset;
5943 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
5945 struct isl_basic_map *bmap;
5946 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
5947 bmap = isl_basic_map_finalize(bmap);
5948 return bmap;
5951 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
5953 struct isl_basic_set *bset;
5954 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
5955 bset = isl_basic_set_finalize(bset);
5956 return bset;
5959 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5961 int i;
5962 unsigned total = isl_space_dim(dim, isl_dim_all);
5963 isl_basic_map *bmap;
5965 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5966 for (i = 0; i < total; ++i) {
5967 int k = isl_basic_map_alloc_inequality(bmap);
5968 if (k < 0)
5969 goto error;
5970 isl_seq_clr(bmap->ineq[k], 1 + total);
5971 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5973 return bmap;
5974 error:
5975 isl_basic_map_free(bmap);
5976 return NULL;
5979 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5981 return isl_basic_map_nat_universe(dim);
5984 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5986 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5989 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5991 return isl_map_nat_universe(dim);
5994 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
5996 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
5999 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6001 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6004 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6006 struct isl_map *map;
6007 if (!space)
6008 return NULL;
6009 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6010 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6011 return map;
6014 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6016 struct isl_set *set;
6017 if (!space)
6018 return NULL;
6019 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6020 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6021 return set;
6024 struct isl_map *isl_map_dup(struct isl_map *map)
6026 int i;
6027 struct isl_map *dup;
6029 if (!map)
6030 return NULL;
6031 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6032 for (i = 0; i < map->n; ++i)
6033 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6034 return dup;
6037 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6038 __isl_take isl_basic_map *bmap)
6040 if (!bmap || !map)
6041 goto error;
6042 if (isl_basic_map_plain_is_empty(bmap)) {
6043 isl_basic_map_free(bmap);
6044 return map;
6046 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6047 isl_assert(map->ctx, map->n < map->size, goto error);
6048 map->p[map->n] = bmap;
6049 map->n++;
6050 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6051 return map;
6052 error:
6053 if (map)
6054 isl_map_free(map);
6055 if (bmap)
6056 isl_basic_map_free(bmap);
6057 return NULL;
6060 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6062 int i;
6064 if (!map)
6065 return NULL;
6067 if (--map->ref > 0)
6068 return NULL;
6070 clear_caches(map);
6071 isl_ctx_deref(map->ctx);
6072 for (i = 0; i < map->n; ++i)
6073 isl_basic_map_free(map->p[i]);
6074 isl_space_free(map->dim);
6075 free(map);
6077 return NULL;
6080 static struct isl_basic_map *isl_basic_map_fix_pos_si(
6081 struct isl_basic_map *bmap, unsigned pos, int value)
6083 int j;
6085 bmap = isl_basic_map_cow(bmap);
6086 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6087 j = isl_basic_map_alloc_equality(bmap);
6088 if (j < 0)
6089 goto error;
6090 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6091 isl_int_set_si(bmap->eq[j][pos], -1);
6092 isl_int_set_si(bmap->eq[j][0], value);
6093 bmap = isl_basic_map_simplify(bmap);
6094 return isl_basic_map_finalize(bmap);
6095 error:
6096 isl_basic_map_free(bmap);
6097 return NULL;
6100 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6101 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6103 int j;
6105 bmap = isl_basic_map_cow(bmap);
6106 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6107 j = isl_basic_map_alloc_equality(bmap);
6108 if (j < 0)
6109 goto error;
6110 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6111 isl_int_set_si(bmap->eq[j][pos], -1);
6112 isl_int_set(bmap->eq[j][0], value);
6113 bmap = isl_basic_map_simplify(bmap);
6114 return isl_basic_map_finalize(bmap);
6115 error:
6116 isl_basic_map_free(bmap);
6117 return NULL;
6120 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6121 enum isl_dim_type type, unsigned pos, int value)
6123 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6124 return isl_basic_map_free(bmap);
6125 return isl_basic_map_fix_pos_si(bmap,
6126 isl_basic_map_offset(bmap, type) + pos, value);
6129 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6130 enum isl_dim_type type, unsigned pos, isl_int value)
6132 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6133 return isl_basic_map_free(bmap);
6134 return isl_basic_map_fix_pos(bmap,
6135 isl_basic_map_offset(bmap, type) + pos, value);
6138 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6139 * to be equal to "v".
6141 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6142 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6144 if (!bmap || !v)
6145 goto error;
6146 if (!isl_val_is_int(v))
6147 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6148 "expecting integer value", goto error);
6149 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6150 goto error;
6151 pos += isl_basic_map_offset(bmap, type);
6152 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6153 isl_val_free(v);
6154 return bmap;
6155 error:
6156 isl_basic_map_free(bmap);
6157 isl_val_free(v);
6158 return NULL;
6161 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6162 * to be equal to "v".
6164 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6165 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6167 return isl_basic_map_fix_val(bset, type, pos, v);
6170 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6171 enum isl_dim_type type, unsigned pos, int value)
6173 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6174 type, pos, value));
6177 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6178 enum isl_dim_type type, unsigned pos, isl_int value)
6180 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6181 type, pos, value));
6184 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6185 unsigned input, int value)
6187 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6190 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6191 unsigned dim, int value)
6193 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6194 isl_dim_set, dim, value));
6197 static int remove_if_empty(__isl_keep isl_map *map, int i)
6199 int empty = isl_basic_map_plain_is_empty(map->p[i]);
6201 if (empty < 0)
6202 return -1;
6203 if (!empty)
6204 return 0;
6206 isl_basic_map_free(map->p[i]);
6207 if (i != map->n - 1) {
6208 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6209 map->p[i] = map->p[map->n - 1];
6211 map->n--;
6213 return 0;
6216 /* Perform "fn" on each basic map of "map", where we may not be holding
6217 * the only reference to "map".
6218 * In particular, "fn" should be a semantics preserving operation
6219 * that we want to apply to all copies of "map". We therefore need
6220 * to be careful not to modify "map" in a way that breaks "map"
6221 * in case anything goes wrong.
6223 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6224 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6226 struct isl_basic_map *bmap;
6227 int i;
6229 if (!map)
6230 return NULL;
6232 for (i = map->n - 1; i >= 0; --i) {
6233 bmap = isl_basic_map_copy(map->p[i]);
6234 bmap = fn(bmap);
6235 if (!bmap)
6236 goto error;
6237 isl_basic_map_free(map->p[i]);
6238 map->p[i] = bmap;
6239 if (remove_if_empty(map, i) < 0)
6240 goto error;
6243 return map;
6244 error:
6245 isl_map_free(map);
6246 return NULL;
6249 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6250 enum isl_dim_type type, unsigned pos, int value)
6252 int i;
6254 map = isl_map_cow(map);
6255 if (!map)
6256 return NULL;
6258 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6259 for (i = map->n - 1; i >= 0; --i) {
6260 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6261 if (remove_if_empty(map, i) < 0)
6262 goto error;
6264 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6265 return map;
6266 error:
6267 isl_map_free(map);
6268 return NULL;
6271 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6272 enum isl_dim_type type, unsigned pos, int value)
6274 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6277 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6278 enum isl_dim_type type, unsigned pos, isl_int value)
6280 int i;
6282 map = isl_map_cow(map);
6283 if (!map)
6284 return NULL;
6286 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6287 for (i = 0; i < map->n; ++i) {
6288 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6289 if (!map->p[i])
6290 goto error;
6292 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6293 return map;
6294 error:
6295 isl_map_free(map);
6296 return NULL;
6299 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6300 enum isl_dim_type type, unsigned pos, isl_int value)
6302 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6305 /* Fix the value of the variable at position "pos" of type "type" of "map"
6306 * to be equal to "v".
6308 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6309 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6311 int i;
6313 map = isl_map_cow(map);
6314 if (!map || !v)
6315 goto error;
6317 if (!isl_val_is_int(v))
6318 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6319 "expecting integer value", goto error);
6320 if (pos >= isl_map_dim(map, type))
6321 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6322 "index out of bounds", goto error);
6323 for (i = map->n - 1; i >= 0; --i) {
6324 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6325 isl_val_copy(v));
6326 if (remove_if_empty(map, i) < 0)
6327 goto error;
6329 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6330 isl_val_free(v);
6331 return map;
6332 error:
6333 isl_map_free(map);
6334 isl_val_free(v);
6335 return NULL;
6338 /* Fix the value of the variable at position "pos" of type "type" of "set"
6339 * to be equal to "v".
6341 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6342 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6344 return isl_map_fix_val(set, type, pos, v);
6347 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6348 unsigned input, int value)
6350 return isl_map_fix_si(map, isl_dim_in, input, value);
6353 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6355 return set_from_map(isl_map_fix_si(set_to_map(set),
6356 isl_dim_set, dim, value));
6359 static __isl_give isl_basic_map *basic_map_bound_si(
6360 __isl_take isl_basic_map *bmap,
6361 enum isl_dim_type type, unsigned pos, int value, int upper)
6363 int j;
6365 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6366 return isl_basic_map_free(bmap);
6367 pos += isl_basic_map_offset(bmap, type);
6368 bmap = isl_basic_map_cow(bmap);
6369 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6370 j = isl_basic_map_alloc_inequality(bmap);
6371 if (j < 0)
6372 goto error;
6373 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6374 if (upper) {
6375 isl_int_set_si(bmap->ineq[j][pos], -1);
6376 isl_int_set_si(bmap->ineq[j][0], value);
6377 } else {
6378 isl_int_set_si(bmap->ineq[j][pos], 1);
6379 isl_int_set_si(bmap->ineq[j][0], -value);
6381 bmap = isl_basic_map_simplify(bmap);
6382 return isl_basic_map_finalize(bmap);
6383 error:
6384 isl_basic_map_free(bmap);
6385 return NULL;
6388 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6389 __isl_take isl_basic_map *bmap,
6390 enum isl_dim_type type, unsigned pos, int value)
6392 return basic_map_bound_si(bmap, type, pos, value, 0);
6395 /* Constrain the values of the given dimension to be no greater than "value".
6397 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6398 __isl_take isl_basic_map *bmap,
6399 enum isl_dim_type type, unsigned pos, int value)
6401 return basic_map_bound_si(bmap, type, pos, value, 1);
6404 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6405 enum isl_dim_type type, unsigned pos, int value, int upper)
6407 int i;
6409 map = isl_map_cow(map);
6410 if (!map)
6411 return NULL;
6413 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6414 for (i = 0; i < map->n; ++i) {
6415 map->p[i] = basic_map_bound_si(map->p[i],
6416 type, pos, value, upper);
6417 if (!map->p[i])
6418 goto error;
6420 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6421 return map;
6422 error:
6423 isl_map_free(map);
6424 return NULL;
6427 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6428 enum isl_dim_type type, unsigned pos, int value)
6430 return map_bound_si(map, type, pos, value, 0);
6433 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6434 enum isl_dim_type type, unsigned pos, int value)
6436 return map_bound_si(map, type, pos, value, 1);
6439 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6440 enum isl_dim_type type, unsigned pos, int value)
6442 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6443 type, pos, value));
6446 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6447 enum isl_dim_type type, unsigned pos, int value)
6449 return isl_map_upper_bound_si(set, type, pos, value);
6452 /* Bound the given variable of "bmap" from below (or above is "upper"
6453 * is set) to "value".
6455 static __isl_give isl_basic_map *basic_map_bound(
6456 __isl_take isl_basic_map *bmap,
6457 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6459 int j;
6461 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6462 return isl_basic_map_free(bmap);
6463 pos += isl_basic_map_offset(bmap, type);
6464 bmap = isl_basic_map_cow(bmap);
6465 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6466 j = isl_basic_map_alloc_inequality(bmap);
6467 if (j < 0)
6468 goto error;
6469 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6470 if (upper) {
6471 isl_int_set_si(bmap->ineq[j][pos], -1);
6472 isl_int_set(bmap->ineq[j][0], value);
6473 } else {
6474 isl_int_set_si(bmap->ineq[j][pos], 1);
6475 isl_int_neg(bmap->ineq[j][0], value);
6477 bmap = isl_basic_map_simplify(bmap);
6478 return isl_basic_map_finalize(bmap);
6479 error:
6480 isl_basic_map_free(bmap);
6481 return NULL;
6484 /* Bound the given variable of "map" from below (or above is "upper"
6485 * is set) to "value".
6487 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6488 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6490 int i;
6492 map = isl_map_cow(map);
6493 if (!map)
6494 return NULL;
6496 if (pos >= isl_map_dim(map, type))
6497 isl_die(map->ctx, isl_error_invalid,
6498 "index out of bounds", goto error);
6499 for (i = map->n - 1; i >= 0; --i) {
6500 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6501 if (remove_if_empty(map, i) < 0)
6502 goto error;
6504 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6505 return map;
6506 error:
6507 isl_map_free(map);
6508 return NULL;
6511 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6512 enum isl_dim_type type, unsigned pos, isl_int value)
6514 return map_bound(map, type, pos, value, 0);
6517 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6518 enum isl_dim_type type, unsigned pos, isl_int value)
6520 return map_bound(map, type, pos, value, 1);
6523 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6524 enum isl_dim_type type, unsigned pos, isl_int value)
6526 return isl_map_lower_bound(set, type, pos, value);
6529 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6530 enum isl_dim_type type, unsigned pos, isl_int value)
6532 return isl_map_upper_bound(set, type, pos, value);
6535 /* Force the values of the variable at position "pos" of type "type" of "set"
6536 * to be no smaller than "value".
6538 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6539 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6541 if (!value)
6542 goto error;
6543 if (!isl_val_is_int(value))
6544 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6545 "expecting integer value", goto error);
6546 set = isl_set_lower_bound(set, type, pos, value->n);
6547 isl_val_free(value);
6548 return set;
6549 error:
6550 isl_val_free(value);
6551 isl_set_free(set);
6552 return NULL;
6555 /* Force the values of the variable at position "pos" of type "type" of "set"
6556 * to be no greater than "value".
6558 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6559 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6561 if (!value)
6562 goto error;
6563 if (!isl_val_is_int(value))
6564 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6565 "expecting integer value", goto error);
6566 set = isl_set_upper_bound(set, type, pos, value->n);
6567 isl_val_free(value);
6568 return set;
6569 error:
6570 isl_val_free(value);
6571 isl_set_free(set);
6572 return NULL;
6575 /* Bound the given variable of "bset" from below (or above is "upper"
6576 * is set) to "value".
6578 static __isl_give isl_basic_set *isl_basic_set_bound(
6579 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6580 isl_int value, int upper)
6582 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6583 type, pos, value, upper));
6586 /* Bound the given variable of "bset" from below (or above is "upper"
6587 * is set) to "value".
6589 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6590 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6591 __isl_take isl_val *value, int upper)
6593 if (!value)
6594 goto error;
6595 if (!isl_val_is_int(value))
6596 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6597 "expecting integer value", goto error);
6598 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6599 isl_val_free(value);
6600 return bset;
6601 error:
6602 isl_val_free(value);
6603 isl_basic_set_free(bset);
6604 return NULL;
6607 /* Bound the given variable of "bset" from below to "value".
6609 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6610 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6611 __isl_take isl_val *value)
6613 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6616 /* Bound the given variable of "bset" from above to "value".
6618 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6619 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6620 __isl_take isl_val *value)
6622 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6625 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6627 int i;
6629 map = isl_map_cow(map);
6630 if (!map)
6631 return NULL;
6633 map->dim = isl_space_reverse(map->dim);
6634 if (!map->dim)
6635 goto error;
6636 for (i = 0; i < map->n; ++i) {
6637 map->p[i] = isl_basic_map_reverse(map->p[i]);
6638 if (!map->p[i])
6639 goto error;
6641 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6642 return map;
6643 error:
6644 isl_map_free(map);
6645 return NULL;
6648 #undef TYPE
6649 #define TYPE isl_pw_multi_aff
6650 #undef SUFFIX
6651 #define SUFFIX _pw_multi_aff
6652 #undef EMPTY
6653 #define EMPTY isl_pw_multi_aff_empty
6654 #undef ADD
6655 #define ADD isl_pw_multi_aff_union_add
6656 #include "isl_map_lexopt_templ.c"
6658 /* Given a map "map", compute the lexicographically minimal
6659 * (or maximal) image element for each domain element in dom,
6660 * in the form of an isl_pw_multi_aff.
6661 * If "empty" is not NULL, then set *empty to those elements in dom that
6662 * do not have an image element.
6663 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6664 * should be computed over the domain of "map". "empty" is also NULL
6665 * in this case.
6667 * We first compute the lexicographically minimal or maximal element
6668 * in the first basic map. This results in a partial solution "res"
6669 * and a subset "todo" of dom that still need to be handled.
6670 * We then consider each of the remaining maps in "map" and successively
6671 * update both "res" and "todo".
6672 * If "empty" is NULL, then the todo sets are not needed and therefore
6673 * also not computed.
6675 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6676 __isl_take isl_map *map, __isl_take isl_set *dom,
6677 __isl_give isl_set **empty, unsigned flags)
6679 int i;
6680 int full;
6681 isl_pw_multi_aff *res;
6682 isl_set *todo;
6684 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6685 if (!map || (!full && !dom))
6686 goto error;
6688 if (isl_map_plain_is_empty(map)) {
6689 if (empty)
6690 *empty = dom;
6691 else
6692 isl_set_free(dom);
6693 return isl_pw_multi_aff_from_map(map);
6696 res = basic_map_partial_lexopt_pw_multi_aff(
6697 isl_basic_map_copy(map->p[0]),
6698 isl_set_copy(dom), empty, flags);
6700 if (empty)
6701 todo = *empty;
6702 for (i = 1; i < map->n; ++i) {
6703 isl_pw_multi_aff *res_i;
6705 res_i = basic_map_partial_lexopt_pw_multi_aff(
6706 isl_basic_map_copy(map->p[i]),
6707 isl_set_copy(dom), empty, flags);
6709 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6710 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6711 else
6712 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6714 if (empty)
6715 todo = isl_set_intersect(todo, *empty);
6718 isl_set_free(dom);
6719 isl_map_free(map);
6721 if (empty)
6722 *empty = todo;
6724 return res;
6725 error:
6726 if (empty)
6727 *empty = NULL;
6728 isl_set_free(dom);
6729 isl_map_free(map);
6730 return NULL;
6733 #undef TYPE
6734 #define TYPE isl_map
6735 #undef SUFFIX
6736 #define SUFFIX
6737 #undef EMPTY
6738 #define EMPTY isl_map_empty
6739 #undef ADD
6740 #define ADD isl_map_union_disjoint
6741 #include "isl_map_lexopt_templ.c"
6743 /* Given a map "map", compute the lexicographically minimal
6744 * (or maximal) image element for each domain element in "dom",
6745 * in the form of an isl_map.
6746 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6747 * do not have an image element.
6748 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6749 * should be computed over the domain of "map". "empty" is also NULL
6750 * in this case.
6752 * If the input consists of more than one disjunct, then first
6753 * compute the desired result in the form of an isl_pw_multi_aff and
6754 * then convert that into an isl_map.
6756 * This function used to have an explicit implementation in terms
6757 * of isl_maps, but it would continually intersect the domains of
6758 * partial results with the complement of the domain of the next
6759 * partial solution, potentially leading to an explosion in the number
6760 * of disjuncts if there are several disjuncts in the input.
6761 * An even earlier implementation of this function would look for
6762 * better results in the domain of the partial result and for extra
6763 * results in the complement of this domain, which would lead to
6764 * even more splintering.
6766 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6767 __isl_take isl_map *map, __isl_take isl_set *dom,
6768 __isl_give isl_set **empty, unsigned flags)
6770 int full;
6771 struct isl_map *res;
6772 isl_pw_multi_aff *pma;
6774 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6775 if (!map || (!full && !dom))
6776 goto error;
6778 if (isl_map_plain_is_empty(map)) {
6779 if (empty)
6780 *empty = dom;
6781 else
6782 isl_set_free(dom);
6783 return map;
6786 if (map->n == 1) {
6787 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6788 dom, empty, flags);
6789 isl_map_free(map);
6790 return res;
6793 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6794 flags);
6795 return isl_map_from_pw_multi_aff(pma);
6796 error:
6797 if (empty)
6798 *empty = NULL;
6799 isl_set_free(dom);
6800 isl_map_free(map);
6801 return NULL;
6804 __isl_give isl_map *isl_map_partial_lexmax(
6805 __isl_take isl_map *map, __isl_take isl_set *dom,
6806 __isl_give isl_set **empty)
6808 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6811 __isl_give isl_map *isl_map_partial_lexmin(
6812 __isl_take isl_map *map, __isl_take isl_set *dom,
6813 __isl_give isl_set **empty)
6815 return isl_map_partial_lexopt(map, dom, empty, 0);
6818 __isl_give isl_set *isl_set_partial_lexmin(
6819 __isl_take isl_set *set, __isl_take isl_set *dom,
6820 __isl_give isl_set **empty)
6822 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6823 dom, empty));
6826 __isl_give isl_set *isl_set_partial_lexmax(
6827 __isl_take isl_set *set, __isl_take isl_set *dom,
6828 __isl_give isl_set **empty)
6830 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6831 dom, empty));
6834 /* Compute the lexicographic minimum (or maximum if "flags" includes
6835 * ISL_OPT_MAX) of "bset" over its parametric domain.
6837 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6838 unsigned flags)
6840 return isl_basic_map_lexopt(bset, flags);
6843 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6845 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6848 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6850 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6853 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6855 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6858 /* Compute the lexicographic minimum of "bset" over its parametric domain
6859 * for the purpose of quantifier elimination.
6860 * That is, find an explicit representation for all the existentially
6861 * quantified variables in "bset" by computing their lexicographic
6862 * minimum.
6864 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6865 __isl_take isl_basic_set *bset)
6867 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6870 /* Extract the first and only affine expression from list
6871 * and then add it to *pwaff with the given dom.
6872 * This domain is known to be disjoint from other domains
6873 * because of the way isl_basic_map_foreach_lexmax works.
6875 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6876 __isl_take isl_aff_list *list, void *user)
6878 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6879 isl_aff *aff;
6880 isl_pw_aff **pwaff = user;
6881 isl_pw_aff *pwaff_i;
6883 if (!list)
6884 goto error;
6885 if (isl_aff_list_n_aff(list) != 1)
6886 isl_die(ctx, isl_error_internal,
6887 "expecting single element list", goto error);
6889 aff = isl_aff_list_get_aff(list, 0);
6890 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6892 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6894 isl_aff_list_free(list);
6896 return isl_stat_ok;
6897 error:
6898 isl_basic_set_free(dom);
6899 isl_aff_list_free(list);
6900 return isl_stat_error;
6903 /* Given a basic map with one output dimension, compute the minimum or
6904 * maximum of that dimension as an isl_pw_aff.
6906 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6907 * call update_dim_opt on each leaf of the result.
6909 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6910 int max)
6912 isl_space *dim = isl_basic_map_get_space(bmap);
6913 isl_pw_aff *pwaff;
6914 isl_stat r;
6916 dim = isl_space_from_domain(isl_space_domain(dim));
6917 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6918 pwaff = isl_pw_aff_empty(dim);
6920 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6921 if (r < 0)
6922 return isl_pw_aff_free(pwaff);
6924 return pwaff;
6927 /* Compute the minimum or maximum of the given output dimension
6928 * as a function of the parameters and the input dimensions,
6929 * but independently of the other output dimensions.
6931 * We first project out the other output dimension and then compute
6932 * the "lexicographic" maximum in each basic map, combining the results
6933 * using isl_pw_aff_union_max.
6935 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6936 int max)
6938 int i;
6939 isl_pw_aff *pwaff;
6940 unsigned n_out;
6942 n_out = isl_map_dim(map, isl_dim_out);
6943 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6944 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6945 if (!map)
6946 return NULL;
6948 if (map->n == 0) {
6949 isl_space *dim = isl_map_get_space(map);
6950 isl_map_free(map);
6951 return isl_pw_aff_empty(dim);
6954 pwaff = basic_map_dim_opt(map->p[0], max);
6955 for (i = 1; i < map->n; ++i) {
6956 isl_pw_aff *pwaff_i;
6958 pwaff_i = basic_map_dim_opt(map->p[i], max);
6959 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6962 isl_map_free(map);
6964 return pwaff;
6967 /* Compute the minimum of the given output dimension as a function of the
6968 * parameters and input dimensions, but independently of
6969 * the other output dimensions.
6971 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6973 return map_dim_opt(map, pos, 0);
6976 /* Compute the maximum of the given output dimension as a function of the
6977 * parameters and input dimensions, but independently of
6978 * the other output dimensions.
6980 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6982 return map_dim_opt(map, pos, 1);
6985 /* Compute the minimum or maximum of the given set dimension
6986 * as a function of the parameters,
6987 * but independently of the other set dimensions.
6989 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6990 int max)
6992 return map_dim_opt(set, pos, max);
6995 /* Compute the maximum of the given set dimension as a function of the
6996 * parameters, but independently of the other set dimensions.
6998 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7000 return set_dim_opt(set, pos, 1);
7003 /* Compute the minimum of the given set dimension as a function of the
7004 * parameters, but independently of the other set dimensions.
7006 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7008 return set_dim_opt(set, pos, 0);
7011 /* Apply a preimage specified by "mat" on the parameters of "bset".
7012 * bset is assumed to have only parameters and divs.
7014 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7015 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7017 unsigned nparam;
7019 if (!bset || !mat)
7020 goto error;
7022 bset->dim = isl_space_cow(bset->dim);
7023 if (!bset->dim)
7024 goto error;
7026 nparam = isl_basic_set_dim(bset, isl_dim_param);
7028 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7030 bset->dim->nparam = 0;
7031 bset->dim->n_out = nparam;
7032 bset = isl_basic_set_preimage(bset, mat);
7033 if (bset) {
7034 bset->dim->nparam = bset->dim->n_out;
7035 bset->dim->n_out = 0;
7037 return bset;
7038 error:
7039 isl_mat_free(mat);
7040 isl_basic_set_free(bset);
7041 return NULL;
7044 /* Apply a preimage specified by "mat" on the parameters of "set".
7045 * set is assumed to have only parameters and divs.
7047 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7048 __isl_take isl_mat *mat)
7050 isl_space *space;
7051 unsigned nparam;
7053 if (!set || !mat)
7054 goto error;
7056 nparam = isl_set_dim(set, isl_dim_param);
7058 if (mat->n_row != 1 + nparam)
7059 isl_die(isl_set_get_ctx(set), isl_error_internal,
7060 "unexpected number of rows", goto error);
7062 space = isl_set_get_space(set);
7063 space = isl_space_move_dims(space, isl_dim_set, 0,
7064 isl_dim_param, 0, nparam);
7065 set = isl_set_reset_space(set, space);
7066 set = isl_set_preimage(set, mat);
7067 nparam = isl_set_dim(set, isl_dim_out);
7068 space = isl_set_get_space(set);
7069 space = isl_space_move_dims(space, isl_dim_param, 0,
7070 isl_dim_out, 0, nparam);
7071 set = isl_set_reset_space(set, space);
7072 return set;
7073 error:
7074 isl_mat_free(mat);
7075 isl_set_free(set);
7076 return NULL;
7079 /* Intersect the basic set "bset" with the affine space specified by the
7080 * equalities in "eq".
7082 static __isl_give isl_basic_set *basic_set_append_equalities(
7083 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7085 int i, k;
7086 unsigned len;
7088 if (!bset || !eq)
7089 goto error;
7091 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7092 eq->n_row, 0);
7093 if (!bset)
7094 goto error;
7096 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7097 for (i = 0; i < eq->n_row; ++i) {
7098 k = isl_basic_set_alloc_equality(bset);
7099 if (k < 0)
7100 goto error;
7101 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7102 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7104 isl_mat_free(eq);
7106 bset = isl_basic_set_gauss(bset, NULL);
7107 bset = isl_basic_set_finalize(bset);
7109 return bset;
7110 error:
7111 isl_mat_free(eq);
7112 isl_basic_set_free(bset);
7113 return NULL;
7116 /* Intersect the set "set" with the affine space specified by the
7117 * equalities in "eq".
7119 static struct isl_set *set_append_equalities(struct isl_set *set,
7120 struct isl_mat *eq)
7122 int i;
7124 if (!set || !eq)
7125 goto error;
7127 for (i = 0; i < set->n; ++i) {
7128 set->p[i] = basic_set_append_equalities(set->p[i],
7129 isl_mat_copy(eq));
7130 if (!set->p[i])
7131 goto error;
7133 isl_mat_free(eq);
7134 return set;
7135 error:
7136 isl_mat_free(eq);
7137 isl_set_free(set);
7138 return NULL;
7141 /* Given a basic set "bset" that only involves parameters and existentially
7142 * quantified variables, return the index of the first equality
7143 * that only involves parameters. If there is no such equality then
7144 * return bset->n_eq.
7146 * This function assumes that isl_basic_set_gauss has been called on "bset".
7148 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7150 int i, j;
7151 unsigned nparam, n_div;
7153 if (!bset)
7154 return -1;
7156 nparam = isl_basic_set_dim(bset, isl_dim_param);
7157 n_div = isl_basic_set_dim(bset, isl_dim_div);
7159 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7160 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7161 ++i;
7164 return i;
7167 /* Compute an explicit representation for the existentially quantified
7168 * variables in "bset" by computing the "minimal value" of the set
7169 * variables. Since there are no set variables, the computation of
7170 * the minimal value essentially computes an explicit representation
7171 * of the non-empty part(s) of "bset".
7173 * The input only involves parameters and existentially quantified variables.
7174 * All equalities among parameters have been removed.
7176 * Since the existentially quantified variables in the result are in general
7177 * going to be different from those in the input, we first replace
7178 * them by the minimal number of variables based on their equalities.
7179 * This should simplify the parametric integer programming.
7181 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7183 isl_morph *morph1, *morph2;
7184 isl_set *set;
7185 unsigned n;
7187 if (!bset)
7188 return NULL;
7189 if (bset->n_eq == 0)
7190 return isl_basic_set_lexmin_compute_divs(bset);
7192 morph1 = isl_basic_set_parameter_compression(bset);
7193 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7194 bset = isl_basic_set_lift(bset);
7195 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7196 bset = isl_morph_basic_set(morph2, bset);
7197 n = isl_basic_set_dim(bset, isl_dim_set);
7198 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7200 set = isl_basic_set_lexmin_compute_divs(bset);
7202 set = isl_morph_set(isl_morph_inverse(morph1), set);
7204 return set;
7207 /* Project the given basic set onto its parameter domain, possibly introducing
7208 * new, explicit, existential variables in the constraints.
7209 * The input has parameters and (possibly implicit) existential variables.
7210 * The output has the same parameters, but only
7211 * explicit existentially quantified variables.
7213 * The actual projection is performed by pip, but pip doesn't seem
7214 * to like equalities very much, so we first remove the equalities
7215 * among the parameters by performing a variable compression on
7216 * the parameters. Afterward, an inverse transformation is performed
7217 * and the equalities among the parameters are inserted back in.
7219 * The variable compression on the parameters may uncover additional
7220 * equalities that were only implicit before. We therefore check
7221 * if there are any new parameter equalities in the result and
7222 * if so recurse. The removal of parameter equalities is required
7223 * for the parameter compression performed by base_compute_divs.
7225 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7227 int i;
7228 struct isl_mat *eq;
7229 struct isl_mat *T, *T2;
7230 struct isl_set *set;
7231 unsigned nparam;
7233 bset = isl_basic_set_cow(bset);
7234 if (!bset)
7235 return NULL;
7237 if (bset->n_eq == 0)
7238 return base_compute_divs(bset);
7240 bset = isl_basic_set_gauss(bset, NULL);
7241 if (!bset)
7242 return NULL;
7243 if (isl_basic_set_plain_is_empty(bset))
7244 return isl_set_from_basic_set(bset);
7246 i = first_parameter_equality(bset);
7247 if (i == bset->n_eq)
7248 return base_compute_divs(bset);
7250 nparam = isl_basic_set_dim(bset, isl_dim_param);
7251 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7252 0, 1 + nparam);
7253 eq = isl_mat_cow(eq);
7254 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7255 if (T && T->n_col == 0) {
7256 isl_mat_free(T);
7257 isl_mat_free(T2);
7258 isl_mat_free(eq);
7259 bset = isl_basic_set_set_to_empty(bset);
7260 return isl_set_from_basic_set(bset);
7262 bset = basic_set_parameter_preimage(bset, T);
7264 i = first_parameter_equality(bset);
7265 if (!bset)
7266 set = NULL;
7267 else if (i == bset->n_eq)
7268 set = base_compute_divs(bset);
7269 else
7270 set = parameter_compute_divs(bset);
7271 set = set_parameter_preimage(set, T2);
7272 set = set_append_equalities(set, eq);
7273 return set;
7276 /* Insert the divs from "ls" before those of "bmap".
7278 * The number of columns is not changed, which means that the last
7279 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7280 * The caller is responsible for removing the same number of dimensions
7281 * from the space of "bmap".
7283 static __isl_give isl_basic_map *insert_divs_from_local_space(
7284 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7286 int i;
7287 int n_div;
7288 int old_n_div;
7290 n_div = isl_local_space_dim(ls, isl_dim_div);
7291 if (n_div == 0)
7292 return bmap;
7294 old_n_div = bmap->n_div;
7295 bmap = insert_div_rows(bmap, n_div);
7296 if (!bmap)
7297 return NULL;
7299 for (i = 0; i < n_div; ++i) {
7300 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7301 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7304 return bmap;
7307 /* Replace the space of "bmap" by the space and divs of "ls".
7309 * If "ls" has any divs, then we simplify the result since we may
7310 * have discovered some additional equalities that could simplify
7311 * the div expressions.
7313 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7314 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7316 int n_div;
7318 bmap = isl_basic_map_cow(bmap);
7319 if (!bmap || !ls)
7320 goto error;
7322 n_div = isl_local_space_dim(ls, isl_dim_div);
7323 bmap = insert_divs_from_local_space(bmap, ls);
7324 if (!bmap)
7325 goto error;
7327 isl_space_free(bmap->dim);
7328 bmap->dim = isl_local_space_get_space(ls);
7329 if (!bmap->dim)
7330 goto error;
7332 isl_local_space_free(ls);
7333 if (n_div > 0)
7334 bmap = isl_basic_map_simplify(bmap);
7335 bmap = isl_basic_map_finalize(bmap);
7336 return bmap;
7337 error:
7338 isl_basic_map_free(bmap);
7339 isl_local_space_free(ls);
7340 return NULL;
7343 /* Replace the space of "map" by the space and divs of "ls".
7345 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7346 __isl_take isl_local_space *ls)
7348 int i;
7350 map = isl_map_cow(map);
7351 if (!map || !ls)
7352 goto error;
7354 for (i = 0; i < map->n; ++i) {
7355 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7356 isl_local_space_copy(ls));
7357 if (!map->p[i])
7358 goto error;
7360 isl_space_free(map->dim);
7361 map->dim = isl_local_space_get_space(ls);
7362 if (!map->dim)
7363 goto error;
7365 isl_local_space_free(ls);
7366 return map;
7367 error:
7368 isl_local_space_free(ls);
7369 isl_map_free(map);
7370 return NULL;
7373 /* Compute an explicit representation for the existentially
7374 * quantified variables for which do not know any explicit representation yet.
7376 * We first sort the existentially quantified variables so that the
7377 * existentially quantified variables for which we already have an explicit
7378 * representation are placed before those for which we do not.
7379 * The input dimensions, the output dimensions and the existentially
7380 * quantified variables for which we already have an explicit
7381 * representation are then turned into parameters.
7382 * compute_divs returns a map with the same parameters and
7383 * no input or output dimensions and the dimension specification
7384 * is reset to that of the input, including the existentially quantified
7385 * variables for which we already had an explicit representation.
7387 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7389 struct isl_basic_set *bset;
7390 struct isl_set *set;
7391 struct isl_map *map;
7392 isl_space *dim;
7393 isl_local_space *ls;
7394 unsigned nparam;
7395 unsigned n_in;
7396 unsigned n_out;
7397 int n_known;
7398 int i;
7400 bmap = isl_basic_map_sort_divs(bmap);
7401 bmap = isl_basic_map_cow(bmap);
7402 if (!bmap)
7403 return NULL;
7405 n_known = isl_basic_map_first_unknown_div(bmap);
7406 if (n_known < 0)
7407 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7409 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7410 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7411 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7412 dim = isl_space_set_alloc(bmap->ctx,
7413 nparam + n_in + n_out + n_known, 0);
7414 if (!dim)
7415 goto error;
7417 ls = isl_basic_map_get_local_space(bmap);
7418 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7419 n_known, bmap->n_div - n_known);
7420 if (n_known > 0) {
7421 for (i = n_known; i < bmap->n_div; ++i)
7422 swap_div(bmap, i - n_known, i);
7423 bmap->n_div -= n_known;
7424 bmap->extra -= n_known;
7426 bmap = isl_basic_map_reset_space(bmap, dim);
7427 bset = bset_from_bmap(bmap);
7429 set = parameter_compute_divs(bset);
7430 map = set_to_map(set);
7431 map = replace_space_by_local_space(map, ls);
7433 return map;
7434 error:
7435 isl_basic_map_free(bmap);
7436 return NULL;
7439 /* Remove the explicit representation of local variable "div",
7440 * if there is any.
7442 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7443 __isl_take isl_basic_map *bmap, int div)
7445 isl_bool unknown;
7447 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7448 if (unknown < 0)
7449 return isl_basic_map_free(bmap);
7450 if (unknown)
7451 return bmap;
7453 bmap = isl_basic_map_cow(bmap);
7454 if (!bmap)
7455 return NULL;
7456 isl_int_set_si(bmap->div[div][0], 0);
7457 return bmap;
7460 /* Is local variable "div" of "bmap" marked as not having an explicit
7461 * representation?
7462 * Note that even if "div" is not marked in this way and therefore
7463 * has an explicit representation, this representation may still
7464 * depend (indirectly) on other local variables that do not
7465 * have an explicit representation.
7467 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7468 int div)
7470 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7471 return isl_bool_error;
7472 return isl_int_is_zero(bmap->div[div][0]);
7475 /* Return the position of the first local variable that does not
7476 * have an explicit representation.
7477 * Return the total number of local variables if they all have
7478 * an explicit representation.
7479 * Return -1 on error.
7481 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7483 int i;
7485 if (!bmap)
7486 return -1;
7488 for (i = 0; i < bmap->n_div; ++i) {
7489 if (!isl_basic_map_div_is_known(bmap, i))
7490 return i;
7492 return bmap->n_div;
7495 /* Return the position of the first local variable that does not
7496 * have an explicit representation.
7497 * Return the total number of local variables if they all have
7498 * an explicit representation.
7499 * Return -1 on error.
7501 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7503 return isl_basic_map_first_unknown_div(bset);
7506 /* Does "bmap" have an explicit representation for all local variables?
7508 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7510 int first, n;
7512 n = isl_basic_map_dim(bmap, isl_dim_div);
7513 first = isl_basic_map_first_unknown_div(bmap);
7514 if (first < 0)
7515 return isl_bool_error;
7516 return first == n;
7519 /* Do all basic maps in "map" have an explicit representation
7520 * for all local variables?
7522 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7524 int i;
7526 if (!map)
7527 return isl_bool_error;
7529 for (i = 0; i < map->n; ++i) {
7530 int known = isl_basic_map_divs_known(map->p[i]);
7531 if (known <= 0)
7532 return known;
7535 return isl_bool_true;
7538 /* If bmap contains any unknown divs, then compute explicit
7539 * expressions for them. However, this computation may be
7540 * quite expensive, so first try to remove divs that aren't
7541 * strictly needed.
7543 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7545 int known;
7546 struct isl_map *map;
7548 known = isl_basic_map_divs_known(bmap);
7549 if (known < 0)
7550 goto error;
7551 if (known)
7552 return isl_map_from_basic_map(bmap);
7554 bmap = isl_basic_map_drop_redundant_divs(bmap);
7556 known = isl_basic_map_divs_known(bmap);
7557 if (known < 0)
7558 goto error;
7559 if (known)
7560 return isl_map_from_basic_map(bmap);
7562 map = compute_divs(bmap);
7563 return map;
7564 error:
7565 isl_basic_map_free(bmap);
7566 return NULL;
7569 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7571 int i;
7572 int known;
7573 struct isl_map *res;
7575 if (!map)
7576 return NULL;
7577 if (map->n == 0)
7578 return map;
7580 known = isl_map_divs_known(map);
7581 if (known < 0) {
7582 isl_map_free(map);
7583 return NULL;
7585 if (known)
7586 return map;
7588 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7589 for (i = 1 ; i < map->n; ++i) {
7590 struct isl_map *r2;
7591 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7592 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7593 res = isl_map_union_disjoint(res, r2);
7594 else
7595 res = isl_map_union(res, r2);
7597 isl_map_free(map);
7599 return res;
7602 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7604 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7607 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7609 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7612 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7614 int i;
7615 struct isl_set *set;
7617 if (!map)
7618 goto error;
7620 map = isl_map_cow(map);
7621 if (!map)
7622 return NULL;
7624 set = set_from_map(map);
7625 set->dim = isl_space_domain(set->dim);
7626 if (!set->dim)
7627 goto error;
7628 for (i = 0; i < map->n; ++i) {
7629 set->p[i] = isl_basic_map_domain(map->p[i]);
7630 if (!set->p[i])
7631 goto error;
7633 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7634 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7635 return set;
7636 error:
7637 isl_map_free(map);
7638 return NULL;
7641 /* Return the union of "map1" and "map2", where we assume for now that
7642 * "map1" and "map2" are disjoint. Note that the basic maps inside
7643 * "map1" or "map2" may not be disjoint from each other.
7644 * Also note that this function is also called from isl_map_union,
7645 * which takes care of handling the situation where "map1" and "map2"
7646 * may not be disjoint.
7648 * If one of the inputs is empty, we can simply return the other input.
7649 * Similarly, if one of the inputs is universal, then it is equal to the union.
7651 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7652 __isl_take isl_map *map2)
7654 int i;
7655 unsigned flags = 0;
7656 struct isl_map *map = NULL;
7657 int is_universe;
7659 if (!map1 || !map2)
7660 goto error;
7662 if (!isl_space_is_equal(map1->dim, map2->dim))
7663 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7664 "spaces don't match", goto error);
7666 if (map1->n == 0) {
7667 isl_map_free(map1);
7668 return map2;
7670 if (map2->n == 0) {
7671 isl_map_free(map2);
7672 return map1;
7675 is_universe = isl_map_plain_is_universe(map1);
7676 if (is_universe < 0)
7677 goto error;
7678 if (is_universe) {
7679 isl_map_free(map2);
7680 return map1;
7683 is_universe = isl_map_plain_is_universe(map2);
7684 if (is_universe < 0)
7685 goto error;
7686 if (is_universe) {
7687 isl_map_free(map1);
7688 return map2;
7691 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7692 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7693 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7695 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7696 map1->n + map2->n, flags);
7697 if (!map)
7698 goto error;
7699 for (i = 0; i < map1->n; ++i) {
7700 map = isl_map_add_basic_map(map,
7701 isl_basic_map_copy(map1->p[i]));
7702 if (!map)
7703 goto error;
7705 for (i = 0; i < map2->n; ++i) {
7706 map = isl_map_add_basic_map(map,
7707 isl_basic_map_copy(map2->p[i]));
7708 if (!map)
7709 goto error;
7711 isl_map_free(map1);
7712 isl_map_free(map2);
7713 return map;
7714 error:
7715 isl_map_free(map);
7716 isl_map_free(map1);
7717 isl_map_free(map2);
7718 return NULL;
7721 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7722 * guaranteed to be disjoint by the caller.
7724 * Note that this functions is called from within isl_map_make_disjoint,
7725 * so we have to be careful not to touch the constraints of the inputs
7726 * in any way.
7728 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7729 __isl_take isl_map *map2)
7731 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7734 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7735 * not be disjoint. The parameters are assumed to have been aligned.
7737 * We currently simply call map_union_disjoint, the internal operation
7738 * of which does not really depend on the inputs being disjoint.
7739 * If the result contains more than one basic map, then we clear
7740 * the disjoint flag since the result may contain basic maps from
7741 * both inputs and these are not guaranteed to be disjoint.
7743 * As a special case, if "map1" and "map2" are obviously equal,
7744 * then we simply return "map1".
7746 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7747 __isl_take isl_map *map2)
7749 int equal;
7751 if (!map1 || !map2)
7752 goto error;
7754 equal = isl_map_plain_is_equal(map1, map2);
7755 if (equal < 0)
7756 goto error;
7757 if (equal) {
7758 isl_map_free(map2);
7759 return map1;
7762 map1 = map_union_disjoint(map1, map2);
7763 if (!map1)
7764 return NULL;
7765 if (map1->n > 1)
7766 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7767 return map1;
7768 error:
7769 isl_map_free(map1);
7770 isl_map_free(map2);
7771 return NULL;
7774 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7775 * not be disjoint.
7777 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7778 __isl_take isl_map *map2)
7780 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7783 __isl_give isl_set *isl_set_union_disjoint(
7784 __isl_take isl_set *set1, __isl_take isl_set *set2)
7786 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7787 set_to_map(set2)));
7790 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7792 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7795 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7796 * the results.
7798 * "map" and "set" are assumed to be compatible and non-NULL.
7800 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7801 __isl_take isl_set *set,
7802 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7803 __isl_take isl_basic_set *bset))
7805 unsigned flags = 0;
7806 struct isl_map *result;
7807 int i, j;
7809 if (isl_set_plain_is_universe(set)) {
7810 isl_set_free(set);
7811 return map;
7814 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7815 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7816 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7818 result = isl_map_alloc_space(isl_space_copy(map->dim),
7819 map->n * set->n, flags);
7820 for (i = 0; result && i < map->n; ++i)
7821 for (j = 0; j < set->n; ++j) {
7822 result = isl_map_add_basic_map(result,
7823 fn(isl_basic_map_copy(map->p[i]),
7824 isl_basic_set_copy(set->p[j])));
7825 if (!result)
7826 break;
7829 isl_map_free(map);
7830 isl_set_free(set);
7831 return result;
7834 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7835 __isl_take isl_set *set)
7837 isl_bool ok;
7839 ok = isl_map_compatible_range(map, set);
7840 if (ok < 0)
7841 goto error;
7842 if (!ok)
7843 isl_die(set->ctx, isl_error_invalid,
7844 "incompatible spaces", goto error);
7846 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7847 error:
7848 isl_map_free(map);
7849 isl_set_free(set);
7850 return NULL;
7853 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7854 __isl_take isl_set *set)
7856 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7859 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7860 __isl_take isl_set *set)
7862 isl_bool ok;
7864 ok = isl_map_compatible_domain(map, set);
7865 if (ok < 0)
7866 goto error;
7867 if (!ok)
7868 isl_die(set->ctx, isl_error_invalid,
7869 "incompatible spaces", goto error);
7871 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7872 error:
7873 isl_map_free(map);
7874 isl_set_free(set);
7875 return NULL;
7878 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7879 __isl_take isl_set *set)
7881 return isl_map_align_params_map_map_and(map, set,
7882 &map_intersect_domain);
7885 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7886 * in the space B -> C, return the intersection.
7887 * The parameters are assumed to have been aligned.
7889 * The map "factor" is first extended to a map living in the space
7890 * [A -> B] -> C and then a regular intersection is computed.
7892 static __isl_give isl_map *map_intersect_domain_factor_range(
7893 __isl_take isl_map *map, __isl_take isl_map *factor)
7895 isl_space *space;
7896 isl_map *ext_factor;
7898 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7899 ext_factor = isl_map_universe(space);
7900 ext_factor = isl_map_domain_product(ext_factor, factor);
7901 return map_intersect(map, ext_factor);
7904 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7905 * in the space B -> C, return the intersection.
7907 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7908 __isl_take isl_map *map, __isl_take isl_map *factor)
7910 return isl_map_align_params_map_map_and(map, factor,
7911 &map_intersect_domain_factor_range);
7914 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7915 * in the space A -> C, return the intersection.
7917 * The map "factor" is first extended to a map living in the space
7918 * A -> [B -> C] and then a regular intersection is computed.
7920 static __isl_give isl_map *map_intersect_range_factor_range(
7921 __isl_take isl_map *map, __isl_take isl_map *factor)
7923 isl_space *space;
7924 isl_map *ext_factor;
7926 space = isl_space_range_factor_domain(isl_map_get_space(map));
7927 ext_factor = isl_map_universe(space);
7928 ext_factor = isl_map_range_product(ext_factor, factor);
7929 return isl_map_intersect(map, ext_factor);
7932 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7933 * in the space A -> C, return the intersection.
7935 __isl_give isl_map *isl_map_intersect_range_factor_range(
7936 __isl_take isl_map *map, __isl_take isl_map *factor)
7938 return isl_map_align_params_map_map_and(map, factor,
7939 &map_intersect_range_factor_range);
7942 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7943 __isl_take isl_map *map2)
7945 if (!map1 || !map2)
7946 goto error;
7947 map1 = isl_map_reverse(map1);
7948 map1 = isl_map_apply_range(map1, map2);
7949 return isl_map_reverse(map1);
7950 error:
7951 isl_map_free(map1);
7952 isl_map_free(map2);
7953 return NULL;
7956 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7957 __isl_take isl_map *map2)
7959 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7962 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7963 __isl_take isl_map *map2)
7965 isl_space *dim_result;
7966 struct isl_map *result;
7967 int i, j;
7969 if (!map1 || !map2)
7970 goto error;
7972 dim_result = isl_space_join(isl_space_copy(map1->dim),
7973 isl_space_copy(map2->dim));
7975 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7976 if (!result)
7977 goto error;
7978 for (i = 0; i < map1->n; ++i)
7979 for (j = 0; j < map2->n; ++j) {
7980 result = isl_map_add_basic_map(result,
7981 isl_basic_map_apply_range(
7982 isl_basic_map_copy(map1->p[i]),
7983 isl_basic_map_copy(map2->p[j])));
7984 if (!result)
7985 goto error;
7987 isl_map_free(map1);
7988 isl_map_free(map2);
7989 if (result && result->n <= 1)
7990 ISL_F_SET(result, ISL_MAP_DISJOINT);
7991 return result;
7992 error:
7993 isl_map_free(map1);
7994 isl_map_free(map2);
7995 return NULL;
7998 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7999 __isl_take isl_map *map2)
8001 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8005 * returns range - domain
8007 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8009 isl_space *target_space;
8010 struct isl_basic_set *bset;
8011 unsigned dim;
8012 unsigned nparam;
8013 int i;
8015 if (!bmap)
8016 goto error;
8017 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8018 bmap->dim, isl_dim_out),
8019 goto error);
8020 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8021 dim = isl_basic_map_dim(bmap, isl_dim_in);
8022 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8023 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8024 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8025 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8026 for (i = 0; i < dim; ++i) {
8027 int j = isl_basic_map_alloc_equality(bmap);
8028 if (j < 0) {
8029 bmap = isl_basic_map_free(bmap);
8030 break;
8032 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8033 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8034 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8035 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8037 bset = isl_basic_map_domain(bmap);
8038 bset = isl_basic_set_reset_space(bset, target_space);
8039 return bset;
8040 error:
8041 isl_basic_map_free(bmap);
8042 return NULL;
8046 * returns range - domain
8048 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8050 int i;
8051 isl_space *dim;
8052 struct isl_set *result;
8054 if (!map)
8055 return NULL;
8057 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8058 map->dim, isl_dim_out),
8059 goto error);
8060 dim = isl_map_get_space(map);
8061 dim = isl_space_domain(dim);
8062 result = isl_set_alloc_space(dim, map->n, 0);
8063 if (!result)
8064 goto error;
8065 for (i = 0; i < map->n; ++i)
8066 result = isl_set_add_basic_set(result,
8067 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8068 isl_map_free(map);
8069 return result;
8070 error:
8071 isl_map_free(map);
8072 return NULL;
8076 * returns [domain -> range] -> range - domain
8078 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8079 __isl_take isl_basic_map *bmap)
8081 int i, k;
8082 isl_space *dim;
8083 isl_basic_map *domain;
8084 int nparam, n;
8085 unsigned total;
8087 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8088 bmap->dim, isl_dim_out))
8089 isl_die(bmap->ctx, isl_error_invalid,
8090 "domain and range don't match", goto error);
8092 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8093 n = isl_basic_map_dim(bmap, isl_dim_in);
8095 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
8096 domain = isl_basic_map_universe(dim);
8098 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8099 bmap = isl_basic_map_apply_range(bmap, domain);
8100 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8102 total = isl_basic_map_total_dim(bmap);
8104 for (i = 0; i < n; ++i) {
8105 k = isl_basic_map_alloc_equality(bmap);
8106 if (k < 0)
8107 goto error;
8108 isl_seq_clr(bmap->eq[k], 1 + total);
8109 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8110 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8111 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8114 bmap = isl_basic_map_gauss(bmap, NULL);
8115 return isl_basic_map_finalize(bmap);
8116 error:
8117 isl_basic_map_free(bmap);
8118 return NULL;
8122 * returns [domain -> range] -> range - domain
8124 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8126 int i;
8127 isl_space *domain_dim;
8129 if (!map)
8130 return NULL;
8132 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8133 map->dim, isl_dim_out))
8134 isl_die(map->ctx, isl_error_invalid,
8135 "domain and range don't match", goto error);
8137 map = isl_map_cow(map);
8138 if (!map)
8139 return NULL;
8141 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
8142 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8143 map->dim = isl_space_join(map->dim, domain_dim);
8144 if (!map->dim)
8145 goto error;
8146 for (i = 0; i < map->n; ++i) {
8147 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8148 if (!map->p[i])
8149 goto error;
8151 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8152 return map;
8153 error:
8154 isl_map_free(map);
8155 return NULL;
8158 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
8160 struct isl_basic_map *bmap;
8161 unsigned nparam;
8162 unsigned dim;
8163 int i;
8165 if (!dims)
8166 return NULL;
8168 nparam = dims->nparam;
8169 dim = dims->n_out;
8170 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
8171 if (!bmap)
8172 goto error;
8174 for (i = 0; i < dim; ++i) {
8175 int j = isl_basic_map_alloc_equality(bmap);
8176 if (j < 0)
8177 goto error;
8178 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8179 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8180 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
8182 return isl_basic_map_finalize(bmap);
8183 error:
8184 isl_basic_map_free(bmap);
8185 return NULL;
8188 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
8190 if (!dim)
8191 return NULL;
8192 if (dim->n_in != dim->n_out)
8193 isl_die(dim->ctx, isl_error_invalid,
8194 "number of input and output dimensions needs to be "
8195 "the same", goto error);
8196 return basic_map_identity(dim);
8197 error:
8198 isl_space_free(dim);
8199 return NULL;
8202 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8204 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8207 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8209 isl_space *dim = isl_set_get_space(set);
8210 isl_map *id;
8211 id = isl_map_identity(isl_space_map_from_set(dim));
8212 return isl_map_intersect_range(id, set);
8215 /* Construct a basic set with all set dimensions having only non-negative
8216 * values.
8218 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8219 __isl_take isl_space *space)
8221 int i;
8222 unsigned nparam;
8223 unsigned dim;
8224 struct isl_basic_set *bset;
8226 if (!space)
8227 return NULL;
8228 nparam = space->nparam;
8229 dim = space->n_out;
8230 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8231 if (!bset)
8232 return NULL;
8233 for (i = 0; i < dim; ++i) {
8234 int k = isl_basic_set_alloc_inequality(bset);
8235 if (k < 0)
8236 goto error;
8237 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8238 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8240 return bset;
8241 error:
8242 isl_basic_set_free(bset);
8243 return NULL;
8246 /* Construct the half-space x_pos >= 0.
8248 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
8249 int pos)
8251 int k;
8252 isl_basic_set *nonneg;
8254 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8255 k = isl_basic_set_alloc_inequality(nonneg);
8256 if (k < 0)
8257 goto error;
8258 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8259 isl_int_set_si(nonneg->ineq[k][pos], 1);
8261 return isl_basic_set_finalize(nonneg);
8262 error:
8263 isl_basic_set_free(nonneg);
8264 return NULL;
8267 /* Construct the half-space x_pos <= -1.
8269 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
8271 int k;
8272 isl_basic_set *neg;
8274 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8275 k = isl_basic_set_alloc_inequality(neg);
8276 if (k < 0)
8277 goto error;
8278 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8279 isl_int_set_si(neg->ineq[k][0], -1);
8280 isl_int_set_si(neg->ineq[k][pos], -1);
8282 return isl_basic_set_finalize(neg);
8283 error:
8284 isl_basic_set_free(neg);
8285 return NULL;
8288 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8289 enum isl_dim_type type, unsigned first, unsigned n)
8291 int i;
8292 unsigned offset;
8293 isl_basic_set *nonneg;
8294 isl_basic_set *neg;
8296 if (!set)
8297 return NULL;
8298 if (n == 0)
8299 return set;
8301 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8303 offset = pos(set->dim, type);
8304 for (i = 0; i < n; ++i) {
8305 nonneg = nonneg_halfspace(isl_set_get_space(set),
8306 offset + first + i);
8307 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8309 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8312 return set;
8313 error:
8314 isl_set_free(set);
8315 return NULL;
8318 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8319 int len,
8320 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8321 void *user)
8323 isl_set *half;
8325 if (!set)
8326 return isl_stat_error;
8327 if (isl_set_plain_is_empty(set)) {
8328 isl_set_free(set);
8329 return isl_stat_ok;
8331 if (first == len)
8332 return fn(set, signs, user);
8334 signs[first] = 1;
8335 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8336 1 + first));
8337 half = isl_set_intersect(half, isl_set_copy(set));
8338 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8339 goto error;
8341 signs[first] = -1;
8342 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8343 1 + first));
8344 half = isl_set_intersect(half, set);
8345 return foreach_orthant(half, signs, first + 1, len, fn, user);
8346 error:
8347 isl_set_free(set);
8348 return isl_stat_error;
8351 /* Call "fn" on the intersections of "set" with each of the orthants
8352 * (except for obviously empty intersections). The orthant is identified
8353 * by the signs array, with each entry having value 1 or -1 according
8354 * to the sign of the corresponding variable.
8356 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8357 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8358 void *user)
8360 unsigned nparam;
8361 unsigned nvar;
8362 int *signs;
8363 isl_stat r;
8365 if (!set)
8366 return isl_stat_error;
8367 if (isl_set_plain_is_empty(set))
8368 return isl_stat_ok;
8370 nparam = isl_set_dim(set, isl_dim_param);
8371 nvar = isl_set_dim(set, isl_dim_set);
8373 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8375 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8376 fn, user);
8378 free(signs);
8380 return r;
8383 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8385 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8388 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8389 __isl_keep isl_basic_map *bmap2)
8391 int is_subset;
8392 struct isl_map *map1;
8393 struct isl_map *map2;
8395 if (!bmap1 || !bmap2)
8396 return isl_bool_error;
8398 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8399 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8401 is_subset = isl_map_is_subset(map1, map2);
8403 isl_map_free(map1);
8404 isl_map_free(map2);
8406 return is_subset;
8409 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8410 __isl_keep isl_basic_set *bset2)
8412 return isl_basic_map_is_subset(bset1, bset2);
8415 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8416 __isl_keep isl_basic_map *bmap2)
8418 isl_bool is_subset;
8420 if (!bmap1 || !bmap2)
8421 return isl_bool_error;
8422 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8423 if (is_subset != isl_bool_true)
8424 return is_subset;
8425 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8426 return is_subset;
8429 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8430 __isl_keep isl_basic_set *bset2)
8432 return isl_basic_map_is_equal(
8433 bset_to_bmap(bset1), bset_to_bmap(bset2));
8436 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8438 int i;
8439 int is_empty;
8441 if (!map)
8442 return isl_bool_error;
8443 for (i = 0; i < map->n; ++i) {
8444 is_empty = isl_basic_map_is_empty(map->p[i]);
8445 if (is_empty < 0)
8446 return isl_bool_error;
8447 if (!is_empty)
8448 return isl_bool_false;
8450 return isl_bool_true;
8453 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8455 return map ? map->n == 0 : isl_bool_error;
8458 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8460 return set ? set->n == 0 : isl_bool_error;
8463 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8465 return isl_map_is_empty(set_to_map(set));
8468 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8469 __isl_keep isl_map *map2)
8471 if (!map1 || !map2)
8472 return isl_bool_error;
8474 return isl_space_is_equal(map1->dim, map2->dim);
8477 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8478 __isl_keep isl_set *set2)
8480 if (!set1 || !set2)
8481 return isl_bool_error;
8483 return isl_space_is_equal(set1->dim, set2->dim);
8486 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8488 isl_bool is_subset;
8490 if (!map1 || !map2)
8491 return isl_bool_error;
8492 is_subset = isl_map_is_subset(map1, map2);
8493 if (is_subset != isl_bool_true)
8494 return is_subset;
8495 is_subset = isl_map_is_subset(map2, map1);
8496 return is_subset;
8499 /* Is "map1" equal to "map2"?
8501 * First check if they are obviously equal.
8502 * If not, then perform a more detailed analysis.
8504 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8506 isl_bool equal;
8508 equal = isl_map_plain_is_equal(map1, map2);
8509 if (equal < 0 || equal)
8510 return equal;
8511 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8514 isl_bool isl_basic_map_is_strict_subset(
8515 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8517 isl_bool is_subset;
8519 if (!bmap1 || !bmap2)
8520 return isl_bool_error;
8521 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8522 if (is_subset != isl_bool_true)
8523 return is_subset;
8524 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8525 if (is_subset == isl_bool_error)
8526 return is_subset;
8527 return !is_subset;
8530 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8531 __isl_keep isl_map *map2)
8533 isl_bool is_subset;
8535 if (!map1 || !map2)
8536 return isl_bool_error;
8537 is_subset = isl_map_is_subset(map1, map2);
8538 if (is_subset != isl_bool_true)
8539 return is_subset;
8540 is_subset = isl_map_is_subset(map2, map1);
8541 if (is_subset == isl_bool_error)
8542 return is_subset;
8543 return !is_subset;
8546 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8547 __isl_keep isl_set *set2)
8549 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8552 /* Is "bmap" obviously equal to the universe with the same space?
8554 * That is, does it not have any constraints?
8556 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8558 if (!bmap)
8559 return isl_bool_error;
8560 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8563 /* Is "bset" obviously equal to the universe with the same space?
8565 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8567 return isl_basic_map_plain_is_universe(bset);
8570 /* If "c" does not involve any existentially quantified variables,
8571 * then set *univ to false and abort
8573 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8575 isl_bool *univ = user;
8576 unsigned n;
8578 n = isl_constraint_dim(c, isl_dim_div);
8579 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8580 isl_constraint_free(c);
8581 if (*univ < 0 || !*univ)
8582 return isl_stat_error;
8583 return isl_stat_ok;
8586 /* Is "bmap" equal to the universe with the same space?
8588 * First check if it is obviously equal to the universe.
8589 * If not and if there are any constraints not involving
8590 * existentially quantified variables, then it is certainly
8591 * not equal to the universe.
8592 * Otherwise, check if the universe is a subset of "bmap".
8594 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8596 isl_bool univ;
8597 isl_basic_map *test;
8599 univ = isl_basic_map_plain_is_universe(bmap);
8600 if (univ < 0 || univ)
8601 return univ;
8602 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8603 return isl_bool_false;
8604 univ = isl_bool_true;
8605 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8606 univ)
8607 return isl_bool_error;
8608 if (univ < 0 || !univ)
8609 return univ;
8610 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8611 univ = isl_basic_map_is_subset(test, bmap);
8612 isl_basic_map_free(test);
8613 return univ;
8616 /* Is "bset" equal to the universe with the same space?
8618 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8620 return isl_basic_map_is_universe(bset);
8623 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8625 int i;
8627 if (!map)
8628 return isl_bool_error;
8630 for (i = 0; i < map->n; ++i) {
8631 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8632 if (r < 0 || r)
8633 return r;
8636 return isl_bool_false;
8639 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8641 return isl_map_plain_is_universe(set_to_map(set));
8644 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8646 struct isl_basic_set *bset = NULL;
8647 struct isl_vec *sample = NULL;
8648 isl_bool empty, non_empty;
8650 if (!bmap)
8651 return isl_bool_error;
8653 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8654 return isl_bool_true;
8656 if (isl_basic_map_plain_is_universe(bmap))
8657 return isl_bool_false;
8659 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8660 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8661 copy = isl_basic_map_remove_redundancies(copy);
8662 empty = isl_basic_map_plain_is_empty(copy);
8663 isl_basic_map_free(copy);
8664 return empty;
8667 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8668 if (non_empty < 0)
8669 return isl_bool_error;
8670 if (non_empty)
8671 return isl_bool_false;
8672 isl_vec_free(bmap->sample);
8673 bmap->sample = NULL;
8674 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8675 if (!bset)
8676 return isl_bool_error;
8677 sample = isl_basic_set_sample_vec(bset);
8678 if (!sample)
8679 return isl_bool_error;
8680 empty = sample->size == 0;
8681 isl_vec_free(bmap->sample);
8682 bmap->sample = sample;
8683 if (empty)
8684 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8686 return empty;
8689 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8691 if (!bmap)
8692 return isl_bool_error;
8693 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8696 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8698 if (!bset)
8699 return isl_bool_error;
8700 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8703 /* Is "bmap" known to be non-empty?
8705 * That is, is the cached sample still valid?
8707 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8709 unsigned total;
8711 if (!bmap)
8712 return isl_bool_error;
8713 if (!bmap->sample)
8714 return isl_bool_false;
8715 total = 1 + isl_basic_map_total_dim(bmap);
8716 if (bmap->sample->size != total)
8717 return isl_bool_false;
8718 return isl_basic_map_contains(bmap, bmap->sample);
8721 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8723 return isl_basic_map_is_empty(bset_to_bmap(bset));
8726 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8727 __isl_take isl_basic_map *bmap2)
8729 struct isl_map *map;
8730 if (!bmap1 || !bmap2)
8731 goto error;
8733 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8735 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8736 if (!map)
8737 goto error;
8738 map = isl_map_add_basic_map(map, bmap1);
8739 map = isl_map_add_basic_map(map, bmap2);
8740 return map;
8741 error:
8742 isl_basic_map_free(bmap1);
8743 isl_basic_map_free(bmap2);
8744 return NULL;
8747 struct isl_set *isl_basic_set_union(
8748 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8750 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8751 bset_to_bmap(bset2)));
8754 /* Order divs such that any div only depends on previous divs */
8755 __isl_give isl_basic_map *isl_basic_map_order_divs(
8756 __isl_take isl_basic_map *bmap)
8758 int i;
8759 unsigned off;
8761 if (!bmap)
8762 return NULL;
8764 off = isl_space_dim(bmap->dim, isl_dim_all);
8766 for (i = 0; i < bmap->n_div; ++i) {
8767 int pos;
8768 if (isl_int_is_zero(bmap->div[i][0]))
8769 continue;
8770 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8771 bmap->n_div-i);
8772 if (pos == -1)
8773 continue;
8774 if (pos == 0)
8775 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8776 "integer division depends on itself",
8777 return isl_basic_map_free(bmap));
8778 isl_basic_map_swap_div(bmap, i, i + pos);
8779 --i;
8781 return bmap;
8784 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8786 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8789 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8791 int i;
8793 if (!map)
8794 return 0;
8796 for (i = 0; i < map->n; ++i) {
8797 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8798 if (!map->p[i])
8799 goto error;
8802 return map;
8803 error:
8804 isl_map_free(map);
8805 return NULL;
8808 /* Sort the local variables of "bset".
8810 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8811 __isl_take isl_basic_set *bset)
8813 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8816 /* Apply the expansion computed by isl_merge_divs.
8817 * The expansion itself is given by "exp" while the resulting
8818 * list of divs is given by "div".
8820 * Move the integer divisions of "bmap" into the right position
8821 * according to "exp" and then introduce the additional integer
8822 * divisions, adding div constraints.
8823 * The moving should be done first to avoid moving coefficients
8824 * in the definitions of the extra integer divisions.
8826 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8827 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8829 int i, j;
8830 int n_div;
8832 bmap = isl_basic_map_cow(bmap);
8833 if (!bmap || !div)
8834 goto error;
8836 if (div->n_row < bmap->n_div)
8837 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8838 "not an expansion", goto error);
8840 n_div = bmap->n_div;
8841 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8842 div->n_row - n_div, 0,
8843 2 * (div->n_row - n_div));
8845 for (i = n_div; i < div->n_row; ++i)
8846 if (isl_basic_map_alloc_div(bmap) < 0)
8847 goto error;
8849 for (j = n_div - 1; j >= 0; --j) {
8850 if (exp[j] == j)
8851 break;
8852 isl_basic_map_swap_div(bmap, j, exp[j]);
8854 j = 0;
8855 for (i = 0; i < div->n_row; ++i) {
8856 if (j < n_div && exp[j] == i) {
8857 j++;
8858 } else {
8859 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8860 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8861 continue;
8862 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8863 goto error;
8867 isl_mat_free(div);
8868 return bmap;
8869 error:
8870 isl_basic_map_free(bmap);
8871 isl_mat_free(div);
8872 return NULL;
8875 /* Apply the expansion computed by isl_merge_divs.
8876 * The expansion itself is given by "exp" while the resulting
8877 * list of divs is given by "div".
8879 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8880 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8882 return isl_basic_map_expand_divs(bset, div, exp);
8885 /* Look for a div in dst that corresponds to the div "div" in src.
8886 * The divs before "div" in src and dst are assumed to be the same.
8888 * Returns -1 if no corresponding div was found and the position
8889 * of the corresponding div in dst otherwise.
8891 static int find_div(__isl_keep isl_basic_map *dst,
8892 __isl_keep isl_basic_map *src, unsigned div)
8894 int i;
8896 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8898 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8899 for (i = div; i < dst->n_div; ++i)
8900 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8901 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8902 dst->n_div - div) == -1)
8903 return i;
8904 return -1;
8907 /* Align the divs of "dst" to those of "src", adding divs from "src"
8908 * if needed. That is, make sure that the first src->n_div divs
8909 * of the result are equal to those of src.
8911 * The result is not finalized as by design it will have redundant
8912 * divs if any divs from "src" were copied.
8914 __isl_give isl_basic_map *isl_basic_map_align_divs(
8915 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8917 int i;
8918 int known, extended;
8919 unsigned total;
8921 if (!dst || !src)
8922 return isl_basic_map_free(dst);
8924 if (src->n_div == 0)
8925 return dst;
8927 known = isl_basic_map_divs_known(src);
8928 if (known < 0)
8929 return isl_basic_map_free(dst);
8930 if (!known)
8931 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8932 "some src divs are unknown",
8933 return isl_basic_map_free(dst));
8935 src = isl_basic_map_order_divs(src);
8937 extended = 0;
8938 total = isl_space_dim(src->dim, isl_dim_all);
8939 for (i = 0; i < src->n_div; ++i) {
8940 int j = find_div(dst, src, i);
8941 if (j < 0) {
8942 if (!extended) {
8943 int extra = src->n_div - i;
8944 dst = isl_basic_map_cow(dst);
8945 if (!dst)
8946 return NULL;
8947 dst = isl_basic_map_extend_space(dst,
8948 isl_space_copy(dst->dim),
8949 extra, 0, 2 * extra);
8950 extended = 1;
8952 j = isl_basic_map_alloc_div(dst);
8953 if (j < 0)
8954 return isl_basic_map_free(dst);
8955 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8956 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8957 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8958 return isl_basic_map_free(dst);
8960 if (j != i)
8961 isl_basic_map_swap_div(dst, i, j);
8963 return dst;
8966 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8968 int i;
8970 if (!map)
8971 return NULL;
8972 if (map->n == 0)
8973 return map;
8974 map = isl_map_compute_divs(map);
8975 map = isl_map_cow(map);
8976 if (!map)
8977 return NULL;
8979 for (i = 1; i < map->n; ++i)
8980 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8981 for (i = 1; i < map->n; ++i) {
8982 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8983 if (!map->p[i])
8984 return isl_map_free(map);
8987 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8988 return map;
8991 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8993 return isl_map_align_divs_internal(map);
8996 struct isl_set *isl_set_align_divs(struct isl_set *set)
8998 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9001 /* Align the divs of the basic maps in "map" to those
9002 * of the basic maps in "list", as well as to the other basic maps in "map".
9003 * The elements in "list" are assumed to have known divs.
9005 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9006 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9008 int i, n;
9010 map = isl_map_compute_divs(map);
9011 map = isl_map_cow(map);
9012 if (!map || !list)
9013 return isl_map_free(map);
9014 if (map->n == 0)
9015 return map;
9017 n = isl_basic_map_list_n_basic_map(list);
9018 for (i = 0; i < n; ++i) {
9019 isl_basic_map *bmap;
9021 bmap = isl_basic_map_list_get_basic_map(list, i);
9022 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9023 isl_basic_map_free(bmap);
9025 if (!map->p[0])
9026 return isl_map_free(map);
9028 return isl_map_align_divs_internal(map);
9031 /* Align the divs of each element of "list" to those of "bmap".
9032 * Both "bmap" and the elements of "list" are assumed to have known divs.
9034 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9035 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9037 int i, n;
9039 if (!list || !bmap)
9040 return isl_basic_map_list_free(list);
9042 n = isl_basic_map_list_n_basic_map(list);
9043 for (i = 0; i < n; ++i) {
9044 isl_basic_map *bmap_i;
9046 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9047 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9048 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9051 return list;
9054 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9055 __isl_take isl_map *map)
9057 isl_bool ok;
9059 ok = isl_map_compatible_domain(map, set);
9060 if (ok < 0)
9061 goto error;
9062 if (!ok)
9063 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9064 "incompatible spaces", goto error);
9065 map = isl_map_intersect_domain(map, set);
9066 set = isl_map_range(map);
9067 return set;
9068 error:
9069 isl_set_free(set);
9070 isl_map_free(map);
9071 return NULL;
9074 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9075 __isl_take isl_map *map)
9077 return isl_map_align_params_map_map_and(set, map, &set_apply);
9080 /* There is no need to cow as removing empty parts doesn't change
9081 * the meaning of the set.
9083 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9085 int i;
9087 if (!map)
9088 return NULL;
9090 for (i = map->n - 1; i >= 0; --i)
9091 remove_if_empty(map, i);
9093 return map;
9096 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9098 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9101 /* Given two basic sets bset1 and bset2, compute the maximal difference
9102 * between the values of dimension pos in bset1 and those in bset2
9103 * for any common value of the parameters and dimensions preceding pos.
9105 static enum isl_lp_result basic_set_maximal_difference_at(
9106 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9107 int pos, isl_int *opt)
9109 isl_basic_map *bmap1;
9110 isl_basic_map *bmap2;
9111 struct isl_ctx *ctx;
9112 struct isl_vec *obj;
9113 unsigned total;
9114 unsigned nparam;
9115 unsigned dim1;
9116 enum isl_lp_result res;
9118 if (!bset1 || !bset2)
9119 return isl_lp_error;
9121 nparam = isl_basic_set_n_param(bset1);
9122 dim1 = isl_basic_set_n_dim(bset1);
9124 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9125 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9126 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9127 isl_dim_out, 0, pos);
9128 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9129 isl_dim_out, 0, pos);
9130 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
9131 if (!bmap1)
9132 return isl_lp_error;
9134 total = isl_basic_map_total_dim(bmap1);
9135 ctx = bmap1->ctx;
9136 obj = isl_vec_alloc(ctx, 1 + total);
9137 if (!obj)
9138 goto error;
9139 isl_seq_clr(obj->block.data, 1 + total);
9140 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9141 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9142 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9143 opt, NULL, NULL);
9144 isl_basic_map_free(bmap1);
9145 isl_vec_free(obj);
9146 return res;
9147 error:
9148 isl_basic_map_free(bmap1);
9149 return isl_lp_error;
9152 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9153 * for any common value of the parameters and dimensions preceding pos
9154 * in both basic sets, the values of dimension pos in bset1 are
9155 * smaller or larger than those in bset2.
9157 * Returns
9158 * 1 if bset1 follows bset2
9159 * -1 if bset1 precedes bset2
9160 * 0 if bset1 and bset2 are incomparable
9161 * -2 if some error occurred.
9163 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
9164 struct isl_basic_set *bset2, int pos)
9166 isl_int opt;
9167 enum isl_lp_result res;
9168 int cmp;
9170 isl_int_init(opt);
9172 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9174 if (res == isl_lp_empty)
9175 cmp = 0;
9176 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9177 res == isl_lp_unbounded)
9178 cmp = 1;
9179 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9180 cmp = -1;
9181 else
9182 cmp = -2;
9184 isl_int_clear(opt);
9185 return cmp;
9188 /* Given two basic sets bset1 and bset2, check whether
9189 * for any common value of the parameters and dimensions preceding pos
9190 * there is a value of dimension pos in bset1 that is larger
9191 * than a value of the same dimension in bset2.
9193 * Return
9194 * 1 if there exists such a pair
9195 * 0 if there is no such pair, but there is a pair of equal values
9196 * -1 otherwise
9197 * -2 if some error occurred.
9199 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9200 __isl_keep isl_basic_set *bset2, int pos)
9202 isl_int opt;
9203 enum isl_lp_result res;
9204 int cmp;
9206 isl_int_init(opt);
9208 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9210 if (res == isl_lp_empty)
9211 cmp = -1;
9212 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9213 res == isl_lp_unbounded)
9214 cmp = 1;
9215 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9216 cmp = -1;
9217 else if (res == isl_lp_ok)
9218 cmp = 0;
9219 else
9220 cmp = -2;
9222 isl_int_clear(opt);
9223 return cmp;
9226 /* Given two sets set1 and set2, check whether
9227 * for any common value of the parameters and dimensions preceding pos
9228 * there is a value of dimension pos in set1 that is larger
9229 * than a value of the same dimension in set2.
9231 * Return
9232 * 1 if there exists such a pair
9233 * 0 if there is no such pair, but there is a pair of equal values
9234 * -1 otherwise
9235 * -2 if some error occurred.
9237 int isl_set_follows_at(__isl_keep isl_set *set1,
9238 __isl_keep isl_set *set2, int pos)
9240 int i, j;
9241 int follows = -1;
9243 if (!set1 || !set2)
9244 return -2;
9246 for (i = 0; i < set1->n; ++i)
9247 for (j = 0; j < set2->n; ++j) {
9248 int f;
9249 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9250 if (f == 1 || f == -2)
9251 return f;
9252 if (f > follows)
9253 follows = f;
9256 return follows;
9259 static isl_bool isl_basic_map_plain_has_fixed_var(
9260 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9262 int i;
9263 int d;
9264 unsigned total;
9266 if (!bmap)
9267 return isl_bool_error;
9268 total = isl_basic_map_total_dim(bmap);
9269 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9270 for (; d+1 > pos; --d)
9271 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9272 break;
9273 if (d != pos)
9274 continue;
9275 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9276 return isl_bool_false;
9277 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9278 return isl_bool_false;
9279 if (!isl_int_is_one(bmap->eq[i][1+d]))
9280 return isl_bool_false;
9281 if (val)
9282 isl_int_neg(*val, bmap->eq[i][0]);
9283 return isl_bool_true;
9285 return isl_bool_false;
9288 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9289 unsigned pos, isl_int *val)
9291 int i;
9292 isl_int v;
9293 isl_int tmp;
9294 isl_bool fixed;
9296 if (!map)
9297 return isl_bool_error;
9298 if (map->n == 0)
9299 return isl_bool_false;
9300 if (map->n == 1)
9301 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9302 isl_int_init(v);
9303 isl_int_init(tmp);
9304 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9305 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9306 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9307 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9308 fixed = isl_bool_false;
9310 if (val)
9311 isl_int_set(*val, v);
9312 isl_int_clear(tmp);
9313 isl_int_clear(v);
9314 return fixed;
9317 static isl_bool isl_basic_set_plain_has_fixed_var(
9318 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9320 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9321 pos, val);
9324 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9325 enum isl_dim_type type, unsigned pos, isl_int *val)
9327 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9328 return isl_bool_error;
9329 return isl_basic_map_plain_has_fixed_var(bmap,
9330 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9333 /* If "bmap" 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_basic_map_plain_get_val_if_fixed(
9338 __isl_keep isl_basic_map *bmap,
9339 enum isl_dim_type type, unsigned pos)
9341 isl_ctx *ctx;
9342 isl_val *v;
9343 isl_bool fixed;
9345 if (!bmap)
9346 return NULL;
9347 ctx = isl_basic_map_get_ctx(bmap);
9348 v = isl_val_alloc(ctx);
9349 if (!v)
9350 return NULL;
9351 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9352 if (fixed < 0)
9353 return isl_val_free(v);
9354 if (fixed) {
9355 isl_int_set_si(v->d, 1);
9356 return v;
9358 isl_val_free(v);
9359 return isl_val_nan(ctx);
9362 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9363 enum isl_dim_type type, unsigned pos, isl_int *val)
9365 if (pos >= isl_map_dim(map, type))
9366 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9367 "position out of bounds", return isl_bool_error);
9368 return isl_map_plain_has_fixed_var(map,
9369 map_offset(map, type) - 1 + pos, val);
9372 /* If "map" obviously lies on a hyperplane where the given dimension
9373 * has a fixed value, then return that value.
9374 * Otherwise return NaN.
9376 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9377 enum isl_dim_type type, unsigned pos)
9379 isl_ctx *ctx;
9380 isl_val *v;
9381 isl_bool fixed;
9383 if (!map)
9384 return NULL;
9385 ctx = isl_map_get_ctx(map);
9386 v = isl_val_alloc(ctx);
9387 if (!v)
9388 return NULL;
9389 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9390 if (fixed < 0)
9391 return isl_val_free(v);
9392 if (fixed) {
9393 isl_int_set_si(v->d, 1);
9394 return v;
9396 isl_val_free(v);
9397 return isl_val_nan(ctx);
9400 /* If "set" obviously lies on a hyperplane where the given dimension
9401 * has a fixed value, then return that value.
9402 * Otherwise return NaN.
9404 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9405 enum isl_dim_type type, unsigned pos)
9407 return isl_map_plain_get_val_if_fixed(set, type, pos);
9410 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
9411 enum isl_dim_type type, unsigned pos, isl_int *val)
9413 return isl_map_plain_is_fixed(set, type, pos, val);
9416 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9417 * then return this fixed value in *val.
9419 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9420 unsigned dim, isl_int *val)
9422 return isl_basic_set_plain_has_fixed_var(bset,
9423 isl_basic_set_n_param(bset) + dim, val);
9426 /* Return -1 if the constraint "c1" should be sorted before "c2"
9427 * and 1 if it should be sorted after "c2".
9428 * Return 0 if the two constraints are the same (up to the constant term).
9430 * In particular, if a constraint involves later variables than another
9431 * then it is sorted after this other constraint.
9432 * uset_gist depends on constraints without existentially quantified
9433 * variables sorting first.
9435 * For constraints that have the same latest variable, those
9436 * with the same coefficient for this latest variable (first in absolute value
9437 * and then in actual value) are grouped together.
9438 * This is useful for detecting pairs of constraints that can
9439 * be chained in their printed representation.
9441 * Finally, within a group, constraints are sorted according to
9442 * their coefficients (excluding the constant term).
9444 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9446 isl_int **c1 = (isl_int **) p1;
9447 isl_int **c2 = (isl_int **) p2;
9448 int l1, l2;
9449 unsigned size = *(unsigned *) arg;
9450 int cmp;
9452 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9453 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9455 if (l1 != l2)
9456 return l1 - l2;
9458 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9459 if (cmp != 0)
9460 return cmp;
9461 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9462 if (cmp != 0)
9463 return -cmp;
9465 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9468 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9469 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9470 * and 0 if the two constraints are the same (up to the constant term).
9472 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9473 isl_int *c1, isl_int *c2)
9475 unsigned total;
9477 if (!bmap)
9478 return -2;
9479 total = isl_basic_map_total_dim(bmap);
9480 return sort_constraint_cmp(&c1, &c2, &total);
9483 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9484 __isl_take isl_basic_map *bmap)
9486 unsigned total;
9488 if (!bmap)
9489 return NULL;
9490 if (bmap->n_ineq == 0)
9491 return bmap;
9492 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9493 return bmap;
9494 total = isl_basic_map_total_dim(bmap);
9495 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9496 &sort_constraint_cmp, &total) < 0)
9497 return isl_basic_map_free(bmap);
9498 return bmap;
9501 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9502 __isl_take isl_basic_set *bset)
9504 isl_basic_map *bmap = bset_to_bmap(bset);
9505 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9508 __isl_give isl_basic_map *isl_basic_map_normalize(
9509 __isl_take isl_basic_map *bmap)
9511 if (!bmap)
9512 return NULL;
9513 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9514 return bmap;
9515 bmap = isl_basic_map_remove_redundancies(bmap);
9516 bmap = isl_basic_map_sort_constraints(bmap);
9517 if (bmap)
9518 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9519 return bmap;
9521 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9522 __isl_keep isl_basic_map *bmap2)
9524 int i, cmp;
9525 unsigned total;
9526 isl_space *space1, *space2;
9528 if (!bmap1 || !bmap2)
9529 return -1;
9531 if (bmap1 == bmap2)
9532 return 0;
9533 space1 = isl_basic_map_peek_space(bmap1);
9534 space2 = isl_basic_map_peek_space(bmap2);
9535 cmp = isl_space_cmp(space1, space2);
9536 if (cmp)
9537 return cmp;
9538 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9539 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9540 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9541 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9542 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9543 return 0;
9544 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9545 return 1;
9546 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9547 return -1;
9548 if (bmap1->n_eq != bmap2->n_eq)
9549 return bmap1->n_eq - bmap2->n_eq;
9550 if (bmap1->n_ineq != bmap2->n_ineq)
9551 return bmap1->n_ineq - bmap2->n_ineq;
9552 if (bmap1->n_div != bmap2->n_div)
9553 return bmap1->n_div - bmap2->n_div;
9554 total = isl_basic_map_total_dim(bmap1);
9555 for (i = 0; i < bmap1->n_eq; ++i) {
9556 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9557 if (cmp)
9558 return cmp;
9560 for (i = 0; i < bmap1->n_ineq; ++i) {
9561 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9562 if (cmp)
9563 return cmp;
9565 for (i = 0; i < bmap1->n_div; ++i) {
9566 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9567 if (cmp)
9568 return cmp;
9570 return 0;
9573 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9574 __isl_keep isl_basic_set *bset2)
9576 return isl_basic_map_plain_cmp(bset1, bset2);
9579 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9581 int i, cmp;
9583 if (set1 == set2)
9584 return 0;
9585 if (set1->n != set2->n)
9586 return set1->n - set2->n;
9588 for (i = 0; i < set1->n; ++i) {
9589 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9590 if (cmp)
9591 return cmp;
9594 return 0;
9597 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9598 __isl_keep isl_basic_map *bmap2)
9600 if (!bmap1 || !bmap2)
9601 return isl_bool_error;
9602 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9605 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9606 __isl_keep isl_basic_set *bset2)
9608 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9609 bset_to_bmap(bset2));
9612 static int qsort_bmap_cmp(const void *p1, const void *p2)
9614 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9615 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9617 return isl_basic_map_plain_cmp(bmap1, bmap2);
9620 /* Sort the basic maps of "map" and remove duplicate basic maps.
9622 * While removing basic maps, we make sure that the basic maps remain
9623 * sorted because isl_map_normalize expects the basic maps of the result
9624 * to be sorted.
9626 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9628 int i, j;
9630 map = isl_map_remove_empty_parts(map);
9631 if (!map)
9632 return NULL;
9633 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9634 for (i = map->n - 1; i >= 1; --i) {
9635 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9636 continue;
9637 isl_basic_map_free(map->p[i-1]);
9638 for (j = i; j < map->n; ++j)
9639 map->p[j - 1] = map->p[j];
9640 map->n--;
9643 return map;
9646 /* Remove obvious duplicates among the basic maps of "map".
9648 * Unlike isl_map_normalize, this function does not remove redundant
9649 * constraints and only removes duplicates that have exactly the same
9650 * constraints in the input. It does sort the constraints and
9651 * the basic maps to ease the detection of duplicates.
9653 * If "map" has already been normalized or if the basic maps are
9654 * disjoint, then there can be no duplicates.
9656 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9658 int i;
9659 isl_basic_map *bmap;
9661 if (!map)
9662 return NULL;
9663 if (map->n <= 1)
9664 return map;
9665 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9666 return map;
9667 for (i = 0; i < map->n; ++i) {
9668 bmap = isl_basic_map_copy(map->p[i]);
9669 bmap = isl_basic_map_sort_constraints(bmap);
9670 if (!bmap)
9671 return isl_map_free(map);
9672 isl_basic_map_free(map->p[i]);
9673 map->p[i] = bmap;
9676 map = sort_and_remove_duplicates(map);
9677 return map;
9680 /* We normalize in place, but if anything goes wrong we need
9681 * to return NULL, so we need to make sure we don't change the
9682 * meaning of any possible other copies of map.
9684 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9686 int i;
9687 struct isl_basic_map *bmap;
9689 if (!map)
9690 return NULL;
9691 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9692 return map;
9693 for (i = 0; i < map->n; ++i) {
9694 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9695 if (!bmap)
9696 goto error;
9697 isl_basic_map_free(map->p[i]);
9698 map->p[i] = bmap;
9701 map = sort_and_remove_duplicates(map);
9702 if (map)
9703 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9704 return map;
9705 error:
9706 isl_map_free(map);
9707 return NULL;
9710 struct isl_set *isl_set_normalize(struct isl_set *set)
9712 return set_from_map(isl_map_normalize(set_to_map(set)));
9715 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9716 __isl_keep isl_map *map2)
9718 int i;
9719 isl_bool equal;
9721 if (!map1 || !map2)
9722 return isl_bool_error;
9724 if (map1 == map2)
9725 return isl_bool_true;
9726 if (!isl_space_is_equal(map1->dim, map2->dim))
9727 return isl_bool_false;
9729 map1 = isl_map_copy(map1);
9730 map2 = isl_map_copy(map2);
9731 map1 = isl_map_normalize(map1);
9732 map2 = isl_map_normalize(map2);
9733 if (!map1 || !map2)
9734 goto error;
9735 equal = map1->n == map2->n;
9736 for (i = 0; equal && i < map1->n; ++i) {
9737 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9738 if (equal < 0)
9739 goto error;
9741 isl_map_free(map1);
9742 isl_map_free(map2);
9743 return equal;
9744 error:
9745 isl_map_free(map1);
9746 isl_map_free(map2);
9747 return isl_bool_error;
9750 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9751 __isl_keep isl_set *set2)
9753 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9756 /* Return the basic maps in "map" as a list.
9758 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9759 __isl_keep isl_map *map)
9761 int i;
9762 isl_ctx *ctx;
9763 isl_basic_map_list *list;
9765 if (!map)
9766 return NULL;
9767 ctx = isl_map_get_ctx(map);
9768 list = isl_basic_map_list_alloc(ctx, map->n);
9770 for (i = 0; i < map->n; ++i) {
9771 isl_basic_map *bmap;
9773 bmap = isl_basic_map_copy(map->p[i]);
9774 list = isl_basic_map_list_add(list, bmap);
9777 return list;
9780 /* Return the intersection of the elements in the non-empty list "list".
9781 * All elements are assumed to live in the same space.
9783 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9784 __isl_take isl_basic_map_list *list)
9786 int i, n;
9787 isl_basic_map *bmap;
9789 if (!list)
9790 return NULL;
9791 n = isl_basic_map_list_n_basic_map(list);
9792 if (n < 1)
9793 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9794 "expecting non-empty list", goto error);
9796 bmap = isl_basic_map_list_get_basic_map(list, 0);
9797 for (i = 1; i < n; ++i) {
9798 isl_basic_map *bmap_i;
9800 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9801 bmap = isl_basic_map_intersect(bmap, bmap_i);
9804 isl_basic_map_list_free(list);
9805 return bmap;
9806 error:
9807 isl_basic_map_list_free(list);
9808 return NULL;
9811 /* Return the intersection of the elements in the non-empty list "list".
9812 * All elements are assumed to live in the same space.
9814 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9815 __isl_take isl_basic_set_list *list)
9817 return isl_basic_map_list_intersect(list);
9820 /* Return the union of the elements of "list".
9821 * The list is required to have at least one element.
9823 __isl_give isl_set *isl_basic_set_list_union(
9824 __isl_take isl_basic_set_list *list)
9826 int i, n;
9827 isl_space *space;
9828 isl_basic_set *bset;
9829 isl_set *set;
9831 if (!list)
9832 return NULL;
9833 n = isl_basic_set_list_n_basic_set(list);
9834 if (n < 1)
9835 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9836 "expecting non-empty list", goto error);
9838 bset = isl_basic_set_list_get_basic_set(list, 0);
9839 space = isl_basic_set_get_space(bset);
9840 isl_basic_set_free(bset);
9842 set = isl_set_alloc_space(space, n, 0);
9843 for (i = 0; i < n; ++i) {
9844 bset = isl_basic_set_list_get_basic_set(list, i);
9845 set = isl_set_add_basic_set(set, bset);
9848 isl_basic_set_list_free(list);
9849 return set;
9850 error:
9851 isl_basic_set_list_free(list);
9852 return NULL;
9855 /* Return the union of the elements in the non-empty list "list".
9856 * All elements are assumed to live in the same space.
9858 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9860 int i, n;
9861 isl_set *set;
9863 if (!list)
9864 return NULL;
9865 n = isl_set_list_n_set(list);
9866 if (n < 1)
9867 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9868 "expecting non-empty list", goto error);
9870 set = isl_set_list_get_set(list, 0);
9871 for (i = 1; i < n; ++i) {
9872 isl_set *set_i;
9874 set_i = isl_set_list_get_set(list, i);
9875 set = isl_set_union(set, set_i);
9878 isl_set_list_free(list);
9879 return set;
9880 error:
9881 isl_set_list_free(list);
9882 return NULL;
9885 __isl_give isl_basic_map *isl_basic_map_product(
9886 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9888 isl_space *dim_result = NULL;
9889 struct isl_basic_map *bmap;
9890 unsigned in1, in2, out1, out2, nparam, total, pos;
9891 struct isl_dim_map *dim_map1, *dim_map2;
9893 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9894 goto error;
9895 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9896 isl_space_copy(bmap2->dim));
9898 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9899 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9900 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9901 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9902 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9904 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9905 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9906 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9907 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9908 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9909 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9910 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9911 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9912 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9913 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9914 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9916 bmap = isl_basic_map_alloc_space(dim_result,
9917 bmap1->n_div + bmap2->n_div,
9918 bmap1->n_eq + bmap2->n_eq,
9919 bmap1->n_ineq + bmap2->n_ineq);
9920 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9921 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9922 bmap = isl_basic_map_simplify(bmap);
9923 return isl_basic_map_finalize(bmap);
9924 error:
9925 isl_basic_map_free(bmap1);
9926 isl_basic_map_free(bmap2);
9927 return NULL;
9930 __isl_give isl_basic_map *isl_basic_map_flat_product(
9931 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9933 isl_basic_map *prod;
9935 prod = isl_basic_map_product(bmap1, bmap2);
9936 prod = isl_basic_map_flatten(prod);
9937 return prod;
9940 __isl_give isl_basic_set *isl_basic_set_flat_product(
9941 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9943 return isl_basic_map_flat_range_product(bset1, bset2);
9946 __isl_give isl_basic_map *isl_basic_map_domain_product(
9947 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9949 isl_space *space_result = NULL;
9950 isl_basic_map *bmap;
9951 unsigned in1, in2, out, nparam, total, pos;
9952 struct isl_dim_map *dim_map1, *dim_map2;
9954 if (!bmap1 || !bmap2)
9955 goto error;
9957 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9958 isl_space_copy(bmap2->dim));
9960 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9961 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9962 out = isl_basic_map_dim(bmap1, isl_dim_out);
9963 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9965 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9966 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9967 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9968 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9969 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9970 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9971 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9972 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9973 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9974 isl_dim_map_div(dim_map1, bmap1, pos += out);
9975 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9977 bmap = isl_basic_map_alloc_space(space_result,
9978 bmap1->n_div + bmap2->n_div,
9979 bmap1->n_eq + bmap2->n_eq,
9980 bmap1->n_ineq + bmap2->n_ineq);
9981 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9982 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9983 bmap = isl_basic_map_simplify(bmap);
9984 return isl_basic_map_finalize(bmap);
9985 error:
9986 isl_basic_map_free(bmap1);
9987 isl_basic_map_free(bmap2);
9988 return NULL;
9991 __isl_give isl_basic_map *isl_basic_map_range_product(
9992 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9994 isl_bool rational;
9995 isl_space *dim_result = NULL;
9996 isl_basic_map *bmap;
9997 unsigned in, out1, out2, nparam, total, pos;
9998 struct isl_dim_map *dim_map1, *dim_map2;
10000 rational = isl_basic_map_is_rational(bmap1);
10001 if (rational >= 0 && rational)
10002 rational = isl_basic_map_is_rational(bmap2);
10003 if (!bmap1 || !bmap2 || rational < 0)
10004 goto error;
10006 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10007 goto error;
10009 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10010 isl_space_copy(bmap2->dim));
10012 in = isl_basic_map_dim(bmap1, isl_dim_in);
10013 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10014 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10015 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10017 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10018 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10019 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10020 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10021 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10022 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10023 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10024 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10025 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10026 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10027 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10029 bmap = isl_basic_map_alloc_space(dim_result,
10030 bmap1->n_div + bmap2->n_div,
10031 bmap1->n_eq + bmap2->n_eq,
10032 bmap1->n_ineq + bmap2->n_ineq);
10033 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10034 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10035 if (rational)
10036 bmap = isl_basic_map_set_rational(bmap);
10037 bmap = isl_basic_map_simplify(bmap);
10038 return isl_basic_map_finalize(bmap);
10039 error:
10040 isl_basic_map_free(bmap1);
10041 isl_basic_map_free(bmap2);
10042 return NULL;
10045 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10046 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10048 isl_basic_map *prod;
10050 prod = isl_basic_map_range_product(bmap1, bmap2);
10051 prod = isl_basic_map_flatten_range(prod);
10052 return prod;
10055 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10056 * and collect the results.
10057 * The result live in the space obtained by calling "space_product"
10058 * on the spaces of "map1" and "map2".
10059 * If "remove_duplicates" is set then the result may contain duplicates
10060 * (even if the inputs do not) and so we try and remove the obvious
10061 * duplicates.
10063 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10064 __isl_take isl_map *map2,
10065 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10066 __isl_take isl_space *right),
10067 __isl_give isl_basic_map *(*basic_map_product)(
10068 __isl_take isl_basic_map *left,
10069 __isl_take isl_basic_map *right),
10070 int remove_duplicates)
10072 unsigned flags = 0;
10073 struct isl_map *result;
10074 int i, j;
10075 isl_bool m;
10077 m = isl_map_has_equal_params(map1, map2);
10078 if (m < 0)
10079 goto error;
10080 if (!m)
10081 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10082 "parameters don't match", goto error);
10084 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10085 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10086 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10088 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10089 isl_space_copy(map2->dim)),
10090 map1->n * map2->n, flags);
10091 if (!result)
10092 goto error;
10093 for (i = 0; i < map1->n; ++i)
10094 for (j = 0; j < map2->n; ++j) {
10095 struct isl_basic_map *part;
10096 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10097 isl_basic_map_copy(map2->p[j]));
10098 if (isl_basic_map_is_empty(part))
10099 isl_basic_map_free(part);
10100 else
10101 result = isl_map_add_basic_map(result, part);
10102 if (!result)
10103 goto error;
10105 if (remove_duplicates)
10106 result = isl_map_remove_obvious_duplicates(result);
10107 isl_map_free(map1);
10108 isl_map_free(map2);
10109 return result;
10110 error:
10111 isl_map_free(map1);
10112 isl_map_free(map2);
10113 return NULL;
10116 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10118 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10119 __isl_take isl_map *map2)
10121 return map_product(map1, map2, &isl_space_product,
10122 &isl_basic_map_product, 0);
10125 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10126 __isl_take isl_map *map2)
10128 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10131 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10133 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10134 __isl_take isl_map *map2)
10136 isl_map *prod;
10138 prod = isl_map_product(map1, map2);
10139 prod = isl_map_flatten(prod);
10140 return prod;
10143 /* Given two set A and B, construct its Cartesian product A x B.
10145 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10147 return isl_map_range_product(set1, set2);
10150 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10151 __isl_take isl_set *set2)
10153 return isl_map_flat_range_product(set1, set2);
10156 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10158 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10159 __isl_take isl_map *map2)
10161 return map_product(map1, map2, &isl_space_domain_product,
10162 &isl_basic_map_domain_product, 1);
10165 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10167 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10168 __isl_take isl_map *map2)
10170 return map_product(map1, map2, &isl_space_range_product,
10171 &isl_basic_map_range_product, 1);
10174 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10175 __isl_take isl_map *map2)
10177 return isl_map_align_params_map_map_and(map1, map2,
10178 &map_domain_product_aligned);
10181 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10182 __isl_take isl_map *map2)
10184 return isl_map_align_params_map_map_and(map1, map2,
10185 &map_range_product_aligned);
10188 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10190 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10192 isl_space *space;
10193 int total1, keep1, total2, keep2;
10195 if (!map)
10196 return NULL;
10197 if (!isl_space_domain_is_wrapping(map->dim) ||
10198 !isl_space_range_is_wrapping(map->dim))
10199 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10200 "not a product", return isl_map_free(map));
10202 space = isl_map_get_space(map);
10203 total1 = isl_space_dim(space, isl_dim_in);
10204 total2 = isl_space_dim(space, isl_dim_out);
10205 space = isl_space_factor_domain(space);
10206 keep1 = isl_space_dim(space, isl_dim_in);
10207 keep2 = isl_space_dim(space, isl_dim_out);
10208 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10209 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10210 map = isl_map_reset_space(map, space);
10212 return map;
10215 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10217 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10219 isl_space *space;
10220 int total1, keep1, total2, keep2;
10222 if (!map)
10223 return NULL;
10224 if (!isl_space_domain_is_wrapping(map->dim) ||
10225 !isl_space_range_is_wrapping(map->dim))
10226 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10227 "not a product", return isl_map_free(map));
10229 space = isl_map_get_space(map);
10230 total1 = isl_space_dim(space, isl_dim_in);
10231 total2 = isl_space_dim(space, isl_dim_out);
10232 space = isl_space_factor_range(space);
10233 keep1 = isl_space_dim(space, isl_dim_in);
10234 keep2 = isl_space_dim(space, isl_dim_out);
10235 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10236 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10237 map = isl_map_reset_space(map, space);
10239 return map;
10242 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10244 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10246 isl_space *space;
10247 int total, keep;
10249 if (!map)
10250 return NULL;
10251 if (!isl_space_domain_is_wrapping(map->dim))
10252 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10253 "domain is not a product", return isl_map_free(map));
10255 space = isl_map_get_space(map);
10256 total = isl_space_dim(space, isl_dim_in);
10257 space = isl_space_domain_factor_domain(space);
10258 keep = isl_space_dim(space, isl_dim_in);
10259 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10260 map = isl_map_reset_space(map, space);
10262 return map;
10265 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10267 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10269 isl_space *space;
10270 int total, keep;
10272 if (!map)
10273 return NULL;
10274 if (!isl_space_domain_is_wrapping(map->dim))
10275 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10276 "domain is not a product", return isl_map_free(map));
10278 space = isl_map_get_space(map);
10279 total = isl_space_dim(space, isl_dim_in);
10280 space = isl_space_domain_factor_range(space);
10281 keep = isl_space_dim(space, isl_dim_in);
10282 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10283 map = isl_map_reset_space(map, space);
10285 return map;
10288 /* Given a map A -> [B -> C], extract the map A -> B.
10290 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10292 isl_space *space;
10293 int total, keep;
10295 if (!map)
10296 return NULL;
10297 if (!isl_space_range_is_wrapping(map->dim))
10298 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10299 "range is not a product", return isl_map_free(map));
10301 space = isl_map_get_space(map);
10302 total = isl_space_dim(space, isl_dim_out);
10303 space = isl_space_range_factor_domain(space);
10304 keep = isl_space_dim(space, isl_dim_out);
10305 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10306 map = isl_map_reset_space(map, space);
10308 return map;
10311 /* Given a map A -> [B -> C], extract the map A -> C.
10313 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10315 isl_space *space;
10316 int total, keep;
10318 if (!map)
10319 return NULL;
10320 if (!isl_space_range_is_wrapping(map->dim))
10321 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10322 "range is not a product", return isl_map_free(map));
10324 space = isl_map_get_space(map);
10325 total = isl_space_dim(space, isl_dim_out);
10326 space = isl_space_range_factor_range(space);
10327 keep = isl_space_dim(space, isl_dim_out);
10328 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10329 map = isl_map_reset_space(map, space);
10331 return map;
10334 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10336 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10337 __isl_take isl_map *map2)
10339 isl_map *prod;
10341 prod = isl_map_domain_product(map1, map2);
10342 prod = isl_map_flatten_domain(prod);
10343 return prod;
10346 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10348 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10349 __isl_take isl_map *map2)
10351 isl_map *prod;
10353 prod = isl_map_range_product(map1, map2);
10354 prod = isl_map_flatten_range(prod);
10355 return prod;
10358 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10360 int i;
10361 uint32_t hash = isl_hash_init();
10362 unsigned total;
10364 if (!bmap)
10365 return 0;
10366 bmap = isl_basic_map_copy(bmap);
10367 bmap = isl_basic_map_normalize(bmap);
10368 if (!bmap)
10369 return 0;
10370 total = isl_basic_map_total_dim(bmap);
10371 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10372 for (i = 0; i < bmap->n_eq; ++i) {
10373 uint32_t c_hash;
10374 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10375 isl_hash_hash(hash, c_hash);
10377 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10378 for (i = 0; i < bmap->n_ineq; ++i) {
10379 uint32_t c_hash;
10380 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10381 isl_hash_hash(hash, c_hash);
10383 isl_hash_byte(hash, bmap->n_div & 0xFF);
10384 for (i = 0; i < bmap->n_div; ++i) {
10385 uint32_t c_hash;
10386 if (isl_int_is_zero(bmap->div[i][0]))
10387 continue;
10388 isl_hash_byte(hash, i & 0xFF);
10389 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10390 isl_hash_hash(hash, c_hash);
10392 isl_basic_map_free(bmap);
10393 return hash;
10396 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10398 return isl_basic_map_get_hash(bset_to_bmap(bset));
10401 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10403 int i;
10404 uint32_t hash;
10406 if (!map)
10407 return 0;
10408 map = isl_map_copy(map);
10409 map = isl_map_normalize(map);
10410 if (!map)
10411 return 0;
10413 hash = isl_hash_init();
10414 for (i = 0; i < map->n; ++i) {
10415 uint32_t bmap_hash;
10416 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10417 isl_hash_hash(hash, bmap_hash);
10420 isl_map_free(map);
10422 return hash;
10425 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10427 return isl_map_get_hash(set_to_map(set));
10430 /* Return the number of basic maps in the (current) representation of "map".
10432 int isl_map_n_basic_map(__isl_keep isl_map *map)
10434 return map ? map->n : 0;
10437 int isl_set_n_basic_set(__isl_keep isl_set *set)
10439 return set ? set->n : 0;
10442 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10443 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10445 int i;
10447 if (!map)
10448 return isl_stat_error;
10450 for (i = 0; i < map->n; ++i)
10451 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10452 return isl_stat_error;
10454 return isl_stat_ok;
10457 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10458 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10460 int i;
10462 if (!set)
10463 return isl_stat_error;
10465 for (i = 0; i < set->n; ++i)
10466 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10467 return isl_stat_error;
10469 return isl_stat_ok;
10472 /* Return a list of basic sets, the union of which is equal to "set".
10474 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10475 __isl_keep isl_set *set)
10477 int i;
10478 isl_basic_set_list *list;
10480 if (!set)
10481 return NULL;
10483 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10484 for (i = 0; i < set->n; ++i) {
10485 isl_basic_set *bset;
10487 bset = isl_basic_set_copy(set->p[i]);
10488 list = isl_basic_set_list_add(list, bset);
10491 return list;
10494 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10496 isl_space *dim;
10498 if (!bset)
10499 return NULL;
10501 bset = isl_basic_set_cow(bset);
10502 if (!bset)
10503 return NULL;
10505 dim = isl_basic_set_get_space(bset);
10506 dim = isl_space_lift(dim, bset->n_div);
10507 if (!dim)
10508 goto error;
10509 isl_space_free(bset->dim);
10510 bset->dim = dim;
10511 bset->extra -= bset->n_div;
10512 bset->n_div = 0;
10514 bset = isl_basic_set_finalize(bset);
10516 return bset;
10517 error:
10518 isl_basic_set_free(bset);
10519 return NULL;
10522 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10524 int i;
10525 isl_space *dim;
10526 unsigned n_div;
10528 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10530 if (!set)
10531 return NULL;
10533 set = isl_set_cow(set);
10534 if (!set)
10535 return NULL;
10537 n_div = set->p[0]->n_div;
10538 dim = isl_set_get_space(set);
10539 dim = isl_space_lift(dim, n_div);
10540 if (!dim)
10541 goto error;
10542 isl_space_free(set->dim);
10543 set->dim = dim;
10545 for (i = 0; i < set->n; ++i) {
10546 set->p[i] = isl_basic_set_lift(set->p[i]);
10547 if (!set->p[i])
10548 goto error;
10551 return set;
10552 error:
10553 isl_set_free(set);
10554 return NULL;
10557 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10559 unsigned dim;
10560 int size = 0;
10562 if (!bset)
10563 return -1;
10565 dim = isl_basic_set_total_dim(bset);
10566 size += bset->n_eq * (1 + dim);
10567 size += bset->n_ineq * (1 + dim);
10568 size += bset->n_div * (2 + dim);
10570 return size;
10573 int isl_set_size(__isl_keep isl_set *set)
10575 int i;
10576 int size = 0;
10578 if (!set)
10579 return -1;
10581 for (i = 0; i < set->n; ++i)
10582 size += isl_basic_set_size(set->p[i]);
10584 return size;
10587 /* Check if there is any lower bound (if lower == 0) and/or upper
10588 * bound (if upper == 0) on the specified dim.
10590 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10591 enum isl_dim_type type, unsigned pos, int lower, int upper)
10593 int i;
10595 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10596 return isl_bool_error;
10598 pos += isl_basic_map_offset(bmap, type);
10600 for (i = 0; i < bmap->n_div; ++i) {
10601 if (isl_int_is_zero(bmap->div[i][0]))
10602 continue;
10603 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10604 return isl_bool_true;
10607 for (i = 0; i < bmap->n_eq; ++i)
10608 if (!isl_int_is_zero(bmap->eq[i][pos]))
10609 return isl_bool_true;
10611 for (i = 0; i < bmap->n_ineq; ++i) {
10612 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10613 if (sgn > 0)
10614 lower = 1;
10615 if (sgn < 0)
10616 upper = 1;
10619 return lower && upper;
10622 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10623 enum isl_dim_type type, unsigned pos)
10625 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10628 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10629 enum isl_dim_type type, unsigned pos)
10631 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10634 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10635 enum isl_dim_type type, unsigned pos)
10637 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10640 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10641 enum isl_dim_type type, unsigned pos)
10643 int i;
10645 if (!map)
10646 return isl_bool_error;
10648 for (i = 0; i < map->n; ++i) {
10649 isl_bool bounded;
10650 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10651 if (bounded < 0 || !bounded)
10652 return bounded;
10655 return isl_bool_true;
10658 /* Return true if the specified dim is involved in both an upper bound
10659 * and a lower bound.
10661 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10662 enum isl_dim_type type, unsigned pos)
10664 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10667 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10669 static isl_bool has_any_bound(__isl_keep isl_map *map,
10670 enum isl_dim_type type, unsigned pos,
10671 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10672 enum isl_dim_type type, unsigned pos))
10674 int i;
10676 if (!map)
10677 return isl_bool_error;
10679 for (i = 0; i < map->n; ++i) {
10680 isl_bool bounded;
10681 bounded = fn(map->p[i], type, pos);
10682 if (bounded < 0 || bounded)
10683 return bounded;
10686 return isl_bool_false;
10689 /* Return 1 if the specified dim is involved in any lower bound.
10691 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10692 enum isl_dim_type type, unsigned pos)
10694 return has_any_bound(set, type, pos,
10695 &isl_basic_map_dim_has_lower_bound);
10698 /* Return 1 if the specified dim is involved in any upper bound.
10700 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10701 enum isl_dim_type type, unsigned pos)
10703 return has_any_bound(set, type, pos,
10704 &isl_basic_map_dim_has_upper_bound);
10707 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10709 static isl_bool has_bound(__isl_keep isl_map *map,
10710 enum isl_dim_type type, unsigned pos,
10711 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10712 enum isl_dim_type type, unsigned pos))
10714 int i;
10716 if (!map)
10717 return isl_bool_error;
10719 for (i = 0; i < map->n; ++i) {
10720 isl_bool bounded;
10721 bounded = fn(map->p[i], type, pos);
10722 if (bounded < 0 || !bounded)
10723 return bounded;
10726 return isl_bool_true;
10729 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10731 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10732 enum isl_dim_type type, unsigned pos)
10734 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10737 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10739 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10740 enum isl_dim_type type, unsigned pos)
10742 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10745 /* For each of the "n" variables starting at "first", determine
10746 * the sign of the variable and put the results in the first "n"
10747 * elements of the array "signs".
10748 * Sign
10749 * 1 means that the variable is non-negative
10750 * -1 means that the variable is non-positive
10751 * 0 means the variable attains both positive and negative values.
10753 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10754 unsigned first, unsigned n, int *signs)
10756 isl_vec *bound = NULL;
10757 struct isl_tab *tab = NULL;
10758 struct isl_tab_undo *snap;
10759 int i;
10761 if (!bset || !signs)
10762 return isl_stat_error;
10764 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10765 tab = isl_tab_from_basic_set(bset, 0);
10766 if (!bound || !tab)
10767 goto error;
10769 isl_seq_clr(bound->el, bound->size);
10770 isl_int_set_si(bound->el[0], -1);
10772 snap = isl_tab_snap(tab);
10773 for (i = 0; i < n; ++i) {
10774 int empty;
10776 isl_int_set_si(bound->el[1 + first + i], -1);
10777 if (isl_tab_add_ineq(tab, bound->el) < 0)
10778 goto error;
10779 empty = tab->empty;
10780 isl_int_set_si(bound->el[1 + first + i], 0);
10781 if (isl_tab_rollback(tab, snap) < 0)
10782 goto error;
10784 if (empty) {
10785 signs[i] = 1;
10786 continue;
10789 isl_int_set_si(bound->el[1 + first + i], 1);
10790 if (isl_tab_add_ineq(tab, bound->el) < 0)
10791 goto error;
10792 empty = tab->empty;
10793 isl_int_set_si(bound->el[1 + first + i], 0);
10794 if (isl_tab_rollback(tab, snap) < 0)
10795 goto error;
10797 signs[i] = empty ? -1 : 0;
10800 isl_tab_free(tab);
10801 isl_vec_free(bound);
10802 return isl_stat_ok;
10803 error:
10804 isl_tab_free(tab);
10805 isl_vec_free(bound);
10806 return isl_stat_error;
10809 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10810 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10812 if (!bset || !signs)
10813 return isl_stat_error;
10814 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10815 return isl_stat_error);
10817 first += pos(bset->dim, type) - 1;
10818 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10821 /* Is it possible for the integer division "div" to depend (possibly
10822 * indirectly) on any output dimensions?
10824 * If the div is undefined, then we conservatively assume that it
10825 * may depend on them.
10826 * Otherwise, we check if it actually depends on them or on any integer
10827 * divisions that may depend on them.
10829 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10831 int i;
10832 unsigned n_out, o_out;
10833 unsigned n_div, o_div;
10835 if (isl_int_is_zero(bmap->div[div][0]))
10836 return isl_bool_true;
10838 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10839 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10841 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10842 return isl_bool_true;
10844 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10845 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10847 for (i = 0; i < n_div; ++i) {
10848 isl_bool may_involve;
10850 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10851 continue;
10852 may_involve = div_may_involve_output(bmap, i);
10853 if (may_involve < 0 || may_involve)
10854 return may_involve;
10857 return isl_bool_false;
10860 /* Return the first integer division of "bmap" in the range
10861 * [first, first + n[ that may depend on any output dimensions and
10862 * that has a non-zero coefficient in "c" (where the first coefficient
10863 * in "c" corresponds to integer division "first").
10865 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10866 isl_int *c, int first, int n)
10868 int k;
10870 if (!bmap)
10871 return -1;
10873 for (k = first; k < first + n; ++k) {
10874 isl_bool may_involve;
10876 if (isl_int_is_zero(c[k]))
10877 continue;
10878 may_involve = div_may_involve_output(bmap, k);
10879 if (may_involve < 0)
10880 return -1;
10881 if (may_involve)
10882 return k;
10885 return first + n;
10888 /* Look for a pair of inequality constraints in "bmap" of the form
10890 * -l + i >= 0 or i >= l
10891 * and
10892 * n + l - i >= 0 or i <= l + n
10894 * with n < "m" and i the output dimension at position "pos".
10895 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10896 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10897 * and earlier output dimensions, as well as integer divisions that do
10898 * not involve any of the output dimensions.
10900 * Return the index of the first inequality constraint or bmap->n_ineq
10901 * if no such pair can be found.
10903 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10904 int pos, isl_int m)
10906 int i, j;
10907 isl_ctx *ctx;
10908 unsigned total;
10909 unsigned n_div, o_div;
10910 unsigned n_out, o_out;
10911 int less;
10913 if (!bmap)
10914 return -1;
10916 ctx = isl_basic_map_get_ctx(bmap);
10917 total = isl_basic_map_total_dim(bmap);
10918 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10919 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10920 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10921 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10922 for (i = 0; i < bmap->n_ineq; ++i) {
10923 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10924 continue;
10925 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10926 n_out - (pos + 1)) != -1)
10927 continue;
10928 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10929 0, n_div) < n_div)
10930 continue;
10931 for (j = i + 1; j < bmap->n_ineq; ++j) {
10932 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10933 ctx->one))
10934 continue;
10935 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10936 bmap->ineq[j] + 1, total))
10937 continue;
10938 break;
10940 if (j >= bmap->n_ineq)
10941 continue;
10942 isl_int_add(bmap->ineq[i][0],
10943 bmap->ineq[i][0], bmap->ineq[j][0]);
10944 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10945 isl_int_sub(bmap->ineq[i][0],
10946 bmap->ineq[i][0], bmap->ineq[j][0]);
10947 if (!less)
10948 continue;
10949 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10950 return i;
10951 else
10952 return j;
10955 return bmap->n_ineq;
10958 /* Return the index of the equality of "bmap" that defines
10959 * the output dimension "pos" in terms of earlier dimensions.
10960 * The equality may also involve integer divisions, as long
10961 * as those integer divisions are defined in terms of
10962 * parameters or input dimensions.
10963 * In this case, *div is set to the number of integer divisions and
10964 * *ineq is set to the number of inequality constraints (provided
10965 * div and ineq are not NULL).
10967 * The equality may also involve a single integer division involving
10968 * the output dimensions (typically only output dimension "pos") as
10969 * long as the coefficient of output dimension "pos" is 1 or -1 and
10970 * there is a pair of constraints i >= l and i <= l + n, with i referring
10971 * to output dimension "pos", l an expression involving only earlier
10972 * dimensions and n smaller than the coefficient of the integer division
10973 * in the equality. In this case, the output dimension can be defined
10974 * in terms of a modulo expression that does not involve the integer division.
10975 * *div is then set to this single integer division and
10976 * *ineq is set to the index of constraint i >= l.
10978 * Return bmap->n_eq if there is no such equality.
10979 * Return -1 on error.
10981 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10982 int pos, int *div, int *ineq)
10984 int j, k, l;
10985 unsigned n_out, o_out;
10986 unsigned n_div, o_div;
10988 if (!bmap)
10989 return -1;
10991 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10992 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10993 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10994 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10996 if (ineq)
10997 *ineq = bmap->n_ineq;
10998 if (div)
10999 *div = n_div;
11000 for (j = 0; j < bmap->n_eq; ++j) {
11001 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11002 continue;
11003 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11004 n_out - (pos + 1)) != -1)
11005 continue;
11006 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11007 0, n_div);
11008 if (k >= n_div)
11009 return j;
11010 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11011 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11012 continue;
11013 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11014 k + 1, n_div - (k+1)) < n_div)
11015 continue;
11016 l = find_modulo_constraint_pair(bmap, pos,
11017 bmap->eq[j][o_div + k]);
11018 if (l < 0)
11019 return -1;
11020 if (l >= bmap->n_ineq)
11021 continue;
11022 if (div)
11023 *div = k;
11024 if (ineq)
11025 *ineq = l;
11026 return j;
11029 return bmap->n_eq;
11032 /* Check if the given basic map is obviously single-valued.
11033 * In particular, for each output dimension, check that there is
11034 * an equality that defines the output dimension in terms of
11035 * earlier dimensions.
11037 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11039 int i;
11040 unsigned n_out;
11042 if (!bmap)
11043 return isl_bool_error;
11045 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11047 for (i = 0; i < n_out; ++i) {
11048 int eq;
11050 eq = isl_basic_map_output_defining_equality(bmap, i,
11051 NULL, NULL);
11052 if (eq < 0)
11053 return isl_bool_error;
11054 if (eq >= bmap->n_eq)
11055 return isl_bool_false;
11058 return isl_bool_true;
11061 /* Check if the given basic map is single-valued.
11062 * We simply compute
11064 * M \circ M^-1
11066 * and check if the result is a subset of the identity mapping.
11068 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11070 isl_space *space;
11071 isl_basic_map *test;
11072 isl_basic_map *id;
11073 isl_bool sv;
11075 sv = isl_basic_map_plain_is_single_valued(bmap);
11076 if (sv < 0 || sv)
11077 return sv;
11079 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11080 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11082 space = isl_basic_map_get_space(bmap);
11083 space = isl_space_map_from_set(isl_space_range(space));
11084 id = isl_basic_map_identity(space);
11086 sv = isl_basic_map_is_subset(test, id);
11088 isl_basic_map_free(test);
11089 isl_basic_map_free(id);
11091 return sv;
11094 /* Check if the given map is obviously single-valued.
11096 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11098 if (!map)
11099 return isl_bool_error;
11100 if (map->n == 0)
11101 return isl_bool_true;
11102 if (map->n >= 2)
11103 return isl_bool_false;
11105 return isl_basic_map_plain_is_single_valued(map->p[0]);
11108 /* Check if the given map is single-valued.
11109 * We simply compute
11111 * M \circ M^-1
11113 * and check if the result is a subset of the identity mapping.
11115 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11117 isl_space *dim;
11118 isl_map *test;
11119 isl_map *id;
11120 isl_bool sv;
11122 sv = isl_map_plain_is_single_valued(map);
11123 if (sv < 0 || sv)
11124 return sv;
11126 test = isl_map_reverse(isl_map_copy(map));
11127 test = isl_map_apply_range(test, isl_map_copy(map));
11129 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11130 id = isl_map_identity(dim);
11132 sv = isl_map_is_subset(test, id);
11134 isl_map_free(test);
11135 isl_map_free(id);
11137 return sv;
11140 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11142 isl_bool in;
11144 map = isl_map_copy(map);
11145 map = isl_map_reverse(map);
11146 in = isl_map_is_single_valued(map);
11147 isl_map_free(map);
11149 return in;
11152 /* Check if the given map is obviously injective.
11154 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11156 isl_bool in;
11158 map = isl_map_copy(map);
11159 map = isl_map_reverse(map);
11160 in = isl_map_plain_is_single_valued(map);
11161 isl_map_free(map);
11163 return in;
11166 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11168 isl_bool sv;
11170 sv = isl_map_is_single_valued(map);
11171 if (sv < 0 || !sv)
11172 return sv;
11174 return isl_map_is_injective(map);
11177 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11179 return isl_map_is_single_valued(set_to_map(set));
11182 /* Does "map" only map elements to themselves?
11184 * If the domain and range spaces are different, then "map"
11185 * is considered not to be an identity relation, even if it is empty.
11186 * Otherwise, construct the maximal identity relation and
11187 * check whether "map" is a subset of this relation.
11189 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11191 isl_space *space;
11192 isl_map *id;
11193 isl_bool equal, is_identity;
11195 space = isl_map_get_space(map);
11196 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11197 isl_space_free(space);
11198 if (equal < 0 || !equal)
11199 return equal;
11201 id = isl_map_identity(isl_map_get_space(map));
11202 is_identity = isl_map_is_subset(map, id);
11203 isl_map_free(id);
11205 return is_identity;
11208 int isl_map_is_translation(__isl_keep isl_map *map)
11210 int ok;
11211 isl_set *delta;
11213 delta = isl_map_deltas(isl_map_copy(map));
11214 ok = isl_set_is_singleton(delta);
11215 isl_set_free(delta);
11217 return ok;
11220 static int unique(isl_int *p, unsigned pos, unsigned len)
11222 if (isl_seq_first_non_zero(p, pos) != -1)
11223 return 0;
11224 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11225 return 0;
11226 return 1;
11229 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11231 int i, j;
11232 unsigned nvar;
11233 unsigned ovar;
11235 if (!bset)
11236 return isl_bool_error;
11238 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11239 return isl_bool_false;
11241 nvar = isl_basic_set_dim(bset, isl_dim_set);
11242 ovar = isl_space_offset(bset->dim, isl_dim_set);
11243 for (j = 0; j < nvar; ++j) {
11244 int lower = 0, upper = 0;
11245 for (i = 0; i < bset->n_eq; ++i) {
11246 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11247 continue;
11248 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11249 return isl_bool_false;
11250 break;
11252 if (i < bset->n_eq)
11253 continue;
11254 for (i = 0; i < bset->n_ineq; ++i) {
11255 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11256 continue;
11257 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11258 return isl_bool_false;
11259 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11260 lower = 1;
11261 else
11262 upper = 1;
11264 if (!lower || !upper)
11265 return isl_bool_false;
11268 return isl_bool_true;
11271 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11273 if (!set)
11274 return isl_bool_error;
11275 if (set->n != 1)
11276 return isl_bool_false;
11278 return isl_basic_set_is_box(set->p[0]);
11281 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11283 if (!bset)
11284 return isl_bool_error;
11286 return isl_space_is_wrapping(bset->dim);
11289 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11291 if (!set)
11292 return isl_bool_error;
11294 return isl_space_is_wrapping(set->dim);
11297 /* Modify the space of "map" through a call to "change".
11298 * If "can_change" is set (not NULL), then first call it to check
11299 * if the modification is allowed, printing the error message "cannot_change"
11300 * if it is not.
11302 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11303 isl_bool (*can_change)(__isl_keep isl_map *map),
11304 const char *cannot_change,
11305 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11307 isl_bool ok;
11308 isl_space *space;
11310 if (!map)
11311 return NULL;
11313 ok = can_change ? can_change(map) : isl_bool_true;
11314 if (ok < 0)
11315 return isl_map_free(map);
11316 if (!ok)
11317 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11318 return isl_map_free(map));
11320 space = change(isl_map_get_space(map));
11321 map = isl_map_reset_space(map, space);
11323 return map;
11326 /* Is the domain of "map" a wrapped relation?
11328 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11330 if (!map)
11331 return isl_bool_error;
11333 return isl_space_domain_is_wrapping(map->dim);
11336 /* Does "map" have a wrapped relation in both domain and range?
11338 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11340 return isl_space_is_product(isl_map_peek_space(map));
11343 /* Is the range of "map" a wrapped relation?
11345 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11347 if (!map)
11348 return isl_bool_error;
11350 return isl_space_range_is_wrapping(map->dim);
11353 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11355 bmap = isl_basic_map_cow(bmap);
11356 if (!bmap)
11357 return NULL;
11359 bmap->dim = isl_space_wrap(bmap->dim);
11360 if (!bmap->dim)
11361 goto error;
11363 bmap = isl_basic_map_finalize(bmap);
11365 return bset_from_bmap(bmap);
11366 error:
11367 isl_basic_map_free(bmap);
11368 return NULL;
11371 /* Given a map A -> B, return the set (A -> B).
11373 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11375 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11378 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11380 bset = isl_basic_set_cow(bset);
11381 if (!bset)
11382 return NULL;
11384 bset->dim = isl_space_unwrap(bset->dim);
11385 if (!bset->dim)
11386 goto error;
11388 bset = isl_basic_set_finalize(bset);
11390 return bset_to_bmap(bset);
11391 error:
11392 isl_basic_set_free(bset);
11393 return NULL;
11396 /* Given a set (A -> B), return the map A -> B.
11397 * Error out if "set" is not of the form (A -> B).
11399 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11401 return isl_map_change_space(set, &isl_set_is_wrapping,
11402 "not a wrapping set", &isl_space_unwrap);
11405 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11406 enum isl_dim_type type)
11408 if (!bmap)
11409 return NULL;
11411 if (!isl_space_is_named_or_nested(bmap->dim, type))
11412 return bmap;
11414 bmap = isl_basic_map_cow(bmap);
11415 if (!bmap)
11416 return NULL;
11418 bmap->dim = isl_space_reset(bmap->dim, type);
11419 if (!bmap->dim)
11420 goto error;
11422 bmap = isl_basic_map_finalize(bmap);
11424 return bmap;
11425 error:
11426 isl_basic_map_free(bmap);
11427 return NULL;
11430 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11431 enum isl_dim_type type)
11433 int i;
11435 if (!map)
11436 return NULL;
11438 if (!isl_space_is_named_or_nested(map->dim, type))
11439 return map;
11441 map = isl_map_cow(map);
11442 if (!map)
11443 return NULL;
11445 for (i = 0; i < map->n; ++i) {
11446 map->p[i] = isl_basic_map_reset(map->p[i], type);
11447 if (!map->p[i])
11448 goto error;
11450 map->dim = isl_space_reset(map->dim, type);
11451 if (!map->dim)
11452 goto error;
11454 return map;
11455 error:
11456 isl_map_free(map);
11457 return NULL;
11460 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11462 if (!bmap)
11463 return NULL;
11465 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11466 return bmap;
11468 bmap = isl_basic_map_cow(bmap);
11469 if (!bmap)
11470 return NULL;
11472 bmap->dim = isl_space_flatten(bmap->dim);
11473 if (!bmap->dim)
11474 goto error;
11476 bmap = isl_basic_map_finalize(bmap);
11478 return bmap;
11479 error:
11480 isl_basic_map_free(bmap);
11481 return NULL;
11484 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11486 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11489 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11490 __isl_take isl_basic_map *bmap)
11492 if (!bmap)
11493 return NULL;
11495 if (!bmap->dim->nested[0])
11496 return bmap;
11498 bmap = isl_basic_map_cow(bmap);
11499 if (!bmap)
11500 return NULL;
11502 bmap->dim = isl_space_flatten_domain(bmap->dim);
11503 if (!bmap->dim)
11504 goto error;
11506 bmap = isl_basic_map_finalize(bmap);
11508 return bmap;
11509 error:
11510 isl_basic_map_free(bmap);
11511 return NULL;
11514 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11515 __isl_take isl_basic_map *bmap)
11517 if (!bmap)
11518 return NULL;
11520 if (!bmap->dim->nested[1])
11521 return bmap;
11523 bmap = isl_basic_map_cow(bmap);
11524 if (!bmap)
11525 return NULL;
11527 bmap->dim = isl_space_flatten_range(bmap->dim);
11528 if (!bmap->dim)
11529 goto error;
11531 bmap = isl_basic_map_finalize(bmap);
11533 return bmap;
11534 error:
11535 isl_basic_map_free(bmap);
11536 return NULL;
11539 /* Remove any internal structure from the spaces of domain and range of "map".
11541 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11543 if (!map)
11544 return NULL;
11546 if (!map->dim->nested[0] && !map->dim->nested[1])
11547 return map;
11549 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11552 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11554 return set_from_map(isl_map_flatten(set_to_map(set)));
11557 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11559 isl_space *dim, *flat_dim;
11560 isl_map *map;
11562 dim = isl_set_get_space(set);
11563 flat_dim = isl_space_flatten(isl_space_copy(dim));
11564 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11565 map = isl_map_intersect_domain(map, set);
11567 return map;
11570 /* Remove any internal structure from the space of the domain of "map".
11572 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11574 if (!map)
11575 return NULL;
11577 if (!map->dim->nested[0])
11578 return map;
11580 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11583 /* Remove any internal structure from the space of the range of "map".
11585 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11587 if (!map)
11588 return NULL;
11590 if (!map->dim->nested[1])
11591 return map;
11593 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11596 /* Reorder the dimensions of "bmap" according to the given dim_map
11597 * and set the dimension specification to "dim" and
11598 * perform Gaussian elimination on the result.
11600 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11601 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11603 isl_basic_map *res;
11604 unsigned flags;
11606 bmap = isl_basic_map_cow(bmap);
11607 if (!bmap || !dim || !dim_map)
11608 goto error;
11610 flags = bmap->flags;
11611 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11612 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11613 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11614 res = isl_basic_map_alloc_space(dim,
11615 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11616 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11617 if (res)
11618 res->flags = flags;
11619 res = isl_basic_map_gauss(res, NULL);
11620 res = isl_basic_map_finalize(res);
11621 return res;
11622 error:
11623 free(dim_map);
11624 isl_basic_map_free(bmap);
11625 isl_space_free(dim);
11626 return NULL;
11629 /* Reorder the dimensions of "map" according to given reordering.
11631 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11632 __isl_take isl_reordering *r)
11634 int i;
11635 struct isl_dim_map *dim_map;
11637 map = isl_map_cow(map);
11638 dim_map = isl_dim_map_from_reordering(r);
11639 if (!map || !r || !dim_map)
11640 goto error;
11642 for (i = 0; i < map->n; ++i) {
11643 struct isl_dim_map *dim_map_i;
11645 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11647 map->p[i] = isl_basic_map_realign(map->p[i],
11648 isl_space_copy(r->dim), dim_map_i);
11650 if (!map->p[i])
11651 goto error;
11654 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11656 isl_reordering_free(r);
11657 free(dim_map);
11658 return map;
11659 error:
11660 free(dim_map);
11661 isl_map_free(map);
11662 isl_reordering_free(r);
11663 return NULL;
11666 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11667 __isl_take isl_reordering *r)
11669 return set_from_map(isl_map_realign(set_to_map(set), r));
11672 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11673 __isl_take isl_space *model)
11675 isl_ctx *ctx;
11676 isl_bool aligned;
11678 if (!map || !model)
11679 goto error;
11681 ctx = isl_space_get_ctx(model);
11682 if (!isl_space_has_named_params(model))
11683 isl_die(ctx, isl_error_invalid,
11684 "model has unnamed parameters", goto error);
11685 if (isl_map_check_named_params(map) < 0)
11686 goto error;
11687 aligned = isl_map_space_has_equal_params(map, model);
11688 if (aligned < 0)
11689 goto error;
11690 if (!aligned) {
11691 isl_reordering *exp;
11693 model = isl_space_drop_dims(model, isl_dim_in,
11694 0, isl_space_dim(model, isl_dim_in));
11695 model = isl_space_drop_dims(model, isl_dim_out,
11696 0, isl_space_dim(model, isl_dim_out));
11697 exp = isl_parameter_alignment_reordering(map->dim, model);
11698 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11699 map = isl_map_realign(map, exp);
11702 isl_space_free(model);
11703 return map;
11704 error:
11705 isl_space_free(model);
11706 isl_map_free(map);
11707 return NULL;
11710 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11711 __isl_take isl_space *model)
11713 return isl_map_align_params(set, model);
11716 /* Align the parameters of "bmap" to those of "model", introducing
11717 * additional parameters if needed.
11719 __isl_give isl_basic_map *isl_basic_map_align_params(
11720 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11722 isl_ctx *ctx;
11723 isl_bool equal_params;
11725 if (!bmap || !model)
11726 goto error;
11728 ctx = isl_space_get_ctx(model);
11729 if (!isl_space_has_named_params(model))
11730 isl_die(ctx, isl_error_invalid,
11731 "model has unnamed parameters", goto error);
11732 if (!isl_space_has_named_params(bmap->dim))
11733 isl_die(ctx, isl_error_invalid,
11734 "relation has unnamed parameters", goto error);
11735 equal_params = isl_space_has_equal_params(bmap->dim, model);
11736 if (equal_params < 0)
11737 goto error;
11738 if (!equal_params) {
11739 isl_reordering *exp;
11740 struct isl_dim_map *dim_map;
11742 model = isl_space_drop_dims(model, isl_dim_in,
11743 0, isl_space_dim(model, isl_dim_in));
11744 model = isl_space_drop_dims(model, isl_dim_out,
11745 0, isl_space_dim(model, isl_dim_out));
11746 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11747 exp = isl_reordering_extend_space(exp,
11748 isl_basic_map_get_space(bmap));
11749 dim_map = isl_dim_map_from_reordering(exp);
11750 bmap = isl_basic_map_realign(bmap,
11751 exp ? isl_space_copy(exp->dim) : NULL,
11752 isl_dim_map_extend(dim_map, bmap));
11753 isl_reordering_free(exp);
11754 free(dim_map);
11757 isl_space_free(model);
11758 return bmap;
11759 error:
11760 isl_space_free(model);
11761 isl_basic_map_free(bmap);
11762 return NULL;
11765 /* Do "bset" and "space" have the same parameters?
11767 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11768 __isl_keep isl_space *space)
11770 isl_space *bset_space;
11772 bset_space = isl_basic_set_peek_space(bset);
11773 return isl_space_has_equal_params(bset_space, space);
11776 /* Do "map" and "space" have the same parameters?
11778 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11779 __isl_keep isl_space *space)
11781 isl_space *map_space;
11783 map_space = isl_map_peek_space(map);
11784 return isl_space_has_equal_params(map_space, space);
11787 /* Do "set" and "space" have the same parameters?
11789 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11790 __isl_keep isl_space *space)
11792 return isl_map_space_has_equal_params(set_to_map(set), space);
11795 /* Align the parameters of "bset" to those of "model", introducing
11796 * additional parameters if needed.
11798 __isl_give isl_basic_set *isl_basic_set_align_params(
11799 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11801 return isl_basic_map_align_params(bset, model);
11804 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11805 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11806 enum isl_dim_type c2, enum isl_dim_type c3,
11807 enum isl_dim_type c4, enum isl_dim_type c5)
11809 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11810 struct isl_mat *mat;
11811 int i, j, k;
11812 int pos;
11814 if (!bmap)
11815 return NULL;
11816 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11817 isl_basic_map_total_dim(bmap) + 1);
11818 if (!mat)
11819 return NULL;
11820 for (i = 0; i < bmap->n_eq; ++i)
11821 for (j = 0, pos = 0; j < 5; ++j) {
11822 int off = isl_basic_map_offset(bmap, c[j]);
11823 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11824 isl_int_set(mat->row[i][pos],
11825 bmap->eq[i][off + k]);
11826 ++pos;
11830 return mat;
11833 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11834 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11835 enum isl_dim_type c2, enum isl_dim_type c3,
11836 enum isl_dim_type c4, enum isl_dim_type c5)
11838 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11839 struct isl_mat *mat;
11840 int i, j, k;
11841 int pos;
11843 if (!bmap)
11844 return NULL;
11845 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11846 isl_basic_map_total_dim(bmap) + 1);
11847 if (!mat)
11848 return NULL;
11849 for (i = 0; i < bmap->n_ineq; ++i)
11850 for (j = 0, pos = 0; j < 5; ++j) {
11851 int off = isl_basic_map_offset(bmap, c[j]);
11852 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11853 isl_int_set(mat->row[i][pos],
11854 bmap->ineq[i][off + k]);
11855 ++pos;
11859 return mat;
11862 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11863 __isl_take isl_space *dim,
11864 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11865 enum isl_dim_type c2, enum isl_dim_type c3,
11866 enum isl_dim_type c4, enum isl_dim_type c5)
11868 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11869 isl_basic_map *bmap;
11870 unsigned total;
11871 unsigned extra;
11872 int i, j, k, l;
11873 int pos;
11875 if (!dim || !eq || !ineq)
11876 goto error;
11878 if (eq->n_col != ineq->n_col)
11879 isl_die(dim->ctx, isl_error_invalid,
11880 "equalities and inequalities matrices should have "
11881 "same number of columns", goto error);
11883 total = 1 + isl_space_dim(dim, isl_dim_all);
11885 if (eq->n_col < total)
11886 isl_die(dim->ctx, isl_error_invalid,
11887 "number of columns too small", goto error);
11889 extra = eq->n_col - total;
11891 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11892 eq->n_row, ineq->n_row);
11893 if (!bmap)
11894 goto error;
11895 for (i = 0; i < extra; ++i) {
11896 k = isl_basic_map_alloc_div(bmap);
11897 if (k < 0)
11898 goto error;
11899 isl_int_set_si(bmap->div[k][0], 0);
11901 for (i = 0; i < eq->n_row; ++i) {
11902 l = isl_basic_map_alloc_equality(bmap);
11903 if (l < 0)
11904 goto error;
11905 for (j = 0, pos = 0; j < 5; ++j) {
11906 int off = isl_basic_map_offset(bmap, c[j]);
11907 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11908 isl_int_set(bmap->eq[l][off + k],
11909 eq->row[i][pos]);
11910 ++pos;
11914 for (i = 0; i < ineq->n_row; ++i) {
11915 l = isl_basic_map_alloc_inequality(bmap);
11916 if (l < 0)
11917 goto error;
11918 for (j = 0, pos = 0; j < 5; ++j) {
11919 int off = isl_basic_map_offset(bmap, c[j]);
11920 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11921 isl_int_set(bmap->ineq[l][off + k],
11922 ineq->row[i][pos]);
11923 ++pos;
11928 isl_space_free(dim);
11929 isl_mat_free(eq);
11930 isl_mat_free(ineq);
11932 bmap = isl_basic_map_simplify(bmap);
11933 return isl_basic_map_finalize(bmap);
11934 error:
11935 isl_space_free(dim);
11936 isl_mat_free(eq);
11937 isl_mat_free(ineq);
11938 return NULL;
11941 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11942 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11943 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11945 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11946 c1, c2, c3, c4, isl_dim_in);
11949 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11950 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11951 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11953 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11954 c1, c2, c3, c4, isl_dim_in);
11957 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11958 __isl_take isl_space *dim,
11959 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11960 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11962 isl_basic_map *bmap;
11963 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11964 c1, c2, c3, c4, isl_dim_in);
11965 return bset_from_bmap(bmap);
11968 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11970 if (!bmap)
11971 return isl_bool_error;
11973 return isl_space_can_zip(bmap->dim);
11976 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11978 if (!map)
11979 return isl_bool_error;
11981 return isl_space_can_zip(map->dim);
11984 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11985 * (A -> C) -> (B -> D).
11987 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11989 unsigned pos;
11990 unsigned n1;
11991 unsigned n2;
11993 if (!bmap)
11994 return NULL;
11996 if (!isl_basic_map_can_zip(bmap))
11997 isl_die(bmap->ctx, isl_error_invalid,
11998 "basic map cannot be zipped", goto error);
11999 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12000 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12001 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12002 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12003 bmap = isl_basic_map_cow(bmap);
12004 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12005 if (!bmap)
12006 return NULL;
12007 bmap->dim = isl_space_zip(bmap->dim);
12008 if (!bmap->dim)
12009 goto error;
12010 bmap = isl_basic_map_mark_final(bmap);
12011 return bmap;
12012 error:
12013 isl_basic_map_free(bmap);
12014 return NULL;
12017 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12018 * (A -> C) -> (B -> D).
12020 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12022 int i;
12024 if (!map)
12025 return NULL;
12027 if (!isl_map_can_zip(map))
12028 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12029 goto error);
12031 map = isl_map_cow(map);
12032 if (!map)
12033 return NULL;
12035 for (i = 0; i < map->n; ++i) {
12036 map->p[i] = isl_basic_map_zip(map->p[i]);
12037 if (!map->p[i])
12038 goto error;
12041 map->dim = isl_space_zip(map->dim);
12042 if (!map->dim)
12043 goto error;
12045 return map;
12046 error:
12047 isl_map_free(map);
12048 return NULL;
12051 /* Can we apply isl_basic_map_curry to "bmap"?
12052 * That is, does it have a nested relation in its domain?
12054 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12056 if (!bmap)
12057 return isl_bool_error;
12059 return isl_space_can_curry(bmap->dim);
12062 /* Can we apply isl_map_curry to "map"?
12063 * That is, does it have a nested relation in its domain?
12065 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12067 if (!map)
12068 return isl_bool_error;
12070 return isl_space_can_curry(map->dim);
12073 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12074 * A -> (B -> C).
12076 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12079 if (!bmap)
12080 return NULL;
12082 if (!isl_basic_map_can_curry(bmap))
12083 isl_die(bmap->ctx, isl_error_invalid,
12084 "basic map cannot be curried", goto error);
12085 bmap = isl_basic_map_cow(bmap);
12086 if (!bmap)
12087 return NULL;
12088 bmap->dim = isl_space_curry(bmap->dim);
12089 if (!bmap->dim)
12090 goto error;
12091 bmap = isl_basic_map_mark_final(bmap);
12092 return bmap;
12093 error:
12094 isl_basic_map_free(bmap);
12095 return NULL;
12098 /* Given a map (A -> B) -> C, return the corresponding map
12099 * A -> (B -> C).
12101 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12103 return isl_map_change_space(map, &isl_map_can_curry,
12104 "map cannot be curried", &isl_space_curry);
12107 /* Can isl_map_range_curry be applied to "map"?
12108 * That is, does it have a nested relation in its range,
12109 * the domain of which is itself a nested relation?
12111 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12113 if (!map)
12114 return isl_bool_error;
12116 return isl_space_can_range_curry(map->dim);
12119 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12120 * A -> (B -> (C -> D)).
12122 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12124 return isl_map_change_space(map, &isl_map_can_range_curry,
12125 "map range cannot be curried",
12126 &isl_space_range_curry);
12129 /* Can we apply isl_basic_map_uncurry to "bmap"?
12130 * That is, does it have a nested relation in its domain?
12132 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12134 if (!bmap)
12135 return isl_bool_error;
12137 return isl_space_can_uncurry(bmap->dim);
12140 /* Can we apply isl_map_uncurry to "map"?
12141 * That is, does it have a nested relation in its domain?
12143 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12145 if (!map)
12146 return isl_bool_error;
12148 return isl_space_can_uncurry(map->dim);
12151 /* Given a basic map A -> (B -> C), return the corresponding basic map
12152 * (A -> B) -> C.
12154 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12157 if (!bmap)
12158 return NULL;
12160 if (!isl_basic_map_can_uncurry(bmap))
12161 isl_die(bmap->ctx, isl_error_invalid,
12162 "basic map cannot be uncurried",
12163 return isl_basic_map_free(bmap));
12164 bmap = isl_basic_map_cow(bmap);
12165 if (!bmap)
12166 return NULL;
12167 bmap->dim = isl_space_uncurry(bmap->dim);
12168 if (!bmap->dim)
12169 return isl_basic_map_free(bmap);
12170 bmap = isl_basic_map_mark_final(bmap);
12171 return bmap;
12174 /* Given a map A -> (B -> C), return the corresponding map
12175 * (A -> B) -> C.
12177 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12179 return isl_map_change_space(map, &isl_map_can_uncurry,
12180 "map cannot be uncurried", &isl_space_uncurry);
12183 /* Construct a basic map mapping the domain of the affine expression
12184 * to a one-dimensional range prescribed by the affine expression.
12185 * If "rational" is set, then construct a rational basic map.
12187 * A NaN affine expression cannot be converted to a basic map.
12189 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12190 __isl_take isl_aff *aff, int rational)
12192 int k;
12193 int pos;
12194 isl_bool is_nan;
12195 isl_local_space *ls;
12196 isl_basic_map *bmap = NULL;
12198 if (!aff)
12199 return NULL;
12200 is_nan = isl_aff_is_nan(aff);
12201 if (is_nan < 0)
12202 goto error;
12203 if (is_nan)
12204 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12205 "cannot convert NaN", goto error);
12207 ls = isl_aff_get_local_space(aff);
12208 bmap = isl_basic_map_from_local_space(ls);
12209 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12210 k = isl_basic_map_alloc_equality(bmap);
12211 if (k < 0)
12212 goto error;
12214 pos = isl_basic_map_offset(bmap, isl_dim_out);
12215 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12216 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12217 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12218 aff->v->size - (pos + 1));
12220 isl_aff_free(aff);
12221 if (rational)
12222 bmap = isl_basic_map_set_rational(bmap);
12223 bmap = isl_basic_map_gauss(bmap, NULL);
12224 bmap = isl_basic_map_finalize(bmap);
12225 return bmap;
12226 error:
12227 isl_aff_free(aff);
12228 isl_basic_map_free(bmap);
12229 return NULL;
12232 /* Construct a basic map mapping the domain of the affine expression
12233 * to a one-dimensional range prescribed by the affine expression.
12235 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12237 return isl_basic_map_from_aff2(aff, 0);
12240 /* Construct a map mapping the domain of the affine expression
12241 * to a one-dimensional range prescribed by the affine expression.
12243 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12245 isl_basic_map *bmap;
12247 bmap = isl_basic_map_from_aff(aff);
12248 return isl_map_from_basic_map(bmap);
12251 /* Construct a basic map mapping the domain the multi-affine expression
12252 * to its range, with each dimension in the range equated to the
12253 * corresponding affine expression.
12254 * If "rational" is set, then construct a rational basic map.
12256 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12257 __isl_take isl_multi_aff *maff, int rational)
12259 int i;
12260 isl_space *space;
12261 isl_basic_map *bmap;
12263 if (!maff)
12264 return NULL;
12266 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12267 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12268 "invalid space", goto error);
12270 space = isl_space_domain(isl_multi_aff_get_space(maff));
12271 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12272 if (rational)
12273 bmap = isl_basic_map_set_rational(bmap);
12275 for (i = 0; i < maff->n; ++i) {
12276 isl_aff *aff;
12277 isl_basic_map *bmap_i;
12279 aff = isl_aff_copy(maff->p[i]);
12280 bmap_i = isl_basic_map_from_aff2(aff, rational);
12282 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12285 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12287 isl_multi_aff_free(maff);
12288 return bmap;
12289 error:
12290 isl_multi_aff_free(maff);
12291 return NULL;
12294 /* Construct a basic map mapping the domain the multi-affine expression
12295 * to its range, with each dimension in the range equated to the
12296 * corresponding affine expression.
12298 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12299 __isl_take isl_multi_aff *ma)
12301 return isl_basic_map_from_multi_aff2(ma, 0);
12304 /* Construct a map mapping the domain the multi-affine expression
12305 * to its range, with each dimension in the range equated to the
12306 * corresponding affine expression.
12308 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12310 isl_basic_map *bmap;
12312 bmap = isl_basic_map_from_multi_aff(maff);
12313 return isl_map_from_basic_map(bmap);
12316 /* Construct a basic map mapping a domain in the given space to
12317 * to an n-dimensional range, with n the number of elements in the list,
12318 * where each coordinate in the range is prescribed by the
12319 * corresponding affine expression.
12320 * The domains of all affine expressions in the list are assumed to match
12321 * domain_dim.
12323 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12324 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12326 int i;
12327 isl_space *dim;
12328 isl_basic_map *bmap;
12330 if (!list)
12331 return NULL;
12333 dim = isl_space_from_domain(domain_dim);
12334 bmap = isl_basic_map_universe(dim);
12336 for (i = 0; i < list->n; ++i) {
12337 isl_aff *aff;
12338 isl_basic_map *bmap_i;
12340 aff = isl_aff_copy(list->p[i]);
12341 bmap_i = isl_basic_map_from_aff(aff);
12343 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12346 isl_aff_list_free(list);
12347 return bmap;
12350 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12351 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12353 return isl_map_equate(set, type1, pos1, type2, pos2);
12356 /* Construct a basic map where the given dimensions are equal to each other.
12358 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12359 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12361 isl_basic_map *bmap = NULL;
12362 int i;
12364 if (!space)
12365 return NULL;
12367 if (pos1 >= isl_space_dim(space, type1))
12368 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12369 "index out of bounds", goto error);
12370 if (pos2 >= isl_space_dim(space, type2))
12371 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12372 "index out of bounds", goto error);
12374 if (type1 == type2 && pos1 == pos2)
12375 return isl_basic_map_universe(space);
12377 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12378 i = isl_basic_map_alloc_equality(bmap);
12379 if (i < 0)
12380 goto error;
12381 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12382 pos1 += isl_basic_map_offset(bmap, type1);
12383 pos2 += isl_basic_map_offset(bmap, type2);
12384 isl_int_set_si(bmap->eq[i][pos1], -1);
12385 isl_int_set_si(bmap->eq[i][pos2], 1);
12386 bmap = isl_basic_map_finalize(bmap);
12387 isl_space_free(space);
12388 return bmap;
12389 error:
12390 isl_space_free(space);
12391 isl_basic_map_free(bmap);
12392 return NULL;
12395 /* Add a constraint imposing that the given two dimensions are equal.
12397 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12398 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12400 isl_basic_map *eq;
12402 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12404 bmap = isl_basic_map_intersect(bmap, eq);
12406 return bmap;
12409 /* Add a constraint imposing that the given two dimensions are equal.
12411 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12412 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12414 isl_basic_map *bmap;
12416 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12418 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12420 return map;
12423 /* Add a constraint imposing that the given two dimensions have opposite values.
12425 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12426 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12428 isl_basic_map *bmap = NULL;
12429 int i;
12431 if (!map)
12432 return NULL;
12434 if (pos1 >= isl_map_dim(map, type1))
12435 isl_die(map->ctx, isl_error_invalid,
12436 "index out of bounds", goto error);
12437 if (pos2 >= isl_map_dim(map, type2))
12438 isl_die(map->ctx, isl_error_invalid,
12439 "index out of bounds", goto error);
12441 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12442 i = isl_basic_map_alloc_equality(bmap);
12443 if (i < 0)
12444 goto error;
12445 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12446 pos1 += isl_basic_map_offset(bmap, type1);
12447 pos2 += isl_basic_map_offset(bmap, type2);
12448 isl_int_set_si(bmap->eq[i][pos1], 1);
12449 isl_int_set_si(bmap->eq[i][pos2], 1);
12450 bmap = isl_basic_map_finalize(bmap);
12452 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12454 return map;
12455 error:
12456 isl_basic_map_free(bmap);
12457 isl_map_free(map);
12458 return NULL;
12461 /* Construct a constraint imposing that the value of the first dimension is
12462 * greater than or equal to that of the second.
12464 static __isl_give isl_constraint *constraint_order_ge(
12465 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12466 enum isl_dim_type type2, int pos2)
12468 isl_constraint *c;
12470 if (!space)
12471 return NULL;
12473 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12475 if (pos1 >= isl_constraint_dim(c, type1))
12476 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12477 "index out of bounds", return isl_constraint_free(c));
12478 if (pos2 >= isl_constraint_dim(c, type2))
12479 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12480 "index out of bounds", return isl_constraint_free(c));
12482 if (type1 == type2 && pos1 == pos2)
12483 return c;
12485 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12486 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12488 return c;
12491 /* Add a constraint imposing that the value of the first dimension is
12492 * greater than or equal to that of the second.
12494 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12495 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12497 isl_constraint *c;
12498 isl_space *space;
12500 if (type1 == type2 && pos1 == pos2)
12501 return bmap;
12502 space = isl_basic_map_get_space(bmap);
12503 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12504 bmap = isl_basic_map_add_constraint(bmap, c);
12506 return bmap;
12509 /* Add a constraint imposing that the value of the first dimension is
12510 * greater than or equal to that of the second.
12512 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12513 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12515 isl_constraint *c;
12516 isl_space *space;
12518 if (type1 == type2 && pos1 == pos2)
12519 return map;
12520 space = isl_map_get_space(map);
12521 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12522 map = isl_map_add_constraint(map, c);
12524 return map;
12527 /* Add a constraint imposing that the value of the first dimension is
12528 * less than or equal to that of the second.
12530 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12531 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12533 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12536 /* Construct a basic map where the value of the first dimension is
12537 * greater than that of the second.
12539 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12540 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12542 isl_basic_map *bmap = NULL;
12543 int i;
12545 if (!space)
12546 return NULL;
12548 if (pos1 >= isl_space_dim(space, type1))
12549 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12550 "index out of bounds", goto error);
12551 if (pos2 >= isl_space_dim(space, type2))
12552 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12553 "index out of bounds", goto error);
12555 if (type1 == type2 && pos1 == pos2)
12556 return isl_basic_map_empty(space);
12558 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12559 i = isl_basic_map_alloc_inequality(bmap);
12560 if (i < 0)
12561 return isl_basic_map_free(bmap);
12562 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12563 pos1 += isl_basic_map_offset(bmap, type1);
12564 pos2 += isl_basic_map_offset(bmap, type2);
12565 isl_int_set_si(bmap->ineq[i][pos1], 1);
12566 isl_int_set_si(bmap->ineq[i][pos2], -1);
12567 isl_int_set_si(bmap->ineq[i][0], -1);
12568 bmap = isl_basic_map_finalize(bmap);
12570 return bmap;
12571 error:
12572 isl_space_free(space);
12573 isl_basic_map_free(bmap);
12574 return NULL;
12577 /* Add a constraint imposing that the value of the first dimension is
12578 * greater than that of the second.
12580 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12581 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12583 isl_basic_map *gt;
12585 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12587 bmap = isl_basic_map_intersect(bmap, gt);
12589 return bmap;
12592 /* Add a constraint imposing that the value of the first dimension is
12593 * greater than that of the second.
12595 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12596 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12598 isl_basic_map *bmap;
12600 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12602 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12604 return map;
12607 /* Add a constraint imposing that the value of the first dimension is
12608 * smaller than that of the second.
12610 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12611 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12613 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12616 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12617 int pos)
12619 isl_aff *div;
12620 isl_local_space *ls;
12622 if (!bmap)
12623 return NULL;
12625 if (!isl_basic_map_divs_known(bmap))
12626 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12627 "some divs are unknown", return NULL);
12629 ls = isl_basic_map_get_local_space(bmap);
12630 div = isl_local_space_get_div(ls, pos);
12631 isl_local_space_free(ls);
12633 return div;
12636 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12637 int pos)
12639 return isl_basic_map_get_div(bset, pos);
12642 /* Plug in "subs" for dimension "type", "pos" of "bset".
12644 * Let i be the dimension to replace and let "subs" be of the form
12646 * f/d
12648 * Any integer division with a non-zero coefficient for i,
12650 * floor((a i + g)/m)
12652 * is replaced by
12654 * floor((a f + d g)/(m d))
12656 * Constraints of the form
12658 * a i + g
12660 * are replaced by
12662 * a f + d g
12664 * We currently require that "subs" is an integral expression.
12665 * Handling rational expressions may require us to add stride constraints
12666 * as we do in isl_basic_set_preimage_multi_aff.
12668 __isl_give isl_basic_set *isl_basic_set_substitute(
12669 __isl_take isl_basic_set *bset,
12670 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12672 int i;
12673 isl_int v;
12674 isl_ctx *ctx;
12676 if (bset && isl_basic_set_plain_is_empty(bset))
12677 return bset;
12679 bset = isl_basic_set_cow(bset);
12680 if (!bset || !subs)
12681 goto error;
12683 ctx = isl_basic_set_get_ctx(bset);
12684 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12685 isl_die(ctx, isl_error_invalid,
12686 "spaces don't match", goto error);
12687 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12688 isl_die(ctx, isl_error_unsupported,
12689 "cannot handle divs yet", goto error);
12690 if (!isl_int_is_one(subs->v->el[0]))
12691 isl_die(ctx, isl_error_invalid,
12692 "can only substitute integer expressions", goto error);
12694 pos += isl_basic_set_offset(bset, type);
12696 isl_int_init(v);
12698 for (i = 0; i < bset->n_eq; ++i) {
12699 if (isl_int_is_zero(bset->eq[i][pos]))
12700 continue;
12701 isl_int_set(v, bset->eq[i][pos]);
12702 isl_int_set_si(bset->eq[i][pos], 0);
12703 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12704 v, subs->v->el + 1, subs->v->size - 1);
12707 for (i = 0; i < bset->n_ineq; ++i) {
12708 if (isl_int_is_zero(bset->ineq[i][pos]))
12709 continue;
12710 isl_int_set(v, bset->ineq[i][pos]);
12711 isl_int_set_si(bset->ineq[i][pos], 0);
12712 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12713 v, subs->v->el + 1, subs->v->size - 1);
12716 for (i = 0; i < bset->n_div; ++i) {
12717 if (isl_int_is_zero(bset->div[i][1 + pos]))
12718 continue;
12719 isl_int_set(v, bset->div[i][1 + pos]);
12720 isl_int_set_si(bset->div[i][1 + pos], 0);
12721 isl_seq_combine(bset->div[i] + 1,
12722 subs->v->el[0], bset->div[i] + 1,
12723 v, subs->v->el + 1, subs->v->size - 1);
12724 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12727 isl_int_clear(v);
12729 bset = isl_basic_set_simplify(bset);
12730 return isl_basic_set_finalize(bset);
12731 error:
12732 isl_basic_set_free(bset);
12733 return NULL;
12736 /* Plug in "subs" for dimension "type", "pos" of "set".
12738 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12739 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12741 int i;
12743 if (set && isl_set_plain_is_empty(set))
12744 return set;
12746 set = isl_set_cow(set);
12747 if (!set || !subs)
12748 goto error;
12750 for (i = set->n - 1; i >= 0; --i) {
12751 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12752 if (remove_if_empty(set, i) < 0)
12753 goto error;
12756 return set;
12757 error:
12758 isl_set_free(set);
12759 return NULL;
12762 /* Check if the range of "ma" is compatible with the domain or range
12763 * (depending on "type") of "bmap".
12765 static isl_stat check_basic_map_compatible_range_multi_aff(
12766 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12767 __isl_keep isl_multi_aff *ma)
12769 isl_bool m;
12770 isl_space *ma_space;
12772 ma_space = isl_multi_aff_get_space(ma);
12774 m = isl_space_has_equal_params(bmap->dim, ma_space);
12775 if (m < 0)
12776 goto error;
12777 if (!m)
12778 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12779 "parameters don't match", goto error);
12780 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12781 if (m < 0)
12782 goto error;
12783 if (!m)
12784 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12785 "spaces don't match", goto error);
12787 isl_space_free(ma_space);
12788 return isl_stat_ok;
12789 error:
12790 isl_space_free(ma_space);
12791 return isl_stat_error;
12794 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12795 * coefficients before the transformed range of dimensions,
12796 * the "n_after" coefficients after the transformed range of dimensions
12797 * and the coefficients of the other divs in "bmap".
12799 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12800 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12802 int i;
12803 int n_param;
12804 int n_set;
12805 isl_local_space *ls;
12807 if (n_div == 0)
12808 return 0;
12810 ls = isl_aff_get_domain_local_space(ma->p[0]);
12811 if (!ls)
12812 return -1;
12814 n_param = isl_local_space_dim(ls, isl_dim_param);
12815 n_set = isl_local_space_dim(ls, isl_dim_set);
12816 for (i = 0; i < n_div; ++i) {
12817 int o_bmap = 0, o_ls = 0;
12819 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12820 o_bmap += 1 + 1 + n_param;
12821 o_ls += 1 + 1 + n_param;
12822 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12823 o_bmap += n_before;
12824 isl_seq_cpy(bmap->div[i] + o_bmap,
12825 ls->div->row[i] + o_ls, n_set);
12826 o_bmap += n_set;
12827 o_ls += n_set;
12828 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12829 o_bmap += n_after;
12830 isl_seq_cpy(bmap->div[i] + o_bmap,
12831 ls->div->row[i] + o_ls, n_div);
12832 o_bmap += n_div;
12833 o_ls += n_div;
12834 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12835 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12836 goto error;
12839 isl_local_space_free(ls);
12840 return 0;
12841 error:
12842 isl_local_space_free(ls);
12843 return -1;
12846 /* How many stride constraints does "ma" enforce?
12847 * That is, how many of the affine expressions have a denominator
12848 * different from one?
12850 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12852 int i;
12853 int strides = 0;
12855 for (i = 0; i < ma->n; ++i)
12856 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12857 strides++;
12859 return strides;
12862 /* For each affine expression in ma of the form
12864 * x_i = (f_i y + h_i)/m_i
12866 * with m_i different from one, add a constraint to "bmap"
12867 * of the form
12869 * f_i y + h_i = m_i alpha_i
12871 * with alpha_i an additional existentially quantified variable.
12873 * The input variables of "ma" correspond to a subset of the variables
12874 * of "bmap". There are "n_before" variables in "bmap" before this
12875 * subset and "n_after" variables after this subset.
12876 * The integer divisions of the affine expressions in "ma" are assumed
12877 * to have been aligned. There are "n_div_ma" of them and
12878 * they appear first in "bmap", straight after the "n_after" variables.
12880 static __isl_give isl_basic_map *add_ma_strides(
12881 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12882 int n_before, int n_after, int n_div_ma)
12884 int i, k;
12885 int div;
12886 int total;
12887 int n_param;
12888 int n_in;
12890 total = isl_basic_map_total_dim(bmap);
12891 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12892 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12893 for (i = 0; i < ma->n; ++i) {
12894 int o_bmap = 0, o_ma = 1;
12896 if (isl_int_is_one(ma->p[i]->v->el[0]))
12897 continue;
12898 div = isl_basic_map_alloc_div(bmap);
12899 k = isl_basic_map_alloc_equality(bmap);
12900 if (div < 0 || k < 0)
12901 goto error;
12902 isl_int_set_si(bmap->div[div][0], 0);
12903 isl_seq_cpy(bmap->eq[k] + o_bmap,
12904 ma->p[i]->v->el + o_ma, 1 + n_param);
12905 o_bmap += 1 + n_param;
12906 o_ma += 1 + n_param;
12907 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12908 o_bmap += n_before;
12909 isl_seq_cpy(bmap->eq[k] + o_bmap,
12910 ma->p[i]->v->el + o_ma, n_in);
12911 o_bmap += n_in;
12912 o_ma += n_in;
12913 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12914 o_bmap += n_after;
12915 isl_seq_cpy(bmap->eq[k] + o_bmap,
12916 ma->p[i]->v->el + o_ma, n_div_ma);
12917 o_bmap += n_div_ma;
12918 o_ma += n_div_ma;
12919 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12920 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12921 total++;
12924 return bmap;
12925 error:
12926 isl_basic_map_free(bmap);
12927 return NULL;
12930 /* Replace the domain or range space (depending on "type) of "space" by "set".
12932 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12933 enum isl_dim_type type, __isl_take isl_space *set)
12935 if (type == isl_dim_in) {
12936 space = isl_space_range(space);
12937 space = isl_space_map_from_domain_and_range(set, space);
12938 } else {
12939 space = isl_space_domain(space);
12940 space = isl_space_map_from_domain_and_range(space, set);
12943 return space;
12946 /* Compute the preimage of the domain or range (depending on "type")
12947 * of "bmap" under the function represented by "ma".
12948 * In other words, plug in "ma" in the domain or range of "bmap".
12949 * The result is a basic map that lives in the same space as "bmap"
12950 * except that the domain or range has been replaced by
12951 * the domain space of "ma".
12953 * If bmap is represented by
12955 * A(p) + S u + B x + T v + C(divs) >= 0,
12957 * where u and x are input and output dimensions if type == isl_dim_out
12958 * while x and v are input and output dimensions if type == isl_dim_in,
12959 * and ma is represented by
12961 * x = D(p) + F(y) + G(divs')
12963 * then the result is
12965 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12967 * The divs in the input set are similarly adjusted.
12968 * In particular
12970 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12972 * becomes
12974 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12975 * B_i G(divs') + c_i(divs))/n_i)
12977 * If bmap is not a rational map and if F(y) involves any denominators
12979 * x_i = (f_i y + h_i)/m_i
12981 * then additional constraints are added to ensure that we only
12982 * map back integer points. That is we enforce
12984 * f_i y + h_i = m_i alpha_i
12986 * with alpha_i an additional existentially quantified variable.
12988 * We first copy over the divs from "ma".
12989 * Then we add the modified constraints and divs from "bmap".
12990 * Finally, we add the stride constraints, if needed.
12992 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12993 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12994 __isl_take isl_multi_aff *ma)
12996 int i, k;
12997 isl_space *space;
12998 isl_basic_map *res = NULL;
12999 int n_before, n_after, n_div_bmap, n_div_ma;
13000 isl_int f, c1, c2, g;
13001 isl_bool rational;
13002 int strides;
13004 isl_int_init(f);
13005 isl_int_init(c1);
13006 isl_int_init(c2);
13007 isl_int_init(g);
13009 ma = isl_multi_aff_align_divs(ma);
13010 if (!bmap || !ma)
13011 goto error;
13012 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
13013 goto error;
13015 if (type == isl_dim_in) {
13016 n_before = 0;
13017 n_after = isl_basic_map_dim(bmap, isl_dim_out);
13018 } else {
13019 n_before = isl_basic_map_dim(bmap, isl_dim_in);
13020 n_after = 0;
13022 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
13023 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
13025 space = isl_multi_aff_get_domain_space(ma);
13026 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
13027 rational = isl_basic_map_is_rational(bmap);
13028 strides = rational ? 0 : multi_aff_strides(ma);
13029 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
13030 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
13031 if (rational)
13032 res = isl_basic_map_set_rational(res);
13034 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
13035 if (isl_basic_map_alloc_div(res) < 0)
13036 goto error;
13038 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
13039 goto error;
13041 for (i = 0; i < bmap->n_eq; ++i) {
13042 k = isl_basic_map_alloc_equality(res);
13043 if (k < 0)
13044 goto error;
13045 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13046 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13049 for (i = 0; i < bmap->n_ineq; ++i) {
13050 k = isl_basic_map_alloc_inequality(res);
13051 if (k < 0)
13052 goto error;
13053 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13054 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13057 for (i = 0; i < bmap->n_div; ++i) {
13058 if (isl_int_is_zero(bmap->div[i][0])) {
13059 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13060 continue;
13062 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13063 n_before, n_after, n_div_ma, n_div_bmap,
13064 f, c1, c2, g, 1);
13067 if (strides)
13068 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13070 isl_int_clear(f);
13071 isl_int_clear(c1);
13072 isl_int_clear(c2);
13073 isl_int_clear(g);
13074 isl_basic_map_free(bmap);
13075 isl_multi_aff_free(ma);
13076 res = isl_basic_map_simplify(res);
13077 return isl_basic_map_finalize(res);
13078 error:
13079 isl_int_clear(f);
13080 isl_int_clear(c1);
13081 isl_int_clear(c2);
13082 isl_int_clear(g);
13083 isl_basic_map_free(bmap);
13084 isl_multi_aff_free(ma);
13085 isl_basic_map_free(res);
13086 return NULL;
13089 /* Compute the preimage of "bset" under the function represented by "ma".
13090 * In other words, plug in "ma" in "bset". The result is a basic set
13091 * that lives in the domain space of "ma".
13093 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13094 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13096 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13099 /* Compute the preimage of the domain of "bmap" under the function
13100 * represented by "ma".
13101 * In other words, plug in "ma" in the domain of "bmap".
13102 * The result is a basic map that lives in the same space as "bmap"
13103 * except that the domain has been replaced by the domain space of "ma".
13105 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13106 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13108 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13111 /* Compute the preimage of the range of "bmap" under the function
13112 * represented by "ma".
13113 * In other words, plug in "ma" in the range of "bmap".
13114 * The result is a basic map that lives in the same space as "bmap"
13115 * except that the range has been replaced by the domain space of "ma".
13117 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13118 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13120 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13123 /* Check if the range of "ma" is compatible with the domain or range
13124 * (depending on "type") of "map".
13125 * Return isl_stat_error if anything is wrong.
13127 static isl_stat check_map_compatible_range_multi_aff(
13128 __isl_keep isl_map *map, enum isl_dim_type type,
13129 __isl_keep isl_multi_aff *ma)
13131 isl_bool m;
13132 isl_space *ma_space;
13134 ma_space = isl_multi_aff_get_space(ma);
13135 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13136 isl_space_free(ma_space);
13137 if (m < 0)
13138 return isl_stat_error;
13139 if (!m)
13140 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13141 "spaces don't match", return isl_stat_error);
13142 return isl_stat_ok;
13145 /* Compute the preimage of the domain or range (depending on "type")
13146 * of "map" under the function represented by "ma".
13147 * In other words, plug in "ma" in the domain or range of "map".
13148 * The result is a map that lives in the same space as "map"
13149 * except that the domain or range has been replaced by
13150 * the domain space of "ma".
13152 * The parameters are assumed to have been aligned.
13154 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13155 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13157 int i;
13158 isl_space *space;
13160 map = isl_map_cow(map);
13161 ma = isl_multi_aff_align_divs(ma);
13162 if (!map || !ma)
13163 goto error;
13164 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13165 goto error;
13167 for (i = 0; i < map->n; ++i) {
13168 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13169 isl_multi_aff_copy(ma));
13170 if (!map->p[i])
13171 goto error;
13174 space = isl_multi_aff_get_domain_space(ma);
13175 space = isl_space_set(isl_map_get_space(map), type, space);
13177 isl_space_free(map->dim);
13178 map->dim = space;
13179 if (!map->dim)
13180 goto error;
13182 isl_multi_aff_free(ma);
13183 if (map->n > 1)
13184 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13185 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13186 return map;
13187 error:
13188 isl_multi_aff_free(ma);
13189 isl_map_free(map);
13190 return NULL;
13193 /* Compute the preimage of the domain or range (depending on "type")
13194 * of "map" under the function represented by "ma".
13195 * In other words, plug in "ma" in the domain or range of "map".
13196 * The result is a map that lives in the same space as "map"
13197 * except that the domain or range has been replaced by
13198 * the domain space of "ma".
13200 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13201 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13203 isl_bool aligned;
13205 if (!map || !ma)
13206 goto error;
13208 aligned = isl_map_space_has_equal_params(map, ma->space);
13209 if (aligned < 0)
13210 goto error;
13211 if (aligned)
13212 return map_preimage_multi_aff(map, type, ma);
13214 if (isl_map_check_named_params(map) < 0)
13215 goto error;
13216 if (!isl_space_has_named_params(ma->space))
13217 isl_die(map->ctx, isl_error_invalid,
13218 "unaligned unnamed parameters", goto error);
13219 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13220 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13222 return map_preimage_multi_aff(map, type, ma);
13223 error:
13224 isl_multi_aff_free(ma);
13225 return isl_map_free(map);
13228 /* Compute the preimage of "set" under the function represented by "ma".
13229 * In other words, plug in "ma" in "set". The result is a set
13230 * that lives in the domain space of "ma".
13232 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13233 __isl_take isl_multi_aff *ma)
13235 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13238 /* Compute the preimage of the domain of "map" under the function
13239 * represented by "ma".
13240 * In other words, plug in "ma" in the domain of "map".
13241 * The result is a map that lives in the same space as "map"
13242 * except that the domain has been replaced by the domain space of "ma".
13244 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13245 __isl_take isl_multi_aff *ma)
13247 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13250 /* Compute the preimage of the range of "map" under the function
13251 * represented by "ma".
13252 * In other words, plug in "ma" in the range of "map".
13253 * The result is a map that lives in the same space as "map"
13254 * except that the range has been replaced by the domain space of "ma".
13256 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13257 __isl_take isl_multi_aff *ma)
13259 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13262 /* Compute the preimage of "map" under the function represented by "pma".
13263 * In other words, plug in "pma" in the domain or range of "map".
13264 * The result is a map that lives in the same space as "map",
13265 * except that the space of type "type" has been replaced by
13266 * the domain space of "pma".
13268 * The parameters of "map" and "pma" are assumed to have been aligned.
13270 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13271 __isl_take isl_map *map, enum isl_dim_type type,
13272 __isl_take isl_pw_multi_aff *pma)
13274 int i;
13275 isl_map *res;
13277 if (!pma)
13278 goto error;
13280 if (pma->n == 0) {
13281 isl_pw_multi_aff_free(pma);
13282 res = isl_map_empty(isl_map_get_space(map));
13283 isl_map_free(map);
13284 return res;
13287 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13288 isl_multi_aff_copy(pma->p[0].maff));
13289 if (type == isl_dim_in)
13290 res = isl_map_intersect_domain(res,
13291 isl_map_copy(pma->p[0].set));
13292 else
13293 res = isl_map_intersect_range(res,
13294 isl_map_copy(pma->p[0].set));
13296 for (i = 1; i < pma->n; ++i) {
13297 isl_map *res_i;
13299 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13300 isl_multi_aff_copy(pma->p[i].maff));
13301 if (type == isl_dim_in)
13302 res_i = isl_map_intersect_domain(res_i,
13303 isl_map_copy(pma->p[i].set));
13304 else
13305 res_i = isl_map_intersect_range(res_i,
13306 isl_map_copy(pma->p[i].set));
13307 res = isl_map_union(res, res_i);
13310 isl_pw_multi_aff_free(pma);
13311 isl_map_free(map);
13312 return res;
13313 error:
13314 isl_pw_multi_aff_free(pma);
13315 isl_map_free(map);
13316 return NULL;
13319 /* Compute the preimage of "map" under the function represented by "pma".
13320 * In other words, plug in "pma" in the domain or range of "map".
13321 * The result is a map that lives in the same space as "map",
13322 * except that the space of type "type" has been replaced by
13323 * the domain space of "pma".
13325 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13326 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13328 isl_bool aligned;
13330 if (!map || !pma)
13331 goto error;
13333 aligned = isl_map_space_has_equal_params(map, pma->dim);
13334 if (aligned < 0)
13335 goto error;
13336 if (aligned)
13337 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13339 if (isl_map_check_named_params(map) < 0)
13340 goto error;
13341 if (!isl_space_has_named_params(pma->dim))
13342 isl_die(map->ctx, isl_error_invalid,
13343 "unaligned unnamed parameters", goto error);
13344 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13345 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13347 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13348 error:
13349 isl_pw_multi_aff_free(pma);
13350 return isl_map_free(map);
13353 /* Compute the preimage of "set" under the function represented by "pma".
13354 * In other words, plug in "pma" in "set". The result is a set
13355 * that lives in the domain space of "pma".
13357 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13358 __isl_take isl_pw_multi_aff *pma)
13360 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13363 /* Compute the preimage of the domain of "map" under the function
13364 * represented by "pma".
13365 * In other words, plug in "pma" in the domain of "map".
13366 * The result is a map that lives in the same space as "map",
13367 * except that domain space has been replaced by the domain space of "pma".
13369 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13370 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13372 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13375 /* Compute the preimage of the range of "map" under the function
13376 * represented by "pma".
13377 * In other words, plug in "pma" in the range of "map".
13378 * The result is a map that lives in the same space as "map",
13379 * except that range space has been replaced by the domain space of "pma".
13381 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13382 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13384 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13387 /* Compute the preimage of "map" under the function represented by "mpa".
13388 * In other words, plug in "mpa" in the domain or range of "map".
13389 * The result is a map that lives in the same space as "map",
13390 * except that the space of type "type" has been replaced by
13391 * the domain space of "mpa".
13393 * If the map does not involve any constraints that refer to the
13394 * dimensions of the substituted space, then the only possible
13395 * effect of "mpa" on the map is to map the space to a different space.
13396 * We create a separate isl_multi_aff to effectuate this change
13397 * in order to avoid spurious splitting of the map along the pieces
13398 * of "mpa".
13400 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13401 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13403 int n;
13404 isl_pw_multi_aff *pma;
13406 if (!map || !mpa)
13407 goto error;
13409 n = isl_map_dim(map, type);
13410 if (!isl_map_involves_dims(map, type, 0, n)) {
13411 isl_space *space;
13412 isl_multi_aff *ma;
13414 space = isl_multi_pw_aff_get_space(mpa);
13415 isl_multi_pw_aff_free(mpa);
13416 ma = isl_multi_aff_zero(space);
13417 return isl_map_preimage_multi_aff(map, type, ma);
13420 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13421 return isl_map_preimage_pw_multi_aff(map, type, pma);
13422 error:
13423 isl_map_free(map);
13424 isl_multi_pw_aff_free(mpa);
13425 return NULL;
13428 /* Compute the preimage of "map" under the function represented by "mpa".
13429 * In other words, plug in "mpa" in the domain "map".
13430 * The result is a map that lives in the same space as "map",
13431 * except that domain space has been replaced by the domain space of "mpa".
13433 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13434 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13436 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13439 /* Compute the preimage of "set" by the function represented by "mpa".
13440 * In other words, plug in "mpa" in "set".
13442 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13443 __isl_take isl_multi_pw_aff *mpa)
13445 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13448 /* Return a copy of the equality constraints of "bset" as a matrix.
13450 __isl_give isl_mat *isl_basic_set_extract_equalities(
13451 __isl_keep isl_basic_set *bset)
13453 isl_ctx *ctx;
13454 unsigned total;
13456 if (!bset)
13457 return NULL;
13459 ctx = isl_basic_set_get_ctx(bset);
13460 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13461 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13464 /* Are the "n" "coefficients" starting at "first" of the integer division
13465 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13466 * to each other?
13467 * The "coefficient" at position 0 is the denominator.
13468 * The "coefficient" at position 1 is the constant term.
13470 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13471 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13472 unsigned first, unsigned n)
13474 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13475 return isl_bool_error;
13476 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13477 return isl_bool_error;
13478 return isl_seq_eq(bmap1->div[pos1] + first,
13479 bmap2->div[pos2] + first, n);
13482 /* Are the integer division expressions at position "pos1" in "bmap1" and
13483 * "pos2" in "bmap2" equal to each other, except that the constant terms
13484 * are different?
13486 isl_bool isl_basic_map_equal_div_expr_except_constant(
13487 __isl_keep isl_basic_map *bmap1, int pos1,
13488 __isl_keep isl_basic_map *bmap2, int pos2)
13490 isl_bool equal;
13491 unsigned total;
13493 if (!bmap1 || !bmap2)
13494 return isl_bool_error;
13495 total = isl_basic_map_total_dim(bmap1);
13496 if (total != isl_basic_map_total_dim(bmap2))
13497 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13498 "incomparable div expressions", return isl_bool_error);
13499 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13500 0, 1);
13501 if (equal < 0 || !equal)
13502 return equal;
13503 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13504 1, 1);
13505 if (equal < 0 || equal)
13506 return isl_bool_not(equal);
13507 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13508 2, total);
13511 /* Replace the numerator of the constant term of the integer division
13512 * expression at position "div" in "bmap" by "value".
13513 * The caller guarantees that this does not change the meaning
13514 * of the input.
13516 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13517 __isl_take isl_basic_map *bmap, int div, int value)
13519 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13520 return isl_basic_map_free(bmap);
13522 isl_int_set_si(bmap->div[div][1], value);
13524 return bmap;
13527 /* Is the point "inner" internal to inequality constraint "ineq"
13528 * of "bset"?
13529 * The point is considered to be internal to the inequality constraint,
13530 * if it strictly lies on the positive side of the inequality constraint,
13531 * or if it lies on the constraint and the constraint is lexico-positive.
13533 static isl_bool is_internal(__isl_keep isl_vec *inner,
13534 __isl_keep isl_basic_set *bset, int ineq)
13536 isl_ctx *ctx;
13537 int pos;
13538 unsigned total;
13540 if (!inner || !bset)
13541 return isl_bool_error;
13543 ctx = isl_basic_set_get_ctx(bset);
13544 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13545 &ctx->normalize_gcd);
13546 if (!isl_int_is_zero(ctx->normalize_gcd))
13547 return isl_int_is_nonneg(ctx->normalize_gcd);
13549 total = isl_basic_set_dim(bset, isl_dim_all);
13550 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13551 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13554 /* Tighten the inequality constraints of "bset" that are outward with respect
13555 * to the point "vec".
13556 * That is, tighten the constraints that are not satisfied by "vec".
13558 * "vec" is a point internal to some superset S of "bset" that is used
13559 * to make the subsets of S disjoint, by tightening one half of the constraints
13560 * that separate two subsets. In particular, the constraints of S
13561 * are all satisfied by "vec" and should not be tightened.
13562 * Of the internal constraints, those that have "vec" on the outside
13563 * are tightened. The shared facet is included in the adjacent subset
13564 * with the opposite constraint.
13565 * For constraints that saturate "vec", this criterion cannot be used
13566 * to determine which of the two sides should be tightened.
13567 * Instead, the sign of the first non-zero coefficient is used
13568 * to make this choice. Note that this second criterion is never used
13569 * on the constraints of S since "vec" is interior to "S".
13571 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13572 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13574 int j;
13576 bset = isl_basic_set_cow(bset);
13577 if (!bset)
13578 return NULL;
13579 for (j = 0; j < bset->n_ineq; ++j) {
13580 isl_bool internal;
13582 internal = is_internal(vec, bset, j);
13583 if (internal < 0)
13584 return isl_basic_set_free(bset);
13585 if (internal)
13586 continue;
13587 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13590 return bset;
13593 /* Replace the variables x of type "type" starting at "first" in "bmap"
13594 * by x' with x = M x' with M the matrix trans.
13595 * That is, replace the corresponding coefficients c by c M.
13597 * The transformation matrix should be a square matrix.
13599 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13600 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13601 __isl_take isl_mat *trans)
13603 unsigned pos;
13605 bmap = isl_basic_map_cow(bmap);
13606 if (!bmap || !trans)
13607 goto error;
13609 if (trans->n_row != trans->n_col)
13610 isl_die(trans->ctx, isl_error_invalid,
13611 "expecting square transformation matrix", goto error);
13612 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
13613 isl_die(trans->ctx, isl_error_invalid,
13614 "oversized transformation matrix", goto error);
13616 pos = isl_basic_map_offset(bmap, type) + first;
13618 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13619 isl_mat_copy(trans)) < 0)
13620 goto error;
13621 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13622 isl_mat_copy(trans)) < 0)
13623 goto error;
13624 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13625 isl_mat_copy(trans)) < 0)
13626 goto error;
13628 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
13629 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13631 isl_mat_free(trans);
13632 return bmap;
13633 error:
13634 isl_mat_free(trans);
13635 isl_basic_map_free(bmap);
13636 return NULL;
13639 /* Replace the variables x of type "type" starting at "first" in "bset"
13640 * by x' with x = M x' with M the matrix trans.
13641 * That is, replace the corresponding coefficients c by c M.
13643 * The transformation matrix should be a square matrix.
13645 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13646 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13647 __isl_take isl_mat *trans)
13649 return isl_basic_map_transform_dims(bset, type, first, trans);