privately expose isl_aff_scale_down
[isl.git] / isl_map.c
blob61f9c6f03af748182fff2e3b771a9082a5947717
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 /* Given a basic map with one output dimension, compute the minimum or
6871 * maximum of that dimension as an isl_pw_aff.
6873 * Compute the optimum as a lexicographic optimum over the single
6874 * output dimension and extract the single isl_pw_aff from the result.
6876 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6877 int max)
6879 isl_pw_multi_aff *pma;
6880 isl_pw_aff *pwaff;
6882 bmap = isl_basic_map_copy(bmap);
6883 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6884 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6885 isl_pw_multi_aff_free(pma);
6887 return pwaff;
6890 /* Compute the minimum or maximum of the given output dimension
6891 * as a function of the parameters and the input dimensions,
6892 * but independently of the other output dimensions.
6894 * We first project out the other output dimension and then compute
6895 * the "lexicographic" maximum in each basic map, combining the results
6896 * using isl_pw_aff_union_max.
6898 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6899 int max)
6901 int i;
6902 isl_pw_aff *pwaff;
6903 unsigned n_out;
6905 n_out = isl_map_dim(map, isl_dim_out);
6906 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6907 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6908 if (!map)
6909 return NULL;
6911 if (map->n == 0) {
6912 isl_space *dim = isl_map_get_space(map);
6913 isl_map_free(map);
6914 return isl_pw_aff_empty(dim);
6917 pwaff = basic_map_dim_opt(map->p[0], max);
6918 for (i = 1; i < map->n; ++i) {
6919 isl_pw_aff *pwaff_i;
6921 pwaff_i = basic_map_dim_opt(map->p[i], max);
6922 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6925 isl_map_free(map);
6927 return pwaff;
6930 /* Compute the minimum of the given output dimension as a function of the
6931 * parameters and input dimensions, but independently of
6932 * the other output dimensions.
6934 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6936 return map_dim_opt(map, pos, 0);
6939 /* Compute the maximum of the given output dimension as a function of the
6940 * parameters and input dimensions, but independently of
6941 * the other output dimensions.
6943 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6945 return map_dim_opt(map, pos, 1);
6948 /* Compute the minimum or maximum of the given set dimension
6949 * as a function of the parameters,
6950 * but independently of the other set dimensions.
6952 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6953 int max)
6955 return map_dim_opt(set, pos, max);
6958 /* Compute the maximum of the given set dimension as a function of the
6959 * parameters, but independently of the other set dimensions.
6961 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6963 return set_dim_opt(set, pos, 1);
6966 /* Compute the minimum of the given set dimension as a function of the
6967 * parameters, but independently of the other set dimensions.
6969 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6971 return set_dim_opt(set, pos, 0);
6974 /* Apply a preimage specified by "mat" on the parameters of "bset".
6975 * bset is assumed to have only parameters and divs.
6977 static __isl_give isl_basic_set *basic_set_parameter_preimage(
6978 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
6980 unsigned nparam;
6982 if (!bset || !mat)
6983 goto error;
6985 bset->dim = isl_space_cow(bset->dim);
6986 if (!bset->dim)
6987 goto error;
6989 nparam = isl_basic_set_dim(bset, isl_dim_param);
6991 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6993 bset->dim->nparam = 0;
6994 bset->dim->n_out = nparam;
6995 bset = isl_basic_set_preimage(bset, mat);
6996 if (bset) {
6997 bset->dim->nparam = bset->dim->n_out;
6998 bset->dim->n_out = 0;
7000 return bset;
7001 error:
7002 isl_mat_free(mat);
7003 isl_basic_set_free(bset);
7004 return NULL;
7007 /* Apply a preimage specified by "mat" on the parameters of "set".
7008 * set is assumed to have only parameters and divs.
7010 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7011 __isl_take isl_mat *mat)
7013 isl_space *space;
7014 unsigned nparam;
7016 if (!set || !mat)
7017 goto error;
7019 nparam = isl_set_dim(set, isl_dim_param);
7021 if (mat->n_row != 1 + nparam)
7022 isl_die(isl_set_get_ctx(set), isl_error_internal,
7023 "unexpected number of rows", goto error);
7025 space = isl_set_get_space(set);
7026 space = isl_space_move_dims(space, isl_dim_set, 0,
7027 isl_dim_param, 0, nparam);
7028 set = isl_set_reset_space(set, space);
7029 set = isl_set_preimage(set, mat);
7030 nparam = isl_set_dim(set, isl_dim_out);
7031 space = isl_set_get_space(set);
7032 space = isl_space_move_dims(space, isl_dim_param, 0,
7033 isl_dim_out, 0, nparam);
7034 set = isl_set_reset_space(set, space);
7035 return set;
7036 error:
7037 isl_mat_free(mat);
7038 isl_set_free(set);
7039 return NULL;
7042 /* Intersect the basic set "bset" with the affine space specified by the
7043 * equalities in "eq".
7045 static __isl_give isl_basic_set *basic_set_append_equalities(
7046 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7048 int i, k;
7049 unsigned len;
7051 if (!bset || !eq)
7052 goto error;
7054 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7055 eq->n_row, 0);
7056 if (!bset)
7057 goto error;
7059 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7060 for (i = 0; i < eq->n_row; ++i) {
7061 k = isl_basic_set_alloc_equality(bset);
7062 if (k < 0)
7063 goto error;
7064 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7065 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7067 isl_mat_free(eq);
7069 bset = isl_basic_set_gauss(bset, NULL);
7070 bset = isl_basic_set_finalize(bset);
7072 return bset;
7073 error:
7074 isl_mat_free(eq);
7075 isl_basic_set_free(bset);
7076 return NULL;
7079 /* Intersect the set "set" with the affine space specified by the
7080 * equalities in "eq".
7082 static struct isl_set *set_append_equalities(struct isl_set *set,
7083 struct isl_mat *eq)
7085 int i;
7087 if (!set || !eq)
7088 goto error;
7090 for (i = 0; i < set->n; ++i) {
7091 set->p[i] = basic_set_append_equalities(set->p[i],
7092 isl_mat_copy(eq));
7093 if (!set->p[i])
7094 goto error;
7096 isl_mat_free(eq);
7097 return set;
7098 error:
7099 isl_mat_free(eq);
7100 isl_set_free(set);
7101 return NULL;
7104 /* Given a basic set "bset" that only involves parameters and existentially
7105 * quantified variables, return the index of the first equality
7106 * that only involves parameters. If there is no such equality then
7107 * return bset->n_eq.
7109 * This function assumes that isl_basic_set_gauss has been called on "bset".
7111 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7113 int i, j;
7114 unsigned nparam, n_div;
7116 if (!bset)
7117 return -1;
7119 nparam = isl_basic_set_dim(bset, isl_dim_param);
7120 n_div = isl_basic_set_dim(bset, isl_dim_div);
7122 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7123 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7124 ++i;
7127 return i;
7130 /* Compute an explicit representation for the existentially quantified
7131 * variables in "bset" by computing the "minimal value" of the set
7132 * variables. Since there are no set variables, the computation of
7133 * the minimal value essentially computes an explicit representation
7134 * of the non-empty part(s) of "bset".
7136 * The input only involves parameters and existentially quantified variables.
7137 * All equalities among parameters have been removed.
7139 * Since the existentially quantified variables in the result are in general
7140 * going to be different from those in the input, we first replace
7141 * them by the minimal number of variables based on their equalities.
7142 * This should simplify the parametric integer programming.
7144 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7146 isl_morph *morph1, *morph2;
7147 isl_set *set;
7148 unsigned n;
7150 if (!bset)
7151 return NULL;
7152 if (bset->n_eq == 0)
7153 return isl_basic_set_lexmin_compute_divs(bset);
7155 morph1 = isl_basic_set_parameter_compression(bset);
7156 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7157 bset = isl_basic_set_lift(bset);
7158 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7159 bset = isl_morph_basic_set(morph2, bset);
7160 n = isl_basic_set_dim(bset, isl_dim_set);
7161 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7163 set = isl_basic_set_lexmin_compute_divs(bset);
7165 set = isl_morph_set(isl_morph_inverse(morph1), set);
7167 return set;
7170 /* Project the given basic set onto its parameter domain, possibly introducing
7171 * new, explicit, existential variables in the constraints.
7172 * The input has parameters and (possibly implicit) existential variables.
7173 * The output has the same parameters, but only
7174 * explicit existentially quantified variables.
7176 * The actual projection is performed by pip, but pip doesn't seem
7177 * to like equalities very much, so we first remove the equalities
7178 * among the parameters by performing a variable compression on
7179 * the parameters. Afterward, an inverse transformation is performed
7180 * and the equalities among the parameters are inserted back in.
7182 * The variable compression on the parameters may uncover additional
7183 * equalities that were only implicit before. We therefore check
7184 * if there are any new parameter equalities in the result and
7185 * if so recurse. The removal of parameter equalities is required
7186 * for the parameter compression performed by base_compute_divs.
7188 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7190 int i;
7191 struct isl_mat *eq;
7192 struct isl_mat *T, *T2;
7193 struct isl_set *set;
7194 unsigned nparam;
7196 bset = isl_basic_set_cow(bset);
7197 if (!bset)
7198 return NULL;
7200 if (bset->n_eq == 0)
7201 return base_compute_divs(bset);
7203 bset = isl_basic_set_gauss(bset, NULL);
7204 if (!bset)
7205 return NULL;
7206 if (isl_basic_set_plain_is_empty(bset))
7207 return isl_set_from_basic_set(bset);
7209 i = first_parameter_equality(bset);
7210 if (i == bset->n_eq)
7211 return base_compute_divs(bset);
7213 nparam = isl_basic_set_dim(bset, isl_dim_param);
7214 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7215 0, 1 + nparam);
7216 eq = isl_mat_cow(eq);
7217 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7218 if (T && T->n_col == 0) {
7219 isl_mat_free(T);
7220 isl_mat_free(T2);
7221 isl_mat_free(eq);
7222 bset = isl_basic_set_set_to_empty(bset);
7223 return isl_set_from_basic_set(bset);
7225 bset = basic_set_parameter_preimage(bset, T);
7227 i = first_parameter_equality(bset);
7228 if (!bset)
7229 set = NULL;
7230 else if (i == bset->n_eq)
7231 set = base_compute_divs(bset);
7232 else
7233 set = parameter_compute_divs(bset);
7234 set = set_parameter_preimage(set, T2);
7235 set = set_append_equalities(set, eq);
7236 return set;
7239 /* Insert the divs from "ls" before those of "bmap".
7241 * The number of columns is not changed, which means that the last
7242 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7243 * The caller is responsible for removing the same number of dimensions
7244 * from the space of "bmap".
7246 static __isl_give isl_basic_map *insert_divs_from_local_space(
7247 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7249 int i;
7250 int n_div;
7251 int old_n_div;
7253 n_div = isl_local_space_dim(ls, isl_dim_div);
7254 if (n_div == 0)
7255 return bmap;
7257 old_n_div = bmap->n_div;
7258 bmap = insert_div_rows(bmap, n_div);
7259 if (!bmap)
7260 return NULL;
7262 for (i = 0; i < n_div; ++i) {
7263 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7264 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7267 return bmap;
7270 /* Replace the space of "bmap" by the space and divs of "ls".
7272 * If "ls" has any divs, then we simplify the result since we may
7273 * have discovered some additional equalities that could simplify
7274 * the div expressions.
7276 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7277 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7279 int n_div;
7281 bmap = isl_basic_map_cow(bmap);
7282 if (!bmap || !ls)
7283 goto error;
7285 n_div = isl_local_space_dim(ls, isl_dim_div);
7286 bmap = insert_divs_from_local_space(bmap, ls);
7287 if (!bmap)
7288 goto error;
7290 isl_space_free(bmap->dim);
7291 bmap->dim = isl_local_space_get_space(ls);
7292 if (!bmap->dim)
7293 goto error;
7295 isl_local_space_free(ls);
7296 if (n_div > 0)
7297 bmap = isl_basic_map_simplify(bmap);
7298 bmap = isl_basic_map_finalize(bmap);
7299 return bmap;
7300 error:
7301 isl_basic_map_free(bmap);
7302 isl_local_space_free(ls);
7303 return NULL;
7306 /* Replace the space of "map" by the space and divs of "ls".
7308 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7309 __isl_take isl_local_space *ls)
7311 int i;
7313 map = isl_map_cow(map);
7314 if (!map || !ls)
7315 goto error;
7317 for (i = 0; i < map->n; ++i) {
7318 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7319 isl_local_space_copy(ls));
7320 if (!map->p[i])
7321 goto error;
7323 isl_space_free(map->dim);
7324 map->dim = isl_local_space_get_space(ls);
7325 if (!map->dim)
7326 goto error;
7328 isl_local_space_free(ls);
7329 return map;
7330 error:
7331 isl_local_space_free(ls);
7332 isl_map_free(map);
7333 return NULL;
7336 /* Compute an explicit representation for the existentially
7337 * quantified variables for which do not know any explicit representation yet.
7339 * We first sort the existentially quantified variables so that the
7340 * existentially quantified variables for which we already have an explicit
7341 * representation are placed before those for which we do not.
7342 * The input dimensions, the output dimensions and the existentially
7343 * quantified variables for which we already have an explicit
7344 * representation are then turned into parameters.
7345 * compute_divs returns a map with the same parameters and
7346 * no input or output dimensions and the dimension specification
7347 * is reset to that of the input, including the existentially quantified
7348 * variables for which we already had an explicit representation.
7350 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7352 struct isl_basic_set *bset;
7353 struct isl_set *set;
7354 struct isl_map *map;
7355 isl_space *dim;
7356 isl_local_space *ls;
7357 unsigned nparam;
7358 unsigned n_in;
7359 unsigned n_out;
7360 int n_known;
7361 int i;
7363 bmap = isl_basic_map_sort_divs(bmap);
7364 bmap = isl_basic_map_cow(bmap);
7365 if (!bmap)
7366 return NULL;
7368 n_known = isl_basic_map_first_unknown_div(bmap);
7369 if (n_known < 0)
7370 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7372 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7373 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7374 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7375 dim = isl_space_set_alloc(bmap->ctx,
7376 nparam + n_in + n_out + n_known, 0);
7377 if (!dim)
7378 goto error;
7380 ls = isl_basic_map_get_local_space(bmap);
7381 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7382 n_known, bmap->n_div - n_known);
7383 if (n_known > 0) {
7384 for (i = n_known; i < bmap->n_div; ++i)
7385 swap_div(bmap, i - n_known, i);
7386 bmap->n_div -= n_known;
7387 bmap->extra -= n_known;
7389 bmap = isl_basic_map_reset_space(bmap, dim);
7390 bset = bset_from_bmap(bmap);
7392 set = parameter_compute_divs(bset);
7393 map = set_to_map(set);
7394 map = replace_space_by_local_space(map, ls);
7396 return map;
7397 error:
7398 isl_basic_map_free(bmap);
7399 return NULL;
7402 /* Remove the explicit representation of local variable "div",
7403 * if there is any.
7405 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7406 __isl_take isl_basic_map *bmap, int div)
7408 isl_bool unknown;
7410 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7411 if (unknown < 0)
7412 return isl_basic_map_free(bmap);
7413 if (unknown)
7414 return bmap;
7416 bmap = isl_basic_map_cow(bmap);
7417 if (!bmap)
7418 return NULL;
7419 isl_int_set_si(bmap->div[div][0], 0);
7420 return bmap;
7423 /* Is local variable "div" of "bmap" marked as not having an explicit
7424 * representation?
7425 * Note that even if "div" is not marked in this way and therefore
7426 * has an explicit representation, this representation may still
7427 * depend (indirectly) on other local variables that do not
7428 * have an explicit representation.
7430 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7431 int div)
7433 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7434 return isl_bool_error;
7435 return isl_int_is_zero(bmap->div[div][0]);
7438 /* Return the position of the first local variable that does not
7439 * have an explicit representation.
7440 * Return the total number of local variables if they all have
7441 * an explicit representation.
7442 * Return -1 on error.
7444 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7446 int i;
7448 if (!bmap)
7449 return -1;
7451 for (i = 0; i < bmap->n_div; ++i) {
7452 if (!isl_basic_map_div_is_known(bmap, i))
7453 return i;
7455 return bmap->n_div;
7458 /* Return the position of the first local variable that does not
7459 * have an explicit representation.
7460 * Return the total number of local variables if they all have
7461 * an explicit representation.
7462 * Return -1 on error.
7464 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7466 return isl_basic_map_first_unknown_div(bset);
7469 /* Does "bmap" have an explicit representation for all local variables?
7471 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7473 int first, n;
7475 n = isl_basic_map_dim(bmap, isl_dim_div);
7476 first = isl_basic_map_first_unknown_div(bmap);
7477 if (first < 0)
7478 return isl_bool_error;
7479 return first == n;
7482 /* Do all basic maps in "map" have an explicit representation
7483 * for all local variables?
7485 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7487 int i;
7489 if (!map)
7490 return isl_bool_error;
7492 for (i = 0; i < map->n; ++i) {
7493 int known = isl_basic_map_divs_known(map->p[i]);
7494 if (known <= 0)
7495 return known;
7498 return isl_bool_true;
7501 /* If bmap contains any unknown divs, then compute explicit
7502 * expressions for them. However, this computation may be
7503 * quite expensive, so first try to remove divs that aren't
7504 * strictly needed.
7506 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7508 int known;
7509 struct isl_map *map;
7511 known = isl_basic_map_divs_known(bmap);
7512 if (known < 0)
7513 goto error;
7514 if (known)
7515 return isl_map_from_basic_map(bmap);
7517 bmap = isl_basic_map_drop_redundant_divs(bmap);
7519 known = isl_basic_map_divs_known(bmap);
7520 if (known < 0)
7521 goto error;
7522 if (known)
7523 return isl_map_from_basic_map(bmap);
7525 map = compute_divs(bmap);
7526 return map;
7527 error:
7528 isl_basic_map_free(bmap);
7529 return NULL;
7532 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7534 int i;
7535 int known;
7536 struct isl_map *res;
7538 if (!map)
7539 return NULL;
7540 if (map->n == 0)
7541 return map;
7543 known = isl_map_divs_known(map);
7544 if (known < 0) {
7545 isl_map_free(map);
7546 return NULL;
7548 if (known)
7549 return map;
7551 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7552 for (i = 1 ; i < map->n; ++i) {
7553 struct isl_map *r2;
7554 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7555 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7556 res = isl_map_union_disjoint(res, r2);
7557 else
7558 res = isl_map_union(res, r2);
7560 isl_map_free(map);
7562 return res;
7565 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7567 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7570 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7572 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7575 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7577 int i;
7578 struct isl_set *set;
7580 if (!map)
7581 goto error;
7583 map = isl_map_cow(map);
7584 if (!map)
7585 return NULL;
7587 set = set_from_map(map);
7588 set->dim = isl_space_domain(set->dim);
7589 if (!set->dim)
7590 goto error;
7591 for (i = 0; i < map->n; ++i) {
7592 set->p[i] = isl_basic_map_domain(map->p[i]);
7593 if (!set->p[i])
7594 goto error;
7596 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7597 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7598 return set;
7599 error:
7600 isl_map_free(map);
7601 return NULL;
7604 /* Return the union of "map1" and "map2", where we assume for now that
7605 * "map1" and "map2" are disjoint. Note that the basic maps inside
7606 * "map1" or "map2" may not be disjoint from each other.
7607 * Also note that this function is also called from isl_map_union,
7608 * which takes care of handling the situation where "map1" and "map2"
7609 * may not be disjoint.
7611 * If one of the inputs is empty, we can simply return the other input.
7612 * Similarly, if one of the inputs is universal, then it is equal to the union.
7614 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7615 __isl_take isl_map *map2)
7617 int i;
7618 unsigned flags = 0;
7619 struct isl_map *map = NULL;
7620 int is_universe;
7622 if (!map1 || !map2)
7623 goto error;
7625 if (!isl_space_is_equal(map1->dim, map2->dim))
7626 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7627 "spaces don't match", goto error);
7629 if (map1->n == 0) {
7630 isl_map_free(map1);
7631 return map2;
7633 if (map2->n == 0) {
7634 isl_map_free(map2);
7635 return map1;
7638 is_universe = isl_map_plain_is_universe(map1);
7639 if (is_universe < 0)
7640 goto error;
7641 if (is_universe) {
7642 isl_map_free(map2);
7643 return map1;
7646 is_universe = isl_map_plain_is_universe(map2);
7647 if (is_universe < 0)
7648 goto error;
7649 if (is_universe) {
7650 isl_map_free(map1);
7651 return map2;
7654 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7655 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7656 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7658 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7659 map1->n + map2->n, flags);
7660 if (!map)
7661 goto error;
7662 for (i = 0; i < map1->n; ++i) {
7663 map = isl_map_add_basic_map(map,
7664 isl_basic_map_copy(map1->p[i]));
7665 if (!map)
7666 goto error;
7668 for (i = 0; i < map2->n; ++i) {
7669 map = isl_map_add_basic_map(map,
7670 isl_basic_map_copy(map2->p[i]));
7671 if (!map)
7672 goto error;
7674 isl_map_free(map1);
7675 isl_map_free(map2);
7676 return map;
7677 error:
7678 isl_map_free(map);
7679 isl_map_free(map1);
7680 isl_map_free(map2);
7681 return NULL;
7684 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7685 * guaranteed to be disjoint by the caller.
7687 * Note that this functions is called from within isl_map_make_disjoint,
7688 * so we have to be careful not to touch the constraints of the inputs
7689 * in any way.
7691 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7692 __isl_take isl_map *map2)
7694 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7697 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7698 * not be disjoint. The parameters are assumed to have been aligned.
7700 * We currently simply call map_union_disjoint, the internal operation
7701 * of which does not really depend on the inputs being disjoint.
7702 * If the result contains more than one basic map, then we clear
7703 * the disjoint flag since the result may contain basic maps from
7704 * both inputs and these are not guaranteed to be disjoint.
7706 * As a special case, if "map1" and "map2" are obviously equal,
7707 * then we simply return "map1".
7709 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7710 __isl_take isl_map *map2)
7712 int equal;
7714 if (!map1 || !map2)
7715 goto error;
7717 equal = isl_map_plain_is_equal(map1, map2);
7718 if (equal < 0)
7719 goto error;
7720 if (equal) {
7721 isl_map_free(map2);
7722 return map1;
7725 map1 = map_union_disjoint(map1, map2);
7726 if (!map1)
7727 return NULL;
7728 if (map1->n > 1)
7729 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7730 return map1;
7731 error:
7732 isl_map_free(map1);
7733 isl_map_free(map2);
7734 return NULL;
7737 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7738 * not be disjoint.
7740 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7741 __isl_take isl_map *map2)
7743 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7746 __isl_give isl_set *isl_set_union_disjoint(
7747 __isl_take isl_set *set1, __isl_take isl_set *set2)
7749 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7750 set_to_map(set2)));
7753 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7755 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7758 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7759 * the results.
7761 * "map" and "set" are assumed to be compatible and non-NULL.
7763 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7764 __isl_take isl_set *set,
7765 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7766 __isl_take isl_basic_set *bset))
7768 unsigned flags = 0;
7769 struct isl_map *result;
7770 int i, j;
7772 if (isl_set_plain_is_universe(set)) {
7773 isl_set_free(set);
7774 return map;
7777 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7778 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7779 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7781 result = isl_map_alloc_space(isl_space_copy(map->dim),
7782 map->n * set->n, flags);
7783 for (i = 0; result && i < map->n; ++i)
7784 for (j = 0; j < set->n; ++j) {
7785 result = isl_map_add_basic_map(result,
7786 fn(isl_basic_map_copy(map->p[i]),
7787 isl_basic_set_copy(set->p[j])));
7788 if (!result)
7789 break;
7792 isl_map_free(map);
7793 isl_set_free(set);
7794 return result;
7797 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7798 __isl_take isl_set *set)
7800 isl_bool ok;
7802 ok = isl_map_compatible_range(map, set);
7803 if (ok < 0)
7804 goto error;
7805 if (!ok)
7806 isl_die(set->ctx, isl_error_invalid,
7807 "incompatible spaces", goto error);
7809 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7810 error:
7811 isl_map_free(map);
7812 isl_set_free(set);
7813 return NULL;
7816 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7817 __isl_take isl_set *set)
7819 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7822 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7823 __isl_take isl_set *set)
7825 isl_bool ok;
7827 ok = isl_map_compatible_domain(map, set);
7828 if (ok < 0)
7829 goto error;
7830 if (!ok)
7831 isl_die(set->ctx, isl_error_invalid,
7832 "incompatible spaces", goto error);
7834 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7835 error:
7836 isl_map_free(map);
7837 isl_set_free(set);
7838 return NULL;
7841 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7842 __isl_take isl_set *set)
7844 return isl_map_align_params_map_map_and(map, set,
7845 &map_intersect_domain);
7848 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7849 * in the space B -> C, return the intersection.
7850 * The parameters are assumed to have been aligned.
7852 * The map "factor" is first extended to a map living in the space
7853 * [A -> B] -> C and then a regular intersection is computed.
7855 static __isl_give isl_map *map_intersect_domain_factor_range(
7856 __isl_take isl_map *map, __isl_take isl_map *factor)
7858 isl_space *space;
7859 isl_map *ext_factor;
7861 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7862 ext_factor = isl_map_universe(space);
7863 ext_factor = isl_map_domain_product(ext_factor, factor);
7864 return map_intersect(map, ext_factor);
7867 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7868 * in the space B -> C, return the intersection.
7870 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7871 __isl_take isl_map *map, __isl_take isl_map *factor)
7873 return isl_map_align_params_map_map_and(map, factor,
7874 &map_intersect_domain_factor_range);
7877 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7878 * in the space A -> C, return the intersection.
7880 * The map "factor" is first extended to a map living in the space
7881 * A -> [B -> C] and then a regular intersection is computed.
7883 static __isl_give isl_map *map_intersect_range_factor_range(
7884 __isl_take isl_map *map, __isl_take isl_map *factor)
7886 isl_space *space;
7887 isl_map *ext_factor;
7889 space = isl_space_range_factor_domain(isl_map_get_space(map));
7890 ext_factor = isl_map_universe(space);
7891 ext_factor = isl_map_range_product(ext_factor, factor);
7892 return isl_map_intersect(map, ext_factor);
7895 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7896 * in the space A -> C, return the intersection.
7898 __isl_give isl_map *isl_map_intersect_range_factor_range(
7899 __isl_take isl_map *map, __isl_take isl_map *factor)
7901 return isl_map_align_params_map_map_and(map, factor,
7902 &map_intersect_range_factor_range);
7905 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7906 __isl_take isl_map *map2)
7908 if (!map1 || !map2)
7909 goto error;
7910 map1 = isl_map_reverse(map1);
7911 map1 = isl_map_apply_range(map1, map2);
7912 return isl_map_reverse(map1);
7913 error:
7914 isl_map_free(map1);
7915 isl_map_free(map2);
7916 return NULL;
7919 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7920 __isl_take isl_map *map2)
7922 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7925 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7926 __isl_take isl_map *map2)
7928 isl_space *dim_result;
7929 struct isl_map *result;
7930 int i, j;
7932 if (!map1 || !map2)
7933 goto error;
7935 dim_result = isl_space_join(isl_space_copy(map1->dim),
7936 isl_space_copy(map2->dim));
7938 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7939 if (!result)
7940 goto error;
7941 for (i = 0; i < map1->n; ++i)
7942 for (j = 0; j < map2->n; ++j) {
7943 result = isl_map_add_basic_map(result,
7944 isl_basic_map_apply_range(
7945 isl_basic_map_copy(map1->p[i]),
7946 isl_basic_map_copy(map2->p[j])));
7947 if (!result)
7948 goto error;
7950 isl_map_free(map1);
7951 isl_map_free(map2);
7952 if (result && result->n <= 1)
7953 ISL_F_SET(result, ISL_MAP_DISJOINT);
7954 return result;
7955 error:
7956 isl_map_free(map1);
7957 isl_map_free(map2);
7958 return NULL;
7961 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7962 __isl_take isl_map *map2)
7964 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7968 * returns range - domain
7970 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
7972 isl_space *target_space;
7973 struct isl_basic_set *bset;
7974 unsigned dim;
7975 unsigned nparam;
7976 int i;
7978 if (!bmap)
7979 goto error;
7980 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7981 bmap->dim, isl_dim_out),
7982 goto error);
7983 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7984 dim = isl_basic_map_dim(bmap, isl_dim_in);
7985 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7986 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7987 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7988 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7989 for (i = 0; i < dim; ++i) {
7990 int j = isl_basic_map_alloc_equality(bmap);
7991 if (j < 0) {
7992 bmap = isl_basic_map_free(bmap);
7993 break;
7995 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7996 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7997 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7998 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8000 bset = isl_basic_map_domain(bmap);
8001 bset = isl_basic_set_reset_space(bset, target_space);
8002 return bset;
8003 error:
8004 isl_basic_map_free(bmap);
8005 return NULL;
8009 * returns range - domain
8011 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8013 int i;
8014 isl_space *dim;
8015 struct isl_set *result;
8017 if (!map)
8018 return NULL;
8020 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8021 map->dim, isl_dim_out),
8022 goto error);
8023 dim = isl_map_get_space(map);
8024 dim = isl_space_domain(dim);
8025 result = isl_set_alloc_space(dim, map->n, 0);
8026 if (!result)
8027 goto error;
8028 for (i = 0; i < map->n; ++i)
8029 result = isl_set_add_basic_set(result,
8030 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8031 isl_map_free(map);
8032 return result;
8033 error:
8034 isl_map_free(map);
8035 return NULL;
8039 * returns [domain -> range] -> range - domain
8041 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8042 __isl_take isl_basic_map *bmap)
8044 int i, k;
8045 isl_space *dim;
8046 isl_basic_map *domain;
8047 int nparam, n;
8048 unsigned total;
8050 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8051 bmap->dim, isl_dim_out))
8052 isl_die(bmap->ctx, isl_error_invalid,
8053 "domain and range don't match", goto error);
8055 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8056 n = isl_basic_map_dim(bmap, isl_dim_in);
8058 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
8059 domain = isl_basic_map_universe(dim);
8061 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8062 bmap = isl_basic_map_apply_range(bmap, domain);
8063 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8065 total = isl_basic_map_total_dim(bmap);
8067 for (i = 0; i < n; ++i) {
8068 k = isl_basic_map_alloc_equality(bmap);
8069 if (k < 0)
8070 goto error;
8071 isl_seq_clr(bmap->eq[k], 1 + total);
8072 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8073 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8074 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8077 bmap = isl_basic_map_gauss(bmap, NULL);
8078 return isl_basic_map_finalize(bmap);
8079 error:
8080 isl_basic_map_free(bmap);
8081 return NULL;
8085 * returns [domain -> range] -> range - domain
8087 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8089 int i;
8090 isl_space *domain_dim;
8092 if (!map)
8093 return NULL;
8095 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8096 map->dim, isl_dim_out))
8097 isl_die(map->ctx, isl_error_invalid,
8098 "domain and range don't match", goto error);
8100 map = isl_map_cow(map);
8101 if (!map)
8102 return NULL;
8104 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
8105 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8106 map->dim = isl_space_join(map->dim, domain_dim);
8107 if (!map->dim)
8108 goto error;
8109 for (i = 0; i < map->n; ++i) {
8110 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8111 if (!map->p[i])
8112 goto error;
8114 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8115 return map;
8116 error:
8117 isl_map_free(map);
8118 return NULL;
8121 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
8123 struct isl_basic_map *bmap;
8124 unsigned nparam;
8125 unsigned dim;
8126 int i;
8128 if (!dims)
8129 return NULL;
8131 nparam = dims->nparam;
8132 dim = dims->n_out;
8133 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
8134 if (!bmap)
8135 goto error;
8137 for (i = 0; i < dim; ++i) {
8138 int j = isl_basic_map_alloc_equality(bmap);
8139 if (j < 0)
8140 goto error;
8141 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8142 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8143 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
8145 return isl_basic_map_finalize(bmap);
8146 error:
8147 isl_basic_map_free(bmap);
8148 return NULL;
8151 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
8153 if (!dim)
8154 return NULL;
8155 if (dim->n_in != dim->n_out)
8156 isl_die(dim->ctx, isl_error_invalid,
8157 "number of input and output dimensions needs to be "
8158 "the same", goto error);
8159 return basic_map_identity(dim);
8160 error:
8161 isl_space_free(dim);
8162 return NULL;
8165 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8167 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8170 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8172 isl_space *dim = isl_set_get_space(set);
8173 isl_map *id;
8174 id = isl_map_identity(isl_space_map_from_set(dim));
8175 return isl_map_intersect_range(id, set);
8178 /* Construct a basic set with all set dimensions having only non-negative
8179 * values.
8181 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8182 __isl_take isl_space *space)
8184 int i;
8185 unsigned nparam;
8186 unsigned dim;
8187 struct isl_basic_set *bset;
8189 if (!space)
8190 return NULL;
8191 nparam = space->nparam;
8192 dim = space->n_out;
8193 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8194 if (!bset)
8195 return NULL;
8196 for (i = 0; i < dim; ++i) {
8197 int k = isl_basic_set_alloc_inequality(bset);
8198 if (k < 0)
8199 goto error;
8200 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8201 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8203 return bset;
8204 error:
8205 isl_basic_set_free(bset);
8206 return NULL;
8209 /* Construct the half-space x_pos >= 0.
8211 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
8212 int pos)
8214 int k;
8215 isl_basic_set *nonneg;
8217 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8218 k = isl_basic_set_alloc_inequality(nonneg);
8219 if (k < 0)
8220 goto error;
8221 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8222 isl_int_set_si(nonneg->ineq[k][pos], 1);
8224 return isl_basic_set_finalize(nonneg);
8225 error:
8226 isl_basic_set_free(nonneg);
8227 return NULL;
8230 /* Construct the half-space x_pos <= -1.
8232 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
8234 int k;
8235 isl_basic_set *neg;
8237 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
8238 k = isl_basic_set_alloc_inequality(neg);
8239 if (k < 0)
8240 goto error;
8241 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8242 isl_int_set_si(neg->ineq[k][0], -1);
8243 isl_int_set_si(neg->ineq[k][pos], -1);
8245 return isl_basic_set_finalize(neg);
8246 error:
8247 isl_basic_set_free(neg);
8248 return NULL;
8251 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8252 enum isl_dim_type type, unsigned first, unsigned n)
8254 int i;
8255 unsigned offset;
8256 isl_basic_set *nonneg;
8257 isl_basic_set *neg;
8259 if (!set)
8260 return NULL;
8261 if (n == 0)
8262 return set;
8264 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8266 offset = pos(set->dim, type);
8267 for (i = 0; i < n; ++i) {
8268 nonneg = nonneg_halfspace(isl_set_get_space(set),
8269 offset + first + i);
8270 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8272 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8275 return set;
8276 error:
8277 isl_set_free(set);
8278 return NULL;
8281 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8282 int len,
8283 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8284 void *user)
8286 isl_set *half;
8288 if (!set)
8289 return isl_stat_error;
8290 if (isl_set_plain_is_empty(set)) {
8291 isl_set_free(set);
8292 return isl_stat_ok;
8294 if (first == len)
8295 return fn(set, signs, user);
8297 signs[first] = 1;
8298 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8299 1 + first));
8300 half = isl_set_intersect(half, isl_set_copy(set));
8301 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8302 goto error;
8304 signs[first] = -1;
8305 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8306 1 + first));
8307 half = isl_set_intersect(half, set);
8308 return foreach_orthant(half, signs, first + 1, len, fn, user);
8309 error:
8310 isl_set_free(set);
8311 return isl_stat_error;
8314 /* Call "fn" on the intersections of "set" with each of the orthants
8315 * (except for obviously empty intersections). The orthant is identified
8316 * by the signs array, with each entry having value 1 or -1 according
8317 * to the sign of the corresponding variable.
8319 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8320 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8321 void *user)
8323 unsigned nparam;
8324 unsigned nvar;
8325 int *signs;
8326 isl_stat r;
8328 if (!set)
8329 return isl_stat_error;
8330 if (isl_set_plain_is_empty(set))
8331 return isl_stat_ok;
8333 nparam = isl_set_dim(set, isl_dim_param);
8334 nvar = isl_set_dim(set, isl_dim_set);
8336 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8338 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8339 fn, user);
8341 free(signs);
8343 return r;
8346 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8348 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8351 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8352 __isl_keep isl_basic_map *bmap2)
8354 int is_subset;
8355 struct isl_map *map1;
8356 struct isl_map *map2;
8358 if (!bmap1 || !bmap2)
8359 return isl_bool_error;
8361 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8362 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8364 is_subset = isl_map_is_subset(map1, map2);
8366 isl_map_free(map1);
8367 isl_map_free(map2);
8369 return is_subset;
8372 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8373 __isl_keep isl_basic_set *bset2)
8375 return isl_basic_map_is_subset(bset1, bset2);
8378 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8379 __isl_keep isl_basic_map *bmap2)
8381 isl_bool is_subset;
8383 if (!bmap1 || !bmap2)
8384 return isl_bool_error;
8385 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8386 if (is_subset != isl_bool_true)
8387 return is_subset;
8388 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8389 return is_subset;
8392 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8393 __isl_keep isl_basic_set *bset2)
8395 return isl_basic_map_is_equal(
8396 bset_to_bmap(bset1), bset_to_bmap(bset2));
8399 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8401 int i;
8402 int is_empty;
8404 if (!map)
8405 return isl_bool_error;
8406 for (i = 0; i < map->n; ++i) {
8407 is_empty = isl_basic_map_is_empty(map->p[i]);
8408 if (is_empty < 0)
8409 return isl_bool_error;
8410 if (!is_empty)
8411 return isl_bool_false;
8413 return isl_bool_true;
8416 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8418 return map ? map->n == 0 : isl_bool_error;
8421 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8423 return set ? set->n == 0 : isl_bool_error;
8426 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8428 return isl_map_is_empty(set_to_map(set));
8431 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8432 __isl_keep isl_map *map2)
8434 if (!map1 || !map2)
8435 return isl_bool_error;
8437 return isl_space_is_equal(map1->dim, map2->dim);
8440 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8441 __isl_keep isl_set *set2)
8443 if (!set1 || !set2)
8444 return isl_bool_error;
8446 return isl_space_is_equal(set1->dim, set2->dim);
8449 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8451 isl_bool is_subset;
8453 if (!map1 || !map2)
8454 return isl_bool_error;
8455 is_subset = isl_map_is_subset(map1, map2);
8456 if (is_subset != isl_bool_true)
8457 return is_subset;
8458 is_subset = isl_map_is_subset(map2, map1);
8459 return is_subset;
8462 /* Is "map1" equal to "map2"?
8464 * First check if they are obviously equal.
8465 * If not, then perform a more detailed analysis.
8467 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8469 isl_bool equal;
8471 equal = isl_map_plain_is_equal(map1, map2);
8472 if (equal < 0 || equal)
8473 return equal;
8474 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8477 isl_bool isl_basic_map_is_strict_subset(
8478 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8480 isl_bool is_subset;
8482 if (!bmap1 || !bmap2)
8483 return isl_bool_error;
8484 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8485 if (is_subset != isl_bool_true)
8486 return is_subset;
8487 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8488 if (is_subset == isl_bool_error)
8489 return is_subset;
8490 return !is_subset;
8493 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8494 __isl_keep isl_map *map2)
8496 isl_bool is_subset;
8498 if (!map1 || !map2)
8499 return isl_bool_error;
8500 is_subset = isl_map_is_subset(map1, map2);
8501 if (is_subset != isl_bool_true)
8502 return is_subset;
8503 is_subset = isl_map_is_subset(map2, map1);
8504 if (is_subset == isl_bool_error)
8505 return is_subset;
8506 return !is_subset;
8509 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8510 __isl_keep isl_set *set2)
8512 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8515 /* Is "bmap" obviously equal to the universe with the same space?
8517 * That is, does it not have any constraints?
8519 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8521 if (!bmap)
8522 return isl_bool_error;
8523 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8526 /* Is "bset" obviously equal to the universe with the same space?
8528 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8530 return isl_basic_map_plain_is_universe(bset);
8533 /* If "c" does not involve any existentially quantified variables,
8534 * then set *univ to false and abort
8536 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8538 isl_bool *univ = user;
8539 unsigned n;
8541 n = isl_constraint_dim(c, isl_dim_div);
8542 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8543 isl_constraint_free(c);
8544 if (*univ < 0 || !*univ)
8545 return isl_stat_error;
8546 return isl_stat_ok;
8549 /* Is "bmap" equal to the universe with the same space?
8551 * First check if it is obviously equal to the universe.
8552 * If not and if there are any constraints not involving
8553 * existentially quantified variables, then it is certainly
8554 * not equal to the universe.
8555 * Otherwise, check if the universe is a subset of "bmap".
8557 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8559 isl_bool univ;
8560 isl_basic_map *test;
8562 univ = isl_basic_map_plain_is_universe(bmap);
8563 if (univ < 0 || univ)
8564 return univ;
8565 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8566 return isl_bool_false;
8567 univ = isl_bool_true;
8568 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8569 univ)
8570 return isl_bool_error;
8571 if (univ < 0 || !univ)
8572 return univ;
8573 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8574 univ = isl_basic_map_is_subset(test, bmap);
8575 isl_basic_map_free(test);
8576 return univ;
8579 /* Is "bset" equal to the universe with the same space?
8581 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8583 return isl_basic_map_is_universe(bset);
8586 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8588 int i;
8590 if (!map)
8591 return isl_bool_error;
8593 for (i = 0; i < map->n; ++i) {
8594 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8595 if (r < 0 || r)
8596 return r;
8599 return isl_bool_false;
8602 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8604 return isl_map_plain_is_universe(set_to_map(set));
8607 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8609 struct isl_basic_set *bset = NULL;
8610 struct isl_vec *sample = NULL;
8611 isl_bool empty, non_empty;
8613 if (!bmap)
8614 return isl_bool_error;
8616 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8617 return isl_bool_true;
8619 if (isl_basic_map_plain_is_universe(bmap))
8620 return isl_bool_false;
8622 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8623 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8624 copy = isl_basic_map_remove_redundancies(copy);
8625 empty = isl_basic_map_plain_is_empty(copy);
8626 isl_basic_map_free(copy);
8627 return empty;
8630 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8631 if (non_empty < 0)
8632 return isl_bool_error;
8633 if (non_empty)
8634 return isl_bool_false;
8635 isl_vec_free(bmap->sample);
8636 bmap->sample = NULL;
8637 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8638 if (!bset)
8639 return isl_bool_error;
8640 sample = isl_basic_set_sample_vec(bset);
8641 if (!sample)
8642 return isl_bool_error;
8643 empty = sample->size == 0;
8644 isl_vec_free(bmap->sample);
8645 bmap->sample = sample;
8646 if (empty)
8647 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8649 return empty;
8652 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8654 if (!bmap)
8655 return isl_bool_error;
8656 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8659 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8661 if (!bset)
8662 return isl_bool_error;
8663 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8666 /* Is "bmap" known to be non-empty?
8668 * That is, is the cached sample still valid?
8670 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8672 unsigned total;
8674 if (!bmap)
8675 return isl_bool_error;
8676 if (!bmap->sample)
8677 return isl_bool_false;
8678 total = 1 + isl_basic_map_total_dim(bmap);
8679 if (bmap->sample->size != total)
8680 return isl_bool_false;
8681 return isl_basic_map_contains(bmap, bmap->sample);
8684 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8686 return isl_basic_map_is_empty(bset_to_bmap(bset));
8689 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8690 __isl_take isl_basic_map *bmap2)
8692 struct isl_map *map;
8693 if (!bmap1 || !bmap2)
8694 goto error;
8696 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8698 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8699 if (!map)
8700 goto error;
8701 map = isl_map_add_basic_map(map, bmap1);
8702 map = isl_map_add_basic_map(map, bmap2);
8703 return map;
8704 error:
8705 isl_basic_map_free(bmap1);
8706 isl_basic_map_free(bmap2);
8707 return NULL;
8710 struct isl_set *isl_basic_set_union(
8711 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8713 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8714 bset_to_bmap(bset2)));
8717 /* Order divs such that any div only depends on previous divs */
8718 __isl_give isl_basic_map *isl_basic_map_order_divs(
8719 __isl_take isl_basic_map *bmap)
8721 int i;
8722 unsigned off;
8724 if (!bmap)
8725 return NULL;
8727 off = isl_space_dim(bmap->dim, isl_dim_all);
8729 for (i = 0; i < bmap->n_div; ++i) {
8730 int pos;
8731 if (isl_int_is_zero(bmap->div[i][0]))
8732 continue;
8733 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8734 bmap->n_div-i);
8735 if (pos == -1)
8736 continue;
8737 if (pos == 0)
8738 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8739 "integer division depends on itself",
8740 return isl_basic_map_free(bmap));
8741 isl_basic_map_swap_div(bmap, i, i + pos);
8742 --i;
8744 return bmap;
8747 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8749 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8752 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8754 int i;
8756 if (!map)
8757 return 0;
8759 for (i = 0; i < map->n; ++i) {
8760 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8761 if (!map->p[i])
8762 goto error;
8765 return map;
8766 error:
8767 isl_map_free(map);
8768 return NULL;
8771 /* Sort the local variables of "bset".
8773 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8774 __isl_take isl_basic_set *bset)
8776 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8779 /* Apply the expansion computed by isl_merge_divs.
8780 * The expansion itself is given by "exp" while the resulting
8781 * list of divs is given by "div".
8783 * Move the integer divisions of "bmap" into the right position
8784 * according to "exp" and then introduce the additional integer
8785 * divisions, adding div constraints.
8786 * The moving should be done first to avoid moving coefficients
8787 * in the definitions of the extra integer divisions.
8789 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8790 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8792 int i, j;
8793 int n_div;
8795 bmap = isl_basic_map_cow(bmap);
8796 if (!bmap || !div)
8797 goto error;
8799 if (div->n_row < bmap->n_div)
8800 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8801 "not an expansion", goto error);
8803 n_div = bmap->n_div;
8804 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8805 div->n_row - n_div, 0,
8806 2 * (div->n_row - n_div));
8808 for (i = n_div; i < div->n_row; ++i)
8809 if (isl_basic_map_alloc_div(bmap) < 0)
8810 goto error;
8812 for (j = n_div - 1; j >= 0; --j) {
8813 if (exp[j] == j)
8814 break;
8815 isl_basic_map_swap_div(bmap, j, exp[j]);
8817 j = 0;
8818 for (i = 0; i < div->n_row; ++i) {
8819 if (j < n_div && exp[j] == i) {
8820 j++;
8821 } else {
8822 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8823 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8824 continue;
8825 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8826 goto error;
8830 isl_mat_free(div);
8831 return bmap;
8832 error:
8833 isl_basic_map_free(bmap);
8834 isl_mat_free(div);
8835 return NULL;
8838 /* Apply the expansion computed by isl_merge_divs.
8839 * The expansion itself is given by "exp" while the resulting
8840 * list of divs is given by "div".
8842 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8843 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8845 return isl_basic_map_expand_divs(bset, div, exp);
8848 /* Look for a div in dst that corresponds to the div "div" in src.
8849 * The divs before "div" in src and dst are assumed to be the same.
8851 * Returns -1 if no corresponding div was found and the position
8852 * of the corresponding div in dst otherwise.
8854 static int find_div(__isl_keep isl_basic_map *dst,
8855 __isl_keep isl_basic_map *src, unsigned div)
8857 int i;
8859 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8861 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8862 for (i = div; i < dst->n_div; ++i)
8863 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8864 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8865 dst->n_div - div) == -1)
8866 return i;
8867 return -1;
8870 /* Align the divs of "dst" to those of "src", adding divs from "src"
8871 * if needed. That is, make sure that the first src->n_div divs
8872 * of the result are equal to those of src.
8874 * The result is not finalized as by design it will have redundant
8875 * divs if any divs from "src" were copied.
8877 __isl_give isl_basic_map *isl_basic_map_align_divs(
8878 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8880 int i;
8881 int known, extended;
8882 unsigned total;
8884 if (!dst || !src)
8885 return isl_basic_map_free(dst);
8887 if (src->n_div == 0)
8888 return dst;
8890 known = isl_basic_map_divs_known(src);
8891 if (known < 0)
8892 return isl_basic_map_free(dst);
8893 if (!known)
8894 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8895 "some src divs are unknown",
8896 return isl_basic_map_free(dst));
8898 src = isl_basic_map_order_divs(src);
8900 extended = 0;
8901 total = isl_space_dim(src->dim, isl_dim_all);
8902 for (i = 0; i < src->n_div; ++i) {
8903 int j = find_div(dst, src, i);
8904 if (j < 0) {
8905 if (!extended) {
8906 int extra = src->n_div - i;
8907 dst = isl_basic_map_cow(dst);
8908 if (!dst)
8909 return NULL;
8910 dst = isl_basic_map_extend_space(dst,
8911 isl_space_copy(dst->dim),
8912 extra, 0, 2 * extra);
8913 extended = 1;
8915 j = isl_basic_map_alloc_div(dst);
8916 if (j < 0)
8917 return isl_basic_map_free(dst);
8918 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8919 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8920 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8921 return isl_basic_map_free(dst);
8923 if (j != i)
8924 isl_basic_map_swap_div(dst, i, j);
8926 return dst;
8929 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
8931 int i;
8933 if (!map)
8934 return NULL;
8935 if (map->n == 0)
8936 return map;
8937 map = isl_map_compute_divs(map);
8938 map = isl_map_cow(map);
8939 if (!map)
8940 return NULL;
8942 for (i = 1; i < map->n; ++i)
8943 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8944 for (i = 1; i < map->n; ++i) {
8945 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8946 if (!map->p[i])
8947 return isl_map_free(map);
8950 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8951 return map;
8954 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
8956 return isl_map_align_divs_internal(map);
8959 struct isl_set *isl_set_align_divs(struct isl_set *set)
8961 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
8964 /* Align the divs of the basic maps in "map" to those
8965 * of the basic maps in "list", as well as to the other basic maps in "map".
8966 * The elements in "list" are assumed to have known divs.
8968 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8969 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8971 int i, n;
8973 map = isl_map_compute_divs(map);
8974 map = isl_map_cow(map);
8975 if (!map || !list)
8976 return isl_map_free(map);
8977 if (map->n == 0)
8978 return map;
8980 n = isl_basic_map_list_n_basic_map(list);
8981 for (i = 0; i < n; ++i) {
8982 isl_basic_map *bmap;
8984 bmap = isl_basic_map_list_get_basic_map(list, i);
8985 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8986 isl_basic_map_free(bmap);
8988 if (!map->p[0])
8989 return isl_map_free(map);
8991 return isl_map_align_divs_internal(map);
8994 /* Align the divs of each element of "list" to those of "bmap".
8995 * Both "bmap" and the elements of "list" are assumed to have known divs.
8997 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8998 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9000 int i, n;
9002 if (!list || !bmap)
9003 return isl_basic_map_list_free(list);
9005 n = isl_basic_map_list_n_basic_map(list);
9006 for (i = 0; i < n; ++i) {
9007 isl_basic_map *bmap_i;
9009 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9010 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9011 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9014 return list;
9017 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9018 __isl_take isl_map *map)
9020 isl_bool ok;
9022 ok = isl_map_compatible_domain(map, set);
9023 if (ok < 0)
9024 goto error;
9025 if (!ok)
9026 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9027 "incompatible spaces", goto error);
9028 map = isl_map_intersect_domain(map, set);
9029 set = isl_map_range(map);
9030 return set;
9031 error:
9032 isl_set_free(set);
9033 isl_map_free(map);
9034 return NULL;
9037 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9038 __isl_take isl_map *map)
9040 return isl_map_align_params_map_map_and(set, map, &set_apply);
9043 /* There is no need to cow as removing empty parts doesn't change
9044 * the meaning of the set.
9046 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9048 int i;
9050 if (!map)
9051 return NULL;
9053 for (i = map->n - 1; i >= 0; --i)
9054 remove_if_empty(map, i);
9056 return map;
9059 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9061 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9064 /* Given two basic sets bset1 and bset2, compute the maximal difference
9065 * between the values of dimension pos in bset1 and those in bset2
9066 * for any common value of the parameters and dimensions preceding pos.
9068 static enum isl_lp_result basic_set_maximal_difference_at(
9069 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9070 int pos, isl_int *opt)
9072 isl_basic_map *bmap1;
9073 isl_basic_map *bmap2;
9074 struct isl_ctx *ctx;
9075 struct isl_vec *obj;
9076 unsigned total;
9077 unsigned nparam;
9078 unsigned dim1;
9079 enum isl_lp_result res;
9081 if (!bset1 || !bset2)
9082 return isl_lp_error;
9084 nparam = isl_basic_set_n_param(bset1);
9085 dim1 = isl_basic_set_n_dim(bset1);
9087 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9088 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9089 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9090 isl_dim_out, 0, pos);
9091 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9092 isl_dim_out, 0, pos);
9093 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
9094 if (!bmap1)
9095 return isl_lp_error;
9097 total = isl_basic_map_total_dim(bmap1);
9098 ctx = bmap1->ctx;
9099 obj = isl_vec_alloc(ctx, 1 + total);
9100 if (!obj)
9101 goto error;
9102 isl_seq_clr(obj->block.data, 1 + total);
9103 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9104 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9105 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9106 opt, NULL, NULL);
9107 isl_basic_map_free(bmap1);
9108 isl_vec_free(obj);
9109 return res;
9110 error:
9111 isl_basic_map_free(bmap1);
9112 return isl_lp_error;
9115 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9116 * for any common value of the parameters and dimensions preceding pos
9117 * in both basic sets, the values of dimension pos in bset1 are
9118 * smaller or larger than those in bset2.
9120 * Returns
9121 * 1 if bset1 follows bset2
9122 * -1 if bset1 precedes bset2
9123 * 0 if bset1 and bset2 are incomparable
9124 * -2 if some error occurred.
9126 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
9127 struct isl_basic_set *bset2, int pos)
9129 isl_int opt;
9130 enum isl_lp_result res;
9131 int cmp;
9133 isl_int_init(opt);
9135 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9137 if (res == isl_lp_empty)
9138 cmp = 0;
9139 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9140 res == isl_lp_unbounded)
9141 cmp = 1;
9142 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9143 cmp = -1;
9144 else
9145 cmp = -2;
9147 isl_int_clear(opt);
9148 return cmp;
9151 /* Given two basic sets bset1 and bset2, check whether
9152 * for any common value of the parameters and dimensions preceding pos
9153 * there is a value of dimension pos in bset1 that is larger
9154 * than a value of the same dimension in bset2.
9156 * Return
9157 * 1 if there exists such a pair
9158 * 0 if there is no such pair, but there is a pair of equal values
9159 * -1 otherwise
9160 * -2 if some error occurred.
9162 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9163 __isl_keep isl_basic_set *bset2, int pos)
9165 isl_int opt;
9166 enum isl_lp_result res;
9167 int cmp;
9169 isl_int_init(opt);
9171 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9173 if (res == isl_lp_empty)
9174 cmp = -1;
9175 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9176 res == isl_lp_unbounded)
9177 cmp = 1;
9178 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9179 cmp = -1;
9180 else if (res == isl_lp_ok)
9181 cmp = 0;
9182 else
9183 cmp = -2;
9185 isl_int_clear(opt);
9186 return cmp;
9189 /* Given two sets set1 and set2, check whether
9190 * for any common value of the parameters and dimensions preceding pos
9191 * there is a value of dimension pos in set1 that is larger
9192 * than a value of the same dimension in set2.
9194 * Return
9195 * 1 if there exists such a pair
9196 * 0 if there is no such pair, but there is a pair of equal values
9197 * -1 otherwise
9198 * -2 if some error occurred.
9200 int isl_set_follows_at(__isl_keep isl_set *set1,
9201 __isl_keep isl_set *set2, int pos)
9203 int i, j;
9204 int follows = -1;
9206 if (!set1 || !set2)
9207 return -2;
9209 for (i = 0; i < set1->n; ++i)
9210 for (j = 0; j < set2->n; ++j) {
9211 int f;
9212 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9213 if (f == 1 || f == -2)
9214 return f;
9215 if (f > follows)
9216 follows = f;
9219 return follows;
9222 static isl_bool isl_basic_map_plain_has_fixed_var(
9223 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9225 int i;
9226 int d;
9227 unsigned total;
9229 if (!bmap)
9230 return isl_bool_error;
9231 total = isl_basic_map_total_dim(bmap);
9232 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9233 for (; d+1 > pos; --d)
9234 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9235 break;
9236 if (d != pos)
9237 continue;
9238 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9239 return isl_bool_false;
9240 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9241 return isl_bool_false;
9242 if (!isl_int_is_one(bmap->eq[i][1+d]))
9243 return isl_bool_false;
9244 if (val)
9245 isl_int_neg(*val, bmap->eq[i][0]);
9246 return isl_bool_true;
9248 return isl_bool_false;
9251 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9252 unsigned pos, isl_int *val)
9254 int i;
9255 isl_int v;
9256 isl_int tmp;
9257 isl_bool fixed;
9259 if (!map)
9260 return isl_bool_error;
9261 if (map->n == 0)
9262 return isl_bool_false;
9263 if (map->n == 1)
9264 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9265 isl_int_init(v);
9266 isl_int_init(tmp);
9267 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9268 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9269 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9270 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9271 fixed = isl_bool_false;
9273 if (val)
9274 isl_int_set(*val, v);
9275 isl_int_clear(tmp);
9276 isl_int_clear(v);
9277 return fixed;
9280 static isl_bool isl_basic_set_plain_has_fixed_var(
9281 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9283 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9284 pos, val);
9287 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9288 enum isl_dim_type type, unsigned pos, isl_int *val)
9290 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9291 return isl_bool_error;
9292 return isl_basic_map_plain_has_fixed_var(bmap,
9293 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9296 /* If "bmap" obviously lies on a hyperplane where the given dimension
9297 * has a fixed value, then return that value.
9298 * Otherwise return NaN.
9300 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9301 __isl_keep isl_basic_map *bmap,
9302 enum isl_dim_type type, unsigned pos)
9304 isl_ctx *ctx;
9305 isl_val *v;
9306 isl_bool fixed;
9308 if (!bmap)
9309 return NULL;
9310 ctx = isl_basic_map_get_ctx(bmap);
9311 v = isl_val_alloc(ctx);
9312 if (!v)
9313 return NULL;
9314 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9315 if (fixed < 0)
9316 return isl_val_free(v);
9317 if (fixed) {
9318 isl_int_set_si(v->d, 1);
9319 return v;
9321 isl_val_free(v);
9322 return isl_val_nan(ctx);
9325 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9326 enum isl_dim_type type, unsigned pos, isl_int *val)
9328 if (pos >= isl_map_dim(map, type))
9329 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9330 "position out of bounds", return isl_bool_error);
9331 return isl_map_plain_has_fixed_var(map,
9332 map_offset(map, type) - 1 + pos, val);
9335 /* If "map" obviously lies on a hyperplane where the given dimension
9336 * has a fixed value, then return that value.
9337 * Otherwise return NaN.
9339 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9340 enum isl_dim_type type, unsigned pos)
9342 isl_ctx *ctx;
9343 isl_val *v;
9344 isl_bool fixed;
9346 if (!map)
9347 return NULL;
9348 ctx = isl_map_get_ctx(map);
9349 v = isl_val_alloc(ctx);
9350 if (!v)
9351 return NULL;
9352 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9353 if (fixed < 0)
9354 return isl_val_free(v);
9355 if (fixed) {
9356 isl_int_set_si(v->d, 1);
9357 return v;
9359 isl_val_free(v);
9360 return isl_val_nan(ctx);
9363 /* If "set" obviously lies on a hyperplane where the given dimension
9364 * has a fixed value, then return that value.
9365 * Otherwise return NaN.
9367 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9368 enum isl_dim_type type, unsigned pos)
9370 return isl_map_plain_get_val_if_fixed(set, type, pos);
9373 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
9374 enum isl_dim_type type, unsigned pos, isl_int *val)
9376 return isl_map_plain_is_fixed(set, type, pos, val);
9379 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9380 * then return this fixed value in *val.
9382 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9383 unsigned dim, isl_int *val)
9385 return isl_basic_set_plain_has_fixed_var(bset,
9386 isl_basic_set_n_param(bset) + dim, val);
9389 /* Return -1 if the constraint "c1" should be sorted before "c2"
9390 * and 1 if it should be sorted after "c2".
9391 * Return 0 if the two constraints are the same (up to the constant term).
9393 * In particular, if a constraint involves later variables than another
9394 * then it is sorted after this other constraint.
9395 * uset_gist depends on constraints without existentially quantified
9396 * variables sorting first.
9398 * For constraints that have the same latest variable, those
9399 * with the same coefficient for this latest variable (first in absolute value
9400 * and then in actual value) are grouped together.
9401 * This is useful for detecting pairs of constraints that can
9402 * be chained in their printed representation.
9404 * Finally, within a group, constraints are sorted according to
9405 * their coefficients (excluding the constant term).
9407 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9409 isl_int **c1 = (isl_int **) p1;
9410 isl_int **c2 = (isl_int **) p2;
9411 int l1, l2;
9412 unsigned size = *(unsigned *) arg;
9413 int cmp;
9415 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9416 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9418 if (l1 != l2)
9419 return l1 - l2;
9421 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9422 if (cmp != 0)
9423 return cmp;
9424 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9425 if (cmp != 0)
9426 return -cmp;
9428 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9431 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9432 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9433 * and 0 if the two constraints are the same (up to the constant term).
9435 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9436 isl_int *c1, isl_int *c2)
9438 unsigned total;
9440 if (!bmap)
9441 return -2;
9442 total = isl_basic_map_total_dim(bmap);
9443 return sort_constraint_cmp(&c1, &c2, &total);
9446 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9447 __isl_take isl_basic_map *bmap)
9449 unsigned total;
9451 if (!bmap)
9452 return NULL;
9453 if (bmap->n_ineq == 0)
9454 return bmap;
9455 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9456 return bmap;
9457 total = isl_basic_map_total_dim(bmap);
9458 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9459 &sort_constraint_cmp, &total) < 0)
9460 return isl_basic_map_free(bmap);
9461 return bmap;
9464 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9465 __isl_take isl_basic_set *bset)
9467 isl_basic_map *bmap = bset_to_bmap(bset);
9468 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9471 __isl_give isl_basic_map *isl_basic_map_normalize(
9472 __isl_take isl_basic_map *bmap)
9474 if (!bmap)
9475 return NULL;
9476 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9477 return bmap;
9478 bmap = isl_basic_map_remove_redundancies(bmap);
9479 bmap = isl_basic_map_sort_constraints(bmap);
9480 if (bmap)
9481 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9482 return bmap;
9484 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9485 __isl_keep isl_basic_map *bmap2)
9487 int i, cmp;
9488 unsigned total;
9489 isl_space *space1, *space2;
9491 if (!bmap1 || !bmap2)
9492 return -1;
9494 if (bmap1 == bmap2)
9495 return 0;
9496 space1 = isl_basic_map_peek_space(bmap1);
9497 space2 = isl_basic_map_peek_space(bmap2);
9498 cmp = isl_space_cmp(space1, space2);
9499 if (cmp)
9500 return cmp;
9501 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9502 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9503 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9504 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9505 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9506 return 0;
9507 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9508 return 1;
9509 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9510 return -1;
9511 if (bmap1->n_eq != bmap2->n_eq)
9512 return bmap1->n_eq - bmap2->n_eq;
9513 if (bmap1->n_ineq != bmap2->n_ineq)
9514 return bmap1->n_ineq - bmap2->n_ineq;
9515 if (bmap1->n_div != bmap2->n_div)
9516 return bmap1->n_div - bmap2->n_div;
9517 total = isl_basic_map_total_dim(bmap1);
9518 for (i = 0; i < bmap1->n_eq; ++i) {
9519 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9520 if (cmp)
9521 return cmp;
9523 for (i = 0; i < bmap1->n_ineq; ++i) {
9524 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9525 if (cmp)
9526 return cmp;
9528 for (i = 0; i < bmap1->n_div; ++i) {
9529 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9530 if (cmp)
9531 return cmp;
9533 return 0;
9536 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9537 __isl_keep isl_basic_set *bset2)
9539 return isl_basic_map_plain_cmp(bset1, bset2);
9542 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9544 int i, cmp;
9546 if (set1 == set2)
9547 return 0;
9548 if (set1->n != set2->n)
9549 return set1->n - set2->n;
9551 for (i = 0; i < set1->n; ++i) {
9552 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9553 if (cmp)
9554 return cmp;
9557 return 0;
9560 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9561 __isl_keep isl_basic_map *bmap2)
9563 if (!bmap1 || !bmap2)
9564 return isl_bool_error;
9565 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9568 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9569 __isl_keep isl_basic_set *bset2)
9571 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9572 bset_to_bmap(bset2));
9575 static int qsort_bmap_cmp(const void *p1, const void *p2)
9577 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9578 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9580 return isl_basic_map_plain_cmp(bmap1, bmap2);
9583 /* Sort the basic maps of "map" and remove duplicate basic maps.
9585 * While removing basic maps, we make sure that the basic maps remain
9586 * sorted because isl_map_normalize expects the basic maps of the result
9587 * to be sorted.
9589 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9591 int i, j;
9593 map = isl_map_remove_empty_parts(map);
9594 if (!map)
9595 return NULL;
9596 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9597 for (i = map->n - 1; i >= 1; --i) {
9598 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9599 continue;
9600 isl_basic_map_free(map->p[i-1]);
9601 for (j = i; j < map->n; ++j)
9602 map->p[j - 1] = map->p[j];
9603 map->n--;
9606 return map;
9609 /* Remove obvious duplicates among the basic maps of "map".
9611 * Unlike isl_map_normalize, this function does not remove redundant
9612 * constraints and only removes duplicates that have exactly the same
9613 * constraints in the input. It does sort the constraints and
9614 * the basic maps to ease the detection of duplicates.
9616 * If "map" has already been normalized or if the basic maps are
9617 * disjoint, then there can be no duplicates.
9619 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9621 int i;
9622 isl_basic_map *bmap;
9624 if (!map)
9625 return NULL;
9626 if (map->n <= 1)
9627 return map;
9628 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9629 return map;
9630 for (i = 0; i < map->n; ++i) {
9631 bmap = isl_basic_map_copy(map->p[i]);
9632 bmap = isl_basic_map_sort_constraints(bmap);
9633 if (!bmap)
9634 return isl_map_free(map);
9635 isl_basic_map_free(map->p[i]);
9636 map->p[i] = bmap;
9639 map = sort_and_remove_duplicates(map);
9640 return map;
9643 /* We normalize in place, but if anything goes wrong we need
9644 * to return NULL, so we need to make sure we don't change the
9645 * meaning of any possible other copies of map.
9647 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9649 int i;
9650 struct isl_basic_map *bmap;
9652 if (!map)
9653 return NULL;
9654 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9655 return map;
9656 for (i = 0; i < map->n; ++i) {
9657 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9658 if (!bmap)
9659 goto error;
9660 isl_basic_map_free(map->p[i]);
9661 map->p[i] = bmap;
9664 map = sort_and_remove_duplicates(map);
9665 if (map)
9666 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9667 return map;
9668 error:
9669 isl_map_free(map);
9670 return NULL;
9673 struct isl_set *isl_set_normalize(struct isl_set *set)
9675 return set_from_map(isl_map_normalize(set_to_map(set)));
9678 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9679 __isl_keep isl_map *map2)
9681 int i;
9682 isl_bool equal;
9684 if (!map1 || !map2)
9685 return isl_bool_error;
9687 if (map1 == map2)
9688 return isl_bool_true;
9689 if (!isl_space_is_equal(map1->dim, map2->dim))
9690 return isl_bool_false;
9692 map1 = isl_map_copy(map1);
9693 map2 = isl_map_copy(map2);
9694 map1 = isl_map_normalize(map1);
9695 map2 = isl_map_normalize(map2);
9696 if (!map1 || !map2)
9697 goto error;
9698 equal = map1->n == map2->n;
9699 for (i = 0; equal && i < map1->n; ++i) {
9700 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9701 if (equal < 0)
9702 goto error;
9704 isl_map_free(map1);
9705 isl_map_free(map2);
9706 return equal;
9707 error:
9708 isl_map_free(map1);
9709 isl_map_free(map2);
9710 return isl_bool_error;
9713 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9714 __isl_keep isl_set *set2)
9716 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9719 /* Return the basic maps in "map" as a list.
9721 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9722 __isl_keep isl_map *map)
9724 int i;
9725 isl_ctx *ctx;
9726 isl_basic_map_list *list;
9728 if (!map)
9729 return NULL;
9730 ctx = isl_map_get_ctx(map);
9731 list = isl_basic_map_list_alloc(ctx, map->n);
9733 for (i = 0; i < map->n; ++i) {
9734 isl_basic_map *bmap;
9736 bmap = isl_basic_map_copy(map->p[i]);
9737 list = isl_basic_map_list_add(list, bmap);
9740 return list;
9743 /* Return the intersection of the elements in the non-empty list "list".
9744 * All elements are assumed to live in the same space.
9746 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9747 __isl_take isl_basic_map_list *list)
9749 int i, n;
9750 isl_basic_map *bmap;
9752 if (!list)
9753 return NULL;
9754 n = isl_basic_map_list_n_basic_map(list);
9755 if (n < 1)
9756 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9757 "expecting non-empty list", goto error);
9759 bmap = isl_basic_map_list_get_basic_map(list, 0);
9760 for (i = 1; i < n; ++i) {
9761 isl_basic_map *bmap_i;
9763 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9764 bmap = isl_basic_map_intersect(bmap, bmap_i);
9767 isl_basic_map_list_free(list);
9768 return bmap;
9769 error:
9770 isl_basic_map_list_free(list);
9771 return NULL;
9774 /* Return the intersection of the elements in the non-empty list "list".
9775 * All elements are assumed to live in the same space.
9777 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9778 __isl_take isl_basic_set_list *list)
9780 return isl_basic_map_list_intersect(list);
9783 /* Return the union of the elements of "list".
9784 * The list is required to have at least one element.
9786 __isl_give isl_set *isl_basic_set_list_union(
9787 __isl_take isl_basic_set_list *list)
9789 int i, n;
9790 isl_space *space;
9791 isl_basic_set *bset;
9792 isl_set *set;
9794 if (!list)
9795 return NULL;
9796 n = isl_basic_set_list_n_basic_set(list);
9797 if (n < 1)
9798 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9799 "expecting non-empty list", goto error);
9801 bset = isl_basic_set_list_get_basic_set(list, 0);
9802 space = isl_basic_set_get_space(bset);
9803 isl_basic_set_free(bset);
9805 set = isl_set_alloc_space(space, n, 0);
9806 for (i = 0; i < n; ++i) {
9807 bset = isl_basic_set_list_get_basic_set(list, i);
9808 set = isl_set_add_basic_set(set, bset);
9811 isl_basic_set_list_free(list);
9812 return set;
9813 error:
9814 isl_basic_set_list_free(list);
9815 return NULL;
9818 /* Return the union of the elements in the non-empty list "list".
9819 * All elements are assumed to live in the same space.
9821 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9823 int i, n;
9824 isl_set *set;
9826 if (!list)
9827 return NULL;
9828 n = isl_set_list_n_set(list);
9829 if (n < 1)
9830 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9831 "expecting non-empty list", goto error);
9833 set = isl_set_list_get_set(list, 0);
9834 for (i = 1; i < n; ++i) {
9835 isl_set *set_i;
9837 set_i = isl_set_list_get_set(list, i);
9838 set = isl_set_union(set, set_i);
9841 isl_set_list_free(list);
9842 return set;
9843 error:
9844 isl_set_list_free(list);
9845 return NULL;
9848 __isl_give isl_basic_map *isl_basic_map_product(
9849 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9851 isl_space *dim_result = NULL;
9852 struct isl_basic_map *bmap;
9853 unsigned in1, in2, out1, out2, nparam, total, pos;
9854 struct isl_dim_map *dim_map1, *dim_map2;
9856 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9857 goto error;
9858 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9859 isl_space_copy(bmap2->dim));
9861 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9862 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9863 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9864 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9865 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9867 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9868 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9869 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9870 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9871 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9872 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9873 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9874 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9875 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9876 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9877 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9879 bmap = isl_basic_map_alloc_space(dim_result,
9880 bmap1->n_div + bmap2->n_div,
9881 bmap1->n_eq + bmap2->n_eq,
9882 bmap1->n_ineq + bmap2->n_ineq);
9883 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9884 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9885 bmap = isl_basic_map_simplify(bmap);
9886 return isl_basic_map_finalize(bmap);
9887 error:
9888 isl_basic_map_free(bmap1);
9889 isl_basic_map_free(bmap2);
9890 return NULL;
9893 __isl_give isl_basic_map *isl_basic_map_flat_product(
9894 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9896 isl_basic_map *prod;
9898 prod = isl_basic_map_product(bmap1, bmap2);
9899 prod = isl_basic_map_flatten(prod);
9900 return prod;
9903 __isl_give isl_basic_set *isl_basic_set_flat_product(
9904 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9906 return isl_basic_map_flat_range_product(bset1, bset2);
9909 __isl_give isl_basic_map *isl_basic_map_domain_product(
9910 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9912 isl_space *space_result = NULL;
9913 isl_basic_map *bmap;
9914 unsigned in1, in2, out, nparam, total, pos;
9915 struct isl_dim_map *dim_map1, *dim_map2;
9917 if (!bmap1 || !bmap2)
9918 goto error;
9920 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9921 isl_space_copy(bmap2->dim));
9923 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9924 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9925 out = isl_basic_map_dim(bmap1, isl_dim_out);
9926 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9928 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9929 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9930 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9931 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9932 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9933 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9934 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9935 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9936 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9937 isl_dim_map_div(dim_map1, bmap1, pos += out);
9938 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9940 bmap = isl_basic_map_alloc_space(space_result,
9941 bmap1->n_div + bmap2->n_div,
9942 bmap1->n_eq + bmap2->n_eq,
9943 bmap1->n_ineq + bmap2->n_ineq);
9944 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9945 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9946 bmap = isl_basic_map_simplify(bmap);
9947 return isl_basic_map_finalize(bmap);
9948 error:
9949 isl_basic_map_free(bmap1);
9950 isl_basic_map_free(bmap2);
9951 return NULL;
9954 __isl_give isl_basic_map *isl_basic_map_range_product(
9955 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9957 isl_bool rational;
9958 isl_space *dim_result = NULL;
9959 isl_basic_map *bmap;
9960 unsigned in, out1, out2, nparam, total, pos;
9961 struct isl_dim_map *dim_map1, *dim_map2;
9963 rational = isl_basic_map_is_rational(bmap1);
9964 if (rational >= 0 && rational)
9965 rational = isl_basic_map_is_rational(bmap2);
9966 if (!bmap1 || !bmap2 || rational < 0)
9967 goto error;
9969 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9970 goto error;
9972 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9973 isl_space_copy(bmap2->dim));
9975 in = isl_basic_map_dim(bmap1, isl_dim_in);
9976 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9977 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9978 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9980 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9981 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9982 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9983 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9984 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9985 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9986 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9987 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9988 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9989 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9990 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9992 bmap = isl_basic_map_alloc_space(dim_result,
9993 bmap1->n_div + bmap2->n_div,
9994 bmap1->n_eq + bmap2->n_eq,
9995 bmap1->n_ineq + bmap2->n_ineq);
9996 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9997 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9998 if (rational)
9999 bmap = isl_basic_map_set_rational(bmap);
10000 bmap = isl_basic_map_simplify(bmap);
10001 return isl_basic_map_finalize(bmap);
10002 error:
10003 isl_basic_map_free(bmap1);
10004 isl_basic_map_free(bmap2);
10005 return NULL;
10008 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10009 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10011 isl_basic_map *prod;
10013 prod = isl_basic_map_range_product(bmap1, bmap2);
10014 prod = isl_basic_map_flatten_range(prod);
10015 return prod;
10018 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10019 * and collect the results.
10020 * The result live in the space obtained by calling "space_product"
10021 * on the spaces of "map1" and "map2".
10022 * If "remove_duplicates" is set then the result may contain duplicates
10023 * (even if the inputs do not) and so we try and remove the obvious
10024 * duplicates.
10026 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10027 __isl_take isl_map *map2,
10028 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10029 __isl_take isl_space *right),
10030 __isl_give isl_basic_map *(*basic_map_product)(
10031 __isl_take isl_basic_map *left,
10032 __isl_take isl_basic_map *right),
10033 int remove_duplicates)
10035 unsigned flags = 0;
10036 struct isl_map *result;
10037 int i, j;
10038 isl_bool m;
10040 m = isl_map_has_equal_params(map1, map2);
10041 if (m < 0)
10042 goto error;
10043 if (!m)
10044 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10045 "parameters don't match", goto error);
10047 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10048 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10049 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10051 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10052 isl_space_copy(map2->dim)),
10053 map1->n * map2->n, flags);
10054 if (!result)
10055 goto error;
10056 for (i = 0; i < map1->n; ++i)
10057 for (j = 0; j < map2->n; ++j) {
10058 struct isl_basic_map *part;
10059 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10060 isl_basic_map_copy(map2->p[j]));
10061 if (isl_basic_map_is_empty(part))
10062 isl_basic_map_free(part);
10063 else
10064 result = isl_map_add_basic_map(result, part);
10065 if (!result)
10066 goto error;
10068 if (remove_duplicates)
10069 result = isl_map_remove_obvious_duplicates(result);
10070 isl_map_free(map1);
10071 isl_map_free(map2);
10072 return result;
10073 error:
10074 isl_map_free(map1);
10075 isl_map_free(map2);
10076 return NULL;
10079 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10081 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10082 __isl_take isl_map *map2)
10084 return map_product(map1, map2, &isl_space_product,
10085 &isl_basic_map_product, 0);
10088 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10089 __isl_take isl_map *map2)
10091 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10094 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10096 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10097 __isl_take isl_map *map2)
10099 isl_map *prod;
10101 prod = isl_map_product(map1, map2);
10102 prod = isl_map_flatten(prod);
10103 return prod;
10106 /* Given two set A and B, construct its Cartesian product A x B.
10108 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10110 return isl_map_range_product(set1, set2);
10113 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10114 __isl_take isl_set *set2)
10116 return isl_map_flat_range_product(set1, set2);
10119 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10121 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10122 __isl_take isl_map *map2)
10124 return map_product(map1, map2, &isl_space_domain_product,
10125 &isl_basic_map_domain_product, 1);
10128 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10130 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10131 __isl_take isl_map *map2)
10133 return map_product(map1, map2, &isl_space_range_product,
10134 &isl_basic_map_range_product, 1);
10137 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10138 __isl_take isl_map *map2)
10140 return isl_map_align_params_map_map_and(map1, map2,
10141 &map_domain_product_aligned);
10144 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10145 __isl_take isl_map *map2)
10147 return isl_map_align_params_map_map_and(map1, map2,
10148 &map_range_product_aligned);
10151 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10153 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10155 isl_space *space;
10156 int total1, keep1, total2, keep2;
10158 if (!map)
10159 return NULL;
10160 if (!isl_space_domain_is_wrapping(map->dim) ||
10161 !isl_space_range_is_wrapping(map->dim))
10162 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10163 "not a product", return isl_map_free(map));
10165 space = isl_map_get_space(map);
10166 total1 = isl_space_dim(space, isl_dim_in);
10167 total2 = isl_space_dim(space, isl_dim_out);
10168 space = isl_space_factor_domain(space);
10169 keep1 = isl_space_dim(space, isl_dim_in);
10170 keep2 = isl_space_dim(space, isl_dim_out);
10171 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10172 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10173 map = isl_map_reset_space(map, space);
10175 return map;
10178 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10180 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10182 isl_space *space;
10183 int total1, keep1, total2, keep2;
10185 if (!map)
10186 return NULL;
10187 if (!isl_space_domain_is_wrapping(map->dim) ||
10188 !isl_space_range_is_wrapping(map->dim))
10189 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10190 "not a product", return isl_map_free(map));
10192 space = isl_map_get_space(map);
10193 total1 = isl_space_dim(space, isl_dim_in);
10194 total2 = isl_space_dim(space, isl_dim_out);
10195 space = isl_space_factor_range(space);
10196 keep1 = isl_space_dim(space, isl_dim_in);
10197 keep2 = isl_space_dim(space, isl_dim_out);
10198 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10199 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10200 map = isl_map_reset_space(map, space);
10202 return map;
10205 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10207 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10209 isl_space *space;
10210 int total, keep;
10212 if (!map)
10213 return NULL;
10214 if (!isl_space_domain_is_wrapping(map->dim))
10215 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10216 "domain is not a product", return isl_map_free(map));
10218 space = isl_map_get_space(map);
10219 total = isl_space_dim(space, isl_dim_in);
10220 space = isl_space_domain_factor_domain(space);
10221 keep = isl_space_dim(space, isl_dim_in);
10222 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10223 map = isl_map_reset_space(map, space);
10225 return map;
10228 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10230 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10232 isl_space *space;
10233 int total, keep;
10235 if (!map)
10236 return NULL;
10237 if (!isl_space_domain_is_wrapping(map->dim))
10238 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10239 "domain is not a product", return isl_map_free(map));
10241 space = isl_map_get_space(map);
10242 total = isl_space_dim(space, isl_dim_in);
10243 space = isl_space_domain_factor_range(space);
10244 keep = isl_space_dim(space, isl_dim_in);
10245 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10246 map = isl_map_reset_space(map, space);
10248 return map;
10251 /* Given a map A -> [B -> C], extract the map A -> B.
10253 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10255 isl_space *space;
10256 int total, keep;
10258 if (!map)
10259 return NULL;
10260 if (!isl_space_range_is_wrapping(map->dim))
10261 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10262 "range is not a product", return isl_map_free(map));
10264 space = isl_map_get_space(map);
10265 total = isl_space_dim(space, isl_dim_out);
10266 space = isl_space_range_factor_domain(space);
10267 keep = isl_space_dim(space, isl_dim_out);
10268 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10269 map = isl_map_reset_space(map, space);
10271 return map;
10274 /* Given a map A -> [B -> C], extract the map A -> C.
10276 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10278 isl_space *space;
10279 int total, keep;
10281 if (!map)
10282 return NULL;
10283 if (!isl_space_range_is_wrapping(map->dim))
10284 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10285 "range is not a product", return isl_map_free(map));
10287 space = isl_map_get_space(map);
10288 total = isl_space_dim(space, isl_dim_out);
10289 space = isl_space_range_factor_range(space);
10290 keep = isl_space_dim(space, isl_dim_out);
10291 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10292 map = isl_map_reset_space(map, space);
10294 return map;
10297 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10299 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10300 __isl_take isl_map *map2)
10302 isl_map *prod;
10304 prod = isl_map_domain_product(map1, map2);
10305 prod = isl_map_flatten_domain(prod);
10306 return prod;
10309 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10311 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10312 __isl_take isl_map *map2)
10314 isl_map *prod;
10316 prod = isl_map_range_product(map1, map2);
10317 prod = isl_map_flatten_range(prod);
10318 return prod;
10321 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10323 int i;
10324 uint32_t hash = isl_hash_init();
10325 unsigned total;
10327 if (!bmap)
10328 return 0;
10329 bmap = isl_basic_map_copy(bmap);
10330 bmap = isl_basic_map_normalize(bmap);
10331 if (!bmap)
10332 return 0;
10333 total = isl_basic_map_total_dim(bmap);
10334 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10335 for (i = 0; i < bmap->n_eq; ++i) {
10336 uint32_t c_hash;
10337 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10338 isl_hash_hash(hash, c_hash);
10340 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10341 for (i = 0; i < bmap->n_ineq; ++i) {
10342 uint32_t c_hash;
10343 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10344 isl_hash_hash(hash, c_hash);
10346 isl_hash_byte(hash, bmap->n_div & 0xFF);
10347 for (i = 0; i < bmap->n_div; ++i) {
10348 uint32_t c_hash;
10349 if (isl_int_is_zero(bmap->div[i][0]))
10350 continue;
10351 isl_hash_byte(hash, i & 0xFF);
10352 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10353 isl_hash_hash(hash, c_hash);
10355 isl_basic_map_free(bmap);
10356 return hash;
10359 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10361 return isl_basic_map_get_hash(bset_to_bmap(bset));
10364 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10366 int i;
10367 uint32_t hash;
10369 if (!map)
10370 return 0;
10371 map = isl_map_copy(map);
10372 map = isl_map_normalize(map);
10373 if (!map)
10374 return 0;
10376 hash = isl_hash_init();
10377 for (i = 0; i < map->n; ++i) {
10378 uint32_t bmap_hash;
10379 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10380 isl_hash_hash(hash, bmap_hash);
10383 isl_map_free(map);
10385 return hash;
10388 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10390 return isl_map_get_hash(set_to_map(set));
10393 /* Return the number of basic maps in the (current) representation of "map".
10395 int isl_map_n_basic_map(__isl_keep isl_map *map)
10397 return map ? map->n : 0;
10400 int isl_set_n_basic_set(__isl_keep isl_set *set)
10402 return set ? set->n : 0;
10405 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10406 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10408 int i;
10410 if (!map)
10411 return isl_stat_error;
10413 for (i = 0; i < map->n; ++i)
10414 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10415 return isl_stat_error;
10417 return isl_stat_ok;
10420 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10421 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10423 int i;
10425 if (!set)
10426 return isl_stat_error;
10428 for (i = 0; i < set->n; ++i)
10429 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10430 return isl_stat_error;
10432 return isl_stat_ok;
10435 /* Return a list of basic sets, the union of which is equal to "set".
10437 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10438 __isl_keep isl_set *set)
10440 int i;
10441 isl_basic_set_list *list;
10443 if (!set)
10444 return NULL;
10446 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10447 for (i = 0; i < set->n; ++i) {
10448 isl_basic_set *bset;
10450 bset = isl_basic_set_copy(set->p[i]);
10451 list = isl_basic_set_list_add(list, bset);
10454 return list;
10457 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10459 isl_space *dim;
10461 if (!bset)
10462 return NULL;
10464 bset = isl_basic_set_cow(bset);
10465 if (!bset)
10466 return NULL;
10468 dim = isl_basic_set_get_space(bset);
10469 dim = isl_space_lift(dim, bset->n_div);
10470 if (!dim)
10471 goto error;
10472 isl_space_free(bset->dim);
10473 bset->dim = dim;
10474 bset->extra -= bset->n_div;
10475 bset->n_div = 0;
10477 bset = isl_basic_set_finalize(bset);
10479 return bset;
10480 error:
10481 isl_basic_set_free(bset);
10482 return NULL;
10485 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10487 int i;
10488 isl_space *dim;
10489 unsigned n_div;
10491 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10493 if (!set)
10494 return NULL;
10496 set = isl_set_cow(set);
10497 if (!set)
10498 return NULL;
10500 n_div = set->p[0]->n_div;
10501 dim = isl_set_get_space(set);
10502 dim = isl_space_lift(dim, n_div);
10503 if (!dim)
10504 goto error;
10505 isl_space_free(set->dim);
10506 set->dim = dim;
10508 for (i = 0; i < set->n; ++i) {
10509 set->p[i] = isl_basic_set_lift(set->p[i]);
10510 if (!set->p[i])
10511 goto error;
10514 return set;
10515 error:
10516 isl_set_free(set);
10517 return NULL;
10520 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10522 unsigned dim;
10523 int size = 0;
10525 if (!bset)
10526 return -1;
10528 dim = isl_basic_set_total_dim(bset);
10529 size += bset->n_eq * (1 + dim);
10530 size += bset->n_ineq * (1 + dim);
10531 size += bset->n_div * (2 + dim);
10533 return size;
10536 int isl_set_size(__isl_keep isl_set *set)
10538 int i;
10539 int size = 0;
10541 if (!set)
10542 return -1;
10544 for (i = 0; i < set->n; ++i)
10545 size += isl_basic_set_size(set->p[i]);
10547 return size;
10550 /* Check if there is any lower bound (if lower == 0) and/or upper
10551 * bound (if upper == 0) on the specified dim.
10553 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10554 enum isl_dim_type type, unsigned pos, int lower, int upper)
10556 int i;
10558 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10559 return isl_bool_error;
10561 pos += isl_basic_map_offset(bmap, type);
10563 for (i = 0; i < bmap->n_div; ++i) {
10564 if (isl_int_is_zero(bmap->div[i][0]))
10565 continue;
10566 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10567 return isl_bool_true;
10570 for (i = 0; i < bmap->n_eq; ++i)
10571 if (!isl_int_is_zero(bmap->eq[i][pos]))
10572 return isl_bool_true;
10574 for (i = 0; i < bmap->n_ineq; ++i) {
10575 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10576 if (sgn > 0)
10577 lower = 1;
10578 if (sgn < 0)
10579 upper = 1;
10582 return lower && upper;
10585 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10586 enum isl_dim_type type, unsigned pos)
10588 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10591 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10592 enum isl_dim_type type, unsigned pos)
10594 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10597 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10598 enum isl_dim_type type, unsigned pos)
10600 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10603 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10604 enum isl_dim_type type, unsigned pos)
10606 int i;
10608 if (!map)
10609 return isl_bool_error;
10611 for (i = 0; i < map->n; ++i) {
10612 isl_bool bounded;
10613 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10614 if (bounded < 0 || !bounded)
10615 return bounded;
10618 return isl_bool_true;
10621 /* Return true if the specified dim is involved in both an upper bound
10622 * and a lower bound.
10624 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10625 enum isl_dim_type type, unsigned pos)
10627 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10630 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10632 static isl_bool has_any_bound(__isl_keep isl_map *map,
10633 enum isl_dim_type type, unsigned pos,
10634 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10635 enum isl_dim_type type, unsigned pos))
10637 int i;
10639 if (!map)
10640 return isl_bool_error;
10642 for (i = 0; i < map->n; ++i) {
10643 isl_bool bounded;
10644 bounded = fn(map->p[i], type, pos);
10645 if (bounded < 0 || bounded)
10646 return bounded;
10649 return isl_bool_false;
10652 /* Return 1 if the specified dim is involved in any lower bound.
10654 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10655 enum isl_dim_type type, unsigned pos)
10657 return has_any_bound(set, type, pos,
10658 &isl_basic_map_dim_has_lower_bound);
10661 /* Return 1 if the specified dim is involved in any upper bound.
10663 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10664 enum isl_dim_type type, unsigned pos)
10666 return has_any_bound(set, type, pos,
10667 &isl_basic_map_dim_has_upper_bound);
10670 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10672 static isl_bool has_bound(__isl_keep isl_map *map,
10673 enum isl_dim_type type, unsigned pos,
10674 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10675 enum isl_dim_type type, unsigned pos))
10677 int i;
10679 if (!map)
10680 return isl_bool_error;
10682 for (i = 0; i < map->n; ++i) {
10683 isl_bool bounded;
10684 bounded = fn(map->p[i], type, pos);
10685 if (bounded < 0 || !bounded)
10686 return bounded;
10689 return isl_bool_true;
10692 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10694 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10695 enum isl_dim_type type, unsigned pos)
10697 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10700 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10702 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10703 enum isl_dim_type type, unsigned pos)
10705 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10708 /* For each of the "n" variables starting at "first", determine
10709 * the sign of the variable and put the results in the first "n"
10710 * elements of the array "signs".
10711 * Sign
10712 * 1 means that the variable is non-negative
10713 * -1 means that the variable is non-positive
10714 * 0 means the variable attains both positive and negative values.
10716 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10717 unsigned first, unsigned n, int *signs)
10719 isl_vec *bound = NULL;
10720 struct isl_tab *tab = NULL;
10721 struct isl_tab_undo *snap;
10722 int i;
10724 if (!bset || !signs)
10725 return isl_stat_error;
10727 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10728 tab = isl_tab_from_basic_set(bset, 0);
10729 if (!bound || !tab)
10730 goto error;
10732 isl_seq_clr(bound->el, bound->size);
10733 isl_int_set_si(bound->el[0], -1);
10735 snap = isl_tab_snap(tab);
10736 for (i = 0; i < n; ++i) {
10737 int empty;
10739 isl_int_set_si(bound->el[1 + first + i], -1);
10740 if (isl_tab_add_ineq(tab, bound->el) < 0)
10741 goto error;
10742 empty = tab->empty;
10743 isl_int_set_si(bound->el[1 + first + i], 0);
10744 if (isl_tab_rollback(tab, snap) < 0)
10745 goto error;
10747 if (empty) {
10748 signs[i] = 1;
10749 continue;
10752 isl_int_set_si(bound->el[1 + first + i], 1);
10753 if (isl_tab_add_ineq(tab, bound->el) < 0)
10754 goto error;
10755 empty = tab->empty;
10756 isl_int_set_si(bound->el[1 + first + i], 0);
10757 if (isl_tab_rollback(tab, snap) < 0)
10758 goto error;
10760 signs[i] = empty ? -1 : 0;
10763 isl_tab_free(tab);
10764 isl_vec_free(bound);
10765 return isl_stat_ok;
10766 error:
10767 isl_tab_free(tab);
10768 isl_vec_free(bound);
10769 return isl_stat_error;
10772 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10773 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10775 if (!bset || !signs)
10776 return isl_stat_error;
10777 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10778 return isl_stat_error);
10780 first += pos(bset->dim, type) - 1;
10781 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10784 /* Is it possible for the integer division "div" to depend (possibly
10785 * indirectly) on any output dimensions?
10787 * If the div is undefined, then we conservatively assume that it
10788 * may depend on them.
10789 * Otherwise, we check if it actually depends on them or on any integer
10790 * divisions that may depend on them.
10792 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10794 int i;
10795 unsigned n_out, o_out;
10796 unsigned n_div, o_div;
10798 if (isl_int_is_zero(bmap->div[div][0]))
10799 return isl_bool_true;
10801 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10802 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10804 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10805 return isl_bool_true;
10807 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10808 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10810 for (i = 0; i < n_div; ++i) {
10811 isl_bool may_involve;
10813 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10814 continue;
10815 may_involve = div_may_involve_output(bmap, i);
10816 if (may_involve < 0 || may_involve)
10817 return may_involve;
10820 return isl_bool_false;
10823 /* Return the first integer division of "bmap" in the range
10824 * [first, first + n[ that may depend on any output dimensions and
10825 * that has a non-zero coefficient in "c" (where the first coefficient
10826 * in "c" corresponds to integer division "first").
10828 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10829 isl_int *c, int first, int n)
10831 int k;
10833 if (!bmap)
10834 return -1;
10836 for (k = first; k < first + n; ++k) {
10837 isl_bool may_involve;
10839 if (isl_int_is_zero(c[k]))
10840 continue;
10841 may_involve = div_may_involve_output(bmap, k);
10842 if (may_involve < 0)
10843 return -1;
10844 if (may_involve)
10845 return k;
10848 return first + n;
10851 /* Look for a pair of inequality constraints in "bmap" of the form
10853 * -l + i >= 0 or i >= l
10854 * and
10855 * n + l - i >= 0 or i <= l + n
10857 * with n < "m" and i the output dimension at position "pos".
10858 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10859 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10860 * and earlier output dimensions, as well as integer divisions that do
10861 * not involve any of the output dimensions.
10863 * Return the index of the first inequality constraint or bmap->n_ineq
10864 * if no such pair can be found.
10866 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10867 int pos, isl_int m)
10869 int i, j;
10870 isl_ctx *ctx;
10871 unsigned total;
10872 unsigned n_div, o_div;
10873 unsigned n_out, o_out;
10874 int less;
10876 if (!bmap)
10877 return -1;
10879 ctx = isl_basic_map_get_ctx(bmap);
10880 total = isl_basic_map_total_dim(bmap);
10881 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10882 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10883 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10884 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10885 for (i = 0; i < bmap->n_ineq; ++i) {
10886 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10887 continue;
10888 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10889 n_out - (pos + 1)) != -1)
10890 continue;
10891 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10892 0, n_div) < n_div)
10893 continue;
10894 for (j = i + 1; j < bmap->n_ineq; ++j) {
10895 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10896 ctx->one))
10897 continue;
10898 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10899 bmap->ineq[j] + 1, total))
10900 continue;
10901 break;
10903 if (j >= bmap->n_ineq)
10904 continue;
10905 isl_int_add(bmap->ineq[i][0],
10906 bmap->ineq[i][0], bmap->ineq[j][0]);
10907 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10908 isl_int_sub(bmap->ineq[i][0],
10909 bmap->ineq[i][0], bmap->ineq[j][0]);
10910 if (!less)
10911 continue;
10912 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10913 return i;
10914 else
10915 return j;
10918 return bmap->n_ineq;
10921 /* Return the index of the equality of "bmap" that defines
10922 * the output dimension "pos" in terms of earlier dimensions.
10923 * The equality may also involve integer divisions, as long
10924 * as those integer divisions are defined in terms of
10925 * parameters or input dimensions.
10926 * In this case, *div is set to the number of integer divisions and
10927 * *ineq is set to the number of inequality constraints (provided
10928 * div and ineq are not NULL).
10930 * The equality may also involve a single integer division involving
10931 * the output dimensions (typically only output dimension "pos") as
10932 * long as the coefficient of output dimension "pos" is 1 or -1 and
10933 * there is a pair of constraints i >= l and i <= l + n, with i referring
10934 * to output dimension "pos", l an expression involving only earlier
10935 * dimensions and n smaller than the coefficient of the integer division
10936 * in the equality. In this case, the output dimension can be defined
10937 * in terms of a modulo expression that does not involve the integer division.
10938 * *div is then set to this single integer division and
10939 * *ineq is set to the index of constraint i >= l.
10941 * Return bmap->n_eq if there is no such equality.
10942 * Return -1 on error.
10944 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10945 int pos, int *div, int *ineq)
10947 int j, k, l;
10948 unsigned n_out, o_out;
10949 unsigned n_div, o_div;
10951 if (!bmap)
10952 return -1;
10954 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10955 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10956 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10957 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10959 if (ineq)
10960 *ineq = bmap->n_ineq;
10961 if (div)
10962 *div = n_div;
10963 for (j = 0; j < bmap->n_eq; ++j) {
10964 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10965 continue;
10966 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10967 n_out - (pos + 1)) != -1)
10968 continue;
10969 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10970 0, n_div);
10971 if (k >= n_div)
10972 return j;
10973 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10974 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10975 continue;
10976 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10977 k + 1, n_div - (k+1)) < n_div)
10978 continue;
10979 l = find_modulo_constraint_pair(bmap, pos,
10980 bmap->eq[j][o_div + k]);
10981 if (l < 0)
10982 return -1;
10983 if (l >= bmap->n_ineq)
10984 continue;
10985 if (div)
10986 *div = k;
10987 if (ineq)
10988 *ineq = l;
10989 return j;
10992 return bmap->n_eq;
10995 /* Check if the given basic map is obviously single-valued.
10996 * In particular, for each output dimension, check that there is
10997 * an equality that defines the output dimension in terms of
10998 * earlier dimensions.
11000 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11002 int i;
11003 unsigned n_out;
11005 if (!bmap)
11006 return isl_bool_error;
11008 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11010 for (i = 0; i < n_out; ++i) {
11011 int eq;
11013 eq = isl_basic_map_output_defining_equality(bmap, i,
11014 NULL, NULL);
11015 if (eq < 0)
11016 return isl_bool_error;
11017 if (eq >= bmap->n_eq)
11018 return isl_bool_false;
11021 return isl_bool_true;
11024 /* Check if the given basic map is single-valued.
11025 * We simply compute
11027 * M \circ M^-1
11029 * and check if the result is a subset of the identity mapping.
11031 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11033 isl_space *space;
11034 isl_basic_map *test;
11035 isl_basic_map *id;
11036 isl_bool sv;
11038 sv = isl_basic_map_plain_is_single_valued(bmap);
11039 if (sv < 0 || sv)
11040 return sv;
11042 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11043 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11045 space = isl_basic_map_get_space(bmap);
11046 space = isl_space_map_from_set(isl_space_range(space));
11047 id = isl_basic_map_identity(space);
11049 sv = isl_basic_map_is_subset(test, id);
11051 isl_basic_map_free(test);
11052 isl_basic_map_free(id);
11054 return sv;
11057 /* Check if the given map is obviously single-valued.
11059 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11061 if (!map)
11062 return isl_bool_error;
11063 if (map->n == 0)
11064 return isl_bool_true;
11065 if (map->n >= 2)
11066 return isl_bool_false;
11068 return isl_basic_map_plain_is_single_valued(map->p[0]);
11071 /* Check if the given map is single-valued.
11072 * We simply compute
11074 * M \circ M^-1
11076 * and check if the result is a subset of the identity mapping.
11078 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11080 isl_space *dim;
11081 isl_map *test;
11082 isl_map *id;
11083 isl_bool sv;
11085 sv = isl_map_plain_is_single_valued(map);
11086 if (sv < 0 || sv)
11087 return sv;
11089 test = isl_map_reverse(isl_map_copy(map));
11090 test = isl_map_apply_range(test, isl_map_copy(map));
11092 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11093 id = isl_map_identity(dim);
11095 sv = isl_map_is_subset(test, id);
11097 isl_map_free(test);
11098 isl_map_free(id);
11100 return sv;
11103 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11105 isl_bool in;
11107 map = isl_map_copy(map);
11108 map = isl_map_reverse(map);
11109 in = isl_map_is_single_valued(map);
11110 isl_map_free(map);
11112 return in;
11115 /* Check if the given map is obviously injective.
11117 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11119 isl_bool in;
11121 map = isl_map_copy(map);
11122 map = isl_map_reverse(map);
11123 in = isl_map_plain_is_single_valued(map);
11124 isl_map_free(map);
11126 return in;
11129 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11131 isl_bool sv;
11133 sv = isl_map_is_single_valued(map);
11134 if (sv < 0 || !sv)
11135 return sv;
11137 return isl_map_is_injective(map);
11140 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11142 return isl_map_is_single_valued(set_to_map(set));
11145 /* Does "map" only map elements to themselves?
11147 * If the domain and range spaces are different, then "map"
11148 * is considered not to be an identity relation, even if it is empty.
11149 * Otherwise, construct the maximal identity relation and
11150 * check whether "map" is a subset of this relation.
11152 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11154 isl_space *space;
11155 isl_map *id;
11156 isl_bool equal, is_identity;
11158 space = isl_map_get_space(map);
11159 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11160 isl_space_free(space);
11161 if (equal < 0 || !equal)
11162 return equal;
11164 id = isl_map_identity(isl_map_get_space(map));
11165 is_identity = isl_map_is_subset(map, id);
11166 isl_map_free(id);
11168 return is_identity;
11171 int isl_map_is_translation(__isl_keep isl_map *map)
11173 int ok;
11174 isl_set *delta;
11176 delta = isl_map_deltas(isl_map_copy(map));
11177 ok = isl_set_is_singleton(delta);
11178 isl_set_free(delta);
11180 return ok;
11183 static int unique(isl_int *p, unsigned pos, unsigned len)
11185 if (isl_seq_first_non_zero(p, pos) != -1)
11186 return 0;
11187 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11188 return 0;
11189 return 1;
11192 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11194 int i, j;
11195 unsigned nvar;
11196 unsigned ovar;
11198 if (!bset)
11199 return isl_bool_error;
11201 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11202 return isl_bool_false;
11204 nvar = isl_basic_set_dim(bset, isl_dim_set);
11205 ovar = isl_space_offset(bset->dim, isl_dim_set);
11206 for (j = 0; j < nvar; ++j) {
11207 int lower = 0, upper = 0;
11208 for (i = 0; i < bset->n_eq; ++i) {
11209 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11210 continue;
11211 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11212 return isl_bool_false;
11213 break;
11215 if (i < bset->n_eq)
11216 continue;
11217 for (i = 0; i < bset->n_ineq; ++i) {
11218 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11219 continue;
11220 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11221 return isl_bool_false;
11222 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11223 lower = 1;
11224 else
11225 upper = 1;
11227 if (!lower || !upper)
11228 return isl_bool_false;
11231 return isl_bool_true;
11234 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11236 if (!set)
11237 return isl_bool_error;
11238 if (set->n != 1)
11239 return isl_bool_false;
11241 return isl_basic_set_is_box(set->p[0]);
11244 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11246 if (!bset)
11247 return isl_bool_error;
11249 return isl_space_is_wrapping(bset->dim);
11252 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11254 if (!set)
11255 return isl_bool_error;
11257 return isl_space_is_wrapping(set->dim);
11260 /* Modify the space of "map" through a call to "change".
11261 * If "can_change" is set (not NULL), then first call it to check
11262 * if the modification is allowed, printing the error message "cannot_change"
11263 * if it is not.
11265 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11266 isl_bool (*can_change)(__isl_keep isl_map *map),
11267 const char *cannot_change,
11268 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11270 isl_bool ok;
11271 isl_space *space;
11273 if (!map)
11274 return NULL;
11276 ok = can_change ? can_change(map) : isl_bool_true;
11277 if (ok < 0)
11278 return isl_map_free(map);
11279 if (!ok)
11280 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11281 return isl_map_free(map));
11283 space = change(isl_map_get_space(map));
11284 map = isl_map_reset_space(map, space);
11286 return map;
11289 /* Is the domain of "map" a wrapped relation?
11291 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11293 if (!map)
11294 return isl_bool_error;
11296 return isl_space_domain_is_wrapping(map->dim);
11299 /* Does "map" have a wrapped relation in both domain and range?
11301 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11303 return isl_space_is_product(isl_map_peek_space(map));
11306 /* Is the range of "map" a wrapped relation?
11308 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11310 if (!map)
11311 return isl_bool_error;
11313 return isl_space_range_is_wrapping(map->dim);
11316 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11318 bmap = isl_basic_map_cow(bmap);
11319 if (!bmap)
11320 return NULL;
11322 bmap->dim = isl_space_wrap(bmap->dim);
11323 if (!bmap->dim)
11324 goto error;
11326 bmap = isl_basic_map_finalize(bmap);
11328 return bset_from_bmap(bmap);
11329 error:
11330 isl_basic_map_free(bmap);
11331 return NULL;
11334 /* Given a map A -> B, return the set (A -> B).
11336 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11338 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11341 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11343 bset = isl_basic_set_cow(bset);
11344 if (!bset)
11345 return NULL;
11347 bset->dim = isl_space_unwrap(bset->dim);
11348 if (!bset->dim)
11349 goto error;
11351 bset = isl_basic_set_finalize(bset);
11353 return bset_to_bmap(bset);
11354 error:
11355 isl_basic_set_free(bset);
11356 return NULL;
11359 /* Given a set (A -> B), return the map A -> B.
11360 * Error out if "set" is not of the form (A -> B).
11362 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11364 return isl_map_change_space(set, &isl_set_is_wrapping,
11365 "not a wrapping set", &isl_space_unwrap);
11368 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11369 enum isl_dim_type type)
11371 if (!bmap)
11372 return NULL;
11374 if (!isl_space_is_named_or_nested(bmap->dim, type))
11375 return bmap;
11377 bmap = isl_basic_map_cow(bmap);
11378 if (!bmap)
11379 return NULL;
11381 bmap->dim = isl_space_reset(bmap->dim, type);
11382 if (!bmap->dim)
11383 goto error;
11385 bmap = isl_basic_map_finalize(bmap);
11387 return bmap;
11388 error:
11389 isl_basic_map_free(bmap);
11390 return NULL;
11393 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11394 enum isl_dim_type type)
11396 int i;
11398 if (!map)
11399 return NULL;
11401 if (!isl_space_is_named_or_nested(map->dim, type))
11402 return map;
11404 map = isl_map_cow(map);
11405 if (!map)
11406 return NULL;
11408 for (i = 0; i < map->n; ++i) {
11409 map->p[i] = isl_basic_map_reset(map->p[i], type);
11410 if (!map->p[i])
11411 goto error;
11413 map->dim = isl_space_reset(map->dim, type);
11414 if (!map->dim)
11415 goto error;
11417 return map;
11418 error:
11419 isl_map_free(map);
11420 return NULL;
11423 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11425 if (!bmap)
11426 return NULL;
11428 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11429 return bmap;
11431 bmap = isl_basic_map_cow(bmap);
11432 if (!bmap)
11433 return NULL;
11435 bmap->dim = isl_space_flatten(bmap->dim);
11436 if (!bmap->dim)
11437 goto error;
11439 bmap = isl_basic_map_finalize(bmap);
11441 return bmap;
11442 error:
11443 isl_basic_map_free(bmap);
11444 return NULL;
11447 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11449 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11452 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11453 __isl_take isl_basic_map *bmap)
11455 if (!bmap)
11456 return NULL;
11458 if (!bmap->dim->nested[0])
11459 return bmap;
11461 bmap = isl_basic_map_cow(bmap);
11462 if (!bmap)
11463 return NULL;
11465 bmap->dim = isl_space_flatten_domain(bmap->dim);
11466 if (!bmap->dim)
11467 goto error;
11469 bmap = isl_basic_map_finalize(bmap);
11471 return bmap;
11472 error:
11473 isl_basic_map_free(bmap);
11474 return NULL;
11477 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11478 __isl_take isl_basic_map *bmap)
11480 if (!bmap)
11481 return NULL;
11483 if (!bmap->dim->nested[1])
11484 return bmap;
11486 bmap = isl_basic_map_cow(bmap);
11487 if (!bmap)
11488 return NULL;
11490 bmap->dim = isl_space_flatten_range(bmap->dim);
11491 if (!bmap->dim)
11492 goto error;
11494 bmap = isl_basic_map_finalize(bmap);
11496 return bmap;
11497 error:
11498 isl_basic_map_free(bmap);
11499 return NULL;
11502 /* Remove any internal structure from the spaces of domain and range of "map".
11504 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11506 if (!map)
11507 return NULL;
11509 if (!map->dim->nested[0] && !map->dim->nested[1])
11510 return map;
11512 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11515 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11517 return set_from_map(isl_map_flatten(set_to_map(set)));
11520 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11522 isl_space *dim, *flat_dim;
11523 isl_map *map;
11525 dim = isl_set_get_space(set);
11526 flat_dim = isl_space_flatten(isl_space_copy(dim));
11527 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11528 map = isl_map_intersect_domain(map, set);
11530 return map;
11533 /* Remove any internal structure from the space of the domain of "map".
11535 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11537 if (!map)
11538 return NULL;
11540 if (!map->dim->nested[0])
11541 return map;
11543 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11546 /* Remove any internal structure from the space of the range of "map".
11548 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11550 if (!map)
11551 return NULL;
11553 if (!map->dim->nested[1])
11554 return map;
11556 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11559 /* Reorder the dimensions of "bmap" according to the given dim_map
11560 * and set the dimension specification to "dim" and
11561 * perform Gaussian elimination on the result.
11563 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11564 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11566 isl_basic_map *res;
11567 unsigned flags;
11569 bmap = isl_basic_map_cow(bmap);
11570 if (!bmap || !dim || !dim_map)
11571 goto error;
11573 flags = bmap->flags;
11574 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11575 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11576 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11577 res = isl_basic_map_alloc_space(dim,
11578 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11579 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11580 if (res)
11581 res->flags = flags;
11582 res = isl_basic_map_gauss(res, NULL);
11583 res = isl_basic_map_finalize(res);
11584 return res;
11585 error:
11586 free(dim_map);
11587 isl_basic_map_free(bmap);
11588 isl_space_free(dim);
11589 return NULL;
11592 /* Reorder the dimensions of "map" according to given reordering.
11594 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11595 __isl_take isl_reordering *r)
11597 int i;
11598 struct isl_dim_map *dim_map;
11600 map = isl_map_cow(map);
11601 dim_map = isl_dim_map_from_reordering(r);
11602 if (!map || !r || !dim_map)
11603 goto error;
11605 for (i = 0; i < map->n; ++i) {
11606 struct isl_dim_map *dim_map_i;
11608 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11610 map->p[i] = isl_basic_map_realign(map->p[i],
11611 isl_space_copy(r->dim), dim_map_i);
11613 if (!map->p[i])
11614 goto error;
11617 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11619 isl_reordering_free(r);
11620 free(dim_map);
11621 return map;
11622 error:
11623 free(dim_map);
11624 isl_map_free(map);
11625 isl_reordering_free(r);
11626 return NULL;
11629 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11630 __isl_take isl_reordering *r)
11632 return set_from_map(isl_map_realign(set_to_map(set), r));
11635 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11636 __isl_take isl_space *model)
11638 isl_ctx *ctx;
11639 isl_bool aligned;
11641 if (!map || !model)
11642 goto error;
11644 ctx = isl_space_get_ctx(model);
11645 if (!isl_space_has_named_params(model))
11646 isl_die(ctx, isl_error_invalid,
11647 "model has unnamed parameters", goto error);
11648 if (isl_map_check_named_params(map) < 0)
11649 goto error;
11650 aligned = isl_map_space_has_equal_params(map, model);
11651 if (aligned < 0)
11652 goto error;
11653 if (!aligned) {
11654 isl_reordering *exp;
11656 model = isl_space_drop_dims(model, isl_dim_in,
11657 0, isl_space_dim(model, isl_dim_in));
11658 model = isl_space_drop_dims(model, isl_dim_out,
11659 0, isl_space_dim(model, isl_dim_out));
11660 exp = isl_parameter_alignment_reordering(map->dim, model);
11661 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11662 map = isl_map_realign(map, exp);
11665 isl_space_free(model);
11666 return map;
11667 error:
11668 isl_space_free(model);
11669 isl_map_free(map);
11670 return NULL;
11673 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11674 __isl_take isl_space *model)
11676 return isl_map_align_params(set, model);
11679 /* Align the parameters of "bmap" to those of "model", introducing
11680 * additional parameters if needed.
11682 __isl_give isl_basic_map *isl_basic_map_align_params(
11683 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11685 isl_ctx *ctx;
11686 isl_bool equal_params;
11688 if (!bmap || !model)
11689 goto error;
11691 ctx = isl_space_get_ctx(model);
11692 if (!isl_space_has_named_params(model))
11693 isl_die(ctx, isl_error_invalid,
11694 "model has unnamed parameters", goto error);
11695 if (!isl_space_has_named_params(bmap->dim))
11696 isl_die(ctx, isl_error_invalid,
11697 "relation has unnamed parameters", goto error);
11698 equal_params = isl_space_has_equal_params(bmap->dim, model);
11699 if (equal_params < 0)
11700 goto error;
11701 if (!equal_params) {
11702 isl_reordering *exp;
11703 struct isl_dim_map *dim_map;
11705 model = isl_space_drop_dims(model, isl_dim_in,
11706 0, isl_space_dim(model, isl_dim_in));
11707 model = isl_space_drop_dims(model, isl_dim_out,
11708 0, isl_space_dim(model, isl_dim_out));
11709 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11710 exp = isl_reordering_extend_space(exp,
11711 isl_basic_map_get_space(bmap));
11712 dim_map = isl_dim_map_from_reordering(exp);
11713 bmap = isl_basic_map_realign(bmap,
11714 exp ? isl_space_copy(exp->dim) : NULL,
11715 isl_dim_map_extend(dim_map, bmap));
11716 isl_reordering_free(exp);
11717 free(dim_map);
11720 isl_space_free(model);
11721 return bmap;
11722 error:
11723 isl_space_free(model);
11724 isl_basic_map_free(bmap);
11725 return NULL;
11728 /* Do "bset" and "space" have the same parameters?
11730 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11731 __isl_keep isl_space *space)
11733 isl_space *bset_space;
11735 bset_space = isl_basic_set_peek_space(bset);
11736 return isl_space_has_equal_params(bset_space, space);
11739 /* Do "map" and "space" have the same parameters?
11741 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11742 __isl_keep isl_space *space)
11744 isl_space *map_space;
11746 map_space = isl_map_peek_space(map);
11747 return isl_space_has_equal_params(map_space, space);
11750 /* Do "set" and "space" have the same parameters?
11752 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11753 __isl_keep isl_space *space)
11755 return isl_map_space_has_equal_params(set_to_map(set), space);
11758 /* Align the parameters of "bset" to those of "model", introducing
11759 * additional parameters if needed.
11761 __isl_give isl_basic_set *isl_basic_set_align_params(
11762 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11764 return isl_basic_map_align_params(bset, model);
11767 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11768 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11769 enum isl_dim_type c2, enum isl_dim_type c3,
11770 enum isl_dim_type c4, enum isl_dim_type c5)
11772 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11773 struct isl_mat *mat;
11774 int i, j, k;
11775 int pos;
11777 if (!bmap)
11778 return NULL;
11779 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11780 isl_basic_map_total_dim(bmap) + 1);
11781 if (!mat)
11782 return NULL;
11783 for (i = 0; i < bmap->n_eq; ++i)
11784 for (j = 0, pos = 0; j < 5; ++j) {
11785 int off = isl_basic_map_offset(bmap, c[j]);
11786 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11787 isl_int_set(mat->row[i][pos],
11788 bmap->eq[i][off + k]);
11789 ++pos;
11793 return mat;
11796 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11797 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11798 enum isl_dim_type c2, enum isl_dim_type c3,
11799 enum isl_dim_type c4, enum isl_dim_type c5)
11801 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11802 struct isl_mat *mat;
11803 int i, j, k;
11804 int pos;
11806 if (!bmap)
11807 return NULL;
11808 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11809 isl_basic_map_total_dim(bmap) + 1);
11810 if (!mat)
11811 return NULL;
11812 for (i = 0; i < bmap->n_ineq; ++i)
11813 for (j = 0, pos = 0; j < 5; ++j) {
11814 int off = isl_basic_map_offset(bmap, c[j]);
11815 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11816 isl_int_set(mat->row[i][pos],
11817 bmap->ineq[i][off + k]);
11818 ++pos;
11822 return mat;
11825 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11826 __isl_take isl_space *dim,
11827 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11828 enum isl_dim_type c2, enum isl_dim_type c3,
11829 enum isl_dim_type c4, enum isl_dim_type c5)
11831 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11832 isl_basic_map *bmap;
11833 unsigned total;
11834 unsigned extra;
11835 int i, j, k, l;
11836 int pos;
11838 if (!dim || !eq || !ineq)
11839 goto error;
11841 if (eq->n_col != ineq->n_col)
11842 isl_die(dim->ctx, isl_error_invalid,
11843 "equalities and inequalities matrices should have "
11844 "same number of columns", goto error);
11846 total = 1 + isl_space_dim(dim, isl_dim_all);
11848 if (eq->n_col < total)
11849 isl_die(dim->ctx, isl_error_invalid,
11850 "number of columns too small", goto error);
11852 extra = eq->n_col - total;
11854 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11855 eq->n_row, ineq->n_row);
11856 if (!bmap)
11857 goto error;
11858 for (i = 0; i < extra; ++i) {
11859 k = isl_basic_map_alloc_div(bmap);
11860 if (k < 0)
11861 goto error;
11862 isl_int_set_si(bmap->div[k][0], 0);
11864 for (i = 0; i < eq->n_row; ++i) {
11865 l = isl_basic_map_alloc_equality(bmap);
11866 if (l < 0)
11867 goto error;
11868 for (j = 0, pos = 0; j < 5; ++j) {
11869 int off = isl_basic_map_offset(bmap, c[j]);
11870 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11871 isl_int_set(bmap->eq[l][off + k],
11872 eq->row[i][pos]);
11873 ++pos;
11877 for (i = 0; i < ineq->n_row; ++i) {
11878 l = isl_basic_map_alloc_inequality(bmap);
11879 if (l < 0)
11880 goto error;
11881 for (j = 0, pos = 0; j < 5; ++j) {
11882 int off = isl_basic_map_offset(bmap, c[j]);
11883 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11884 isl_int_set(bmap->ineq[l][off + k],
11885 ineq->row[i][pos]);
11886 ++pos;
11891 isl_space_free(dim);
11892 isl_mat_free(eq);
11893 isl_mat_free(ineq);
11895 bmap = isl_basic_map_simplify(bmap);
11896 return isl_basic_map_finalize(bmap);
11897 error:
11898 isl_space_free(dim);
11899 isl_mat_free(eq);
11900 isl_mat_free(ineq);
11901 return NULL;
11904 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11905 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11906 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11908 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11909 c1, c2, c3, c4, isl_dim_in);
11912 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11913 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11914 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11916 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11917 c1, c2, c3, c4, isl_dim_in);
11920 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11921 __isl_take isl_space *dim,
11922 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11923 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11925 isl_basic_map *bmap;
11926 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11927 c1, c2, c3, c4, isl_dim_in);
11928 return bset_from_bmap(bmap);
11931 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11933 if (!bmap)
11934 return isl_bool_error;
11936 return isl_space_can_zip(bmap->dim);
11939 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11941 if (!map)
11942 return isl_bool_error;
11944 return isl_space_can_zip(map->dim);
11947 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11948 * (A -> C) -> (B -> D).
11950 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11952 unsigned pos;
11953 unsigned n1;
11954 unsigned n2;
11956 if (!bmap)
11957 return NULL;
11959 if (!isl_basic_map_can_zip(bmap))
11960 isl_die(bmap->ctx, isl_error_invalid,
11961 "basic map cannot be zipped", goto error);
11962 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11963 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11964 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11965 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11966 bmap = isl_basic_map_cow(bmap);
11967 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11968 if (!bmap)
11969 return NULL;
11970 bmap->dim = isl_space_zip(bmap->dim);
11971 if (!bmap->dim)
11972 goto error;
11973 bmap = isl_basic_map_mark_final(bmap);
11974 return bmap;
11975 error:
11976 isl_basic_map_free(bmap);
11977 return NULL;
11980 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11981 * (A -> C) -> (B -> D).
11983 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11985 int i;
11987 if (!map)
11988 return NULL;
11990 if (!isl_map_can_zip(map))
11991 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11992 goto error);
11994 map = isl_map_cow(map);
11995 if (!map)
11996 return NULL;
11998 for (i = 0; i < map->n; ++i) {
11999 map->p[i] = isl_basic_map_zip(map->p[i]);
12000 if (!map->p[i])
12001 goto error;
12004 map->dim = isl_space_zip(map->dim);
12005 if (!map->dim)
12006 goto error;
12008 return map;
12009 error:
12010 isl_map_free(map);
12011 return NULL;
12014 /* Can we apply isl_basic_map_curry to "bmap"?
12015 * That is, does it have a nested relation in its domain?
12017 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12019 if (!bmap)
12020 return isl_bool_error;
12022 return isl_space_can_curry(bmap->dim);
12025 /* Can we apply isl_map_curry to "map"?
12026 * That is, does it have a nested relation in its domain?
12028 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12030 if (!map)
12031 return isl_bool_error;
12033 return isl_space_can_curry(map->dim);
12036 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12037 * A -> (B -> C).
12039 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12042 if (!bmap)
12043 return NULL;
12045 if (!isl_basic_map_can_curry(bmap))
12046 isl_die(bmap->ctx, isl_error_invalid,
12047 "basic map cannot be curried", goto error);
12048 bmap = isl_basic_map_cow(bmap);
12049 if (!bmap)
12050 return NULL;
12051 bmap->dim = isl_space_curry(bmap->dim);
12052 if (!bmap->dim)
12053 goto error;
12054 bmap = isl_basic_map_mark_final(bmap);
12055 return bmap;
12056 error:
12057 isl_basic_map_free(bmap);
12058 return NULL;
12061 /* Given a map (A -> B) -> C, return the corresponding map
12062 * A -> (B -> C).
12064 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12066 return isl_map_change_space(map, &isl_map_can_curry,
12067 "map cannot be curried", &isl_space_curry);
12070 /* Can isl_map_range_curry be applied to "map"?
12071 * That is, does it have a nested relation in its range,
12072 * the domain of which is itself a nested relation?
12074 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12076 if (!map)
12077 return isl_bool_error;
12079 return isl_space_can_range_curry(map->dim);
12082 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12083 * A -> (B -> (C -> D)).
12085 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12087 return isl_map_change_space(map, &isl_map_can_range_curry,
12088 "map range cannot be curried",
12089 &isl_space_range_curry);
12092 /* Can we apply isl_basic_map_uncurry to "bmap"?
12093 * That is, does it have a nested relation in its domain?
12095 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12097 if (!bmap)
12098 return isl_bool_error;
12100 return isl_space_can_uncurry(bmap->dim);
12103 /* Can we apply isl_map_uncurry to "map"?
12104 * That is, does it have a nested relation in its domain?
12106 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12108 if (!map)
12109 return isl_bool_error;
12111 return isl_space_can_uncurry(map->dim);
12114 /* Given a basic map A -> (B -> C), return the corresponding basic map
12115 * (A -> B) -> C.
12117 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12120 if (!bmap)
12121 return NULL;
12123 if (!isl_basic_map_can_uncurry(bmap))
12124 isl_die(bmap->ctx, isl_error_invalid,
12125 "basic map cannot be uncurried",
12126 return isl_basic_map_free(bmap));
12127 bmap = isl_basic_map_cow(bmap);
12128 if (!bmap)
12129 return NULL;
12130 bmap->dim = isl_space_uncurry(bmap->dim);
12131 if (!bmap->dim)
12132 return isl_basic_map_free(bmap);
12133 bmap = isl_basic_map_mark_final(bmap);
12134 return bmap;
12137 /* Given a map A -> (B -> C), return the corresponding map
12138 * (A -> B) -> C.
12140 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12142 return isl_map_change_space(map, &isl_map_can_uncurry,
12143 "map cannot be uncurried", &isl_space_uncurry);
12146 /* Construct a basic map mapping the domain of the affine expression
12147 * to a one-dimensional range prescribed by the affine expression.
12148 * If "rational" is set, then construct a rational basic map.
12150 * A NaN affine expression cannot be converted to a basic map.
12152 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12153 __isl_take isl_aff *aff, int rational)
12155 int k;
12156 int pos;
12157 isl_bool is_nan;
12158 isl_local_space *ls;
12159 isl_basic_map *bmap = NULL;
12161 if (!aff)
12162 return NULL;
12163 is_nan = isl_aff_is_nan(aff);
12164 if (is_nan < 0)
12165 goto error;
12166 if (is_nan)
12167 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12168 "cannot convert NaN", goto error);
12170 ls = isl_aff_get_local_space(aff);
12171 bmap = isl_basic_map_from_local_space(ls);
12172 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12173 k = isl_basic_map_alloc_equality(bmap);
12174 if (k < 0)
12175 goto error;
12177 pos = isl_basic_map_offset(bmap, isl_dim_out);
12178 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12179 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12180 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12181 aff->v->size - (pos + 1));
12183 isl_aff_free(aff);
12184 if (rational)
12185 bmap = isl_basic_map_set_rational(bmap);
12186 bmap = isl_basic_map_gauss(bmap, NULL);
12187 bmap = isl_basic_map_finalize(bmap);
12188 return bmap;
12189 error:
12190 isl_aff_free(aff);
12191 isl_basic_map_free(bmap);
12192 return NULL;
12195 /* Construct a basic map mapping the domain of the affine expression
12196 * to a one-dimensional range prescribed by the affine expression.
12198 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12200 return isl_basic_map_from_aff2(aff, 0);
12203 /* Construct a map mapping the domain of the affine expression
12204 * to a one-dimensional range prescribed by the affine expression.
12206 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12208 isl_basic_map *bmap;
12210 bmap = isl_basic_map_from_aff(aff);
12211 return isl_map_from_basic_map(bmap);
12214 /* Construct a basic map mapping the domain the multi-affine expression
12215 * to its range, with each dimension in the range equated to the
12216 * corresponding affine expression.
12217 * If "rational" is set, then construct a rational basic map.
12219 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12220 __isl_take isl_multi_aff *maff, int rational)
12222 int i;
12223 isl_space *space;
12224 isl_basic_map *bmap;
12226 if (!maff)
12227 return NULL;
12229 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12230 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12231 "invalid space", goto error);
12233 space = isl_space_domain(isl_multi_aff_get_space(maff));
12234 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12235 if (rational)
12236 bmap = isl_basic_map_set_rational(bmap);
12238 for (i = 0; i < maff->n; ++i) {
12239 isl_aff *aff;
12240 isl_basic_map *bmap_i;
12242 aff = isl_aff_copy(maff->p[i]);
12243 bmap_i = isl_basic_map_from_aff2(aff, rational);
12245 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12248 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12250 isl_multi_aff_free(maff);
12251 return bmap;
12252 error:
12253 isl_multi_aff_free(maff);
12254 return NULL;
12257 /* Construct a basic map mapping the domain the multi-affine expression
12258 * to its range, with each dimension in the range equated to the
12259 * corresponding affine expression.
12261 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12262 __isl_take isl_multi_aff *ma)
12264 return isl_basic_map_from_multi_aff2(ma, 0);
12267 /* Construct a map mapping the domain the multi-affine expression
12268 * to its range, with each dimension in the range equated to the
12269 * corresponding affine expression.
12271 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12273 isl_basic_map *bmap;
12275 bmap = isl_basic_map_from_multi_aff(maff);
12276 return isl_map_from_basic_map(bmap);
12279 /* Construct a basic map mapping a domain in the given space to
12280 * to an n-dimensional range, with n the number of elements in the list,
12281 * where each coordinate in the range is prescribed by the
12282 * corresponding affine expression.
12283 * The domains of all affine expressions in the list are assumed to match
12284 * domain_dim.
12286 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12287 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12289 int i;
12290 isl_space *dim;
12291 isl_basic_map *bmap;
12293 if (!list)
12294 return NULL;
12296 dim = isl_space_from_domain(domain_dim);
12297 bmap = isl_basic_map_universe(dim);
12299 for (i = 0; i < list->n; ++i) {
12300 isl_aff *aff;
12301 isl_basic_map *bmap_i;
12303 aff = isl_aff_copy(list->p[i]);
12304 bmap_i = isl_basic_map_from_aff(aff);
12306 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12309 isl_aff_list_free(list);
12310 return bmap;
12313 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12314 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12316 return isl_map_equate(set, type1, pos1, type2, pos2);
12319 /* Construct a basic map where the given dimensions are equal to each other.
12321 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12322 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12324 isl_basic_map *bmap = NULL;
12325 int i;
12327 if (!space)
12328 return NULL;
12330 if (pos1 >= isl_space_dim(space, type1))
12331 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12332 "index out of bounds", goto error);
12333 if (pos2 >= isl_space_dim(space, type2))
12334 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12335 "index out of bounds", goto error);
12337 if (type1 == type2 && pos1 == pos2)
12338 return isl_basic_map_universe(space);
12340 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12341 i = isl_basic_map_alloc_equality(bmap);
12342 if (i < 0)
12343 goto error;
12344 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12345 pos1 += isl_basic_map_offset(bmap, type1);
12346 pos2 += isl_basic_map_offset(bmap, type2);
12347 isl_int_set_si(bmap->eq[i][pos1], -1);
12348 isl_int_set_si(bmap->eq[i][pos2], 1);
12349 bmap = isl_basic_map_finalize(bmap);
12350 isl_space_free(space);
12351 return bmap;
12352 error:
12353 isl_space_free(space);
12354 isl_basic_map_free(bmap);
12355 return NULL;
12358 /* Add a constraint imposing that the given two dimensions are equal.
12360 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12361 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12363 isl_basic_map *eq;
12365 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12367 bmap = isl_basic_map_intersect(bmap, eq);
12369 return bmap;
12372 /* Add a constraint imposing that the given two dimensions are equal.
12374 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12375 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12377 isl_basic_map *bmap;
12379 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12381 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12383 return map;
12386 /* Add a constraint imposing that the given two dimensions have opposite values.
12388 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12389 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12391 isl_basic_map *bmap = NULL;
12392 int i;
12394 if (!map)
12395 return NULL;
12397 if (pos1 >= isl_map_dim(map, type1))
12398 isl_die(map->ctx, isl_error_invalid,
12399 "index out of bounds", goto error);
12400 if (pos2 >= isl_map_dim(map, type2))
12401 isl_die(map->ctx, isl_error_invalid,
12402 "index out of bounds", goto error);
12404 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12405 i = isl_basic_map_alloc_equality(bmap);
12406 if (i < 0)
12407 goto error;
12408 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12409 pos1 += isl_basic_map_offset(bmap, type1);
12410 pos2 += isl_basic_map_offset(bmap, type2);
12411 isl_int_set_si(bmap->eq[i][pos1], 1);
12412 isl_int_set_si(bmap->eq[i][pos2], 1);
12413 bmap = isl_basic_map_finalize(bmap);
12415 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12417 return map;
12418 error:
12419 isl_basic_map_free(bmap);
12420 isl_map_free(map);
12421 return NULL;
12424 /* Construct a constraint imposing that the value of the first dimension is
12425 * greater than or equal to that of the second.
12427 static __isl_give isl_constraint *constraint_order_ge(
12428 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12429 enum isl_dim_type type2, int pos2)
12431 isl_constraint *c;
12433 if (!space)
12434 return NULL;
12436 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12438 if (pos1 >= isl_constraint_dim(c, type1))
12439 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12440 "index out of bounds", return isl_constraint_free(c));
12441 if (pos2 >= isl_constraint_dim(c, type2))
12442 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12443 "index out of bounds", return isl_constraint_free(c));
12445 if (type1 == type2 && pos1 == pos2)
12446 return c;
12448 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12449 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12451 return c;
12454 /* Add a constraint imposing that the value of the first dimension is
12455 * greater than or equal to that of the second.
12457 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12458 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12460 isl_constraint *c;
12461 isl_space *space;
12463 if (type1 == type2 && pos1 == pos2)
12464 return bmap;
12465 space = isl_basic_map_get_space(bmap);
12466 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12467 bmap = isl_basic_map_add_constraint(bmap, c);
12469 return bmap;
12472 /* Add a constraint imposing that the value of the first dimension is
12473 * greater than or equal to that of the second.
12475 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12476 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12478 isl_constraint *c;
12479 isl_space *space;
12481 if (type1 == type2 && pos1 == pos2)
12482 return map;
12483 space = isl_map_get_space(map);
12484 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12485 map = isl_map_add_constraint(map, c);
12487 return map;
12490 /* Add a constraint imposing that the value of the first dimension is
12491 * less than or equal to that of the second.
12493 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12494 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12496 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12499 /* Construct a basic map where the value of the first dimension is
12500 * greater than that of the second.
12502 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12503 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12505 isl_basic_map *bmap = NULL;
12506 int i;
12508 if (!space)
12509 return NULL;
12511 if (pos1 >= isl_space_dim(space, type1))
12512 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12513 "index out of bounds", goto error);
12514 if (pos2 >= isl_space_dim(space, type2))
12515 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12516 "index out of bounds", goto error);
12518 if (type1 == type2 && pos1 == pos2)
12519 return isl_basic_map_empty(space);
12521 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12522 i = isl_basic_map_alloc_inequality(bmap);
12523 if (i < 0)
12524 return isl_basic_map_free(bmap);
12525 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12526 pos1 += isl_basic_map_offset(bmap, type1);
12527 pos2 += isl_basic_map_offset(bmap, type2);
12528 isl_int_set_si(bmap->ineq[i][pos1], 1);
12529 isl_int_set_si(bmap->ineq[i][pos2], -1);
12530 isl_int_set_si(bmap->ineq[i][0], -1);
12531 bmap = isl_basic_map_finalize(bmap);
12533 return bmap;
12534 error:
12535 isl_space_free(space);
12536 isl_basic_map_free(bmap);
12537 return NULL;
12540 /* Add a constraint imposing that the value of the first dimension is
12541 * greater than that of the second.
12543 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12544 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12546 isl_basic_map *gt;
12548 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12550 bmap = isl_basic_map_intersect(bmap, gt);
12552 return bmap;
12555 /* Add a constraint imposing that the value of the first dimension is
12556 * greater than that of the second.
12558 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12559 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12561 isl_basic_map *bmap;
12563 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12565 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12567 return map;
12570 /* Add a constraint imposing that the value of the first dimension is
12571 * smaller than that of the second.
12573 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12574 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12576 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12579 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12580 int pos)
12582 isl_aff *div;
12583 isl_local_space *ls;
12585 if (!bmap)
12586 return NULL;
12588 if (!isl_basic_map_divs_known(bmap))
12589 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12590 "some divs are unknown", return NULL);
12592 ls = isl_basic_map_get_local_space(bmap);
12593 div = isl_local_space_get_div(ls, pos);
12594 isl_local_space_free(ls);
12596 return div;
12599 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12600 int pos)
12602 return isl_basic_map_get_div(bset, pos);
12605 /* Plug in "subs" for dimension "type", "pos" of "bset".
12607 * Let i be the dimension to replace and let "subs" be of the form
12609 * f/d
12611 * Any integer division with a non-zero coefficient for i,
12613 * floor((a i + g)/m)
12615 * is replaced by
12617 * floor((a f + d g)/(m d))
12619 * Constraints of the form
12621 * a i + g
12623 * are replaced by
12625 * a f + d g
12627 * We currently require that "subs" is an integral expression.
12628 * Handling rational expressions may require us to add stride constraints
12629 * as we do in isl_basic_set_preimage_multi_aff.
12631 __isl_give isl_basic_set *isl_basic_set_substitute(
12632 __isl_take isl_basic_set *bset,
12633 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12635 int i;
12636 isl_int v;
12637 isl_ctx *ctx;
12639 if (bset && isl_basic_set_plain_is_empty(bset))
12640 return bset;
12642 bset = isl_basic_set_cow(bset);
12643 if (!bset || !subs)
12644 goto error;
12646 ctx = isl_basic_set_get_ctx(bset);
12647 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12648 isl_die(ctx, isl_error_invalid,
12649 "spaces don't match", goto error);
12650 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12651 isl_die(ctx, isl_error_unsupported,
12652 "cannot handle divs yet", goto error);
12653 if (!isl_int_is_one(subs->v->el[0]))
12654 isl_die(ctx, isl_error_invalid,
12655 "can only substitute integer expressions", goto error);
12657 pos += isl_basic_set_offset(bset, type);
12659 isl_int_init(v);
12661 for (i = 0; i < bset->n_eq; ++i) {
12662 if (isl_int_is_zero(bset->eq[i][pos]))
12663 continue;
12664 isl_int_set(v, bset->eq[i][pos]);
12665 isl_int_set_si(bset->eq[i][pos], 0);
12666 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12667 v, subs->v->el + 1, subs->v->size - 1);
12670 for (i = 0; i < bset->n_ineq; ++i) {
12671 if (isl_int_is_zero(bset->ineq[i][pos]))
12672 continue;
12673 isl_int_set(v, bset->ineq[i][pos]);
12674 isl_int_set_si(bset->ineq[i][pos], 0);
12675 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12676 v, subs->v->el + 1, subs->v->size - 1);
12679 for (i = 0; i < bset->n_div; ++i) {
12680 if (isl_int_is_zero(bset->div[i][1 + pos]))
12681 continue;
12682 isl_int_set(v, bset->div[i][1 + pos]);
12683 isl_int_set_si(bset->div[i][1 + pos], 0);
12684 isl_seq_combine(bset->div[i] + 1,
12685 subs->v->el[0], bset->div[i] + 1,
12686 v, subs->v->el + 1, subs->v->size - 1);
12687 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12690 isl_int_clear(v);
12692 bset = isl_basic_set_simplify(bset);
12693 return isl_basic_set_finalize(bset);
12694 error:
12695 isl_basic_set_free(bset);
12696 return NULL;
12699 /* Plug in "subs" for dimension "type", "pos" of "set".
12701 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12702 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12704 int i;
12706 if (set && isl_set_plain_is_empty(set))
12707 return set;
12709 set = isl_set_cow(set);
12710 if (!set || !subs)
12711 goto error;
12713 for (i = set->n - 1; i >= 0; --i) {
12714 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12715 if (remove_if_empty(set, i) < 0)
12716 goto error;
12719 return set;
12720 error:
12721 isl_set_free(set);
12722 return NULL;
12725 /* Check if the range of "ma" is compatible with the domain or range
12726 * (depending on "type") of "bmap".
12728 static isl_stat check_basic_map_compatible_range_multi_aff(
12729 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12730 __isl_keep isl_multi_aff *ma)
12732 isl_bool m;
12733 isl_space *ma_space;
12735 ma_space = isl_multi_aff_get_space(ma);
12737 m = isl_space_has_equal_params(bmap->dim, ma_space);
12738 if (m < 0)
12739 goto error;
12740 if (!m)
12741 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12742 "parameters don't match", goto error);
12743 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12744 if (m < 0)
12745 goto error;
12746 if (!m)
12747 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12748 "spaces don't match", goto error);
12750 isl_space_free(ma_space);
12751 return isl_stat_ok;
12752 error:
12753 isl_space_free(ma_space);
12754 return isl_stat_error;
12757 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12758 * coefficients before the transformed range of dimensions,
12759 * the "n_after" coefficients after the transformed range of dimensions
12760 * and the coefficients of the other divs in "bmap".
12762 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12763 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12765 int i;
12766 int n_param;
12767 int n_set;
12768 isl_local_space *ls;
12770 if (n_div == 0)
12771 return 0;
12773 ls = isl_aff_get_domain_local_space(ma->p[0]);
12774 if (!ls)
12775 return -1;
12777 n_param = isl_local_space_dim(ls, isl_dim_param);
12778 n_set = isl_local_space_dim(ls, isl_dim_set);
12779 for (i = 0; i < n_div; ++i) {
12780 int o_bmap = 0, o_ls = 0;
12782 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12783 o_bmap += 1 + 1 + n_param;
12784 o_ls += 1 + 1 + n_param;
12785 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12786 o_bmap += n_before;
12787 isl_seq_cpy(bmap->div[i] + o_bmap,
12788 ls->div->row[i] + o_ls, n_set);
12789 o_bmap += n_set;
12790 o_ls += n_set;
12791 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12792 o_bmap += n_after;
12793 isl_seq_cpy(bmap->div[i] + o_bmap,
12794 ls->div->row[i] + o_ls, n_div);
12795 o_bmap += n_div;
12796 o_ls += n_div;
12797 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12798 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12799 goto error;
12802 isl_local_space_free(ls);
12803 return 0;
12804 error:
12805 isl_local_space_free(ls);
12806 return -1;
12809 /* How many stride constraints does "ma" enforce?
12810 * That is, how many of the affine expressions have a denominator
12811 * different from one?
12813 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12815 int i;
12816 int strides = 0;
12818 for (i = 0; i < ma->n; ++i)
12819 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12820 strides++;
12822 return strides;
12825 /* For each affine expression in ma of the form
12827 * x_i = (f_i y + h_i)/m_i
12829 * with m_i different from one, add a constraint to "bmap"
12830 * of the form
12832 * f_i y + h_i = m_i alpha_i
12834 * with alpha_i an additional existentially quantified variable.
12836 * The input variables of "ma" correspond to a subset of the variables
12837 * of "bmap". There are "n_before" variables in "bmap" before this
12838 * subset and "n_after" variables after this subset.
12839 * The integer divisions of the affine expressions in "ma" are assumed
12840 * to have been aligned. There are "n_div_ma" of them and
12841 * they appear first in "bmap", straight after the "n_after" variables.
12843 static __isl_give isl_basic_map *add_ma_strides(
12844 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12845 int n_before, int n_after, int n_div_ma)
12847 int i, k;
12848 int div;
12849 int total;
12850 int n_param;
12851 int n_in;
12853 total = isl_basic_map_total_dim(bmap);
12854 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12855 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12856 for (i = 0; i < ma->n; ++i) {
12857 int o_bmap = 0, o_ma = 1;
12859 if (isl_int_is_one(ma->p[i]->v->el[0]))
12860 continue;
12861 div = isl_basic_map_alloc_div(bmap);
12862 k = isl_basic_map_alloc_equality(bmap);
12863 if (div < 0 || k < 0)
12864 goto error;
12865 isl_int_set_si(bmap->div[div][0], 0);
12866 isl_seq_cpy(bmap->eq[k] + o_bmap,
12867 ma->p[i]->v->el + o_ma, 1 + n_param);
12868 o_bmap += 1 + n_param;
12869 o_ma += 1 + n_param;
12870 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12871 o_bmap += n_before;
12872 isl_seq_cpy(bmap->eq[k] + o_bmap,
12873 ma->p[i]->v->el + o_ma, n_in);
12874 o_bmap += n_in;
12875 o_ma += n_in;
12876 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12877 o_bmap += n_after;
12878 isl_seq_cpy(bmap->eq[k] + o_bmap,
12879 ma->p[i]->v->el + o_ma, n_div_ma);
12880 o_bmap += n_div_ma;
12881 o_ma += n_div_ma;
12882 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12883 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12884 total++;
12887 return bmap;
12888 error:
12889 isl_basic_map_free(bmap);
12890 return NULL;
12893 /* Replace the domain or range space (depending on "type) of "space" by "set".
12895 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12896 enum isl_dim_type type, __isl_take isl_space *set)
12898 if (type == isl_dim_in) {
12899 space = isl_space_range(space);
12900 space = isl_space_map_from_domain_and_range(set, space);
12901 } else {
12902 space = isl_space_domain(space);
12903 space = isl_space_map_from_domain_and_range(space, set);
12906 return space;
12909 /* Compute the preimage of the domain or range (depending on "type")
12910 * of "bmap" under the function represented by "ma".
12911 * In other words, plug in "ma" in the domain or range of "bmap".
12912 * The result is a basic map that lives in the same space as "bmap"
12913 * except that the domain or range has been replaced by
12914 * the domain space of "ma".
12916 * If bmap is represented by
12918 * A(p) + S u + B x + T v + C(divs) >= 0,
12920 * where u and x are input and output dimensions if type == isl_dim_out
12921 * while x and v are input and output dimensions if type == isl_dim_in,
12922 * and ma is represented by
12924 * x = D(p) + F(y) + G(divs')
12926 * then the result is
12928 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12930 * The divs in the input set are similarly adjusted.
12931 * In particular
12933 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12935 * becomes
12937 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12938 * B_i G(divs') + c_i(divs))/n_i)
12940 * If bmap is not a rational map and if F(y) involves any denominators
12942 * x_i = (f_i y + h_i)/m_i
12944 * then additional constraints are added to ensure that we only
12945 * map back integer points. That is we enforce
12947 * f_i y + h_i = m_i alpha_i
12949 * with alpha_i an additional existentially quantified variable.
12951 * We first copy over the divs from "ma".
12952 * Then we add the modified constraints and divs from "bmap".
12953 * Finally, we add the stride constraints, if needed.
12955 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12956 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12957 __isl_take isl_multi_aff *ma)
12959 int i, k;
12960 isl_space *space;
12961 isl_basic_map *res = NULL;
12962 int n_before, n_after, n_div_bmap, n_div_ma;
12963 isl_int f, c1, c2, g;
12964 isl_bool rational;
12965 int strides;
12967 isl_int_init(f);
12968 isl_int_init(c1);
12969 isl_int_init(c2);
12970 isl_int_init(g);
12972 ma = isl_multi_aff_align_divs(ma);
12973 if (!bmap || !ma)
12974 goto error;
12975 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12976 goto error;
12978 if (type == isl_dim_in) {
12979 n_before = 0;
12980 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12981 } else {
12982 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12983 n_after = 0;
12985 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12986 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12988 space = isl_multi_aff_get_domain_space(ma);
12989 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12990 rational = isl_basic_map_is_rational(bmap);
12991 strides = rational ? 0 : multi_aff_strides(ma);
12992 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12993 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12994 if (rational)
12995 res = isl_basic_map_set_rational(res);
12997 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12998 if (isl_basic_map_alloc_div(res) < 0)
12999 goto error;
13001 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
13002 goto error;
13004 for (i = 0; i < bmap->n_eq; ++i) {
13005 k = isl_basic_map_alloc_equality(res);
13006 if (k < 0)
13007 goto error;
13008 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13009 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13012 for (i = 0; i < bmap->n_ineq; ++i) {
13013 k = isl_basic_map_alloc_inequality(res);
13014 if (k < 0)
13015 goto error;
13016 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13017 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13020 for (i = 0; i < bmap->n_div; ++i) {
13021 if (isl_int_is_zero(bmap->div[i][0])) {
13022 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13023 continue;
13025 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13026 n_before, n_after, n_div_ma, n_div_bmap,
13027 f, c1, c2, g, 1);
13030 if (strides)
13031 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13033 isl_int_clear(f);
13034 isl_int_clear(c1);
13035 isl_int_clear(c2);
13036 isl_int_clear(g);
13037 isl_basic_map_free(bmap);
13038 isl_multi_aff_free(ma);
13039 res = isl_basic_map_simplify(res);
13040 return isl_basic_map_finalize(res);
13041 error:
13042 isl_int_clear(f);
13043 isl_int_clear(c1);
13044 isl_int_clear(c2);
13045 isl_int_clear(g);
13046 isl_basic_map_free(bmap);
13047 isl_multi_aff_free(ma);
13048 isl_basic_map_free(res);
13049 return NULL;
13052 /* Compute the preimage of "bset" under the function represented by "ma".
13053 * In other words, plug in "ma" in "bset". The result is a basic set
13054 * that lives in the domain space of "ma".
13056 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13057 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13059 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13062 /* Compute the preimage of the domain of "bmap" under the function
13063 * represented by "ma".
13064 * In other words, plug in "ma" in the domain of "bmap".
13065 * The result is a basic map that lives in the same space as "bmap"
13066 * except that the domain has been replaced by the domain space of "ma".
13068 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13069 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13071 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13074 /* Compute the preimage of the range of "bmap" under the function
13075 * represented by "ma".
13076 * In other words, plug in "ma" in the range of "bmap".
13077 * The result is a basic map that lives in the same space as "bmap"
13078 * except that the range has been replaced by the domain space of "ma".
13080 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13081 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13083 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13086 /* Check if the range of "ma" is compatible with the domain or range
13087 * (depending on "type") of "map".
13088 * Return isl_stat_error if anything is wrong.
13090 static isl_stat check_map_compatible_range_multi_aff(
13091 __isl_keep isl_map *map, enum isl_dim_type type,
13092 __isl_keep isl_multi_aff *ma)
13094 isl_bool m;
13095 isl_space *ma_space;
13097 ma_space = isl_multi_aff_get_space(ma);
13098 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13099 isl_space_free(ma_space);
13100 if (m < 0)
13101 return isl_stat_error;
13102 if (!m)
13103 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13104 "spaces don't match", return isl_stat_error);
13105 return isl_stat_ok;
13108 /* Compute the preimage of the domain or range (depending on "type")
13109 * of "map" under the function represented by "ma".
13110 * In other words, plug in "ma" in the domain or range of "map".
13111 * The result is a map that lives in the same space as "map"
13112 * except that the domain or range has been replaced by
13113 * the domain space of "ma".
13115 * The parameters are assumed to have been aligned.
13117 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13118 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13120 int i;
13121 isl_space *space;
13123 map = isl_map_cow(map);
13124 ma = isl_multi_aff_align_divs(ma);
13125 if (!map || !ma)
13126 goto error;
13127 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13128 goto error;
13130 for (i = 0; i < map->n; ++i) {
13131 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13132 isl_multi_aff_copy(ma));
13133 if (!map->p[i])
13134 goto error;
13137 space = isl_multi_aff_get_domain_space(ma);
13138 space = isl_space_set(isl_map_get_space(map), type, space);
13140 isl_space_free(map->dim);
13141 map->dim = space;
13142 if (!map->dim)
13143 goto error;
13145 isl_multi_aff_free(ma);
13146 if (map->n > 1)
13147 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13148 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13149 return map;
13150 error:
13151 isl_multi_aff_free(ma);
13152 isl_map_free(map);
13153 return NULL;
13156 /* Compute the preimage of the domain or range (depending on "type")
13157 * of "map" under the function represented by "ma".
13158 * In other words, plug in "ma" in the domain or range of "map".
13159 * The result is a map that lives in the same space as "map"
13160 * except that the domain or range has been replaced by
13161 * the domain space of "ma".
13163 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13164 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13166 isl_bool aligned;
13168 if (!map || !ma)
13169 goto error;
13171 aligned = isl_map_space_has_equal_params(map, ma->space);
13172 if (aligned < 0)
13173 goto error;
13174 if (aligned)
13175 return map_preimage_multi_aff(map, type, ma);
13177 if (isl_map_check_named_params(map) < 0)
13178 goto error;
13179 if (!isl_space_has_named_params(ma->space))
13180 isl_die(map->ctx, isl_error_invalid,
13181 "unaligned unnamed parameters", goto error);
13182 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13183 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13185 return map_preimage_multi_aff(map, type, ma);
13186 error:
13187 isl_multi_aff_free(ma);
13188 return isl_map_free(map);
13191 /* Compute the preimage of "set" under the function represented by "ma".
13192 * In other words, plug in "ma" in "set". The result is a set
13193 * that lives in the domain space of "ma".
13195 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13196 __isl_take isl_multi_aff *ma)
13198 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13201 /* Compute the preimage of the domain of "map" under the function
13202 * represented by "ma".
13203 * In other words, plug in "ma" in the domain of "map".
13204 * The result is a map that lives in the same space as "map"
13205 * except that the domain has been replaced by the domain space of "ma".
13207 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13208 __isl_take isl_multi_aff *ma)
13210 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13213 /* Compute the preimage of the range of "map" under the function
13214 * represented by "ma".
13215 * In other words, plug in "ma" in the range of "map".
13216 * The result is a map that lives in the same space as "map"
13217 * except that the range has been replaced by the domain space of "ma".
13219 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13220 __isl_take isl_multi_aff *ma)
13222 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13225 /* Compute the preimage of "map" under the function represented by "pma".
13226 * In other words, plug in "pma" in the domain or range of "map".
13227 * The result is a map that lives in the same space as "map",
13228 * except that the space of type "type" has been replaced by
13229 * the domain space of "pma".
13231 * The parameters of "map" and "pma" are assumed to have been aligned.
13233 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13234 __isl_take isl_map *map, enum isl_dim_type type,
13235 __isl_take isl_pw_multi_aff *pma)
13237 int i;
13238 isl_map *res;
13240 if (!pma)
13241 goto error;
13243 if (pma->n == 0) {
13244 isl_pw_multi_aff_free(pma);
13245 res = isl_map_empty(isl_map_get_space(map));
13246 isl_map_free(map);
13247 return res;
13250 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13251 isl_multi_aff_copy(pma->p[0].maff));
13252 if (type == isl_dim_in)
13253 res = isl_map_intersect_domain(res,
13254 isl_map_copy(pma->p[0].set));
13255 else
13256 res = isl_map_intersect_range(res,
13257 isl_map_copy(pma->p[0].set));
13259 for (i = 1; i < pma->n; ++i) {
13260 isl_map *res_i;
13262 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13263 isl_multi_aff_copy(pma->p[i].maff));
13264 if (type == isl_dim_in)
13265 res_i = isl_map_intersect_domain(res_i,
13266 isl_map_copy(pma->p[i].set));
13267 else
13268 res_i = isl_map_intersect_range(res_i,
13269 isl_map_copy(pma->p[i].set));
13270 res = isl_map_union(res, res_i);
13273 isl_pw_multi_aff_free(pma);
13274 isl_map_free(map);
13275 return res;
13276 error:
13277 isl_pw_multi_aff_free(pma);
13278 isl_map_free(map);
13279 return NULL;
13282 /* Compute the preimage of "map" under the function represented by "pma".
13283 * In other words, plug in "pma" in the domain or range of "map".
13284 * The result is a map that lives in the same space as "map",
13285 * except that the space of type "type" has been replaced by
13286 * the domain space of "pma".
13288 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13289 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13291 isl_bool aligned;
13293 if (!map || !pma)
13294 goto error;
13296 aligned = isl_map_space_has_equal_params(map, pma->dim);
13297 if (aligned < 0)
13298 goto error;
13299 if (aligned)
13300 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13302 if (isl_map_check_named_params(map) < 0)
13303 goto error;
13304 if (!isl_space_has_named_params(pma->dim))
13305 isl_die(map->ctx, isl_error_invalid,
13306 "unaligned unnamed parameters", goto error);
13307 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13308 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13310 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13311 error:
13312 isl_pw_multi_aff_free(pma);
13313 return isl_map_free(map);
13316 /* Compute the preimage of "set" under the function represented by "pma".
13317 * In other words, plug in "pma" in "set". The result is a set
13318 * that lives in the domain space of "pma".
13320 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13321 __isl_take isl_pw_multi_aff *pma)
13323 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13326 /* Compute the preimage of the domain of "map" under the function
13327 * represented by "pma".
13328 * In other words, plug in "pma" in the domain of "map".
13329 * The result is a map that lives in the same space as "map",
13330 * except that domain space has been replaced by the domain space of "pma".
13332 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13333 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13335 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13338 /* Compute the preimage of the range of "map" under the function
13339 * represented by "pma".
13340 * In other words, plug in "pma" in the range of "map".
13341 * The result is a map that lives in the same space as "map",
13342 * except that range space has been replaced by the domain space of "pma".
13344 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13345 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13347 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13350 /* Compute the preimage of "map" under the function represented by "mpa".
13351 * In other words, plug in "mpa" in the domain or range of "map".
13352 * The result is a map that lives in the same space as "map",
13353 * except that the space of type "type" has been replaced by
13354 * the domain space of "mpa".
13356 * If the map does not involve any constraints that refer to the
13357 * dimensions of the substituted space, then the only possible
13358 * effect of "mpa" on the map is to map the space to a different space.
13359 * We create a separate isl_multi_aff to effectuate this change
13360 * in order to avoid spurious splitting of the map along the pieces
13361 * of "mpa".
13363 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13364 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13366 int n;
13367 isl_pw_multi_aff *pma;
13369 if (!map || !mpa)
13370 goto error;
13372 n = isl_map_dim(map, type);
13373 if (!isl_map_involves_dims(map, type, 0, n)) {
13374 isl_space *space;
13375 isl_multi_aff *ma;
13377 space = isl_multi_pw_aff_get_space(mpa);
13378 isl_multi_pw_aff_free(mpa);
13379 ma = isl_multi_aff_zero(space);
13380 return isl_map_preimage_multi_aff(map, type, ma);
13383 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13384 return isl_map_preimage_pw_multi_aff(map, type, pma);
13385 error:
13386 isl_map_free(map);
13387 isl_multi_pw_aff_free(mpa);
13388 return NULL;
13391 /* Compute the preimage of "map" under the function represented by "mpa".
13392 * In other words, plug in "mpa" in the domain "map".
13393 * The result is a map that lives in the same space as "map",
13394 * except that domain space has been replaced by the domain space of "mpa".
13396 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13397 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13399 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13402 /* Compute the preimage of "set" by the function represented by "mpa".
13403 * In other words, plug in "mpa" in "set".
13405 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13406 __isl_take isl_multi_pw_aff *mpa)
13408 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13411 /* Return a copy of the equality constraints of "bset" as a matrix.
13413 __isl_give isl_mat *isl_basic_set_extract_equalities(
13414 __isl_keep isl_basic_set *bset)
13416 isl_ctx *ctx;
13417 unsigned total;
13419 if (!bset)
13420 return NULL;
13422 ctx = isl_basic_set_get_ctx(bset);
13423 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13424 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13427 /* Are the "n" "coefficients" starting at "first" of the integer division
13428 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13429 * to each other?
13430 * The "coefficient" at position 0 is the denominator.
13431 * The "coefficient" at position 1 is the constant term.
13433 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13434 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13435 unsigned first, unsigned n)
13437 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13438 return isl_bool_error;
13439 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13440 return isl_bool_error;
13441 return isl_seq_eq(bmap1->div[pos1] + first,
13442 bmap2->div[pos2] + first, n);
13445 /* Are the integer division expressions at position "pos1" in "bmap1" and
13446 * "pos2" in "bmap2" equal to each other, except that the constant terms
13447 * are different?
13449 isl_bool isl_basic_map_equal_div_expr_except_constant(
13450 __isl_keep isl_basic_map *bmap1, int pos1,
13451 __isl_keep isl_basic_map *bmap2, int pos2)
13453 isl_bool equal;
13454 unsigned total;
13456 if (!bmap1 || !bmap2)
13457 return isl_bool_error;
13458 total = isl_basic_map_total_dim(bmap1);
13459 if (total != isl_basic_map_total_dim(bmap2))
13460 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13461 "incomparable div expressions", return isl_bool_error);
13462 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13463 0, 1);
13464 if (equal < 0 || !equal)
13465 return equal;
13466 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13467 1, 1);
13468 if (equal < 0 || equal)
13469 return isl_bool_not(equal);
13470 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13471 2, total);
13474 /* Replace the numerator of the constant term of the integer division
13475 * expression at position "div" in "bmap" by "value".
13476 * The caller guarantees that this does not change the meaning
13477 * of the input.
13479 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13480 __isl_take isl_basic_map *bmap, int div, int value)
13482 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13483 return isl_basic_map_free(bmap);
13485 isl_int_set_si(bmap->div[div][1], value);
13487 return bmap;
13490 /* Is the point "inner" internal to inequality constraint "ineq"
13491 * of "bset"?
13492 * The point is considered to be internal to the inequality constraint,
13493 * if it strictly lies on the positive side of the inequality constraint,
13494 * or if it lies on the constraint and the constraint is lexico-positive.
13496 static isl_bool is_internal(__isl_keep isl_vec *inner,
13497 __isl_keep isl_basic_set *bset, int ineq)
13499 isl_ctx *ctx;
13500 int pos;
13501 unsigned total;
13503 if (!inner || !bset)
13504 return isl_bool_error;
13506 ctx = isl_basic_set_get_ctx(bset);
13507 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13508 &ctx->normalize_gcd);
13509 if (!isl_int_is_zero(ctx->normalize_gcd))
13510 return isl_int_is_nonneg(ctx->normalize_gcd);
13512 total = isl_basic_set_dim(bset, isl_dim_all);
13513 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13514 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13517 /* Tighten the inequality constraints of "bset" that are outward with respect
13518 * to the point "vec".
13519 * That is, tighten the constraints that are not satisfied by "vec".
13521 * "vec" is a point internal to some superset S of "bset" that is used
13522 * to make the subsets of S disjoint, by tightening one half of the constraints
13523 * that separate two subsets. In particular, the constraints of S
13524 * are all satisfied by "vec" and should not be tightened.
13525 * Of the internal constraints, those that have "vec" on the outside
13526 * are tightened. The shared facet is included in the adjacent subset
13527 * with the opposite constraint.
13528 * For constraints that saturate "vec", this criterion cannot be used
13529 * to determine which of the two sides should be tightened.
13530 * Instead, the sign of the first non-zero coefficient is used
13531 * to make this choice. Note that this second criterion is never used
13532 * on the constraints of S since "vec" is interior to "S".
13534 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13535 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13537 int j;
13539 bset = isl_basic_set_cow(bset);
13540 if (!bset)
13541 return NULL;
13542 for (j = 0; j < bset->n_ineq; ++j) {
13543 isl_bool internal;
13545 internal = is_internal(vec, bset, j);
13546 if (internal < 0)
13547 return isl_basic_set_free(bset);
13548 if (internal)
13549 continue;
13550 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13553 return bset;
13556 /* Replace the variables x of type "type" starting at "first" in "bmap"
13557 * by x' with x = M x' with M the matrix trans.
13558 * That is, replace the corresponding coefficients c by c M.
13560 * The transformation matrix should be a square matrix.
13562 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13563 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13564 __isl_take isl_mat *trans)
13566 unsigned pos;
13568 bmap = isl_basic_map_cow(bmap);
13569 if (!bmap || !trans)
13570 goto error;
13572 if (trans->n_row != trans->n_col)
13573 isl_die(trans->ctx, isl_error_invalid,
13574 "expecting square transformation matrix", goto error);
13575 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
13576 isl_die(trans->ctx, isl_error_invalid,
13577 "oversized transformation matrix", goto error);
13579 pos = isl_basic_map_offset(bmap, type) + first;
13581 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13582 isl_mat_copy(trans)) < 0)
13583 goto error;
13584 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13585 isl_mat_copy(trans)) < 0)
13586 goto error;
13587 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13588 isl_mat_copy(trans)) < 0)
13589 goto error;
13591 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
13592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13594 isl_mat_free(trans);
13595 return bmap;
13596 error:
13597 isl_mat_free(trans);
13598 isl_basic_map_free(bmap);
13599 return NULL;
13602 /* Replace the variables x of type "type" starting at "first" in "bset"
13603 * by x' with x = M x' with M the matrix trans.
13604 * That is, replace the corresponding coefficients c by c M.
13606 * The transformation matrix should be a square matrix.
13608 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13609 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13610 __isl_take isl_mat *trans)
13612 return isl_basic_map_transform_dims(bset, type, first, trans);