isl_basic_map_get_space: extract out isl_basic_map_peek_space
[isl.git] / isl_map.c
blob9d9e5de8dcbfd5a08cee9a4d98afe0a67de7f0e9
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 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
93 return map ? n(map->dim, type) : 0;
96 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
98 return set ? n(set->dim, type) : 0;
101 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
102 enum isl_dim_type type)
104 isl_space *space;
106 if (!bmap)
107 return 0;
109 space = bmap->dim;
110 switch (type) {
111 case isl_dim_cst: return 0;
112 case isl_dim_param: return 1;
113 case isl_dim_in: return 1 + space->nparam;
114 case isl_dim_out: return 1 + space->nparam + space->n_in;
115 case isl_dim_div: return 1 + space->nparam + space->n_in +
116 space->n_out;
117 default: return 0;
121 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
122 enum isl_dim_type type)
124 return isl_basic_map_offset(bset, type);
127 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
129 return pos(map->dim, type);
132 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
133 enum isl_dim_type type)
135 return isl_basic_map_dim(bset, type);
138 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
140 return isl_basic_set_dim(bset, isl_dim_set);
143 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
145 return isl_basic_set_dim(bset, isl_dim_param);
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
150 if (!bset)
151 return 0;
152 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
155 unsigned isl_set_n_dim(__isl_keep isl_set *set)
157 return isl_set_dim(set, isl_dim_set);
160 unsigned isl_set_n_param(__isl_keep isl_set *set)
162 return isl_set_dim(set, isl_dim_param);
165 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
167 return bmap ? bmap->dim->n_in : 0;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
172 return bmap ? bmap->dim->n_out : 0;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
177 return bmap ? bmap->dim->nparam : 0;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
182 return bmap ? bmap->n_div : 0;
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
187 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
190 unsigned isl_map_n_in(const struct isl_map *map)
192 return map ? map->dim->n_in : 0;
195 unsigned isl_map_n_out(const struct isl_map *map)
197 return map ? map->dim->n_out : 0;
200 unsigned isl_map_n_param(const struct isl_map *map)
202 return map ? map->dim->nparam : 0;
205 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
206 __isl_keep isl_set *set)
208 isl_bool m;
209 if (!map || !set)
210 return isl_bool_error;
211 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
212 if (m < 0 || !m)
213 return m;
214 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
215 set->dim, isl_dim_set);
218 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
219 __isl_keep isl_basic_set *bset)
221 isl_bool m;
222 if (!bmap || !bset)
223 return isl_bool_error;
224 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
225 if (m < 0 || !m)
226 return m;
227 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
228 bset->dim, isl_dim_set);
231 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
232 __isl_keep isl_set *set)
234 isl_bool m;
235 if (!map || !set)
236 return isl_bool_error;
237 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
238 if (m < 0 || !m)
239 return m;
240 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
241 set->dim, isl_dim_set);
244 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
245 __isl_keep isl_basic_set *bset)
247 isl_bool m;
248 if (!bmap || !bset)
249 return isl_bool_error;
250 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
251 if (m < 0 || !m)
252 return m;
253 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
254 bset->dim, isl_dim_set);
257 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
259 return bmap ? bmap->ctx : NULL;
262 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
264 return bset ? bset->ctx : NULL;
267 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
269 return map ? map->ctx : NULL;
272 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
274 return set ? set->ctx : NULL;
277 /* Return the space of "bmap".
279 __isl_keep isl_space *isl_basic_map_peek_space(
280 __isl_keep const isl_basic_map *bmap)
282 return bmap ? bmap->dim : NULL;
285 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
287 return isl_space_copy(isl_basic_map_peek_space(bmap));
290 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
292 return isl_basic_map_get_space(bset_to_bmap(bset));
295 /* Extract the divs in "bmap" as a matrix.
297 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
299 int i;
300 isl_ctx *ctx;
301 isl_mat *div;
302 unsigned total;
303 unsigned cols;
305 if (!bmap)
306 return NULL;
308 ctx = isl_basic_map_get_ctx(bmap);
309 total = isl_space_dim(bmap->dim, isl_dim_all);
310 cols = 1 + 1 + total + bmap->n_div;
311 div = isl_mat_alloc(ctx, bmap->n_div, cols);
312 if (!div)
313 return NULL;
315 for (i = 0; i < bmap->n_div; ++i)
316 isl_seq_cpy(div->row[i], bmap->div[i], cols);
318 return div;
321 /* Extract the divs in "bset" as a matrix.
323 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
325 return isl_basic_map_get_divs(bset);
328 __isl_give isl_local_space *isl_basic_map_get_local_space(
329 __isl_keep isl_basic_map *bmap)
331 isl_mat *div;
333 if (!bmap)
334 return NULL;
336 div = isl_basic_map_get_divs(bmap);
337 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
340 __isl_give isl_local_space *isl_basic_set_get_local_space(
341 __isl_keep isl_basic_set *bset)
343 return isl_basic_map_get_local_space(bset);
346 /* For each known div d = floor(f/m), add the constraints
348 * f - m d >= 0
349 * -(f-(m-1)) + m d >= 0
351 * Do not finalize the result.
353 static __isl_give isl_basic_map *add_known_div_constraints(
354 __isl_take isl_basic_map *bmap)
356 int i;
357 unsigned n_div;
359 if (!bmap)
360 return NULL;
361 n_div = isl_basic_map_dim(bmap, isl_dim_div);
362 if (n_div == 0)
363 return bmap;
364 bmap = isl_basic_map_cow(bmap);
365 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
366 if (!bmap)
367 return NULL;
368 for (i = 0; i < n_div; ++i) {
369 if (isl_int_is_zero(bmap->div[i][0]))
370 continue;
371 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
372 return isl_basic_map_free(bmap);
375 return bmap;
378 __isl_give isl_basic_map *isl_basic_map_from_local_space(
379 __isl_take isl_local_space *ls)
381 int i;
382 int n_div;
383 isl_basic_map *bmap;
385 if (!ls)
386 return NULL;
388 n_div = isl_local_space_dim(ls, isl_dim_div);
389 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
390 n_div, 0, 2 * n_div);
392 for (i = 0; i < n_div; ++i)
393 if (isl_basic_map_alloc_div(bmap) < 0)
394 goto error;
396 for (i = 0; i < n_div; ++i)
397 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
398 bmap = add_known_div_constraints(bmap);
400 isl_local_space_free(ls);
401 return bmap;
402 error:
403 isl_local_space_free(ls);
404 isl_basic_map_free(bmap);
405 return NULL;
408 __isl_give isl_basic_set *isl_basic_set_from_local_space(
409 __isl_take isl_local_space *ls)
411 return isl_basic_map_from_local_space(ls);
414 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
416 if (!map)
417 return NULL;
418 return isl_space_copy(map->dim);
421 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
423 if (!set)
424 return NULL;
425 return isl_space_copy(set->dim);
428 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
429 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
431 bmap = isl_basic_map_cow(bmap);
432 if (!bmap)
433 return NULL;
434 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
435 if (!bmap->dim)
436 goto error;
437 bmap = isl_basic_map_finalize(bmap);
438 return bmap;
439 error:
440 isl_basic_map_free(bmap);
441 return NULL;
444 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
445 __isl_take isl_basic_set *bset, const char *s)
447 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
450 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
451 enum isl_dim_type type)
453 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
456 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
457 enum isl_dim_type type, const char *s)
459 int i;
461 map = isl_map_cow(map);
462 if (!map)
463 return NULL;
465 map->dim = isl_space_set_tuple_name(map->dim, type, s);
466 if (!map->dim)
467 goto error;
469 for (i = 0; i < map->n; ++i) {
470 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
471 if (!map->p[i])
472 goto error;
475 return map;
476 error:
477 isl_map_free(map);
478 return NULL;
481 /* Replace the identifier of the tuple of type "type" by "id".
483 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
484 __isl_take isl_basic_map *bmap,
485 enum isl_dim_type type, __isl_take isl_id *id)
487 bmap = isl_basic_map_cow(bmap);
488 if (!bmap)
489 goto error;
490 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
491 if (!bmap->dim)
492 return isl_basic_map_free(bmap);
493 bmap = isl_basic_map_finalize(bmap);
494 return bmap;
495 error:
496 isl_id_free(id);
497 return NULL;
500 /* Replace the identifier of the tuple by "id".
502 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
503 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
505 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
508 /* Does the input or output tuple have a name?
510 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
512 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
515 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
516 enum isl_dim_type type)
518 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
521 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
522 const char *s)
524 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
525 isl_dim_set, s));
528 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
529 enum isl_dim_type type, __isl_take isl_id *id)
531 map = isl_map_cow(map);
532 if (!map)
533 goto error;
535 map->dim = isl_space_set_tuple_id(map->dim, type, id);
537 return isl_map_reset_space(map, isl_space_copy(map->dim));
538 error:
539 isl_id_free(id);
540 return NULL;
543 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
544 __isl_take isl_id *id)
546 return isl_map_set_tuple_id(set, isl_dim_set, id);
549 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
550 enum isl_dim_type type)
552 map = isl_map_cow(map);
553 if (!map)
554 return NULL;
556 map->dim = isl_space_reset_tuple_id(map->dim, type);
558 return isl_map_reset_space(map, isl_space_copy(map->dim));
561 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
563 return isl_map_reset_tuple_id(set, isl_dim_set);
566 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
568 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
571 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
572 enum isl_dim_type type)
574 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
577 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
579 return isl_map_has_tuple_id(set, isl_dim_set);
582 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
584 return isl_map_get_tuple_id(set, isl_dim_set);
587 /* Does the set tuple have a name?
589 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
591 if (!set)
592 return isl_bool_error;
593 return isl_space_has_tuple_name(set->dim, isl_dim_set);
597 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
599 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
602 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
604 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
607 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
608 enum isl_dim_type type, unsigned pos)
610 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
613 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
614 enum isl_dim_type type, unsigned pos)
616 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
619 /* Does the given dimension have a name?
621 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
622 enum isl_dim_type type, unsigned pos)
624 if (!map)
625 return isl_bool_error;
626 return isl_space_has_dim_name(map->dim, type, pos);
629 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
630 enum isl_dim_type type, unsigned pos)
632 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
635 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
636 enum isl_dim_type type, unsigned pos)
638 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
641 /* Does the given dimension have a name?
643 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
644 enum isl_dim_type type, unsigned pos)
646 if (!set)
647 return isl_bool_error;
648 return isl_space_has_dim_name(set->dim, type, pos);
651 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
652 __isl_take isl_basic_map *bmap,
653 enum isl_dim_type type, unsigned pos, const char *s)
655 bmap = isl_basic_map_cow(bmap);
656 if (!bmap)
657 return NULL;
658 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
659 if (!bmap->dim)
660 goto error;
661 return isl_basic_map_finalize(bmap);
662 error:
663 isl_basic_map_free(bmap);
664 return NULL;
667 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
668 enum isl_dim_type type, unsigned pos, const char *s)
670 int i;
672 map = isl_map_cow(map);
673 if (!map)
674 return NULL;
676 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
677 if (!map->dim)
678 goto error;
680 for (i = 0; i < map->n; ++i) {
681 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
682 if (!map->p[i])
683 goto error;
686 return map;
687 error:
688 isl_map_free(map);
689 return NULL;
692 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
693 __isl_take isl_basic_set *bset,
694 enum isl_dim_type type, unsigned pos, const char *s)
696 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
697 type, pos, s));
700 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
701 enum isl_dim_type type, unsigned pos, const char *s)
703 return set_from_map(isl_map_set_dim_name(set_to_map(set),
704 type, pos, s));
707 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
708 enum isl_dim_type type, unsigned pos)
710 if (!bmap)
711 return isl_bool_error;
712 return isl_space_has_dim_id(bmap->dim, type, pos);
715 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
716 enum isl_dim_type type, unsigned pos)
718 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
721 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
722 enum isl_dim_type type, unsigned pos)
724 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
727 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
728 enum isl_dim_type type, unsigned pos)
730 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
733 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
734 enum isl_dim_type type, unsigned pos)
736 return isl_map_has_dim_id(set, type, pos);
739 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
740 enum isl_dim_type type, unsigned pos)
742 return isl_map_get_dim_id(set, type, pos);
745 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
746 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
748 map = isl_map_cow(map);
749 if (!map)
750 goto error;
752 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
754 return isl_map_reset_space(map, isl_space_copy(map->dim));
755 error:
756 isl_id_free(id);
757 return NULL;
760 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
761 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
763 return isl_map_set_dim_id(set, type, pos, id);
766 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
767 __isl_keep isl_id *id)
769 if (!map)
770 return -1;
771 return isl_space_find_dim_by_id(map->dim, type, id);
774 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
775 __isl_keep isl_id *id)
777 return isl_map_find_dim_by_id(set, type, id);
780 /* Return the position of the dimension of the given type and name
781 * in "bmap".
782 * Return -1 if no such dimension can be found.
784 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
785 enum isl_dim_type type, const char *name)
787 if (!bmap)
788 return -1;
789 return isl_space_find_dim_by_name(bmap->dim, type, name);
792 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
793 const char *name)
795 if (!map)
796 return -1;
797 return isl_space_find_dim_by_name(map->dim, type, name);
800 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
801 const char *name)
803 return isl_map_find_dim_by_name(set, type, name);
806 /* Reset the user pointer on all identifiers of parameters and tuples
807 * of the space of "map".
809 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
811 isl_space *space;
813 space = isl_map_get_space(map);
814 space = isl_space_reset_user(space);
815 map = isl_map_reset_space(map, space);
817 return map;
820 /* Reset the user pointer on all identifiers of parameters and tuples
821 * of the space of "set".
823 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
825 return isl_map_reset_user(set);
828 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
830 if (!bmap)
831 return isl_bool_error;
832 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
835 /* Has "map" been marked as a rational map?
836 * In particular, have all basic maps in "map" been marked this way?
837 * An empty map is not considered to be rational.
838 * Maps where only some of the basic maps are marked rational
839 * are not allowed.
841 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
843 int i;
844 isl_bool rational;
846 if (!map)
847 return isl_bool_error;
848 if (map->n == 0)
849 return isl_bool_false;
850 rational = isl_basic_map_is_rational(map->p[0]);
851 if (rational < 0)
852 return rational;
853 for (i = 1; i < map->n; ++i) {
854 isl_bool rational_i;
856 rational_i = isl_basic_map_is_rational(map->p[i]);
857 if (rational_i < 0)
858 return rational_i;
859 if (rational != rational_i)
860 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
861 "mixed rational and integer basic maps "
862 "not supported", return isl_bool_error);
865 return rational;
868 /* Has "set" been marked as a rational set?
869 * In particular, have all basic set in "set" been marked this way?
870 * An empty set is not considered to be rational.
871 * Sets where only some of the basic sets are marked rational
872 * are not allowed.
874 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
876 return isl_map_is_rational(set);
879 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
881 return isl_basic_map_is_rational(bset);
884 /* Does "bmap" contain any rational points?
886 * If "bmap" has an equality for each dimension, equating the dimension
887 * to an integer constant, then it has no rational points, even if it
888 * is marked as rational.
890 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
892 isl_bool has_rational = isl_bool_true;
893 unsigned total;
895 if (!bmap)
896 return isl_bool_error;
897 if (isl_basic_map_plain_is_empty(bmap))
898 return isl_bool_false;
899 if (!isl_basic_map_is_rational(bmap))
900 return isl_bool_false;
901 bmap = isl_basic_map_copy(bmap);
902 bmap = isl_basic_map_implicit_equalities(bmap);
903 if (!bmap)
904 return isl_bool_error;
905 total = isl_basic_map_total_dim(bmap);
906 if (bmap->n_eq == total) {
907 int i, j;
908 for (i = 0; i < bmap->n_eq; ++i) {
909 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
910 if (j < 0)
911 break;
912 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
913 !isl_int_is_negone(bmap->eq[i][1 + j]))
914 break;
915 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
916 total - j - 1);
917 if (j >= 0)
918 break;
920 if (i == bmap->n_eq)
921 has_rational = isl_bool_false;
923 isl_basic_map_free(bmap);
925 return has_rational;
928 /* Does "map" contain any rational points?
930 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
932 int i;
933 isl_bool has_rational;
935 if (!map)
936 return isl_bool_error;
937 for (i = 0; i < map->n; ++i) {
938 has_rational = isl_basic_map_has_rational(map->p[i]);
939 if (has_rational < 0 || has_rational)
940 return has_rational;
942 return isl_bool_false;
945 /* Does "set" contain any rational points?
947 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
949 return isl_map_has_rational(set);
952 /* Is this basic set a parameter domain?
954 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
956 if (!bset)
957 return isl_bool_error;
958 return isl_space_is_params(bset->dim);
961 /* Is this set a parameter domain?
963 isl_bool isl_set_is_params(__isl_keep isl_set *set)
965 if (!set)
966 return isl_bool_error;
967 return isl_space_is_params(set->dim);
970 /* Is this map actually a parameter domain?
971 * Users should never call this function. Outside of isl,
972 * a map can never be a parameter domain.
974 isl_bool isl_map_is_params(__isl_keep isl_map *map)
976 if (!map)
977 return isl_bool_error;
978 return isl_space_is_params(map->dim);
981 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
982 struct isl_basic_map *bmap, unsigned extra,
983 unsigned n_eq, unsigned n_ineq)
985 int i;
986 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
988 bmap->ctx = ctx;
989 isl_ctx_ref(ctx);
991 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
992 if (isl_blk_is_error(bmap->block))
993 goto error;
995 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
996 if ((n_ineq + n_eq) && !bmap->ineq)
997 goto error;
999 if (extra == 0) {
1000 bmap->block2 = isl_blk_empty();
1001 bmap->div = NULL;
1002 } else {
1003 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1004 if (isl_blk_is_error(bmap->block2))
1005 goto error;
1007 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1008 if (!bmap->div)
1009 goto error;
1012 for (i = 0; i < n_ineq + n_eq; ++i)
1013 bmap->ineq[i] = bmap->block.data + i * row_size;
1015 for (i = 0; i < extra; ++i)
1016 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1018 bmap->ref = 1;
1019 bmap->flags = 0;
1020 bmap->c_size = n_eq + n_ineq;
1021 bmap->eq = bmap->ineq + n_ineq;
1022 bmap->extra = extra;
1023 bmap->n_eq = 0;
1024 bmap->n_ineq = 0;
1025 bmap->n_div = 0;
1026 bmap->sample = NULL;
1028 return bmap;
1029 error:
1030 isl_basic_map_free(bmap);
1031 return NULL;
1034 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1035 unsigned nparam, unsigned dim, unsigned extra,
1036 unsigned n_eq, unsigned n_ineq)
1038 struct isl_basic_map *bmap;
1039 isl_space *space;
1041 space = isl_space_set_alloc(ctx, nparam, dim);
1042 if (!space)
1043 return NULL;
1045 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1046 return bset_from_bmap(bmap);
1049 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1050 unsigned extra, unsigned n_eq, unsigned n_ineq)
1052 struct isl_basic_map *bmap;
1053 if (!dim)
1054 return NULL;
1055 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1056 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1057 return bset_from_bmap(bmap);
1058 error:
1059 isl_space_free(dim);
1060 return NULL;
1063 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1064 unsigned extra, unsigned n_eq, unsigned n_ineq)
1066 struct isl_basic_map *bmap;
1068 if (!dim)
1069 return NULL;
1070 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1071 if (!bmap)
1072 goto error;
1073 bmap->dim = dim;
1075 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1076 error:
1077 isl_space_free(dim);
1078 return NULL;
1081 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1082 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1083 unsigned n_eq, unsigned n_ineq)
1085 struct isl_basic_map *bmap;
1086 isl_space *dim;
1088 dim = isl_space_alloc(ctx, nparam, in, out);
1089 if (!dim)
1090 return NULL;
1092 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1093 return bmap;
1096 static void dup_constraints(
1097 struct isl_basic_map *dst, struct isl_basic_map *src)
1099 int i;
1100 unsigned total = isl_basic_map_total_dim(src);
1102 for (i = 0; i < src->n_eq; ++i) {
1103 int j = isl_basic_map_alloc_equality(dst);
1104 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1107 for (i = 0; i < src->n_ineq; ++i) {
1108 int j = isl_basic_map_alloc_inequality(dst);
1109 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1112 for (i = 0; i < src->n_div; ++i) {
1113 int j = isl_basic_map_alloc_div(dst);
1114 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1116 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1119 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1121 struct isl_basic_map *dup;
1123 if (!bmap)
1124 return NULL;
1125 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1126 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1127 if (!dup)
1128 return NULL;
1129 dup_constraints(dup, bmap);
1130 dup->flags = bmap->flags;
1131 dup->sample = isl_vec_copy(bmap->sample);
1132 return dup;
1135 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1137 struct isl_basic_map *dup;
1139 dup = isl_basic_map_dup(bset_to_bmap(bset));
1140 return bset_from_bmap(dup);
1143 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1145 if (!bset)
1146 return NULL;
1148 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1149 bset->ref++;
1150 return bset;
1152 return isl_basic_set_dup(bset);
1155 struct isl_set *isl_set_copy(struct isl_set *set)
1157 if (!set)
1158 return NULL;
1160 set->ref++;
1161 return set;
1164 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1166 if (!bmap)
1167 return NULL;
1169 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1170 bmap->ref++;
1171 return bmap;
1173 bmap = isl_basic_map_dup(bmap);
1174 if (bmap)
1175 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1176 return bmap;
1179 struct isl_map *isl_map_copy(struct isl_map *map)
1181 if (!map)
1182 return NULL;
1184 map->ref++;
1185 return map;
1188 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1190 if (!bmap)
1191 return NULL;
1193 if (--bmap->ref > 0)
1194 return NULL;
1196 isl_ctx_deref(bmap->ctx);
1197 free(bmap->div);
1198 isl_blk_free(bmap->ctx, bmap->block2);
1199 free(bmap->ineq);
1200 isl_blk_free(bmap->ctx, bmap->block);
1201 isl_vec_free(bmap->sample);
1202 isl_space_free(bmap->dim);
1203 free(bmap);
1205 return NULL;
1208 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1210 return isl_basic_map_free(bset_to_bmap(bset));
1213 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1215 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1218 __isl_give isl_map *isl_map_align_params_map_map_and(
1219 __isl_take isl_map *map1, __isl_take isl_map *map2,
1220 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1221 __isl_take isl_map *map2))
1223 if (!map1 || !map2)
1224 goto error;
1225 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1226 return fn(map1, map2);
1227 if (!isl_space_has_named_params(map1->dim) ||
1228 !isl_space_has_named_params(map2->dim))
1229 isl_die(map1->ctx, isl_error_invalid,
1230 "unaligned unnamed parameters", goto error);
1231 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1232 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1233 return fn(map1, map2);
1234 error:
1235 isl_map_free(map1);
1236 isl_map_free(map2);
1237 return NULL;
1240 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1241 __isl_keep isl_map *map2,
1242 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1244 isl_bool r;
1246 if (!map1 || !map2)
1247 return isl_bool_error;
1248 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1249 return fn(map1, map2);
1250 if (!isl_space_has_named_params(map1->dim) ||
1251 !isl_space_has_named_params(map2->dim))
1252 isl_die(map1->ctx, isl_error_invalid,
1253 "unaligned unnamed parameters", return isl_bool_error);
1254 map1 = isl_map_copy(map1);
1255 map2 = isl_map_copy(map2);
1256 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1257 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1258 r = fn(map1, map2);
1259 isl_map_free(map1);
1260 isl_map_free(map2);
1261 return r;
1264 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1266 struct isl_ctx *ctx;
1267 if (!bmap)
1268 return -1;
1269 ctx = bmap->ctx;
1270 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1271 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1272 return -1);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1274 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1275 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1276 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1277 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1278 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1279 isl_int *t;
1280 int j = isl_basic_map_alloc_inequality(bmap);
1281 if (j < 0)
1282 return -1;
1283 t = bmap->ineq[j];
1284 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1285 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1286 bmap->eq[-1] = t;
1287 bmap->n_eq++;
1288 bmap->n_ineq--;
1289 bmap->eq--;
1290 return 0;
1292 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1293 bmap->extra - bmap->n_div);
1294 return bmap->n_eq++;
1297 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1299 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1302 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1304 if (!bmap)
1305 return -1;
1306 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1307 bmap->n_eq -= n;
1308 return 0;
1311 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1313 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1316 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1318 isl_int *t;
1319 if (!bmap)
1320 return -1;
1321 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1323 if (pos != bmap->n_eq - 1) {
1324 t = bmap->eq[pos];
1325 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1326 bmap->eq[bmap->n_eq - 1] = t;
1328 bmap->n_eq--;
1329 return 0;
1332 /* Turn inequality "pos" of "bmap" into an equality.
1334 * In particular, we move the inequality in front of the equalities
1335 * and move the last inequality in the position of the moved inequality.
1336 * Note that isl_tab_make_equalities_explicit depends on this particular
1337 * change in the ordering of the constraints.
1339 void isl_basic_map_inequality_to_equality(
1340 struct isl_basic_map *bmap, unsigned pos)
1342 isl_int *t;
1344 t = bmap->ineq[pos];
1345 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1346 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1347 bmap->eq[-1] = t;
1348 bmap->n_eq++;
1349 bmap->n_ineq--;
1350 bmap->eq--;
1351 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1353 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1357 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1359 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1362 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1364 struct isl_ctx *ctx;
1365 if (!bmap)
1366 return -1;
1367 ctx = bmap->ctx;
1368 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1369 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1370 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1371 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1372 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1373 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1374 1 + isl_basic_map_total_dim(bmap),
1375 bmap->extra - bmap->n_div);
1376 return bmap->n_ineq++;
1379 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1381 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1384 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1386 if (!bmap)
1387 return -1;
1388 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1389 bmap->n_ineq -= n;
1390 return 0;
1393 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1395 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1398 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1400 isl_int *t;
1401 if (!bmap)
1402 return -1;
1403 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1405 if (pos != bmap->n_ineq - 1) {
1406 t = bmap->ineq[pos];
1407 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1408 bmap->ineq[bmap->n_ineq - 1] = t;
1409 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1411 bmap->n_ineq--;
1412 return 0;
1415 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1417 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1420 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1421 isl_int *eq)
1423 int k;
1425 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1426 if (!bmap)
1427 return NULL;
1428 k = isl_basic_map_alloc_equality(bmap);
1429 if (k < 0)
1430 goto error;
1431 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1432 return bmap;
1433 error:
1434 isl_basic_map_free(bmap);
1435 return NULL;
1438 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1439 isl_int *eq)
1441 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1444 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1445 isl_int *ineq)
1447 int k;
1449 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1450 if (!bmap)
1451 return NULL;
1452 k = isl_basic_map_alloc_inequality(bmap);
1453 if (k < 0)
1454 goto error;
1455 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1456 return bmap;
1457 error:
1458 isl_basic_map_free(bmap);
1459 return NULL;
1462 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1463 isl_int *ineq)
1465 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1468 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1470 if (!bmap)
1471 return -1;
1472 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1473 isl_seq_clr(bmap->div[bmap->n_div] +
1474 1 + 1 + isl_basic_map_total_dim(bmap),
1475 bmap->extra - bmap->n_div);
1476 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1477 return bmap->n_div++;
1480 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1482 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1485 /* Check that there are "n" dimensions of type "type" starting at "first"
1486 * in "bmap".
1488 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1489 enum isl_dim_type type, unsigned first, unsigned n)
1491 unsigned dim;
1493 if (!bmap)
1494 return isl_stat_error;
1495 dim = isl_basic_map_dim(bmap, type);
1496 if (first + n > dim || first + n < first)
1497 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1498 "position or range out of bounds",
1499 return isl_stat_error);
1500 return isl_stat_ok;
1503 /* Insert an extra integer division, prescribed by "div", to "bmap"
1504 * at (integer division) position "pos".
1506 * The integer division is first added at the end and then moved
1507 * into the right position.
1509 __isl_give isl_basic_map *isl_basic_map_insert_div(
1510 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1512 int i, k;
1514 bmap = isl_basic_map_cow(bmap);
1515 if (!bmap || !div)
1516 return isl_basic_map_free(bmap);
1518 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1519 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1520 "unexpected size", return isl_basic_map_free(bmap));
1521 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1522 return isl_basic_map_free(bmap);
1524 bmap = isl_basic_map_extend_space(bmap,
1525 isl_basic_map_get_space(bmap), 1, 0, 2);
1526 k = isl_basic_map_alloc_div(bmap);
1527 if (k < 0)
1528 return isl_basic_map_free(bmap);
1529 isl_seq_cpy(bmap->div[k], div->el, div->size);
1530 isl_int_set_si(bmap->div[k][div->size], 0);
1532 for (i = k; i > pos; --i)
1533 isl_basic_map_swap_div(bmap, i, i - 1);
1535 return bmap;
1538 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1540 if (!bmap)
1541 return isl_stat_error;
1542 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1543 bmap->n_div -= n;
1544 return isl_stat_ok;
1547 /* Copy constraint from src to dst, putting the vars of src at offset
1548 * dim_off in dst and the divs of src at offset div_off in dst.
1549 * If both sets are actually map, then dim_off applies to the input
1550 * variables.
1552 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1553 struct isl_basic_map *src_map, isl_int *src,
1554 unsigned in_off, unsigned out_off, unsigned div_off)
1556 unsigned src_nparam = isl_basic_map_n_param(src_map);
1557 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1558 unsigned src_in = isl_basic_map_n_in(src_map);
1559 unsigned dst_in = isl_basic_map_n_in(dst_map);
1560 unsigned src_out = isl_basic_map_n_out(src_map);
1561 unsigned dst_out = isl_basic_map_n_out(dst_map);
1562 isl_int_set(dst[0], src[0]);
1563 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1564 if (dst_nparam > src_nparam)
1565 isl_seq_clr(dst+1+src_nparam,
1566 dst_nparam - src_nparam);
1567 isl_seq_clr(dst+1+dst_nparam, in_off);
1568 isl_seq_cpy(dst+1+dst_nparam+in_off,
1569 src+1+src_nparam,
1570 isl_min(dst_in-in_off, src_in));
1571 if (dst_in-in_off > src_in)
1572 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1573 dst_in - in_off - src_in);
1574 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1575 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1576 src+1+src_nparam+src_in,
1577 isl_min(dst_out-out_off, src_out));
1578 if (dst_out-out_off > src_out)
1579 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1580 dst_out - out_off - src_out);
1581 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1582 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1583 src+1+src_nparam+src_in+src_out,
1584 isl_min(dst_map->extra-div_off, src_map->n_div));
1585 if (dst_map->n_div-div_off > src_map->n_div)
1586 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1587 div_off+src_map->n_div,
1588 dst_map->n_div - div_off - src_map->n_div);
1591 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1592 struct isl_basic_map *src_map, isl_int *src,
1593 unsigned in_off, unsigned out_off, unsigned div_off)
1595 isl_int_set(dst[0], src[0]);
1596 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1599 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1600 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1602 int i;
1603 unsigned div_off;
1605 if (!bmap1 || !bmap2)
1606 goto error;
1608 div_off = bmap1->n_div;
1610 for (i = 0; i < bmap2->n_eq; ++i) {
1611 int i1 = isl_basic_map_alloc_equality(bmap1);
1612 if (i1 < 0)
1613 goto error;
1614 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1615 i_pos, o_pos, div_off);
1618 for (i = 0; i < bmap2->n_ineq; ++i) {
1619 int i1 = isl_basic_map_alloc_inequality(bmap1);
1620 if (i1 < 0)
1621 goto error;
1622 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1623 i_pos, o_pos, div_off);
1626 for (i = 0; i < bmap2->n_div; ++i) {
1627 int i1 = isl_basic_map_alloc_div(bmap1);
1628 if (i1 < 0)
1629 goto error;
1630 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1631 i_pos, o_pos, div_off);
1634 isl_basic_map_free(bmap2);
1636 return bmap1;
1638 error:
1639 isl_basic_map_free(bmap1);
1640 isl_basic_map_free(bmap2);
1641 return NULL;
1644 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1645 struct isl_basic_set *bset2, unsigned pos)
1647 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1648 bset_to_bmap(bset2), 0, pos));
1651 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1652 __isl_take isl_space *dim, unsigned extra,
1653 unsigned n_eq, unsigned n_ineq)
1655 struct isl_basic_map *ext;
1656 unsigned flags;
1657 int dims_ok;
1659 if (!dim)
1660 goto error;
1662 if (!base)
1663 goto error;
1665 dims_ok = isl_space_is_equal(base->dim, dim) &&
1666 base->extra >= base->n_div + extra;
1668 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1669 room_for_ineq(base, n_ineq)) {
1670 isl_space_free(dim);
1671 return base;
1674 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1675 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1676 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1677 extra += base->extra;
1678 n_eq += base->n_eq;
1679 n_ineq += base->n_ineq;
1681 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1682 dim = NULL;
1683 if (!ext)
1684 goto error;
1686 if (dims_ok)
1687 ext->sample = isl_vec_copy(base->sample);
1688 flags = base->flags;
1689 ext = add_constraints(ext, base, 0, 0);
1690 if (ext) {
1691 ext->flags = flags;
1692 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1695 return ext;
1697 error:
1698 isl_space_free(dim);
1699 isl_basic_map_free(base);
1700 return NULL;
1703 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1704 __isl_take isl_space *dim, unsigned extra,
1705 unsigned n_eq, unsigned n_ineq)
1707 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1708 dim, extra, n_eq, n_ineq));
1711 struct isl_basic_map *isl_basic_map_extend_constraints(
1712 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1714 if (!base)
1715 return NULL;
1716 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1717 0, n_eq, n_ineq);
1720 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1721 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1722 unsigned n_eq, unsigned n_ineq)
1724 struct isl_basic_map *bmap;
1725 isl_space *dim;
1727 if (!base)
1728 return NULL;
1729 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1730 if (!dim)
1731 goto error;
1733 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1734 return bmap;
1735 error:
1736 isl_basic_map_free(base);
1737 return NULL;
1740 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1741 unsigned nparam, unsigned dim, unsigned extra,
1742 unsigned n_eq, unsigned n_ineq)
1744 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1745 nparam, 0, dim, extra, n_eq, n_ineq));
1748 struct isl_basic_set *isl_basic_set_extend_constraints(
1749 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1751 isl_basic_map *bmap = bset_to_bmap(base);
1752 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1753 return bset_from_bmap(bmap);
1756 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1758 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1761 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1763 if (!bmap)
1764 return NULL;
1766 if (bmap->ref > 1) {
1767 bmap->ref--;
1768 bmap = isl_basic_map_dup(bmap);
1770 if (bmap) {
1771 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1772 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1774 return bmap;
1777 /* Clear all cached information in "map", either because it is about
1778 * to be modified or because it is being freed.
1779 * Always return the same pointer that is passed in.
1780 * This is needed for the use in isl_map_free.
1782 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1784 isl_basic_map_free(map->cached_simple_hull[0]);
1785 isl_basic_map_free(map->cached_simple_hull[1]);
1786 map->cached_simple_hull[0] = NULL;
1787 map->cached_simple_hull[1] = NULL;
1788 return map;
1791 struct isl_set *isl_set_cow(struct isl_set *set)
1793 return isl_map_cow(set);
1796 /* Return an isl_map that is equal to "map" and that has only
1797 * a single reference.
1799 * If the original input already has only one reference, then
1800 * simply return it, but clear all cached information, since
1801 * it may be rendered invalid by the operations that will be
1802 * performed on the result.
1804 * Otherwise, create a duplicate (without any cached information).
1806 struct isl_map *isl_map_cow(struct isl_map *map)
1808 if (!map)
1809 return NULL;
1811 if (map->ref == 1)
1812 return clear_caches(map);
1813 map->ref--;
1814 return isl_map_dup(map);
1817 static void swap_vars(struct isl_blk blk, isl_int *a,
1818 unsigned a_len, unsigned b_len)
1820 isl_seq_cpy(blk.data, a+a_len, b_len);
1821 isl_seq_cpy(blk.data+b_len, a, a_len);
1822 isl_seq_cpy(a, blk.data, b_len+a_len);
1825 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1826 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1828 int i;
1829 struct isl_blk blk;
1831 if (!bmap)
1832 goto error;
1834 isl_assert(bmap->ctx,
1835 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1837 if (n1 == 0 || n2 == 0)
1838 return bmap;
1840 bmap = isl_basic_map_cow(bmap);
1841 if (!bmap)
1842 return NULL;
1844 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1845 if (isl_blk_is_error(blk))
1846 goto error;
1848 for (i = 0; i < bmap->n_eq; ++i)
1849 swap_vars(blk,
1850 bmap->eq[i] + pos, n1, n2);
1852 for (i = 0; i < bmap->n_ineq; ++i)
1853 swap_vars(blk,
1854 bmap->ineq[i] + pos, n1, n2);
1856 for (i = 0; i < bmap->n_div; ++i)
1857 swap_vars(blk,
1858 bmap->div[i]+1 + pos, n1, n2);
1860 isl_blk_free(bmap->ctx, blk);
1862 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1863 bmap = isl_basic_map_gauss(bmap, NULL);
1864 return isl_basic_map_finalize(bmap);
1865 error:
1866 isl_basic_map_free(bmap);
1867 return NULL;
1870 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1872 int i = 0;
1873 unsigned total;
1874 if (!bmap)
1875 goto error;
1876 total = isl_basic_map_total_dim(bmap);
1877 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1878 return isl_basic_map_free(bmap);
1879 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1880 if (bmap->n_eq > 0)
1881 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1882 else {
1883 i = isl_basic_map_alloc_equality(bmap);
1884 if (i < 0)
1885 goto error;
1887 isl_int_set_si(bmap->eq[i][0], 1);
1888 isl_seq_clr(bmap->eq[i]+1, total);
1889 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1890 isl_vec_free(bmap->sample);
1891 bmap->sample = NULL;
1892 return isl_basic_map_finalize(bmap);
1893 error:
1894 isl_basic_map_free(bmap);
1895 return NULL;
1898 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1900 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1903 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1904 * of "bmap").
1906 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1908 isl_int *t = bmap->div[a];
1909 bmap->div[a] = bmap->div[b];
1910 bmap->div[b] = t;
1913 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1914 * div definitions accordingly.
1916 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1918 int i;
1919 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1921 swap_div(bmap, a, b);
1923 for (i = 0; i < bmap->n_eq; ++i)
1924 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1926 for (i = 0; i < bmap->n_ineq; ++i)
1927 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1929 for (i = 0; i < bmap->n_div; ++i)
1930 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1931 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1934 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1935 * div definitions accordingly.
1937 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1939 isl_basic_map_swap_div(bset, a, b);
1942 /* Eliminate the specified n dimensions starting at first from the
1943 * constraints, without removing the dimensions from the space.
1944 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1946 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1947 enum isl_dim_type type, unsigned first, unsigned n)
1949 int i;
1951 if (!map)
1952 return NULL;
1953 if (n == 0)
1954 return map;
1956 if (first + n > isl_map_dim(map, type) || first + n < first)
1957 isl_die(map->ctx, isl_error_invalid,
1958 "index out of bounds", goto error);
1960 map = isl_map_cow(map);
1961 if (!map)
1962 return NULL;
1964 for (i = 0; i < map->n; ++i) {
1965 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1966 if (!map->p[i])
1967 goto error;
1969 return map;
1970 error:
1971 isl_map_free(map);
1972 return NULL;
1975 /* Eliminate the specified n dimensions starting at first from the
1976 * constraints, without removing the dimensions from the space.
1977 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1979 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1980 enum isl_dim_type type, unsigned first, unsigned n)
1982 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1985 /* Eliminate the specified n dimensions starting at first from the
1986 * constraints, without removing the dimensions from the space.
1987 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1989 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1990 unsigned first, unsigned n)
1992 return isl_set_eliminate(set, isl_dim_set, first, n);
1995 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1996 __isl_take isl_basic_map *bmap)
1998 if (!bmap)
1999 return NULL;
2000 bmap = isl_basic_map_eliminate_vars(bmap,
2001 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2002 if (!bmap)
2003 return NULL;
2004 bmap->n_div = 0;
2005 return isl_basic_map_finalize(bmap);
2008 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2009 __isl_take isl_basic_set *bset)
2011 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2014 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2016 int i;
2018 if (!map)
2019 return NULL;
2020 if (map->n == 0)
2021 return map;
2023 map = isl_map_cow(map);
2024 if (!map)
2025 return NULL;
2027 for (i = 0; i < map->n; ++i) {
2028 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2029 if (!map->p[i])
2030 goto error;
2032 return map;
2033 error:
2034 isl_map_free(map);
2035 return NULL;
2038 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2040 return isl_map_remove_divs(set);
2043 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2044 enum isl_dim_type type, unsigned first, unsigned n)
2046 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2047 return isl_basic_map_free(bmap);
2048 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2049 return bmap;
2050 bmap = isl_basic_map_eliminate_vars(bmap,
2051 isl_basic_map_offset(bmap, type) - 1 + first, n);
2052 if (!bmap)
2053 return bmap;
2054 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2055 return bmap;
2056 bmap = isl_basic_map_drop(bmap, type, first, n);
2057 return bmap;
2060 /* Return true if the definition of the given div (recursively) involves
2061 * any of the given variables.
2063 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2064 unsigned first, unsigned n)
2066 int i;
2067 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2069 if (isl_int_is_zero(bmap->div[div][0]))
2070 return isl_bool_false;
2071 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2072 return isl_bool_true;
2074 for (i = bmap->n_div - 1; i >= 0; --i) {
2075 isl_bool involves;
2077 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2078 continue;
2079 involves = div_involves_vars(bmap, i, first, n);
2080 if (involves < 0 || involves)
2081 return involves;
2084 return isl_bool_false;
2087 /* Try and add a lower and/or upper bound on "div" to "bmap"
2088 * based on inequality "i".
2089 * "total" is the total number of variables (excluding the divs).
2090 * "v" is a temporary object that can be used during the calculations.
2091 * If "lb" is set, then a lower bound should be constructed.
2092 * If "ub" is set, then an upper bound should be constructed.
2094 * The calling function has already checked that the inequality does not
2095 * reference "div", but we still need to check that the inequality is
2096 * of the right form. We'll consider the case where we want to construct
2097 * a lower bound. The construction of upper bounds is similar.
2099 * Let "div" be of the form
2101 * q = floor((a + f(x))/d)
2103 * We essentially check if constraint "i" is of the form
2105 * b + f(x) >= 0
2107 * so that we can use it to derive a lower bound on "div".
2108 * However, we allow a slightly more general form
2110 * b + g(x) >= 0
2112 * with the condition that the coefficients of g(x) - f(x) are all
2113 * divisible by d.
2114 * Rewriting this constraint as
2116 * 0 >= -b - g(x)
2118 * adding a + f(x) to both sides and dividing by d, we obtain
2120 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2122 * Taking the floor on both sides, we obtain
2124 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2126 * or
2128 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2130 * In the case of an upper bound, we construct the constraint
2132 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2135 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2136 __isl_take isl_basic_map *bmap, int div, int i,
2137 unsigned total, isl_int v, int lb, int ub)
2139 int j;
2141 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2142 if (lb) {
2143 isl_int_sub(v, bmap->ineq[i][1 + j],
2144 bmap->div[div][1 + 1 + j]);
2145 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2147 if (ub) {
2148 isl_int_add(v, bmap->ineq[i][1 + j],
2149 bmap->div[div][1 + 1 + j]);
2150 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2153 if (!lb && !ub)
2154 return bmap;
2156 bmap = isl_basic_map_cow(bmap);
2157 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2158 if (lb) {
2159 int k = isl_basic_map_alloc_inequality(bmap);
2160 if (k < 0)
2161 goto error;
2162 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2163 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2164 bmap->div[div][1 + j]);
2165 isl_int_cdiv_q(bmap->ineq[k][j],
2166 bmap->ineq[k][j], bmap->div[div][0]);
2168 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2170 if (ub) {
2171 int k = isl_basic_map_alloc_inequality(bmap);
2172 if (k < 0)
2173 goto error;
2174 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2175 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2176 bmap->div[div][1 + j]);
2177 isl_int_fdiv_q(bmap->ineq[k][j],
2178 bmap->ineq[k][j], bmap->div[div][0]);
2180 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2183 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2184 return bmap;
2185 error:
2186 isl_basic_map_free(bmap);
2187 return NULL;
2190 /* This function is called right before "div" is eliminated from "bmap"
2191 * using Fourier-Motzkin.
2192 * Look through the constraints of "bmap" for constraints on the argument
2193 * of the integer division and use them to construct constraints on the
2194 * integer division itself. These constraints can then be combined
2195 * during the Fourier-Motzkin elimination.
2196 * Note that it is only useful to introduce lower bounds on "div"
2197 * if "bmap" already contains upper bounds on "div" as the newly
2198 * introduce lower bounds can then be combined with the pre-existing
2199 * upper bounds. Similarly for upper bounds.
2200 * We therefore first check if "bmap" contains any lower and/or upper bounds
2201 * on "div".
2203 * It is interesting to note that the introduction of these constraints
2204 * can indeed lead to more accurate results, even when compared to
2205 * deriving constraints on the argument of "div" from constraints on "div".
2206 * Consider, for example, the set
2208 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2210 * The second constraint can be rewritten as
2212 * 2 * [(-i-2j+3)/4] + k >= 0
2214 * from which we can derive
2216 * -i - 2j + 3 >= -2k
2218 * or
2220 * i + 2j <= 3 + 2k
2222 * Combined with the first constraint, we obtain
2224 * -3 <= 3 + 2k or k >= -3
2226 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2227 * the first constraint, we obtain
2229 * [(i + 2j)/4] >= [-3/4] = -1
2231 * Combining this constraint with the second constraint, we obtain
2233 * k >= -2
2235 static __isl_give isl_basic_map *insert_bounds_on_div(
2236 __isl_take isl_basic_map *bmap, int div)
2238 int i;
2239 int check_lb, check_ub;
2240 isl_int v;
2241 unsigned total;
2243 if (!bmap)
2244 return NULL;
2246 if (isl_int_is_zero(bmap->div[div][0]))
2247 return bmap;
2249 total = isl_space_dim(bmap->dim, isl_dim_all);
2251 check_lb = 0;
2252 check_ub = 0;
2253 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2254 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2255 if (s > 0)
2256 check_ub = 1;
2257 if (s < 0)
2258 check_lb = 1;
2261 if (!check_lb && !check_ub)
2262 return bmap;
2264 isl_int_init(v);
2266 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2267 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2268 continue;
2270 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2271 check_lb, check_ub);
2274 isl_int_clear(v);
2276 return bmap;
2279 /* Remove all divs (recursively) involving any of the given dimensions
2280 * in their definitions.
2282 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2283 __isl_take isl_basic_map *bmap,
2284 enum isl_dim_type type, unsigned first, unsigned n)
2286 int i;
2288 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2289 return isl_basic_map_free(bmap);
2290 first += isl_basic_map_offset(bmap, type);
2292 for (i = bmap->n_div - 1; i >= 0; --i) {
2293 isl_bool involves;
2295 involves = div_involves_vars(bmap, i, first, n);
2296 if (involves < 0)
2297 return isl_basic_map_free(bmap);
2298 if (!involves)
2299 continue;
2300 bmap = insert_bounds_on_div(bmap, i);
2301 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2302 if (!bmap)
2303 return NULL;
2304 i = bmap->n_div;
2307 return bmap;
2310 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2311 __isl_take isl_basic_set *bset,
2312 enum isl_dim_type type, unsigned first, unsigned n)
2314 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2317 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2318 enum isl_dim_type type, unsigned first, unsigned n)
2320 int i;
2322 if (!map)
2323 return NULL;
2324 if (map->n == 0)
2325 return map;
2327 map = isl_map_cow(map);
2328 if (!map)
2329 return NULL;
2331 for (i = 0; i < map->n; ++i) {
2332 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2333 type, first, n);
2334 if (!map->p[i])
2335 goto error;
2337 return map;
2338 error:
2339 isl_map_free(map);
2340 return NULL;
2343 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2344 enum isl_dim_type type, unsigned first, unsigned n)
2346 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2347 type, first, n));
2350 /* Does the description of "bmap" depend on the specified dimensions?
2351 * We also check whether the dimensions appear in any of the div definitions.
2352 * In principle there is no need for this check. If the dimensions appear
2353 * in a div definition, they also appear in the defining constraints of that
2354 * div.
2356 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2357 enum isl_dim_type type, unsigned first, unsigned n)
2359 int i;
2361 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2362 return isl_bool_error;
2364 first += isl_basic_map_offset(bmap, type);
2365 for (i = 0; i < bmap->n_eq; ++i)
2366 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2367 return isl_bool_true;
2368 for (i = 0; i < bmap->n_ineq; ++i)
2369 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2370 return isl_bool_true;
2371 for (i = 0; i < bmap->n_div; ++i) {
2372 if (isl_int_is_zero(bmap->div[i][0]))
2373 continue;
2374 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2375 return isl_bool_true;
2378 return isl_bool_false;
2381 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2382 enum isl_dim_type type, unsigned first, unsigned n)
2384 int i;
2386 if (!map)
2387 return isl_bool_error;
2389 if (first + n > isl_map_dim(map, type))
2390 isl_die(map->ctx, isl_error_invalid,
2391 "index out of bounds", return isl_bool_error);
2393 for (i = 0; i < map->n; ++i) {
2394 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2395 type, first, n);
2396 if (involves < 0 || involves)
2397 return involves;
2400 return isl_bool_false;
2403 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2404 enum isl_dim_type type, unsigned first, unsigned n)
2406 return isl_basic_map_involves_dims(bset, type, first, n);
2409 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2410 enum isl_dim_type type, unsigned first, unsigned n)
2412 return isl_map_involves_dims(set, type, first, n);
2415 /* Drop all constraints in bmap that involve any of the dimensions
2416 * first to first+n-1.
2418 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2419 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2421 int i;
2423 if (n == 0)
2424 return bmap;
2426 bmap = isl_basic_map_cow(bmap);
2428 if (!bmap)
2429 return NULL;
2431 for (i = bmap->n_eq - 1; i >= 0; --i) {
2432 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2433 continue;
2434 isl_basic_map_drop_equality(bmap, i);
2437 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2438 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2439 continue;
2440 isl_basic_map_drop_inequality(bmap, i);
2443 bmap = isl_basic_map_add_known_div_constraints(bmap);
2444 return bmap;
2447 /* Drop all constraints in bset that involve any of the dimensions
2448 * first to first+n-1.
2450 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2451 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2453 return isl_basic_map_drop_constraints_involving(bset, first, n);
2456 /* Drop all constraints in bmap that do not involve any of the dimensions
2457 * first to first + n - 1 of the given type.
2459 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2460 __isl_take isl_basic_map *bmap,
2461 enum isl_dim_type type, unsigned first, unsigned n)
2463 int i;
2465 if (n == 0) {
2466 isl_space *space = isl_basic_map_get_space(bmap);
2467 isl_basic_map_free(bmap);
2468 return isl_basic_map_universe(space);
2470 bmap = isl_basic_map_cow(bmap);
2471 if (!bmap)
2472 return NULL;
2474 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2475 return isl_basic_map_free(bmap);
2477 first += isl_basic_map_offset(bmap, type) - 1;
2479 for (i = bmap->n_eq - 1; i >= 0; --i) {
2480 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2481 continue;
2482 isl_basic_map_drop_equality(bmap, i);
2485 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2486 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2487 continue;
2488 isl_basic_map_drop_inequality(bmap, i);
2491 bmap = isl_basic_map_add_known_div_constraints(bmap);
2492 return bmap;
2495 /* Drop all constraints in bset that do not involve any of the dimensions
2496 * first to first + n - 1 of the given type.
2498 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2499 __isl_take isl_basic_set *bset,
2500 enum isl_dim_type type, unsigned first, unsigned n)
2502 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2503 type, first, n);
2506 /* Drop all constraints in bmap that involve any of the dimensions
2507 * first to first + n - 1 of the given type.
2509 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2510 __isl_take isl_basic_map *bmap,
2511 enum isl_dim_type type, unsigned first, unsigned n)
2513 if (!bmap)
2514 return NULL;
2515 if (n == 0)
2516 return bmap;
2518 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2519 return isl_basic_map_free(bmap);
2521 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2522 first += isl_basic_map_offset(bmap, type) - 1;
2523 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2526 /* Drop all constraints in bset that involve any of the dimensions
2527 * first to first + n - 1 of the given type.
2529 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2530 __isl_take isl_basic_set *bset,
2531 enum isl_dim_type type, unsigned first, unsigned n)
2533 return isl_basic_map_drop_constraints_involving_dims(bset,
2534 type, first, n);
2537 /* Drop constraints from "map" by applying "drop" to each basic map.
2539 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2540 enum isl_dim_type type, unsigned first, unsigned n,
2541 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2542 enum isl_dim_type type, unsigned first, unsigned n))
2544 int i;
2545 unsigned dim;
2547 if (!map)
2548 return NULL;
2550 dim = isl_map_dim(map, type);
2551 if (first + n > dim || first + n < first)
2552 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2553 "index out of bounds", return isl_map_free(map));
2555 map = isl_map_cow(map);
2556 if (!map)
2557 return NULL;
2559 for (i = 0; i < map->n; ++i) {
2560 map->p[i] = drop(map->p[i], type, first, n);
2561 if (!map->p[i])
2562 return isl_map_free(map);
2565 if (map->n > 1)
2566 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2568 return map;
2571 /* Drop all constraints in map that involve any of the dimensions
2572 * first to first + n - 1 of the given type.
2574 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2575 __isl_take isl_map *map,
2576 enum isl_dim_type type, unsigned first, unsigned n)
2578 if (n == 0)
2579 return map;
2580 return drop_constraints(map, type, first, n,
2581 &isl_basic_map_drop_constraints_involving_dims);
2584 /* Drop all constraints in "map" that do not involve any of the dimensions
2585 * first to first + n - 1 of the given type.
2587 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2588 __isl_take isl_map *map,
2589 enum isl_dim_type type, unsigned first, unsigned n)
2591 if (n == 0) {
2592 isl_space *space = isl_map_get_space(map);
2593 isl_map_free(map);
2594 return isl_map_universe(space);
2596 return drop_constraints(map, type, first, n,
2597 &isl_basic_map_drop_constraints_not_involving_dims);
2600 /* Drop all constraints in set that involve any of the dimensions
2601 * first to first + n - 1 of the given type.
2603 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2604 __isl_take isl_set *set,
2605 enum isl_dim_type type, unsigned first, unsigned n)
2607 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2610 /* Drop all constraints in "set" that do not involve any of the dimensions
2611 * first to first + n - 1 of the given type.
2613 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2614 __isl_take isl_set *set,
2615 enum isl_dim_type type, unsigned first, unsigned n)
2617 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2620 /* Does local variable "div" of "bmap" have a complete explicit representation?
2621 * Having a complete explicit representation requires not only
2622 * an explicit representation, but also that all local variables
2623 * that appear in this explicit representation in turn have
2624 * a complete explicit representation.
2626 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2628 int i;
2629 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2630 isl_bool marked;
2632 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2633 if (marked < 0 || marked)
2634 return isl_bool_not(marked);
2636 for (i = bmap->n_div - 1; i >= 0; --i) {
2637 isl_bool known;
2639 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2640 continue;
2641 known = isl_basic_map_div_is_known(bmap, i);
2642 if (known < 0 || !known)
2643 return known;
2646 return isl_bool_true;
2649 /* Remove all divs that are unknown or defined in terms of unknown divs.
2651 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2652 __isl_take isl_basic_map *bmap)
2654 int i;
2656 if (!bmap)
2657 return NULL;
2659 for (i = bmap->n_div - 1; i >= 0; --i) {
2660 if (isl_basic_map_div_is_known(bmap, i))
2661 continue;
2662 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2663 if (!bmap)
2664 return NULL;
2665 i = bmap->n_div;
2668 return bmap;
2671 /* Remove all divs that are unknown or defined in terms of unknown divs.
2673 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2674 __isl_take isl_basic_set *bset)
2676 return isl_basic_map_remove_unknown_divs(bset);
2679 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2681 int i;
2683 if (!map)
2684 return NULL;
2685 if (map->n == 0)
2686 return map;
2688 map = isl_map_cow(map);
2689 if (!map)
2690 return NULL;
2692 for (i = 0; i < map->n; ++i) {
2693 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2694 if (!map->p[i])
2695 goto error;
2697 return map;
2698 error:
2699 isl_map_free(map);
2700 return NULL;
2703 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2705 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2708 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2709 __isl_take isl_basic_set *bset,
2710 enum isl_dim_type type, unsigned first, unsigned n)
2712 isl_basic_map *bmap = bset_to_bmap(bset);
2713 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2714 return bset_from_bmap(bmap);
2717 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2718 enum isl_dim_type type, unsigned first, unsigned n)
2720 int i;
2722 if (n == 0)
2723 return map;
2725 map = isl_map_cow(map);
2726 if (!map)
2727 return NULL;
2728 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2730 for (i = 0; i < map->n; ++i) {
2731 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2732 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2733 if (!map->p[i])
2734 goto error;
2736 map = isl_map_drop(map, type, first, n);
2737 return map;
2738 error:
2739 isl_map_free(map);
2740 return NULL;
2743 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2744 enum isl_dim_type type, unsigned first, unsigned n)
2746 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2747 type, first, n));
2750 /* Project out n inputs starting at first using Fourier-Motzkin */
2751 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2752 unsigned first, unsigned n)
2754 return isl_map_remove_dims(map, isl_dim_in, first, n);
2757 static void dump_term(struct isl_basic_map *bmap,
2758 isl_int c, int pos, FILE *out)
2760 const char *name;
2761 unsigned in = isl_basic_map_n_in(bmap);
2762 unsigned dim = in + isl_basic_map_n_out(bmap);
2763 unsigned nparam = isl_basic_map_n_param(bmap);
2764 if (!pos)
2765 isl_int_print(out, c, 0);
2766 else {
2767 if (!isl_int_is_one(c))
2768 isl_int_print(out, c, 0);
2769 if (pos < 1 + nparam) {
2770 name = isl_space_get_dim_name(bmap->dim,
2771 isl_dim_param, pos - 1);
2772 if (name)
2773 fprintf(out, "%s", name);
2774 else
2775 fprintf(out, "p%d", pos - 1);
2776 } else if (pos < 1 + nparam + in)
2777 fprintf(out, "i%d", pos - 1 - nparam);
2778 else if (pos < 1 + nparam + dim)
2779 fprintf(out, "o%d", pos - 1 - nparam - in);
2780 else
2781 fprintf(out, "e%d", pos - 1 - nparam - dim);
2785 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2786 int sign, FILE *out)
2788 int i;
2789 int first;
2790 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2791 isl_int v;
2793 isl_int_init(v);
2794 for (i = 0, first = 1; i < len; ++i) {
2795 if (isl_int_sgn(c[i]) * sign <= 0)
2796 continue;
2797 if (!first)
2798 fprintf(out, " + ");
2799 first = 0;
2800 isl_int_abs(v, c[i]);
2801 dump_term(bmap, v, i, out);
2803 isl_int_clear(v);
2804 if (first)
2805 fprintf(out, "0");
2808 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2809 const char *op, FILE *out, int indent)
2811 int i;
2813 fprintf(out, "%*s", indent, "");
2815 dump_constraint_sign(bmap, c, 1, out);
2816 fprintf(out, " %s ", op);
2817 dump_constraint_sign(bmap, c, -1, out);
2819 fprintf(out, "\n");
2821 for (i = bmap->n_div; i < bmap->extra; ++i) {
2822 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2823 continue;
2824 fprintf(out, "%*s", indent, "");
2825 fprintf(out, "ERROR: unused div coefficient not zero\n");
2826 abort();
2830 static void dump_constraints(struct isl_basic_map *bmap,
2831 isl_int **c, unsigned n,
2832 const char *op, FILE *out, int indent)
2834 int i;
2836 for (i = 0; i < n; ++i)
2837 dump_constraint(bmap, c[i], op, out, indent);
2840 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2842 int j;
2843 int first = 1;
2844 unsigned total = isl_basic_map_total_dim(bmap);
2846 for (j = 0; j < 1 + total; ++j) {
2847 if (isl_int_is_zero(exp[j]))
2848 continue;
2849 if (!first && isl_int_is_pos(exp[j]))
2850 fprintf(out, "+");
2851 dump_term(bmap, exp[j], j, out);
2852 first = 0;
2856 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2858 int i;
2860 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2861 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2863 for (i = 0; i < bmap->n_div; ++i) {
2864 fprintf(out, "%*s", indent, "");
2865 fprintf(out, "e%d = [(", i);
2866 dump_affine(bmap, bmap->div[i]+1, out);
2867 fprintf(out, ")/");
2868 isl_int_print(out, bmap->div[i][0], 0);
2869 fprintf(out, "]\n");
2873 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2874 FILE *out, int indent)
2876 if (!bset) {
2877 fprintf(out, "null basic set\n");
2878 return;
2881 fprintf(out, "%*s", indent, "");
2882 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2883 bset->ref, bset->dim->nparam, bset->dim->n_out,
2884 bset->extra, bset->flags);
2885 dump(bset_to_bmap(bset), out, indent);
2888 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2889 FILE *out, int indent)
2891 if (!bmap) {
2892 fprintf(out, "null basic map\n");
2893 return;
2896 fprintf(out, "%*s", indent, "");
2897 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2898 "flags: %x, n_name: %d\n",
2899 bmap->ref,
2900 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2901 bmap->extra, bmap->flags, bmap->dim->n_id);
2902 dump(bmap, out, indent);
2905 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2907 unsigned total;
2908 if (!bmap)
2909 return -1;
2910 total = isl_basic_map_total_dim(bmap);
2911 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2912 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2913 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2914 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2915 return 0;
2918 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2919 unsigned flags)
2921 if (!space)
2922 return NULL;
2923 if (isl_space_dim(space, isl_dim_in) != 0)
2924 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2925 "set cannot have input dimensions", goto error);
2926 return isl_map_alloc_space(space, n, flags);
2927 error:
2928 isl_space_free(space);
2929 return NULL;
2932 /* Make sure "map" has room for at least "n" more basic maps.
2934 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2936 int i;
2937 struct isl_map *grown = NULL;
2939 if (!map)
2940 return NULL;
2941 isl_assert(map->ctx, n >= 0, goto error);
2942 if (map->n + n <= map->size)
2943 return map;
2944 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2945 if (!grown)
2946 goto error;
2947 for (i = 0; i < map->n; ++i) {
2948 grown->p[i] = isl_basic_map_copy(map->p[i]);
2949 if (!grown->p[i])
2950 goto error;
2951 grown->n++;
2953 isl_map_free(map);
2954 return grown;
2955 error:
2956 isl_map_free(grown);
2957 isl_map_free(map);
2958 return NULL;
2961 /* Make sure "set" has room for at least "n" more basic sets.
2963 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2965 return set_from_map(isl_map_grow(set_to_map(set), n));
2968 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2970 return isl_map_from_basic_map(bset);
2973 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2975 struct isl_map *map;
2977 if (!bmap)
2978 return NULL;
2980 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2981 return isl_map_add_basic_map(map, bmap);
2984 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2985 __isl_take isl_basic_set *bset)
2987 return set_from_map(isl_map_add_basic_map(set_to_map(set),
2988 bset_to_bmap(bset)));
2991 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2993 return isl_map_free(set);
2996 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2998 int i;
3000 if (!set) {
3001 fprintf(out, "null set\n");
3002 return;
3005 fprintf(out, "%*s", indent, "");
3006 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3007 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3008 set->flags);
3009 for (i = 0; i < set->n; ++i) {
3010 fprintf(out, "%*s", indent, "");
3011 fprintf(out, "basic set %d:\n", i);
3012 isl_basic_set_print_internal(set->p[i], out, indent+4);
3016 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3018 int i;
3020 if (!map) {
3021 fprintf(out, "null map\n");
3022 return;
3025 fprintf(out, "%*s", indent, "");
3026 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3027 "flags: %x, n_name: %d\n",
3028 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3029 map->dim->n_out, map->flags, map->dim->n_id);
3030 for (i = 0; i < map->n; ++i) {
3031 fprintf(out, "%*s", indent, "");
3032 fprintf(out, "basic map %d:\n", i);
3033 isl_basic_map_print_internal(map->p[i], out, indent+4);
3037 struct isl_basic_map *isl_basic_map_intersect_domain(
3038 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3040 struct isl_basic_map *bmap_domain;
3042 if (!bmap || !bset)
3043 goto error;
3045 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3046 bset->dim, isl_dim_param), goto error);
3048 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3049 isl_assert(bset->ctx,
3050 isl_basic_map_compatible_domain(bmap, bset), goto error);
3052 bmap = isl_basic_map_cow(bmap);
3053 if (!bmap)
3054 goto error;
3055 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3056 bset->n_div, bset->n_eq, bset->n_ineq);
3057 bmap_domain = isl_basic_map_from_domain(bset);
3058 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3060 bmap = isl_basic_map_simplify(bmap);
3061 return isl_basic_map_finalize(bmap);
3062 error:
3063 isl_basic_map_free(bmap);
3064 isl_basic_set_free(bset);
3065 return NULL;
3068 /* Check that the space of "bset" is the same as that of the range of "bmap".
3070 static isl_stat isl_basic_map_check_compatible_range(
3071 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3073 isl_bool ok;
3075 ok = isl_basic_map_compatible_range(bmap, bset);
3076 if (ok < 0)
3077 return isl_stat_error;
3078 if (!ok)
3079 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3080 "incompatible spaces", return isl_stat_error);
3082 return isl_stat_ok;
3085 struct isl_basic_map *isl_basic_map_intersect_range(
3086 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3088 struct isl_basic_map *bmap_range;
3090 if (!bmap || !bset)
3091 goto error;
3093 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3094 bset->dim, isl_dim_param), goto error);
3096 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3097 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3098 goto error;
3100 if (isl_basic_set_plain_is_universe(bset)) {
3101 isl_basic_set_free(bset);
3102 return bmap;
3105 bmap = isl_basic_map_cow(bmap);
3106 if (!bmap)
3107 goto error;
3108 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3109 bset->n_div, bset->n_eq, bset->n_ineq);
3110 bmap_range = bset_to_bmap(bset);
3111 bmap = add_constraints(bmap, bmap_range, 0, 0);
3113 bmap = isl_basic_map_simplify(bmap);
3114 return isl_basic_map_finalize(bmap);
3115 error:
3116 isl_basic_map_free(bmap);
3117 isl_basic_set_free(bset);
3118 return NULL;
3121 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3122 __isl_keep isl_vec *vec)
3124 int i;
3125 unsigned total;
3126 isl_int s;
3128 if (!bmap || !vec)
3129 return isl_bool_error;
3131 total = 1 + isl_basic_map_total_dim(bmap);
3132 if (total != vec->size)
3133 return isl_bool_false;
3135 isl_int_init(s);
3137 for (i = 0; i < bmap->n_eq; ++i) {
3138 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3139 if (!isl_int_is_zero(s)) {
3140 isl_int_clear(s);
3141 return isl_bool_false;
3145 for (i = 0; i < bmap->n_ineq; ++i) {
3146 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3147 if (isl_int_is_neg(s)) {
3148 isl_int_clear(s);
3149 return isl_bool_false;
3153 isl_int_clear(s);
3155 return isl_bool_true;
3158 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3159 __isl_keep isl_vec *vec)
3161 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3164 struct isl_basic_map *isl_basic_map_intersect(
3165 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3167 struct isl_vec *sample = NULL;
3169 if (!bmap1 || !bmap2)
3170 goto error;
3172 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
3173 bmap2->dim, isl_dim_param), goto error);
3174 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3175 isl_space_dim(bmap1->dim, isl_dim_param) &&
3176 isl_space_dim(bmap2->dim, isl_dim_all) !=
3177 isl_space_dim(bmap2->dim, isl_dim_param))
3178 return isl_basic_map_intersect(bmap2, bmap1);
3180 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3181 isl_space_dim(bmap2->dim, isl_dim_param))
3182 isl_assert(bmap1->ctx,
3183 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3185 if (isl_basic_map_plain_is_empty(bmap1)) {
3186 isl_basic_map_free(bmap2);
3187 return bmap1;
3189 if (isl_basic_map_plain_is_empty(bmap2)) {
3190 isl_basic_map_free(bmap1);
3191 return bmap2;
3194 if (bmap1->sample &&
3195 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3196 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3197 sample = isl_vec_copy(bmap1->sample);
3198 else if (bmap2->sample &&
3199 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3200 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3201 sample = isl_vec_copy(bmap2->sample);
3203 bmap1 = isl_basic_map_cow(bmap1);
3204 if (!bmap1)
3205 goto error;
3206 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3207 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3208 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3210 if (!bmap1)
3211 isl_vec_free(sample);
3212 else if (sample) {
3213 isl_vec_free(bmap1->sample);
3214 bmap1->sample = sample;
3217 bmap1 = isl_basic_map_simplify(bmap1);
3218 return isl_basic_map_finalize(bmap1);
3219 error:
3220 if (sample)
3221 isl_vec_free(sample);
3222 isl_basic_map_free(bmap1);
3223 isl_basic_map_free(bmap2);
3224 return NULL;
3227 struct isl_basic_set *isl_basic_set_intersect(
3228 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3230 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3231 bset_to_bmap(bset2)));
3234 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3235 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3237 return isl_basic_set_intersect(bset1, bset2);
3240 /* Special case of isl_map_intersect, where both map1 and map2
3241 * are convex, without any divs and such that either map1 or map2
3242 * contains a single constraint. This constraint is then simply
3243 * added to the other map.
3245 static __isl_give isl_map *map_intersect_add_constraint(
3246 __isl_take isl_map *map1, __isl_take isl_map *map2)
3248 isl_assert(map1->ctx, map1->n == 1, goto error);
3249 isl_assert(map2->ctx, map1->n == 1, goto error);
3250 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3251 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3253 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3254 return isl_map_intersect(map2, map1);
3256 map1 = isl_map_cow(map1);
3257 if (!map1)
3258 goto error;
3259 if (isl_map_plain_is_empty(map1)) {
3260 isl_map_free(map2);
3261 return map1;
3263 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3264 if (map2->p[0]->n_eq == 1)
3265 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3266 else
3267 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3268 map2->p[0]->ineq[0]);
3270 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3271 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3272 if (!map1->p[0])
3273 goto error;
3275 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3276 isl_basic_map_free(map1->p[0]);
3277 map1->n = 0;
3280 isl_map_free(map2);
3282 return map1;
3283 error:
3284 isl_map_free(map1);
3285 isl_map_free(map2);
3286 return NULL;
3289 /* map2 may be either a parameter domain or a map living in the same
3290 * space as map1.
3292 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3293 __isl_take isl_map *map2)
3295 unsigned flags = 0;
3296 isl_map *result;
3297 int i, j;
3299 if (!map1 || !map2)
3300 goto error;
3302 if ((isl_map_plain_is_empty(map1) ||
3303 isl_map_plain_is_universe(map2)) &&
3304 isl_space_is_equal(map1->dim, map2->dim)) {
3305 isl_map_free(map2);
3306 return map1;
3308 if ((isl_map_plain_is_empty(map2) ||
3309 isl_map_plain_is_universe(map1)) &&
3310 isl_space_is_equal(map1->dim, map2->dim)) {
3311 isl_map_free(map1);
3312 return map2;
3315 if (map1->n == 1 && map2->n == 1 &&
3316 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3317 isl_space_is_equal(map1->dim, map2->dim) &&
3318 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3319 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3320 return map_intersect_add_constraint(map1, map2);
3322 if (isl_space_dim(map2->dim, isl_dim_all) !=
3323 isl_space_dim(map2->dim, isl_dim_param))
3324 isl_assert(map1->ctx,
3325 isl_space_is_equal(map1->dim, map2->dim), goto error);
3327 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3328 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3329 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3331 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3332 map1->n * map2->n, flags);
3333 if (!result)
3334 goto error;
3335 for (i = 0; i < map1->n; ++i)
3336 for (j = 0; j < map2->n; ++j) {
3337 struct isl_basic_map *part;
3338 part = isl_basic_map_intersect(
3339 isl_basic_map_copy(map1->p[i]),
3340 isl_basic_map_copy(map2->p[j]));
3341 if (isl_basic_map_is_empty(part) < 0)
3342 part = isl_basic_map_free(part);
3343 result = isl_map_add_basic_map(result, part);
3344 if (!result)
3345 goto error;
3347 isl_map_free(map1);
3348 isl_map_free(map2);
3349 return result;
3350 error:
3351 isl_map_free(map1);
3352 isl_map_free(map2);
3353 return NULL;
3356 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3357 __isl_take isl_map *map2)
3359 if (!map1 || !map2)
3360 goto error;
3361 if (!isl_space_is_equal(map1->dim, map2->dim))
3362 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3363 "spaces don't match", goto error);
3364 return map_intersect_internal(map1, map2);
3365 error:
3366 isl_map_free(map1);
3367 isl_map_free(map2);
3368 return NULL;
3371 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3372 __isl_take isl_map *map2)
3374 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3377 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3379 return set_from_map(isl_map_intersect(set_to_map(set1),
3380 set_to_map(set2)));
3383 /* map_intersect_internal accepts intersections
3384 * with parameter domains, so we can just call that function.
3386 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3387 __isl_take isl_set *params)
3389 return map_intersect_internal(map, params);
3392 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3393 __isl_take isl_map *map2)
3395 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3398 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3399 __isl_take isl_set *params)
3401 return isl_map_intersect_params(set, params);
3404 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3406 isl_space *space;
3407 unsigned pos, n1, n2;
3409 if (!bmap)
3410 return NULL;
3411 bmap = isl_basic_map_cow(bmap);
3412 if (!bmap)
3413 return NULL;
3414 space = isl_space_reverse(isl_space_copy(bmap->dim));
3415 pos = isl_basic_map_offset(bmap, isl_dim_in);
3416 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3417 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3418 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3419 return isl_basic_map_reset_space(bmap, space);
3422 static __isl_give isl_basic_map *basic_map_space_reset(
3423 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3425 isl_space *space;
3427 if (!bmap)
3428 return NULL;
3429 if (!isl_space_is_named_or_nested(bmap->dim, type))
3430 return bmap;
3432 space = isl_basic_map_get_space(bmap);
3433 space = isl_space_reset(space, type);
3434 bmap = isl_basic_map_reset_space(bmap, space);
3435 return bmap;
3438 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3439 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3440 unsigned pos, unsigned n)
3442 isl_bool rational;
3443 isl_space *res_dim;
3444 struct isl_basic_map *res;
3445 struct isl_dim_map *dim_map;
3446 unsigned total, off;
3447 enum isl_dim_type t;
3449 if (n == 0)
3450 return basic_map_space_reset(bmap, type);
3452 if (!bmap)
3453 return NULL;
3455 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3457 total = isl_basic_map_total_dim(bmap) + n;
3458 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3459 off = 0;
3460 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3461 if (t != type) {
3462 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3463 } else {
3464 unsigned size = isl_basic_map_dim(bmap, t);
3465 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3466 0, pos, off);
3467 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3468 pos, size - pos, off + pos + n);
3470 off += isl_space_dim(res_dim, t);
3472 isl_dim_map_div(dim_map, bmap, off);
3474 res = isl_basic_map_alloc_space(res_dim,
3475 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3476 rational = isl_basic_map_is_rational(bmap);
3477 if (rational < 0)
3478 res = isl_basic_map_free(res);
3479 if (rational)
3480 res = isl_basic_map_set_rational(res);
3481 if (isl_basic_map_plain_is_empty(bmap)) {
3482 isl_basic_map_free(bmap);
3483 free(dim_map);
3484 return isl_basic_map_set_to_empty(res);
3486 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3487 return isl_basic_map_finalize(res);
3490 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3491 __isl_take isl_basic_set *bset,
3492 enum isl_dim_type type, unsigned pos, unsigned n)
3494 return isl_basic_map_insert_dims(bset, type, pos, n);
3497 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3498 enum isl_dim_type type, unsigned n)
3500 if (!bmap)
3501 return NULL;
3502 return isl_basic_map_insert_dims(bmap, type,
3503 isl_basic_map_dim(bmap, type), n);
3506 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3507 enum isl_dim_type type, unsigned n)
3509 if (!bset)
3510 return NULL;
3511 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3512 return isl_basic_map_add_dims(bset, type, n);
3513 error:
3514 isl_basic_set_free(bset);
3515 return NULL;
3518 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3519 enum isl_dim_type type)
3521 isl_space *space;
3523 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3524 return map;
3526 space = isl_map_get_space(map);
3527 space = isl_space_reset(space, type);
3528 map = isl_map_reset_space(map, space);
3529 return map;
3532 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3533 enum isl_dim_type type, unsigned pos, unsigned n)
3535 int i;
3537 if (n == 0)
3538 return map_space_reset(map, type);
3540 map = isl_map_cow(map);
3541 if (!map)
3542 return NULL;
3544 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3545 if (!map->dim)
3546 goto error;
3548 for (i = 0; i < map->n; ++i) {
3549 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3550 if (!map->p[i])
3551 goto error;
3554 return map;
3555 error:
3556 isl_map_free(map);
3557 return NULL;
3560 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3561 enum isl_dim_type type, unsigned pos, unsigned n)
3563 return isl_map_insert_dims(set, type, pos, n);
3566 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3567 enum isl_dim_type type, unsigned n)
3569 if (!map)
3570 return NULL;
3571 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3574 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3575 enum isl_dim_type type, unsigned n)
3577 if (!set)
3578 return NULL;
3579 isl_assert(set->ctx, type != isl_dim_in, goto error);
3580 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3581 error:
3582 isl_set_free(set);
3583 return NULL;
3586 __isl_give isl_basic_map *isl_basic_map_move_dims(
3587 __isl_take isl_basic_map *bmap,
3588 enum isl_dim_type dst_type, unsigned dst_pos,
3589 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3591 struct isl_dim_map *dim_map;
3592 struct isl_basic_map *res;
3593 enum isl_dim_type t;
3594 unsigned total, off;
3596 if (!bmap)
3597 return NULL;
3598 if (n == 0)
3599 return bmap;
3601 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3602 return isl_basic_map_free(bmap);
3604 if (dst_type == src_type && dst_pos == src_pos)
3605 return bmap;
3607 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3609 if (pos(bmap->dim, dst_type) + dst_pos ==
3610 pos(bmap->dim, src_type) + src_pos +
3611 ((src_type < dst_type) ? n : 0)) {
3612 bmap = isl_basic_map_cow(bmap);
3613 if (!bmap)
3614 return NULL;
3616 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3617 src_type, src_pos, n);
3618 if (!bmap->dim)
3619 goto error;
3621 bmap = isl_basic_map_finalize(bmap);
3623 return bmap;
3626 total = isl_basic_map_total_dim(bmap);
3627 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3629 off = 0;
3630 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3631 unsigned size = isl_space_dim(bmap->dim, t);
3632 if (t == dst_type) {
3633 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3634 0, dst_pos, off);
3635 off += dst_pos;
3636 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3637 src_pos, n, off);
3638 off += n;
3639 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3640 dst_pos, size - dst_pos, off);
3641 off += size - dst_pos;
3642 } else if (t == src_type) {
3643 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3644 0, src_pos, off);
3645 off += src_pos;
3646 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3647 src_pos + n, size - src_pos - n, off);
3648 off += size - src_pos - n;
3649 } else {
3650 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3651 off += size;
3654 isl_dim_map_div(dim_map, bmap, off);
3656 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3657 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3658 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3659 if (!bmap)
3660 goto error;
3662 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3663 src_type, src_pos, n);
3664 if (!bmap->dim)
3665 goto error;
3667 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3668 bmap = isl_basic_map_gauss(bmap, NULL);
3669 bmap = isl_basic_map_finalize(bmap);
3671 return bmap;
3672 error:
3673 isl_basic_map_free(bmap);
3674 return NULL;
3677 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3678 enum isl_dim_type dst_type, unsigned dst_pos,
3679 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3681 isl_basic_map *bmap = bset_to_bmap(bset);
3682 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3683 src_type, src_pos, n);
3684 return bset_from_bmap(bmap);
3687 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3688 enum isl_dim_type dst_type, unsigned dst_pos,
3689 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3691 if (!set)
3692 return NULL;
3693 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3694 return set_from_map(isl_map_move_dims(set_to_map(set),
3695 dst_type, dst_pos, src_type, src_pos, n));
3696 error:
3697 isl_set_free(set);
3698 return NULL;
3701 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3702 enum isl_dim_type dst_type, unsigned dst_pos,
3703 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3705 int i;
3707 if (!map)
3708 return NULL;
3709 if (n == 0)
3710 return map;
3712 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3713 goto error);
3715 if (dst_type == src_type && dst_pos == src_pos)
3716 return map;
3718 isl_assert(map->ctx, dst_type != src_type, goto error);
3720 map = isl_map_cow(map);
3721 if (!map)
3722 return NULL;
3724 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3725 if (!map->dim)
3726 goto error;
3728 for (i = 0; i < map->n; ++i) {
3729 map->p[i] = isl_basic_map_move_dims(map->p[i],
3730 dst_type, dst_pos,
3731 src_type, src_pos, n);
3732 if (!map->p[i])
3733 goto error;
3736 return map;
3737 error:
3738 isl_map_free(map);
3739 return NULL;
3742 /* Move the specified dimensions to the last columns right before
3743 * the divs. Don't change the dimension specification of bmap.
3744 * That's the responsibility of the caller.
3746 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3747 enum isl_dim_type type, unsigned first, unsigned n)
3749 struct isl_dim_map *dim_map;
3750 struct isl_basic_map *res;
3751 enum isl_dim_type t;
3752 unsigned total, off;
3754 if (!bmap)
3755 return NULL;
3756 if (pos(bmap->dim, type) + first + n ==
3757 1 + isl_space_dim(bmap->dim, isl_dim_all))
3758 return bmap;
3760 total = isl_basic_map_total_dim(bmap);
3761 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3763 off = 0;
3764 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3765 unsigned size = isl_space_dim(bmap->dim, t);
3766 if (t == type) {
3767 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3768 0, first, off);
3769 off += first;
3770 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3771 first, n, total - bmap->n_div - n);
3772 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3773 first + n, size - (first + n), off);
3774 off += size - (first + n);
3775 } else {
3776 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3777 off += size;
3780 isl_dim_map_div(dim_map, bmap, off + n);
3782 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3783 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3784 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3785 return res;
3788 /* Insert "n" rows in the divs of "bmap".
3790 * The number of columns is not changed, which means that the last
3791 * dimensions of "bmap" are being reintepreted as the new divs.
3792 * The space of "bmap" is not adjusted, however, which means
3793 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3794 * from the space of "bmap" is the responsibility of the caller.
3796 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3797 int n)
3799 int i;
3800 size_t row_size;
3801 isl_int **new_div;
3802 isl_int *old;
3804 bmap = isl_basic_map_cow(bmap);
3805 if (!bmap)
3806 return NULL;
3808 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3809 old = bmap->block2.data;
3810 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3811 (bmap->extra + n) * (1 + row_size));
3812 if (!bmap->block2.data)
3813 return isl_basic_map_free(bmap);
3814 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3815 if (!new_div)
3816 return isl_basic_map_free(bmap);
3817 for (i = 0; i < n; ++i) {
3818 new_div[i] = bmap->block2.data +
3819 (bmap->extra + i) * (1 + row_size);
3820 isl_seq_clr(new_div[i], 1 + row_size);
3822 for (i = 0; i < bmap->extra; ++i)
3823 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3824 free(bmap->div);
3825 bmap->div = new_div;
3826 bmap->n_div += n;
3827 bmap->extra += n;
3829 return bmap;
3832 /* Drop constraints from "bmap" that only involve the variables
3833 * of "type" in the range [first, first + n] that are not related
3834 * to any of the variables outside that interval.
3835 * These constraints cannot influence the values for the variables
3836 * outside the interval, except in case they cause "bmap" to be empty.
3837 * Only drop the constraints if "bmap" is known to be non-empty.
3839 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3840 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3841 unsigned first, unsigned n)
3843 int i;
3844 int *groups;
3845 unsigned dim, n_div;
3846 isl_bool non_empty;
3848 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3849 if (non_empty < 0)
3850 return isl_basic_map_free(bmap);
3851 if (!non_empty)
3852 return bmap;
3854 dim = isl_basic_map_dim(bmap, isl_dim_all);
3855 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3856 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3857 if (!groups)
3858 return isl_basic_map_free(bmap);
3859 first += isl_basic_map_offset(bmap, type) - 1;
3860 for (i = 0; i < first; ++i)
3861 groups[i] = -1;
3862 for (i = first + n; i < dim - n_div; ++i)
3863 groups[i] = -1;
3865 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3867 return bmap;
3870 /* Turn the n dimensions of type type, starting at first
3871 * into existentially quantified variables.
3873 * If a subset of the projected out variables are unrelated
3874 * to any of the variables that remain, then the constraints
3875 * involving this subset are simply dropped first.
3877 __isl_give isl_basic_map *isl_basic_map_project_out(
3878 __isl_take isl_basic_map *bmap,
3879 enum isl_dim_type type, unsigned first, unsigned n)
3881 if (n == 0)
3882 return basic_map_space_reset(bmap, type);
3883 if (type == isl_dim_div)
3884 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3885 "cannot project out existentially quantified variables",
3886 return isl_basic_map_free(bmap));
3888 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3889 if (!bmap)
3890 return NULL;
3892 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3893 return isl_basic_map_remove_dims(bmap, type, first, n);
3895 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3896 return isl_basic_map_free(bmap);
3898 bmap = move_last(bmap, type, first, n);
3899 bmap = isl_basic_map_cow(bmap);
3900 bmap = insert_div_rows(bmap, n);
3901 if (!bmap)
3902 return NULL;
3904 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3905 if (!bmap->dim)
3906 goto error;
3907 bmap = isl_basic_map_simplify(bmap);
3908 bmap = isl_basic_map_drop_redundant_divs(bmap);
3909 return isl_basic_map_finalize(bmap);
3910 error:
3911 isl_basic_map_free(bmap);
3912 return NULL;
3915 /* Turn the n dimensions of type type, starting at first
3916 * into existentially quantified variables.
3918 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3919 enum isl_dim_type type, unsigned first, unsigned n)
3921 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3922 type, first, n));
3925 /* Turn the n dimensions of type type, starting at first
3926 * into existentially quantified variables.
3928 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3929 enum isl_dim_type type, unsigned first, unsigned n)
3931 int i;
3933 if (!map)
3934 return NULL;
3936 if (n == 0)
3937 return map_space_reset(map, type);
3939 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3941 map = isl_map_cow(map);
3942 if (!map)
3943 return NULL;
3945 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3946 if (!map->dim)
3947 goto error;
3949 for (i = 0; i < map->n; ++i) {
3950 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3951 if (!map->p[i])
3952 goto error;
3955 return map;
3956 error:
3957 isl_map_free(map);
3958 return NULL;
3961 /* Turn the n dimensions of type type, starting at first
3962 * into existentially quantified variables.
3964 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3965 enum isl_dim_type type, unsigned first, unsigned n)
3967 return set_from_map(isl_map_project_out(set_to_map(set),
3968 type, first, n));
3971 /* Return a map that projects the elements in "set" onto their
3972 * "n" set dimensions starting at "first".
3973 * "type" should be equal to isl_dim_set.
3975 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3976 enum isl_dim_type type, unsigned first, unsigned n)
3978 int i;
3979 int dim;
3980 isl_map *map;
3982 if (!set)
3983 return NULL;
3984 if (type != isl_dim_set)
3985 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3986 "only set dimensions can be projected out", goto error);
3987 dim = isl_set_dim(set, isl_dim_set);
3988 if (first + n > dim || first + n < first)
3989 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3990 "index out of bounds", goto error);
3992 map = isl_map_from_domain(set);
3993 map = isl_map_add_dims(map, isl_dim_out, n);
3994 for (i = 0; i < n; ++i)
3995 map = isl_map_equate(map, isl_dim_in, first + i,
3996 isl_dim_out, i);
3997 return map;
3998 error:
3999 isl_set_free(set);
4000 return NULL;
4003 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4005 int i, j;
4007 for (i = 0; i < n; ++i) {
4008 j = isl_basic_map_alloc_div(bmap);
4009 if (j < 0)
4010 goto error;
4011 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4013 return bmap;
4014 error:
4015 isl_basic_map_free(bmap);
4016 return NULL;
4019 struct isl_basic_map *isl_basic_map_apply_range(
4020 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4022 isl_space *dim_result = NULL;
4023 struct isl_basic_map *bmap;
4024 unsigned n_in, n_out, n, nparam, total, pos;
4025 struct isl_dim_map *dim_map1, *dim_map2;
4027 if (!bmap1 || !bmap2)
4028 goto error;
4029 if (!isl_space_match(bmap1->dim, isl_dim_param,
4030 bmap2->dim, isl_dim_param))
4031 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4032 "parameters don't match", goto error);
4033 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4034 bmap2->dim, isl_dim_in))
4035 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4036 "spaces don't match", goto error);
4038 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4039 isl_space_copy(bmap2->dim));
4041 n_in = isl_basic_map_n_in(bmap1);
4042 n_out = isl_basic_map_n_out(bmap2);
4043 n = isl_basic_map_n_out(bmap1);
4044 nparam = isl_basic_map_n_param(bmap1);
4046 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4047 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4048 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4049 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4050 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4051 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4052 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4053 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4054 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4055 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4056 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4058 bmap = isl_basic_map_alloc_space(dim_result,
4059 bmap1->n_div + bmap2->n_div + n,
4060 bmap1->n_eq + bmap2->n_eq,
4061 bmap1->n_ineq + bmap2->n_ineq);
4062 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4063 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4064 bmap = add_divs(bmap, n);
4065 bmap = isl_basic_map_simplify(bmap);
4066 bmap = isl_basic_map_drop_redundant_divs(bmap);
4067 return isl_basic_map_finalize(bmap);
4068 error:
4069 isl_basic_map_free(bmap1);
4070 isl_basic_map_free(bmap2);
4071 return NULL;
4074 struct isl_basic_set *isl_basic_set_apply(
4075 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4077 if (!bset || !bmap)
4078 goto error;
4080 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4081 goto error);
4083 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4084 bmap));
4085 error:
4086 isl_basic_set_free(bset);
4087 isl_basic_map_free(bmap);
4088 return NULL;
4091 struct isl_basic_map *isl_basic_map_apply_domain(
4092 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4094 if (!bmap1 || !bmap2)
4095 goto error;
4097 if (!isl_space_match(bmap1->dim, isl_dim_param,
4098 bmap2->dim, isl_dim_param))
4099 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4100 "parameters don't match", goto error);
4101 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4102 bmap2->dim, isl_dim_in))
4103 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4104 "spaces don't match", goto error);
4106 bmap1 = isl_basic_map_reverse(bmap1);
4107 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4108 return isl_basic_map_reverse(bmap1);
4109 error:
4110 isl_basic_map_free(bmap1);
4111 isl_basic_map_free(bmap2);
4112 return NULL;
4115 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4116 * A \cap B -> f(A) + f(B)
4118 struct isl_basic_map *isl_basic_map_sum(
4119 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4121 unsigned n_in, n_out, nparam, total, pos;
4122 struct isl_basic_map *bmap = NULL;
4123 struct isl_dim_map *dim_map1, *dim_map2;
4124 int i;
4126 if (!bmap1 || !bmap2)
4127 goto error;
4129 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4130 goto error);
4132 nparam = isl_basic_map_n_param(bmap1);
4133 n_in = isl_basic_map_n_in(bmap1);
4134 n_out = isl_basic_map_n_out(bmap1);
4136 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4137 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4138 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4139 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4140 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4141 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4142 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4143 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4144 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4145 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4146 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4148 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4149 bmap1->n_div + bmap2->n_div + 2 * n_out,
4150 bmap1->n_eq + bmap2->n_eq + n_out,
4151 bmap1->n_ineq + bmap2->n_ineq);
4152 for (i = 0; i < n_out; ++i) {
4153 int j = isl_basic_map_alloc_equality(bmap);
4154 if (j < 0)
4155 goto error;
4156 isl_seq_clr(bmap->eq[j], 1+total);
4157 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4158 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4159 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4161 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4162 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4163 bmap = add_divs(bmap, 2 * n_out);
4165 bmap = isl_basic_map_simplify(bmap);
4166 return isl_basic_map_finalize(bmap);
4167 error:
4168 isl_basic_map_free(bmap);
4169 isl_basic_map_free(bmap1);
4170 isl_basic_map_free(bmap2);
4171 return NULL;
4174 /* Given two maps A -> f(A) and B -> g(B), construct a map
4175 * A \cap B -> f(A) + f(B)
4177 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4179 struct isl_map *result;
4180 int i, j;
4182 if (!map1 || !map2)
4183 goto error;
4185 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4187 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4188 map1->n * map2->n, 0);
4189 if (!result)
4190 goto error;
4191 for (i = 0; i < map1->n; ++i)
4192 for (j = 0; j < map2->n; ++j) {
4193 struct isl_basic_map *part;
4194 part = isl_basic_map_sum(
4195 isl_basic_map_copy(map1->p[i]),
4196 isl_basic_map_copy(map2->p[j]));
4197 if (isl_basic_map_is_empty(part))
4198 isl_basic_map_free(part);
4199 else
4200 result = isl_map_add_basic_map(result, part);
4201 if (!result)
4202 goto error;
4204 isl_map_free(map1);
4205 isl_map_free(map2);
4206 return result;
4207 error:
4208 isl_map_free(map1);
4209 isl_map_free(map2);
4210 return NULL;
4213 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4214 __isl_take isl_set *set2)
4216 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4219 /* Given a basic map A -> f(A), construct A -> -f(A).
4221 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4223 int i, j;
4224 unsigned off, n;
4226 bmap = isl_basic_map_cow(bmap);
4227 if (!bmap)
4228 return NULL;
4230 n = isl_basic_map_dim(bmap, isl_dim_out);
4231 off = isl_basic_map_offset(bmap, isl_dim_out);
4232 for (i = 0; i < bmap->n_eq; ++i)
4233 for (j = 0; j < n; ++j)
4234 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4235 for (i = 0; i < bmap->n_ineq; ++i)
4236 for (j = 0; j < n; ++j)
4237 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4238 for (i = 0; i < bmap->n_div; ++i)
4239 for (j = 0; j < n; ++j)
4240 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4241 bmap = isl_basic_map_gauss(bmap, NULL);
4242 return isl_basic_map_finalize(bmap);
4245 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4247 return isl_basic_map_neg(bset);
4250 /* Given a map A -> f(A), construct A -> -f(A).
4252 struct isl_map *isl_map_neg(struct isl_map *map)
4254 int i;
4256 map = isl_map_cow(map);
4257 if (!map)
4258 return NULL;
4260 for (i = 0; i < map->n; ++i) {
4261 map->p[i] = isl_basic_map_neg(map->p[i]);
4262 if (!map->p[i])
4263 goto error;
4266 return map;
4267 error:
4268 isl_map_free(map);
4269 return NULL;
4272 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4274 return set_from_map(isl_map_neg(set_to_map(set)));
4277 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4278 * A -> floor(f(A)/d).
4280 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4281 isl_int d)
4283 unsigned n_in, n_out, nparam, total, pos;
4284 struct isl_basic_map *result = NULL;
4285 struct isl_dim_map *dim_map;
4286 int i;
4288 if (!bmap)
4289 return NULL;
4291 nparam = isl_basic_map_n_param(bmap);
4292 n_in = isl_basic_map_n_in(bmap);
4293 n_out = isl_basic_map_n_out(bmap);
4295 total = nparam + n_in + n_out + bmap->n_div + n_out;
4296 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4297 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4298 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4299 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4300 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4302 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4303 bmap->n_div + n_out,
4304 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4305 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4306 result = add_divs(result, n_out);
4307 for (i = 0; i < n_out; ++i) {
4308 int j;
4309 j = isl_basic_map_alloc_inequality(result);
4310 if (j < 0)
4311 goto error;
4312 isl_seq_clr(result->ineq[j], 1+total);
4313 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4314 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4315 j = isl_basic_map_alloc_inequality(result);
4316 if (j < 0)
4317 goto error;
4318 isl_seq_clr(result->ineq[j], 1+total);
4319 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4320 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4321 isl_int_sub_ui(result->ineq[j][0], d, 1);
4324 result = isl_basic_map_simplify(result);
4325 return isl_basic_map_finalize(result);
4326 error:
4327 isl_basic_map_free(result);
4328 return NULL;
4331 /* Given a map A -> f(A) and an integer d, construct a map
4332 * A -> floor(f(A)/d).
4334 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4336 int i;
4338 map = isl_map_cow(map);
4339 if (!map)
4340 return NULL;
4342 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4343 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4344 for (i = 0; i < map->n; ++i) {
4345 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4346 if (!map->p[i])
4347 goto error;
4350 return map;
4351 error:
4352 isl_map_free(map);
4353 return NULL;
4356 /* Given a map A -> f(A) and an integer d, construct a map
4357 * A -> floor(f(A)/d).
4359 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4360 __isl_take isl_val *d)
4362 if (!map || !d)
4363 goto error;
4364 if (!isl_val_is_int(d))
4365 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4366 "expecting integer denominator", goto error);
4367 map = isl_map_floordiv(map, d->n);
4368 isl_val_free(d);
4369 return map;
4370 error:
4371 isl_map_free(map);
4372 isl_val_free(d);
4373 return NULL;
4376 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4378 int i;
4379 unsigned nparam;
4380 unsigned n_in;
4382 i = isl_basic_map_alloc_equality(bmap);
4383 if (i < 0)
4384 goto error;
4385 nparam = isl_basic_map_n_param(bmap);
4386 n_in = isl_basic_map_n_in(bmap);
4387 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4388 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4389 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4390 return isl_basic_map_finalize(bmap);
4391 error:
4392 isl_basic_map_free(bmap);
4393 return NULL;
4396 /* Add a constraint to "bmap" expressing i_pos < o_pos
4398 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4400 int i;
4401 unsigned nparam;
4402 unsigned n_in;
4404 i = isl_basic_map_alloc_inequality(bmap);
4405 if (i < 0)
4406 goto error;
4407 nparam = isl_basic_map_n_param(bmap);
4408 n_in = isl_basic_map_n_in(bmap);
4409 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4410 isl_int_set_si(bmap->ineq[i][0], -1);
4411 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4412 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4413 return isl_basic_map_finalize(bmap);
4414 error:
4415 isl_basic_map_free(bmap);
4416 return NULL;
4419 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4421 static __isl_give isl_basic_map *var_less_or_equal(
4422 __isl_take isl_basic_map *bmap, unsigned pos)
4424 int i;
4425 unsigned nparam;
4426 unsigned n_in;
4428 i = isl_basic_map_alloc_inequality(bmap);
4429 if (i < 0)
4430 goto error;
4431 nparam = isl_basic_map_n_param(bmap);
4432 n_in = isl_basic_map_n_in(bmap);
4433 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4434 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4435 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4436 return isl_basic_map_finalize(bmap);
4437 error:
4438 isl_basic_map_free(bmap);
4439 return NULL;
4442 /* Add a constraint to "bmap" expressing i_pos > o_pos
4444 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4446 int i;
4447 unsigned nparam;
4448 unsigned n_in;
4450 i = isl_basic_map_alloc_inequality(bmap);
4451 if (i < 0)
4452 goto error;
4453 nparam = isl_basic_map_n_param(bmap);
4454 n_in = isl_basic_map_n_in(bmap);
4455 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4456 isl_int_set_si(bmap->ineq[i][0], -1);
4457 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4458 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4459 return isl_basic_map_finalize(bmap);
4460 error:
4461 isl_basic_map_free(bmap);
4462 return NULL;
4465 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4467 static __isl_give isl_basic_map *var_more_or_equal(
4468 __isl_take isl_basic_map *bmap, unsigned pos)
4470 int i;
4471 unsigned nparam;
4472 unsigned n_in;
4474 i = isl_basic_map_alloc_inequality(bmap);
4475 if (i < 0)
4476 goto error;
4477 nparam = isl_basic_map_n_param(bmap);
4478 n_in = isl_basic_map_n_in(bmap);
4479 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4480 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4481 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4482 return isl_basic_map_finalize(bmap);
4483 error:
4484 isl_basic_map_free(bmap);
4485 return NULL;
4488 __isl_give isl_basic_map *isl_basic_map_equal(
4489 __isl_take isl_space *dim, unsigned n_equal)
4491 int i;
4492 struct isl_basic_map *bmap;
4493 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4494 if (!bmap)
4495 return NULL;
4496 for (i = 0; i < n_equal && bmap; ++i)
4497 bmap = var_equal(bmap, i);
4498 return isl_basic_map_finalize(bmap);
4501 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4503 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4504 unsigned pos)
4506 int i;
4507 struct isl_basic_map *bmap;
4508 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4509 if (!bmap)
4510 return NULL;
4511 for (i = 0; i < pos && bmap; ++i)
4512 bmap = var_equal(bmap, i);
4513 if (bmap)
4514 bmap = var_less(bmap, pos);
4515 return isl_basic_map_finalize(bmap);
4518 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4520 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4521 __isl_take isl_space *dim, unsigned pos)
4523 int i;
4524 isl_basic_map *bmap;
4526 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4527 for (i = 0; i < pos; ++i)
4528 bmap = var_equal(bmap, i);
4529 bmap = var_less_or_equal(bmap, pos);
4530 return isl_basic_map_finalize(bmap);
4533 /* Return a relation on "dim" expressing i_pos > o_pos
4535 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4536 unsigned pos)
4538 int i;
4539 struct isl_basic_map *bmap;
4540 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4541 if (!bmap)
4542 return NULL;
4543 for (i = 0; i < pos && bmap; ++i)
4544 bmap = var_equal(bmap, i);
4545 if (bmap)
4546 bmap = var_more(bmap, pos);
4547 return isl_basic_map_finalize(bmap);
4550 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4552 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4553 __isl_take isl_space *dim, unsigned pos)
4555 int i;
4556 isl_basic_map *bmap;
4558 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4559 for (i = 0; i < pos; ++i)
4560 bmap = var_equal(bmap, i);
4561 bmap = var_more_or_equal(bmap, pos);
4562 return isl_basic_map_finalize(bmap);
4565 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4566 unsigned n, int equal)
4568 struct isl_map *map;
4569 int i;
4571 if (n == 0 && equal)
4572 return isl_map_universe(dims);
4574 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4576 for (i = 0; i + 1 < n; ++i)
4577 map = isl_map_add_basic_map(map,
4578 isl_basic_map_less_at(isl_space_copy(dims), i));
4579 if (n > 0) {
4580 if (equal)
4581 map = isl_map_add_basic_map(map,
4582 isl_basic_map_less_or_equal_at(dims, n - 1));
4583 else
4584 map = isl_map_add_basic_map(map,
4585 isl_basic_map_less_at(dims, n - 1));
4586 } else
4587 isl_space_free(dims);
4589 return map;
4592 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4594 if (!dims)
4595 return NULL;
4596 return map_lex_lte_first(dims, dims->n_out, equal);
4599 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4601 return map_lex_lte_first(dim, n, 0);
4604 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4606 return map_lex_lte_first(dim, n, 1);
4609 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4611 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4614 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4616 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4619 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4620 unsigned n, int equal)
4622 struct isl_map *map;
4623 int i;
4625 if (n == 0 && equal)
4626 return isl_map_universe(dims);
4628 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4630 for (i = 0; i + 1 < n; ++i)
4631 map = isl_map_add_basic_map(map,
4632 isl_basic_map_more_at(isl_space_copy(dims), i));
4633 if (n > 0) {
4634 if (equal)
4635 map = isl_map_add_basic_map(map,
4636 isl_basic_map_more_or_equal_at(dims, n - 1));
4637 else
4638 map = isl_map_add_basic_map(map,
4639 isl_basic_map_more_at(dims, n - 1));
4640 } else
4641 isl_space_free(dims);
4643 return map;
4646 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4648 if (!dims)
4649 return NULL;
4650 return map_lex_gte_first(dims, dims->n_out, equal);
4653 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4655 return map_lex_gte_first(dim, n, 0);
4658 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4660 return map_lex_gte_first(dim, n, 1);
4663 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4665 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4668 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4670 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4673 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4674 __isl_take isl_set *set2)
4676 isl_map *map;
4677 map = isl_map_lex_le(isl_set_get_space(set1));
4678 map = isl_map_intersect_domain(map, set1);
4679 map = isl_map_intersect_range(map, set2);
4680 return map;
4683 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4684 __isl_take isl_set *set2)
4686 isl_map *map;
4687 map = isl_map_lex_lt(isl_set_get_space(set1));
4688 map = isl_map_intersect_domain(map, set1);
4689 map = isl_map_intersect_range(map, set2);
4690 return map;
4693 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4694 __isl_take isl_set *set2)
4696 isl_map *map;
4697 map = isl_map_lex_ge(isl_set_get_space(set1));
4698 map = isl_map_intersect_domain(map, set1);
4699 map = isl_map_intersect_range(map, set2);
4700 return map;
4703 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4704 __isl_take isl_set *set2)
4706 isl_map *map;
4707 map = isl_map_lex_gt(isl_set_get_space(set1));
4708 map = isl_map_intersect_domain(map, set1);
4709 map = isl_map_intersect_range(map, set2);
4710 return map;
4713 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4714 __isl_take isl_map *map2)
4716 isl_map *map;
4717 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4718 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4719 map = isl_map_apply_range(map, isl_map_reverse(map2));
4720 return map;
4723 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4724 __isl_take isl_map *map2)
4726 isl_map *map;
4727 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4728 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4729 map = isl_map_apply_range(map, isl_map_reverse(map2));
4730 return map;
4733 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4734 __isl_take isl_map *map2)
4736 isl_map *map;
4737 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4738 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4739 map = isl_map_apply_range(map, isl_map_reverse(map2));
4740 return map;
4743 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4744 __isl_take isl_map *map2)
4746 isl_map *map;
4747 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4748 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4749 map = isl_map_apply_range(map, isl_map_reverse(map2));
4750 return map;
4753 /* For a div d = floor(f/m), add the constraint
4755 * f - m d >= 0
4757 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4758 unsigned pos, isl_int *div)
4760 int i;
4761 unsigned total = isl_basic_map_total_dim(bmap);
4763 i = isl_basic_map_alloc_inequality(bmap);
4764 if (i < 0)
4765 return isl_stat_error;
4766 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4767 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4769 return isl_stat_ok;
4772 /* For a div d = floor(f/m), add the constraint
4774 * -(f-(m-1)) + m d >= 0
4776 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4777 unsigned pos, isl_int *div)
4779 int i;
4780 unsigned total = isl_basic_map_total_dim(bmap);
4782 i = isl_basic_map_alloc_inequality(bmap);
4783 if (i < 0)
4784 return isl_stat_error;
4785 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4786 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4787 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4788 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4790 return isl_stat_ok;
4793 /* For a div d = floor(f/m), add the constraints
4795 * f - m d >= 0
4796 * -(f-(m-1)) + m d >= 0
4798 * Note that the second constraint is the negation of
4800 * f - m d >= m
4802 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4803 unsigned pos, isl_int *div)
4805 if (add_upper_div_constraint(bmap, pos, div) < 0)
4806 return -1;
4807 if (add_lower_div_constraint(bmap, pos, div) < 0)
4808 return -1;
4809 return 0;
4812 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4813 unsigned pos, isl_int *div)
4815 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4816 pos, div);
4819 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4821 unsigned total = isl_basic_map_total_dim(bmap);
4822 unsigned div_pos = total - bmap->n_div + div;
4824 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4825 bmap->div[div]);
4828 /* For each known div d = floor(f/m), add the constraints
4830 * f - m d >= 0
4831 * -(f-(m-1)) + m d >= 0
4833 * Remove duplicate constraints in case of some these div constraints
4834 * already appear in "bmap".
4836 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4837 __isl_take isl_basic_map *bmap)
4839 unsigned n_div;
4841 if (!bmap)
4842 return NULL;
4843 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4844 if (n_div == 0)
4845 return bmap;
4847 bmap = add_known_div_constraints(bmap);
4848 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4849 bmap = isl_basic_map_finalize(bmap);
4850 return bmap;
4853 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4855 * In particular, if this div is of the form d = floor(f/m),
4856 * then add the constraint
4858 * f - m d >= 0
4860 * if sign < 0 or the constraint
4862 * -(f-(m-1)) + m d >= 0
4864 * if sign > 0.
4866 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4867 unsigned div, int sign)
4869 unsigned total;
4870 unsigned div_pos;
4872 if (!bmap)
4873 return -1;
4875 total = isl_basic_map_total_dim(bmap);
4876 div_pos = total - bmap->n_div + div;
4878 if (sign < 0)
4879 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4880 else
4881 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4884 struct isl_basic_set *isl_basic_map_underlying_set(
4885 struct isl_basic_map *bmap)
4887 if (!bmap)
4888 goto error;
4889 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4890 bmap->n_div == 0 &&
4891 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4892 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4893 return bset_from_bmap(bmap);
4894 bmap = isl_basic_map_cow(bmap);
4895 if (!bmap)
4896 goto error;
4897 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4898 if (!bmap->dim)
4899 goto error;
4900 bmap->extra -= bmap->n_div;
4901 bmap->n_div = 0;
4902 bmap = isl_basic_map_finalize(bmap);
4903 return bset_from_bmap(bmap);
4904 error:
4905 isl_basic_map_free(bmap);
4906 return NULL;
4909 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4910 __isl_take isl_basic_set *bset)
4912 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4915 /* Replace each element in "list" by the result of applying
4916 * isl_basic_map_underlying_set to the element.
4918 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4919 __isl_take isl_basic_map_list *list)
4921 int i, n;
4923 if (!list)
4924 return NULL;
4926 n = isl_basic_map_list_n_basic_map(list);
4927 for (i = 0; i < n; ++i) {
4928 isl_basic_map *bmap;
4929 isl_basic_set *bset;
4931 bmap = isl_basic_map_list_get_basic_map(list, i);
4932 bset = isl_basic_set_underlying_set(bmap);
4933 list = isl_basic_set_list_set_basic_set(list, i, bset);
4936 return list;
4939 struct isl_basic_map *isl_basic_map_overlying_set(
4940 struct isl_basic_set *bset, struct isl_basic_map *like)
4942 struct isl_basic_map *bmap;
4943 struct isl_ctx *ctx;
4944 unsigned total;
4945 int i;
4947 if (!bset || !like)
4948 goto error;
4949 ctx = bset->ctx;
4950 isl_assert(ctx, bset->n_div == 0, goto error);
4951 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4952 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4953 goto error);
4954 if (like->n_div == 0) {
4955 isl_space *space = isl_basic_map_get_space(like);
4956 isl_basic_map_free(like);
4957 return isl_basic_map_reset_space(bset, space);
4959 bset = isl_basic_set_cow(bset);
4960 if (!bset)
4961 goto error;
4962 total = bset->dim->n_out + bset->extra;
4963 bmap = bset_to_bmap(bset);
4964 isl_space_free(bmap->dim);
4965 bmap->dim = isl_space_copy(like->dim);
4966 if (!bmap->dim)
4967 goto error;
4968 bmap->n_div = like->n_div;
4969 bmap->extra += like->n_div;
4970 if (bmap->extra) {
4971 unsigned ltotal;
4972 isl_int **div;
4973 ltotal = total - bmap->extra + like->extra;
4974 if (ltotal > total)
4975 ltotal = total;
4976 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4977 bmap->extra * (1 + 1 + total));
4978 if (isl_blk_is_error(bmap->block2))
4979 goto error;
4980 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4981 if (!div)
4982 goto error;
4983 bmap->div = div;
4984 for (i = 0; i < bmap->extra; ++i)
4985 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4986 for (i = 0; i < like->n_div; ++i) {
4987 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4988 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4990 bmap = isl_basic_map_add_known_div_constraints(bmap);
4992 isl_basic_map_free(like);
4993 bmap = isl_basic_map_simplify(bmap);
4994 bmap = isl_basic_map_finalize(bmap);
4995 return bmap;
4996 error:
4997 isl_basic_map_free(like);
4998 isl_basic_set_free(bset);
4999 return NULL;
5002 struct isl_basic_set *isl_basic_set_from_underlying_set(
5003 struct isl_basic_set *bset, struct isl_basic_set *like)
5005 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5006 bset_to_bmap(like)));
5009 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5011 int i;
5013 map = isl_map_cow(map);
5014 if (!map)
5015 return NULL;
5016 map->dim = isl_space_cow(map->dim);
5017 if (!map->dim)
5018 goto error;
5020 for (i = 1; i < map->n; ++i)
5021 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5022 goto error);
5023 for (i = 0; i < map->n; ++i) {
5024 map->p[i] = bset_to_bmap(
5025 isl_basic_map_underlying_set(map->p[i]));
5026 if (!map->p[i])
5027 goto error;
5029 if (map->n == 0)
5030 map->dim = isl_space_underlying(map->dim, 0);
5031 else {
5032 isl_space_free(map->dim);
5033 map->dim = isl_space_copy(map->p[0]->dim);
5035 if (!map->dim)
5036 goto error;
5037 return set_from_map(map);
5038 error:
5039 isl_map_free(map);
5040 return NULL;
5043 /* Replace the space of "bmap" by "space".
5045 * If the space of "bmap" is identical to "space" (including the identifiers
5046 * of the input and output dimensions), then simply return the original input.
5048 __isl_give isl_basic_map *isl_basic_map_reset_space(
5049 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5051 isl_bool equal;
5053 if (!bmap)
5054 goto error;
5055 equal = isl_space_is_equal(bmap->dim, space);
5056 if (equal >= 0 && equal)
5057 equal = isl_space_match(bmap->dim, isl_dim_in,
5058 space, isl_dim_in);
5059 if (equal >= 0 && equal)
5060 equal = isl_space_match(bmap->dim, isl_dim_out,
5061 space, isl_dim_out);
5062 if (equal < 0)
5063 goto error;
5064 if (equal) {
5065 isl_space_free(space);
5066 return bmap;
5068 bmap = isl_basic_map_cow(bmap);
5069 if (!bmap || !space)
5070 goto error;
5072 isl_space_free(bmap->dim);
5073 bmap->dim = space;
5075 bmap = isl_basic_map_finalize(bmap);
5077 return bmap;
5078 error:
5079 isl_basic_map_free(bmap);
5080 isl_space_free(space);
5081 return NULL;
5084 __isl_give isl_basic_set *isl_basic_set_reset_space(
5085 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5087 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5088 dim));
5091 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5092 __isl_take isl_space *dim)
5094 int i;
5096 map = isl_map_cow(map);
5097 if (!map || !dim)
5098 goto error;
5100 for (i = 0; i < map->n; ++i) {
5101 map->p[i] = isl_basic_map_reset_space(map->p[i],
5102 isl_space_copy(dim));
5103 if (!map->p[i])
5104 goto error;
5106 isl_space_free(map->dim);
5107 map->dim = dim;
5109 return map;
5110 error:
5111 isl_map_free(map);
5112 isl_space_free(dim);
5113 return NULL;
5116 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5117 __isl_take isl_space *dim)
5119 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5122 /* Compute the parameter domain of the given basic set.
5124 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5126 isl_bool is_params;
5127 isl_space *space;
5128 unsigned n;
5130 is_params = isl_basic_set_is_params(bset);
5131 if (is_params < 0)
5132 return isl_basic_set_free(bset);
5133 if (is_params)
5134 return bset;
5136 n = isl_basic_set_dim(bset, isl_dim_set);
5137 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5138 space = isl_basic_set_get_space(bset);
5139 space = isl_space_params(space);
5140 bset = isl_basic_set_reset_space(bset, space);
5141 return bset;
5144 /* Construct a zero-dimensional basic set with the given parameter domain.
5146 __isl_give isl_basic_set *isl_basic_set_from_params(
5147 __isl_take isl_basic_set *bset)
5149 isl_space *space;
5150 space = isl_basic_set_get_space(bset);
5151 space = isl_space_set_from_params(space);
5152 bset = isl_basic_set_reset_space(bset, space);
5153 return bset;
5156 /* Compute the parameter domain of the given set.
5158 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5160 isl_space *space;
5161 unsigned n;
5163 if (isl_set_is_params(set))
5164 return set;
5166 n = isl_set_dim(set, isl_dim_set);
5167 set = isl_set_project_out(set, isl_dim_set, 0, n);
5168 space = isl_set_get_space(set);
5169 space = isl_space_params(space);
5170 set = isl_set_reset_space(set, space);
5171 return set;
5174 /* Construct a zero-dimensional set with the given parameter domain.
5176 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5178 isl_space *space;
5179 space = isl_set_get_space(set);
5180 space = isl_space_set_from_params(space);
5181 set = isl_set_reset_space(set, space);
5182 return set;
5185 /* Compute the parameter domain of the given map.
5187 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5189 isl_space *space;
5190 unsigned n;
5192 n = isl_map_dim(map, isl_dim_in);
5193 map = isl_map_project_out(map, isl_dim_in, 0, n);
5194 n = isl_map_dim(map, isl_dim_out);
5195 map = isl_map_project_out(map, isl_dim_out, 0, n);
5196 space = isl_map_get_space(map);
5197 space = isl_space_params(space);
5198 map = isl_map_reset_space(map, space);
5199 return map;
5202 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5204 isl_space *space;
5205 unsigned n_out;
5207 if (!bmap)
5208 return NULL;
5209 space = isl_space_domain(isl_basic_map_get_space(bmap));
5211 n_out = isl_basic_map_n_out(bmap);
5212 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5214 return isl_basic_map_reset_space(bmap, space);
5217 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5219 if (!bmap)
5220 return isl_bool_error;
5221 return isl_space_may_be_set(bmap->dim);
5224 /* Is this basic map actually a set?
5225 * Users should never call this function. Outside of isl,
5226 * the type should indicate whether something is a set or a map.
5228 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5230 if (!bmap)
5231 return isl_bool_error;
5232 return isl_space_is_set(bmap->dim);
5235 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5237 isl_bool is_set;
5239 is_set = isl_basic_map_is_set(bmap);
5240 if (is_set < 0)
5241 goto error;
5242 if (is_set)
5243 return bmap;
5244 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5245 error:
5246 isl_basic_map_free(bmap);
5247 return NULL;
5250 __isl_give isl_basic_map *isl_basic_map_domain_map(
5251 __isl_take isl_basic_map *bmap)
5253 int i;
5254 isl_space *dim;
5255 isl_basic_map *domain;
5256 int nparam, n_in, n_out;
5258 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5259 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5260 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5262 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5263 domain = isl_basic_map_universe(dim);
5265 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5266 bmap = isl_basic_map_apply_range(bmap, domain);
5267 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5269 for (i = 0; i < n_in; ++i)
5270 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5271 isl_dim_out, i);
5273 bmap = isl_basic_map_gauss(bmap, NULL);
5274 return isl_basic_map_finalize(bmap);
5277 __isl_give isl_basic_map *isl_basic_map_range_map(
5278 __isl_take isl_basic_map *bmap)
5280 int i;
5281 isl_space *dim;
5282 isl_basic_map *range;
5283 int nparam, n_in, n_out;
5285 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5286 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5287 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5289 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5290 range = isl_basic_map_universe(dim);
5292 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5293 bmap = isl_basic_map_apply_range(bmap, range);
5294 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5296 for (i = 0; i < n_out; ++i)
5297 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5298 isl_dim_out, i);
5300 bmap = isl_basic_map_gauss(bmap, NULL);
5301 return isl_basic_map_finalize(bmap);
5304 int isl_map_may_be_set(__isl_keep isl_map *map)
5306 if (!map)
5307 return -1;
5308 return isl_space_may_be_set(map->dim);
5311 /* Is this map actually a set?
5312 * Users should never call this function. Outside of isl,
5313 * the type should indicate whether something is a set or a map.
5315 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5317 if (!map)
5318 return isl_bool_error;
5319 return isl_space_is_set(map->dim);
5322 struct isl_set *isl_map_range(struct isl_map *map)
5324 int i;
5325 isl_bool is_set;
5326 struct isl_set *set;
5328 is_set = isl_map_is_set(map);
5329 if (is_set < 0)
5330 goto error;
5331 if (is_set)
5332 return set_from_map(map);
5334 map = isl_map_cow(map);
5335 if (!map)
5336 goto error;
5338 set = set_from_map(map);
5339 set->dim = isl_space_range(set->dim);
5340 if (!set->dim)
5341 goto error;
5342 for (i = 0; i < map->n; ++i) {
5343 set->p[i] = isl_basic_map_range(map->p[i]);
5344 if (!set->p[i])
5345 goto error;
5347 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5348 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5349 return set;
5350 error:
5351 isl_map_free(map);
5352 return NULL;
5355 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5357 int i;
5359 map = isl_map_cow(map);
5360 if (!map)
5361 return NULL;
5363 map->dim = isl_space_domain_map(map->dim);
5364 if (!map->dim)
5365 goto error;
5366 for (i = 0; i < map->n; ++i) {
5367 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5368 if (!map->p[i])
5369 goto error;
5371 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5372 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5373 return map;
5374 error:
5375 isl_map_free(map);
5376 return NULL;
5379 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5381 int i;
5382 isl_space *range_dim;
5384 map = isl_map_cow(map);
5385 if (!map)
5386 return NULL;
5388 range_dim = isl_space_range(isl_map_get_space(map));
5389 range_dim = isl_space_from_range(range_dim);
5390 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5391 map->dim = isl_space_join(map->dim, range_dim);
5392 if (!map->dim)
5393 goto error;
5394 for (i = 0; i < map->n; ++i) {
5395 map->p[i] = isl_basic_map_range_map(map->p[i]);
5396 if (!map->p[i])
5397 goto error;
5399 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5400 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5401 return map;
5402 error:
5403 isl_map_free(map);
5404 return NULL;
5407 /* Given a wrapped map of the form A[B -> C],
5408 * return the map A[B -> C] -> B.
5410 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5412 isl_id *id;
5413 isl_map *map;
5415 if (!set)
5416 return NULL;
5417 if (!isl_set_has_tuple_id(set))
5418 return isl_map_domain_map(isl_set_unwrap(set));
5420 id = isl_set_get_tuple_id(set);
5421 map = isl_map_domain_map(isl_set_unwrap(set));
5422 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5424 return map;
5427 __isl_give isl_basic_map *isl_basic_map_from_domain(
5428 __isl_take isl_basic_set *bset)
5430 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5433 __isl_give isl_basic_map *isl_basic_map_from_range(
5434 __isl_take isl_basic_set *bset)
5436 isl_space *space;
5437 space = isl_basic_set_get_space(bset);
5438 space = isl_space_from_range(space);
5439 bset = isl_basic_set_reset_space(bset, space);
5440 return bset_to_bmap(bset);
5443 /* Create a relation with the given set as range.
5444 * The domain of the created relation is a zero-dimensional
5445 * flat anonymous space.
5447 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5449 isl_space *space;
5450 space = isl_set_get_space(set);
5451 space = isl_space_from_range(space);
5452 set = isl_set_reset_space(set, space);
5453 return set_to_map(set);
5456 /* Create a relation with the given set as domain.
5457 * The range of the created relation is a zero-dimensional
5458 * flat anonymous space.
5460 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5462 return isl_map_reverse(isl_map_from_range(set));
5465 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5466 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5468 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5471 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5472 __isl_take isl_set *range)
5474 return isl_map_apply_range(isl_map_reverse(domain), range);
5477 /* Return a newly allocated isl_map with given space and flags and
5478 * room for "n" basic maps.
5479 * Make sure that all cached information is cleared.
5481 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5482 unsigned flags)
5484 struct isl_map *map;
5486 if (!space)
5487 return NULL;
5488 if (n < 0)
5489 isl_die(space->ctx, isl_error_internal,
5490 "negative number of basic maps", goto error);
5491 map = isl_calloc(space->ctx, struct isl_map,
5492 sizeof(struct isl_map) +
5493 (n - 1) * sizeof(struct isl_basic_map *));
5494 if (!map)
5495 goto error;
5497 map->ctx = space->ctx;
5498 isl_ctx_ref(map->ctx);
5499 map->ref = 1;
5500 map->size = n;
5501 map->n = 0;
5502 map->dim = space;
5503 map->flags = flags;
5504 return map;
5505 error:
5506 isl_space_free(space);
5507 return NULL;
5510 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5512 struct isl_basic_map *bmap;
5513 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5514 bmap = isl_basic_map_set_to_empty(bmap);
5515 return bmap;
5518 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5520 struct isl_basic_set *bset;
5521 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5522 bset = isl_basic_set_set_to_empty(bset);
5523 return bset;
5526 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5528 struct isl_basic_map *bmap;
5529 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5530 bmap = isl_basic_map_finalize(bmap);
5531 return bmap;
5534 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5536 struct isl_basic_set *bset;
5537 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5538 bset = isl_basic_set_finalize(bset);
5539 return bset;
5542 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5544 int i;
5545 unsigned total = isl_space_dim(dim, isl_dim_all);
5546 isl_basic_map *bmap;
5548 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5549 for (i = 0; i < total; ++i) {
5550 int k = isl_basic_map_alloc_inequality(bmap);
5551 if (k < 0)
5552 goto error;
5553 isl_seq_clr(bmap->ineq[k], 1 + total);
5554 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5556 return bmap;
5557 error:
5558 isl_basic_map_free(bmap);
5559 return NULL;
5562 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5564 return isl_basic_map_nat_universe(dim);
5567 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5569 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5572 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5574 return isl_map_nat_universe(dim);
5577 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5579 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5582 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5584 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5587 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5589 struct isl_map *map;
5590 if (!dim)
5591 return NULL;
5592 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5593 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5594 return map;
5597 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5599 struct isl_set *set;
5600 if (!dim)
5601 return NULL;
5602 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5603 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5604 return set;
5607 struct isl_map *isl_map_dup(struct isl_map *map)
5609 int i;
5610 struct isl_map *dup;
5612 if (!map)
5613 return NULL;
5614 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5615 for (i = 0; i < map->n; ++i)
5616 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5617 return dup;
5620 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5621 __isl_take isl_basic_map *bmap)
5623 if (!bmap || !map)
5624 goto error;
5625 if (isl_basic_map_plain_is_empty(bmap)) {
5626 isl_basic_map_free(bmap);
5627 return map;
5629 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5630 isl_assert(map->ctx, map->n < map->size, goto error);
5631 map->p[map->n] = bmap;
5632 map->n++;
5633 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5634 return map;
5635 error:
5636 if (map)
5637 isl_map_free(map);
5638 if (bmap)
5639 isl_basic_map_free(bmap);
5640 return NULL;
5643 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5645 int i;
5647 if (!map)
5648 return NULL;
5650 if (--map->ref > 0)
5651 return NULL;
5653 clear_caches(map);
5654 isl_ctx_deref(map->ctx);
5655 for (i = 0; i < map->n; ++i)
5656 isl_basic_map_free(map->p[i]);
5657 isl_space_free(map->dim);
5658 free(map);
5660 return NULL;
5663 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5664 struct isl_basic_map *bmap, unsigned pos, int value)
5666 int j;
5668 bmap = isl_basic_map_cow(bmap);
5669 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5670 j = isl_basic_map_alloc_equality(bmap);
5671 if (j < 0)
5672 goto error;
5673 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5674 isl_int_set_si(bmap->eq[j][pos], -1);
5675 isl_int_set_si(bmap->eq[j][0], value);
5676 bmap = isl_basic_map_simplify(bmap);
5677 return isl_basic_map_finalize(bmap);
5678 error:
5679 isl_basic_map_free(bmap);
5680 return NULL;
5683 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5684 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5686 int j;
5688 bmap = isl_basic_map_cow(bmap);
5689 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5690 j = isl_basic_map_alloc_equality(bmap);
5691 if (j < 0)
5692 goto error;
5693 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5694 isl_int_set_si(bmap->eq[j][pos], -1);
5695 isl_int_set(bmap->eq[j][0], value);
5696 bmap = isl_basic_map_simplify(bmap);
5697 return isl_basic_map_finalize(bmap);
5698 error:
5699 isl_basic_map_free(bmap);
5700 return NULL;
5703 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5704 enum isl_dim_type type, unsigned pos, int value)
5706 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5707 return isl_basic_map_free(bmap);
5708 return isl_basic_map_fix_pos_si(bmap,
5709 isl_basic_map_offset(bmap, type) + pos, value);
5712 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5713 enum isl_dim_type type, unsigned pos, isl_int value)
5715 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5716 return isl_basic_map_free(bmap);
5717 return isl_basic_map_fix_pos(bmap,
5718 isl_basic_map_offset(bmap, type) + pos, value);
5721 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5722 * to be equal to "v".
5724 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5725 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5727 if (!bmap || !v)
5728 goto error;
5729 if (!isl_val_is_int(v))
5730 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5731 "expecting integer value", goto error);
5732 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5733 goto error;
5734 pos += isl_basic_map_offset(bmap, type);
5735 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5736 isl_val_free(v);
5737 return bmap;
5738 error:
5739 isl_basic_map_free(bmap);
5740 isl_val_free(v);
5741 return NULL;
5744 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5745 * to be equal to "v".
5747 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5748 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5750 return isl_basic_map_fix_val(bset, type, pos, v);
5753 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5754 enum isl_dim_type type, unsigned pos, int value)
5756 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5757 type, pos, value));
5760 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5761 enum isl_dim_type type, unsigned pos, isl_int value)
5763 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5764 type, pos, value));
5767 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5768 unsigned input, int value)
5770 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5773 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5774 unsigned dim, int value)
5776 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5777 isl_dim_set, dim, value));
5780 static int remove_if_empty(__isl_keep isl_map *map, int i)
5782 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5784 if (empty < 0)
5785 return -1;
5786 if (!empty)
5787 return 0;
5789 isl_basic_map_free(map->p[i]);
5790 if (i != map->n - 1) {
5791 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5792 map->p[i] = map->p[map->n - 1];
5794 map->n--;
5796 return 0;
5799 /* Perform "fn" on each basic map of "map", where we may not be holding
5800 * the only reference to "map".
5801 * In particular, "fn" should be a semantics preserving operation
5802 * that we want to apply to all copies of "map". We therefore need
5803 * to be careful not to modify "map" in a way that breaks "map"
5804 * in case anything goes wrong.
5806 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5807 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5809 struct isl_basic_map *bmap;
5810 int i;
5812 if (!map)
5813 return NULL;
5815 for (i = map->n - 1; i >= 0; --i) {
5816 bmap = isl_basic_map_copy(map->p[i]);
5817 bmap = fn(bmap);
5818 if (!bmap)
5819 goto error;
5820 isl_basic_map_free(map->p[i]);
5821 map->p[i] = bmap;
5822 if (remove_if_empty(map, i) < 0)
5823 goto error;
5826 return map;
5827 error:
5828 isl_map_free(map);
5829 return NULL;
5832 struct isl_map *isl_map_fix_si(struct isl_map *map,
5833 enum isl_dim_type type, unsigned pos, int value)
5835 int i;
5837 map = isl_map_cow(map);
5838 if (!map)
5839 return NULL;
5841 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5842 for (i = map->n - 1; i >= 0; --i) {
5843 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5844 if (remove_if_empty(map, i) < 0)
5845 goto error;
5847 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5848 return map;
5849 error:
5850 isl_map_free(map);
5851 return NULL;
5854 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5855 enum isl_dim_type type, unsigned pos, int value)
5857 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5860 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5861 enum isl_dim_type type, unsigned pos, isl_int value)
5863 int i;
5865 map = isl_map_cow(map);
5866 if (!map)
5867 return NULL;
5869 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5870 for (i = 0; i < map->n; ++i) {
5871 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5872 if (!map->p[i])
5873 goto error;
5875 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5876 return map;
5877 error:
5878 isl_map_free(map);
5879 return NULL;
5882 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5883 enum isl_dim_type type, unsigned pos, isl_int value)
5885 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5888 /* Fix the value of the variable at position "pos" of type "type" of "map"
5889 * to be equal to "v".
5891 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5892 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5894 int i;
5896 map = isl_map_cow(map);
5897 if (!map || !v)
5898 goto error;
5900 if (!isl_val_is_int(v))
5901 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5902 "expecting integer value", goto error);
5903 if (pos >= isl_map_dim(map, type))
5904 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5905 "index out of bounds", goto error);
5906 for (i = map->n - 1; i >= 0; --i) {
5907 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5908 isl_val_copy(v));
5909 if (remove_if_empty(map, i) < 0)
5910 goto error;
5912 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5913 isl_val_free(v);
5914 return map;
5915 error:
5916 isl_map_free(map);
5917 isl_val_free(v);
5918 return NULL;
5921 /* Fix the value of the variable at position "pos" of type "type" of "set"
5922 * to be equal to "v".
5924 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5925 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5927 return isl_map_fix_val(set, type, pos, v);
5930 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5931 unsigned input, int value)
5933 return isl_map_fix_si(map, isl_dim_in, input, value);
5936 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5938 return set_from_map(isl_map_fix_si(set_to_map(set),
5939 isl_dim_set, dim, value));
5942 static __isl_give isl_basic_map *basic_map_bound_si(
5943 __isl_take isl_basic_map *bmap,
5944 enum isl_dim_type type, unsigned pos, int value, int upper)
5946 int j;
5948 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5949 return isl_basic_map_free(bmap);
5950 pos += isl_basic_map_offset(bmap, type);
5951 bmap = isl_basic_map_cow(bmap);
5952 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5953 j = isl_basic_map_alloc_inequality(bmap);
5954 if (j < 0)
5955 goto error;
5956 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5957 if (upper) {
5958 isl_int_set_si(bmap->ineq[j][pos], -1);
5959 isl_int_set_si(bmap->ineq[j][0], value);
5960 } else {
5961 isl_int_set_si(bmap->ineq[j][pos], 1);
5962 isl_int_set_si(bmap->ineq[j][0], -value);
5964 bmap = isl_basic_map_simplify(bmap);
5965 return isl_basic_map_finalize(bmap);
5966 error:
5967 isl_basic_map_free(bmap);
5968 return NULL;
5971 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5972 __isl_take isl_basic_map *bmap,
5973 enum isl_dim_type type, unsigned pos, int value)
5975 return basic_map_bound_si(bmap, type, pos, value, 0);
5978 /* Constrain the values of the given dimension to be no greater than "value".
5980 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5981 __isl_take isl_basic_map *bmap,
5982 enum isl_dim_type type, unsigned pos, int value)
5984 return basic_map_bound_si(bmap, type, pos, value, 1);
5987 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5988 enum isl_dim_type type, unsigned pos, int value, int upper)
5990 int i;
5992 map = isl_map_cow(map);
5993 if (!map)
5994 return NULL;
5996 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5997 for (i = 0; i < map->n; ++i) {
5998 map->p[i] = basic_map_bound_si(map->p[i],
5999 type, pos, value, upper);
6000 if (!map->p[i])
6001 goto error;
6003 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6004 return map;
6005 error:
6006 isl_map_free(map);
6007 return NULL;
6010 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6011 enum isl_dim_type type, unsigned pos, int value)
6013 return map_bound_si(map, type, pos, value, 0);
6016 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6017 enum isl_dim_type type, unsigned pos, int value)
6019 return map_bound_si(map, type, pos, value, 1);
6022 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6023 enum isl_dim_type type, unsigned pos, int value)
6025 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6026 type, pos, value));
6029 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6030 enum isl_dim_type type, unsigned pos, int value)
6032 return isl_map_upper_bound_si(set, type, pos, value);
6035 /* Bound the given variable of "bmap" from below (or above is "upper"
6036 * is set) to "value".
6038 static __isl_give isl_basic_map *basic_map_bound(
6039 __isl_take isl_basic_map *bmap,
6040 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6042 int j;
6044 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6045 return isl_basic_map_free(bmap);
6046 pos += isl_basic_map_offset(bmap, type);
6047 bmap = isl_basic_map_cow(bmap);
6048 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6049 j = isl_basic_map_alloc_inequality(bmap);
6050 if (j < 0)
6051 goto error;
6052 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6053 if (upper) {
6054 isl_int_set_si(bmap->ineq[j][pos], -1);
6055 isl_int_set(bmap->ineq[j][0], value);
6056 } else {
6057 isl_int_set_si(bmap->ineq[j][pos], 1);
6058 isl_int_neg(bmap->ineq[j][0], value);
6060 bmap = isl_basic_map_simplify(bmap);
6061 return isl_basic_map_finalize(bmap);
6062 error:
6063 isl_basic_map_free(bmap);
6064 return NULL;
6067 /* Bound the given variable of "map" from below (or above is "upper"
6068 * is set) to "value".
6070 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6071 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6073 int i;
6075 map = isl_map_cow(map);
6076 if (!map)
6077 return NULL;
6079 if (pos >= isl_map_dim(map, type))
6080 isl_die(map->ctx, isl_error_invalid,
6081 "index out of bounds", goto error);
6082 for (i = map->n - 1; i >= 0; --i) {
6083 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6084 if (remove_if_empty(map, i) < 0)
6085 goto error;
6087 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6088 return map;
6089 error:
6090 isl_map_free(map);
6091 return NULL;
6094 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6095 enum isl_dim_type type, unsigned pos, isl_int value)
6097 return map_bound(map, type, pos, value, 0);
6100 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6101 enum isl_dim_type type, unsigned pos, isl_int value)
6103 return map_bound(map, type, pos, value, 1);
6106 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6107 enum isl_dim_type type, unsigned pos, isl_int value)
6109 return isl_map_lower_bound(set, type, pos, value);
6112 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6113 enum isl_dim_type type, unsigned pos, isl_int value)
6115 return isl_map_upper_bound(set, type, pos, value);
6118 /* Force the values of the variable at position "pos" of type "type" of "set"
6119 * to be no smaller than "value".
6121 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6122 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6124 if (!value)
6125 goto error;
6126 if (!isl_val_is_int(value))
6127 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6128 "expecting integer value", goto error);
6129 set = isl_set_lower_bound(set, type, pos, value->n);
6130 isl_val_free(value);
6131 return set;
6132 error:
6133 isl_val_free(value);
6134 isl_set_free(set);
6135 return NULL;
6138 /* Force the values of the variable at position "pos" of type "type" of "set"
6139 * to be no greater than "value".
6141 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6142 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6144 if (!value)
6145 goto error;
6146 if (!isl_val_is_int(value))
6147 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6148 "expecting integer value", goto error);
6149 set = isl_set_upper_bound(set, type, pos, value->n);
6150 isl_val_free(value);
6151 return set;
6152 error:
6153 isl_val_free(value);
6154 isl_set_free(set);
6155 return NULL;
6158 struct isl_map *isl_map_reverse(struct isl_map *map)
6160 int i;
6162 map = isl_map_cow(map);
6163 if (!map)
6164 return NULL;
6166 map->dim = isl_space_reverse(map->dim);
6167 if (!map->dim)
6168 goto error;
6169 for (i = 0; i < map->n; ++i) {
6170 map->p[i] = isl_basic_map_reverse(map->p[i]);
6171 if (!map->p[i])
6172 goto error;
6174 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6175 return map;
6176 error:
6177 isl_map_free(map);
6178 return NULL;
6181 #undef TYPE
6182 #define TYPE isl_pw_multi_aff
6183 #undef SUFFIX
6184 #define SUFFIX _pw_multi_aff
6185 #undef EMPTY
6186 #define EMPTY isl_pw_multi_aff_empty
6187 #undef ADD
6188 #define ADD isl_pw_multi_aff_union_add
6189 #include "isl_map_lexopt_templ.c"
6191 /* Given a map "map", compute the lexicographically minimal
6192 * (or maximal) image element for each domain element in dom,
6193 * in the form of an isl_pw_multi_aff.
6194 * If "empty" is not NULL, then set *empty to those elements in dom that
6195 * do not have an image element.
6196 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6197 * should be computed over the domain of "map". "empty" is also NULL
6198 * in this case.
6200 * We first compute the lexicographically minimal or maximal element
6201 * in the first basic map. This results in a partial solution "res"
6202 * and a subset "todo" of dom that still need to be handled.
6203 * We then consider each of the remaining maps in "map" and successively
6204 * update both "res" and "todo".
6205 * If "empty" is NULL, then the todo sets are not needed and therefore
6206 * also not computed.
6208 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6209 __isl_take isl_map *map, __isl_take isl_set *dom,
6210 __isl_give isl_set **empty, unsigned flags)
6212 int i;
6213 int full;
6214 isl_pw_multi_aff *res;
6215 isl_set *todo;
6217 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6218 if (!map || (!full && !dom))
6219 goto error;
6221 if (isl_map_plain_is_empty(map)) {
6222 if (empty)
6223 *empty = dom;
6224 else
6225 isl_set_free(dom);
6226 return isl_pw_multi_aff_from_map(map);
6229 res = basic_map_partial_lexopt_pw_multi_aff(
6230 isl_basic_map_copy(map->p[0]),
6231 isl_set_copy(dom), empty, flags);
6233 if (empty)
6234 todo = *empty;
6235 for (i = 1; i < map->n; ++i) {
6236 isl_pw_multi_aff *res_i;
6238 res_i = basic_map_partial_lexopt_pw_multi_aff(
6239 isl_basic_map_copy(map->p[i]),
6240 isl_set_copy(dom), empty, flags);
6242 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6243 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6244 else
6245 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6247 if (empty)
6248 todo = isl_set_intersect(todo, *empty);
6251 isl_set_free(dom);
6252 isl_map_free(map);
6254 if (empty)
6255 *empty = todo;
6257 return res;
6258 error:
6259 if (empty)
6260 *empty = NULL;
6261 isl_set_free(dom);
6262 isl_map_free(map);
6263 return NULL;
6266 #undef TYPE
6267 #define TYPE isl_map
6268 #undef SUFFIX
6269 #define SUFFIX
6270 #undef EMPTY
6271 #define EMPTY isl_map_empty
6272 #undef ADD
6273 #define ADD isl_map_union_disjoint
6274 #include "isl_map_lexopt_templ.c"
6276 /* Given a map "map", compute the lexicographically minimal
6277 * (or maximal) image element for each domain element in "dom",
6278 * in the form of an isl_map.
6279 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6280 * do not have an image element.
6281 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6282 * should be computed over the domain of "map". "empty" is also NULL
6283 * in this case.
6285 * If the input consists of more than one disjunct, then first
6286 * compute the desired result in the form of an isl_pw_multi_aff and
6287 * then convert that into an isl_map.
6289 * This function used to have an explicit implementation in terms
6290 * of isl_maps, but it would continually intersect the domains of
6291 * partial results with the complement of the domain of the next
6292 * partial solution, potentially leading to an explosion in the number
6293 * of disjuncts if there are several disjuncts in the input.
6294 * An even earlier implementation of this function would look for
6295 * better results in the domain of the partial result and for extra
6296 * results in the complement of this domain, which would lead to
6297 * even more splintering.
6299 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6300 __isl_take isl_map *map, __isl_take isl_set *dom,
6301 __isl_give isl_set **empty, unsigned flags)
6303 int full;
6304 struct isl_map *res;
6305 isl_pw_multi_aff *pma;
6307 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6308 if (!map || (!full && !dom))
6309 goto error;
6311 if (isl_map_plain_is_empty(map)) {
6312 if (empty)
6313 *empty = dom;
6314 else
6315 isl_set_free(dom);
6316 return map;
6319 if (map->n == 1) {
6320 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6321 dom, empty, flags);
6322 isl_map_free(map);
6323 return res;
6326 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6327 flags);
6328 return isl_map_from_pw_multi_aff(pma);
6329 error:
6330 if (empty)
6331 *empty = NULL;
6332 isl_set_free(dom);
6333 isl_map_free(map);
6334 return NULL;
6337 __isl_give isl_map *isl_map_partial_lexmax(
6338 __isl_take isl_map *map, __isl_take isl_set *dom,
6339 __isl_give isl_set **empty)
6341 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6344 __isl_give isl_map *isl_map_partial_lexmin(
6345 __isl_take isl_map *map, __isl_take isl_set *dom,
6346 __isl_give isl_set **empty)
6348 return isl_map_partial_lexopt(map, dom, empty, 0);
6351 __isl_give isl_set *isl_set_partial_lexmin(
6352 __isl_take isl_set *set, __isl_take isl_set *dom,
6353 __isl_give isl_set **empty)
6355 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6356 dom, empty));
6359 __isl_give isl_set *isl_set_partial_lexmax(
6360 __isl_take isl_set *set, __isl_take isl_set *dom,
6361 __isl_give isl_set **empty)
6363 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6364 dom, empty));
6367 /* Compute the lexicographic minimum (or maximum if "flags" includes
6368 * ISL_OPT_MAX) of "bset" over its parametric domain.
6370 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6371 unsigned flags)
6373 return isl_basic_map_lexopt(bset, flags);
6376 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6378 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6381 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6383 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6386 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6388 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6391 /* Compute the lexicographic minimum of "bset" over its parametric domain
6392 * for the purpose of quantifier elimination.
6393 * That is, find an explicit representation for all the existentially
6394 * quantified variables in "bset" by computing their lexicographic
6395 * minimum.
6397 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6398 __isl_take isl_basic_set *bset)
6400 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6403 /* Extract the first and only affine expression from list
6404 * and then add it to *pwaff with the given dom.
6405 * This domain is known to be disjoint from other domains
6406 * because of the way isl_basic_map_foreach_lexmax works.
6408 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6409 __isl_take isl_aff_list *list, void *user)
6411 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6412 isl_aff *aff;
6413 isl_pw_aff **pwaff = user;
6414 isl_pw_aff *pwaff_i;
6416 if (!list)
6417 goto error;
6418 if (isl_aff_list_n_aff(list) != 1)
6419 isl_die(ctx, isl_error_internal,
6420 "expecting single element list", goto error);
6422 aff = isl_aff_list_get_aff(list, 0);
6423 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6425 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6427 isl_aff_list_free(list);
6429 return isl_stat_ok;
6430 error:
6431 isl_basic_set_free(dom);
6432 isl_aff_list_free(list);
6433 return isl_stat_error;
6436 /* Given a basic map with one output dimension, compute the minimum or
6437 * maximum of that dimension as an isl_pw_aff.
6439 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6440 * call update_dim_opt on each leaf of the result.
6442 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6443 int max)
6445 isl_space *dim = isl_basic_map_get_space(bmap);
6446 isl_pw_aff *pwaff;
6447 isl_stat r;
6449 dim = isl_space_from_domain(isl_space_domain(dim));
6450 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6451 pwaff = isl_pw_aff_empty(dim);
6453 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6454 if (r < 0)
6455 return isl_pw_aff_free(pwaff);
6457 return pwaff;
6460 /* Compute the minimum or maximum of the given output dimension
6461 * as a function of the parameters and the input dimensions,
6462 * but independently of the other output dimensions.
6464 * We first project out the other output dimension and then compute
6465 * the "lexicographic" maximum in each basic map, combining the results
6466 * using isl_pw_aff_union_max.
6468 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6469 int max)
6471 int i;
6472 isl_pw_aff *pwaff;
6473 unsigned n_out;
6475 n_out = isl_map_dim(map, isl_dim_out);
6476 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6477 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6478 if (!map)
6479 return NULL;
6481 if (map->n == 0) {
6482 isl_space *dim = isl_map_get_space(map);
6483 isl_map_free(map);
6484 return isl_pw_aff_empty(dim);
6487 pwaff = basic_map_dim_opt(map->p[0], max);
6488 for (i = 1; i < map->n; ++i) {
6489 isl_pw_aff *pwaff_i;
6491 pwaff_i = basic_map_dim_opt(map->p[i], max);
6492 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6495 isl_map_free(map);
6497 return pwaff;
6500 /* Compute the minimum of the given output dimension as a function of the
6501 * parameters and input dimensions, but independently of
6502 * the other output dimensions.
6504 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6506 return map_dim_opt(map, pos, 0);
6509 /* Compute the maximum of the given output dimension as a function of the
6510 * parameters and input dimensions, but independently of
6511 * the other output dimensions.
6513 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6515 return map_dim_opt(map, pos, 1);
6518 /* Compute the minimum or maximum of the given set dimension
6519 * as a function of the parameters,
6520 * but independently of the other set dimensions.
6522 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6523 int max)
6525 return map_dim_opt(set, pos, max);
6528 /* Compute the maximum of the given set dimension as a function of the
6529 * parameters, but independently of the other set dimensions.
6531 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6533 return set_dim_opt(set, pos, 1);
6536 /* Compute the minimum of the given set dimension as a function of the
6537 * parameters, but independently of the other set dimensions.
6539 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6541 return set_dim_opt(set, pos, 0);
6544 /* Apply a preimage specified by "mat" on the parameters of "bset".
6545 * bset is assumed to have only parameters and divs.
6547 static struct isl_basic_set *basic_set_parameter_preimage(
6548 struct isl_basic_set *bset, struct isl_mat *mat)
6550 unsigned nparam;
6552 if (!bset || !mat)
6553 goto error;
6555 bset->dim = isl_space_cow(bset->dim);
6556 if (!bset->dim)
6557 goto error;
6559 nparam = isl_basic_set_dim(bset, isl_dim_param);
6561 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6563 bset->dim->nparam = 0;
6564 bset->dim->n_out = nparam;
6565 bset = isl_basic_set_preimage(bset, mat);
6566 if (bset) {
6567 bset->dim->nparam = bset->dim->n_out;
6568 bset->dim->n_out = 0;
6570 return bset;
6571 error:
6572 isl_mat_free(mat);
6573 isl_basic_set_free(bset);
6574 return NULL;
6577 /* Apply a preimage specified by "mat" on the parameters of "set".
6578 * set is assumed to have only parameters and divs.
6580 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6581 __isl_take isl_mat *mat)
6583 isl_space *space;
6584 unsigned nparam;
6586 if (!set || !mat)
6587 goto error;
6589 nparam = isl_set_dim(set, isl_dim_param);
6591 if (mat->n_row != 1 + nparam)
6592 isl_die(isl_set_get_ctx(set), isl_error_internal,
6593 "unexpected number of rows", goto error);
6595 space = isl_set_get_space(set);
6596 space = isl_space_move_dims(space, isl_dim_set, 0,
6597 isl_dim_param, 0, nparam);
6598 set = isl_set_reset_space(set, space);
6599 set = isl_set_preimage(set, mat);
6600 nparam = isl_set_dim(set, isl_dim_out);
6601 space = isl_set_get_space(set);
6602 space = isl_space_move_dims(space, isl_dim_param, 0,
6603 isl_dim_out, 0, nparam);
6604 set = isl_set_reset_space(set, space);
6605 return set;
6606 error:
6607 isl_mat_free(mat);
6608 isl_set_free(set);
6609 return NULL;
6612 /* Intersect the basic set "bset" with the affine space specified by the
6613 * equalities in "eq".
6615 static struct isl_basic_set *basic_set_append_equalities(
6616 struct isl_basic_set *bset, struct isl_mat *eq)
6618 int i, k;
6619 unsigned len;
6621 if (!bset || !eq)
6622 goto error;
6624 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6625 eq->n_row, 0);
6626 if (!bset)
6627 goto error;
6629 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6630 for (i = 0; i < eq->n_row; ++i) {
6631 k = isl_basic_set_alloc_equality(bset);
6632 if (k < 0)
6633 goto error;
6634 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6635 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6637 isl_mat_free(eq);
6639 bset = isl_basic_set_gauss(bset, NULL);
6640 bset = isl_basic_set_finalize(bset);
6642 return bset;
6643 error:
6644 isl_mat_free(eq);
6645 isl_basic_set_free(bset);
6646 return NULL;
6649 /* Intersect the set "set" with the affine space specified by the
6650 * equalities in "eq".
6652 static struct isl_set *set_append_equalities(struct isl_set *set,
6653 struct isl_mat *eq)
6655 int i;
6657 if (!set || !eq)
6658 goto error;
6660 for (i = 0; i < set->n; ++i) {
6661 set->p[i] = basic_set_append_equalities(set->p[i],
6662 isl_mat_copy(eq));
6663 if (!set->p[i])
6664 goto error;
6666 isl_mat_free(eq);
6667 return set;
6668 error:
6669 isl_mat_free(eq);
6670 isl_set_free(set);
6671 return NULL;
6674 /* Given a basic set "bset" that only involves parameters and existentially
6675 * quantified variables, return the index of the first equality
6676 * that only involves parameters. If there is no such equality then
6677 * return bset->n_eq.
6679 * This function assumes that isl_basic_set_gauss has been called on "bset".
6681 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6683 int i, j;
6684 unsigned nparam, n_div;
6686 if (!bset)
6687 return -1;
6689 nparam = isl_basic_set_dim(bset, isl_dim_param);
6690 n_div = isl_basic_set_dim(bset, isl_dim_div);
6692 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6693 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6694 ++i;
6697 return i;
6700 /* Compute an explicit representation for the existentially quantified
6701 * variables in "bset" by computing the "minimal value" of the set
6702 * variables. Since there are no set variables, the computation of
6703 * the minimal value essentially computes an explicit representation
6704 * of the non-empty part(s) of "bset".
6706 * The input only involves parameters and existentially quantified variables.
6707 * All equalities among parameters have been removed.
6709 * Since the existentially quantified variables in the result are in general
6710 * going to be different from those in the input, we first replace
6711 * them by the minimal number of variables based on their equalities.
6712 * This should simplify the parametric integer programming.
6714 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6716 isl_morph *morph1, *morph2;
6717 isl_set *set;
6718 unsigned n;
6720 if (!bset)
6721 return NULL;
6722 if (bset->n_eq == 0)
6723 return isl_basic_set_lexmin_compute_divs(bset);
6725 morph1 = isl_basic_set_parameter_compression(bset);
6726 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6727 bset = isl_basic_set_lift(bset);
6728 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6729 bset = isl_morph_basic_set(morph2, bset);
6730 n = isl_basic_set_dim(bset, isl_dim_set);
6731 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6733 set = isl_basic_set_lexmin_compute_divs(bset);
6735 set = isl_morph_set(isl_morph_inverse(morph1), set);
6737 return set;
6740 /* Project the given basic set onto its parameter domain, possibly introducing
6741 * new, explicit, existential variables in the constraints.
6742 * The input has parameters and (possibly implicit) existential variables.
6743 * The output has the same parameters, but only
6744 * explicit existentially quantified variables.
6746 * The actual projection is performed by pip, but pip doesn't seem
6747 * to like equalities very much, so we first remove the equalities
6748 * among the parameters by performing a variable compression on
6749 * the parameters. Afterward, an inverse transformation is performed
6750 * and the equalities among the parameters are inserted back in.
6752 * The variable compression on the parameters may uncover additional
6753 * equalities that were only implicit before. We therefore check
6754 * if there are any new parameter equalities in the result and
6755 * if so recurse. The removal of parameter equalities is required
6756 * for the parameter compression performed by base_compute_divs.
6758 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6760 int i;
6761 struct isl_mat *eq;
6762 struct isl_mat *T, *T2;
6763 struct isl_set *set;
6764 unsigned nparam;
6766 bset = isl_basic_set_cow(bset);
6767 if (!bset)
6768 return NULL;
6770 if (bset->n_eq == 0)
6771 return base_compute_divs(bset);
6773 bset = isl_basic_set_gauss(bset, NULL);
6774 if (!bset)
6775 return NULL;
6776 if (isl_basic_set_plain_is_empty(bset))
6777 return isl_set_from_basic_set(bset);
6779 i = first_parameter_equality(bset);
6780 if (i == bset->n_eq)
6781 return base_compute_divs(bset);
6783 nparam = isl_basic_set_dim(bset, isl_dim_param);
6784 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6785 0, 1 + nparam);
6786 eq = isl_mat_cow(eq);
6787 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6788 if (T && T->n_col == 0) {
6789 isl_mat_free(T);
6790 isl_mat_free(T2);
6791 isl_mat_free(eq);
6792 bset = isl_basic_set_set_to_empty(bset);
6793 return isl_set_from_basic_set(bset);
6795 bset = basic_set_parameter_preimage(bset, T);
6797 i = first_parameter_equality(bset);
6798 if (!bset)
6799 set = NULL;
6800 else if (i == bset->n_eq)
6801 set = base_compute_divs(bset);
6802 else
6803 set = parameter_compute_divs(bset);
6804 set = set_parameter_preimage(set, T2);
6805 set = set_append_equalities(set, eq);
6806 return set;
6809 /* Insert the divs from "ls" before those of "bmap".
6811 * The number of columns is not changed, which means that the last
6812 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6813 * The caller is responsible for removing the same number of dimensions
6814 * from the space of "bmap".
6816 static __isl_give isl_basic_map *insert_divs_from_local_space(
6817 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6819 int i;
6820 int n_div;
6821 int old_n_div;
6823 n_div = isl_local_space_dim(ls, isl_dim_div);
6824 if (n_div == 0)
6825 return bmap;
6827 old_n_div = bmap->n_div;
6828 bmap = insert_div_rows(bmap, n_div);
6829 if (!bmap)
6830 return NULL;
6832 for (i = 0; i < n_div; ++i) {
6833 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6834 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6837 return bmap;
6840 /* Replace the space of "bmap" by the space and divs of "ls".
6842 * If "ls" has any divs, then we simplify the result since we may
6843 * have discovered some additional equalities that could simplify
6844 * the div expressions.
6846 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6847 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6849 int n_div;
6851 bmap = isl_basic_map_cow(bmap);
6852 if (!bmap || !ls)
6853 goto error;
6855 n_div = isl_local_space_dim(ls, isl_dim_div);
6856 bmap = insert_divs_from_local_space(bmap, ls);
6857 if (!bmap)
6858 goto error;
6860 isl_space_free(bmap->dim);
6861 bmap->dim = isl_local_space_get_space(ls);
6862 if (!bmap->dim)
6863 goto error;
6865 isl_local_space_free(ls);
6866 if (n_div > 0)
6867 bmap = isl_basic_map_simplify(bmap);
6868 bmap = isl_basic_map_finalize(bmap);
6869 return bmap;
6870 error:
6871 isl_basic_map_free(bmap);
6872 isl_local_space_free(ls);
6873 return NULL;
6876 /* Replace the space of "map" by the space and divs of "ls".
6878 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6879 __isl_take isl_local_space *ls)
6881 int i;
6883 map = isl_map_cow(map);
6884 if (!map || !ls)
6885 goto error;
6887 for (i = 0; i < map->n; ++i) {
6888 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6889 isl_local_space_copy(ls));
6890 if (!map->p[i])
6891 goto error;
6893 isl_space_free(map->dim);
6894 map->dim = isl_local_space_get_space(ls);
6895 if (!map->dim)
6896 goto error;
6898 isl_local_space_free(ls);
6899 return map;
6900 error:
6901 isl_local_space_free(ls);
6902 isl_map_free(map);
6903 return NULL;
6906 /* Compute an explicit representation for the existentially
6907 * quantified variables for which do not know any explicit representation yet.
6909 * We first sort the existentially quantified variables so that the
6910 * existentially quantified variables for which we already have an explicit
6911 * representation are placed before those for which we do not.
6912 * The input dimensions, the output dimensions and the existentially
6913 * quantified variables for which we already have an explicit
6914 * representation are then turned into parameters.
6915 * compute_divs returns a map with the same parameters and
6916 * no input or output dimensions and the dimension specification
6917 * is reset to that of the input, including the existentially quantified
6918 * variables for which we already had an explicit representation.
6920 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6922 struct isl_basic_set *bset;
6923 struct isl_set *set;
6924 struct isl_map *map;
6925 isl_space *dim;
6926 isl_local_space *ls;
6927 unsigned nparam;
6928 unsigned n_in;
6929 unsigned n_out;
6930 int n_known;
6931 int i;
6933 bmap = isl_basic_map_sort_divs(bmap);
6934 bmap = isl_basic_map_cow(bmap);
6935 if (!bmap)
6936 return NULL;
6938 n_known = isl_basic_map_first_unknown_div(bmap);
6939 if (n_known < 0)
6940 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6942 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6943 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6944 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6945 dim = isl_space_set_alloc(bmap->ctx,
6946 nparam + n_in + n_out + n_known, 0);
6947 if (!dim)
6948 goto error;
6950 ls = isl_basic_map_get_local_space(bmap);
6951 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6952 n_known, bmap->n_div - n_known);
6953 if (n_known > 0) {
6954 for (i = n_known; i < bmap->n_div; ++i)
6955 swap_div(bmap, i - n_known, i);
6956 bmap->n_div -= n_known;
6957 bmap->extra -= n_known;
6959 bmap = isl_basic_map_reset_space(bmap, dim);
6960 bset = bset_from_bmap(bmap);
6962 set = parameter_compute_divs(bset);
6963 map = set_to_map(set);
6964 map = replace_space_by_local_space(map, ls);
6966 return map;
6967 error:
6968 isl_basic_map_free(bmap);
6969 return NULL;
6972 /* Remove the explicit representation of local variable "div",
6973 * if there is any.
6975 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6976 __isl_take isl_basic_map *bmap, int div)
6978 isl_bool unknown;
6980 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6981 if (unknown < 0)
6982 return isl_basic_map_free(bmap);
6983 if (unknown)
6984 return bmap;
6986 bmap = isl_basic_map_cow(bmap);
6987 if (!bmap)
6988 return NULL;
6989 isl_int_set_si(bmap->div[div][0], 0);
6990 return bmap;
6993 /* Is local variable "div" of "bmap" marked as not having an explicit
6994 * representation?
6995 * Note that even if "div" is not marked in this way and therefore
6996 * has an explicit representation, this representation may still
6997 * depend (indirectly) on other local variables that do not
6998 * have an explicit representation.
7000 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7001 int div)
7003 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7004 return isl_bool_error;
7005 return isl_int_is_zero(bmap->div[div][0]);
7008 /* Return the position of the first local variable that does not
7009 * have an explicit representation.
7010 * Return the total number of local variables if they all have
7011 * an explicit representation.
7012 * Return -1 on error.
7014 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7016 int i;
7018 if (!bmap)
7019 return -1;
7021 for (i = 0; i < bmap->n_div; ++i) {
7022 if (!isl_basic_map_div_is_known(bmap, i))
7023 return i;
7025 return bmap->n_div;
7028 /* Return the position of the first local variable that does not
7029 * have an explicit representation.
7030 * Return the total number of local variables if they all have
7031 * an explicit representation.
7032 * Return -1 on error.
7034 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7036 return isl_basic_map_first_unknown_div(bset);
7039 /* Does "bmap" have an explicit representation for all local variables?
7041 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7043 int first, n;
7045 n = isl_basic_map_dim(bmap, isl_dim_div);
7046 first = isl_basic_map_first_unknown_div(bmap);
7047 if (first < 0)
7048 return isl_bool_error;
7049 return first == n;
7052 /* Do all basic maps in "map" have an explicit representation
7053 * for all local variables?
7055 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7057 int i;
7059 if (!map)
7060 return isl_bool_error;
7062 for (i = 0; i < map->n; ++i) {
7063 int known = isl_basic_map_divs_known(map->p[i]);
7064 if (known <= 0)
7065 return known;
7068 return isl_bool_true;
7071 /* If bmap contains any unknown divs, then compute explicit
7072 * expressions for them. However, this computation may be
7073 * quite expensive, so first try to remove divs that aren't
7074 * strictly needed.
7076 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7078 int known;
7079 struct isl_map *map;
7081 known = isl_basic_map_divs_known(bmap);
7082 if (known < 0)
7083 goto error;
7084 if (known)
7085 return isl_map_from_basic_map(bmap);
7087 bmap = isl_basic_map_drop_redundant_divs(bmap);
7089 known = isl_basic_map_divs_known(bmap);
7090 if (known < 0)
7091 goto error;
7092 if (known)
7093 return isl_map_from_basic_map(bmap);
7095 map = compute_divs(bmap);
7096 return map;
7097 error:
7098 isl_basic_map_free(bmap);
7099 return NULL;
7102 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7104 int i;
7105 int known;
7106 struct isl_map *res;
7108 if (!map)
7109 return NULL;
7110 if (map->n == 0)
7111 return map;
7113 known = isl_map_divs_known(map);
7114 if (known < 0) {
7115 isl_map_free(map);
7116 return NULL;
7118 if (known)
7119 return map;
7121 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7122 for (i = 1 ; i < map->n; ++i) {
7123 struct isl_map *r2;
7124 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7125 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7126 res = isl_map_union_disjoint(res, r2);
7127 else
7128 res = isl_map_union(res, r2);
7130 isl_map_free(map);
7132 return res;
7135 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7137 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7140 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7142 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7145 struct isl_set *isl_map_domain(struct isl_map *map)
7147 int i;
7148 struct isl_set *set;
7150 if (!map)
7151 goto error;
7153 map = isl_map_cow(map);
7154 if (!map)
7155 return NULL;
7157 set = set_from_map(map);
7158 set->dim = isl_space_domain(set->dim);
7159 if (!set->dim)
7160 goto error;
7161 for (i = 0; i < map->n; ++i) {
7162 set->p[i] = isl_basic_map_domain(map->p[i]);
7163 if (!set->p[i])
7164 goto error;
7166 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7167 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7168 return set;
7169 error:
7170 isl_map_free(map);
7171 return NULL;
7174 /* Return the union of "map1" and "map2", where we assume for now that
7175 * "map1" and "map2" are disjoint. Note that the basic maps inside
7176 * "map1" or "map2" may not be disjoint from each other.
7177 * Also note that this function is also called from isl_map_union,
7178 * which takes care of handling the situation where "map1" and "map2"
7179 * may not be disjoint.
7181 * If one of the inputs is empty, we can simply return the other input.
7182 * Similarly, if one of the inputs is universal, then it is equal to the union.
7184 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7185 __isl_take isl_map *map2)
7187 int i;
7188 unsigned flags = 0;
7189 struct isl_map *map = NULL;
7190 int is_universe;
7192 if (!map1 || !map2)
7193 goto error;
7195 if (!isl_space_is_equal(map1->dim, map2->dim))
7196 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7197 "spaces don't match", goto error);
7199 if (map1->n == 0) {
7200 isl_map_free(map1);
7201 return map2;
7203 if (map2->n == 0) {
7204 isl_map_free(map2);
7205 return map1;
7208 is_universe = isl_map_plain_is_universe(map1);
7209 if (is_universe < 0)
7210 goto error;
7211 if (is_universe) {
7212 isl_map_free(map2);
7213 return map1;
7216 is_universe = isl_map_plain_is_universe(map2);
7217 if (is_universe < 0)
7218 goto error;
7219 if (is_universe) {
7220 isl_map_free(map1);
7221 return map2;
7224 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7225 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7226 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7228 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7229 map1->n + map2->n, flags);
7230 if (!map)
7231 goto error;
7232 for (i = 0; i < map1->n; ++i) {
7233 map = isl_map_add_basic_map(map,
7234 isl_basic_map_copy(map1->p[i]));
7235 if (!map)
7236 goto error;
7238 for (i = 0; i < map2->n; ++i) {
7239 map = isl_map_add_basic_map(map,
7240 isl_basic_map_copy(map2->p[i]));
7241 if (!map)
7242 goto error;
7244 isl_map_free(map1);
7245 isl_map_free(map2);
7246 return map;
7247 error:
7248 isl_map_free(map);
7249 isl_map_free(map1);
7250 isl_map_free(map2);
7251 return NULL;
7254 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7255 * guaranteed to be disjoint by the caller.
7257 * Note that this functions is called from within isl_map_make_disjoint,
7258 * so we have to be careful not to touch the constraints of the inputs
7259 * in any way.
7261 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7262 __isl_take isl_map *map2)
7264 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7267 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7268 * not be disjoint. The parameters are assumed to have been aligned.
7270 * We currently simply call map_union_disjoint, the internal operation
7271 * of which does not really depend on the inputs being disjoint.
7272 * If the result contains more than one basic map, then we clear
7273 * the disjoint flag since the result may contain basic maps from
7274 * both inputs and these are not guaranteed to be disjoint.
7276 * As a special case, if "map1" and "map2" are obviously equal,
7277 * then we simply return "map1".
7279 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7280 __isl_take isl_map *map2)
7282 int equal;
7284 if (!map1 || !map2)
7285 goto error;
7287 equal = isl_map_plain_is_equal(map1, map2);
7288 if (equal < 0)
7289 goto error;
7290 if (equal) {
7291 isl_map_free(map2);
7292 return map1;
7295 map1 = map_union_disjoint(map1, map2);
7296 if (!map1)
7297 return NULL;
7298 if (map1->n > 1)
7299 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7300 return map1;
7301 error:
7302 isl_map_free(map1);
7303 isl_map_free(map2);
7304 return NULL;
7307 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7308 * not be disjoint.
7310 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7311 __isl_take isl_map *map2)
7313 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7316 struct isl_set *isl_set_union_disjoint(
7317 struct isl_set *set1, struct isl_set *set2)
7319 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7320 set_to_map(set2)));
7323 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7325 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7328 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7329 * the results.
7331 * "map" and "set" are assumed to be compatible and non-NULL.
7333 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7334 __isl_take isl_set *set,
7335 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7336 __isl_take isl_basic_set *bset))
7338 unsigned flags = 0;
7339 struct isl_map *result;
7340 int i, j;
7342 if (isl_set_plain_is_universe(set)) {
7343 isl_set_free(set);
7344 return map;
7347 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7348 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7349 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7351 result = isl_map_alloc_space(isl_space_copy(map->dim),
7352 map->n * set->n, flags);
7353 for (i = 0; result && i < map->n; ++i)
7354 for (j = 0; j < set->n; ++j) {
7355 result = isl_map_add_basic_map(result,
7356 fn(isl_basic_map_copy(map->p[i]),
7357 isl_basic_set_copy(set->p[j])));
7358 if (!result)
7359 break;
7362 isl_map_free(map);
7363 isl_set_free(set);
7364 return result;
7367 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7368 __isl_take isl_set *set)
7370 isl_bool ok;
7372 ok = isl_map_compatible_range(map, set);
7373 if (ok < 0)
7374 goto error;
7375 if (!ok)
7376 isl_die(set->ctx, isl_error_invalid,
7377 "incompatible spaces", goto error);
7379 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7380 error:
7381 isl_map_free(map);
7382 isl_set_free(set);
7383 return NULL;
7386 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7387 __isl_take isl_set *set)
7389 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7392 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7393 __isl_take isl_set *set)
7395 isl_bool ok;
7397 ok = isl_map_compatible_domain(map, set);
7398 if (ok < 0)
7399 goto error;
7400 if (!ok)
7401 isl_die(set->ctx, isl_error_invalid,
7402 "incompatible spaces", goto error);
7404 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7405 error:
7406 isl_map_free(map);
7407 isl_set_free(set);
7408 return NULL;
7411 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7412 __isl_take isl_set *set)
7414 return isl_map_align_params_map_map_and(map, set,
7415 &map_intersect_domain);
7418 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7419 __isl_take isl_map *map2)
7421 if (!map1 || !map2)
7422 goto error;
7423 map1 = isl_map_reverse(map1);
7424 map1 = isl_map_apply_range(map1, map2);
7425 return isl_map_reverse(map1);
7426 error:
7427 isl_map_free(map1);
7428 isl_map_free(map2);
7429 return NULL;
7432 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7433 __isl_take isl_map *map2)
7435 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7438 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7439 __isl_take isl_map *map2)
7441 isl_space *dim_result;
7442 struct isl_map *result;
7443 int i, j;
7445 if (!map1 || !map2)
7446 goto error;
7448 dim_result = isl_space_join(isl_space_copy(map1->dim),
7449 isl_space_copy(map2->dim));
7451 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7452 if (!result)
7453 goto error;
7454 for (i = 0; i < map1->n; ++i)
7455 for (j = 0; j < map2->n; ++j) {
7456 result = isl_map_add_basic_map(result,
7457 isl_basic_map_apply_range(
7458 isl_basic_map_copy(map1->p[i]),
7459 isl_basic_map_copy(map2->p[j])));
7460 if (!result)
7461 goto error;
7463 isl_map_free(map1);
7464 isl_map_free(map2);
7465 if (result && result->n <= 1)
7466 ISL_F_SET(result, ISL_MAP_DISJOINT);
7467 return result;
7468 error:
7469 isl_map_free(map1);
7470 isl_map_free(map2);
7471 return NULL;
7474 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7475 __isl_take isl_map *map2)
7477 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7481 * returns range - domain
7483 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7485 isl_space *target_space;
7486 struct isl_basic_set *bset;
7487 unsigned dim;
7488 unsigned nparam;
7489 int i;
7491 if (!bmap)
7492 goto error;
7493 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7494 bmap->dim, isl_dim_out),
7495 goto error);
7496 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7497 dim = isl_basic_map_n_in(bmap);
7498 nparam = isl_basic_map_n_param(bmap);
7499 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7500 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7501 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7502 for (i = 0; i < dim; ++i) {
7503 int j = isl_basic_map_alloc_equality(bmap);
7504 if (j < 0) {
7505 bmap = isl_basic_map_free(bmap);
7506 break;
7508 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7509 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7510 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7511 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7513 bset = isl_basic_map_domain(bmap);
7514 bset = isl_basic_set_reset_space(bset, target_space);
7515 return bset;
7516 error:
7517 isl_basic_map_free(bmap);
7518 return NULL;
7522 * returns range - domain
7524 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7526 int i;
7527 isl_space *dim;
7528 struct isl_set *result;
7530 if (!map)
7531 return NULL;
7533 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7534 map->dim, isl_dim_out),
7535 goto error);
7536 dim = isl_map_get_space(map);
7537 dim = isl_space_domain(dim);
7538 result = isl_set_alloc_space(dim, map->n, 0);
7539 if (!result)
7540 goto error;
7541 for (i = 0; i < map->n; ++i)
7542 result = isl_set_add_basic_set(result,
7543 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7544 isl_map_free(map);
7545 return result;
7546 error:
7547 isl_map_free(map);
7548 return NULL;
7552 * returns [domain -> range] -> range - domain
7554 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7555 __isl_take isl_basic_map *bmap)
7557 int i, k;
7558 isl_space *dim;
7559 isl_basic_map *domain;
7560 int nparam, n;
7561 unsigned total;
7563 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7564 bmap->dim, isl_dim_out))
7565 isl_die(bmap->ctx, isl_error_invalid,
7566 "domain and range don't match", goto error);
7568 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7569 n = isl_basic_map_dim(bmap, isl_dim_in);
7571 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7572 domain = isl_basic_map_universe(dim);
7574 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7575 bmap = isl_basic_map_apply_range(bmap, domain);
7576 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7578 total = isl_basic_map_total_dim(bmap);
7580 for (i = 0; i < n; ++i) {
7581 k = isl_basic_map_alloc_equality(bmap);
7582 if (k < 0)
7583 goto error;
7584 isl_seq_clr(bmap->eq[k], 1 + total);
7585 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7586 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7587 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7590 bmap = isl_basic_map_gauss(bmap, NULL);
7591 return isl_basic_map_finalize(bmap);
7592 error:
7593 isl_basic_map_free(bmap);
7594 return NULL;
7598 * returns [domain -> range] -> range - domain
7600 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7602 int i;
7603 isl_space *domain_dim;
7605 if (!map)
7606 return NULL;
7608 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7609 map->dim, isl_dim_out))
7610 isl_die(map->ctx, isl_error_invalid,
7611 "domain and range don't match", goto error);
7613 map = isl_map_cow(map);
7614 if (!map)
7615 return NULL;
7617 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7618 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7619 map->dim = isl_space_join(map->dim, domain_dim);
7620 if (!map->dim)
7621 goto error;
7622 for (i = 0; i < map->n; ++i) {
7623 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7624 if (!map->p[i])
7625 goto error;
7627 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7628 return map;
7629 error:
7630 isl_map_free(map);
7631 return NULL;
7634 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7636 struct isl_basic_map *bmap;
7637 unsigned nparam;
7638 unsigned dim;
7639 int i;
7641 if (!dims)
7642 return NULL;
7644 nparam = dims->nparam;
7645 dim = dims->n_out;
7646 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7647 if (!bmap)
7648 goto error;
7650 for (i = 0; i < dim; ++i) {
7651 int j = isl_basic_map_alloc_equality(bmap);
7652 if (j < 0)
7653 goto error;
7654 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7655 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7656 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7658 return isl_basic_map_finalize(bmap);
7659 error:
7660 isl_basic_map_free(bmap);
7661 return NULL;
7664 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7666 if (!dim)
7667 return NULL;
7668 if (dim->n_in != dim->n_out)
7669 isl_die(dim->ctx, isl_error_invalid,
7670 "number of input and output dimensions needs to be "
7671 "the same", goto error);
7672 return basic_map_identity(dim);
7673 error:
7674 isl_space_free(dim);
7675 return NULL;
7678 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7680 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7683 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7685 isl_space *dim = isl_set_get_space(set);
7686 isl_map *id;
7687 id = isl_map_identity(isl_space_map_from_set(dim));
7688 return isl_map_intersect_range(id, set);
7691 /* Construct a basic set with all set dimensions having only non-negative
7692 * values.
7694 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7695 __isl_take isl_space *space)
7697 int i;
7698 unsigned nparam;
7699 unsigned dim;
7700 struct isl_basic_set *bset;
7702 if (!space)
7703 return NULL;
7704 nparam = space->nparam;
7705 dim = space->n_out;
7706 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7707 if (!bset)
7708 return NULL;
7709 for (i = 0; i < dim; ++i) {
7710 int k = isl_basic_set_alloc_inequality(bset);
7711 if (k < 0)
7712 goto error;
7713 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7714 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7716 return bset;
7717 error:
7718 isl_basic_set_free(bset);
7719 return NULL;
7722 /* Construct the half-space x_pos >= 0.
7724 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7725 int pos)
7727 int k;
7728 isl_basic_set *nonneg;
7730 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7731 k = isl_basic_set_alloc_inequality(nonneg);
7732 if (k < 0)
7733 goto error;
7734 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7735 isl_int_set_si(nonneg->ineq[k][pos], 1);
7737 return isl_basic_set_finalize(nonneg);
7738 error:
7739 isl_basic_set_free(nonneg);
7740 return NULL;
7743 /* Construct the half-space x_pos <= -1.
7745 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7747 int k;
7748 isl_basic_set *neg;
7750 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7751 k = isl_basic_set_alloc_inequality(neg);
7752 if (k < 0)
7753 goto error;
7754 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7755 isl_int_set_si(neg->ineq[k][0], -1);
7756 isl_int_set_si(neg->ineq[k][pos], -1);
7758 return isl_basic_set_finalize(neg);
7759 error:
7760 isl_basic_set_free(neg);
7761 return NULL;
7764 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7765 enum isl_dim_type type, unsigned first, unsigned n)
7767 int i;
7768 unsigned offset;
7769 isl_basic_set *nonneg;
7770 isl_basic_set *neg;
7772 if (!set)
7773 return NULL;
7774 if (n == 0)
7775 return set;
7777 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7779 offset = pos(set->dim, type);
7780 for (i = 0; i < n; ++i) {
7781 nonneg = nonneg_halfspace(isl_set_get_space(set),
7782 offset + first + i);
7783 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7785 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7788 return set;
7789 error:
7790 isl_set_free(set);
7791 return NULL;
7794 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7795 int len,
7796 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7797 void *user)
7799 isl_set *half;
7801 if (!set)
7802 return isl_stat_error;
7803 if (isl_set_plain_is_empty(set)) {
7804 isl_set_free(set);
7805 return isl_stat_ok;
7807 if (first == len)
7808 return fn(set, signs, user);
7810 signs[first] = 1;
7811 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7812 1 + first));
7813 half = isl_set_intersect(half, isl_set_copy(set));
7814 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7815 goto error;
7817 signs[first] = -1;
7818 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7819 1 + first));
7820 half = isl_set_intersect(half, set);
7821 return foreach_orthant(half, signs, first + 1, len, fn, user);
7822 error:
7823 isl_set_free(set);
7824 return isl_stat_error;
7827 /* Call "fn" on the intersections of "set" with each of the orthants
7828 * (except for obviously empty intersections). The orthant is identified
7829 * by the signs array, with each entry having value 1 or -1 according
7830 * to the sign of the corresponding variable.
7832 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
7833 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7834 void *user)
7836 unsigned nparam;
7837 unsigned nvar;
7838 int *signs;
7839 isl_stat r;
7841 if (!set)
7842 return isl_stat_error;
7843 if (isl_set_plain_is_empty(set))
7844 return isl_stat_ok;
7846 nparam = isl_set_dim(set, isl_dim_param);
7847 nvar = isl_set_dim(set, isl_dim_set);
7849 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7851 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7852 fn, user);
7854 free(signs);
7856 return r;
7859 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7861 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7864 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7865 __isl_keep isl_basic_map *bmap2)
7867 int is_subset;
7868 struct isl_map *map1;
7869 struct isl_map *map2;
7871 if (!bmap1 || !bmap2)
7872 return isl_bool_error;
7874 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7875 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7877 is_subset = isl_map_is_subset(map1, map2);
7879 isl_map_free(map1);
7880 isl_map_free(map2);
7882 return is_subset;
7885 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7886 __isl_keep isl_basic_set *bset2)
7888 return isl_basic_map_is_subset(bset1, bset2);
7891 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7892 __isl_keep isl_basic_map *bmap2)
7894 isl_bool is_subset;
7896 if (!bmap1 || !bmap2)
7897 return isl_bool_error;
7898 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7899 if (is_subset != isl_bool_true)
7900 return is_subset;
7901 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7902 return is_subset;
7905 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7906 __isl_keep isl_basic_set *bset2)
7908 return isl_basic_map_is_equal(
7909 bset_to_bmap(bset1), bset_to_bmap(bset2));
7912 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7914 int i;
7915 int is_empty;
7917 if (!map)
7918 return isl_bool_error;
7919 for (i = 0; i < map->n; ++i) {
7920 is_empty = isl_basic_map_is_empty(map->p[i]);
7921 if (is_empty < 0)
7922 return isl_bool_error;
7923 if (!is_empty)
7924 return isl_bool_false;
7926 return isl_bool_true;
7929 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7931 return map ? map->n == 0 : isl_bool_error;
7934 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7936 return set ? set->n == 0 : isl_bool_error;
7939 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7941 return isl_map_is_empty(set_to_map(set));
7944 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
7945 __isl_keep isl_map *map2)
7947 if (!map1 || !map2)
7948 return isl_bool_error;
7950 return isl_space_is_equal(map1->dim, map2->dim);
7953 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
7954 __isl_keep isl_set *set2)
7956 if (!set1 || !set2)
7957 return isl_bool_error;
7959 return isl_space_is_equal(set1->dim, set2->dim);
7962 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7964 isl_bool is_subset;
7966 if (!map1 || !map2)
7967 return isl_bool_error;
7968 is_subset = isl_map_is_subset(map1, map2);
7969 if (is_subset != isl_bool_true)
7970 return is_subset;
7971 is_subset = isl_map_is_subset(map2, map1);
7972 return is_subset;
7975 /* Is "map1" equal to "map2"?
7977 * First check if they are obviously equal.
7978 * If not, then perform a more detailed analysis.
7980 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7982 isl_bool equal;
7984 equal = isl_map_plain_is_equal(map1, map2);
7985 if (equal < 0 || equal)
7986 return equal;
7987 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7990 isl_bool isl_basic_map_is_strict_subset(
7991 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7993 isl_bool is_subset;
7995 if (!bmap1 || !bmap2)
7996 return isl_bool_error;
7997 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7998 if (is_subset != isl_bool_true)
7999 return is_subset;
8000 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8001 if (is_subset == isl_bool_error)
8002 return is_subset;
8003 return !is_subset;
8006 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8007 __isl_keep isl_map *map2)
8009 isl_bool is_subset;
8011 if (!map1 || !map2)
8012 return isl_bool_error;
8013 is_subset = isl_map_is_subset(map1, map2);
8014 if (is_subset != isl_bool_true)
8015 return is_subset;
8016 is_subset = isl_map_is_subset(map2, map1);
8017 if (is_subset == isl_bool_error)
8018 return is_subset;
8019 return !is_subset;
8022 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8023 __isl_keep isl_set *set2)
8025 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8028 /* Is "bmap" obviously equal to the universe with the same space?
8030 * That is, does it not have any constraints?
8032 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8034 if (!bmap)
8035 return isl_bool_error;
8036 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8039 /* Is "bset" obviously equal to the universe with the same space?
8041 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8043 return isl_basic_map_plain_is_universe(bset);
8046 /* If "c" does not involve any existentially quantified variables,
8047 * then set *univ to false and abort
8049 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8051 isl_bool *univ = user;
8052 unsigned n;
8054 n = isl_constraint_dim(c, isl_dim_div);
8055 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8056 isl_constraint_free(c);
8057 if (*univ < 0 || !*univ)
8058 return isl_stat_error;
8059 return isl_stat_ok;
8062 /* Is "bmap" equal to the universe with the same space?
8064 * First check if it is obviously equal to the universe.
8065 * If not and if there are any constraints not involving
8066 * existentially quantified variables, then it is certainly
8067 * not equal to the universe.
8068 * Otherwise, check if the universe is a subset of "bmap".
8070 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8072 isl_bool univ;
8073 isl_basic_map *test;
8075 univ = isl_basic_map_plain_is_universe(bmap);
8076 if (univ < 0 || univ)
8077 return univ;
8078 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8079 return isl_bool_false;
8080 univ = isl_bool_true;
8081 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8082 univ)
8083 return isl_bool_error;
8084 if (univ < 0 || !univ)
8085 return univ;
8086 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8087 univ = isl_basic_map_is_subset(test, bmap);
8088 isl_basic_map_free(test);
8089 return univ;
8092 /* Is "bset" equal to the universe with the same space?
8094 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8096 return isl_basic_map_is_universe(bset);
8099 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8101 int i;
8103 if (!map)
8104 return isl_bool_error;
8106 for (i = 0; i < map->n; ++i) {
8107 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8108 if (r < 0 || r)
8109 return r;
8112 return isl_bool_false;
8115 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8117 return isl_map_plain_is_universe(set_to_map(set));
8120 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8122 struct isl_basic_set *bset = NULL;
8123 struct isl_vec *sample = NULL;
8124 isl_bool empty, non_empty;
8126 if (!bmap)
8127 return isl_bool_error;
8129 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8130 return isl_bool_true;
8132 if (isl_basic_map_plain_is_universe(bmap))
8133 return isl_bool_false;
8135 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8136 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8137 copy = isl_basic_map_remove_redundancies(copy);
8138 empty = isl_basic_map_plain_is_empty(copy);
8139 isl_basic_map_free(copy);
8140 return empty;
8143 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8144 if (non_empty < 0)
8145 return isl_bool_error;
8146 if (non_empty)
8147 return isl_bool_false;
8148 isl_vec_free(bmap->sample);
8149 bmap->sample = NULL;
8150 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8151 if (!bset)
8152 return isl_bool_error;
8153 sample = isl_basic_set_sample_vec(bset);
8154 if (!sample)
8155 return isl_bool_error;
8156 empty = sample->size == 0;
8157 isl_vec_free(bmap->sample);
8158 bmap->sample = sample;
8159 if (empty)
8160 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8162 return empty;
8165 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8167 if (!bmap)
8168 return isl_bool_error;
8169 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8172 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8174 if (!bset)
8175 return isl_bool_error;
8176 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8179 /* Is "bmap" known to be non-empty?
8181 * That is, is the cached sample still valid?
8183 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8185 unsigned total;
8187 if (!bmap)
8188 return isl_bool_error;
8189 if (!bmap->sample)
8190 return isl_bool_false;
8191 total = 1 + isl_basic_map_total_dim(bmap);
8192 if (bmap->sample->size != total)
8193 return isl_bool_false;
8194 return isl_basic_map_contains(bmap, bmap->sample);
8197 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8199 return isl_basic_map_is_empty(bset_to_bmap(bset));
8202 struct isl_map *isl_basic_map_union(
8203 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8205 struct isl_map *map;
8206 if (!bmap1 || !bmap2)
8207 goto error;
8209 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8211 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8212 if (!map)
8213 goto error;
8214 map = isl_map_add_basic_map(map, bmap1);
8215 map = isl_map_add_basic_map(map, bmap2);
8216 return map;
8217 error:
8218 isl_basic_map_free(bmap1);
8219 isl_basic_map_free(bmap2);
8220 return NULL;
8223 struct isl_set *isl_basic_set_union(
8224 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8226 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8227 bset_to_bmap(bset2)));
8230 /* Order divs such that any div only depends on previous divs */
8231 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8233 int i;
8234 unsigned off;
8236 if (!bmap)
8237 return NULL;
8239 off = isl_space_dim(bmap->dim, isl_dim_all);
8241 for (i = 0; i < bmap->n_div; ++i) {
8242 int pos;
8243 if (isl_int_is_zero(bmap->div[i][0]))
8244 continue;
8245 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8246 bmap->n_div-i);
8247 if (pos == -1)
8248 continue;
8249 if (pos == 0)
8250 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8251 "integer division depends on itself",
8252 return isl_basic_map_free(bmap));
8253 isl_basic_map_swap_div(bmap, i, i + pos);
8254 --i;
8256 return bmap;
8259 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8261 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8264 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8266 int i;
8268 if (!map)
8269 return 0;
8271 for (i = 0; i < map->n; ++i) {
8272 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8273 if (!map->p[i])
8274 goto error;
8277 return map;
8278 error:
8279 isl_map_free(map);
8280 return NULL;
8283 /* Sort the local variables of "bset".
8285 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8286 __isl_take isl_basic_set *bset)
8288 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8291 /* Apply the expansion computed by isl_merge_divs.
8292 * The expansion itself is given by "exp" while the resulting
8293 * list of divs is given by "div".
8295 * Move the integer divisions of "bmap" into the right position
8296 * according to "exp" and then introduce the additional integer
8297 * divisions, adding div constraints.
8298 * The moving should be done first to avoid moving coefficients
8299 * in the definitions of the extra integer divisions.
8301 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8302 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8304 int i, j;
8305 int n_div;
8307 bmap = isl_basic_map_cow(bmap);
8308 if (!bmap || !div)
8309 goto error;
8311 if (div->n_row < bmap->n_div)
8312 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8313 "not an expansion", goto error);
8315 n_div = bmap->n_div;
8316 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8317 div->n_row - n_div, 0,
8318 2 * (div->n_row - n_div));
8320 for (i = n_div; i < div->n_row; ++i)
8321 if (isl_basic_map_alloc_div(bmap) < 0)
8322 goto error;
8324 for (j = n_div - 1; j >= 0; --j) {
8325 if (exp[j] == j)
8326 break;
8327 isl_basic_map_swap_div(bmap, j, exp[j]);
8329 j = 0;
8330 for (i = 0; i < div->n_row; ++i) {
8331 if (j < n_div && exp[j] == i) {
8332 j++;
8333 } else {
8334 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8335 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8336 continue;
8337 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8338 goto error;
8342 isl_mat_free(div);
8343 return bmap;
8344 error:
8345 isl_basic_map_free(bmap);
8346 isl_mat_free(div);
8347 return NULL;
8350 /* Apply the expansion computed by isl_merge_divs.
8351 * The expansion itself is given by "exp" while the resulting
8352 * list of divs is given by "div".
8354 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8355 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8357 return isl_basic_map_expand_divs(bset, div, exp);
8360 /* Look for a div in dst that corresponds to the div "div" in src.
8361 * The divs before "div" in src and dst are assumed to be the same.
8363 * Returns -1 if no corresponding div was found and the position
8364 * of the corresponding div in dst otherwise.
8366 static int find_div(struct isl_basic_map *dst,
8367 struct isl_basic_map *src, unsigned div)
8369 int i;
8371 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8373 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8374 for (i = div; i < dst->n_div; ++i)
8375 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8376 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8377 dst->n_div - div) == -1)
8378 return i;
8379 return -1;
8382 /* Align the divs of "dst" to those of "src", adding divs from "src"
8383 * if needed. That is, make sure that the first src->n_div divs
8384 * of the result are equal to those of src.
8386 * The result is not finalized as by design it will have redundant
8387 * divs if any divs from "src" were copied.
8389 __isl_give isl_basic_map *isl_basic_map_align_divs(
8390 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8392 int i;
8393 int known, extended;
8394 unsigned total;
8396 if (!dst || !src)
8397 return isl_basic_map_free(dst);
8399 if (src->n_div == 0)
8400 return dst;
8402 known = isl_basic_map_divs_known(src);
8403 if (known < 0)
8404 return isl_basic_map_free(dst);
8405 if (!known)
8406 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8407 "some src divs are unknown",
8408 return isl_basic_map_free(dst));
8410 src = isl_basic_map_order_divs(src);
8412 extended = 0;
8413 total = isl_space_dim(src->dim, isl_dim_all);
8414 for (i = 0; i < src->n_div; ++i) {
8415 int j = find_div(dst, src, i);
8416 if (j < 0) {
8417 if (!extended) {
8418 int extra = src->n_div - i;
8419 dst = isl_basic_map_cow(dst);
8420 if (!dst)
8421 return NULL;
8422 dst = isl_basic_map_extend_space(dst,
8423 isl_space_copy(dst->dim),
8424 extra, 0, 2 * extra);
8425 extended = 1;
8427 j = isl_basic_map_alloc_div(dst);
8428 if (j < 0)
8429 return isl_basic_map_free(dst);
8430 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8431 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8432 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8433 return isl_basic_map_free(dst);
8435 if (j != i)
8436 isl_basic_map_swap_div(dst, i, j);
8438 return dst;
8441 struct isl_map *isl_map_align_divs(struct isl_map *map)
8443 int i;
8445 if (!map)
8446 return NULL;
8447 if (map->n == 0)
8448 return map;
8449 map = isl_map_compute_divs(map);
8450 map = isl_map_cow(map);
8451 if (!map)
8452 return NULL;
8454 for (i = 1; i < map->n; ++i)
8455 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8456 for (i = 1; i < map->n; ++i) {
8457 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8458 if (!map->p[i])
8459 return isl_map_free(map);
8462 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8463 return map;
8466 struct isl_set *isl_set_align_divs(struct isl_set *set)
8468 return set_from_map(isl_map_align_divs(set_to_map(set)));
8471 /* Align the divs of the basic maps in "map" to those
8472 * of the basic maps in "list", as well as to the other basic maps in "map".
8473 * The elements in "list" are assumed to have known divs.
8475 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8476 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8478 int i, n;
8480 map = isl_map_compute_divs(map);
8481 map = isl_map_cow(map);
8482 if (!map || !list)
8483 return isl_map_free(map);
8484 if (map->n == 0)
8485 return map;
8487 n = isl_basic_map_list_n_basic_map(list);
8488 for (i = 0; i < n; ++i) {
8489 isl_basic_map *bmap;
8491 bmap = isl_basic_map_list_get_basic_map(list, i);
8492 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8493 isl_basic_map_free(bmap);
8495 if (!map->p[0])
8496 return isl_map_free(map);
8498 return isl_map_align_divs(map);
8501 /* Align the divs of each element of "list" to those of "bmap".
8502 * Both "bmap" and the elements of "list" are assumed to have known divs.
8504 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8505 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8507 int i, n;
8509 if (!list || !bmap)
8510 return isl_basic_map_list_free(list);
8512 n = isl_basic_map_list_n_basic_map(list);
8513 for (i = 0; i < n; ++i) {
8514 isl_basic_map *bmap_i;
8516 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8517 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8518 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8521 return list;
8524 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8525 __isl_take isl_map *map)
8527 isl_bool ok;
8529 ok = isl_map_compatible_domain(map, set);
8530 if (ok < 0)
8531 goto error;
8532 if (!ok)
8533 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8534 "incompatible spaces", goto error);
8535 map = isl_map_intersect_domain(map, set);
8536 set = isl_map_range(map);
8537 return set;
8538 error:
8539 isl_set_free(set);
8540 isl_map_free(map);
8541 return NULL;
8544 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8545 __isl_take isl_map *map)
8547 return isl_map_align_params_map_map_and(set, map, &set_apply);
8550 /* There is no need to cow as removing empty parts doesn't change
8551 * the meaning of the set.
8553 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8555 int i;
8557 if (!map)
8558 return NULL;
8560 for (i = map->n - 1; i >= 0; --i)
8561 remove_if_empty(map, i);
8563 return map;
8566 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8568 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8571 /* Given two basic sets bset1 and bset2, compute the maximal difference
8572 * between the values of dimension pos in bset1 and those in bset2
8573 * for any common value of the parameters and dimensions preceding pos.
8575 static enum isl_lp_result basic_set_maximal_difference_at(
8576 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8577 int pos, isl_int *opt)
8579 isl_basic_map *bmap1;
8580 isl_basic_map *bmap2;
8581 struct isl_ctx *ctx;
8582 struct isl_vec *obj;
8583 unsigned total;
8584 unsigned nparam;
8585 unsigned dim1;
8586 enum isl_lp_result res;
8588 if (!bset1 || !bset2)
8589 return isl_lp_error;
8591 nparam = isl_basic_set_n_param(bset1);
8592 dim1 = isl_basic_set_n_dim(bset1);
8594 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8595 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8596 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8597 isl_dim_out, 0, pos);
8598 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8599 isl_dim_out, 0, pos);
8600 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8601 if (!bmap1)
8602 return isl_lp_error;
8604 total = isl_basic_map_total_dim(bmap1);
8605 ctx = bmap1->ctx;
8606 obj = isl_vec_alloc(ctx, 1 + total);
8607 if (!obj)
8608 goto error;
8609 isl_seq_clr(obj->block.data, 1 + total);
8610 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8611 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8612 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8613 opt, NULL, NULL);
8614 isl_basic_map_free(bmap1);
8615 isl_vec_free(obj);
8616 return res;
8617 error:
8618 isl_basic_map_free(bmap1);
8619 return isl_lp_error;
8622 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8623 * for any common value of the parameters and dimensions preceding pos
8624 * in both basic sets, the values of dimension pos in bset1 are
8625 * smaller or larger than those in bset2.
8627 * Returns
8628 * 1 if bset1 follows bset2
8629 * -1 if bset1 precedes bset2
8630 * 0 if bset1 and bset2 are incomparable
8631 * -2 if some error occurred.
8633 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8634 struct isl_basic_set *bset2, int pos)
8636 isl_int opt;
8637 enum isl_lp_result res;
8638 int cmp;
8640 isl_int_init(opt);
8642 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8644 if (res == isl_lp_empty)
8645 cmp = 0;
8646 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8647 res == isl_lp_unbounded)
8648 cmp = 1;
8649 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8650 cmp = -1;
8651 else
8652 cmp = -2;
8654 isl_int_clear(opt);
8655 return cmp;
8658 /* Given two basic sets bset1 and bset2, check whether
8659 * for any common value of the parameters and dimensions preceding pos
8660 * there is a value of dimension pos in bset1 that is larger
8661 * than a value of the same dimension in bset2.
8663 * Return
8664 * 1 if there exists such a pair
8665 * 0 if there is no such pair, but there is a pair of equal values
8666 * -1 otherwise
8667 * -2 if some error occurred.
8669 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8670 __isl_keep isl_basic_set *bset2, int pos)
8672 isl_int opt;
8673 enum isl_lp_result res;
8674 int cmp;
8676 isl_int_init(opt);
8678 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8680 if (res == isl_lp_empty)
8681 cmp = -1;
8682 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8683 res == isl_lp_unbounded)
8684 cmp = 1;
8685 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8686 cmp = -1;
8687 else if (res == isl_lp_ok)
8688 cmp = 0;
8689 else
8690 cmp = -2;
8692 isl_int_clear(opt);
8693 return cmp;
8696 /* Given two sets set1 and set2, check whether
8697 * for any common value of the parameters and dimensions preceding pos
8698 * there is a value of dimension pos in set1 that is larger
8699 * than a value of the same dimension in set2.
8701 * Return
8702 * 1 if there exists such a pair
8703 * 0 if there is no such pair, but there is a pair of equal values
8704 * -1 otherwise
8705 * -2 if some error occurred.
8707 int isl_set_follows_at(__isl_keep isl_set *set1,
8708 __isl_keep isl_set *set2, int pos)
8710 int i, j;
8711 int follows = -1;
8713 if (!set1 || !set2)
8714 return -2;
8716 for (i = 0; i < set1->n; ++i)
8717 for (j = 0; j < set2->n; ++j) {
8718 int f;
8719 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8720 if (f == 1 || f == -2)
8721 return f;
8722 if (f > follows)
8723 follows = f;
8726 return follows;
8729 static isl_bool isl_basic_map_plain_has_fixed_var(
8730 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8732 int i;
8733 int d;
8734 unsigned total;
8736 if (!bmap)
8737 return isl_bool_error;
8738 total = isl_basic_map_total_dim(bmap);
8739 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8740 for (; d+1 > pos; --d)
8741 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8742 break;
8743 if (d != pos)
8744 continue;
8745 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8746 return isl_bool_false;
8747 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8748 return isl_bool_false;
8749 if (!isl_int_is_one(bmap->eq[i][1+d]))
8750 return isl_bool_false;
8751 if (val)
8752 isl_int_neg(*val, bmap->eq[i][0]);
8753 return isl_bool_true;
8755 return isl_bool_false;
8758 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8759 unsigned pos, isl_int *val)
8761 int i;
8762 isl_int v;
8763 isl_int tmp;
8764 isl_bool fixed;
8766 if (!map)
8767 return isl_bool_error;
8768 if (map->n == 0)
8769 return isl_bool_false;
8770 if (map->n == 1)
8771 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8772 isl_int_init(v);
8773 isl_int_init(tmp);
8774 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8775 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
8776 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8777 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
8778 fixed = isl_bool_false;
8780 if (val)
8781 isl_int_set(*val, v);
8782 isl_int_clear(tmp);
8783 isl_int_clear(v);
8784 return fixed;
8787 static isl_bool isl_basic_set_plain_has_fixed_var(
8788 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
8790 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8791 pos, val);
8794 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8795 enum isl_dim_type type, unsigned pos, isl_int *val)
8797 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
8798 return isl_bool_error;
8799 return isl_basic_map_plain_has_fixed_var(bmap,
8800 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8803 /* If "bmap" obviously lies on a hyperplane where the given dimension
8804 * has a fixed value, then return that value.
8805 * Otherwise return NaN.
8807 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8808 __isl_keep isl_basic_map *bmap,
8809 enum isl_dim_type type, unsigned pos)
8811 isl_ctx *ctx;
8812 isl_val *v;
8813 isl_bool fixed;
8815 if (!bmap)
8816 return NULL;
8817 ctx = isl_basic_map_get_ctx(bmap);
8818 v = isl_val_alloc(ctx);
8819 if (!v)
8820 return NULL;
8821 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8822 if (fixed < 0)
8823 return isl_val_free(v);
8824 if (fixed) {
8825 isl_int_set_si(v->d, 1);
8826 return v;
8828 isl_val_free(v);
8829 return isl_val_nan(ctx);
8832 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
8833 enum isl_dim_type type, unsigned pos, isl_int *val)
8835 if (pos >= isl_map_dim(map, type))
8836 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8837 "position out of bounds", return isl_bool_error);
8838 return isl_map_plain_has_fixed_var(map,
8839 map_offset(map, type) - 1 + pos, val);
8842 /* If "map" obviously lies on a hyperplane where the given dimension
8843 * has a fixed value, then return that value.
8844 * Otherwise return NaN.
8846 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8847 enum isl_dim_type type, unsigned pos)
8849 isl_ctx *ctx;
8850 isl_val *v;
8851 isl_bool fixed;
8853 if (!map)
8854 return NULL;
8855 ctx = isl_map_get_ctx(map);
8856 v = isl_val_alloc(ctx);
8857 if (!v)
8858 return NULL;
8859 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8860 if (fixed < 0)
8861 return isl_val_free(v);
8862 if (fixed) {
8863 isl_int_set_si(v->d, 1);
8864 return v;
8866 isl_val_free(v);
8867 return isl_val_nan(ctx);
8870 /* If "set" obviously lies on a hyperplane where the given dimension
8871 * has a fixed value, then return that value.
8872 * Otherwise return NaN.
8874 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8875 enum isl_dim_type type, unsigned pos)
8877 return isl_map_plain_get_val_if_fixed(set, type, pos);
8880 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
8881 enum isl_dim_type type, unsigned pos, isl_int *val)
8883 return isl_map_plain_is_fixed(set, type, pos, val);
8886 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8887 * then return this fixed value in *val.
8889 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8890 unsigned dim, isl_int *val)
8892 return isl_basic_set_plain_has_fixed_var(bset,
8893 isl_basic_set_n_param(bset) + dim, val);
8896 /* Return -1 if the constraint "c1" should be sorted before "c2"
8897 * and 1 if it should be sorted after "c2".
8898 * Return 0 if the two constraints are the same (up to the constant term).
8900 * In particular, if a constraint involves later variables than another
8901 * then it is sorted after this other constraint.
8902 * uset_gist depends on constraints without existentially quantified
8903 * variables sorting first.
8905 * For constraints that have the same latest variable, those
8906 * with the same coefficient for this latest variable (first in absolute value
8907 * and then in actual value) are grouped together.
8908 * This is useful for detecting pairs of constraints that can
8909 * be chained in their printed representation.
8911 * Finally, within a group, constraints are sorted according to
8912 * their coefficients (excluding the constant term).
8914 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8916 isl_int **c1 = (isl_int **) p1;
8917 isl_int **c2 = (isl_int **) p2;
8918 int l1, l2;
8919 unsigned size = *(unsigned *) arg;
8920 int cmp;
8922 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8923 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8925 if (l1 != l2)
8926 return l1 - l2;
8928 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8929 if (cmp != 0)
8930 return cmp;
8931 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8932 if (cmp != 0)
8933 return -cmp;
8935 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8938 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
8939 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
8940 * and 0 if the two constraints are the same (up to the constant term).
8942 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
8943 isl_int *c1, isl_int *c2)
8945 unsigned total;
8947 if (!bmap)
8948 return -2;
8949 total = isl_basic_map_total_dim(bmap);
8950 return sort_constraint_cmp(&c1, &c2, &total);
8953 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
8954 __isl_take isl_basic_map *bmap)
8956 unsigned total;
8958 if (!bmap)
8959 return NULL;
8960 if (bmap->n_ineq == 0)
8961 return bmap;
8962 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8963 return bmap;
8964 total = isl_basic_map_total_dim(bmap);
8965 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
8966 &sort_constraint_cmp, &total) < 0)
8967 return isl_basic_map_free(bmap);
8968 return bmap;
8971 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8972 __isl_take isl_basic_set *bset)
8974 isl_basic_map *bmap = bset_to_bmap(bset);
8975 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
8978 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8980 if (!bmap)
8981 return NULL;
8982 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8983 return bmap;
8984 bmap = isl_basic_map_remove_redundancies(bmap);
8985 bmap = isl_basic_map_sort_constraints(bmap);
8986 if (bmap)
8987 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8988 return bmap;
8990 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
8991 __isl_keep isl_basic_map *bmap2)
8993 int i, cmp;
8994 unsigned total;
8996 if (!bmap1 || !bmap2)
8997 return -1;
8999 if (bmap1 == bmap2)
9000 return 0;
9001 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9002 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9003 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9004 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9005 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9006 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9007 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9008 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9009 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9010 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9011 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9012 return 0;
9013 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9014 return 1;
9015 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9016 return -1;
9017 if (bmap1->n_eq != bmap2->n_eq)
9018 return bmap1->n_eq - bmap2->n_eq;
9019 if (bmap1->n_ineq != bmap2->n_ineq)
9020 return bmap1->n_ineq - bmap2->n_ineq;
9021 if (bmap1->n_div != bmap2->n_div)
9022 return bmap1->n_div - bmap2->n_div;
9023 total = isl_basic_map_total_dim(bmap1);
9024 for (i = 0; i < bmap1->n_eq; ++i) {
9025 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9026 if (cmp)
9027 return cmp;
9029 for (i = 0; i < bmap1->n_ineq; ++i) {
9030 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9031 if (cmp)
9032 return cmp;
9034 for (i = 0; i < bmap1->n_div; ++i) {
9035 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9036 if (cmp)
9037 return cmp;
9039 return 0;
9042 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9043 __isl_keep isl_basic_set *bset2)
9045 return isl_basic_map_plain_cmp(bset1, bset2);
9048 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9050 int i, cmp;
9052 if (set1 == set2)
9053 return 0;
9054 if (set1->n != set2->n)
9055 return set1->n - set2->n;
9057 for (i = 0; i < set1->n; ++i) {
9058 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9059 if (cmp)
9060 return cmp;
9063 return 0;
9066 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9067 __isl_keep isl_basic_map *bmap2)
9069 if (!bmap1 || !bmap2)
9070 return isl_bool_error;
9071 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9074 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9075 __isl_keep isl_basic_set *bset2)
9077 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9078 bset_to_bmap(bset2));
9081 static int qsort_bmap_cmp(const void *p1, const void *p2)
9083 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9084 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9086 return isl_basic_map_plain_cmp(bmap1, bmap2);
9089 /* Sort the basic maps of "map" and remove duplicate basic maps.
9091 * While removing basic maps, we make sure that the basic maps remain
9092 * sorted because isl_map_normalize expects the basic maps of the result
9093 * to be sorted.
9095 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9097 int i, j;
9099 map = isl_map_remove_empty_parts(map);
9100 if (!map)
9101 return NULL;
9102 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9103 for (i = map->n - 1; i >= 1; --i) {
9104 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9105 continue;
9106 isl_basic_map_free(map->p[i-1]);
9107 for (j = i; j < map->n; ++j)
9108 map->p[j - 1] = map->p[j];
9109 map->n--;
9112 return map;
9115 /* Remove obvious duplicates among the basic maps of "map".
9117 * Unlike isl_map_normalize, this function does not remove redundant
9118 * constraints and only removes duplicates that have exactly the same
9119 * constraints in the input. It does sort the constraints and
9120 * the basic maps to ease the detection of duplicates.
9122 * If "map" has already been normalized or if the basic maps are
9123 * disjoint, then there can be no duplicates.
9125 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9127 int i;
9128 isl_basic_map *bmap;
9130 if (!map)
9131 return NULL;
9132 if (map->n <= 1)
9133 return map;
9134 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9135 return map;
9136 for (i = 0; i < map->n; ++i) {
9137 bmap = isl_basic_map_copy(map->p[i]);
9138 bmap = isl_basic_map_sort_constraints(bmap);
9139 if (!bmap)
9140 return isl_map_free(map);
9141 isl_basic_map_free(map->p[i]);
9142 map->p[i] = bmap;
9145 map = sort_and_remove_duplicates(map);
9146 return map;
9149 /* We normalize in place, but if anything goes wrong we need
9150 * to return NULL, so we need to make sure we don't change the
9151 * meaning of any possible other copies of map.
9153 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9155 int i;
9156 struct isl_basic_map *bmap;
9158 if (!map)
9159 return NULL;
9160 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9161 return map;
9162 for (i = 0; i < map->n; ++i) {
9163 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9164 if (!bmap)
9165 goto error;
9166 isl_basic_map_free(map->p[i]);
9167 map->p[i] = bmap;
9170 map = sort_and_remove_duplicates(map);
9171 if (map)
9172 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9173 return map;
9174 error:
9175 isl_map_free(map);
9176 return NULL;
9179 struct isl_set *isl_set_normalize(struct isl_set *set)
9181 return set_from_map(isl_map_normalize(set_to_map(set)));
9184 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9185 __isl_keep isl_map *map2)
9187 int i;
9188 isl_bool equal;
9190 if (!map1 || !map2)
9191 return isl_bool_error;
9193 if (map1 == map2)
9194 return isl_bool_true;
9195 if (!isl_space_is_equal(map1->dim, map2->dim))
9196 return isl_bool_false;
9198 map1 = isl_map_copy(map1);
9199 map2 = isl_map_copy(map2);
9200 map1 = isl_map_normalize(map1);
9201 map2 = isl_map_normalize(map2);
9202 if (!map1 || !map2)
9203 goto error;
9204 equal = map1->n == map2->n;
9205 for (i = 0; equal && i < map1->n; ++i) {
9206 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9207 if (equal < 0)
9208 goto error;
9210 isl_map_free(map1);
9211 isl_map_free(map2);
9212 return equal;
9213 error:
9214 isl_map_free(map1);
9215 isl_map_free(map2);
9216 return isl_bool_error;
9219 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9220 __isl_keep isl_set *set2)
9222 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9225 /* Return the basic maps in "map" as a list.
9227 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9228 __isl_keep isl_map *map)
9230 int i;
9231 isl_ctx *ctx;
9232 isl_basic_map_list *list;
9234 if (!map)
9235 return NULL;
9236 ctx = isl_map_get_ctx(map);
9237 list = isl_basic_map_list_alloc(ctx, map->n);
9239 for (i = 0; i < map->n; ++i) {
9240 isl_basic_map *bmap;
9242 bmap = isl_basic_map_copy(map->p[i]);
9243 list = isl_basic_map_list_add(list, bmap);
9246 return list;
9249 /* Return the intersection of the elements in the non-empty list "list".
9250 * All elements are assumed to live in the same space.
9252 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9253 __isl_take isl_basic_map_list *list)
9255 int i, n;
9256 isl_basic_map *bmap;
9258 if (!list)
9259 return NULL;
9260 n = isl_basic_map_list_n_basic_map(list);
9261 if (n < 1)
9262 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9263 "expecting non-empty list", goto error);
9265 bmap = isl_basic_map_list_get_basic_map(list, 0);
9266 for (i = 1; i < n; ++i) {
9267 isl_basic_map *bmap_i;
9269 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9270 bmap = isl_basic_map_intersect(bmap, bmap_i);
9273 isl_basic_map_list_free(list);
9274 return bmap;
9275 error:
9276 isl_basic_map_list_free(list);
9277 return NULL;
9280 /* Return the intersection of the elements in the non-empty list "list".
9281 * All elements are assumed to live in the same space.
9283 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9284 __isl_take isl_basic_set_list *list)
9286 return isl_basic_map_list_intersect(list);
9289 /* Return the union of the elements of "list".
9290 * The list is required to have at least one element.
9292 __isl_give isl_set *isl_basic_set_list_union(
9293 __isl_take isl_basic_set_list *list)
9295 int i, n;
9296 isl_space *space;
9297 isl_basic_set *bset;
9298 isl_set *set;
9300 if (!list)
9301 return NULL;
9302 n = isl_basic_set_list_n_basic_set(list);
9303 if (n < 1)
9304 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9305 "expecting non-empty list", goto error);
9307 bset = isl_basic_set_list_get_basic_set(list, 0);
9308 space = isl_basic_set_get_space(bset);
9309 isl_basic_set_free(bset);
9311 set = isl_set_alloc_space(space, n, 0);
9312 for (i = 0; i < n; ++i) {
9313 bset = isl_basic_set_list_get_basic_set(list, i);
9314 set = isl_set_add_basic_set(set, bset);
9317 isl_basic_set_list_free(list);
9318 return set;
9319 error:
9320 isl_basic_set_list_free(list);
9321 return NULL;
9324 /* Return the union of the elements in the non-empty list "list".
9325 * All elements are assumed to live in the same space.
9327 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9329 int i, n;
9330 isl_set *set;
9332 if (!list)
9333 return NULL;
9334 n = isl_set_list_n_set(list);
9335 if (n < 1)
9336 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9337 "expecting non-empty list", goto error);
9339 set = isl_set_list_get_set(list, 0);
9340 for (i = 1; i < n; ++i) {
9341 isl_set *set_i;
9343 set_i = isl_set_list_get_set(list, i);
9344 set = isl_set_union(set, set_i);
9347 isl_set_list_free(list);
9348 return set;
9349 error:
9350 isl_set_list_free(list);
9351 return NULL;
9354 struct isl_basic_map *isl_basic_map_product(
9355 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9357 isl_space *dim_result = NULL;
9358 struct isl_basic_map *bmap;
9359 unsigned in1, in2, out1, out2, nparam, total, pos;
9360 struct isl_dim_map *dim_map1, *dim_map2;
9362 if (!bmap1 || !bmap2)
9363 goto error;
9365 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9366 bmap2->dim, isl_dim_param), goto error);
9367 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9368 isl_space_copy(bmap2->dim));
9370 in1 = isl_basic_map_n_in(bmap1);
9371 in2 = isl_basic_map_n_in(bmap2);
9372 out1 = isl_basic_map_n_out(bmap1);
9373 out2 = isl_basic_map_n_out(bmap2);
9374 nparam = isl_basic_map_n_param(bmap1);
9376 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9377 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9378 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9379 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9380 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9381 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9382 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9383 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9384 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9385 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9386 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9388 bmap = isl_basic_map_alloc_space(dim_result,
9389 bmap1->n_div + bmap2->n_div,
9390 bmap1->n_eq + bmap2->n_eq,
9391 bmap1->n_ineq + bmap2->n_ineq);
9392 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9393 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9394 bmap = isl_basic_map_simplify(bmap);
9395 return isl_basic_map_finalize(bmap);
9396 error:
9397 isl_basic_map_free(bmap1);
9398 isl_basic_map_free(bmap2);
9399 return NULL;
9402 __isl_give isl_basic_map *isl_basic_map_flat_product(
9403 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9405 isl_basic_map *prod;
9407 prod = isl_basic_map_product(bmap1, bmap2);
9408 prod = isl_basic_map_flatten(prod);
9409 return prod;
9412 __isl_give isl_basic_set *isl_basic_set_flat_product(
9413 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9415 return isl_basic_map_flat_range_product(bset1, bset2);
9418 __isl_give isl_basic_map *isl_basic_map_domain_product(
9419 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9421 isl_space *space_result = NULL;
9422 isl_basic_map *bmap;
9423 unsigned in1, in2, out, nparam, total, pos;
9424 struct isl_dim_map *dim_map1, *dim_map2;
9426 if (!bmap1 || !bmap2)
9427 goto error;
9429 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9430 isl_space_copy(bmap2->dim));
9432 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9433 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9434 out = isl_basic_map_dim(bmap1, isl_dim_out);
9435 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9437 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9438 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9439 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9440 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9441 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9442 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9443 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9444 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9445 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9446 isl_dim_map_div(dim_map1, bmap1, pos += out);
9447 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9449 bmap = isl_basic_map_alloc_space(space_result,
9450 bmap1->n_div + bmap2->n_div,
9451 bmap1->n_eq + bmap2->n_eq,
9452 bmap1->n_ineq + bmap2->n_ineq);
9453 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9454 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9455 bmap = isl_basic_map_simplify(bmap);
9456 return isl_basic_map_finalize(bmap);
9457 error:
9458 isl_basic_map_free(bmap1);
9459 isl_basic_map_free(bmap2);
9460 return NULL;
9463 __isl_give isl_basic_map *isl_basic_map_range_product(
9464 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9466 isl_bool rational;
9467 isl_space *dim_result = NULL;
9468 isl_basic_map *bmap;
9469 unsigned in, out1, out2, nparam, total, pos;
9470 struct isl_dim_map *dim_map1, *dim_map2;
9472 rational = isl_basic_map_is_rational(bmap1);
9473 if (rational >= 0 && rational)
9474 rational = isl_basic_map_is_rational(bmap2);
9475 if (!bmap1 || !bmap2 || rational < 0)
9476 goto error;
9478 if (!isl_space_match(bmap1->dim, isl_dim_param,
9479 bmap2->dim, isl_dim_param))
9480 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9481 "parameters don't match", goto error);
9483 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9484 isl_space_copy(bmap2->dim));
9486 in = isl_basic_map_dim(bmap1, isl_dim_in);
9487 out1 = isl_basic_map_n_out(bmap1);
9488 out2 = isl_basic_map_n_out(bmap2);
9489 nparam = isl_basic_map_n_param(bmap1);
9491 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9492 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9493 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9494 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9495 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9496 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9497 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9498 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9499 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9500 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9501 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9503 bmap = isl_basic_map_alloc_space(dim_result,
9504 bmap1->n_div + bmap2->n_div,
9505 bmap1->n_eq + bmap2->n_eq,
9506 bmap1->n_ineq + bmap2->n_ineq);
9507 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9508 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9509 if (rational)
9510 bmap = isl_basic_map_set_rational(bmap);
9511 bmap = isl_basic_map_simplify(bmap);
9512 return isl_basic_map_finalize(bmap);
9513 error:
9514 isl_basic_map_free(bmap1);
9515 isl_basic_map_free(bmap2);
9516 return NULL;
9519 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9520 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9522 isl_basic_map *prod;
9524 prod = isl_basic_map_range_product(bmap1, bmap2);
9525 prod = isl_basic_map_flatten_range(prod);
9526 return prod;
9529 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9530 * and collect the results.
9531 * The result live in the space obtained by calling "space_product"
9532 * on the spaces of "map1" and "map2".
9533 * If "remove_duplicates" is set then the result may contain duplicates
9534 * (even if the inputs do not) and so we try and remove the obvious
9535 * duplicates.
9537 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9538 __isl_take isl_map *map2,
9539 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9540 __isl_take isl_space *right),
9541 __isl_give isl_basic_map *(*basic_map_product)(
9542 __isl_take isl_basic_map *left,
9543 __isl_take isl_basic_map *right),
9544 int remove_duplicates)
9546 unsigned flags = 0;
9547 struct isl_map *result;
9548 int i, j;
9550 if (!map1 || !map2)
9551 goto error;
9553 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9554 map2->dim, isl_dim_param), goto error);
9556 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9557 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9558 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9560 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9561 isl_space_copy(map2->dim)),
9562 map1->n * map2->n, flags);
9563 if (!result)
9564 goto error;
9565 for (i = 0; i < map1->n; ++i)
9566 for (j = 0; j < map2->n; ++j) {
9567 struct isl_basic_map *part;
9568 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9569 isl_basic_map_copy(map2->p[j]));
9570 if (isl_basic_map_is_empty(part))
9571 isl_basic_map_free(part);
9572 else
9573 result = isl_map_add_basic_map(result, part);
9574 if (!result)
9575 goto error;
9577 if (remove_duplicates)
9578 result = isl_map_remove_obvious_duplicates(result);
9579 isl_map_free(map1);
9580 isl_map_free(map2);
9581 return result;
9582 error:
9583 isl_map_free(map1);
9584 isl_map_free(map2);
9585 return NULL;
9588 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9590 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9591 __isl_take isl_map *map2)
9593 return map_product(map1, map2, &isl_space_product,
9594 &isl_basic_map_product, 0);
9597 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9598 __isl_take isl_map *map2)
9600 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9603 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9605 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9606 __isl_take isl_map *map2)
9608 isl_map *prod;
9610 prod = isl_map_product(map1, map2);
9611 prod = isl_map_flatten(prod);
9612 return prod;
9615 /* Given two set A and B, construct its Cartesian product A x B.
9617 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9619 return isl_map_range_product(set1, set2);
9622 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9623 __isl_take isl_set *set2)
9625 return isl_map_flat_range_product(set1, set2);
9628 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9630 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9631 __isl_take isl_map *map2)
9633 return map_product(map1, map2, &isl_space_domain_product,
9634 &isl_basic_map_domain_product, 1);
9637 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9639 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9640 __isl_take isl_map *map2)
9642 return map_product(map1, map2, &isl_space_range_product,
9643 &isl_basic_map_range_product, 1);
9646 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9647 __isl_take isl_map *map2)
9649 return isl_map_align_params_map_map_and(map1, map2,
9650 &map_domain_product_aligned);
9653 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9654 __isl_take isl_map *map2)
9656 return isl_map_align_params_map_map_and(map1, map2,
9657 &map_range_product_aligned);
9660 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9662 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9664 isl_space *space;
9665 int total1, keep1, total2, keep2;
9667 if (!map)
9668 return NULL;
9669 if (!isl_space_domain_is_wrapping(map->dim) ||
9670 !isl_space_range_is_wrapping(map->dim))
9671 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9672 "not a product", return isl_map_free(map));
9674 space = isl_map_get_space(map);
9675 total1 = isl_space_dim(space, isl_dim_in);
9676 total2 = isl_space_dim(space, isl_dim_out);
9677 space = isl_space_factor_domain(space);
9678 keep1 = isl_space_dim(space, isl_dim_in);
9679 keep2 = isl_space_dim(space, isl_dim_out);
9680 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9681 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9682 map = isl_map_reset_space(map, space);
9684 return map;
9687 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9689 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9691 isl_space *space;
9692 int total1, keep1, total2, keep2;
9694 if (!map)
9695 return NULL;
9696 if (!isl_space_domain_is_wrapping(map->dim) ||
9697 !isl_space_range_is_wrapping(map->dim))
9698 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9699 "not a product", return isl_map_free(map));
9701 space = isl_map_get_space(map);
9702 total1 = isl_space_dim(space, isl_dim_in);
9703 total2 = isl_space_dim(space, isl_dim_out);
9704 space = isl_space_factor_range(space);
9705 keep1 = isl_space_dim(space, isl_dim_in);
9706 keep2 = isl_space_dim(space, isl_dim_out);
9707 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9708 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9709 map = isl_map_reset_space(map, space);
9711 return map;
9714 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9716 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9718 isl_space *space;
9719 int total, keep;
9721 if (!map)
9722 return NULL;
9723 if (!isl_space_domain_is_wrapping(map->dim))
9724 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9725 "domain is not a product", return isl_map_free(map));
9727 space = isl_map_get_space(map);
9728 total = isl_space_dim(space, isl_dim_in);
9729 space = isl_space_domain_factor_domain(space);
9730 keep = isl_space_dim(space, isl_dim_in);
9731 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9732 map = isl_map_reset_space(map, space);
9734 return map;
9737 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9739 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9741 isl_space *space;
9742 int total, keep;
9744 if (!map)
9745 return NULL;
9746 if (!isl_space_domain_is_wrapping(map->dim))
9747 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9748 "domain is not a product", return isl_map_free(map));
9750 space = isl_map_get_space(map);
9751 total = isl_space_dim(space, isl_dim_in);
9752 space = isl_space_domain_factor_range(space);
9753 keep = isl_space_dim(space, isl_dim_in);
9754 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9755 map = isl_map_reset_space(map, space);
9757 return map;
9760 /* Given a map A -> [B -> C], extract the map A -> B.
9762 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9764 isl_space *space;
9765 int total, keep;
9767 if (!map)
9768 return NULL;
9769 if (!isl_space_range_is_wrapping(map->dim))
9770 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9771 "range is not a product", return isl_map_free(map));
9773 space = isl_map_get_space(map);
9774 total = isl_space_dim(space, isl_dim_out);
9775 space = isl_space_range_factor_domain(space);
9776 keep = isl_space_dim(space, isl_dim_out);
9777 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9778 map = isl_map_reset_space(map, space);
9780 return map;
9783 /* Given a map A -> [B -> C], extract the map A -> C.
9785 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9787 isl_space *space;
9788 int total, keep;
9790 if (!map)
9791 return NULL;
9792 if (!isl_space_range_is_wrapping(map->dim))
9793 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9794 "range is not a product", return isl_map_free(map));
9796 space = isl_map_get_space(map);
9797 total = isl_space_dim(space, isl_dim_out);
9798 space = isl_space_range_factor_range(space);
9799 keep = isl_space_dim(space, isl_dim_out);
9800 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9801 map = isl_map_reset_space(map, space);
9803 return map;
9806 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9808 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9809 __isl_take isl_map *map2)
9811 isl_map *prod;
9813 prod = isl_map_domain_product(map1, map2);
9814 prod = isl_map_flatten_domain(prod);
9815 return prod;
9818 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9820 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9821 __isl_take isl_map *map2)
9823 isl_map *prod;
9825 prod = isl_map_range_product(map1, map2);
9826 prod = isl_map_flatten_range(prod);
9827 return prod;
9830 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9832 int i;
9833 uint32_t hash = isl_hash_init();
9834 unsigned total;
9836 if (!bmap)
9837 return 0;
9838 bmap = isl_basic_map_copy(bmap);
9839 bmap = isl_basic_map_normalize(bmap);
9840 if (!bmap)
9841 return 0;
9842 total = isl_basic_map_total_dim(bmap);
9843 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9844 for (i = 0; i < bmap->n_eq; ++i) {
9845 uint32_t c_hash;
9846 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9847 isl_hash_hash(hash, c_hash);
9849 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9850 for (i = 0; i < bmap->n_ineq; ++i) {
9851 uint32_t c_hash;
9852 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9853 isl_hash_hash(hash, c_hash);
9855 isl_hash_byte(hash, bmap->n_div & 0xFF);
9856 for (i = 0; i < bmap->n_div; ++i) {
9857 uint32_t c_hash;
9858 if (isl_int_is_zero(bmap->div[i][0]))
9859 continue;
9860 isl_hash_byte(hash, i & 0xFF);
9861 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9862 isl_hash_hash(hash, c_hash);
9864 isl_basic_map_free(bmap);
9865 return hash;
9868 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9870 return isl_basic_map_get_hash(bset_to_bmap(bset));
9873 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9875 int i;
9876 uint32_t hash;
9878 if (!map)
9879 return 0;
9880 map = isl_map_copy(map);
9881 map = isl_map_normalize(map);
9882 if (!map)
9883 return 0;
9885 hash = isl_hash_init();
9886 for (i = 0; i < map->n; ++i) {
9887 uint32_t bmap_hash;
9888 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9889 isl_hash_hash(hash, bmap_hash);
9892 isl_map_free(map);
9894 return hash;
9897 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9899 return isl_map_get_hash(set_to_map(set));
9902 /* Return the number of basic maps in the (current) representation of "map".
9904 int isl_map_n_basic_map(__isl_keep isl_map *map)
9906 return map ? map->n : 0;
9909 int isl_set_n_basic_set(__isl_keep isl_set *set)
9911 return set ? set->n : 0;
9914 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
9915 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9917 int i;
9919 if (!map)
9920 return isl_stat_error;
9922 for (i = 0; i < map->n; ++i)
9923 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9924 return isl_stat_error;
9926 return isl_stat_ok;
9929 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
9930 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9932 int i;
9934 if (!set)
9935 return isl_stat_error;
9937 for (i = 0; i < set->n; ++i)
9938 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9939 return isl_stat_error;
9941 return isl_stat_ok;
9944 /* Return a list of basic sets, the union of which is equal to "set".
9946 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9947 __isl_keep isl_set *set)
9949 int i;
9950 isl_basic_set_list *list;
9952 if (!set)
9953 return NULL;
9955 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
9956 for (i = 0; i < set->n; ++i) {
9957 isl_basic_set *bset;
9959 bset = isl_basic_set_copy(set->p[i]);
9960 list = isl_basic_set_list_add(list, bset);
9963 return list;
9966 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9968 isl_space *dim;
9970 if (!bset)
9971 return NULL;
9973 bset = isl_basic_set_cow(bset);
9974 if (!bset)
9975 return NULL;
9977 dim = isl_basic_set_get_space(bset);
9978 dim = isl_space_lift(dim, bset->n_div);
9979 if (!dim)
9980 goto error;
9981 isl_space_free(bset->dim);
9982 bset->dim = dim;
9983 bset->extra -= bset->n_div;
9984 bset->n_div = 0;
9986 bset = isl_basic_set_finalize(bset);
9988 return bset;
9989 error:
9990 isl_basic_set_free(bset);
9991 return NULL;
9994 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9996 int i;
9997 isl_space *dim;
9998 unsigned n_div;
10000 set = isl_set_align_divs(set);
10002 if (!set)
10003 return NULL;
10005 set = isl_set_cow(set);
10006 if (!set)
10007 return NULL;
10009 n_div = set->p[0]->n_div;
10010 dim = isl_set_get_space(set);
10011 dim = isl_space_lift(dim, n_div);
10012 if (!dim)
10013 goto error;
10014 isl_space_free(set->dim);
10015 set->dim = dim;
10017 for (i = 0; i < set->n; ++i) {
10018 set->p[i] = isl_basic_set_lift(set->p[i]);
10019 if (!set->p[i])
10020 goto error;
10023 return set;
10024 error:
10025 isl_set_free(set);
10026 return NULL;
10029 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10031 unsigned dim;
10032 int size = 0;
10034 if (!bset)
10035 return -1;
10037 dim = isl_basic_set_total_dim(bset);
10038 size += bset->n_eq * (1 + dim);
10039 size += bset->n_ineq * (1 + dim);
10040 size += bset->n_div * (2 + dim);
10042 return size;
10045 int isl_set_size(__isl_keep isl_set *set)
10047 int i;
10048 int size = 0;
10050 if (!set)
10051 return -1;
10053 for (i = 0; i < set->n; ++i)
10054 size += isl_basic_set_size(set->p[i]);
10056 return size;
10059 /* Check if there is any lower bound (if lower == 0) and/or upper
10060 * bound (if upper == 0) on the specified dim.
10062 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10063 enum isl_dim_type type, unsigned pos, int lower, int upper)
10065 int i;
10067 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10068 return isl_bool_error;
10070 pos += isl_basic_map_offset(bmap, type);
10072 for (i = 0; i < bmap->n_div; ++i) {
10073 if (isl_int_is_zero(bmap->div[i][0]))
10074 continue;
10075 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10076 return isl_bool_true;
10079 for (i = 0; i < bmap->n_eq; ++i)
10080 if (!isl_int_is_zero(bmap->eq[i][pos]))
10081 return isl_bool_true;
10083 for (i = 0; i < bmap->n_ineq; ++i) {
10084 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10085 if (sgn > 0)
10086 lower = 1;
10087 if (sgn < 0)
10088 upper = 1;
10091 return lower && upper;
10094 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10095 enum isl_dim_type type, unsigned pos)
10097 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10100 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10101 enum isl_dim_type type, unsigned pos)
10103 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10106 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10107 enum isl_dim_type type, unsigned pos)
10109 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10112 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10113 enum isl_dim_type type, unsigned pos)
10115 int i;
10117 if (!map)
10118 return isl_bool_error;
10120 for (i = 0; i < map->n; ++i) {
10121 isl_bool bounded;
10122 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10123 if (bounded < 0 || !bounded)
10124 return bounded;
10127 return isl_bool_true;
10130 /* Return true if the specified dim is involved in both an upper bound
10131 * and a lower bound.
10133 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10134 enum isl_dim_type type, unsigned pos)
10136 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10139 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10141 static isl_bool has_any_bound(__isl_keep isl_map *map,
10142 enum isl_dim_type type, unsigned pos,
10143 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10144 enum isl_dim_type type, unsigned pos))
10146 int i;
10148 if (!map)
10149 return isl_bool_error;
10151 for (i = 0; i < map->n; ++i) {
10152 isl_bool bounded;
10153 bounded = fn(map->p[i], type, pos);
10154 if (bounded < 0 || bounded)
10155 return bounded;
10158 return isl_bool_false;
10161 /* Return 1 if the specified dim is involved in any lower bound.
10163 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10164 enum isl_dim_type type, unsigned pos)
10166 return has_any_bound(set, type, pos,
10167 &isl_basic_map_dim_has_lower_bound);
10170 /* Return 1 if the specified dim is involved in any upper bound.
10172 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10173 enum isl_dim_type type, unsigned pos)
10175 return has_any_bound(set, type, pos,
10176 &isl_basic_map_dim_has_upper_bound);
10179 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10181 static isl_bool has_bound(__isl_keep isl_map *map,
10182 enum isl_dim_type type, unsigned pos,
10183 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10184 enum isl_dim_type type, unsigned pos))
10186 int i;
10188 if (!map)
10189 return isl_bool_error;
10191 for (i = 0; i < map->n; ++i) {
10192 isl_bool bounded;
10193 bounded = fn(map->p[i], type, pos);
10194 if (bounded < 0 || !bounded)
10195 return bounded;
10198 return isl_bool_true;
10201 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10203 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10204 enum isl_dim_type type, unsigned pos)
10206 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10209 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10211 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10212 enum isl_dim_type type, unsigned pos)
10214 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10217 /* For each of the "n" variables starting at "first", determine
10218 * the sign of the variable and put the results in the first "n"
10219 * elements of the array "signs".
10220 * Sign
10221 * 1 means that the variable is non-negative
10222 * -1 means that the variable is non-positive
10223 * 0 means the variable attains both positive and negative values.
10225 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10226 unsigned first, unsigned n, int *signs)
10228 isl_vec *bound = NULL;
10229 struct isl_tab *tab = NULL;
10230 struct isl_tab_undo *snap;
10231 int i;
10233 if (!bset || !signs)
10234 return isl_stat_error;
10236 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10237 tab = isl_tab_from_basic_set(bset, 0);
10238 if (!bound || !tab)
10239 goto error;
10241 isl_seq_clr(bound->el, bound->size);
10242 isl_int_set_si(bound->el[0], -1);
10244 snap = isl_tab_snap(tab);
10245 for (i = 0; i < n; ++i) {
10246 int empty;
10248 isl_int_set_si(bound->el[1 + first + i], -1);
10249 if (isl_tab_add_ineq(tab, bound->el) < 0)
10250 goto error;
10251 empty = tab->empty;
10252 isl_int_set_si(bound->el[1 + first + i], 0);
10253 if (isl_tab_rollback(tab, snap) < 0)
10254 goto error;
10256 if (empty) {
10257 signs[i] = 1;
10258 continue;
10261 isl_int_set_si(bound->el[1 + first + i], 1);
10262 if (isl_tab_add_ineq(tab, bound->el) < 0)
10263 goto error;
10264 empty = tab->empty;
10265 isl_int_set_si(bound->el[1 + first + i], 0);
10266 if (isl_tab_rollback(tab, snap) < 0)
10267 goto error;
10269 signs[i] = empty ? -1 : 0;
10272 isl_tab_free(tab);
10273 isl_vec_free(bound);
10274 return isl_stat_ok;
10275 error:
10276 isl_tab_free(tab);
10277 isl_vec_free(bound);
10278 return isl_stat_error;
10281 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10282 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10284 if (!bset || !signs)
10285 return isl_stat_error;
10286 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10287 return isl_stat_error);
10289 first += pos(bset->dim, type) - 1;
10290 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10293 /* Is it possible for the integer division "div" to depend (possibly
10294 * indirectly) on any output dimensions?
10296 * If the div is undefined, then we conservatively assume that it
10297 * may depend on them.
10298 * Otherwise, we check if it actually depends on them or on any integer
10299 * divisions that may depend on them.
10301 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10303 int i;
10304 unsigned n_out, o_out;
10305 unsigned n_div, o_div;
10307 if (isl_int_is_zero(bmap->div[div][0]))
10308 return isl_bool_true;
10310 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10311 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10313 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10314 return isl_bool_true;
10316 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10317 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10319 for (i = 0; i < n_div; ++i) {
10320 isl_bool may_involve;
10322 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10323 continue;
10324 may_involve = div_may_involve_output(bmap, i);
10325 if (may_involve < 0 || may_involve)
10326 return may_involve;
10329 return isl_bool_false;
10332 /* Return the first integer division of "bmap" in the range
10333 * [first, first + n[ that may depend on any output dimensions and
10334 * that has a non-zero coefficient in "c" (where the first coefficient
10335 * in "c" corresponds to integer division "first").
10337 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10338 isl_int *c, int first, int n)
10340 int k;
10342 if (!bmap)
10343 return -1;
10345 for (k = first; k < first + n; ++k) {
10346 isl_bool may_involve;
10348 if (isl_int_is_zero(c[k]))
10349 continue;
10350 may_involve = div_may_involve_output(bmap, k);
10351 if (may_involve < 0)
10352 return -1;
10353 if (may_involve)
10354 return k;
10357 return first + n;
10360 /* Look for a pair of inequality constraints in "bmap" of the form
10362 * -l + i >= 0 or i >= l
10363 * and
10364 * n + l - i >= 0 or i <= l + n
10366 * with n < "m" and i the output dimension at position "pos".
10367 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10368 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10369 * and earlier output dimensions, as well as integer divisions that do
10370 * not involve any of the output dimensions.
10372 * Return the index of the first inequality constraint or bmap->n_ineq
10373 * if no such pair can be found.
10375 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10376 int pos, isl_int m)
10378 int i, j;
10379 isl_ctx *ctx;
10380 unsigned total;
10381 unsigned n_div, o_div;
10382 unsigned n_out, o_out;
10383 int less;
10385 if (!bmap)
10386 return -1;
10388 ctx = isl_basic_map_get_ctx(bmap);
10389 total = isl_basic_map_total_dim(bmap);
10390 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10391 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10392 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10393 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10394 for (i = 0; i < bmap->n_ineq; ++i) {
10395 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10396 continue;
10397 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10398 n_out - (pos + 1)) != -1)
10399 continue;
10400 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10401 0, n_div) < n_div)
10402 continue;
10403 for (j = i + 1; j < bmap->n_ineq; ++j) {
10404 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10405 ctx->one))
10406 continue;
10407 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10408 bmap->ineq[j] + 1, total))
10409 continue;
10410 break;
10412 if (j >= bmap->n_ineq)
10413 continue;
10414 isl_int_add(bmap->ineq[i][0],
10415 bmap->ineq[i][0], bmap->ineq[j][0]);
10416 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10417 isl_int_sub(bmap->ineq[i][0],
10418 bmap->ineq[i][0], bmap->ineq[j][0]);
10419 if (!less)
10420 continue;
10421 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10422 return i;
10423 else
10424 return j;
10427 return bmap->n_ineq;
10430 /* Return the index of the equality of "bmap" that defines
10431 * the output dimension "pos" in terms of earlier dimensions.
10432 * The equality may also involve integer divisions, as long
10433 * as those integer divisions are defined in terms of
10434 * parameters or input dimensions.
10435 * In this case, *div is set to the number of integer divisions and
10436 * *ineq is set to the number of inequality constraints (provided
10437 * div and ineq are not NULL).
10439 * The equality may also involve a single integer division involving
10440 * the output dimensions (typically only output dimension "pos") as
10441 * long as the coefficient of output dimension "pos" is 1 or -1 and
10442 * there is a pair of constraints i >= l and i <= l + n, with i referring
10443 * to output dimension "pos", l an expression involving only earlier
10444 * dimensions and n smaller than the coefficient of the integer division
10445 * in the equality. In this case, the output dimension can be defined
10446 * in terms of a modulo expression that does not involve the integer division.
10447 * *div is then set to this single integer division and
10448 * *ineq is set to the index of constraint i >= l.
10450 * Return bmap->n_eq if there is no such equality.
10451 * Return -1 on error.
10453 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10454 int pos, int *div, int *ineq)
10456 int j, k, l;
10457 unsigned n_out, o_out;
10458 unsigned n_div, o_div;
10460 if (!bmap)
10461 return -1;
10463 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10464 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10465 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10466 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10468 if (ineq)
10469 *ineq = bmap->n_ineq;
10470 if (div)
10471 *div = n_div;
10472 for (j = 0; j < bmap->n_eq; ++j) {
10473 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10474 continue;
10475 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10476 n_out - (pos + 1)) != -1)
10477 continue;
10478 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10479 0, n_div);
10480 if (k >= n_div)
10481 return j;
10482 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10483 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10484 continue;
10485 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10486 k + 1, n_div - (k+1)) < n_div)
10487 continue;
10488 l = find_modulo_constraint_pair(bmap, pos,
10489 bmap->eq[j][o_div + k]);
10490 if (l < 0)
10491 return -1;
10492 if (l >= bmap->n_ineq)
10493 continue;
10494 if (div)
10495 *div = k;
10496 if (ineq)
10497 *ineq = l;
10498 return j;
10501 return bmap->n_eq;
10504 /* Check if the given basic map is obviously single-valued.
10505 * In particular, for each output dimension, check that there is
10506 * an equality that defines the output dimension in terms of
10507 * earlier dimensions.
10509 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10511 int i;
10512 unsigned n_out;
10514 if (!bmap)
10515 return isl_bool_error;
10517 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10519 for (i = 0; i < n_out; ++i) {
10520 int eq;
10522 eq = isl_basic_map_output_defining_equality(bmap, i,
10523 NULL, NULL);
10524 if (eq < 0)
10525 return isl_bool_error;
10526 if (eq >= bmap->n_eq)
10527 return isl_bool_false;
10530 return isl_bool_true;
10533 /* Check if the given basic map is single-valued.
10534 * We simply compute
10536 * M \circ M^-1
10538 * and check if the result is a subset of the identity mapping.
10540 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10542 isl_space *space;
10543 isl_basic_map *test;
10544 isl_basic_map *id;
10545 isl_bool sv;
10547 sv = isl_basic_map_plain_is_single_valued(bmap);
10548 if (sv < 0 || sv)
10549 return sv;
10551 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10552 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10554 space = isl_basic_map_get_space(bmap);
10555 space = isl_space_map_from_set(isl_space_range(space));
10556 id = isl_basic_map_identity(space);
10558 sv = isl_basic_map_is_subset(test, id);
10560 isl_basic_map_free(test);
10561 isl_basic_map_free(id);
10563 return sv;
10566 /* Check if the given map is obviously single-valued.
10568 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10570 if (!map)
10571 return isl_bool_error;
10572 if (map->n == 0)
10573 return isl_bool_true;
10574 if (map->n >= 2)
10575 return isl_bool_false;
10577 return isl_basic_map_plain_is_single_valued(map->p[0]);
10580 /* Check if the given map is single-valued.
10581 * We simply compute
10583 * M \circ M^-1
10585 * and check if the result is a subset of the identity mapping.
10587 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10589 isl_space *dim;
10590 isl_map *test;
10591 isl_map *id;
10592 isl_bool sv;
10594 sv = isl_map_plain_is_single_valued(map);
10595 if (sv < 0 || sv)
10596 return sv;
10598 test = isl_map_reverse(isl_map_copy(map));
10599 test = isl_map_apply_range(test, isl_map_copy(map));
10601 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10602 id = isl_map_identity(dim);
10604 sv = isl_map_is_subset(test, id);
10606 isl_map_free(test);
10607 isl_map_free(id);
10609 return sv;
10612 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10614 isl_bool in;
10616 map = isl_map_copy(map);
10617 map = isl_map_reverse(map);
10618 in = isl_map_is_single_valued(map);
10619 isl_map_free(map);
10621 return in;
10624 /* Check if the given map is obviously injective.
10626 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10628 isl_bool in;
10630 map = isl_map_copy(map);
10631 map = isl_map_reverse(map);
10632 in = isl_map_plain_is_single_valued(map);
10633 isl_map_free(map);
10635 return in;
10638 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10640 isl_bool sv;
10642 sv = isl_map_is_single_valued(map);
10643 if (sv < 0 || !sv)
10644 return sv;
10646 return isl_map_is_injective(map);
10649 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10651 return isl_map_is_single_valued(set_to_map(set));
10654 /* Does "map" only map elements to themselves?
10656 * If the domain and range spaces are different, then "map"
10657 * is considered not to be an identity relation, even if it is empty.
10658 * Otherwise, construct the maximal identity relation and
10659 * check whether "map" is a subset of this relation.
10661 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10663 isl_space *space;
10664 isl_map *id;
10665 isl_bool equal, is_identity;
10667 space = isl_map_get_space(map);
10668 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10669 isl_space_free(space);
10670 if (equal < 0 || !equal)
10671 return equal;
10673 id = isl_map_identity(isl_map_get_space(map));
10674 is_identity = isl_map_is_subset(map, id);
10675 isl_map_free(id);
10677 return is_identity;
10680 int isl_map_is_translation(__isl_keep isl_map *map)
10682 int ok;
10683 isl_set *delta;
10685 delta = isl_map_deltas(isl_map_copy(map));
10686 ok = isl_set_is_singleton(delta);
10687 isl_set_free(delta);
10689 return ok;
10692 static int unique(isl_int *p, unsigned pos, unsigned len)
10694 if (isl_seq_first_non_zero(p, pos) != -1)
10695 return 0;
10696 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10697 return 0;
10698 return 1;
10701 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10703 int i, j;
10704 unsigned nvar;
10705 unsigned ovar;
10707 if (!bset)
10708 return isl_bool_error;
10710 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10711 return isl_bool_false;
10713 nvar = isl_basic_set_dim(bset, isl_dim_set);
10714 ovar = isl_space_offset(bset->dim, isl_dim_set);
10715 for (j = 0; j < nvar; ++j) {
10716 int lower = 0, upper = 0;
10717 for (i = 0; i < bset->n_eq; ++i) {
10718 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10719 continue;
10720 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10721 return isl_bool_false;
10722 break;
10724 if (i < bset->n_eq)
10725 continue;
10726 for (i = 0; i < bset->n_ineq; ++i) {
10727 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10728 continue;
10729 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10730 return isl_bool_false;
10731 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10732 lower = 1;
10733 else
10734 upper = 1;
10736 if (!lower || !upper)
10737 return isl_bool_false;
10740 return isl_bool_true;
10743 isl_bool isl_set_is_box(__isl_keep isl_set *set)
10745 if (!set)
10746 return isl_bool_error;
10747 if (set->n != 1)
10748 return isl_bool_false;
10750 return isl_basic_set_is_box(set->p[0]);
10753 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10755 if (!bset)
10756 return isl_bool_error;
10758 return isl_space_is_wrapping(bset->dim);
10761 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10763 if (!set)
10764 return isl_bool_error;
10766 return isl_space_is_wrapping(set->dim);
10769 /* Modify the space of "map" through a call to "change".
10770 * If "can_change" is set (not NULL), then first call it to check
10771 * if the modification is allowed, printing the error message "cannot_change"
10772 * if it is not.
10774 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10775 isl_bool (*can_change)(__isl_keep isl_map *map),
10776 const char *cannot_change,
10777 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10779 isl_bool ok;
10780 isl_space *space;
10782 if (!map)
10783 return NULL;
10785 ok = can_change ? can_change(map) : isl_bool_true;
10786 if (ok < 0)
10787 return isl_map_free(map);
10788 if (!ok)
10789 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
10790 return isl_map_free(map));
10792 space = change(isl_map_get_space(map));
10793 map = isl_map_reset_space(map, space);
10795 return map;
10798 /* Is the domain of "map" a wrapped relation?
10800 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10802 if (!map)
10803 return isl_bool_error;
10805 return isl_space_domain_is_wrapping(map->dim);
10808 /* Is the range of "map" a wrapped relation?
10810 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
10812 if (!map)
10813 return isl_bool_error;
10815 return isl_space_range_is_wrapping(map->dim);
10818 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10820 bmap = isl_basic_map_cow(bmap);
10821 if (!bmap)
10822 return NULL;
10824 bmap->dim = isl_space_wrap(bmap->dim);
10825 if (!bmap->dim)
10826 goto error;
10828 bmap = isl_basic_map_finalize(bmap);
10830 return bset_from_bmap(bmap);
10831 error:
10832 isl_basic_map_free(bmap);
10833 return NULL;
10836 /* Given a map A -> B, return the set (A -> B).
10838 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10840 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
10843 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10845 bset = isl_basic_set_cow(bset);
10846 if (!bset)
10847 return NULL;
10849 bset->dim = isl_space_unwrap(bset->dim);
10850 if (!bset->dim)
10851 goto error;
10853 bset = isl_basic_set_finalize(bset);
10855 return bset_to_bmap(bset);
10856 error:
10857 isl_basic_set_free(bset);
10858 return NULL;
10861 /* Given a set (A -> B), return the map A -> B.
10862 * Error out if "set" is not of the form (A -> B).
10864 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10866 return isl_map_change_space(set, &isl_set_is_wrapping,
10867 "not a wrapping set", &isl_space_unwrap);
10870 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10871 enum isl_dim_type type)
10873 if (!bmap)
10874 return NULL;
10876 if (!isl_space_is_named_or_nested(bmap->dim, type))
10877 return bmap;
10879 bmap = isl_basic_map_cow(bmap);
10880 if (!bmap)
10881 return NULL;
10883 bmap->dim = isl_space_reset(bmap->dim, type);
10884 if (!bmap->dim)
10885 goto error;
10887 bmap = isl_basic_map_finalize(bmap);
10889 return bmap;
10890 error:
10891 isl_basic_map_free(bmap);
10892 return NULL;
10895 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10896 enum isl_dim_type type)
10898 int i;
10900 if (!map)
10901 return NULL;
10903 if (!isl_space_is_named_or_nested(map->dim, type))
10904 return map;
10906 map = isl_map_cow(map);
10907 if (!map)
10908 return NULL;
10910 for (i = 0; i < map->n; ++i) {
10911 map->p[i] = isl_basic_map_reset(map->p[i], type);
10912 if (!map->p[i])
10913 goto error;
10915 map->dim = isl_space_reset(map->dim, type);
10916 if (!map->dim)
10917 goto error;
10919 return map;
10920 error:
10921 isl_map_free(map);
10922 return NULL;
10925 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10927 if (!bmap)
10928 return NULL;
10930 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10931 return bmap;
10933 bmap = isl_basic_map_cow(bmap);
10934 if (!bmap)
10935 return NULL;
10937 bmap->dim = isl_space_flatten(bmap->dim);
10938 if (!bmap->dim)
10939 goto error;
10941 bmap = isl_basic_map_finalize(bmap);
10943 return bmap;
10944 error:
10945 isl_basic_map_free(bmap);
10946 return NULL;
10949 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10951 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
10954 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10955 __isl_take isl_basic_map *bmap)
10957 if (!bmap)
10958 return NULL;
10960 if (!bmap->dim->nested[0])
10961 return bmap;
10963 bmap = isl_basic_map_cow(bmap);
10964 if (!bmap)
10965 return NULL;
10967 bmap->dim = isl_space_flatten_domain(bmap->dim);
10968 if (!bmap->dim)
10969 goto error;
10971 bmap = isl_basic_map_finalize(bmap);
10973 return bmap;
10974 error:
10975 isl_basic_map_free(bmap);
10976 return NULL;
10979 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10980 __isl_take isl_basic_map *bmap)
10982 if (!bmap)
10983 return NULL;
10985 if (!bmap->dim->nested[1])
10986 return bmap;
10988 bmap = isl_basic_map_cow(bmap);
10989 if (!bmap)
10990 return NULL;
10992 bmap->dim = isl_space_flatten_range(bmap->dim);
10993 if (!bmap->dim)
10994 goto error;
10996 bmap = isl_basic_map_finalize(bmap);
10998 return bmap;
10999 error:
11000 isl_basic_map_free(bmap);
11001 return NULL;
11004 /* Remove any internal structure from the spaces of domain and range of "map".
11006 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11008 if (!map)
11009 return NULL;
11011 if (!map->dim->nested[0] && !map->dim->nested[1])
11012 return map;
11014 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11017 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11019 return set_from_map(isl_map_flatten(set_to_map(set)));
11022 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11024 isl_space *dim, *flat_dim;
11025 isl_map *map;
11027 dim = isl_set_get_space(set);
11028 flat_dim = isl_space_flatten(isl_space_copy(dim));
11029 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11030 map = isl_map_intersect_domain(map, set);
11032 return map;
11035 /* Remove any internal structure from the space of the domain of "map".
11037 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11039 if (!map)
11040 return NULL;
11042 if (!map->dim->nested[0])
11043 return map;
11045 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11048 /* Remove any internal structure from the space of the range of "map".
11050 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11052 if (!map)
11053 return NULL;
11055 if (!map->dim->nested[1])
11056 return map;
11058 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11061 /* Reorder the dimensions of "bmap" according to the given dim_map
11062 * and set the dimension specification to "dim" and
11063 * perform Gaussian elimination on the result.
11065 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11066 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11068 isl_basic_map *res;
11069 unsigned flags;
11071 bmap = isl_basic_map_cow(bmap);
11072 if (!bmap || !dim || !dim_map)
11073 goto error;
11075 flags = bmap->flags;
11076 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11077 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11078 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11079 res = isl_basic_map_alloc_space(dim,
11080 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11081 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11082 if (res)
11083 res->flags = flags;
11084 res = isl_basic_map_gauss(res, NULL);
11085 res = isl_basic_map_finalize(res);
11086 return res;
11087 error:
11088 free(dim_map);
11089 isl_basic_map_free(bmap);
11090 isl_space_free(dim);
11091 return NULL;
11094 /* Reorder the dimensions of "map" according to given reordering.
11096 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11097 __isl_take isl_reordering *r)
11099 int i;
11100 struct isl_dim_map *dim_map;
11102 map = isl_map_cow(map);
11103 dim_map = isl_dim_map_from_reordering(r);
11104 if (!map || !r || !dim_map)
11105 goto error;
11107 for (i = 0; i < map->n; ++i) {
11108 struct isl_dim_map *dim_map_i;
11110 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11112 map->p[i] = isl_basic_map_realign(map->p[i],
11113 isl_space_copy(r->dim), dim_map_i);
11115 if (!map->p[i])
11116 goto error;
11119 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11121 isl_reordering_free(r);
11122 free(dim_map);
11123 return map;
11124 error:
11125 free(dim_map);
11126 isl_map_free(map);
11127 isl_reordering_free(r);
11128 return NULL;
11131 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11132 __isl_take isl_reordering *r)
11134 return set_from_map(isl_map_realign(set_to_map(set), r));
11137 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11138 __isl_take isl_space *model)
11140 isl_ctx *ctx;
11142 if (!map || !model)
11143 goto error;
11145 ctx = isl_space_get_ctx(model);
11146 if (!isl_space_has_named_params(model))
11147 isl_die(ctx, isl_error_invalid,
11148 "model has unnamed parameters", goto error);
11149 if (!isl_space_has_named_params(map->dim))
11150 isl_die(ctx, isl_error_invalid,
11151 "relation has unnamed parameters", goto error);
11152 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11153 isl_reordering *exp;
11155 model = isl_space_drop_dims(model, isl_dim_in,
11156 0, isl_space_dim(model, isl_dim_in));
11157 model = isl_space_drop_dims(model, isl_dim_out,
11158 0, isl_space_dim(model, isl_dim_out));
11159 exp = isl_parameter_alignment_reordering(map->dim, model);
11160 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11161 map = isl_map_realign(map, exp);
11164 isl_space_free(model);
11165 return map;
11166 error:
11167 isl_space_free(model);
11168 isl_map_free(map);
11169 return NULL;
11172 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11173 __isl_take isl_space *model)
11175 return isl_map_align_params(set, model);
11178 /* Align the parameters of "bmap" to those of "model", introducing
11179 * additional parameters if needed.
11181 __isl_give isl_basic_map *isl_basic_map_align_params(
11182 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11184 isl_ctx *ctx;
11186 if (!bmap || !model)
11187 goto error;
11189 ctx = isl_space_get_ctx(model);
11190 if (!isl_space_has_named_params(model))
11191 isl_die(ctx, isl_error_invalid,
11192 "model has unnamed parameters", goto error);
11193 if (!isl_space_has_named_params(bmap->dim))
11194 isl_die(ctx, isl_error_invalid,
11195 "relation has unnamed parameters", goto error);
11196 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11197 isl_reordering *exp;
11198 struct isl_dim_map *dim_map;
11200 model = isl_space_drop_dims(model, isl_dim_in,
11201 0, isl_space_dim(model, isl_dim_in));
11202 model = isl_space_drop_dims(model, isl_dim_out,
11203 0, isl_space_dim(model, isl_dim_out));
11204 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11205 exp = isl_reordering_extend_space(exp,
11206 isl_basic_map_get_space(bmap));
11207 dim_map = isl_dim_map_from_reordering(exp);
11208 bmap = isl_basic_map_realign(bmap,
11209 exp ? isl_space_copy(exp->dim) : NULL,
11210 isl_dim_map_extend(dim_map, bmap));
11211 isl_reordering_free(exp);
11212 free(dim_map);
11215 isl_space_free(model);
11216 return bmap;
11217 error:
11218 isl_space_free(model);
11219 isl_basic_map_free(bmap);
11220 return NULL;
11223 /* Align the parameters of "bset" to those of "model", introducing
11224 * additional parameters if needed.
11226 __isl_give isl_basic_set *isl_basic_set_align_params(
11227 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11229 return isl_basic_map_align_params(bset, model);
11232 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11233 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11234 enum isl_dim_type c2, enum isl_dim_type c3,
11235 enum isl_dim_type c4, enum isl_dim_type c5)
11237 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11238 struct isl_mat *mat;
11239 int i, j, k;
11240 int pos;
11242 if (!bmap)
11243 return NULL;
11244 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11245 isl_basic_map_total_dim(bmap) + 1);
11246 if (!mat)
11247 return NULL;
11248 for (i = 0; i < bmap->n_eq; ++i)
11249 for (j = 0, pos = 0; j < 5; ++j) {
11250 int off = isl_basic_map_offset(bmap, c[j]);
11251 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11252 isl_int_set(mat->row[i][pos],
11253 bmap->eq[i][off + k]);
11254 ++pos;
11258 return mat;
11261 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11262 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11263 enum isl_dim_type c2, enum isl_dim_type c3,
11264 enum isl_dim_type c4, enum isl_dim_type c5)
11266 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11267 struct isl_mat *mat;
11268 int i, j, k;
11269 int pos;
11271 if (!bmap)
11272 return NULL;
11273 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11274 isl_basic_map_total_dim(bmap) + 1);
11275 if (!mat)
11276 return NULL;
11277 for (i = 0; i < bmap->n_ineq; ++i)
11278 for (j = 0, pos = 0; j < 5; ++j) {
11279 int off = isl_basic_map_offset(bmap, c[j]);
11280 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11281 isl_int_set(mat->row[i][pos],
11282 bmap->ineq[i][off + k]);
11283 ++pos;
11287 return mat;
11290 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11291 __isl_take isl_space *dim,
11292 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11293 enum isl_dim_type c2, enum isl_dim_type c3,
11294 enum isl_dim_type c4, enum isl_dim_type c5)
11296 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11297 isl_basic_map *bmap;
11298 unsigned total;
11299 unsigned extra;
11300 int i, j, k, l;
11301 int pos;
11303 if (!dim || !eq || !ineq)
11304 goto error;
11306 if (eq->n_col != ineq->n_col)
11307 isl_die(dim->ctx, isl_error_invalid,
11308 "equalities and inequalities matrices should have "
11309 "same number of columns", goto error);
11311 total = 1 + isl_space_dim(dim, isl_dim_all);
11313 if (eq->n_col < total)
11314 isl_die(dim->ctx, isl_error_invalid,
11315 "number of columns too small", goto error);
11317 extra = eq->n_col - total;
11319 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11320 eq->n_row, ineq->n_row);
11321 if (!bmap)
11322 goto error;
11323 for (i = 0; i < extra; ++i) {
11324 k = isl_basic_map_alloc_div(bmap);
11325 if (k < 0)
11326 goto error;
11327 isl_int_set_si(bmap->div[k][0], 0);
11329 for (i = 0; i < eq->n_row; ++i) {
11330 l = isl_basic_map_alloc_equality(bmap);
11331 if (l < 0)
11332 goto error;
11333 for (j = 0, pos = 0; j < 5; ++j) {
11334 int off = isl_basic_map_offset(bmap, c[j]);
11335 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11336 isl_int_set(bmap->eq[l][off + k],
11337 eq->row[i][pos]);
11338 ++pos;
11342 for (i = 0; i < ineq->n_row; ++i) {
11343 l = isl_basic_map_alloc_inequality(bmap);
11344 if (l < 0)
11345 goto error;
11346 for (j = 0, pos = 0; j < 5; ++j) {
11347 int off = isl_basic_map_offset(bmap, c[j]);
11348 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11349 isl_int_set(bmap->ineq[l][off + k],
11350 ineq->row[i][pos]);
11351 ++pos;
11356 isl_space_free(dim);
11357 isl_mat_free(eq);
11358 isl_mat_free(ineq);
11360 bmap = isl_basic_map_simplify(bmap);
11361 return isl_basic_map_finalize(bmap);
11362 error:
11363 isl_space_free(dim);
11364 isl_mat_free(eq);
11365 isl_mat_free(ineq);
11366 return NULL;
11369 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11370 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11371 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11373 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11374 c1, c2, c3, c4, isl_dim_in);
11377 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11378 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11379 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11381 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11382 c1, c2, c3, c4, isl_dim_in);
11385 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11386 __isl_take isl_space *dim,
11387 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11388 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11390 isl_basic_map *bmap;
11391 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11392 c1, c2, c3, c4, isl_dim_in);
11393 return bset_from_bmap(bmap);
11396 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11398 if (!bmap)
11399 return isl_bool_error;
11401 return isl_space_can_zip(bmap->dim);
11404 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11406 if (!map)
11407 return isl_bool_error;
11409 return isl_space_can_zip(map->dim);
11412 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11413 * (A -> C) -> (B -> D).
11415 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11417 unsigned pos;
11418 unsigned n1;
11419 unsigned n2;
11421 if (!bmap)
11422 return NULL;
11424 if (!isl_basic_map_can_zip(bmap))
11425 isl_die(bmap->ctx, isl_error_invalid,
11426 "basic map cannot be zipped", goto error);
11427 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11428 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11429 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11430 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11431 bmap = isl_basic_map_cow(bmap);
11432 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11433 if (!bmap)
11434 return NULL;
11435 bmap->dim = isl_space_zip(bmap->dim);
11436 if (!bmap->dim)
11437 goto error;
11438 bmap = isl_basic_map_mark_final(bmap);
11439 return bmap;
11440 error:
11441 isl_basic_map_free(bmap);
11442 return NULL;
11445 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11446 * (A -> C) -> (B -> D).
11448 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11450 int i;
11452 if (!map)
11453 return NULL;
11455 if (!isl_map_can_zip(map))
11456 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11457 goto error);
11459 map = isl_map_cow(map);
11460 if (!map)
11461 return NULL;
11463 for (i = 0; i < map->n; ++i) {
11464 map->p[i] = isl_basic_map_zip(map->p[i]);
11465 if (!map->p[i])
11466 goto error;
11469 map->dim = isl_space_zip(map->dim);
11470 if (!map->dim)
11471 goto error;
11473 return map;
11474 error:
11475 isl_map_free(map);
11476 return NULL;
11479 /* Can we apply isl_basic_map_curry to "bmap"?
11480 * That is, does it have a nested relation in its domain?
11482 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11484 if (!bmap)
11485 return isl_bool_error;
11487 return isl_space_can_curry(bmap->dim);
11490 /* Can we apply isl_map_curry to "map"?
11491 * That is, does it have a nested relation in its domain?
11493 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11495 if (!map)
11496 return isl_bool_error;
11498 return isl_space_can_curry(map->dim);
11501 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11502 * A -> (B -> C).
11504 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11507 if (!bmap)
11508 return NULL;
11510 if (!isl_basic_map_can_curry(bmap))
11511 isl_die(bmap->ctx, isl_error_invalid,
11512 "basic map cannot be curried", goto error);
11513 bmap = isl_basic_map_cow(bmap);
11514 if (!bmap)
11515 return NULL;
11516 bmap->dim = isl_space_curry(bmap->dim);
11517 if (!bmap->dim)
11518 goto error;
11519 bmap = isl_basic_map_mark_final(bmap);
11520 return bmap;
11521 error:
11522 isl_basic_map_free(bmap);
11523 return NULL;
11526 /* Given a map (A -> B) -> C, return the corresponding map
11527 * A -> (B -> C).
11529 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11531 return isl_map_change_space(map, &isl_map_can_curry,
11532 "map cannot be curried", &isl_space_curry);
11535 /* Can isl_map_range_curry be applied to "map"?
11536 * That is, does it have a nested relation in its range,
11537 * the domain of which is itself a nested relation?
11539 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11541 if (!map)
11542 return isl_bool_error;
11544 return isl_space_can_range_curry(map->dim);
11547 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11548 * A -> (B -> (C -> D)).
11550 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11552 return isl_map_change_space(map, &isl_map_can_range_curry,
11553 "map range cannot be curried",
11554 &isl_space_range_curry);
11557 /* Can we apply isl_basic_map_uncurry to "bmap"?
11558 * That is, does it have a nested relation in its domain?
11560 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11562 if (!bmap)
11563 return isl_bool_error;
11565 return isl_space_can_uncurry(bmap->dim);
11568 /* Can we apply isl_map_uncurry to "map"?
11569 * That is, does it have a nested relation in its domain?
11571 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11573 if (!map)
11574 return isl_bool_error;
11576 return isl_space_can_uncurry(map->dim);
11579 /* Given a basic map A -> (B -> C), return the corresponding basic map
11580 * (A -> B) -> C.
11582 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11585 if (!bmap)
11586 return NULL;
11588 if (!isl_basic_map_can_uncurry(bmap))
11589 isl_die(bmap->ctx, isl_error_invalid,
11590 "basic map cannot be uncurried",
11591 return isl_basic_map_free(bmap));
11592 bmap = isl_basic_map_cow(bmap);
11593 if (!bmap)
11594 return NULL;
11595 bmap->dim = isl_space_uncurry(bmap->dim);
11596 if (!bmap->dim)
11597 return isl_basic_map_free(bmap);
11598 bmap = isl_basic_map_mark_final(bmap);
11599 return bmap;
11602 /* Given a map A -> (B -> C), return the corresponding map
11603 * (A -> B) -> C.
11605 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11607 return isl_map_change_space(map, &isl_map_can_uncurry,
11608 "map cannot be uncurried", &isl_space_uncurry);
11611 /* Construct a basic map mapping the domain of the affine expression
11612 * to a one-dimensional range prescribed by the affine expression.
11613 * If "rational" is set, then construct a rational basic map.
11615 * A NaN affine expression cannot be converted to a basic map.
11617 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11618 __isl_take isl_aff *aff, int rational)
11620 int k;
11621 int pos;
11622 isl_bool is_nan;
11623 isl_local_space *ls;
11624 isl_basic_map *bmap = NULL;
11626 if (!aff)
11627 return NULL;
11628 is_nan = isl_aff_is_nan(aff);
11629 if (is_nan < 0)
11630 goto error;
11631 if (is_nan)
11632 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11633 "cannot convert NaN", goto error);
11635 ls = isl_aff_get_local_space(aff);
11636 bmap = isl_basic_map_from_local_space(ls);
11637 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11638 k = isl_basic_map_alloc_equality(bmap);
11639 if (k < 0)
11640 goto error;
11642 pos = isl_basic_map_offset(bmap, isl_dim_out);
11643 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11644 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11645 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11646 aff->v->size - (pos + 1));
11648 isl_aff_free(aff);
11649 if (rational)
11650 bmap = isl_basic_map_set_rational(bmap);
11651 bmap = isl_basic_map_finalize(bmap);
11652 return bmap;
11653 error:
11654 isl_aff_free(aff);
11655 isl_basic_map_free(bmap);
11656 return NULL;
11659 /* Construct a basic map mapping the domain of the affine expression
11660 * to a one-dimensional range prescribed by the affine expression.
11662 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11664 return isl_basic_map_from_aff2(aff, 0);
11667 /* Construct a map mapping the domain of the affine expression
11668 * to a one-dimensional range prescribed by the affine expression.
11670 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11672 isl_basic_map *bmap;
11674 bmap = isl_basic_map_from_aff(aff);
11675 return isl_map_from_basic_map(bmap);
11678 /* Construct a basic map mapping the domain the multi-affine expression
11679 * to its range, with each dimension in the range equated to the
11680 * corresponding affine expression.
11681 * If "rational" is set, then construct a rational basic map.
11683 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
11684 __isl_take isl_multi_aff *maff, int rational)
11686 int i;
11687 isl_space *space;
11688 isl_basic_map *bmap;
11690 if (!maff)
11691 return NULL;
11693 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11694 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11695 "invalid space", goto error);
11697 space = isl_space_domain(isl_multi_aff_get_space(maff));
11698 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11699 if (rational)
11700 bmap = isl_basic_map_set_rational(bmap);
11702 for (i = 0; i < maff->n; ++i) {
11703 isl_aff *aff;
11704 isl_basic_map *bmap_i;
11706 aff = isl_aff_copy(maff->p[i]);
11707 bmap_i = isl_basic_map_from_aff2(aff, rational);
11709 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11712 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11714 isl_multi_aff_free(maff);
11715 return bmap;
11716 error:
11717 isl_multi_aff_free(maff);
11718 return NULL;
11721 /* Construct a basic map mapping the domain the multi-affine expression
11722 * to its range, with each dimension in the range equated to the
11723 * corresponding affine expression.
11725 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11726 __isl_take isl_multi_aff *ma)
11728 return isl_basic_map_from_multi_aff2(ma, 0);
11731 /* Construct a map mapping the domain the multi-affine expression
11732 * to its range, with each dimension in the range equated to the
11733 * corresponding affine expression.
11735 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11737 isl_basic_map *bmap;
11739 bmap = isl_basic_map_from_multi_aff(maff);
11740 return isl_map_from_basic_map(bmap);
11743 /* Construct a basic map mapping a domain in the given space to
11744 * to an n-dimensional range, with n the number of elements in the list,
11745 * where each coordinate in the range is prescribed by the
11746 * corresponding affine expression.
11747 * The domains of all affine expressions in the list are assumed to match
11748 * domain_dim.
11750 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11751 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11753 int i;
11754 isl_space *dim;
11755 isl_basic_map *bmap;
11757 if (!list)
11758 return NULL;
11760 dim = isl_space_from_domain(domain_dim);
11761 bmap = isl_basic_map_universe(dim);
11763 for (i = 0; i < list->n; ++i) {
11764 isl_aff *aff;
11765 isl_basic_map *bmap_i;
11767 aff = isl_aff_copy(list->p[i]);
11768 bmap_i = isl_basic_map_from_aff(aff);
11770 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11773 isl_aff_list_free(list);
11774 return bmap;
11777 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11778 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11780 return isl_map_equate(set, type1, pos1, type2, pos2);
11783 /* Construct a basic map where the given dimensions are equal to each other.
11785 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11786 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11788 isl_basic_map *bmap = NULL;
11789 int i;
11791 if (!space)
11792 return NULL;
11794 if (pos1 >= isl_space_dim(space, type1))
11795 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11796 "index out of bounds", goto error);
11797 if (pos2 >= isl_space_dim(space, type2))
11798 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11799 "index out of bounds", goto error);
11801 if (type1 == type2 && pos1 == pos2)
11802 return isl_basic_map_universe(space);
11804 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11805 i = isl_basic_map_alloc_equality(bmap);
11806 if (i < 0)
11807 goto error;
11808 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11809 pos1 += isl_basic_map_offset(bmap, type1);
11810 pos2 += isl_basic_map_offset(bmap, type2);
11811 isl_int_set_si(bmap->eq[i][pos1], -1);
11812 isl_int_set_si(bmap->eq[i][pos2], 1);
11813 bmap = isl_basic_map_finalize(bmap);
11814 isl_space_free(space);
11815 return bmap;
11816 error:
11817 isl_space_free(space);
11818 isl_basic_map_free(bmap);
11819 return NULL;
11822 /* Add a constraint imposing that the given two dimensions are equal.
11824 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11825 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11827 isl_basic_map *eq;
11829 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11831 bmap = isl_basic_map_intersect(bmap, eq);
11833 return bmap;
11836 /* Add a constraint imposing that the given two dimensions are equal.
11838 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11839 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11841 isl_basic_map *bmap;
11843 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11845 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11847 return map;
11850 /* Add a constraint imposing that the given two dimensions have opposite values.
11852 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11853 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11855 isl_basic_map *bmap = NULL;
11856 int i;
11858 if (!map)
11859 return NULL;
11861 if (pos1 >= isl_map_dim(map, type1))
11862 isl_die(map->ctx, isl_error_invalid,
11863 "index out of bounds", goto error);
11864 if (pos2 >= isl_map_dim(map, type2))
11865 isl_die(map->ctx, isl_error_invalid,
11866 "index out of bounds", goto error);
11868 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11869 i = isl_basic_map_alloc_equality(bmap);
11870 if (i < 0)
11871 goto error;
11872 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11873 pos1 += isl_basic_map_offset(bmap, type1);
11874 pos2 += isl_basic_map_offset(bmap, type2);
11875 isl_int_set_si(bmap->eq[i][pos1], 1);
11876 isl_int_set_si(bmap->eq[i][pos2], 1);
11877 bmap = isl_basic_map_finalize(bmap);
11879 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11881 return map;
11882 error:
11883 isl_basic_map_free(bmap);
11884 isl_map_free(map);
11885 return NULL;
11888 /* Construct a constraint imposing that the value of the first dimension is
11889 * greater than or equal to that of the second.
11891 static __isl_give isl_constraint *constraint_order_ge(
11892 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11893 enum isl_dim_type type2, int pos2)
11895 isl_constraint *c;
11897 if (!space)
11898 return NULL;
11900 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
11902 if (pos1 >= isl_constraint_dim(c, type1))
11903 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11904 "index out of bounds", return isl_constraint_free(c));
11905 if (pos2 >= isl_constraint_dim(c, type2))
11906 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11907 "index out of bounds", return isl_constraint_free(c));
11909 if (type1 == type2 && pos1 == pos2)
11910 return c;
11912 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11913 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11915 return c;
11918 /* Add a constraint imposing that the value of the first dimension is
11919 * greater than or equal to that of the second.
11921 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11922 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11924 isl_constraint *c;
11925 isl_space *space;
11927 if (type1 == type2 && pos1 == pos2)
11928 return bmap;
11929 space = isl_basic_map_get_space(bmap);
11930 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11931 bmap = isl_basic_map_add_constraint(bmap, c);
11933 return bmap;
11936 /* Add a constraint imposing that the value of the first dimension is
11937 * greater than or equal to that of the second.
11939 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11940 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11942 isl_constraint *c;
11943 isl_space *space;
11945 if (type1 == type2 && pos1 == pos2)
11946 return map;
11947 space = isl_map_get_space(map);
11948 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11949 map = isl_map_add_constraint(map, c);
11951 return map;
11954 /* Add a constraint imposing that the value of the first dimension is
11955 * less than or equal to that of the second.
11957 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11958 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11960 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11963 /* Construct a basic map where the value of the first dimension is
11964 * greater than that of the second.
11966 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11967 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11969 isl_basic_map *bmap = NULL;
11970 int i;
11972 if (!space)
11973 return NULL;
11975 if (pos1 >= isl_space_dim(space, type1))
11976 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11977 "index out of bounds", goto error);
11978 if (pos2 >= isl_space_dim(space, type2))
11979 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11980 "index out of bounds", goto error);
11982 if (type1 == type2 && pos1 == pos2)
11983 return isl_basic_map_empty(space);
11985 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11986 i = isl_basic_map_alloc_inequality(bmap);
11987 if (i < 0)
11988 return isl_basic_map_free(bmap);
11989 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11990 pos1 += isl_basic_map_offset(bmap, type1);
11991 pos2 += isl_basic_map_offset(bmap, type2);
11992 isl_int_set_si(bmap->ineq[i][pos1], 1);
11993 isl_int_set_si(bmap->ineq[i][pos2], -1);
11994 isl_int_set_si(bmap->ineq[i][0], -1);
11995 bmap = isl_basic_map_finalize(bmap);
11997 return bmap;
11998 error:
11999 isl_space_free(space);
12000 isl_basic_map_free(bmap);
12001 return NULL;
12004 /* Add a constraint imposing that the value of the first dimension is
12005 * greater than that of the second.
12007 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12008 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12010 isl_basic_map *gt;
12012 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12014 bmap = isl_basic_map_intersect(bmap, gt);
12016 return bmap;
12019 /* Add a constraint imposing that the value of the first dimension is
12020 * greater than that of the second.
12022 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12023 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12025 isl_basic_map *bmap;
12027 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12029 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12031 return map;
12034 /* Add a constraint imposing that the value of the first dimension is
12035 * smaller than that of the second.
12037 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12038 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12040 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12043 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12044 int pos)
12046 isl_aff *div;
12047 isl_local_space *ls;
12049 if (!bmap)
12050 return NULL;
12052 if (!isl_basic_map_divs_known(bmap))
12053 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12054 "some divs are unknown", return NULL);
12056 ls = isl_basic_map_get_local_space(bmap);
12057 div = isl_local_space_get_div(ls, pos);
12058 isl_local_space_free(ls);
12060 return div;
12063 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12064 int pos)
12066 return isl_basic_map_get_div(bset, pos);
12069 /* Plug in "subs" for dimension "type", "pos" of "bset".
12071 * Let i be the dimension to replace and let "subs" be of the form
12073 * f/d
12075 * Any integer division with a non-zero coefficient for i,
12077 * floor((a i + g)/m)
12079 * is replaced by
12081 * floor((a f + d g)/(m d))
12083 * Constraints of the form
12085 * a i + g
12087 * are replaced by
12089 * a f + d g
12091 * We currently require that "subs" is an integral expression.
12092 * Handling rational expressions may require us to add stride constraints
12093 * as we do in isl_basic_set_preimage_multi_aff.
12095 __isl_give isl_basic_set *isl_basic_set_substitute(
12096 __isl_take isl_basic_set *bset,
12097 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12099 int i;
12100 isl_int v;
12101 isl_ctx *ctx;
12103 if (bset && isl_basic_set_plain_is_empty(bset))
12104 return bset;
12106 bset = isl_basic_set_cow(bset);
12107 if (!bset || !subs)
12108 goto error;
12110 ctx = isl_basic_set_get_ctx(bset);
12111 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12112 isl_die(ctx, isl_error_invalid,
12113 "spaces don't match", goto error);
12114 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12115 isl_die(ctx, isl_error_unsupported,
12116 "cannot handle divs yet", goto error);
12117 if (!isl_int_is_one(subs->v->el[0]))
12118 isl_die(ctx, isl_error_invalid,
12119 "can only substitute integer expressions", goto error);
12121 pos += isl_basic_set_offset(bset, type);
12123 isl_int_init(v);
12125 for (i = 0; i < bset->n_eq; ++i) {
12126 if (isl_int_is_zero(bset->eq[i][pos]))
12127 continue;
12128 isl_int_set(v, bset->eq[i][pos]);
12129 isl_int_set_si(bset->eq[i][pos], 0);
12130 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12131 v, subs->v->el + 1, subs->v->size - 1);
12134 for (i = 0; i < bset->n_ineq; ++i) {
12135 if (isl_int_is_zero(bset->ineq[i][pos]))
12136 continue;
12137 isl_int_set(v, bset->ineq[i][pos]);
12138 isl_int_set_si(bset->ineq[i][pos], 0);
12139 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12140 v, subs->v->el + 1, subs->v->size - 1);
12143 for (i = 0; i < bset->n_div; ++i) {
12144 if (isl_int_is_zero(bset->div[i][1 + pos]))
12145 continue;
12146 isl_int_set(v, bset->div[i][1 + pos]);
12147 isl_int_set_si(bset->div[i][1 + pos], 0);
12148 isl_seq_combine(bset->div[i] + 1,
12149 subs->v->el[0], bset->div[i] + 1,
12150 v, subs->v->el + 1, subs->v->size - 1);
12151 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12154 isl_int_clear(v);
12156 bset = isl_basic_set_simplify(bset);
12157 return isl_basic_set_finalize(bset);
12158 error:
12159 isl_basic_set_free(bset);
12160 return NULL;
12163 /* Plug in "subs" for dimension "type", "pos" of "set".
12165 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12166 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12168 int i;
12170 if (set && isl_set_plain_is_empty(set))
12171 return set;
12173 set = isl_set_cow(set);
12174 if (!set || !subs)
12175 goto error;
12177 for (i = set->n - 1; i >= 0; --i) {
12178 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12179 if (remove_if_empty(set, i) < 0)
12180 goto error;
12183 return set;
12184 error:
12185 isl_set_free(set);
12186 return NULL;
12189 /* Check if the range of "ma" is compatible with the domain or range
12190 * (depending on "type") of "bmap".
12192 static isl_stat check_basic_map_compatible_range_multi_aff(
12193 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12194 __isl_keep isl_multi_aff *ma)
12196 isl_bool m;
12197 isl_space *ma_space;
12199 ma_space = isl_multi_aff_get_space(ma);
12201 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12202 if (m < 0)
12203 goto error;
12204 if (!m)
12205 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12206 "parameters don't match", goto error);
12207 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12208 if (m < 0)
12209 goto error;
12210 if (!m)
12211 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12212 "spaces don't match", goto error);
12214 isl_space_free(ma_space);
12215 return isl_stat_ok;
12216 error:
12217 isl_space_free(ma_space);
12218 return isl_stat_error;
12221 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12222 * coefficients before the transformed range of dimensions,
12223 * the "n_after" coefficients after the transformed range of dimensions
12224 * and the coefficients of the other divs in "bmap".
12226 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12227 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12229 int i;
12230 int n_param;
12231 int n_set;
12232 isl_local_space *ls;
12234 if (n_div == 0)
12235 return 0;
12237 ls = isl_aff_get_domain_local_space(ma->p[0]);
12238 if (!ls)
12239 return -1;
12241 n_param = isl_local_space_dim(ls, isl_dim_param);
12242 n_set = isl_local_space_dim(ls, isl_dim_set);
12243 for (i = 0; i < n_div; ++i) {
12244 int o_bmap = 0, o_ls = 0;
12246 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12247 o_bmap += 1 + 1 + n_param;
12248 o_ls += 1 + 1 + n_param;
12249 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12250 o_bmap += n_before;
12251 isl_seq_cpy(bmap->div[i] + o_bmap,
12252 ls->div->row[i] + o_ls, n_set);
12253 o_bmap += n_set;
12254 o_ls += n_set;
12255 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12256 o_bmap += n_after;
12257 isl_seq_cpy(bmap->div[i] + o_bmap,
12258 ls->div->row[i] + o_ls, n_div);
12259 o_bmap += n_div;
12260 o_ls += n_div;
12261 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12262 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12263 goto error;
12266 isl_local_space_free(ls);
12267 return 0;
12268 error:
12269 isl_local_space_free(ls);
12270 return -1;
12273 /* How many stride constraints does "ma" enforce?
12274 * That is, how many of the affine expressions have a denominator
12275 * different from one?
12277 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12279 int i;
12280 int strides = 0;
12282 for (i = 0; i < ma->n; ++i)
12283 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12284 strides++;
12286 return strides;
12289 /* For each affine expression in ma of the form
12291 * x_i = (f_i y + h_i)/m_i
12293 * with m_i different from one, add a constraint to "bmap"
12294 * of the form
12296 * f_i y + h_i = m_i alpha_i
12298 * with alpha_i an additional existentially quantified variable.
12300 * The input variables of "ma" correspond to a subset of the variables
12301 * of "bmap". There are "n_before" variables in "bmap" before this
12302 * subset and "n_after" variables after this subset.
12303 * The integer divisions of the affine expressions in "ma" are assumed
12304 * to have been aligned. There are "n_div_ma" of them and
12305 * they appear first in "bmap", straight after the "n_after" variables.
12307 static __isl_give isl_basic_map *add_ma_strides(
12308 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12309 int n_before, int n_after, int n_div_ma)
12311 int i, k;
12312 int div;
12313 int total;
12314 int n_param;
12315 int n_in;
12317 total = isl_basic_map_total_dim(bmap);
12318 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12319 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12320 for (i = 0; i < ma->n; ++i) {
12321 int o_bmap = 0, o_ma = 1;
12323 if (isl_int_is_one(ma->p[i]->v->el[0]))
12324 continue;
12325 div = isl_basic_map_alloc_div(bmap);
12326 k = isl_basic_map_alloc_equality(bmap);
12327 if (div < 0 || k < 0)
12328 goto error;
12329 isl_int_set_si(bmap->div[div][0], 0);
12330 isl_seq_cpy(bmap->eq[k] + o_bmap,
12331 ma->p[i]->v->el + o_ma, 1 + n_param);
12332 o_bmap += 1 + n_param;
12333 o_ma += 1 + n_param;
12334 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12335 o_bmap += n_before;
12336 isl_seq_cpy(bmap->eq[k] + o_bmap,
12337 ma->p[i]->v->el + o_ma, n_in);
12338 o_bmap += n_in;
12339 o_ma += n_in;
12340 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12341 o_bmap += n_after;
12342 isl_seq_cpy(bmap->eq[k] + o_bmap,
12343 ma->p[i]->v->el + o_ma, n_div_ma);
12344 o_bmap += n_div_ma;
12345 o_ma += n_div_ma;
12346 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12347 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12348 total++;
12351 return bmap;
12352 error:
12353 isl_basic_map_free(bmap);
12354 return NULL;
12357 /* Replace the domain or range space (depending on "type) of "space" by "set".
12359 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12360 enum isl_dim_type type, __isl_take isl_space *set)
12362 if (type == isl_dim_in) {
12363 space = isl_space_range(space);
12364 space = isl_space_map_from_domain_and_range(set, space);
12365 } else {
12366 space = isl_space_domain(space);
12367 space = isl_space_map_from_domain_and_range(space, set);
12370 return space;
12373 /* Compute the preimage of the domain or range (depending on "type")
12374 * of "bmap" under the function represented by "ma".
12375 * In other words, plug in "ma" in the domain or range of "bmap".
12376 * The result is a basic map that lives in the same space as "bmap"
12377 * except that the domain or range has been replaced by
12378 * the domain space of "ma".
12380 * If bmap is represented by
12382 * A(p) + S u + B x + T v + C(divs) >= 0,
12384 * where u and x are input and output dimensions if type == isl_dim_out
12385 * while x and v are input and output dimensions if type == isl_dim_in,
12386 * and ma is represented by
12388 * x = D(p) + F(y) + G(divs')
12390 * then the result is
12392 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12394 * The divs in the input set are similarly adjusted.
12395 * In particular
12397 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12399 * becomes
12401 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12402 * B_i G(divs') + c_i(divs))/n_i)
12404 * If bmap is not a rational map and if F(y) involves any denominators
12406 * x_i = (f_i y + h_i)/m_i
12408 * then additional constraints are added to ensure that we only
12409 * map back integer points. That is we enforce
12411 * f_i y + h_i = m_i alpha_i
12413 * with alpha_i an additional existentially quantified variable.
12415 * We first copy over the divs from "ma".
12416 * Then we add the modified constraints and divs from "bmap".
12417 * Finally, we add the stride constraints, if needed.
12419 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12420 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12421 __isl_take isl_multi_aff *ma)
12423 int i, k;
12424 isl_space *space;
12425 isl_basic_map *res = NULL;
12426 int n_before, n_after, n_div_bmap, n_div_ma;
12427 isl_int f, c1, c2, g;
12428 isl_bool rational;
12429 int strides;
12431 isl_int_init(f);
12432 isl_int_init(c1);
12433 isl_int_init(c2);
12434 isl_int_init(g);
12436 ma = isl_multi_aff_align_divs(ma);
12437 if (!bmap || !ma)
12438 goto error;
12439 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12440 goto error;
12442 if (type == isl_dim_in) {
12443 n_before = 0;
12444 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12445 } else {
12446 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12447 n_after = 0;
12449 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12450 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12452 space = isl_multi_aff_get_domain_space(ma);
12453 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12454 rational = isl_basic_map_is_rational(bmap);
12455 strides = rational ? 0 : multi_aff_strides(ma);
12456 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12457 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12458 if (rational)
12459 res = isl_basic_map_set_rational(res);
12461 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12462 if (isl_basic_map_alloc_div(res) < 0)
12463 goto error;
12465 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12466 goto error;
12468 for (i = 0; i < bmap->n_eq; ++i) {
12469 k = isl_basic_map_alloc_equality(res);
12470 if (k < 0)
12471 goto error;
12472 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12473 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12476 for (i = 0; i < bmap->n_ineq; ++i) {
12477 k = isl_basic_map_alloc_inequality(res);
12478 if (k < 0)
12479 goto error;
12480 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12481 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12484 for (i = 0; i < bmap->n_div; ++i) {
12485 if (isl_int_is_zero(bmap->div[i][0])) {
12486 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12487 continue;
12489 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12490 n_before, n_after, n_div_ma, n_div_bmap,
12491 f, c1, c2, g, 1);
12494 if (strides)
12495 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12497 isl_int_clear(f);
12498 isl_int_clear(c1);
12499 isl_int_clear(c2);
12500 isl_int_clear(g);
12501 isl_basic_map_free(bmap);
12502 isl_multi_aff_free(ma);
12503 res = isl_basic_map_simplify(res);
12504 return isl_basic_map_finalize(res);
12505 error:
12506 isl_int_clear(f);
12507 isl_int_clear(c1);
12508 isl_int_clear(c2);
12509 isl_int_clear(g);
12510 isl_basic_map_free(bmap);
12511 isl_multi_aff_free(ma);
12512 isl_basic_map_free(res);
12513 return NULL;
12516 /* Compute the preimage of "bset" under the function represented by "ma".
12517 * In other words, plug in "ma" in "bset". The result is a basic set
12518 * that lives in the domain space of "ma".
12520 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12521 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12523 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12526 /* Compute the preimage of the domain of "bmap" under the function
12527 * represented by "ma".
12528 * In other words, plug in "ma" in the domain of "bmap".
12529 * The result is a basic map that lives in the same space as "bmap"
12530 * except that the domain has been replaced by the domain space of "ma".
12532 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12533 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12535 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12538 /* Compute the preimage of the range of "bmap" under the function
12539 * represented by "ma".
12540 * In other words, plug in "ma" in the range of "bmap".
12541 * The result is a basic map that lives in the same space as "bmap"
12542 * except that the range has been replaced by the domain space of "ma".
12544 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12545 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12547 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12550 /* Check if the range of "ma" is compatible with the domain or range
12551 * (depending on "type") of "map".
12552 * Return isl_stat_error if anything is wrong.
12554 static isl_stat check_map_compatible_range_multi_aff(
12555 __isl_keep isl_map *map, enum isl_dim_type type,
12556 __isl_keep isl_multi_aff *ma)
12558 isl_bool m;
12559 isl_space *ma_space;
12561 ma_space = isl_multi_aff_get_space(ma);
12562 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12563 isl_space_free(ma_space);
12564 if (m < 0)
12565 return isl_stat_error;
12566 if (!m)
12567 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12568 "spaces don't match", return isl_stat_error);
12569 return isl_stat_ok;
12572 /* Compute the preimage of the domain or range (depending on "type")
12573 * of "map" under the function represented by "ma".
12574 * In other words, plug in "ma" in the domain or range of "map".
12575 * The result is a map that lives in the same space as "map"
12576 * except that the domain or range has been replaced by
12577 * the domain space of "ma".
12579 * The parameters are assumed to have been aligned.
12581 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12582 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12584 int i;
12585 isl_space *space;
12587 map = isl_map_cow(map);
12588 ma = isl_multi_aff_align_divs(ma);
12589 if (!map || !ma)
12590 goto error;
12591 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12592 goto error;
12594 for (i = 0; i < map->n; ++i) {
12595 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12596 isl_multi_aff_copy(ma));
12597 if (!map->p[i])
12598 goto error;
12601 space = isl_multi_aff_get_domain_space(ma);
12602 space = isl_space_set(isl_map_get_space(map), type, space);
12604 isl_space_free(map->dim);
12605 map->dim = space;
12606 if (!map->dim)
12607 goto error;
12609 isl_multi_aff_free(ma);
12610 if (map->n > 1)
12611 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12612 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12613 return map;
12614 error:
12615 isl_multi_aff_free(ma);
12616 isl_map_free(map);
12617 return NULL;
12620 /* Compute the preimage of the domain or range (depending on "type")
12621 * of "map" under the function represented by "ma".
12622 * In other words, plug in "ma" in the domain or range of "map".
12623 * The result is a map that lives in the same space as "map"
12624 * except that the domain or range has been replaced by
12625 * the domain space of "ma".
12627 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12628 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12630 if (!map || !ma)
12631 goto error;
12633 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12634 return map_preimage_multi_aff(map, type, ma);
12636 if (!isl_space_has_named_params(map->dim) ||
12637 !isl_space_has_named_params(ma->space))
12638 isl_die(map->ctx, isl_error_invalid,
12639 "unaligned unnamed parameters", goto error);
12640 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12641 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12643 return map_preimage_multi_aff(map, type, ma);
12644 error:
12645 isl_multi_aff_free(ma);
12646 return isl_map_free(map);
12649 /* Compute the preimage of "set" under the function represented by "ma".
12650 * In other words, plug in "ma" in "set". The result is a set
12651 * that lives in the domain space of "ma".
12653 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12654 __isl_take isl_multi_aff *ma)
12656 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12659 /* Compute the preimage of the domain of "map" under the function
12660 * represented by "ma".
12661 * In other words, plug in "ma" in the domain of "map".
12662 * The result is a map that lives in the same space as "map"
12663 * except that the domain has been replaced by the domain space of "ma".
12665 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12666 __isl_take isl_multi_aff *ma)
12668 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12671 /* Compute the preimage of the range of "map" under the function
12672 * represented by "ma".
12673 * In other words, plug in "ma" in the range of "map".
12674 * The result is a map that lives in the same space as "map"
12675 * except that the range has been replaced by the domain space of "ma".
12677 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12678 __isl_take isl_multi_aff *ma)
12680 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12683 /* Compute the preimage of "map" under the function represented by "pma".
12684 * In other words, plug in "pma" in the domain or range of "map".
12685 * The result is a map that lives in the same space as "map",
12686 * except that the space of type "type" has been replaced by
12687 * the domain space of "pma".
12689 * The parameters of "map" and "pma" are assumed to have been aligned.
12691 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12692 __isl_take isl_map *map, enum isl_dim_type type,
12693 __isl_take isl_pw_multi_aff *pma)
12695 int i;
12696 isl_map *res;
12698 if (!pma)
12699 goto error;
12701 if (pma->n == 0) {
12702 isl_pw_multi_aff_free(pma);
12703 res = isl_map_empty(isl_map_get_space(map));
12704 isl_map_free(map);
12705 return res;
12708 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12709 isl_multi_aff_copy(pma->p[0].maff));
12710 if (type == isl_dim_in)
12711 res = isl_map_intersect_domain(res,
12712 isl_map_copy(pma->p[0].set));
12713 else
12714 res = isl_map_intersect_range(res,
12715 isl_map_copy(pma->p[0].set));
12717 for (i = 1; i < pma->n; ++i) {
12718 isl_map *res_i;
12720 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12721 isl_multi_aff_copy(pma->p[i].maff));
12722 if (type == isl_dim_in)
12723 res_i = isl_map_intersect_domain(res_i,
12724 isl_map_copy(pma->p[i].set));
12725 else
12726 res_i = isl_map_intersect_range(res_i,
12727 isl_map_copy(pma->p[i].set));
12728 res = isl_map_union(res, res_i);
12731 isl_pw_multi_aff_free(pma);
12732 isl_map_free(map);
12733 return res;
12734 error:
12735 isl_pw_multi_aff_free(pma);
12736 isl_map_free(map);
12737 return NULL;
12740 /* Compute the preimage of "map" under the function represented by "pma".
12741 * In other words, plug in "pma" in the domain or range of "map".
12742 * The result is a map that lives in the same space as "map",
12743 * except that the space of type "type" has been replaced by
12744 * the domain space of "pma".
12746 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12747 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12749 if (!map || !pma)
12750 goto error;
12752 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12753 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12755 if (!isl_space_has_named_params(map->dim) ||
12756 !isl_space_has_named_params(pma->dim))
12757 isl_die(map->ctx, isl_error_invalid,
12758 "unaligned unnamed parameters", goto error);
12759 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12760 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12762 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12763 error:
12764 isl_pw_multi_aff_free(pma);
12765 return isl_map_free(map);
12768 /* Compute the preimage of "set" under the function represented by "pma".
12769 * In other words, plug in "pma" in "set". The result is a set
12770 * that lives in the domain space of "pma".
12772 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12773 __isl_take isl_pw_multi_aff *pma)
12775 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12778 /* Compute the preimage of the domain of "map" under the function
12779 * represented by "pma".
12780 * In other words, plug in "pma" in the domain of "map".
12781 * The result is a map that lives in the same space as "map",
12782 * except that domain space has been replaced by the domain space of "pma".
12784 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12785 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12787 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12790 /* Compute the preimage of the range of "map" under the function
12791 * represented by "pma".
12792 * In other words, plug in "pma" in the range of "map".
12793 * The result is a map that lives in the same space as "map",
12794 * except that range space has been replaced by the domain space of "pma".
12796 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12797 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12799 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12802 /* Compute the preimage of "map" under the function represented by "mpa".
12803 * In other words, plug in "mpa" in the domain or range of "map".
12804 * The result is a map that lives in the same space as "map",
12805 * except that the space of type "type" has been replaced by
12806 * the domain space of "mpa".
12808 * If the map does not involve any constraints that refer to the
12809 * dimensions of the substituted space, then the only possible
12810 * effect of "mpa" on the map is to map the space to a different space.
12811 * We create a separate isl_multi_aff to effectuate this change
12812 * in order to avoid spurious splitting of the map along the pieces
12813 * of "mpa".
12815 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12816 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12818 int n;
12819 isl_pw_multi_aff *pma;
12821 if (!map || !mpa)
12822 goto error;
12824 n = isl_map_dim(map, type);
12825 if (!isl_map_involves_dims(map, type, 0, n)) {
12826 isl_space *space;
12827 isl_multi_aff *ma;
12829 space = isl_multi_pw_aff_get_space(mpa);
12830 isl_multi_pw_aff_free(mpa);
12831 ma = isl_multi_aff_zero(space);
12832 return isl_map_preimage_multi_aff(map, type, ma);
12835 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12836 return isl_map_preimage_pw_multi_aff(map, type, pma);
12837 error:
12838 isl_map_free(map);
12839 isl_multi_pw_aff_free(mpa);
12840 return NULL;
12843 /* Compute the preimage of "map" under the function represented by "mpa".
12844 * In other words, plug in "mpa" in the domain "map".
12845 * The result is a map that lives in the same space as "map",
12846 * except that domain space has been replaced by the domain space of "mpa".
12848 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12849 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12851 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12854 /* Compute the preimage of "set" by the function represented by "mpa".
12855 * In other words, plug in "mpa" in "set".
12857 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12858 __isl_take isl_multi_pw_aff *mpa)
12860 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
12863 /* Are the "n" "coefficients" starting at "first" of the integer division
12864 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
12865 * to each other?
12866 * The "coefficient" at position 0 is the denominator.
12867 * The "coefficient" at position 1 is the constant term.
12869 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
12870 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
12871 unsigned first, unsigned n)
12873 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
12874 return isl_bool_error;
12875 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
12876 return isl_bool_error;
12877 return isl_seq_eq(bmap1->div[pos1] + first,
12878 bmap2->div[pos2] + first, n);
12881 /* Are the integer division expressions at position "pos1" in "bmap1" and
12882 * "pos2" in "bmap2" equal to each other, except that the constant terms
12883 * are different?
12885 isl_bool isl_basic_map_equal_div_expr_except_constant(
12886 __isl_keep isl_basic_map *bmap1, int pos1,
12887 __isl_keep isl_basic_map *bmap2, int pos2)
12889 isl_bool equal;
12890 unsigned total;
12892 if (!bmap1 || !bmap2)
12893 return isl_bool_error;
12894 total = isl_basic_map_total_dim(bmap1);
12895 if (total != isl_basic_map_total_dim(bmap2))
12896 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
12897 "incomparable div expressions", return isl_bool_error);
12898 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12899 0, 1);
12900 if (equal < 0 || !equal)
12901 return equal;
12902 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12903 1, 1);
12904 if (equal < 0 || equal)
12905 return isl_bool_not(equal);
12906 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12907 2, total);
12910 /* Replace the numerator of the constant term of the integer division
12911 * expression at position "div" in "bmap" by "value".
12912 * The caller guarantees that this does not change the meaning
12913 * of the input.
12915 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
12916 __isl_take isl_basic_map *bmap, int div, int value)
12918 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
12919 return isl_basic_map_free(bmap);
12921 isl_int_set_si(bmap->div[div][1], value);
12923 return bmap;
12926 /* Is the point "inner" internal to inequality constraint "ineq"
12927 * of "bset"?
12928 * The point is considered to be internal to the inequality constraint,
12929 * if it strictly lies on the positive side of the inequality constraint,
12930 * or if it lies on the constraint and the constraint is lexico-positive.
12932 static isl_bool is_internal(__isl_keep isl_vec *inner,
12933 __isl_keep isl_basic_set *bset, int ineq)
12935 isl_ctx *ctx;
12936 int pos;
12937 unsigned total;
12939 if (!inner || !bset)
12940 return isl_bool_error;
12942 ctx = isl_basic_set_get_ctx(bset);
12943 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
12944 &ctx->normalize_gcd);
12945 if (!isl_int_is_zero(ctx->normalize_gcd))
12946 return isl_int_is_nonneg(ctx->normalize_gcd);
12948 total = isl_basic_set_dim(bset, isl_dim_all);
12949 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
12950 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
12953 /* Tighten the inequality constraints of "bset" that are outward with respect
12954 * to the point "vec".
12955 * That is, tighten the constraints that are not satisfied by "vec".
12957 * "vec" is a point internal to some superset S of "bset" that is used
12958 * to make the subsets of S disjoint, by tightening one half of the constraints
12959 * that separate two subsets. In particular, the constraints of S
12960 * are all satisfied by "vec" and should not be tightened.
12961 * Of the internal constraints, those that have "vec" on the outside
12962 * are tightened. The shared facet is included in the adjacent subset
12963 * with the opposite constraint.
12964 * For constraints that saturate "vec", this criterion cannot be used
12965 * to determine which of the two sides should be tightened.
12966 * Instead, the sign of the first non-zero coefficient is used
12967 * to make this choice. Note that this second criterion is never used
12968 * on the constraints of S since "vec" is interior to "S".
12970 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
12971 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
12973 int j;
12975 bset = isl_basic_set_cow(bset);
12976 if (!bset)
12977 return NULL;
12978 for (j = 0; j < bset->n_ineq; ++j) {
12979 isl_bool internal;
12981 internal = is_internal(vec, bset, j);
12982 if (internal < 0)
12983 return isl_basic_set_free(bset);
12984 if (internal)
12985 continue;
12986 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
12989 return bset;