python: remove redundant is_string_type
[isl.git] / isl_map.c
blob1ba2417a37ec4b6524cbe861776b4791d303bdfd
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 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
207 int m;
208 if (!map || !set)
209 return -1;
210 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
211 if (m < 0 || !m)
212 return m;
213 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
214 set->dim, isl_dim_set);
217 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
218 __isl_keep isl_basic_set *bset)
220 isl_bool m;
221 if (!bmap || !bset)
222 return isl_bool_error;
223 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
224 if (m < 0 || !m)
225 return m;
226 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
227 bset->dim, isl_dim_set);
230 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
232 int m;
233 if (!map || !set)
234 return -1;
235 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
236 if (m < 0 || !m)
237 return m;
238 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
239 set->dim, isl_dim_set);
242 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
243 struct isl_basic_set *bset)
245 int m;
246 if (!bmap || !bset)
247 return -1;
248 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
249 if (m < 0 || !m)
250 return m;
251 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
252 bset->dim, isl_dim_set);
255 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
257 return bmap ? bmap->ctx : NULL;
260 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
262 return bset ? bset->ctx : NULL;
265 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
267 return map ? map->ctx : NULL;
270 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
272 return set ? set->ctx : NULL;
275 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
277 if (!bmap)
278 return NULL;
279 return isl_space_copy(bmap->dim);
282 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
284 if (!bset)
285 return NULL;
286 return isl_space_copy(bset->dim);
289 /* Extract the divs in "bmap" as a matrix.
291 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
293 int i;
294 isl_ctx *ctx;
295 isl_mat *div;
296 unsigned total;
297 unsigned cols;
299 if (!bmap)
300 return NULL;
302 ctx = isl_basic_map_get_ctx(bmap);
303 total = isl_space_dim(bmap->dim, isl_dim_all);
304 cols = 1 + 1 + total + bmap->n_div;
305 div = isl_mat_alloc(ctx, bmap->n_div, cols);
306 if (!div)
307 return NULL;
309 for (i = 0; i < bmap->n_div; ++i)
310 isl_seq_cpy(div->row[i], bmap->div[i], cols);
312 return div;
315 /* Extract the divs in "bset" as a matrix.
317 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
319 return isl_basic_map_get_divs(bset);
322 __isl_give isl_local_space *isl_basic_map_get_local_space(
323 __isl_keep isl_basic_map *bmap)
325 isl_mat *div;
327 if (!bmap)
328 return NULL;
330 div = isl_basic_map_get_divs(bmap);
331 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
334 __isl_give isl_local_space *isl_basic_set_get_local_space(
335 __isl_keep isl_basic_set *bset)
337 return isl_basic_map_get_local_space(bset);
340 /* For each known div d = floor(f/m), add the constraints
342 * f - m d >= 0
343 * -(f-(m-1)) + m d >= 0
345 * Do not finalize the result.
347 static __isl_give isl_basic_map *add_known_div_constraints(
348 __isl_take isl_basic_map *bmap)
350 int i;
351 unsigned n_div;
353 if (!bmap)
354 return NULL;
355 n_div = isl_basic_map_dim(bmap, isl_dim_div);
356 if (n_div == 0)
357 return bmap;
358 bmap = isl_basic_map_cow(bmap);
359 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
360 if (!bmap)
361 return NULL;
362 for (i = 0; i < n_div; ++i) {
363 if (isl_int_is_zero(bmap->div[i][0]))
364 continue;
365 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
366 return isl_basic_map_free(bmap);
369 return bmap;
372 __isl_give isl_basic_map *isl_basic_map_from_local_space(
373 __isl_take isl_local_space *ls)
375 int i;
376 int n_div;
377 isl_basic_map *bmap;
379 if (!ls)
380 return NULL;
382 n_div = isl_local_space_dim(ls, isl_dim_div);
383 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
384 n_div, 0, 2 * n_div);
386 for (i = 0; i < n_div; ++i)
387 if (isl_basic_map_alloc_div(bmap) < 0)
388 goto error;
390 for (i = 0; i < n_div; ++i)
391 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
392 bmap = add_known_div_constraints(bmap);
394 isl_local_space_free(ls);
395 return bmap;
396 error:
397 isl_local_space_free(ls);
398 isl_basic_map_free(bmap);
399 return NULL;
402 __isl_give isl_basic_set *isl_basic_set_from_local_space(
403 __isl_take isl_local_space *ls)
405 return isl_basic_map_from_local_space(ls);
408 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
410 if (!map)
411 return NULL;
412 return isl_space_copy(map->dim);
415 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
417 if (!set)
418 return NULL;
419 return isl_space_copy(set->dim);
422 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
423 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
425 bmap = isl_basic_map_cow(bmap);
426 if (!bmap)
427 return NULL;
428 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
429 if (!bmap->dim)
430 goto error;
431 bmap = isl_basic_map_finalize(bmap);
432 return bmap;
433 error:
434 isl_basic_map_free(bmap);
435 return NULL;
438 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
439 __isl_take isl_basic_set *bset, const char *s)
441 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
444 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
445 enum isl_dim_type type)
447 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
450 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
451 enum isl_dim_type type, const char *s)
453 int i;
455 map = isl_map_cow(map);
456 if (!map)
457 return NULL;
459 map->dim = isl_space_set_tuple_name(map->dim, type, s);
460 if (!map->dim)
461 goto error;
463 for (i = 0; i < map->n; ++i) {
464 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
465 if (!map->p[i])
466 goto error;
469 return map;
470 error:
471 isl_map_free(map);
472 return NULL;
475 /* Replace the identifier of the tuple of type "type" by "id".
477 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
478 __isl_take isl_basic_map *bmap,
479 enum isl_dim_type type, __isl_take isl_id *id)
481 bmap = isl_basic_map_cow(bmap);
482 if (!bmap)
483 goto error;
484 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
485 if (!bmap->dim)
486 return isl_basic_map_free(bmap);
487 bmap = isl_basic_map_finalize(bmap);
488 return bmap;
489 error:
490 isl_id_free(id);
491 return NULL;
494 /* Replace the identifier of the tuple by "id".
496 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
497 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
499 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
502 /* Does the input or output tuple have a name?
504 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
506 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
509 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
510 enum isl_dim_type type)
512 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
515 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
516 const char *s)
518 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
519 isl_dim_set, s));
522 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
523 enum isl_dim_type type, __isl_take isl_id *id)
525 map = isl_map_cow(map);
526 if (!map)
527 goto error;
529 map->dim = isl_space_set_tuple_id(map->dim, type, id);
531 return isl_map_reset_space(map, isl_space_copy(map->dim));
532 error:
533 isl_id_free(id);
534 return NULL;
537 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
538 __isl_take isl_id *id)
540 return isl_map_set_tuple_id(set, isl_dim_set, id);
543 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
544 enum isl_dim_type type)
546 map = isl_map_cow(map);
547 if (!map)
548 return NULL;
550 map->dim = isl_space_reset_tuple_id(map->dim, type);
552 return isl_map_reset_space(map, isl_space_copy(map->dim));
555 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
557 return isl_map_reset_tuple_id(set, isl_dim_set);
560 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
562 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
565 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
566 enum isl_dim_type type)
568 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
571 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
573 return isl_map_has_tuple_id(set, isl_dim_set);
576 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
578 return isl_map_get_tuple_id(set, isl_dim_set);
581 /* Does the set tuple have a name?
583 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
585 if (!set)
586 return isl_bool_error;
587 return isl_space_has_tuple_name(set->dim, isl_dim_set);
591 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
593 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
596 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
598 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
601 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
602 enum isl_dim_type type, unsigned pos)
604 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
607 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
608 enum isl_dim_type type, unsigned pos)
610 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
613 /* Does the given dimension have a name?
615 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
616 enum isl_dim_type type, unsigned pos)
618 if (!map)
619 return isl_bool_error;
620 return isl_space_has_dim_name(map->dim, type, pos);
623 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
624 enum isl_dim_type type, unsigned pos)
626 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
629 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
630 enum isl_dim_type type, unsigned pos)
632 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
635 /* Does the given dimension have a name?
637 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
638 enum isl_dim_type type, unsigned pos)
640 if (!set)
641 return isl_bool_error;
642 return isl_space_has_dim_name(set->dim, type, pos);
645 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
646 __isl_take isl_basic_map *bmap,
647 enum isl_dim_type type, unsigned pos, const char *s)
649 bmap = isl_basic_map_cow(bmap);
650 if (!bmap)
651 return NULL;
652 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
653 if (!bmap->dim)
654 goto error;
655 return isl_basic_map_finalize(bmap);
656 error:
657 isl_basic_map_free(bmap);
658 return NULL;
661 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
662 enum isl_dim_type type, unsigned pos, const char *s)
664 int i;
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
670 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
671 if (!map->dim)
672 goto error;
674 for (i = 0; i < map->n; ++i) {
675 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
676 if (!map->p[i])
677 goto error;
680 return map;
681 error:
682 isl_map_free(map);
683 return NULL;
686 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
687 __isl_take isl_basic_set *bset,
688 enum isl_dim_type type, unsigned pos, const char *s)
690 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
691 type, pos, s));
694 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
695 enum isl_dim_type type, unsigned pos, const char *s)
697 return set_from_map(isl_map_set_dim_name(set_to_map(set),
698 type, pos, s));
701 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
702 enum isl_dim_type type, unsigned pos)
704 if (!bmap)
705 return isl_bool_error;
706 return isl_space_has_dim_id(bmap->dim, type, pos);
709 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
710 enum isl_dim_type type, unsigned pos)
712 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
715 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
716 enum isl_dim_type type, unsigned pos)
718 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
721 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
722 enum isl_dim_type type, unsigned pos)
724 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
727 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
728 enum isl_dim_type type, unsigned pos)
730 return isl_map_has_dim_id(set, type, pos);
733 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
734 enum isl_dim_type type, unsigned pos)
736 return isl_map_get_dim_id(set, type, pos);
739 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
740 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
742 map = isl_map_cow(map);
743 if (!map)
744 goto error;
746 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
748 return isl_map_reset_space(map, isl_space_copy(map->dim));
749 error:
750 isl_id_free(id);
751 return NULL;
754 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
755 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
757 return isl_map_set_dim_id(set, type, pos, id);
760 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
761 __isl_keep isl_id *id)
763 if (!map)
764 return -1;
765 return isl_space_find_dim_by_id(map->dim, type, id);
768 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
769 __isl_keep isl_id *id)
771 return isl_map_find_dim_by_id(set, type, id);
774 /* Return the position of the dimension of the given type and name
775 * in "bmap".
776 * Return -1 if no such dimension can be found.
778 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
779 enum isl_dim_type type, const char *name)
781 if (!bmap)
782 return -1;
783 return isl_space_find_dim_by_name(bmap->dim, type, name);
786 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
787 const char *name)
789 if (!map)
790 return -1;
791 return isl_space_find_dim_by_name(map->dim, type, name);
794 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
795 const char *name)
797 return isl_map_find_dim_by_name(set, type, name);
800 /* Reset the user pointer on all identifiers of parameters and tuples
801 * of the space of "map".
803 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
805 isl_space *space;
807 space = isl_map_get_space(map);
808 space = isl_space_reset_user(space);
809 map = isl_map_reset_space(map, space);
811 return map;
814 /* Reset the user pointer on all identifiers of parameters and tuples
815 * of the space of "set".
817 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
819 return isl_map_reset_user(set);
822 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
824 if (!bmap)
825 return -1;
826 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
829 /* Has "map" been marked as a rational map?
830 * In particular, have all basic maps in "map" been marked this way?
831 * An empty map is not considered to be rational.
832 * Maps where only some of the basic maps are marked rational
833 * are not allowed.
835 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
837 int i;
838 isl_bool rational;
840 if (!map)
841 return isl_bool_error;
842 if (map->n == 0)
843 return isl_bool_false;
844 rational = isl_basic_map_is_rational(map->p[0]);
845 if (rational < 0)
846 return rational;
847 for (i = 1; i < map->n; ++i) {
848 isl_bool rational_i;
850 rational_i = isl_basic_map_is_rational(map->p[i]);
851 if (rational_i < 0)
852 return rational;
853 if (rational != rational_i)
854 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
855 "mixed rational and integer basic maps "
856 "not supported", return isl_bool_error);
859 return rational;
862 /* Has "set" been marked as a rational set?
863 * In particular, have all basic set in "set" been marked this way?
864 * An empty set is not considered to be rational.
865 * Sets where only some of the basic sets are marked rational
866 * are not allowed.
868 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
870 return isl_map_is_rational(set);
873 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
875 return isl_basic_map_is_rational(bset);
878 /* Does "bmap" contain any rational points?
880 * If "bmap" has an equality for each dimension, equating the dimension
881 * to an integer constant, then it has no rational points, even if it
882 * is marked as rational.
884 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
886 int has_rational = 1;
887 unsigned total;
889 if (!bmap)
890 return -1;
891 if (isl_basic_map_plain_is_empty(bmap))
892 return 0;
893 if (!isl_basic_map_is_rational(bmap))
894 return 0;
895 bmap = isl_basic_map_copy(bmap);
896 bmap = isl_basic_map_implicit_equalities(bmap);
897 if (!bmap)
898 return -1;
899 total = isl_basic_map_total_dim(bmap);
900 if (bmap->n_eq == total) {
901 int i, j;
902 for (i = 0; i < bmap->n_eq; ++i) {
903 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
904 if (j < 0)
905 break;
906 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
907 !isl_int_is_negone(bmap->eq[i][1 + j]))
908 break;
909 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
910 total - j - 1);
911 if (j >= 0)
912 break;
914 if (i == bmap->n_eq)
915 has_rational = 0;
917 isl_basic_map_free(bmap);
919 return has_rational;
922 /* Does "map" contain any rational points?
924 int isl_map_has_rational(__isl_keep isl_map *map)
926 int i;
927 int has_rational;
929 if (!map)
930 return -1;
931 for (i = 0; i < map->n; ++i) {
932 has_rational = isl_basic_map_has_rational(map->p[i]);
933 if (has_rational < 0)
934 return -1;
935 if (has_rational)
936 return 1;
938 return 0;
941 /* Does "set" contain any rational points?
943 int isl_set_has_rational(__isl_keep isl_set *set)
945 return isl_map_has_rational(set);
948 /* Is this basic set a parameter domain?
950 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
952 if (!bset)
953 return -1;
954 return isl_space_is_params(bset->dim);
957 /* Is this set a parameter domain?
959 isl_bool isl_set_is_params(__isl_keep isl_set *set)
961 if (!set)
962 return isl_bool_error;
963 return isl_space_is_params(set->dim);
966 /* Is this map actually a parameter domain?
967 * Users should never call this function. Outside of isl,
968 * a map can never be a parameter domain.
970 int isl_map_is_params(__isl_keep isl_map *map)
972 if (!map)
973 return -1;
974 return isl_space_is_params(map->dim);
977 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
978 struct isl_basic_map *bmap, unsigned extra,
979 unsigned n_eq, unsigned n_ineq)
981 int i;
982 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
984 bmap->ctx = ctx;
985 isl_ctx_ref(ctx);
987 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
988 if (isl_blk_is_error(bmap->block))
989 goto error;
991 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
992 if ((n_ineq + n_eq) && !bmap->ineq)
993 goto error;
995 if (extra == 0) {
996 bmap->block2 = isl_blk_empty();
997 bmap->div = NULL;
998 } else {
999 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1000 if (isl_blk_is_error(bmap->block2))
1001 goto error;
1003 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1004 if (!bmap->div)
1005 goto error;
1008 for (i = 0; i < n_ineq + n_eq; ++i)
1009 bmap->ineq[i] = bmap->block.data + i * row_size;
1011 for (i = 0; i < extra; ++i)
1012 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1014 bmap->ref = 1;
1015 bmap->flags = 0;
1016 bmap->c_size = n_eq + n_ineq;
1017 bmap->eq = bmap->ineq + n_ineq;
1018 bmap->extra = extra;
1019 bmap->n_eq = 0;
1020 bmap->n_ineq = 0;
1021 bmap->n_div = 0;
1022 bmap->sample = NULL;
1024 return bmap;
1025 error:
1026 isl_basic_map_free(bmap);
1027 return NULL;
1030 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1031 unsigned nparam, unsigned dim, unsigned extra,
1032 unsigned n_eq, unsigned n_ineq)
1034 struct isl_basic_map *bmap;
1035 isl_space *space;
1037 space = isl_space_set_alloc(ctx, nparam, dim);
1038 if (!space)
1039 return NULL;
1041 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1042 return bset_from_bmap(bmap);
1045 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1046 unsigned extra, unsigned n_eq, unsigned n_ineq)
1048 struct isl_basic_map *bmap;
1049 if (!dim)
1050 return NULL;
1051 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1052 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1053 return bset_from_bmap(bmap);
1054 error:
1055 isl_space_free(dim);
1056 return NULL;
1059 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1060 unsigned extra, unsigned n_eq, unsigned n_ineq)
1062 struct isl_basic_map *bmap;
1064 if (!dim)
1065 return NULL;
1066 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1067 if (!bmap)
1068 goto error;
1069 bmap->dim = dim;
1071 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1072 error:
1073 isl_space_free(dim);
1074 return NULL;
1077 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1078 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1079 unsigned n_eq, unsigned n_ineq)
1081 struct isl_basic_map *bmap;
1082 isl_space *dim;
1084 dim = isl_space_alloc(ctx, nparam, in, out);
1085 if (!dim)
1086 return NULL;
1088 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1089 return bmap;
1092 static void dup_constraints(
1093 struct isl_basic_map *dst, struct isl_basic_map *src)
1095 int i;
1096 unsigned total = isl_basic_map_total_dim(src);
1098 for (i = 0; i < src->n_eq; ++i) {
1099 int j = isl_basic_map_alloc_equality(dst);
1100 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1103 for (i = 0; i < src->n_ineq; ++i) {
1104 int j = isl_basic_map_alloc_inequality(dst);
1105 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1108 for (i = 0; i < src->n_div; ++i) {
1109 int j = isl_basic_map_alloc_div(dst);
1110 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1112 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1115 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1117 struct isl_basic_map *dup;
1119 if (!bmap)
1120 return NULL;
1121 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1122 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1123 if (!dup)
1124 return NULL;
1125 dup_constraints(dup, bmap);
1126 dup->flags = bmap->flags;
1127 dup->sample = isl_vec_copy(bmap->sample);
1128 return dup;
1131 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1133 struct isl_basic_map *dup;
1135 dup = isl_basic_map_dup(bset_to_bmap(bset));
1136 return bset_from_bmap(dup);
1139 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1141 if (!bset)
1142 return NULL;
1144 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1145 bset->ref++;
1146 return bset;
1148 return isl_basic_set_dup(bset);
1151 struct isl_set *isl_set_copy(struct isl_set *set)
1153 if (!set)
1154 return NULL;
1156 set->ref++;
1157 return set;
1160 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1162 if (!bmap)
1163 return NULL;
1165 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1166 bmap->ref++;
1167 return bmap;
1169 bmap = isl_basic_map_dup(bmap);
1170 if (bmap)
1171 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1172 return bmap;
1175 struct isl_map *isl_map_copy(struct isl_map *map)
1177 if (!map)
1178 return NULL;
1180 map->ref++;
1181 return map;
1184 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1186 if (!bmap)
1187 return NULL;
1189 if (--bmap->ref > 0)
1190 return NULL;
1192 isl_ctx_deref(bmap->ctx);
1193 free(bmap->div);
1194 isl_blk_free(bmap->ctx, bmap->block2);
1195 free(bmap->ineq);
1196 isl_blk_free(bmap->ctx, bmap->block);
1197 isl_vec_free(bmap->sample);
1198 isl_space_free(bmap->dim);
1199 free(bmap);
1201 return NULL;
1204 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1206 return isl_basic_map_free(bset_to_bmap(bset));
1209 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1211 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1214 __isl_give isl_map *isl_map_align_params_map_map_and(
1215 __isl_take isl_map *map1, __isl_take isl_map *map2,
1216 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1217 __isl_take isl_map *map2))
1219 if (!map1 || !map2)
1220 goto error;
1221 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1222 return fn(map1, map2);
1223 if (!isl_space_has_named_params(map1->dim) ||
1224 !isl_space_has_named_params(map2->dim))
1225 isl_die(map1->ctx, isl_error_invalid,
1226 "unaligned unnamed parameters", goto error);
1227 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1228 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1229 return fn(map1, map2);
1230 error:
1231 isl_map_free(map1);
1232 isl_map_free(map2);
1233 return NULL;
1236 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1237 __isl_keep isl_map *map2,
1238 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1240 isl_bool r;
1242 if (!map1 || !map2)
1243 return isl_bool_error;
1244 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1245 return fn(map1, map2);
1246 if (!isl_space_has_named_params(map1->dim) ||
1247 !isl_space_has_named_params(map2->dim))
1248 isl_die(map1->ctx, isl_error_invalid,
1249 "unaligned unnamed parameters", return isl_bool_error);
1250 map1 = isl_map_copy(map1);
1251 map2 = isl_map_copy(map2);
1252 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1253 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1254 r = fn(map1, map2);
1255 isl_map_free(map1);
1256 isl_map_free(map2);
1257 return r;
1260 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1262 struct isl_ctx *ctx;
1263 if (!bmap)
1264 return -1;
1265 ctx = bmap->ctx;
1266 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1267 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1268 return -1);
1269 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1274 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1275 isl_int *t;
1276 int j = isl_basic_map_alloc_inequality(bmap);
1277 if (j < 0)
1278 return -1;
1279 t = bmap->ineq[j];
1280 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1281 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1282 bmap->eq[-1] = t;
1283 bmap->n_eq++;
1284 bmap->n_ineq--;
1285 bmap->eq--;
1286 return 0;
1288 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1289 bmap->extra - bmap->n_div);
1290 return bmap->n_eq++;
1293 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1295 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1298 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1300 if (!bmap)
1301 return -1;
1302 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1303 bmap->n_eq -= n;
1304 return 0;
1307 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1309 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1312 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1314 isl_int *t;
1315 if (!bmap)
1316 return -1;
1317 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1319 if (pos != bmap->n_eq - 1) {
1320 t = bmap->eq[pos];
1321 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1322 bmap->eq[bmap->n_eq - 1] = t;
1324 bmap->n_eq--;
1325 return 0;
1328 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1330 return isl_basic_map_drop_equality(bset_to_bmap(bset), pos);
1333 /* Turn inequality "pos" of "bmap" into an equality.
1335 * In particular, we move the inequality in front of the equalities
1336 * and move the last inequality in the position of the moved inequality.
1337 * Note that isl_tab_make_equalities_explicit depends on this particular
1338 * change in the ordering of the constraints.
1340 void isl_basic_map_inequality_to_equality(
1341 struct isl_basic_map *bmap, unsigned pos)
1343 isl_int *t;
1345 t = bmap->ineq[pos];
1346 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1347 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1348 bmap->eq[-1] = t;
1349 bmap->n_eq++;
1350 bmap->n_ineq--;
1351 bmap->eq--;
1352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1353 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1355 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1358 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1360 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1363 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1365 struct isl_ctx *ctx;
1366 if (!bmap)
1367 return -1;
1368 ctx = bmap->ctx;
1369 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1370 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1371 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1372 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1373 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1374 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1375 1 + isl_basic_map_total_dim(bmap),
1376 bmap->extra - bmap->n_div);
1377 return bmap->n_ineq++;
1380 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1382 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1385 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1387 if (!bmap)
1388 return -1;
1389 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1390 bmap->n_ineq -= n;
1391 return 0;
1394 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1396 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1399 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1401 isl_int *t;
1402 if (!bmap)
1403 return -1;
1404 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1406 if (pos != bmap->n_ineq - 1) {
1407 t = bmap->ineq[pos];
1408 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1409 bmap->ineq[bmap->n_ineq - 1] = t;
1410 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1412 bmap->n_ineq--;
1413 return 0;
1416 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1418 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1421 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1422 isl_int *eq)
1424 int k;
1426 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1427 if (!bmap)
1428 return NULL;
1429 k = isl_basic_map_alloc_equality(bmap);
1430 if (k < 0)
1431 goto error;
1432 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1433 return bmap;
1434 error:
1435 isl_basic_map_free(bmap);
1436 return NULL;
1439 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1440 isl_int *eq)
1442 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1445 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1446 isl_int *ineq)
1448 int k;
1450 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1451 if (!bmap)
1452 return NULL;
1453 k = isl_basic_map_alloc_inequality(bmap);
1454 if (k < 0)
1455 goto error;
1456 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1457 return bmap;
1458 error:
1459 isl_basic_map_free(bmap);
1460 return NULL;
1463 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1464 isl_int *ineq)
1466 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1469 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1471 if (!bmap)
1472 return -1;
1473 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1474 isl_seq_clr(bmap->div[bmap->n_div] +
1475 1 + 1 + isl_basic_map_total_dim(bmap),
1476 bmap->extra - bmap->n_div);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1478 return bmap->n_div++;
1481 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1483 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1486 /* Check that there are "n" dimensions of type "type" starting at "first"
1487 * in "bmap".
1489 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1490 enum isl_dim_type type, unsigned first, unsigned n)
1492 unsigned dim;
1494 if (!bmap)
1495 return isl_stat_error;
1496 dim = isl_basic_map_dim(bmap, type);
1497 if (first + n > dim || first + n < first)
1498 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1499 "position or range out of bounds",
1500 return isl_stat_error);
1501 return isl_stat_ok;
1504 /* Insert an extra integer division, prescribed by "div", to "bmap"
1505 * at (integer division) position "pos".
1507 * The integer division is first added at the end and then moved
1508 * into the right position.
1510 __isl_give isl_basic_map *isl_basic_map_insert_div(
1511 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1513 int i, k;
1515 bmap = isl_basic_map_cow(bmap);
1516 if (!bmap || !div)
1517 return isl_basic_map_free(bmap);
1519 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1520 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1521 "unexpected size", return isl_basic_map_free(bmap));
1522 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1523 return isl_basic_map_free(bmap);
1525 bmap = isl_basic_map_extend_space(bmap,
1526 isl_basic_map_get_space(bmap), 1, 0, 2);
1527 k = isl_basic_map_alloc_div(bmap);
1528 if (k < 0)
1529 return isl_basic_map_free(bmap);
1530 isl_seq_cpy(bmap->div[k], div->el, div->size);
1531 isl_int_set_si(bmap->div[k][div->size], 0);
1533 for (i = k; i > pos; --i)
1534 isl_basic_map_swap_div(bmap, i, i - 1);
1536 return bmap;
1539 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1541 if (!bmap)
1542 return -1;
1543 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1544 bmap->n_div -= n;
1545 return 0;
1548 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1550 return isl_basic_map_free_div(bset_to_bmap(bset), n);
1553 /* Copy constraint from src to dst, putting the vars of src at offset
1554 * dim_off in dst and the divs of src at offset div_off in dst.
1555 * If both sets are actually map, then dim_off applies to the input
1556 * variables.
1558 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1559 struct isl_basic_map *src_map, isl_int *src,
1560 unsigned in_off, unsigned out_off, unsigned div_off)
1562 unsigned src_nparam = isl_basic_map_n_param(src_map);
1563 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1564 unsigned src_in = isl_basic_map_n_in(src_map);
1565 unsigned dst_in = isl_basic_map_n_in(dst_map);
1566 unsigned src_out = isl_basic_map_n_out(src_map);
1567 unsigned dst_out = isl_basic_map_n_out(dst_map);
1568 isl_int_set(dst[0], src[0]);
1569 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1570 if (dst_nparam > src_nparam)
1571 isl_seq_clr(dst+1+src_nparam,
1572 dst_nparam - src_nparam);
1573 isl_seq_clr(dst+1+dst_nparam, in_off);
1574 isl_seq_cpy(dst+1+dst_nparam+in_off,
1575 src+1+src_nparam,
1576 isl_min(dst_in-in_off, src_in));
1577 if (dst_in-in_off > src_in)
1578 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1579 dst_in - in_off - src_in);
1580 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1581 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1582 src+1+src_nparam+src_in,
1583 isl_min(dst_out-out_off, src_out));
1584 if (dst_out-out_off > src_out)
1585 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1586 dst_out - out_off - src_out);
1587 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1588 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1589 src+1+src_nparam+src_in+src_out,
1590 isl_min(dst_map->extra-div_off, src_map->n_div));
1591 if (dst_map->n_div-div_off > src_map->n_div)
1592 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1593 div_off+src_map->n_div,
1594 dst_map->n_div - div_off - src_map->n_div);
1597 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1598 struct isl_basic_map *src_map, isl_int *src,
1599 unsigned in_off, unsigned out_off, unsigned div_off)
1601 isl_int_set(dst[0], src[0]);
1602 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1605 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1606 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1608 int i;
1609 unsigned div_off;
1611 if (!bmap1 || !bmap2)
1612 goto error;
1614 div_off = bmap1->n_div;
1616 for (i = 0; i < bmap2->n_eq; ++i) {
1617 int i1 = isl_basic_map_alloc_equality(bmap1);
1618 if (i1 < 0)
1619 goto error;
1620 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1621 i_pos, o_pos, div_off);
1624 for (i = 0; i < bmap2->n_ineq; ++i) {
1625 int i1 = isl_basic_map_alloc_inequality(bmap1);
1626 if (i1 < 0)
1627 goto error;
1628 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1629 i_pos, o_pos, div_off);
1632 for (i = 0; i < bmap2->n_div; ++i) {
1633 int i1 = isl_basic_map_alloc_div(bmap1);
1634 if (i1 < 0)
1635 goto error;
1636 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1637 i_pos, o_pos, div_off);
1640 isl_basic_map_free(bmap2);
1642 return bmap1;
1644 error:
1645 isl_basic_map_free(bmap1);
1646 isl_basic_map_free(bmap2);
1647 return NULL;
1650 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1651 struct isl_basic_set *bset2, unsigned pos)
1653 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1654 bset_to_bmap(bset2), 0, pos));
1657 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1658 __isl_take isl_space *dim, unsigned extra,
1659 unsigned n_eq, unsigned n_ineq)
1661 struct isl_basic_map *ext;
1662 unsigned flags;
1663 int dims_ok;
1665 if (!dim)
1666 goto error;
1668 if (!base)
1669 goto error;
1671 dims_ok = isl_space_is_equal(base->dim, dim) &&
1672 base->extra >= base->n_div + extra;
1674 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1675 room_for_ineq(base, n_ineq)) {
1676 isl_space_free(dim);
1677 return base;
1680 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1681 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1682 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1683 extra += base->extra;
1684 n_eq += base->n_eq;
1685 n_ineq += base->n_ineq;
1687 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1688 dim = NULL;
1689 if (!ext)
1690 goto error;
1692 if (dims_ok)
1693 ext->sample = isl_vec_copy(base->sample);
1694 flags = base->flags;
1695 ext = add_constraints(ext, base, 0, 0);
1696 if (ext) {
1697 ext->flags = flags;
1698 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1701 return ext;
1703 error:
1704 isl_space_free(dim);
1705 isl_basic_map_free(base);
1706 return NULL;
1709 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1710 __isl_take isl_space *dim, unsigned extra,
1711 unsigned n_eq, unsigned n_ineq)
1713 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1714 dim, extra, n_eq, n_ineq));
1717 struct isl_basic_map *isl_basic_map_extend_constraints(
1718 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1720 if (!base)
1721 return NULL;
1722 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1723 0, n_eq, n_ineq);
1726 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1727 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1728 unsigned n_eq, unsigned n_ineq)
1730 struct isl_basic_map *bmap;
1731 isl_space *dim;
1733 if (!base)
1734 return NULL;
1735 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1736 if (!dim)
1737 goto error;
1739 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1740 return bmap;
1741 error:
1742 isl_basic_map_free(base);
1743 return NULL;
1746 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1747 unsigned nparam, unsigned dim, unsigned extra,
1748 unsigned n_eq, unsigned n_ineq)
1750 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1751 nparam, 0, dim, extra, n_eq, n_ineq));
1754 struct isl_basic_set *isl_basic_set_extend_constraints(
1755 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1757 isl_basic_map *bmap = bset_to_bmap(base);
1758 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1759 return bset_from_bmap(bmap);
1762 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1764 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1767 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1769 if (!bmap)
1770 return NULL;
1772 if (bmap->ref > 1) {
1773 bmap->ref--;
1774 bmap = isl_basic_map_dup(bmap);
1776 if (bmap) {
1777 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1778 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1780 return bmap;
1783 /* Clear all cached information in "map", either because it is about
1784 * to be modified or because it is being freed.
1785 * Always return the same pointer that is passed in.
1786 * This is needed for the use in isl_map_free.
1788 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1790 isl_basic_map_free(map->cached_simple_hull[0]);
1791 isl_basic_map_free(map->cached_simple_hull[1]);
1792 map->cached_simple_hull[0] = NULL;
1793 map->cached_simple_hull[1] = NULL;
1794 return map;
1797 struct isl_set *isl_set_cow(struct isl_set *set)
1799 return isl_map_cow(set);
1802 /* Return an isl_map that is equal to "map" and that has only
1803 * a single reference.
1805 * If the original input already has only one reference, then
1806 * simply return it, but clear all cached information, since
1807 * it may be rendered invalid by the operations that will be
1808 * performed on the result.
1810 * Otherwise, create a duplicate (without any cached information).
1812 struct isl_map *isl_map_cow(struct isl_map *map)
1814 if (!map)
1815 return NULL;
1817 if (map->ref == 1)
1818 return clear_caches(map);
1819 map->ref--;
1820 return isl_map_dup(map);
1823 static void swap_vars(struct isl_blk blk, isl_int *a,
1824 unsigned a_len, unsigned b_len)
1826 isl_seq_cpy(blk.data, a+a_len, b_len);
1827 isl_seq_cpy(blk.data+b_len, a, a_len);
1828 isl_seq_cpy(a, blk.data, b_len+a_len);
1831 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1832 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1834 int i;
1835 struct isl_blk blk;
1837 if (!bmap)
1838 goto error;
1840 isl_assert(bmap->ctx,
1841 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1843 if (n1 == 0 || n2 == 0)
1844 return bmap;
1846 bmap = isl_basic_map_cow(bmap);
1847 if (!bmap)
1848 return NULL;
1850 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1851 if (isl_blk_is_error(blk))
1852 goto error;
1854 for (i = 0; i < bmap->n_eq; ++i)
1855 swap_vars(blk,
1856 bmap->eq[i] + pos, n1, n2);
1858 for (i = 0; i < bmap->n_ineq; ++i)
1859 swap_vars(blk,
1860 bmap->ineq[i] + pos, n1, n2);
1862 for (i = 0; i < bmap->n_div; ++i)
1863 swap_vars(blk,
1864 bmap->div[i]+1 + pos, n1, n2);
1866 isl_blk_free(bmap->ctx, blk);
1868 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1869 bmap = isl_basic_map_gauss(bmap, NULL);
1870 return isl_basic_map_finalize(bmap);
1871 error:
1872 isl_basic_map_free(bmap);
1873 return NULL;
1876 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1878 int i = 0;
1879 unsigned total;
1880 if (!bmap)
1881 goto error;
1882 total = isl_basic_map_total_dim(bmap);
1883 isl_basic_map_free_div(bmap, bmap->n_div);
1884 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1885 if (bmap->n_eq > 0)
1886 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1887 else {
1888 i = isl_basic_map_alloc_equality(bmap);
1889 if (i < 0)
1890 goto error;
1892 isl_int_set_si(bmap->eq[i][0], 1);
1893 isl_seq_clr(bmap->eq[i]+1, total);
1894 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1895 isl_vec_free(bmap->sample);
1896 bmap->sample = NULL;
1897 return isl_basic_map_finalize(bmap);
1898 error:
1899 isl_basic_map_free(bmap);
1900 return NULL;
1903 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1905 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1908 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1909 * of "bmap").
1911 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1913 isl_int *t = bmap->div[a];
1914 bmap->div[a] = bmap->div[b];
1915 bmap->div[b] = t;
1918 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1919 * div definitions accordingly.
1921 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1923 int i;
1924 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1926 swap_div(bmap, a, b);
1928 for (i = 0; i < bmap->n_eq; ++i)
1929 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1931 for (i = 0; i < bmap->n_ineq; ++i)
1932 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1934 for (i = 0; i < bmap->n_div; ++i)
1935 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1936 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1939 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1940 * div definitions accordingly.
1942 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1944 isl_basic_map_swap_div(bset, a, b);
1947 /* Eliminate the specified n dimensions starting at first from the
1948 * constraints, without removing the dimensions from the space.
1949 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1951 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1952 enum isl_dim_type type, unsigned first, unsigned n)
1954 int i;
1956 if (!map)
1957 return NULL;
1958 if (n == 0)
1959 return map;
1961 if (first + n > isl_map_dim(map, type) || first + n < first)
1962 isl_die(map->ctx, isl_error_invalid,
1963 "index out of bounds", goto error);
1965 map = isl_map_cow(map);
1966 if (!map)
1967 return NULL;
1969 for (i = 0; i < map->n; ++i) {
1970 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1971 if (!map->p[i])
1972 goto error;
1974 return map;
1975 error:
1976 isl_map_free(map);
1977 return NULL;
1980 /* Eliminate the specified n dimensions starting at first from the
1981 * constraints, without removing the dimensions from the space.
1982 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1984 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1985 enum isl_dim_type type, unsigned first, unsigned n)
1987 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1990 /* Eliminate the specified n dimensions starting at first from the
1991 * constraints, without removing the dimensions from the space.
1992 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1994 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1995 unsigned first, unsigned n)
1997 return isl_set_eliminate(set, isl_dim_set, first, n);
2000 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2001 __isl_take isl_basic_map *bmap)
2003 if (!bmap)
2004 return NULL;
2005 bmap = isl_basic_map_eliminate_vars(bmap,
2006 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2007 if (!bmap)
2008 return NULL;
2009 bmap->n_div = 0;
2010 return isl_basic_map_finalize(bmap);
2013 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2014 __isl_take isl_basic_set *bset)
2016 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2019 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2021 int i;
2023 if (!map)
2024 return NULL;
2025 if (map->n == 0)
2026 return map;
2028 map = isl_map_cow(map);
2029 if (!map)
2030 return NULL;
2032 for (i = 0; i < map->n; ++i) {
2033 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2034 if (!map->p[i])
2035 goto error;
2037 return map;
2038 error:
2039 isl_map_free(map);
2040 return NULL;
2043 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2045 return isl_map_remove_divs(set);
2048 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2049 enum isl_dim_type type, unsigned first, unsigned n)
2051 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2052 return isl_basic_map_free(bmap);
2053 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2054 return bmap;
2055 bmap = isl_basic_map_eliminate_vars(bmap,
2056 isl_basic_map_offset(bmap, type) - 1 + first, n);
2057 if (!bmap)
2058 return bmap;
2059 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2060 return bmap;
2061 bmap = isl_basic_map_drop(bmap, type, first, n);
2062 return bmap;
2065 /* Return true if the definition of the given div (recursively) involves
2066 * any of the given variables.
2068 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2069 unsigned first, unsigned n)
2071 int i;
2072 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2074 if (isl_int_is_zero(bmap->div[div][0]))
2075 return 0;
2076 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2077 return 1;
2079 for (i = bmap->n_div - 1; i >= 0; --i) {
2080 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2081 continue;
2082 if (div_involves_vars(bmap, i, first, n))
2083 return 1;
2086 return 0;
2089 /* Try and add a lower and/or upper bound on "div" to "bmap"
2090 * based on inequality "i".
2091 * "total" is the total number of variables (excluding the divs).
2092 * "v" is a temporary object that can be used during the calculations.
2093 * If "lb" is set, then a lower bound should be constructed.
2094 * If "ub" is set, then an upper bound should be constructed.
2096 * The calling function has already checked that the inequality does not
2097 * reference "div", but we still need to check that the inequality is
2098 * of the right form. We'll consider the case where we want to construct
2099 * a lower bound. The construction of upper bounds is similar.
2101 * Let "div" be of the form
2103 * q = floor((a + f(x))/d)
2105 * We essentially check if constraint "i" is of the form
2107 * b + f(x) >= 0
2109 * so that we can use it to derive a lower bound on "div".
2110 * However, we allow a slightly more general form
2112 * b + g(x) >= 0
2114 * with the condition that the coefficients of g(x) - f(x) are all
2115 * divisible by d.
2116 * Rewriting this constraint as
2118 * 0 >= -b - g(x)
2120 * adding a + f(x) to both sides and dividing by d, we obtain
2122 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2124 * Taking the floor on both sides, we obtain
2126 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2128 * or
2130 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2132 * In the case of an upper bound, we construct the constraint
2134 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2137 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2138 __isl_take isl_basic_map *bmap, int div, int i,
2139 unsigned total, isl_int v, int lb, int ub)
2141 int j;
2143 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2144 if (lb) {
2145 isl_int_sub(v, bmap->ineq[i][1 + j],
2146 bmap->div[div][1 + 1 + j]);
2147 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2149 if (ub) {
2150 isl_int_add(v, bmap->ineq[i][1 + j],
2151 bmap->div[div][1 + 1 + j]);
2152 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2155 if (!lb && !ub)
2156 return bmap;
2158 bmap = isl_basic_map_cow(bmap);
2159 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2160 if (lb) {
2161 int k = isl_basic_map_alloc_inequality(bmap);
2162 if (k < 0)
2163 goto error;
2164 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2165 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2166 bmap->div[div][1 + j]);
2167 isl_int_cdiv_q(bmap->ineq[k][j],
2168 bmap->ineq[k][j], bmap->div[div][0]);
2170 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2172 if (ub) {
2173 int k = isl_basic_map_alloc_inequality(bmap);
2174 if (k < 0)
2175 goto error;
2176 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2177 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2178 bmap->div[div][1 + j]);
2179 isl_int_fdiv_q(bmap->ineq[k][j],
2180 bmap->ineq[k][j], bmap->div[div][0]);
2182 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2185 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2186 return bmap;
2187 error:
2188 isl_basic_map_free(bmap);
2189 return NULL;
2192 /* This function is called right before "div" is eliminated from "bmap"
2193 * using Fourier-Motzkin.
2194 * Look through the constraints of "bmap" for constraints on the argument
2195 * of the integer division and use them to construct constraints on the
2196 * integer division itself. These constraints can then be combined
2197 * during the Fourier-Motzkin elimination.
2198 * Note that it is only useful to introduce lower bounds on "div"
2199 * if "bmap" already contains upper bounds on "div" as the newly
2200 * introduce lower bounds can then be combined with the pre-existing
2201 * upper bounds. Similarly for upper bounds.
2202 * We therefore first check if "bmap" contains any lower and/or upper bounds
2203 * on "div".
2205 * It is interesting to note that the introduction of these constraints
2206 * can indeed lead to more accurate results, even when compared to
2207 * deriving constraints on the argument of "div" from constraints on "div".
2208 * Consider, for example, the set
2210 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2212 * The second constraint can be rewritten as
2214 * 2 * [(-i-2j+3)/4] + k >= 0
2216 * from which we can derive
2218 * -i - 2j + 3 >= -2k
2220 * or
2222 * i + 2j <= 3 + 2k
2224 * Combined with the first constraint, we obtain
2226 * -3 <= 3 + 2k or k >= -3
2228 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2229 * the first constraint, we obtain
2231 * [(i + 2j)/4] >= [-3/4] = -1
2233 * Combining this constraint with the second constraint, we obtain
2235 * k >= -2
2237 static __isl_give isl_basic_map *insert_bounds_on_div(
2238 __isl_take isl_basic_map *bmap, int div)
2240 int i;
2241 int check_lb, check_ub;
2242 isl_int v;
2243 unsigned total;
2245 if (!bmap)
2246 return NULL;
2248 if (isl_int_is_zero(bmap->div[div][0]))
2249 return bmap;
2251 total = isl_space_dim(bmap->dim, isl_dim_all);
2253 check_lb = 0;
2254 check_ub = 0;
2255 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2256 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2257 if (s > 0)
2258 check_ub = 1;
2259 if (s < 0)
2260 check_lb = 1;
2263 if (!check_lb && !check_ub)
2264 return bmap;
2266 isl_int_init(v);
2268 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2269 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2270 continue;
2272 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2273 check_lb, check_ub);
2276 isl_int_clear(v);
2278 return bmap;
2281 /* Remove all divs (recursively) involving any of the given dimensions
2282 * in their definitions.
2284 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2285 __isl_take isl_basic_map *bmap,
2286 enum isl_dim_type type, unsigned first, unsigned n)
2288 int i;
2290 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2291 return isl_basic_map_free(bmap);
2292 first += isl_basic_map_offset(bmap, type);
2294 for (i = bmap->n_div - 1; i >= 0; --i) {
2295 if (!div_involves_vars(bmap, i, first, n))
2296 continue;
2297 bmap = insert_bounds_on_div(bmap, i);
2298 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2299 if (!bmap)
2300 return NULL;
2301 i = bmap->n_div;
2304 return bmap;
2307 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2308 __isl_take isl_basic_set *bset,
2309 enum isl_dim_type type, unsigned first, unsigned n)
2311 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2314 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2315 enum isl_dim_type type, unsigned first, unsigned n)
2317 int i;
2319 if (!map)
2320 return NULL;
2321 if (map->n == 0)
2322 return map;
2324 map = isl_map_cow(map);
2325 if (!map)
2326 return NULL;
2328 for (i = 0; i < map->n; ++i) {
2329 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2330 type, first, n);
2331 if (!map->p[i])
2332 goto error;
2334 return map;
2335 error:
2336 isl_map_free(map);
2337 return NULL;
2340 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2341 enum isl_dim_type type, unsigned first, unsigned n)
2343 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2344 type, first, n));
2347 /* Does the description of "bmap" depend on the specified dimensions?
2348 * We also check whether the dimensions appear in any of the div definitions.
2349 * In principle there is no need for this check. If the dimensions appear
2350 * in a div definition, they also appear in the defining constraints of that
2351 * div.
2353 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2354 enum isl_dim_type type, unsigned first, unsigned n)
2356 int i;
2358 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2359 return isl_bool_error;
2361 first += isl_basic_map_offset(bmap, type);
2362 for (i = 0; i < bmap->n_eq; ++i)
2363 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2364 return isl_bool_true;
2365 for (i = 0; i < bmap->n_ineq; ++i)
2366 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2367 return isl_bool_true;
2368 for (i = 0; i < bmap->n_div; ++i) {
2369 if (isl_int_is_zero(bmap->div[i][0]))
2370 continue;
2371 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2372 return isl_bool_true;
2375 return isl_bool_false;
2378 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2379 enum isl_dim_type type, unsigned first, unsigned n)
2381 int i;
2383 if (!map)
2384 return isl_bool_error;
2386 if (first + n > isl_map_dim(map, type))
2387 isl_die(map->ctx, isl_error_invalid,
2388 "index out of bounds", return isl_bool_error);
2390 for (i = 0; i < map->n; ++i) {
2391 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2392 type, first, n);
2393 if (involves < 0 || involves)
2394 return involves;
2397 return isl_bool_false;
2400 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2401 enum isl_dim_type type, unsigned first, unsigned n)
2403 return isl_basic_map_involves_dims(bset, type, first, n);
2406 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2407 enum isl_dim_type type, unsigned first, unsigned n)
2409 return isl_map_involves_dims(set, type, first, n);
2412 /* Drop all constraints in bmap that involve any of the dimensions
2413 * first to first+n-1.
2415 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2416 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2418 int i;
2420 if (n == 0)
2421 return bmap;
2423 bmap = isl_basic_map_cow(bmap);
2425 if (!bmap)
2426 return NULL;
2428 for (i = bmap->n_eq - 1; i >= 0; --i) {
2429 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2430 continue;
2431 isl_basic_map_drop_equality(bmap, i);
2434 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2435 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2436 continue;
2437 isl_basic_map_drop_inequality(bmap, i);
2440 bmap = isl_basic_map_add_known_div_constraints(bmap);
2441 return bmap;
2444 /* Drop all constraints in bset that involve any of the dimensions
2445 * first to first+n-1.
2447 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2448 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2450 return isl_basic_map_drop_constraints_involving(bset, first, n);
2453 /* Drop all constraints in bmap that do not involve any of the dimensions
2454 * first to first + n - 1 of the given type.
2456 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2457 __isl_take isl_basic_map *bmap,
2458 enum isl_dim_type type, unsigned first, unsigned n)
2460 int i;
2462 if (n == 0) {
2463 isl_space *space = isl_basic_map_get_space(bmap);
2464 isl_basic_map_free(bmap);
2465 return isl_basic_map_universe(space);
2467 bmap = isl_basic_map_cow(bmap);
2468 if (!bmap)
2469 return NULL;
2471 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2472 return isl_basic_map_free(bmap);
2474 first += isl_basic_map_offset(bmap, type) - 1;
2476 for (i = bmap->n_eq - 1; i >= 0; --i) {
2477 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2478 continue;
2479 isl_basic_map_drop_equality(bmap, i);
2482 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2483 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2484 continue;
2485 isl_basic_map_drop_inequality(bmap, i);
2488 bmap = isl_basic_map_add_known_div_constraints(bmap);
2489 return bmap;
2492 /* Drop all constraints in bset that do not involve any of the dimensions
2493 * first to first + n - 1 of the given type.
2495 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2496 __isl_take isl_basic_set *bset,
2497 enum isl_dim_type type, unsigned first, unsigned n)
2499 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2500 type, first, n);
2503 /* Drop all constraints in bmap that involve any of the dimensions
2504 * first to first + n - 1 of the given type.
2506 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2507 __isl_take isl_basic_map *bmap,
2508 enum isl_dim_type type, unsigned first, unsigned n)
2510 if (!bmap)
2511 return NULL;
2512 if (n == 0)
2513 return bmap;
2515 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2516 return isl_basic_map_free(bmap);
2518 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2519 first += isl_basic_map_offset(bmap, type) - 1;
2520 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2523 /* Drop all constraints in bset that involve any of the dimensions
2524 * first to first + n - 1 of the given type.
2526 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2527 __isl_take isl_basic_set *bset,
2528 enum isl_dim_type type, unsigned first, unsigned n)
2530 return isl_basic_map_drop_constraints_involving_dims(bset,
2531 type, first, n);
2534 /* Drop constraints from "map" by applying "drop" to each basic map.
2536 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2537 enum isl_dim_type type, unsigned first, unsigned n,
2538 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2539 enum isl_dim_type type, unsigned first, unsigned n))
2541 int i;
2542 unsigned dim;
2544 if (!map)
2545 return NULL;
2547 dim = isl_map_dim(map, type);
2548 if (first + n > dim || first + n < first)
2549 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2550 "index out of bounds", return isl_map_free(map));
2552 map = isl_map_cow(map);
2553 if (!map)
2554 return NULL;
2556 for (i = 0; i < map->n; ++i) {
2557 map->p[i] = drop(map->p[i], type, first, n);
2558 if (!map->p[i])
2559 return isl_map_free(map);
2562 if (map->n > 1)
2563 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2565 return map;
2568 /* Drop all constraints in map that involve any of the dimensions
2569 * first to first + n - 1 of the given type.
2571 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2572 __isl_take isl_map *map,
2573 enum isl_dim_type type, unsigned first, unsigned n)
2575 if (n == 0)
2576 return map;
2577 return drop_constraints(map, type, first, n,
2578 &isl_basic_map_drop_constraints_involving_dims);
2581 /* Drop all constraints in "map" that do not involve any of the dimensions
2582 * first to first + n - 1 of the given type.
2584 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2585 __isl_take isl_map *map,
2586 enum isl_dim_type type, unsigned first, unsigned n)
2588 if (n == 0) {
2589 isl_space *space = isl_map_get_space(map);
2590 isl_map_free(map);
2591 return isl_map_universe(space);
2593 return drop_constraints(map, type, first, n,
2594 &isl_basic_map_drop_constraints_not_involving_dims);
2597 /* Drop all constraints in set that involve any of the dimensions
2598 * first to first + n - 1 of the given type.
2600 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2601 __isl_take isl_set *set,
2602 enum isl_dim_type type, unsigned first, unsigned n)
2604 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2607 /* Drop all constraints in "set" that do not involve any of the dimensions
2608 * first to first + n - 1 of the given type.
2610 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2611 __isl_take isl_set *set,
2612 enum isl_dim_type type, unsigned first, unsigned n)
2614 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2617 /* Does local variable "div" of "bmap" have a complete explicit representation?
2618 * Having a complete explicit representation requires not only
2619 * an explicit representation, but also that all local variables
2620 * that appear in this explicit representation in turn have
2621 * a complete explicit representation.
2623 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2625 int i;
2626 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2627 isl_bool marked;
2629 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2630 if (marked < 0 || marked)
2631 return isl_bool_not(marked);
2633 for (i = bmap->n_div - 1; i >= 0; --i) {
2634 isl_bool known;
2636 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2637 continue;
2638 known = isl_basic_map_div_is_known(bmap, i);
2639 if (known < 0 || !known)
2640 return known;
2643 return isl_bool_true;
2646 /* Does local variable "div" of "bset" have a complete explicit representation?
2648 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2650 return isl_basic_map_div_is_known(bset, div);
2653 /* Remove all divs that are unknown or defined in terms of unknown divs.
2655 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2656 __isl_take isl_basic_map *bmap)
2658 int i;
2660 if (!bmap)
2661 return NULL;
2663 for (i = bmap->n_div - 1; i >= 0; --i) {
2664 if (isl_basic_map_div_is_known(bmap, i))
2665 continue;
2666 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2667 if (!bmap)
2668 return NULL;
2669 i = bmap->n_div;
2672 return bmap;
2675 /* Remove all divs that are unknown or defined in terms of unknown divs.
2677 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2678 __isl_take isl_basic_set *bset)
2680 return isl_basic_map_remove_unknown_divs(bset);
2683 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2685 int i;
2687 if (!map)
2688 return NULL;
2689 if (map->n == 0)
2690 return map;
2692 map = isl_map_cow(map);
2693 if (!map)
2694 return NULL;
2696 for (i = 0; i < map->n; ++i) {
2697 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2698 if (!map->p[i])
2699 goto error;
2701 return map;
2702 error:
2703 isl_map_free(map);
2704 return NULL;
2707 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2709 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2712 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2713 __isl_take isl_basic_set *bset,
2714 enum isl_dim_type type, unsigned first, unsigned n)
2716 isl_basic_map *bmap = bset_to_bmap(bset);
2717 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2718 return bset_from_bmap(bmap);
2721 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2722 enum isl_dim_type type, unsigned first, unsigned n)
2724 int i;
2726 if (n == 0)
2727 return map;
2729 map = isl_map_cow(map);
2730 if (!map)
2731 return NULL;
2732 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2734 for (i = 0; i < map->n; ++i) {
2735 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2736 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2737 if (!map->p[i])
2738 goto error;
2740 map = isl_map_drop(map, type, first, n);
2741 return map;
2742 error:
2743 isl_map_free(map);
2744 return NULL;
2747 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2748 enum isl_dim_type type, unsigned first, unsigned n)
2750 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2751 type, first, n));
2754 /* Project out n inputs starting at first using Fourier-Motzkin */
2755 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2756 unsigned first, unsigned n)
2758 return isl_map_remove_dims(map, isl_dim_in, first, n);
2761 static void dump_term(struct isl_basic_map *bmap,
2762 isl_int c, int pos, FILE *out)
2764 const char *name;
2765 unsigned in = isl_basic_map_n_in(bmap);
2766 unsigned dim = in + isl_basic_map_n_out(bmap);
2767 unsigned nparam = isl_basic_map_n_param(bmap);
2768 if (!pos)
2769 isl_int_print(out, c, 0);
2770 else {
2771 if (!isl_int_is_one(c))
2772 isl_int_print(out, c, 0);
2773 if (pos < 1 + nparam) {
2774 name = isl_space_get_dim_name(bmap->dim,
2775 isl_dim_param, pos - 1);
2776 if (name)
2777 fprintf(out, "%s", name);
2778 else
2779 fprintf(out, "p%d", pos - 1);
2780 } else if (pos < 1 + nparam + in)
2781 fprintf(out, "i%d", pos - 1 - nparam);
2782 else if (pos < 1 + nparam + dim)
2783 fprintf(out, "o%d", pos - 1 - nparam - in);
2784 else
2785 fprintf(out, "e%d", pos - 1 - nparam - dim);
2789 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2790 int sign, FILE *out)
2792 int i;
2793 int first;
2794 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2795 isl_int v;
2797 isl_int_init(v);
2798 for (i = 0, first = 1; i < len; ++i) {
2799 if (isl_int_sgn(c[i]) * sign <= 0)
2800 continue;
2801 if (!first)
2802 fprintf(out, " + ");
2803 first = 0;
2804 isl_int_abs(v, c[i]);
2805 dump_term(bmap, v, i, out);
2807 isl_int_clear(v);
2808 if (first)
2809 fprintf(out, "0");
2812 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2813 const char *op, FILE *out, int indent)
2815 int i;
2817 fprintf(out, "%*s", indent, "");
2819 dump_constraint_sign(bmap, c, 1, out);
2820 fprintf(out, " %s ", op);
2821 dump_constraint_sign(bmap, c, -1, out);
2823 fprintf(out, "\n");
2825 for (i = bmap->n_div; i < bmap->extra; ++i) {
2826 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2827 continue;
2828 fprintf(out, "%*s", indent, "");
2829 fprintf(out, "ERROR: unused div coefficient not zero\n");
2830 abort();
2834 static void dump_constraints(struct isl_basic_map *bmap,
2835 isl_int **c, unsigned n,
2836 const char *op, FILE *out, int indent)
2838 int i;
2840 for (i = 0; i < n; ++i)
2841 dump_constraint(bmap, c[i], op, out, indent);
2844 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2846 int j;
2847 int first = 1;
2848 unsigned total = isl_basic_map_total_dim(bmap);
2850 for (j = 0; j < 1 + total; ++j) {
2851 if (isl_int_is_zero(exp[j]))
2852 continue;
2853 if (!first && isl_int_is_pos(exp[j]))
2854 fprintf(out, "+");
2855 dump_term(bmap, exp[j], j, out);
2856 first = 0;
2860 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2862 int i;
2864 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2865 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2867 for (i = 0; i < bmap->n_div; ++i) {
2868 fprintf(out, "%*s", indent, "");
2869 fprintf(out, "e%d = [(", i);
2870 dump_affine(bmap, bmap->div[i]+1, out);
2871 fprintf(out, ")/");
2872 isl_int_print(out, bmap->div[i][0], 0);
2873 fprintf(out, "]\n");
2877 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2878 FILE *out, int indent)
2880 if (!bset) {
2881 fprintf(out, "null basic set\n");
2882 return;
2885 fprintf(out, "%*s", indent, "");
2886 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2887 bset->ref, bset->dim->nparam, bset->dim->n_out,
2888 bset->extra, bset->flags);
2889 dump(bset_to_bmap(bset), out, indent);
2892 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2893 FILE *out, int indent)
2895 if (!bmap) {
2896 fprintf(out, "null basic map\n");
2897 return;
2900 fprintf(out, "%*s", indent, "");
2901 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2902 "flags: %x, n_name: %d\n",
2903 bmap->ref,
2904 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2905 bmap->extra, bmap->flags, bmap->dim->n_id);
2906 dump(bmap, out, indent);
2909 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2911 unsigned total;
2912 if (!bmap)
2913 return -1;
2914 total = isl_basic_map_total_dim(bmap);
2915 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2916 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2917 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2918 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2919 return 0;
2922 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2923 unsigned flags)
2925 if (!space)
2926 return NULL;
2927 if (isl_space_dim(space, isl_dim_in) != 0)
2928 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2929 "set cannot have input dimensions", goto error);
2930 return isl_map_alloc_space(space, n, flags);
2931 error:
2932 isl_space_free(space);
2933 return NULL;
2936 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2937 unsigned nparam, unsigned dim, int n, unsigned flags)
2939 struct isl_set *set;
2940 isl_space *dims;
2942 dims = isl_space_alloc(ctx, nparam, 0, dim);
2943 if (!dims)
2944 return NULL;
2946 set = isl_set_alloc_space(dims, n, flags);
2947 return set;
2950 /* Make sure "map" has room for at least "n" more basic maps.
2952 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2954 int i;
2955 struct isl_map *grown = NULL;
2957 if (!map)
2958 return NULL;
2959 isl_assert(map->ctx, n >= 0, goto error);
2960 if (map->n + n <= map->size)
2961 return map;
2962 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2963 if (!grown)
2964 goto error;
2965 for (i = 0; i < map->n; ++i) {
2966 grown->p[i] = isl_basic_map_copy(map->p[i]);
2967 if (!grown->p[i])
2968 goto error;
2969 grown->n++;
2971 isl_map_free(map);
2972 return grown;
2973 error:
2974 isl_map_free(grown);
2975 isl_map_free(map);
2976 return NULL;
2979 /* Make sure "set" has room for at least "n" more basic sets.
2981 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2983 return set_from_map(isl_map_grow(set_to_map(set), n));
2986 struct isl_set *isl_set_dup(struct isl_set *set)
2988 int i;
2989 struct isl_set *dup;
2991 if (!set)
2992 return NULL;
2994 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2995 if (!dup)
2996 return NULL;
2997 for (i = 0; i < set->n; ++i)
2998 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2999 return dup;
3002 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
3004 return isl_map_from_basic_map(bset);
3007 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
3009 struct isl_map *map;
3011 if (!bmap)
3012 return NULL;
3014 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3015 return isl_map_add_basic_map(map, bmap);
3018 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3019 __isl_take isl_basic_set *bset)
3021 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3022 bset_to_bmap(bset)));
3025 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3027 return isl_map_free(set);
3030 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3032 int i;
3034 if (!set) {
3035 fprintf(out, "null set\n");
3036 return;
3039 fprintf(out, "%*s", indent, "");
3040 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3041 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3042 set->flags);
3043 for (i = 0; i < set->n; ++i) {
3044 fprintf(out, "%*s", indent, "");
3045 fprintf(out, "basic set %d:\n", i);
3046 isl_basic_set_print_internal(set->p[i], out, indent+4);
3050 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3052 int i;
3054 if (!map) {
3055 fprintf(out, "null map\n");
3056 return;
3059 fprintf(out, "%*s", indent, "");
3060 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3061 "flags: %x, n_name: %d\n",
3062 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3063 map->dim->n_out, map->flags, map->dim->n_id);
3064 for (i = 0; i < map->n; ++i) {
3065 fprintf(out, "%*s", indent, "");
3066 fprintf(out, "basic map %d:\n", i);
3067 isl_basic_map_print_internal(map->p[i], out, indent+4);
3071 struct isl_basic_map *isl_basic_map_intersect_domain(
3072 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3074 struct isl_basic_map *bmap_domain;
3076 if (!bmap || !bset)
3077 goto error;
3079 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3080 bset->dim, isl_dim_param), goto error);
3082 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3083 isl_assert(bset->ctx,
3084 isl_basic_map_compatible_domain(bmap, bset), goto error);
3086 bmap = isl_basic_map_cow(bmap);
3087 if (!bmap)
3088 goto error;
3089 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3090 bset->n_div, bset->n_eq, bset->n_ineq);
3091 bmap_domain = isl_basic_map_from_domain(bset);
3092 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3094 bmap = isl_basic_map_simplify(bmap);
3095 return isl_basic_map_finalize(bmap);
3096 error:
3097 isl_basic_map_free(bmap);
3098 isl_basic_set_free(bset);
3099 return NULL;
3102 struct isl_basic_map *isl_basic_map_intersect_range(
3103 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3105 struct isl_basic_map *bmap_range;
3107 if (!bmap || !bset)
3108 goto error;
3110 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3111 bset->dim, isl_dim_param), goto error);
3113 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3114 isl_assert(bset->ctx,
3115 isl_basic_map_compatible_range(bmap, bset), goto error);
3117 if (isl_basic_set_plain_is_universe(bset)) {
3118 isl_basic_set_free(bset);
3119 return bmap;
3122 bmap = isl_basic_map_cow(bmap);
3123 if (!bmap)
3124 goto error;
3125 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3126 bset->n_div, bset->n_eq, bset->n_ineq);
3127 bmap_range = bset_to_bmap(bset);
3128 bmap = add_constraints(bmap, bmap_range, 0, 0);
3130 bmap = isl_basic_map_simplify(bmap);
3131 return isl_basic_map_finalize(bmap);
3132 error:
3133 isl_basic_map_free(bmap);
3134 isl_basic_set_free(bset);
3135 return NULL;
3138 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3139 __isl_keep isl_vec *vec)
3141 int i;
3142 unsigned total;
3143 isl_int s;
3145 if (!bmap || !vec)
3146 return isl_bool_error;
3148 total = 1 + isl_basic_map_total_dim(bmap);
3149 if (total != vec->size)
3150 return isl_bool_error;
3152 isl_int_init(s);
3154 for (i = 0; i < bmap->n_eq; ++i) {
3155 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3156 if (!isl_int_is_zero(s)) {
3157 isl_int_clear(s);
3158 return isl_bool_false;
3162 for (i = 0; i < bmap->n_ineq; ++i) {
3163 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3164 if (isl_int_is_neg(s)) {
3165 isl_int_clear(s);
3166 return isl_bool_false;
3170 isl_int_clear(s);
3172 return isl_bool_true;
3175 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3176 __isl_keep isl_vec *vec)
3178 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3181 struct isl_basic_map *isl_basic_map_intersect(
3182 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3184 struct isl_vec *sample = NULL;
3186 if (!bmap1 || !bmap2)
3187 goto error;
3189 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
3190 bmap2->dim, isl_dim_param), goto error);
3191 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3192 isl_space_dim(bmap1->dim, isl_dim_param) &&
3193 isl_space_dim(bmap2->dim, isl_dim_all) !=
3194 isl_space_dim(bmap2->dim, isl_dim_param))
3195 return isl_basic_map_intersect(bmap2, bmap1);
3197 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3198 isl_space_dim(bmap2->dim, isl_dim_param))
3199 isl_assert(bmap1->ctx,
3200 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3202 if (isl_basic_map_plain_is_empty(bmap1)) {
3203 isl_basic_map_free(bmap2);
3204 return bmap1;
3206 if (isl_basic_map_plain_is_empty(bmap2)) {
3207 isl_basic_map_free(bmap1);
3208 return bmap2;
3211 if (bmap1->sample &&
3212 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3213 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3214 sample = isl_vec_copy(bmap1->sample);
3215 else if (bmap2->sample &&
3216 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3217 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3218 sample = isl_vec_copy(bmap2->sample);
3220 bmap1 = isl_basic_map_cow(bmap1);
3221 if (!bmap1)
3222 goto error;
3223 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3224 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3225 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3227 if (!bmap1)
3228 isl_vec_free(sample);
3229 else if (sample) {
3230 isl_vec_free(bmap1->sample);
3231 bmap1->sample = sample;
3234 bmap1 = isl_basic_map_simplify(bmap1);
3235 return isl_basic_map_finalize(bmap1);
3236 error:
3237 if (sample)
3238 isl_vec_free(sample);
3239 isl_basic_map_free(bmap1);
3240 isl_basic_map_free(bmap2);
3241 return NULL;
3244 struct isl_basic_set *isl_basic_set_intersect(
3245 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3247 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3248 bset_to_bmap(bset2)));
3251 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3252 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3254 return isl_basic_set_intersect(bset1, bset2);
3257 /* Special case of isl_map_intersect, where both map1 and map2
3258 * are convex, without any divs and such that either map1 or map2
3259 * contains a single constraint. This constraint is then simply
3260 * added to the other map.
3262 static __isl_give isl_map *map_intersect_add_constraint(
3263 __isl_take isl_map *map1, __isl_take isl_map *map2)
3265 isl_assert(map1->ctx, map1->n == 1, goto error);
3266 isl_assert(map2->ctx, map1->n == 1, goto error);
3267 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3268 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3270 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3271 return isl_map_intersect(map2, map1);
3273 isl_assert(map2->ctx,
3274 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3276 map1 = isl_map_cow(map1);
3277 if (!map1)
3278 goto error;
3279 if (isl_map_plain_is_empty(map1)) {
3280 isl_map_free(map2);
3281 return map1;
3283 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3284 if (map2->p[0]->n_eq == 1)
3285 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3286 else
3287 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3288 map2->p[0]->ineq[0]);
3290 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3291 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3292 if (!map1->p[0])
3293 goto error;
3295 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3296 isl_basic_map_free(map1->p[0]);
3297 map1->n = 0;
3300 isl_map_free(map2);
3302 return map1;
3303 error:
3304 isl_map_free(map1);
3305 isl_map_free(map2);
3306 return NULL;
3309 /* map2 may be either a parameter domain or a map living in the same
3310 * space as map1.
3312 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3313 __isl_take isl_map *map2)
3315 unsigned flags = 0;
3316 isl_map *result;
3317 int i, j;
3319 if (!map1 || !map2)
3320 goto error;
3322 if ((isl_map_plain_is_empty(map1) ||
3323 isl_map_plain_is_universe(map2)) &&
3324 isl_space_is_equal(map1->dim, map2->dim)) {
3325 isl_map_free(map2);
3326 return map1;
3328 if ((isl_map_plain_is_empty(map2) ||
3329 isl_map_plain_is_universe(map1)) &&
3330 isl_space_is_equal(map1->dim, map2->dim)) {
3331 isl_map_free(map1);
3332 return map2;
3335 if (map1->n == 1 && map2->n == 1 &&
3336 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3337 isl_space_is_equal(map1->dim, map2->dim) &&
3338 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3339 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3340 return map_intersect_add_constraint(map1, map2);
3342 if (isl_space_dim(map2->dim, isl_dim_all) !=
3343 isl_space_dim(map2->dim, isl_dim_param))
3344 isl_assert(map1->ctx,
3345 isl_space_is_equal(map1->dim, map2->dim), goto error);
3347 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3348 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3349 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3351 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3352 map1->n * map2->n, flags);
3353 if (!result)
3354 goto error;
3355 for (i = 0; i < map1->n; ++i)
3356 for (j = 0; j < map2->n; ++j) {
3357 struct isl_basic_map *part;
3358 part = isl_basic_map_intersect(
3359 isl_basic_map_copy(map1->p[i]),
3360 isl_basic_map_copy(map2->p[j]));
3361 if (isl_basic_map_is_empty(part) < 0)
3362 part = isl_basic_map_free(part);
3363 result = isl_map_add_basic_map(result, part);
3364 if (!result)
3365 goto error;
3367 isl_map_free(map1);
3368 isl_map_free(map2);
3369 return result;
3370 error:
3371 isl_map_free(map1);
3372 isl_map_free(map2);
3373 return NULL;
3376 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3377 __isl_take isl_map *map2)
3379 if (!map1 || !map2)
3380 goto error;
3381 if (!isl_space_is_equal(map1->dim, map2->dim))
3382 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3383 "spaces don't match", goto error);
3384 return map_intersect_internal(map1, map2);
3385 error:
3386 isl_map_free(map1);
3387 isl_map_free(map2);
3388 return NULL;
3391 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3392 __isl_take isl_map *map2)
3394 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3397 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3399 return set_from_map(isl_map_intersect(set_to_map(set1),
3400 set_to_map(set2)));
3403 /* map_intersect_internal accepts intersections
3404 * with parameter domains, so we can just call that function.
3406 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3407 __isl_take isl_set *params)
3409 return map_intersect_internal(map, params);
3412 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3413 __isl_take isl_map *map2)
3415 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3418 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3419 __isl_take isl_set *params)
3421 return isl_map_intersect_params(set, params);
3424 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3426 isl_space *space;
3427 unsigned pos, n1, n2;
3429 if (!bmap)
3430 return NULL;
3431 bmap = isl_basic_map_cow(bmap);
3432 if (!bmap)
3433 return NULL;
3434 space = isl_space_reverse(isl_space_copy(bmap->dim));
3435 pos = isl_basic_map_offset(bmap, isl_dim_in);
3436 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3437 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3438 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3439 return isl_basic_map_reset_space(bmap, space);
3442 static __isl_give isl_basic_map *basic_map_space_reset(
3443 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3445 isl_space *space;
3447 if (!bmap)
3448 return NULL;
3449 if (!isl_space_is_named_or_nested(bmap->dim, type))
3450 return bmap;
3452 space = isl_basic_map_get_space(bmap);
3453 space = isl_space_reset(space, type);
3454 bmap = isl_basic_map_reset_space(bmap, space);
3455 return bmap;
3458 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3459 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3460 unsigned pos, unsigned n)
3462 isl_space *res_dim;
3463 struct isl_basic_map *res;
3464 struct isl_dim_map *dim_map;
3465 unsigned total, off;
3466 enum isl_dim_type t;
3468 if (n == 0)
3469 return basic_map_space_reset(bmap, type);
3471 if (!bmap)
3472 return NULL;
3474 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3476 total = isl_basic_map_total_dim(bmap) + n;
3477 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3478 off = 0;
3479 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3480 if (t != type) {
3481 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3482 } else {
3483 unsigned size = isl_basic_map_dim(bmap, t);
3484 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3485 0, pos, off);
3486 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3487 pos, size - pos, off + pos + n);
3489 off += isl_space_dim(res_dim, t);
3491 isl_dim_map_div(dim_map, bmap, off);
3493 res = isl_basic_map_alloc_space(res_dim,
3494 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3495 if (isl_basic_map_is_rational(bmap))
3496 res = isl_basic_map_set_rational(res);
3497 if (isl_basic_map_plain_is_empty(bmap)) {
3498 isl_basic_map_free(bmap);
3499 free(dim_map);
3500 return isl_basic_map_set_to_empty(res);
3502 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3503 return isl_basic_map_finalize(res);
3506 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3507 __isl_take isl_basic_set *bset,
3508 enum isl_dim_type type, unsigned pos, unsigned n)
3510 return isl_basic_map_insert_dims(bset, type, pos, n);
3513 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3514 enum isl_dim_type type, unsigned n)
3516 if (!bmap)
3517 return NULL;
3518 return isl_basic_map_insert_dims(bmap, type,
3519 isl_basic_map_dim(bmap, type), n);
3522 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3523 enum isl_dim_type type, unsigned n)
3525 if (!bset)
3526 return NULL;
3527 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3528 return isl_basic_map_add_dims(bset, type, n);
3529 error:
3530 isl_basic_set_free(bset);
3531 return NULL;
3534 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3535 enum isl_dim_type type)
3537 isl_space *space;
3539 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3540 return map;
3542 space = isl_map_get_space(map);
3543 space = isl_space_reset(space, type);
3544 map = isl_map_reset_space(map, space);
3545 return map;
3548 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3549 enum isl_dim_type type, unsigned pos, unsigned n)
3551 int i;
3553 if (n == 0)
3554 return map_space_reset(map, type);
3556 map = isl_map_cow(map);
3557 if (!map)
3558 return NULL;
3560 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3561 if (!map->dim)
3562 goto error;
3564 for (i = 0; i < map->n; ++i) {
3565 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3566 if (!map->p[i])
3567 goto error;
3570 return map;
3571 error:
3572 isl_map_free(map);
3573 return NULL;
3576 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3577 enum isl_dim_type type, unsigned pos, unsigned n)
3579 return isl_map_insert_dims(set, type, pos, n);
3582 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3583 enum isl_dim_type type, unsigned n)
3585 if (!map)
3586 return NULL;
3587 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3590 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3591 enum isl_dim_type type, unsigned n)
3593 if (!set)
3594 return NULL;
3595 isl_assert(set->ctx, type != isl_dim_in, goto error);
3596 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3597 error:
3598 isl_set_free(set);
3599 return NULL;
3602 __isl_give isl_basic_map *isl_basic_map_move_dims(
3603 __isl_take isl_basic_map *bmap,
3604 enum isl_dim_type dst_type, unsigned dst_pos,
3605 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3607 struct isl_dim_map *dim_map;
3608 struct isl_basic_map *res;
3609 enum isl_dim_type t;
3610 unsigned total, off;
3612 if (!bmap)
3613 return NULL;
3614 if (n == 0)
3615 return bmap;
3617 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3618 return isl_basic_map_free(bmap);
3620 if (dst_type == src_type && dst_pos == src_pos)
3621 return bmap;
3623 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3625 if (pos(bmap->dim, dst_type) + dst_pos ==
3626 pos(bmap->dim, src_type) + src_pos +
3627 ((src_type < dst_type) ? n : 0)) {
3628 bmap = isl_basic_map_cow(bmap);
3629 if (!bmap)
3630 return NULL;
3632 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3633 src_type, src_pos, n);
3634 if (!bmap->dim)
3635 goto error;
3637 bmap = isl_basic_map_finalize(bmap);
3639 return bmap;
3642 total = isl_basic_map_total_dim(bmap);
3643 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3645 off = 0;
3646 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3647 unsigned size = isl_space_dim(bmap->dim, t);
3648 if (t == dst_type) {
3649 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3650 0, dst_pos, off);
3651 off += dst_pos;
3652 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3653 src_pos, n, off);
3654 off += n;
3655 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3656 dst_pos, size - dst_pos, off);
3657 off += size - dst_pos;
3658 } else if (t == src_type) {
3659 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3660 0, src_pos, off);
3661 off += src_pos;
3662 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3663 src_pos + n, size - src_pos - n, off);
3664 off += size - src_pos - n;
3665 } else {
3666 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3667 off += size;
3670 isl_dim_map_div(dim_map, bmap, off);
3672 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3673 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3674 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3675 if (!bmap)
3676 goto error;
3678 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3679 src_type, src_pos, n);
3680 if (!bmap->dim)
3681 goto error;
3683 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3684 bmap = isl_basic_map_gauss(bmap, NULL);
3685 bmap = isl_basic_map_finalize(bmap);
3687 return bmap;
3688 error:
3689 isl_basic_map_free(bmap);
3690 return NULL;
3693 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3694 enum isl_dim_type dst_type, unsigned dst_pos,
3695 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3697 isl_basic_map *bmap = bset_to_bmap(bset);
3698 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3699 src_type, src_pos, n);
3700 return bset_from_bmap(bmap);
3703 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3704 enum isl_dim_type dst_type, unsigned dst_pos,
3705 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3707 if (!set)
3708 return NULL;
3709 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3710 return set_from_map(isl_map_move_dims(set_to_map(set),
3711 dst_type, dst_pos, src_type, src_pos, n));
3712 error:
3713 isl_set_free(set);
3714 return NULL;
3717 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3718 enum isl_dim_type dst_type, unsigned dst_pos,
3719 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3721 int i;
3723 if (!map)
3724 return NULL;
3725 if (n == 0)
3726 return map;
3728 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3729 goto error);
3731 if (dst_type == src_type && dst_pos == src_pos)
3732 return map;
3734 isl_assert(map->ctx, dst_type != src_type, goto error);
3736 map = isl_map_cow(map);
3737 if (!map)
3738 return NULL;
3740 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3741 if (!map->dim)
3742 goto error;
3744 for (i = 0; i < map->n; ++i) {
3745 map->p[i] = isl_basic_map_move_dims(map->p[i],
3746 dst_type, dst_pos,
3747 src_type, src_pos, n);
3748 if (!map->p[i])
3749 goto error;
3752 return map;
3753 error:
3754 isl_map_free(map);
3755 return NULL;
3758 /* Move the specified dimensions to the last columns right before
3759 * the divs. Don't change the dimension specification of bmap.
3760 * That's the responsibility of the caller.
3762 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3763 enum isl_dim_type type, unsigned first, unsigned n)
3765 struct isl_dim_map *dim_map;
3766 struct isl_basic_map *res;
3767 enum isl_dim_type t;
3768 unsigned total, off;
3770 if (!bmap)
3771 return NULL;
3772 if (pos(bmap->dim, type) + first + n ==
3773 1 + isl_space_dim(bmap->dim, isl_dim_all))
3774 return bmap;
3776 total = isl_basic_map_total_dim(bmap);
3777 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3779 off = 0;
3780 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3781 unsigned size = isl_space_dim(bmap->dim, t);
3782 if (t == type) {
3783 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3784 0, first, off);
3785 off += first;
3786 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3787 first, n, total - bmap->n_div - n);
3788 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3789 first + n, size - (first + n), off);
3790 off += size - (first + n);
3791 } else {
3792 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3793 off += size;
3796 isl_dim_map_div(dim_map, bmap, off + n);
3798 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3799 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3800 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3801 return res;
3804 /* Insert "n" rows in the divs of "bmap".
3806 * The number of columns is not changed, which means that the last
3807 * dimensions of "bmap" are being reintepreted as the new divs.
3808 * The space of "bmap" is not adjusted, however, which means
3809 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3810 * from the space of "bmap" is the responsibility of the caller.
3812 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3813 int n)
3815 int i;
3816 size_t row_size;
3817 isl_int **new_div;
3818 isl_int *old;
3820 bmap = isl_basic_map_cow(bmap);
3821 if (!bmap)
3822 return NULL;
3824 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3825 old = bmap->block2.data;
3826 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3827 (bmap->extra + n) * (1 + row_size));
3828 if (!bmap->block2.data)
3829 return isl_basic_map_free(bmap);
3830 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3831 if (!new_div)
3832 return isl_basic_map_free(bmap);
3833 for (i = 0; i < n; ++i) {
3834 new_div[i] = bmap->block2.data +
3835 (bmap->extra + i) * (1 + row_size);
3836 isl_seq_clr(new_div[i], 1 + row_size);
3838 for (i = 0; i < bmap->extra; ++i)
3839 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3840 free(bmap->div);
3841 bmap->div = new_div;
3842 bmap->n_div += n;
3843 bmap->extra += n;
3845 return bmap;
3848 /* Drop constraints from "bmap" that only involve the variables
3849 * of "type" in the range [first, first + n] that are not related
3850 * to any of the variables outside that interval.
3851 * These constraints cannot influence the values for the variables
3852 * outside the interval, except in case they cause "bmap" to be empty.
3853 * Only drop the constraints if "bmap" is known to be non-empty.
3855 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3856 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3857 unsigned first, unsigned n)
3859 int i;
3860 int *groups;
3861 unsigned dim, n_div;
3862 isl_bool non_empty;
3864 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3865 if (non_empty < 0)
3866 return isl_basic_map_free(bmap);
3867 if (!non_empty)
3868 return bmap;
3870 dim = isl_basic_map_dim(bmap, isl_dim_all);
3871 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3872 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3873 if (!groups)
3874 return isl_basic_map_free(bmap);
3875 first += isl_basic_map_offset(bmap, type) - 1;
3876 for (i = 0; i < first; ++i)
3877 groups[i] = -1;
3878 for (i = first + n; i < dim - n_div; ++i)
3879 groups[i] = -1;
3881 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3883 return bmap;
3886 /* Turn the n dimensions of type type, starting at first
3887 * into existentially quantified variables.
3889 * If a subset of the projected out variables are unrelated
3890 * to any of the variables that remain, then the constraints
3891 * involving this subset are simply dropped first.
3893 __isl_give isl_basic_map *isl_basic_map_project_out(
3894 __isl_take isl_basic_map *bmap,
3895 enum isl_dim_type type, unsigned first, unsigned n)
3897 if (n == 0)
3898 return basic_map_space_reset(bmap, type);
3899 if (type == isl_dim_div)
3900 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3901 "cannot project out existentially quantified variables",
3902 return isl_basic_map_free(bmap));
3904 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3905 if (!bmap)
3906 return NULL;
3908 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3909 return isl_basic_map_remove_dims(bmap, type, first, n);
3911 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3912 return isl_basic_map_free(bmap);
3914 bmap = move_last(bmap, type, first, n);
3915 bmap = isl_basic_map_cow(bmap);
3916 bmap = insert_div_rows(bmap, n);
3917 if (!bmap)
3918 return NULL;
3920 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3921 if (!bmap->dim)
3922 goto error;
3923 bmap = isl_basic_map_simplify(bmap);
3924 bmap = isl_basic_map_drop_redundant_divs(bmap);
3925 return isl_basic_map_finalize(bmap);
3926 error:
3927 isl_basic_map_free(bmap);
3928 return NULL;
3931 /* Turn the n dimensions of type type, starting at first
3932 * into existentially quantified variables.
3934 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3935 enum isl_dim_type type, unsigned first, unsigned n)
3937 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3938 type, first, n));
3941 /* Turn the n dimensions of type type, starting at first
3942 * into existentially quantified variables.
3944 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3945 enum isl_dim_type type, unsigned first, unsigned n)
3947 int i;
3949 if (!map)
3950 return NULL;
3952 if (n == 0)
3953 return map_space_reset(map, type);
3955 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3957 map = isl_map_cow(map);
3958 if (!map)
3959 return NULL;
3961 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3962 if (!map->dim)
3963 goto error;
3965 for (i = 0; i < map->n; ++i) {
3966 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3967 if (!map->p[i])
3968 goto error;
3971 return map;
3972 error:
3973 isl_map_free(map);
3974 return NULL;
3977 /* Turn the n dimensions of type type, starting at first
3978 * into existentially quantified variables.
3980 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3981 enum isl_dim_type type, unsigned first, unsigned n)
3983 return set_from_map(isl_map_project_out(set_to_map(set),
3984 type, first, n));
3987 /* Return a map that projects the elements in "set" onto their
3988 * "n" set dimensions starting at "first".
3989 * "type" should be equal to isl_dim_set.
3991 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3992 enum isl_dim_type type, unsigned first, unsigned n)
3994 int i;
3995 int dim;
3996 isl_map *map;
3998 if (!set)
3999 return NULL;
4000 if (type != isl_dim_set)
4001 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4002 "only set dimensions can be projected out", goto error);
4003 dim = isl_set_dim(set, isl_dim_set);
4004 if (first + n > dim || first + n < first)
4005 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4006 "index out of bounds", goto error);
4008 map = isl_map_from_domain(set);
4009 map = isl_map_add_dims(map, isl_dim_out, n);
4010 for (i = 0; i < n; ++i)
4011 map = isl_map_equate(map, isl_dim_in, first + i,
4012 isl_dim_out, i);
4013 return map;
4014 error:
4015 isl_set_free(set);
4016 return NULL;
4019 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4021 int i, j;
4023 for (i = 0; i < n; ++i) {
4024 j = isl_basic_map_alloc_div(bmap);
4025 if (j < 0)
4026 goto error;
4027 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4029 return bmap;
4030 error:
4031 isl_basic_map_free(bmap);
4032 return NULL;
4035 struct isl_basic_map *isl_basic_map_apply_range(
4036 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4038 isl_space *dim_result = NULL;
4039 struct isl_basic_map *bmap;
4040 unsigned n_in, n_out, n, nparam, total, pos;
4041 struct isl_dim_map *dim_map1, *dim_map2;
4043 if (!bmap1 || !bmap2)
4044 goto error;
4045 if (!isl_space_match(bmap1->dim, isl_dim_param,
4046 bmap2->dim, isl_dim_param))
4047 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4048 "parameters don't match", goto error);
4049 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4050 bmap2->dim, isl_dim_in))
4051 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4052 "spaces don't match", goto error);
4054 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4055 isl_space_copy(bmap2->dim));
4057 n_in = isl_basic_map_n_in(bmap1);
4058 n_out = isl_basic_map_n_out(bmap2);
4059 n = isl_basic_map_n_out(bmap1);
4060 nparam = isl_basic_map_n_param(bmap1);
4062 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4063 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4064 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4065 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4066 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4067 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4068 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4069 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4070 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4071 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4072 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4074 bmap = isl_basic_map_alloc_space(dim_result,
4075 bmap1->n_div + bmap2->n_div + n,
4076 bmap1->n_eq + bmap2->n_eq,
4077 bmap1->n_ineq + bmap2->n_ineq);
4078 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4079 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4080 bmap = add_divs(bmap, n);
4081 bmap = isl_basic_map_simplify(bmap);
4082 bmap = isl_basic_map_drop_redundant_divs(bmap);
4083 return isl_basic_map_finalize(bmap);
4084 error:
4085 isl_basic_map_free(bmap1);
4086 isl_basic_map_free(bmap2);
4087 return NULL;
4090 struct isl_basic_set *isl_basic_set_apply(
4091 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4093 if (!bset || !bmap)
4094 goto error;
4096 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4097 goto error);
4099 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4100 bmap));
4101 error:
4102 isl_basic_set_free(bset);
4103 isl_basic_map_free(bmap);
4104 return NULL;
4107 struct isl_basic_map *isl_basic_map_apply_domain(
4108 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4110 if (!bmap1 || !bmap2)
4111 goto error;
4113 isl_assert(bmap1->ctx,
4114 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
4115 isl_assert(bmap1->ctx,
4116 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
4117 goto error);
4119 bmap1 = isl_basic_map_reverse(bmap1);
4120 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4121 return isl_basic_map_reverse(bmap1);
4122 error:
4123 isl_basic_map_free(bmap1);
4124 isl_basic_map_free(bmap2);
4125 return NULL;
4128 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4129 * A \cap B -> f(A) + f(B)
4131 struct isl_basic_map *isl_basic_map_sum(
4132 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4134 unsigned n_in, n_out, nparam, total, pos;
4135 struct isl_basic_map *bmap = NULL;
4136 struct isl_dim_map *dim_map1, *dim_map2;
4137 int i;
4139 if (!bmap1 || !bmap2)
4140 goto error;
4142 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4143 goto error);
4145 nparam = isl_basic_map_n_param(bmap1);
4146 n_in = isl_basic_map_n_in(bmap1);
4147 n_out = isl_basic_map_n_out(bmap1);
4149 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4150 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4151 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4152 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4153 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4154 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4155 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4156 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4157 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4158 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4159 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4161 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4162 bmap1->n_div + bmap2->n_div + 2 * n_out,
4163 bmap1->n_eq + bmap2->n_eq + n_out,
4164 bmap1->n_ineq + bmap2->n_ineq);
4165 for (i = 0; i < n_out; ++i) {
4166 int j = isl_basic_map_alloc_equality(bmap);
4167 if (j < 0)
4168 goto error;
4169 isl_seq_clr(bmap->eq[j], 1+total);
4170 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4171 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4172 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4174 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4175 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4176 bmap = add_divs(bmap, 2 * n_out);
4178 bmap = isl_basic_map_simplify(bmap);
4179 return isl_basic_map_finalize(bmap);
4180 error:
4181 isl_basic_map_free(bmap);
4182 isl_basic_map_free(bmap1);
4183 isl_basic_map_free(bmap2);
4184 return NULL;
4187 /* Given two maps A -> f(A) and B -> g(B), construct a map
4188 * A \cap B -> f(A) + f(B)
4190 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4192 struct isl_map *result;
4193 int i, j;
4195 if (!map1 || !map2)
4196 goto error;
4198 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4200 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4201 map1->n * map2->n, 0);
4202 if (!result)
4203 goto error;
4204 for (i = 0; i < map1->n; ++i)
4205 for (j = 0; j < map2->n; ++j) {
4206 struct isl_basic_map *part;
4207 part = isl_basic_map_sum(
4208 isl_basic_map_copy(map1->p[i]),
4209 isl_basic_map_copy(map2->p[j]));
4210 if (isl_basic_map_is_empty(part))
4211 isl_basic_map_free(part);
4212 else
4213 result = isl_map_add_basic_map(result, part);
4214 if (!result)
4215 goto error;
4217 isl_map_free(map1);
4218 isl_map_free(map2);
4219 return result;
4220 error:
4221 isl_map_free(map1);
4222 isl_map_free(map2);
4223 return NULL;
4226 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4227 __isl_take isl_set *set2)
4229 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4232 /* Given a basic map A -> f(A), construct A -> -f(A).
4234 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4236 int i, j;
4237 unsigned off, n;
4239 bmap = isl_basic_map_cow(bmap);
4240 if (!bmap)
4241 return NULL;
4243 n = isl_basic_map_dim(bmap, isl_dim_out);
4244 off = isl_basic_map_offset(bmap, isl_dim_out);
4245 for (i = 0; i < bmap->n_eq; ++i)
4246 for (j = 0; j < n; ++j)
4247 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4248 for (i = 0; i < bmap->n_ineq; ++i)
4249 for (j = 0; j < n; ++j)
4250 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4251 for (i = 0; i < bmap->n_div; ++i)
4252 for (j = 0; j < n; ++j)
4253 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4254 bmap = isl_basic_map_gauss(bmap, NULL);
4255 return isl_basic_map_finalize(bmap);
4258 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4260 return isl_basic_map_neg(bset);
4263 /* Given a map A -> f(A), construct A -> -f(A).
4265 struct isl_map *isl_map_neg(struct isl_map *map)
4267 int i;
4269 map = isl_map_cow(map);
4270 if (!map)
4271 return NULL;
4273 for (i = 0; i < map->n; ++i) {
4274 map->p[i] = isl_basic_map_neg(map->p[i]);
4275 if (!map->p[i])
4276 goto error;
4279 return map;
4280 error:
4281 isl_map_free(map);
4282 return NULL;
4285 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4287 return set_from_map(isl_map_neg(set_to_map(set)));
4290 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4291 * A -> floor(f(A)/d).
4293 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4294 isl_int d)
4296 unsigned n_in, n_out, nparam, total, pos;
4297 struct isl_basic_map *result = NULL;
4298 struct isl_dim_map *dim_map;
4299 int i;
4301 if (!bmap)
4302 return NULL;
4304 nparam = isl_basic_map_n_param(bmap);
4305 n_in = isl_basic_map_n_in(bmap);
4306 n_out = isl_basic_map_n_out(bmap);
4308 total = nparam + n_in + n_out + bmap->n_div + n_out;
4309 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4310 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4311 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4312 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4313 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4315 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4316 bmap->n_div + n_out,
4317 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4318 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4319 result = add_divs(result, n_out);
4320 for (i = 0; i < n_out; ++i) {
4321 int j;
4322 j = isl_basic_map_alloc_inequality(result);
4323 if (j < 0)
4324 goto error;
4325 isl_seq_clr(result->ineq[j], 1+total);
4326 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4327 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4328 j = isl_basic_map_alloc_inequality(result);
4329 if (j < 0)
4330 goto error;
4331 isl_seq_clr(result->ineq[j], 1+total);
4332 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4333 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4334 isl_int_sub_ui(result->ineq[j][0], d, 1);
4337 result = isl_basic_map_simplify(result);
4338 return isl_basic_map_finalize(result);
4339 error:
4340 isl_basic_map_free(result);
4341 return NULL;
4344 /* Given a map A -> f(A) and an integer d, construct a map
4345 * A -> floor(f(A)/d).
4347 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4349 int i;
4351 map = isl_map_cow(map);
4352 if (!map)
4353 return NULL;
4355 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4356 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4357 for (i = 0; i < map->n; ++i) {
4358 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4359 if (!map->p[i])
4360 goto error;
4363 return map;
4364 error:
4365 isl_map_free(map);
4366 return NULL;
4369 /* Given a map A -> f(A) and an integer d, construct a map
4370 * A -> floor(f(A)/d).
4372 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4373 __isl_take isl_val *d)
4375 if (!map || !d)
4376 goto error;
4377 if (!isl_val_is_int(d))
4378 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4379 "expecting integer denominator", goto error);
4380 map = isl_map_floordiv(map, d->n);
4381 isl_val_free(d);
4382 return map;
4383 error:
4384 isl_map_free(map);
4385 isl_val_free(d);
4386 return NULL;
4389 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4391 int i;
4392 unsigned nparam;
4393 unsigned n_in;
4395 i = isl_basic_map_alloc_equality(bmap);
4396 if (i < 0)
4397 goto error;
4398 nparam = isl_basic_map_n_param(bmap);
4399 n_in = isl_basic_map_n_in(bmap);
4400 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4401 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4402 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4403 return isl_basic_map_finalize(bmap);
4404 error:
4405 isl_basic_map_free(bmap);
4406 return NULL;
4409 /* Add a constraint to "bmap" expressing i_pos < o_pos
4411 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4413 int i;
4414 unsigned nparam;
4415 unsigned n_in;
4417 i = isl_basic_map_alloc_inequality(bmap);
4418 if (i < 0)
4419 goto error;
4420 nparam = isl_basic_map_n_param(bmap);
4421 n_in = isl_basic_map_n_in(bmap);
4422 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4423 isl_int_set_si(bmap->ineq[i][0], -1);
4424 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4425 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4426 return isl_basic_map_finalize(bmap);
4427 error:
4428 isl_basic_map_free(bmap);
4429 return NULL;
4432 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4434 static __isl_give isl_basic_map *var_less_or_equal(
4435 __isl_take isl_basic_map *bmap, unsigned pos)
4437 int i;
4438 unsigned nparam;
4439 unsigned n_in;
4441 i = isl_basic_map_alloc_inequality(bmap);
4442 if (i < 0)
4443 goto error;
4444 nparam = isl_basic_map_n_param(bmap);
4445 n_in = isl_basic_map_n_in(bmap);
4446 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4447 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4448 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4449 return isl_basic_map_finalize(bmap);
4450 error:
4451 isl_basic_map_free(bmap);
4452 return NULL;
4455 /* Add a constraint to "bmap" expressing i_pos > o_pos
4457 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4459 int i;
4460 unsigned nparam;
4461 unsigned n_in;
4463 i = isl_basic_map_alloc_inequality(bmap);
4464 if (i < 0)
4465 goto error;
4466 nparam = isl_basic_map_n_param(bmap);
4467 n_in = isl_basic_map_n_in(bmap);
4468 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4469 isl_int_set_si(bmap->ineq[i][0], -1);
4470 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4471 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4472 return isl_basic_map_finalize(bmap);
4473 error:
4474 isl_basic_map_free(bmap);
4475 return NULL;
4478 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4480 static __isl_give isl_basic_map *var_more_or_equal(
4481 __isl_take isl_basic_map *bmap, unsigned pos)
4483 int i;
4484 unsigned nparam;
4485 unsigned n_in;
4487 i = isl_basic_map_alloc_inequality(bmap);
4488 if (i < 0)
4489 goto error;
4490 nparam = isl_basic_map_n_param(bmap);
4491 n_in = isl_basic_map_n_in(bmap);
4492 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4493 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4494 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4495 return isl_basic_map_finalize(bmap);
4496 error:
4497 isl_basic_map_free(bmap);
4498 return NULL;
4501 __isl_give isl_basic_map *isl_basic_map_equal(
4502 __isl_take isl_space *dim, unsigned n_equal)
4504 int i;
4505 struct isl_basic_map *bmap;
4506 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4507 if (!bmap)
4508 return NULL;
4509 for (i = 0; i < n_equal && bmap; ++i)
4510 bmap = var_equal(bmap, i);
4511 return isl_basic_map_finalize(bmap);
4514 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4516 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4517 unsigned pos)
4519 int i;
4520 struct isl_basic_map *bmap;
4521 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4522 if (!bmap)
4523 return NULL;
4524 for (i = 0; i < pos && bmap; ++i)
4525 bmap = var_equal(bmap, i);
4526 if (bmap)
4527 bmap = var_less(bmap, pos);
4528 return isl_basic_map_finalize(bmap);
4531 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4533 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4534 __isl_take isl_space *dim, unsigned pos)
4536 int i;
4537 isl_basic_map *bmap;
4539 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4540 for (i = 0; i < pos; ++i)
4541 bmap = var_equal(bmap, i);
4542 bmap = var_less_or_equal(bmap, pos);
4543 return isl_basic_map_finalize(bmap);
4546 /* Return a relation on "dim" expressing i_pos > o_pos
4548 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4549 unsigned pos)
4551 int i;
4552 struct isl_basic_map *bmap;
4553 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4554 if (!bmap)
4555 return NULL;
4556 for (i = 0; i < pos && bmap; ++i)
4557 bmap = var_equal(bmap, i);
4558 if (bmap)
4559 bmap = var_more(bmap, pos);
4560 return isl_basic_map_finalize(bmap);
4563 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4565 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4566 __isl_take isl_space *dim, unsigned pos)
4568 int i;
4569 isl_basic_map *bmap;
4571 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4572 for (i = 0; i < pos; ++i)
4573 bmap = var_equal(bmap, i);
4574 bmap = var_more_or_equal(bmap, pos);
4575 return isl_basic_map_finalize(bmap);
4578 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4579 unsigned n, int equal)
4581 struct isl_map *map;
4582 int i;
4584 if (n == 0 && equal)
4585 return isl_map_universe(dims);
4587 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4589 for (i = 0; i + 1 < n; ++i)
4590 map = isl_map_add_basic_map(map,
4591 isl_basic_map_less_at(isl_space_copy(dims), i));
4592 if (n > 0) {
4593 if (equal)
4594 map = isl_map_add_basic_map(map,
4595 isl_basic_map_less_or_equal_at(dims, n - 1));
4596 else
4597 map = isl_map_add_basic_map(map,
4598 isl_basic_map_less_at(dims, n - 1));
4599 } else
4600 isl_space_free(dims);
4602 return map;
4605 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4607 if (!dims)
4608 return NULL;
4609 return map_lex_lte_first(dims, dims->n_out, equal);
4612 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4614 return map_lex_lte_first(dim, n, 0);
4617 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4619 return map_lex_lte_first(dim, n, 1);
4622 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4624 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4627 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4629 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4632 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4633 unsigned n, int equal)
4635 struct isl_map *map;
4636 int i;
4638 if (n == 0 && equal)
4639 return isl_map_universe(dims);
4641 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4643 for (i = 0; i + 1 < n; ++i)
4644 map = isl_map_add_basic_map(map,
4645 isl_basic_map_more_at(isl_space_copy(dims), i));
4646 if (n > 0) {
4647 if (equal)
4648 map = isl_map_add_basic_map(map,
4649 isl_basic_map_more_or_equal_at(dims, n - 1));
4650 else
4651 map = isl_map_add_basic_map(map,
4652 isl_basic_map_more_at(dims, n - 1));
4653 } else
4654 isl_space_free(dims);
4656 return map;
4659 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4661 if (!dims)
4662 return NULL;
4663 return map_lex_gte_first(dims, dims->n_out, equal);
4666 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4668 return map_lex_gte_first(dim, n, 0);
4671 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4673 return map_lex_gte_first(dim, n, 1);
4676 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4678 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4681 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4683 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4686 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4687 __isl_take isl_set *set2)
4689 isl_map *map;
4690 map = isl_map_lex_le(isl_set_get_space(set1));
4691 map = isl_map_intersect_domain(map, set1);
4692 map = isl_map_intersect_range(map, set2);
4693 return map;
4696 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4697 __isl_take isl_set *set2)
4699 isl_map *map;
4700 map = isl_map_lex_lt(isl_set_get_space(set1));
4701 map = isl_map_intersect_domain(map, set1);
4702 map = isl_map_intersect_range(map, set2);
4703 return map;
4706 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4707 __isl_take isl_set *set2)
4709 isl_map *map;
4710 map = isl_map_lex_ge(isl_set_get_space(set1));
4711 map = isl_map_intersect_domain(map, set1);
4712 map = isl_map_intersect_range(map, set2);
4713 return map;
4716 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4717 __isl_take isl_set *set2)
4719 isl_map *map;
4720 map = isl_map_lex_gt(isl_set_get_space(set1));
4721 map = isl_map_intersect_domain(map, set1);
4722 map = isl_map_intersect_range(map, set2);
4723 return map;
4726 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4727 __isl_take isl_map *map2)
4729 isl_map *map;
4730 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4731 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4732 map = isl_map_apply_range(map, isl_map_reverse(map2));
4733 return map;
4736 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4737 __isl_take isl_map *map2)
4739 isl_map *map;
4740 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4741 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4742 map = isl_map_apply_range(map, isl_map_reverse(map2));
4743 return map;
4746 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4747 __isl_take isl_map *map2)
4749 isl_map *map;
4750 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4751 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4752 map = isl_map_apply_range(map, isl_map_reverse(map2));
4753 return map;
4756 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4757 __isl_take isl_map *map2)
4759 isl_map *map;
4760 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4761 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4762 map = isl_map_apply_range(map, isl_map_reverse(map2));
4763 return map;
4766 static __isl_give isl_basic_map *basic_map_from_basic_set(
4767 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4769 struct isl_basic_map *bmap;
4771 bset = isl_basic_set_cow(bset);
4772 if (!bset || !dim)
4773 goto error;
4775 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4776 goto error);
4777 isl_space_free(bset->dim);
4778 bmap = bset_to_bmap(bset);
4779 bmap->dim = dim;
4780 return isl_basic_map_finalize(bmap);
4781 error:
4782 isl_basic_set_free(bset);
4783 isl_space_free(dim);
4784 return NULL;
4787 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4788 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4790 return basic_map_from_basic_set(bset, space);
4793 /* For a div d = floor(f/m), add the constraint
4795 * f - m d >= 0
4797 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4798 unsigned pos, isl_int *div)
4800 int i;
4801 unsigned total = isl_basic_map_total_dim(bmap);
4803 i = isl_basic_map_alloc_inequality(bmap);
4804 if (i < 0)
4805 return -1;
4806 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4807 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4809 return 0;
4812 /* For a div d = floor(f/m), add the constraint
4814 * -(f-(m-1)) + m d >= 0
4816 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4817 unsigned pos, isl_int *div)
4819 int i;
4820 unsigned total = isl_basic_map_total_dim(bmap);
4822 i = isl_basic_map_alloc_inequality(bmap);
4823 if (i < 0)
4824 return -1;
4825 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4826 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4827 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4828 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4830 return 0;
4833 /* For a div d = floor(f/m), add the constraints
4835 * f - m d >= 0
4836 * -(f-(m-1)) + m d >= 0
4838 * Note that the second constraint is the negation of
4840 * f - m d >= m
4842 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4843 unsigned pos, isl_int *div)
4845 if (add_upper_div_constraint(bmap, pos, div) < 0)
4846 return -1;
4847 if (add_lower_div_constraint(bmap, pos, div) < 0)
4848 return -1;
4849 return 0;
4852 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4853 unsigned pos, isl_int *div)
4855 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4856 pos, div);
4859 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4861 unsigned total = isl_basic_map_total_dim(bmap);
4862 unsigned div_pos = total - bmap->n_div + div;
4864 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4865 bmap->div[div]);
4868 /* For each known div d = floor(f/m), add the constraints
4870 * f - m d >= 0
4871 * -(f-(m-1)) + m d >= 0
4873 * Remove duplicate constraints in case of some these div constraints
4874 * already appear in "bmap".
4876 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4877 __isl_take isl_basic_map *bmap)
4879 unsigned n_div;
4881 if (!bmap)
4882 return NULL;
4883 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4884 if (n_div == 0)
4885 return bmap;
4887 bmap = add_known_div_constraints(bmap);
4888 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4889 bmap = isl_basic_map_finalize(bmap);
4890 return bmap;
4893 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4895 * In particular, if this div is of the form d = floor(f/m),
4896 * then add the constraint
4898 * f - m d >= 0
4900 * if sign < 0 or the constraint
4902 * -(f-(m-1)) + m d >= 0
4904 * if sign > 0.
4906 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4907 unsigned div, int sign)
4909 unsigned total;
4910 unsigned div_pos;
4912 if (!bmap)
4913 return -1;
4915 total = isl_basic_map_total_dim(bmap);
4916 div_pos = total - bmap->n_div + div;
4918 if (sign < 0)
4919 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4920 else
4921 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4924 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4926 return isl_basic_map_add_div_constraints(bset, div);
4929 struct isl_basic_set *isl_basic_map_underlying_set(
4930 struct isl_basic_map *bmap)
4932 if (!bmap)
4933 goto error;
4934 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4935 bmap->n_div == 0 &&
4936 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4937 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4938 return bset_from_bmap(bmap);
4939 bmap = isl_basic_map_cow(bmap);
4940 if (!bmap)
4941 goto error;
4942 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4943 if (!bmap->dim)
4944 goto error;
4945 bmap->extra -= bmap->n_div;
4946 bmap->n_div = 0;
4947 bmap = isl_basic_map_finalize(bmap);
4948 return bset_from_bmap(bmap);
4949 error:
4950 isl_basic_map_free(bmap);
4951 return NULL;
4954 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4955 __isl_take isl_basic_set *bset)
4957 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4960 /* Replace each element in "list" by the result of applying
4961 * isl_basic_map_underlying_set to the element.
4963 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4964 __isl_take isl_basic_map_list *list)
4966 int i, n;
4968 if (!list)
4969 return NULL;
4971 n = isl_basic_map_list_n_basic_map(list);
4972 for (i = 0; i < n; ++i) {
4973 isl_basic_map *bmap;
4974 isl_basic_set *bset;
4976 bmap = isl_basic_map_list_get_basic_map(list, i);
4977 bset = isl_basic_set_underlying_set(bmap);
4978 list = isl_basic_set_list_set_basic_set(list, i, bset);
4981 return list;
4984 struct isl_basic_map *isl_basic_map_overlying_set(
4985 struct isl_basic_set *bset, struct isl_basic_map *like)
4987 struct isl_basic_map *bmap;
4988 struct isl_ctx *ctx;
4989 unsigned total;
4990 int i;
4992 if (!bset || !like)
4993 goto error;
4994 ctx = bset->ctx;
4995 isl_assert(ctx, bset->n_div == 0, goto error);
4996 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4997 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4998 goto error);
4999 if (like->n_div == 0) {
5000 isl_space *space = isl_basic_map_get_space(like);
5001 isl_basic_map_free(like);
5002 return isl_basic_map_reset_space(bset, space);
5004 bset = isl_basic_set_cow(bset);
5005 if (!bset)
5006 goto error;
5007 total = bset->dim->n_out + bset->extra;
5008 bmap = bset_to_bmap(bset);
5009 isl_space_free(bmap->dim);
5010 bmap->dim = isl_space_copy(like->dim);
5011 if (!bmap->dim)
5012 goto error;
5013 bmap->n_div = like->n_div;
5014 bmap->extra += like->n_div;
5015 if (bmap->extra) {
5016 unsigned ltotal;
5017 isl_int **div;
5018 ltotal = total - bmap->extra + like->extra;
5019 if (ltotal > total)
5020 ltotal = total;
5021 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5022 bmap->extra * (1 + 1 + total));
5023 if (isl_blk_is_error(bmap->block2))
5024 goto error;
5025 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5026 if (!div)
5027 goto error;
5028 bmap->div = div;
5029 for (i = 0; i < bmap->extra; ++i)
5030 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5031 for (i = 0; i < like->n_div; ++i) {
5032 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5033 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5035 bmap = isl_basic_map_add_known_div_constraints(bmap);
5037 isl_basic_map_free(like);
5038 bmap = isl_basic_map_simplify(bmap);
5039 bmap = isl_basic_map_finalize(bmap);
5040 return bmap;
5041 error:
5042 isl_basic_map_free(like);
5043 isl_basic_set_free(bset);
5044 return NULL;
5047 struct isl_basic_set *isl_basic_set_from_underlying_set(
5048 struct isl_basic_set *bset, struct isl_basic_set *like)
5050 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5051 bset_to_bmap(like)));
5054 struct isl_set *isl_set_from_underlying_set(
5055 struct isl_set *set, struct isl_basic_set *like)
5057 int i;
5059 if (!set || !like)
5060 goto error;
5061 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
5062 goto error);
5063 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
5064 isl_basic_set_free(like);
5065 return set;
5067 set = isl_set_cow(set);
5068 if (!set)
5069 goto error;
5070 for (i = 0; i < set->n; ++i) {
5071 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
5072 isl_basic_set_copy(like));
5073 if (!set->p[i])
5074 goto error;
5076 isl_space_free(set->dim);
5077 set->dim = isl_space_copy(like->dim);
5078 if (!set->dim)
5079 goto error;
5080 isl_basic_set_free(like);
5081 return set;
5082 error:
5083 isl_basic_set_free(like);
5084 isl_set_free(set);
5085 return NULL;
5088 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5090 int i;
5092 map = isl_map_cow(map);
5093 if (!map)
5094 return NULL;
5095 map->dim = isl_space_cow(map->dim);
5096 if (!map->dim)
5097 goto error;
5099 for (i = 1; i < map->n; ++i)
5100 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5101 goto error);
5102 for (i = 0; i < map->n; ++i) {
5103 map->p[i] = bset_to_bmap(
5104 isl_basic_map_underlying_set(map->p[i]));
5105 if (!map->p[i])
5106 goto error;
5108 if (map->n == 0)
5109 map->dim = isl_space_underlying(map->dim, 0);
5110 else {
5111 isl_space_free(map->dim);
5112 map->dim = isl_space_copy(map->p[0]->dim);
5114 if (!map->dim)
5115 goto error;
5116 return set_from_map(map);
5117 error:
5118 isl_map_free(map);
5119 return NULL;
5122 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
5124 return set_from_map(isl_map_underlying_set(set_to_map(set)));
5127 /* Replace the space of "bmap" by "space".
5129 * If the space of "bmap" is identical to "space" (including the identifiers
5130 * of the input and output dimensions), then simply return the original input.
5132 __isl_give isl_basic_map *isl_basic_map_reset_space(
5133 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5135 isl_bool equal;
5137 if (!bmap)
5138 goto error;
5139 equal = isl_space_is_equal(bmap->dim, space);
5140 if (equal >= 0 && equal)
5141 equal = isl_space_match(bmap->dim, isl_dim_in,
5142 space, isl_dim_in);
5143 if (equal >= 0 && equal)
5144 equal = isl_space_match(bmap->dim, isl_dim_out,
5145 space, isl_dim_out);
5146 if (equal < 0)
5147 goto error;
5148 if (equal) {
5149 isl_space_free(space);
5150 return bmap;
5152 bmap = isl_basic_map_cow(bmap);
5153 if (!bmap || !space)
5154 goto error;
5156 isl_space_free(bmap->dim);
5157 bmap->dim = space;
5159 bmap = isl_basic_map_finalize(bmap);
5161 return bmap;
5162 error:
5163 isl_basic_map_free(bmap);
5164 isl_space_free(space);
5165 return NULL;
5168 __isl_give isl_basic_set *isl_basic_set_reset_space(
5169 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5171 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5172 dim));
5175 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5176 __isl_take isl_space *dim)
5178 int i;
5180 map = isl_map_cow(map);
5181 if (!map || !dim)
5182 goto error;
5184 for (i = 0; i < map->n; ++i) {
5185 map->p[i] = isl_basic_map_reset_space(map->p[i],
5186 isl_space_copy(dim));
5187 if (!map->p[i])
5188 goto error;
5190 isl_space_free(map->dim);
5191 map->dim = dim;
5193 return map;
5194 error:
5195 isl_map_free(map);
5196 isl_space_free(dim);
5197 return NULL;
5200 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5201 __isl_take isl_space *dim)
5203 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5206 /* Compute the parameter domain of the given basic set.
5208 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5210 isl_space *space;
5211 unsigned n;
5213 if (isl_basic_set_is_params(bset))
5214 return bset;
5216 n = isl_basic_set_dim(bset, isl_dim_set);
5217 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5218 space = isl_basic_set_get_space(bset);
5219 space = isl_space_params(space);
5220 bset = isl_basic_set_reset_space(bset, space);
5221 return bset;
5224 /* Construct a zero-dimensional basic set with the given parameter domain.
5226 __isl_give isl_basic_set *isl_basic_set_from_params(
5227 __isl_take isl_basic_set *bset)
5229 isl_space *space;
5230 space = isl_basic_set_get_space(bset);
5231 space = isl_space_set_from_params(space);
5232 bset = isl_basic_set_reset_space(bset, space);
5233 return bset;
5236 /* Compute the parameter domain of the given set.
5238 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5240 isl_space *space;
5241 unsigned n;
5243 if (isl_set_is_params(set))
5244 return set;
5246 n = isl_set_dim(set, isl_dim_set);
5247 set = isl_set_project_out(set, isl_dim_set, 0, n);
5248 space = isl_set_get_space(set);
5249 space = isl_space_params(space);
5250 set = isl_set_reset_space(set, space);
5251 return set;
5254 /* Construct a zero-dimensional set with the given parameter domain.
5256 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5258 isl_space *space;
5259 space = isl_set_get_space(set);
5260 space = isl_space_set_from_params(space);
5261 set = isl_set_reset_space(set, space);
5262 return set;
5265 /* Compute the parameter domain of the given map.
5267 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5269 isl_space *space;
5270 unsigned n;
5272 n = isl_map_dim(map, isl_dim_in);
5273 map = isl_map_project_out(map, isl_dim_in, 0, n);
5274 n = isl_map_dim(map, isl_dim_out);
5275 map = isl_map_project_out(map, isl_dim_out, 0, n);
5276 space = isl_map_get_space(map);
5277 space = isl_space_params(space);
5278 map = isl_map_reset_space(map, space);
5279 return map;
5282 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5284 isl_space *space;
5285 unsigned n_out;
5287 if (!bmap)
5288 return NULL;
5289 space = isl_space_domain(isl_basic_map_get_space(bmap));
5291 n_out = isl_basic_map_n_out(bmap);
5292 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5294 return isl_basic_map_reset_space(bmap, space);
5297 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5299 if (!bmap)
5300 return -1;
5301 return isl_space_may_be_set(bmap->dim);
5304 /* Is this basic map actually a set?
5305 * Users should never call this function. Outside of isl,
5306 * the type should indicate whether something is a set or a map.
5308 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5310 if (!bmap)
5311 return -1;
5312 return isl_space_is_set(bmap->dim);
5315 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5317 if (!bmap)
5318 return NULL;
5319 if (isl_basic_map_is_set(bmap))
5320 return bmap;
5321 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5324 __isl_give isl_basic_map *isl_basic_map_domain_map(
5325 __isl_take isl_basic_map *bmap)
5327 int i;
5328 isl_space *dim;
5329 isl_basic_map *domain;
5330 int nparam, n_in, n_out;
5332 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5333 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5334 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5336 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5337 domain = isl_basic_map_universe(dim);
5339 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5340 bmap = isl_basic_map_apply_range(bmap, domain);
5341 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5343 for (i = 0; i < n_in; ++i)
5344 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5345 isl_dim_out, i);
5347 bmap = isl_basic_map_gauss(bmap, NULL);
5348 return isl_basic_map_finalize(bmap);
5351 __isl_give isl_basic_map *isl_basic_map_range_map(
5352 __isl_take isl_basic_map *bmap)
5354 int i;
5355 isl_space *dim;
5356 isl_basic_map *range;
5357 int nparam, n_in, n_out;
5359 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5360 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5361 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5363 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5364 range = isl_basic_map_universe(dim);
5366 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5367 bmap = isl_basic_map_apply_range(bmap, range);
5368 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5370 for (i = 0; i < n_out; ++i)
5371 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5372 isl_dim_out, i);
5374 bmap = isl_basic_map_gauss(bmap, NULL);
5375 return isl_basic_map_finalize(bmap);
5378 int isl_map_may_be_set(__isl_keep isl_map *map)
5380 if (!map)
5381 return -1;
5382 return isl_space_may_be_set(map->dim);
5385 /* Is this map actually a set?
5386 * Users should never call this function. Outside of isl,
5387 * the type should indicate whether something is a set or a map.
5389 int isl_map_is_set(__isl_keep isl_map *map)
5391 if (!map)
5392 return -1;
5393 return isl_space_is_set(map->dim);
5396 struct isl_set *isl_map_range(struct isl_map *map)
5398 int i;
5399 struct isl_set *set;
5401 if (!map)
5402 goto error;
5403 if (isl_map_is_set(map))
5404 return set_from_map(map);
5406 map = isl_map_cow(map);
5407 if (!map)
5408 goto error;
5410 set = set_from_map(map);
5411 set->dim = isl_space_range(set->dim);
5412 if (!set->dim)
5413 goto error;
5414 for (i = 0; i < map->n; ++i) {
5415 set->p[i] = isl_basic_map_range(map->p[i]);
5416 if (!set->p[i])
5417 goto error;
5419 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5420 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5421 return set;
5422 error:
5423 isl_map_free(map);
5424 return NULL;
5427 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5429 int i;
5431 map = isl_map_cow(map);
5432 if (!map)
5433 return NULL;
5435 map->dim = isl_space_domain_map(map->dim);
5436 if (!map->dim)
5437 goto error;
5438 for (i = 0; i < map->n; ++i) {
5439 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5440 if (!map->p[i])
5441 goto error;
5443 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5444 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5445 return map;
5446 error:
5447 isl_map_free(map);
5448 return NULL;
5451 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5453 int i;
5454 isl_space *range_dim;
5456 map = isl_map_cow(map);
5457 if (!map)
5458 return NULL;
5460 range_dim = isl_space_range(isl_map_get_space(map));
5461 range_dim = isl_space_from_range(range_dim);
5462 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5463 map->dim = isl_space_join(map->dim, range_dim);
5464 if (!map->dim)
5465 goto error;
5466 for (i = 0; i < map->n; ++i) {
5467 map->p[i] = isl_basic_map_range_map(map->p[i]);
5468 if (!map->p[i])
5469 goto error;
5471 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5472 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5473 return map;
5474 error:
5475 isl_map_free(map);
5476 return NULL;
5479 /* Given a wrapped map of the form A[B -> C],
5480 * return the map A[B -> C] -> B.
5482 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5484 isl_id *id;
5485 isl_map *map;
5487 if (!set)
5488 return NULL;
5489 if (!isl_set_has_tuple_id(set))
5490 return isl_map_domain_map(isl_set_unwrap(set));
5492 id = isl_set_get_tuple_id(set);
5493 map = isl_map_domain_map(isl_set_unwrap(set));
5494 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5496 return map;
5499 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5500 __isl_take isl_space *dim)
5502 int i;
5503 struct isl_map *map = NULL;
5505 set = isl_set_cow(set);
5506 if (!set || !dim)
5507 goto error;
5508 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5509 goto error);
5510 map = set_to_map(set);
5511 for (i = 0; i < set->n; ++i) {
5512 map->p[i] = basic_map_from_basic_set(
5513 set->p[i], isl_space_copy(dim));
5514 if (!map->p[i])
5515 goto error;
5517 isl_space_free(map->dim);
5518 map->dim = dim;
5519 return map;
5520 error:
5521 isl_space_free(dim);
5522 isl_set_free(set);
5523 return NULL;
5526 __isl_give isl_basic_map *isl_basic_map_from_domain(
5527 __isl_take isl_basic_set *bset)
5529 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5532 __isl_give isl_basic_map *isl_basic_map_from_range(
5533 __isl_take isl_basic_set *bset)
5535 isl_space *space;
5536 space = isl_basic_set_get_space(bset);
5537 space = isl_space_from_range(space);
5538 bset = isl_basic_set_reset_space(bset, space);
5539 return bset_to_bmap(bset);
5542 /* Create a relation with the given set as range.
5543 * The domain of the created relation is a zero-dimensional
5544 * flat anonymous space.
5546 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5548 isl_space *space;
5549 space = isl_set_get_space(set);
5550 space = isl_space_from_range(space);
5551 set = isl_set_reset_space(set, space);
5552 return set_to_map(set);
5555 /* Create a relation with the given set as domain.
5556 * The range of the created relation is a zero-dimensional
5557 * flat anonymous space.
5559 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5561 return isl_map_reverse(isl_map_from_range(set));
5564 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5565 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5567 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5570 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5571 __isl_take isl_set *range)
5573 return isl_map_apply_range(isl_map_reverse(domain), range);
5576 /* Return a newly allocated isl_map with given space and flags and
5577 * room for "n" basic maps.
5578 * Make sure that all cached information is cleared.
5580 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5581 unsigned flags)
5583 struct isl_map *map;
5585 if (!space)
5586 return NULL;
5587 if (n < 0)
5588 isl_die(space->ctx, isl_error_internal,
5589 "negative number of basic maps", goto error);
5590 map = isl_calloc(space->ctx, struct isl_map,
5591 sizeof(struct isl_map) +
5592 (n - 1) * sizeof(struct isl_basic_map *));
5593 if (!map)
5594 goto error;
5596 map->ctx = space->ctx;
5597 isl_ctx_ref(map->ctx);
5598 map->ref = 1;
5599 map->size = n;
5600 map->n = 0;
5601 map->dim = space;
5602 map->flags = flags;
5603 return map;
5604 error:
5605 isl_space_free(space);
5606 return NULL;
5609 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5610 unsigned nparam, unsigned in, unsigned out, int n,
5611 unsigned flags)
5613 struct isl_map *map;
5614 isl_space *dims;
5616 dims = isl_space_alloc(ctx, nparam, in, out);
5617 if (!dims)
5618 return NULL;
5620 map = isl_map_alloc_space(dims, n, flags);
5621 return map;
5624 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5626 struct isl_basic_map *bmap;
5627 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5628 bmap = isl_basic_map_set_to_empty(bmap);
5629 return bmap;
5632 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5634 struct isl_basic_set *bset;
5635 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5636 bset = isl_basic_set_set_to_empty(bset);
5637 return bset;
5640 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5642 struct isl_basic_map *bmap;
5643 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5644 bmap = isl_basic_map_finalize(bmap);
5645 return bmap;
5648 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5650 struct isl_basic_set *bset;
5651 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5652 bset = isl_basic_set_finalize(bset);
5653 return bset;
5656 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5658 int i;
5659 unsigned total = isl_space_dim(dim, isl_dim_all);
5660 isl_basic_map *bmap;
5662 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5663 for (i = 0; i < total; ++i) {
5664 int k = isl_basic_map_alloc_inequality(bmap);
5665 if (k < 0)
5666 goto error;
5667 isl_seq_clr(bmap->ineq[k], 1 + total);
5668 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5670 return bmap;
5671 error:
5672 isl_basic_map_free(bmap);
5673 return NULL;
5676 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5678 return isl_basic_map_nat_universe(dim);
5681 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5683 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5686 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5688 return isl_map_nat_universe(dim);
5691 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5693 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5696 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5698 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5701 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5703 struct isl_map *map;
5704 if (!dim)
5705 return NULL;
5706 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5707 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5708 return map;
5711 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5713 struct isl_set *set;
5714 if (!dim)
5715 return NULL;
5716 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5717 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5718 return set;
5721 struct isl_map *isl_map_dup(struct isl_map *map)
5723 int i;
5724 struct isl_map *dup;
5726 if (!map)
5727 return NULL;
5728 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5729 for (i = 0; i < map->n; ++i)
5730 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5731 return dup;
5734 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5735 __isl_take isl_basic_map *bmap)
5737 if (!bmap || !map)
5738 goto error;
5739 if (isl_basic_map_plain_is_empty(bmap)) {
5740 isl_basic_map_free(bmap);
5741 return map;
5743 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5744 isl_assert(map->ctx, map->n < map->size, goto error);
5745 map->p[map->n] = bmap;
5746 map->n++;
5747 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5748 return map;
5749 error:
5750 if (map)
5751 isl_map_free(map);
5752 if (bmap)
5753 isl_basic_map_free(bmap);
5754 return NULL;
5757 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5759 int i;
5761 if (!map)
5762 return NULL;
5764 if (--map->ref > 0)
5765 return NULL;
5767 clear_caches(map);
5768 isl_ctx_deref(map->ctx);
5769 for (i = 0; i < map->n; ++i)
5770 isl_basic_map_free(map->p[i]);
5771 isl_space_free(map->dim);
5772 free(map);
5774 return NULL;
5777 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5778 struct isl_basic_map *bmap, unsigned pos, int value)
5780 int j;
5782 bmap = isl_basic_map_cow(bmap);
5783 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5784 j = isl_basic_map_alloc_equality(bmap);
5785 if (j < 0)
5786 goto error;
5787 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5788 isl_int_set_si(bmap->eq[j][pos], -1);
5789 isl_int_set_si(bmap->eq[j][0], value);
5790 bmap = isl_basic_map_simplify(bmap);
5791 return isl_basic_map_finalize(bmap);
5792 error:
5793 isl_basic_map_free(bmap);
5794 return NULL;
5797 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5798 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5800 int j;
5802 bmap = isl_basic_map_cow(bmap);
5803 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5804 j = isl_basic_map_alloc_equality(bmap);
5805 if (j < 0)
5806 goto error;
5807 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5808 isl_int_set_si(bmap->eq[j][pos], -1);
5809 isl_int_set(bmap->eq[j][0], value);
5810 bmap = isl_basic_map_simplify(bmap);
5811 return isl_basic_map_finalize(bmap);
5812 error:
5813 isl_basic_map_free(bmap);
5814 return NULL;
5817 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5818 enum isl_dim_type type, unsigned pos, int value)
5820 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5821 return isl_basic_map_free(bmap);
5822 return isl_basic_map_fix_pos_si(bmap,
5823 isl_basic_map_offset(bmap, type) + pos, value);
5826 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5827 enum isl_dim_type type, unsigned pos, isl_int value)
5829 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5830 return isl_basic_map_free(bmap);
5831 return isl_basic_map_fix_pos(bmap,
5832 isl_basic_map_offset(bmap, type) + pos, value);
5835 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5836 * to be equal to "v".
5838 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5839 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5841 if (!bmap || !v)
5842 goto error;
5843 if (!isl_val_is_int(v))
5844 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5845 "expecting integer value", goto error);
5846 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5847 goto error;
5848 pos += isl_basic_map_offset(bmap, type);
5849 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5850 isl_val_free(v);
5851 return bmap;
5852 error:
5853 isl_basic_map_free(bmap);
5854 isl_val_free(v);
5855 return NULL;
5858 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5859 * to be equal to "v".
5861 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5862 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5864 return isl_basic_map_fix_val(bset, type, pos, v);
5867 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5868 enum isl_dim_type type, unsigned pos, int value)
5870 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5871 type, pos, value));
5874 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5875 enum isl_dim_type type, unsigned pos, isl_int value)
5877 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5878 type, pos, value));
5881 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5882 unsigned input, int value)
5884 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5887 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5888 unsigned dim, int value)
5890 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5891 isl_dim_set, dim, value));
5894 static int remove_if_empty(__isl_keep isl_map *map, int i)
5896 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5898 if (empty < 0)
5899 return -1;
5900 if (!empty)
5901 return 0;
5903 isl_basic_map_free(map->p[i]);
5904 if (i != map->n - 1) {
5905 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5906 map->p[i] = map->p[map->n - 1];
5908 map->n--;
5910 return 0;
5913 /* Perform "fn" on each basic map of "map", where we may not be holding
5914 * the only reference to "map".
5915 * In particular, "fn" should be a semantics preserving operation
5916 * that we want to apply to all copies of "map". We therefore need
5917 * to be careful not to modify "map" in a way that breaks "map"
5918 * in case anything goes wrong.
5920 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5921 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5923 struct isl_basic_map *bmap;
5924 int i;
5926 if (!map)
5927 return NULL;
5929 for (i = map->n - 1; i >= 0; --i) {
5930 bmap = isl_basic_map_copy(map->p[i]);
5931 bmap = fn(bmap);
5932 if (!bmap)
5933 goto error;
5934 isl_basic_map_free(map->p[i]);
5935 map->p[i] = bmap;
5936 if (remove_if_empty(map, i) < 0)
5937 goto error;
5940 return map;
5941 error:
5942 isl_map_free(map);
5943 return NULL;
5946 struct isl_map *isl_map_fix_si(struct isl_map *map,
5947 enum isl_dim_type type, unsigned pos, int value)
5949 int i;
5951 map = isl_map_cow(map);
5952 if (!map)
5953 return NULL;
5955 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5956 for (i = map->n - 1; i >= 0; --i) {
5957 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5958 if (remove_if_empty(map, i) < 0)
5959 goto error;
5961 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5962 return map;
5963 error:
5964 isl_map_free(map);
5965 return NULL;
5968 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5969 enum isl_dim_type type, unsigned pos, int value)
5971 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5974 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5975 enum isl_dim_type type, unsigned pos, isl_int value)
5977 int i;
5979 map = isl_map_cow(map);
5980 if (!map)
5981 return NULL;
5983 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5984 for (i = 0; i < map->n; ++i) {
5985 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5986 if (!map->p[i])
5987 goto error;
5989 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5990 return map;
5991 error:
5992 isl_map_free(map);
5993 return NULL;
5996 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5997 enum isl_dim_type type, unsigned pos, isl_int value)
5999 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6002 /* Fix the value of the variable at position "pos" of type "type" of "map"
6003 * to be equal to "v".
6005 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6006 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6008 int i;
6010 map = isl_map_cow(map);
6011 if (!map || !v)
6012 goto error;
6014 if (!isl_val_is_int(v))
6015 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6016 "expecting integer value", goto error);
6017 if (pos >= isl_map_dim(map, type))
6018 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6019 "index out of bounds", goto error);
6020 for (i = map->n - 1; i >= 0; --i) {
6021 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6022 isl_val_copy(v));
6023 if (remove_if_empty(map, i) < 0)
6024 goto error;
6026 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6027 isl_val_free(v);
6028 return map;
6029 error:
6030 isl_map_free(map);
6031 isl_val_free(v);
6032 return NULL;
6035 /* Fix the value of the variable at position "pos" of type "type" of "set"
6036 * to be equal to "v".
6038 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6039 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6041 return isl_map_fix_val(set, type, pos, v);
6044 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6045 unsigned input, int value)
6047 return isl_map_fix_si(map, isl_dim_in, input, value);
6050 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6052 return set_from_map(isl_map_fix_si(set_to_map(set),
6053 isl_dim_set, dim, value));
6056 static __isl_give isl_basic_map *basic_map_bound_si(
6057 __isl_take isl_basic_map *bmap,
6058 enum isl_dim_type type, unsigned pos, int value, int upper)
6060 int j;
6062 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6063 return isl_basic_map_free(bmap);
6064 pos += isl_basic_map_offset(bmap, type);
6065 bmap = isl_basic_map_cow(bmap);
6066 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6067 j = isl_basic_map_alloc_inequality(bmap);
6068 if (j < 0)
6069 goto error;
6070 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6071 if (upper) {
6072 isl_int_set_si(bmap->ineq[j][pos], -1);
6073 isl_int_set_si(bmap->ineq[j][0], value);
6074 } else {
6075 isl_int_set_si(bmap->ineq[j][pos], 1);
6076 isl_int_set_si(bmap->ineq[j][0], -value);
6078 bmap = isl_basic_map_simplify(bmap);
6079 return isl_basic_map_finalize(bmap);
6080 error:
6081 isl_basic_map_free(bmap);
6082 return NULL;
6085 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6086 __isl_take isl_basic_map *bmap,
6087 enum isl_dim_type type, unsigned pos, int value)
6089 return basic_map_bound_si(bmap, type, pos, value, 0);
6092 /* Constrain the values of the given dimension to be no greater than "value".
6094 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6095 __isl_take isl_basic_map *bmap,
6096 enum isl_dim_type type, unsigned pos, int value)
6098 return basic_map_bound_si(bmap, type, pos, value, 1);
6101 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
6102 unsigned dim, isl_int value)
6104 int j;
6106 bset = isl_basic_set_cow(bset);
6107 bset = isl_basic_set_extend_constraints(bset, 0, 1);
6108 j = isl_basic_set_alloc_inequality(bset);
6109 if (j < 0)
6110 goto error;
6111 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
6112 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
6113 isl_int_neg(bset->ineq[j][0], value);
6114 bset = isl_basic_set_simplify(bset);
6115 return isl_basic_set_finalize(bset);
6116 error:
6117 isl_basic_set_free(bset);
6118 return NULL;
6121 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6122 enum isl_dim_type type, unsigned pos, int value, int upper)
6124 int i;
6126 map = isl_map_cow(map);
6127 if (!map)
6128 return NULL;
6130 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6131 for (i = 0; i < map->n; ++i) {
6132 map->p[i] = basic_map_bound_si(map->p[i],
6133 type, pos, value, upper);
6134 if (!map->p[i])
6135 goto error;
6137 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6138 return map;
6139 error:
6140 isl_map_free(map);
6141 return NULL;
6144 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6145 enum isl_dim_type type, unsigned pos, int value)
6147 return map_bound_si(map, type, pos, value, 0);
6150 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6151 enum isl_dim_type type, unsigned pos, int value)
6153 return map_bound_si(map, type, pos, value, 1);
6156 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6157 enum isl_dim_type type, unsigned pos, int value)
6159 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6160 type, pos, value));
6163 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6164 enum isl_dim_type type, unsigned pos, int value)
6166 return isl_map_upper_bound_si(set, type, pos, value);
6169 /* Bound the given variable of "bmap" from below (or above is "upper"
6170 * is set) to "value".
6172 static __isl_give isl_basic_map *basic_map_bound(
6173 __isl_take isl_basic_map *bmap,
6174 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6176 int j;
6178 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6179 return isl_basic_map_free(bmap);
6180 pos += isl_basic_map_offset(bmap, type);
6181 bmap = isl_basic_map_cow(bmap);
6182 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6183 j = isl_basic_map_alloc_inequality(bmap);
6184 if (j < 0)
6185 goto error;
6186 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6187 if (upper) {
6188 isl_int_set_si(bmap->ineq[j][pos], -1);
6189 isl_int_set(bmap->ineq[j][0], value);
6190 } else {
6191 isl_int_set_si(bmap->ineq[j][pos], 1);
6192 isl_int_neg(bmap->ineq[j][0], value);
6194 bmap = isl_basic_map_simplify(bmap);
6195 return isl_basic_map_finalize(bmap);
6196 error:
6197 isl_basic_map_free(bmap);
6198 return NULL;
6201 /* Bound the given variable of "map" from below (or above is "upper"
6202 * is set) to "value".
6204 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6205 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6207 int i;
6209 map = isl_map_cow(map);
6210 if (!map)
6211 return NULL;
6213 if (pos >= isl_map_dim(map, type))
6214 isl_die(map->ctx, isl_error_invalid,
6215 "index out of bounds", goto error);
6216 for (i = map->n - 1; i >= 0; --i) {
6217 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6218 if (remove_if_empty(map, i) < 0)
6219 goto error;
6221 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6222 return map;
6223 error:
6224 isl_map_free(map);
6225 return NULL;
6228 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6229 enum isl_dim_type type, unsigned pos, isl_int value)
6231 return map_bound(map, type, pos, value, 0);
6234 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6235 enum isl_dim_type type, unsigned pos, isl_int value)
6237 return map_bound(map, type, pos, value, 1);
6240 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6241 enum isl_dim_type type, unsigned pos, isl_int value)
6243 return isl_map_lower_bound(set, type, pos, value);
6246 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6247 enum isl_dim_type type, unsigned pos, isl_int value)
6249 return isl_map_upper_bound(set, type, pos, value);
6252 /* Force the values of the variable at position "pos" of type "type" of "set"
6253 * to be no smaller than "value".
6255 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6256 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6258 if (!value)
6259 goto error;
6260 if (!isl_val_is_int(value))
6261 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6262 "expecting integer value", goto error);
6263 set = isl_set_lower_bound(set, type, pos, value->n);
6264 isl_val_free(value);
6265 return set;
6266 error:
6267 isl_val_free(value);
6268 isl_set_free(set);
6269 return NULL;
6272 /* Force the values of the variable at position "pos" of type "type" of "set"
6273 * to be no greater than "value".
6275 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6276 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6278 if (!value)
6279 goto error;
6280 if (!isl_val_is_int(value))
6281 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6282 "expecting integer value", goto error);
6283 set = isl_set_upper_bound(set, type, pos, value->n);
6284 isl_val_free(value);
6285 return set;
6286 error:
6287 isl_val_free(value);
6288 isl_set_free(set);
6289 return NULL;
6292 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6293 isl_int value)
6295 int i;
6297 set = isl_set_cow(set);
6298 if (!set)
6299 return NULL;
6301 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6302 for (i = 0; i < set->n; ++i) {
6303 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6304 if (!set->p[i])
6305 goto error;
6307 return set;
6308 error:
6309 isl_set_free(set);
6310 return NULL;
6313 struct isl_map *isl_map_reverse(struct isl_map *map)
6315 int i;
6317 map = isl_map_cow(map);
6318 if (!map)
6319 return NULL;
6321 map->dim = isl_space_reverse(map->dim);
6322 if (!map->dim)
6323 goto error;
6324 for (i = 0; i < map->n; ++i) {
6325 map->p[i] = isl_basic_map_reverse(map->p[i]);
6326 if (!map->p[i])
6327 goto error;
6329 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6330 return map;
6331 error:
6332 isl_map_free(map);
6333 return NULL;
6336 #undef TYPE
6337 #define TYPE isl_pw_multi_aff
6338 #undef SUFFIX
6339 #define SUFFIX _pw_multi_aff
6340 #undef EMPTY
6341 #define EMPTY isl_pw_multi_aff_empty
6342 #undef ADD
6343 #define ADD isl_pw_multi_aff_union_add
6344 #include "isl_map_lexopt_templ.c"
6346 /* Given a map "map", compute the lexicographically minimal
6347 * (or maximal) image element for each domain element in dom,
6348 * in the form of an isl_pw_multi_aff.
6349 * If "empty" is not NULL, then set *empty to those elements in dom that
6350 * do not have an image element.
6351 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6352 * should be computed over the domain of "map". "empty" is also NULL
6353 * in this case.
6355 * We first compute the lexicographically minimal or maximal element
6356 * in the first basic map. This results in a partial solution "res"
6357 * and a subset "todo" of dom that still need to be handled.
6358 * We then consider each of the remaining maps in "map" and successively
6359 * update both "res" and "todo".
6360 * If "empty" is NULL, then the todo sets are not needed and therefore
6361 * also not computed.
6363 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6364 __isl_take isl_map *map, __isl_take isl_set *dom,
6365 __isl_give isl_set **empty, unsigned flags)
6367 int i;
6368 int full;
6369 isl_pw_multi_aff *res;
6370 isl_set *todo;
6372 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6373 if (!map || (!full && !dom))
6374 goto error;
6376 if (isl_map_plain_is_empty(map)) {
6377 if (empty)
6378 *empty = dom;
6379 else
6380 isl_set_free(dom);
6381 return isl_pw_multi_aff_from_map(map);
6384 res = basic_map_partial_lexopt_pw_multi_aff(
6385 isl_basic_map_copy(map->p[0]),
6386 isl_set_copy(dom), empty, flags);
6388 if (empty)
6389 todo = *empty;
6390 for (i = 1; i < map->n; ++i) {
6391 isl_pw_multi_aff *res_i;
6393 res_i = basic_map_partial_lexopt_pw_multi_aff(
6394 isl_basic_map_copy(map->p[i]),
6395 isl_set_copy(dom), empty, flags);
6397 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6398 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6399 else
6400 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6402 if (empty)
6403 todo = isl_set_intersect(todo, *empty);
6406 isl_set_free(dom);
6407 isl_map_free(map);
6409 if (empty)
6410 *empty = todo;
6412 return res;
6413 error:
6414 if (empty)
6415 *empty = NULL;
6416 isl_set_free(dom);
6417 isl_map_free(map);
6418 return NULL;
6421 #undef TYPE
6422 #define TYPE isl_map
6423 #undef SUFFIX
6424 #define SUFFIX
6425 #undef EMPTY
6426 #define EMPTY isl_map_empty
6427 #undef ADD
6428 #define ADD isl_map_union_disjoint
6429 #include "isl_map_lexopt_templ.c"
6431 /* Given a map "map", compute the lexicographically minimal
6432 * (or maximal) image element for each domain element in "dom",
6433 * in the form of an isl_map.
6434 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6435 * do not have an image element.
6436 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6437 * should be computed over the domain of "map". "empty" is also NULL
6438 * in this case.
6440 * If the input consists of more than one disjunct, then first
6441 * compute the desired result in the form of an isl_pw_multi_aff and
6442 * then convert that into an isl_map.
6444 * This function used to have an explicit implementation in terms
6445 * of isl_maps, but it would continually intersect the domains of
6446 * partial results with the complement of the domain of the next
6447 * partial solution, potentially leading to an explosion in the number
6448 * of disjuncts if there are several disjuncts in the input.
6449 * An even earlier implementation of this function would look for
6450 * better results in the domain of the partial result and for extra
6451 * results in the complement of this domain, which would lead to
6452 * even more splintering.
6454 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6455 __isl_take isl_map *map, __isl_take isl_set *dom,
6456 __isl_give isl_set **empty, unsigned flags)
6458 int full;
6459 struct isl_map *res;
6460 isl_pw_multi_aff *pma;
6462 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6463 if (!map || (!full && !dom))
6464 goto error;
6466 if (isl_map_plain_is_empty(map)) {
6467 if (empty)
6468 *empty = dom;
6469 else
6470 isl_set_free(dom);
6471 return map;
6474 if (map->n == 1) {
6475 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6476 dom, empty, flags);
6477 isl_map_free(map);
6478 return res;
6481 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6482 flags);
6483 return isl_map_from_pw_multi_aff(pma);
6484 error:
6485 if (empty)
6486 *empty = NULL;
6487 isl_set_free(dom);
6488 isl_map_free(map);
6489 return NULL;
6492 __isl_give isl_map *isl_map_partial_lexmax(
6493 __isl_take isl_map *map, __isl_take isl_set *dom,
6494 __isl_give isl_set **empty)
6496 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6499 __isl_give isl_map *isl_map_partial_lexmin(
6500 __isl_take isl_map *map, __isl_take isl_set *dom,
6501 __isl_give isl_set **empty)
6503 return isl_map_partial_lexopt(map, dom, empty, 0);
6506 __isl_give isl_set *isl_set_partial_lexmin(
6507 __isl_take isl_set *set, __isl_take isl_set *dom,
6508 __isl_give isl_set **empty)
6510 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6511 dom, empty));
6514 __isl_give isl_set *isl_set_partial_lexmax(
6515 __isl_take isl_set *set, __isl_take isl_set *dom,
6516 __isl_give isl_set **empty)
6518 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6519 dom, empty));
6522 /* Compute the lexicographic minimum (or maximum if "flags" includes
6523 * ISL_OPT_MAX) of "bset" over its parametric domain.
6525 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6526 unsigned flags)
6528 return isl_basic_map_lexopt(bset, flags);
6531 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6533 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6536 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6538 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6541 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6543 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6546 /* Compute the lexicographic minimum of "bset" over its parametric domain
6547 * for the purpose of quantifier elimination.
6548 * That is, find an explicit representation for all the existentially
6549 * quantified variables in "bset" by computing their lexicographic
6550 * minimum.
6552 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6553 __isl_take isl_basic_set *bset)
6555 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6558 /* Extract the first and only affine expression from list
6559 * and then add it to *pwaff with the given dom.
6560 * This domain is known to be disjoint from other domains
6561 * because of the way isl_basic_map_foreach_lexmax works.
6563 static int update_dim_opt(__isl_take isl_basic_set *dom,
6564 __isl_take isl_aff_list *list, void *user)
6566 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6567 isl_aff *aff;
6568 isl_pw_aff **pwaff = user;
6569 isl_pw_aff *pwaff_i;
6571 if (!list)
6572 goto error;
6573 if (isl_aff_list_n_aff(list) != 1)
6574 isl_die(ctx, isl_error_internal,
6575 "expecting single element list", goto error);
6577 aff = isl_aff_list_get_aff(list, 0);
6578 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6580 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6582 isl_aff_list_free(list);
6584 return 0;
6585 error:
6586 isl_basic_set_free(dom);
6587 isl_aff_list_free(list);
6588 return -1;
6591 /* Given a basic map with one output dimension, compute the minimum or
6592 * maximum of that dimension as an isl_pw_aff.
6594 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6595 * call update_dim_opt on each leaf of the result.
6597 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6598 int max)
6600 isl_space *dim = isl_basic_map_get_space(bmap);
6601 isl_pw_aff *pwaff;
6602 int r;
6604 dim = isl_space_from_domain(isl_space_domain(dim));
6605 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6606 pwaff = isl_pw_aff_empty(dim);
6608 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6609 if (r < 0)
6610 return isl_pw_aff_free(pwaff);
6612 return pwaff;
6615 /* Compute the minimum or maximum of the given output dimension
6616 * as a function of the parameters and the input dimensions,
6617 * but independently of the other output dimensions.
6619 * We first project out the other output dimension and then compute
6620 * the "lexicographic" maximum in each basic map, combining the results
6621 * using isl_pw_aff_union_max.
6623 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6624 int max)
6626 int i;
6627 isl_pw_aff *pwaff;
6628 unsigned n_out;
6630 n_out = isl_map_dim(map, isl_dim_out);
6631 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6632 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6633 if (!map)
6634 return NULL;
6636 if (map->n == 0) {
6637 isl_space *dim = isl_map_get_space(map);
6638 isl_map_free(map);
6639 return isl_pw_aff_empty(dim);
6642 pwaff = basic_map_dim_opt(map->p[0], max);
6643 for (i = 1; i < map->n; ++i) {
6644 isl_pw_aff *pwaff_i;
6646 pwaff_i = basic_map_dim_opt(map->p[i], max);
6647 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6650 isl_map_free(map);
6652 return pwaff;
6655 /* Compute the minimum of the given output dimension as a function of the
6656 * parameters and input dimensions, but independently of
6657 * the other output dimensions.
6659 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6661 return map_dim_opt(map, pos, 0);
6664 /* Compute the maximum of the given output dimension as a function of the
6665 * parameters and input dimensions, but independently of
6666 * the other output dimensions.
6668 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6670 return map_dim_opt(map, pos, 1);
6673 /* Compute the minimum or maximum of the given set dimension
6674 * as a function of the parameters,
6675 * but independently of the other set dimensions.
6677 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6678 int max)
6680 return map_dim_opt(set, pos, max);
6683 /* Compute the maximum of the given set dimension as a function of the
6684 * parameters, but independently of the other set dimensions.
6686 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6688 return set_dim_opt(set, pos, 1);
6691 /* Compute the minimum of the given set dimension as a function of the
6692 * parameters, but independently of the other set dimensions.
6694 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6696 return set_dim_opt(set, pos, 0);
6699 /* Apply a preimage specified by "mat" on the parameters of "bset".
6700 * bset is assumed to have only parameters and divs.
6702 static struct isl_basic_set *basic_set_parameter_preimage(
6703 struct isl_basic_set *bset, struct isl_mat *mat)
6705 unsigned nparam;
6707 if (!bset || !mat)
6708 goto error;
6710 bset->dim = isl_space_cow(bset->dim);
6711 if (!bset->dim)
6712 goto error;
6714 nparam = isl_basic_set_dim(bset, isl_dim_param);
6716 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6718 bset->dim->nparam = 0;
6719 bset->dim->n_out = nparam;
6720 bset = isl_basic_set_preimage(bset, mat);
6721 if (bset) {
6722 bset->dim->nparam = bset->dim->n_out;
6723 bset->dim->n_out = 0;
6725 return bset;
6726 error:
6727 isl_mat_free(mat);
6728 isl_basic_set_free(bset);
6729 return NULL;
6732 /* Apply a preimage specified by "mat" on the parameters of "set".
6733 * set is assumed to have only parameters and divs.
6735 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6736 __isl_take isl_mat *mat)
6738 isl_space *space;
6739 unsigned nparam;
6741 if (!set || !mat)
6742 goto error;
6744 nparam = isl_set_dim(set, isl_dim_param);
6746 if (mat->n_row != 1 + nparam)
6747 isl_die(isl_set_get_ctx(set), isl_error_internal,
6748 "unexpected number of rows", goto error);
6750 space = isl_set_get_space(set);
6751 space = isl_space_move_dims(space, isl_dim_set, 0,
6752 isl_dim_param, 0, nparam);
6753 set = isl_set_reset_space(set, space);
6754 set = isl_set_preimage(set, mat);
6755 nparam = isl_set_dim(set, isl_dim_out);
6756 space = isl_set_get_space(set);
6757 space = isl_space_move_dims(space, isl_dim_param, 0,
6758 isl_dim_out, 0, nparam);
6759 set = isl_set_reset_space(set, space);
6760 return set;
6761 error:
6762 isl_mat_free(mat);
6763 isl_set_free(set);
6764 return NULL;
6767 /* Intersect the basic set "bset" with the affine space specified by the
6768 * equalities in "eq".
6770 static struct isl_basic_set *basic_set_append_equalities(
6771 struct isl_basic_set *bset, struct isl_mat *eq)
6773 int i, k;
6774 unsigned len;
6776 if (!bset || !eq)
6777 goto error;
6779 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6780 eq->n_row, 0);
6781 if (!bset)
6782 goto error;
6784 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6785 for (i = 0; i < eq->n_row; ++i) {
6786 k = isl_basic_set_alloc_equality(bset);
6787 if (k < 0)
6788 goto error;
6789 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6790 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6792 isl_mat_free(eq);
6794 bset = isl_basic_set_gauss(bset, NULL);
6795 bset = isl_basic_set_finalize(bset);
6797 return bset;
6798 error:
6799 isl_mat_free(eq);
6800 isl_basic_set_free(bset);
6801 return NULL;
6804 /* Intersect the set "set" with the affine space specified by the
6805 * equalities in "eq".
6807 static struct isl_set *set_append_equalities(struct isl_set *set,
6808 struct isl_mat *eq)
6810 int i;
6812 if (!set || !eq)
6813 goto error;
6815 for (i = 0; i < set->n; ++i) {
6816 set->p[i] = basic_set_append_equalities(set->p[i],
6817 isl_mat_copy(eq));
6818 if (!set->p[i])
6819 goto error;
6821 isl_mat_free(eq);
6822 return set;
6823 error:
6824 isl_mat_free(eq);
6825 isl_set_free(set);
6826 return NULL;
6829 /* Given a basic set "bset" that only involves parameters and existentially
6830 * quantified variables, return the index of the first equality
6831 * that only involves parameters. If there is no such equality then
6832 * return bset->n_eq.
6834 * This function assumes that isl_basic_set_gauss has been called on "bset".
6836 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6838 int i, j;
6839 unsigned nparam, n_div;
6841 if (!bset)
6842 return -1;
6844 nparam = isl_basic_set_dim(bset, isl_dim_param);
6845 n_div = isl_basic_set_dim(bset, isl_dim_div);
6847 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6848 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6849 ++i;
6852 return i;
6855 /* Compute an explicit representation for the existentially quantified
6856 * variables in "bset" by computing the "minimal value" of the set
6857 * variables. Since there are no set variables, the computation of
6858 * the minimal value essentially computes an explicit representation
6859 * of the non-empty part(s) of "bset".
6861 * The input only involves parameters and existentially quantified variables.
6862 * All equalities among parameters have been removed.
6864 * Since the existentially quantified variables in the result are in general
6865 * going to be different from those in the input, we first replace
6866 * them by the minimal number of variables based on their equalities.
6867 * This should simplify the parametric integer programming.
6869 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6871 isl_morph *morph1, *morph2;
6872 isl_set *set;
6873 unsigned n;
6875 if (!bset)
6876 return NULL;
6877 if (bset->n_eq == 0)
6878 return isl_basic_set_lexmin_compute_divs(bset);
6880 morph1 = isl_basic_set_parameter_compression(bset);
6881 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6882 bset = isl_basic_set_lift(bset);
6883 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6884 bset = isl_morph_basic_set(morph2, bset);
6885 n = isl_basic_set_dim(bset, isl_dim_set);
6886 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6888 set = isl_basic_set_lexmin_compute_divs(bset);
6890 set = isl_morph_set(isl_morph_inverse(morph1), set);
6892 return set;
6895 /* Project the given basic set onto its parameter domain, possibly introducing
6896 * new, explicit, existential variables in the constraints.
6897 * The input has parameters and (possibly implicit) existential variables.
6898 * The output has the same parameters, but only
6899 * explicit existentially quantified variables.
6901 * The actual projection is performed by pip, but pip doesn't seem
6902 * to like equalities very much, so we first remove the equalities
6903 * among the parameters by performing a variable compression on
6904 * the parameters. Afterward, an inverse transformation is performed
6905 * and the equalities among the parameters are inserted back in.
6907 * The variable compression on the parameters may uncover additional
6908 * equalities that were only implicit before. We therefore check
6909 * if there are any new parameter equalities in the result and
6910 * if so recurse. The removal of parameter equalities is required
6911 * for the parameter compression performed by base_compute_divs.
6913 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6915 int i;
6916 struct isl_mat *eq;
6917 struct isl_mat *T, *T2;
6918 struct isl_set *set;
6919 unsigned nparam;
6921 bset = isl_basic_set_cow(bset);
6922 if (!bset)
6923 return NULL;
6925 if (bset->n_eq == 0)
6926 return base_compute_divs(bset);
6928 bset = isl_basic_set_gauss(bset, NULL);
6929 if (!bset)
6930 return NULL;
6931 if (isl_basic_set_plain_is_empty(bset))
6932 return isl_set_from_basic_set(bset);
6934 i = first_parameter_equality(bset);
6935 if (i == bset->n_eq)
6936 return base_compute_divs(bset);
6938 nparam = isl_basic_set_dim(bset, isl_dim_param);
6939 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6940 0, 1 + nparam);
6941 eq = isl_mat_cow(eq);
6942 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6943 if (T && T->n_col == 0) {
6944 isl_mat_free(T);
6945 isl_mat_free(T2);
6946 isl_mat_free(eq);
6947 bset = isl_basic_set_set_to_empty(bset);
6948 return isl_set_from_basic_set(bset);
6950 bset = basic_set_parameter_preimage(bset, T);
6952 i = first_parameter_equality(bset);
6953 if (!bset)
6954 set = NULL;
6955 else if (i == bset->n_eq)
6956 set = base_compute_divs(bset);
6957 else
6958 set = parameter_compute_divs(bset);
6959 set = set_parameter_preimage(set, T2);
6960 set = set_append_equalities(set, eq);
6961 return set;
6964 /* Insert the divs from "ls" before those of "bmap".
6966 * The number of columns is not changed, which means that the last
6967 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6968 * The caller is responsible for removing the same number of dimensions
6969 * from the space of "bmap".
6971 static __isl_give isl_basic_map *insert_divs_from_local_space(
6972 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6974 int i;
6975 int n_div;
6976 int old_n_div;
6978 n_div = isl_local_space_dim(ls, isl_dim_div);
6979 if (n_div == 0)
6980 return bmap;
6982 old_n_div = bmap->n_div;
6983 bmap = insert_div_rows(bmap, n_div);
6984 if (!bmap)
6985 return NULL;
6987 for (i = 0; i < n_div; ++i) {
6988 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6989 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6992 return bmap;
6995 /* Replace the space of "bmap" by the space and divs of "ls".
6997 * If "ls" has any divs, then we simplify the result since we may
6998 * have discovered some additional equalities that could simplify
6999 * the div expressions.
7001 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7002 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7004 int n_div;
7006 bmap = isl_basic_map_cow(bmap);
7007 if (!bmap || !ls)
7008 goto error;
7010 n_div = isl_local_space_dim(ls, isl_dim_div);
7011 bmap = insert_divs_from_local_space(bmap, ls);
7012 if (!bmap)
7013 goto error;
7015 isl_space_free(bmap->dim);
7016 bmap->dim = isl_local_space_get_space(ls);
7017 if (!bmap->dim)
7018 goto error;
7020 isl_local_space_free(ls);
7021 if (n_div > 0)
7022 bmap = isl_basic_map_simplify(bmap);
7023 bmap = isl_basic_map_finalize(bmap);
7024 return bmap;
7025 error:
7026 isl_basic_map_free(bmap);
7027 isl_local_space_free(ls);
7028 return NULL;
7031 /* Replace the space of "map" by the space and divs of "ls".
7033 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7034 __isl_take isl_local_space *ls)
7036 int i;
7038 map = isl_map_cow(map);
7039 if (!map || !ls)
7040 goto error;
7042 for (i = 0; i < map->n; ++i) {
7043 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7044 isl_local_space_copy(ls));
7045 if (!map->p[i])
7046 goto error;
7048 isl_space_free(map->dim);
7049 map->dim = isl_local_space_get_space(ls);
7050 if (!map->dim)
7051 goto error;
7053 isl_local_space_free(ls);
7054 return map;
7055 error:
7056 isl_local_space_free(ls);
7057 isl_map_free(map);
7058 return NULL;
7061 /* Compute an explicit representation for the existentially
7062 * quantified variables for which do not know any explicit representation yet.
7064 * We first sort the existentially quantified variables so that the
7065 * existentially quantified variables for which we already have an explicit
7066 * representation are placed before those for which we do not.
7067 * The input dimensions, the output dimensions and the existentially
7068 * quantified variables for which we already have an explicit
7069 * representation are then turned into parameters.
7070 * compute_divs returns a map with the same parameters and
7071 * no input or output dimensions and the dimension specification
7072 * is reset to that of the input, including the existentially quantified
7073 * variables for which we already had an explicit representation.
7075 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7077 struct isl_basic_set *bset;
7078 struct isl_set *set;
7079 struct isl_map *map;
7080 isl_space *dim;
7081 isl_local_space *ls;
7082 unsigned nparam;
7083 unsigned n_in;
7084 unsigned n_out;
7085 int n_known;
7086 int i;
7088 bmap = isl_basic_map_sort_divs(bmap);
7089 bmap = isl_basic_map_cow(bmap);
7090 if (!bmap)
7091 return NULL;
7093 n_known = isl_basic_map_first_unknown_div(bmap);
7094 if (n_known < 0)
7095 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7097 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7098 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7099 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7100 dim = isl_space_set_alloc(bmap->ctx,
7101 nparam + n_in + n_out + n_known, 0);
7102 if (!dim)
7103 goto error;
7105 ls = isl_basic_map_get_local_space(bmap);
7106 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7107 n_known, bmap->n_div - n_known);
7108 if (n_known > 0) {
7109 for (i = n_known; i < bmap->n_div; ++i)
7110 swap_div(bmap, i - n_known, i);
7111 bmap->n_div -= n_known;
7112 bmap->extra -= n_known;
7114 bmap = isl_basic_map_reset_space(bmap, dim);
7115 bset = bset_from_bmap(bmap);
7117 set = parameter_compute_divs(bset);
7118 map = set_to_map(set);
7119 map = replace_space_by_local_space(map, ls);
7121 return map;
7122 error:
7123 isl_basic_map_free(bmap);
7124 return NULL;
7127 /* Remove the explicit representation of local variable "div",
7128 * if there is any.
7130 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7131 __isl_take isl_basic_map *bmap, int div)
7133 isl_bool unknown;
7135 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7136 if (unknown < 0)
7137 return isl_basic_map_free(bmap);
7138 if (unknown)
7139 return bmap;
7141 bmap = isl_basic_map_cow(bmap);
7142 if (!bmap)
7143 return NULL;
7144 isl_int_set_si(bmap->div[div][0], 0);
7145 return bmap;
7148 /* Is local variable "div" of "bmap" marked as not having an explicit
7149 * representation?
7150 * Note that even if "div" is not marked in this way and therefore
7151 * has an explicit representation, this representation may still
7152 * depend (indirectly) on other local variables that do not
7153 * have an explicit representation.
7155 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7156 int div)
7158 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7159 return isl_bool_error;
7160 return isl_int_is_zero(bmap->div[div][0]);
7163 /* Return the position of the first local variable that does not
7164 * have an explicit representation.
7165 * Return the total number of local variables if they all have
7166 * an explicit representation.
7167 * Return -1 on error.
7169 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7171 int i;
7173 if (!bmap)
7174 return -1;
7176 for (i = 0; i < bmap->n_div; ++i) {
7177 if (!isl_basic_map_div_is_known(bmap, i))
7178 return i;
7180 return bmap->n_div;
7183 /* Return the position of the first local variable that does not
7184 * have an explicit representation.
7185 * Return the total number of local variables if they all have
7186 * an explicit representation.
7187 * Return -1 on error.
7189 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7191 return isl_basic_map_first_unknown_div(bset);
7194 /* Does "bmap" have an explicit representation for all local variables?
7196 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7198 int first, n;
7200 n = isl_basic_map_dim(bmap, isl_dim_div);
7201 first = isl_basic_map_first_unknown_div(bmap);
7202 if (first < 0)
7203 return isl_bool_error;
7204 return first == n;
7207 /* Do all basic maps in "map" have an explicit representation
7208 * for all local variables?
7210 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7212 int i;
7214 if (!map)
7215 return isl_bool_error;
7217 for (i = 0; i < map->n; ++i) {
7218 int known = isl_basic_map_divs_known(map->p[i]);
7219 if (known <= 0)
7220 return known;
7223 return isl_bool_true;
7226 /* If bmap contains any unknown divs, then compute explicit
7227 * expressions for them. However, this computation may be
7228 * quite expensive, so first try to remove divs that aren't
7229 * strictly needed.
7231 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7233 int known;
7234 struct isl_map *map;
7236 known = isl_basic_map_divs_known(bmap);
7237 if (known < 0)
7238 goto error;
7239 if (known)
7240 return isl_map_from_basic_map(bmap);
7242 bmap = isl_basic_map_drop_redundant_divs(bmap);
7244 known = isl_basic_map_divs_known(bmap);
7245 if (known < 0)
7246 goto error;
7247 if (known)
7248 return isl_map_from_basic_map(bmap);
7250 map = compute_divs(bmap);
7251 return map;
7252 error:
7253 isl_basic_map_free(bmap);
7254 return NULL;
7257 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7259 int i;
7260 int known;
7261 struct isl_map *res;
7263 if (!map)
7264 return NULL;
7265 if (map->n == 0)
7266 return map;
7268 known = isl_map_divs_known(map);
7269 if (known < 0) {
7270 isl_map_free(map);
7271 return NULL;
7273 if (known)
7274 return map;
7276 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7277 for (i = 1 ; i < map->n; ++i) {
7278 struct isl_map *r2;
7279 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7280 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7281 res = isl_map_union_disjoint(res, r2);
7282 else
7283 res = isl_map_union(res, r2);
7285 isl_map_free(map);
7287 return res;
7290 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7292 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7295 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7297 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7300 struct isl_set *isl_map_domain(struct isl_map *map)
7302 int i;
7303 struct isl_set *set;
7305 if (!map)
7306 goto error;
7308 map = isl_map_cow(map);
7309 if (!map)
7310 return NULL;
7312 set = set_from_map(map);
7313 set->dim = isl_space_domain(set->dim);
7314 if (!set->dim)
7315 goto error;
7316 for (i = 0; i < map->n; ++i) {
7317 set->p[i] = isl_basic_map_domain(map->p[i]);
7318 if (!set->p[i])
7319 goto error;
7321 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7322 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7323 return set;
7324 error:
7325 isl_map_free(map);
7326 return NULL;
7329 /* Return the union of "map1" and "map2", where we assume for now that
7330 * "map1" and "map2" are disjoint. Note that the basic maps inside
7331 * "map1" or "map2" may not be disjoint from each other.
7332 * Also note that this function is also called from isl_map_union,
7333 * which takes care of handling the situation where "map1" and "map2"
7334 * may not be disjoint.
7336 * If one of the inputs is empty, we can simply return the other input.
7337 * Similarly, if one of the inputs is universal, then it is equal to the union.
7339 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7340 __isl_take isl_map *map2)
7342 int i;
7343 unsigned flags = 0;
7344 struct isl_map *map = NULL;
7345 int is_universe;
7347 if (!map1 || !map2)
7348 goto error;
7350 if (!isl_space_is_equal(map1->dim, map2->dim))
7351 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7352 "spaces don't match", goto error);
7354 if (map1->n == 0) {
7355 isl_map_free(map1);
7356 return map2;
7358 if (map2->n == 0) {
7359 isl_map_free(map2);
7360 return map1;
7363 is_universe = isl_map_plain_is_universe(map1);
7364 if (is_universe < 0)
7365 goto error;
7366 if (is_universe) {
7367 isl_map_free(map2);
7368 return map1;
7371 is_universe = isl_map_plain_is_universe(map2);
7372 if (is_universe < 0)
7373 goto error;
7374 if (is_universe) {
7375 isl_map_free(map1);
7376 return map2;
7379 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7380 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7381 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7383 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7384 map1->n + map2->n, flags);
7385 if (!map)
7386 goto error;
7387 for (i = 0; i < map1->n; ++i) {
7388 map = isl_map_add_basic_map(map,
7389 isl_basic_map_copy(map1->p[i]));
7390 if (!map)
7391 goto error;
7393 for (i = 0; i < map2->n; ++i) {
7394 map = isl_map_add_basic_map(map,
7395 isl_basic_map_copy(map2->p[i]));
7396 if (!map)
7397 goto error;
7399 isl_map_free(map1);
7400 isl_map_free(map2);
7401 return map;
7402 error:
7403 isl_map_free(map);
7404 isl_map_free(map1);
7405 isl_map_free(map2);
7406 return NULL;
7409 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7410 * guaranteed to be disjoint by the caller.
7412 * Note that this functions is called from within isl_map_make_disjoint,
7413 * so we have to be careful not to touch the constraints of the inputs
7414 * in any way.
7416 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7417 __isl_take isl_map *map2)
7419 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7422 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7423 * not be disjoint. The parameters are assumed to have been aligned.
7425 * We currently simply call map_union_disjoint, the internal operation
7426 * of which does not really depend on the inputs being disjoint.
7427 * If the result contains more than one basic map, then we clear
7428 * the disjoint flag since the result may contain basic maps from
7429 * both inputs and these are not guaranteed to be disjoint.
7431 * As a special case, if "map1" and "map2" are obviously equal,
7432 * then we simply return "map1".
7434 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7435 __isl_take isl_map *map2)
7437 int equal;
7439 if (!map1 || !map2)
7440 goto error;
7442 equal = isl_map_plain_is_equal(map1, map2);
7443 if (equal < 0)
7444 goto error;
7445 if (equal) {
7446 isl_map_free(map2);
7447 return map1;
7450 map1 = map_union_disjoint(map1, map2);
7451 if (!map1)
7452 return NULL;
7453 if (map1->n > 1)
7454 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7455 return map1;
7456 error:
7457 isl_map_free(map1);
7458 isl_map_free(map2);
7459 return NULL;
7462 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7463 * not be disjoint.
7465 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7466 __isl_take isl_map *map2)
7468 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7471 struct isl_set *isl_set_union_disjoint(
7472 struct isl_set *set1, struct isl_set *set2)
7474 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7475 set_to_map(set2)));
7478 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7480 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7483 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7484 * the results.
7486 * "map" and "set" are assumed to be compatible and non-NULL.
7488 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7489 __isl_take isl_set *set,
7490 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7491 __isl_take isl_basic_set *bset))
7493 unsigned flags = 0;
7494 struct isl_map *result;
7495 int i, j;
7497 if (isl_set_plain_is_universe(set)) {
7498 isl_set_free(set);
7499 return map;
7502 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7503 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7504 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7506 result = isl_map_alloc_space(isl_space_copy(map->dim),
7507 map->n * set->n, flags);
7508 for (i = 0; result && i < map->n; ++i)
7509 for (j = 0; j < set->n; ++j) {
7510 result = isl_map_add_basic_map(result,
7511 fn(isl_basic_map_copy(map->p[i]),
7512 isl_basic_set_copy(set->p[j])));
7513 if (!result)
7514 break;
7517 isl_map_free(map);
7518 isl_set_free(set);
7519 return result;
7522 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7523 __isl_take isl_set *set)
7525 if (!map || !set)
7526 goto error;
7528 if (!isl_map_compatible_range(map, set))
7529 isl_die(set->ctx, isl_error_invalid,
7530 "incompatible spaces", goto error);
7532 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7533 error:
7534 isl_map_free(map);
7535 isl_set_free(set);
7536 return NULL;
7539 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7540 __isl_take isl_set *set)
7542 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7545 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7546 __isl_take isl_set *set)
7548 if (!map || !set)
7549 goto error;
7551 if (!isl_map_compatible_domain(map, set))
7552 isl_die(set->ctx, isl_error_invalid,
7553 "incompatible spaces", goto error);
7555 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7556 error:
7557 isl_map_free(map);
7558 isl_set_free(set);
7559 return NULL;
7562 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7563 __isl_take isl_set *set)
7565 return isl_map_align_params_map_map_and(map, set,
7566 &map_intersect_domain);
7569 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7570 __isl_take isl_map *map2)
7572 if (!map1 || !map2)
7573 goto error;
7574 map1 = isl_map_reverse(map1);
7575 map1 = isl_map_apply_range(map1, map2);
7576 return isl_map_reverse(map1);
7577 error:
7578 isl_map_free(map1);
7579 isl_map_free(map2);
7580 return NULL;
7583 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7584 __isl_take isl_map *map2)
7586 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7589 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7590 __isl_take isl_map *map2)
7592 isl_space *dim_result;
7593 struct isl_map *result;
7594 int i, j;
7596 if (!map1 || !map2)
7597 goto error;
7599 dim_result = isl_space_join(isl_space_copy(map1->dim),
7600 isl_space_copy(map2->dim));
7602 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7603 if (!result)
7604 goto error;
7605 for (i = 0; i < map1->n; ++i)
7606 for (j = 0; j < map2->n; ++j) {
7607 result = isl_map_add_basic_map(result,
7608 isl_basic_map_apply_range(
7609 isl_basic_map_copy(map1->p[i]),
7610 isl_basic_map_copy(map2->p[j])));
7611 if (!result)
7612 goto error;
7614 isl_map_free(map1);
7615 isl_map_free(map2);
7616 if (result && result->n <= 1)
7617 ISL_F_SET(result, ISL_MAP_DISJOINT);
7618 return result;
7619 error:
7620 isl_map_free(map1);
7621 isl_map_free(map2);
7622 return NULL;
7625 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7626 __isl_take isl_map *map2)
7628 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7632 * returns range - domain
7634 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7636 isl_space *target_space;
7637 struct isl_basic_set *bset;
7638 unsigned dim;
7639 unsigned nparam;
7640 int i;
7642 if (!bmap)
7643 goto error;
7644 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7645 bmap->dim, isl_dim_out),
7646 goto error);
7647 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7648 dim = isl_basic_map_n_in(bmap);
7649 nparam = isl_basic_map_n_param(bmap);
7650 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7651 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7652 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7653 for (i = 0; i < dim; ++i) {
7654 int j = isl_basic_map_alloc_equality(bmap);
7655 if (j < 0) {
7656 bmap = isl_basic_map_free(bmap);
7657 break;
7659 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7660 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7661 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7662 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7664 bset = isl_basic_map_domain(bmap);
7665 bset = isl_basic_set_reset_space(bset, target_space);
7666 return bset;
7667 error:
7668 isl_basic_map_free(bmap);
7669 return NULL;
7673 * returns range - domain
7675 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7677 int i;
7678 isl_space *dim;
7679 struct isl_set *result;
7681 if (!map)
7682 return NULL;
7684 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7685 map->dim, isl_dim_out),
7686 goto error);
7687 dim = isl_map_get_space(map);
7688 dim = isl_space_domain(dim);
7689 result = isl_set_alloc_space(dim, map->n, 0);
7690 if (!result)
7691 goto error;
7692 for (i = 0; i < map->n; ++i)
7693 result = isl_set_add_basic_set(result,
7694 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7695 isl_map_free(map);
7696 return result;
7697 error:
7698 isl_map_free(map);
7699 return NULL;
7703 * returns [domain -> range] -> range - domain
7705 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7706 __isl_take isl_basic_map *bmap)
7708 int i, k;
7709 isl_space *dim;
7710 isl_basic_map *domain;
7711 int nparam, n;
7712 unsigned total;
7714 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7715 bmap->dim, isl_dim_out))
7716 isl_die(bmap->ctx, isl_error_invalid,
7717 "domain and range don't match", goto error);
7719 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7720 n = isl_basic_map_dim(bmap, isl_dim_in);
7722 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7723 domain = isl_basic_map_universe(dim);
7725 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7726 bmap = isl_basic_map_apply_range(bmap, domain);
7727 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7729 total = isl_basic_map_total_dim(bmap);
7731 for (i = 0; i < n; ++i) {
7732 k = isl_basic_map_alloc_equality(bmap);
7733 if (k < 0)
7734 goto error;
7735 isl_seq_clr(bmap->eq[k], 1 + total);
7736 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7737 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7738 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7741 bmap = isl_basic_map_gauss(bmap, NULL);
7742 return isl_basic_map_finalize(bmap);
7743 error:
7744 isl_basic_map_free(bmap);
7745 return NULL;
7749 * returns [domain -> range] -> range - domain
7751 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7753 int i;
7754 isl_space *domain_dim;
7756 if (!map)
7757 return NULL;
7759 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7760 map->dim, isl_dim_out))
7761 isl_die(map->ctx, isl_error_invalid,
7762 "domain and range don't match", goto error);
7764 map = isl_map_cow(map);
7765 if (!map)
7766 return NULL;
7768 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7769 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7770 map->dim = isl_space_join(map->dim, domain_dim);
7771 if (!map->dim)
7772 goto error;
7773 for (i = 0; i < map->n; ++i) {
7774 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7775 if (!map->p[i])
7776 goto error;
7778 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7779 return map;
7780 error:
7781 isl_map_free(map);
7782 return NULL;
7785 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7787 struct isl_basic_map *bmap;
7788 unsigned nparam;
7789 unsigned dim;
7790 int i;
7792 if (!dims)
7793 return NULL;
7795 nparam = dims->nparam;
7796 dim = dims->n_out;
7797 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7798 if (!bmap)
7799 goto error;
7801 for (i = 0; i < dim; ++i) {
7802 int j = isl_basic_map_alloc_equality(bmap);
7803 if (j < 0)
7804 goto error;
7805 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7806 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7807 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7809 return isl_basic_map_finalize(bmap);
7810 error:
7811 isl_basic_map_free(bmap);
7812 return NULL;
7815 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7817 if (!dim)
7818 return NULL;
7819 if (dim->n_in != dim->n_out)
7820 isl_die(dim->ctx, isl_error_invalid,
7821 "number of input and output dimensions needs to be "
7822 "the same", goto error);
7823 return basic_map_identity(dim);
7824 error:
7825 isl_space_free(dim);
7826 return NULL;
7829 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7831 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7834 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7836 isl_space *dim = isl_set_get_space(set);
7837 isl_map *id;
7838 id = isl_map_identity(isl_space_map_from_set(dim));
7839 return isl_map_intersect_range(id, set);
7842 /* Construct a basic set with all set dimensions having only non-negative
7843 * values.
7845 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7846 __isl_take isl_space *space)
7848 int i;
7849 unsigned nparam;
7850 unsigned dim;
7851 struct isl_basic_set *bset;
7853 if (!space)
7854 return NULL;
7855 nparam = space->nparam;
7856 dim = space->n_out;
7857 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7858 if (!bset)
7859 return NULL;
7860 for (i = 0; i < dim; ++i) {
7861 int k = isl_basic_set_alloc_inequality(bset);
7862 if (k < 0)
7863 goto error;
7864 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7865 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7867 return bset;
7868 error:
7869 isl_basic_set_free(bset);
7870 return NULL;
7873 /* Construct the half-space x_pos >= 0.
7875 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7876 int pos)
7878 int k;
7879 isl_basic_set *nonneg;
7881 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7882 k = isl_basic_set_alloc_inequality(nonneg);
7883 if (k < 0)
7884 goto error;
7885 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7886 isl_int_set_si(nonneg->ineq[k][pos], 1);
7888 return isl_basic_set_finalize(nonneg);
7889 error:
7890 isl_basic_set_free(nonneg);
7891 return NULL;
7894 /* Construct the half-space x_pos <= -1.
7896 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7898 int k;
7899 isl_basic_set *neg;
7901 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7902 k = isl_basic_set_alloc_inequality(neg);
7903 if (k < 0)
7904 goto error;
7905 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7906 isl_int_set_si(neg->ineq[k][0], -1);
7907 isl_int_set_si(neg->ineq[k][pos], -1);
7909 return isl_basic_set_finalize(neg);
7910 error:
7911 isl_basic_set_free(neg);
7912 return NULL;
7915 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7916 enum isl_dim_type type, unsigned first, unsigned n)
7918 int i;
7919 unsigned offset;
7920 isl_basic_set *nonneg;
7921 isl_basic_set *neg;
7923 if (!set)
7924 return NULL;
7925 if (n == 0)
7926 return set;
7928 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7930 offset = pos(set->dim, type);
7931 for (i = 0; i < n; ++i) {
7932 nonneg = nonneg_halfspace(isl_set_get_space(set),
7933 offset + first + i);
7934 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7936 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7939 return set;
7940 error:
7941 isl_set_free(set);
7942 return NULL;
7945 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7946 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7947 void *user)
7949 isl_set *half;
7951 if (!set)
7952 return -1;
7953 if (isl_set_plain_is_empty(set)) {
7954 isl_set_free(set);
7955 return 0;
7957 if (first == len)
7958 return fn(set, signs, user);
7960 signs[first] = 1;
7961 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7962 1 + first));
7963 half = isl_set_intersect(half, isl_set_copy(set));
7964 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7965 goto error;
7967 signs[first] = -1;
7968 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7969 1 + first));
7970 half = isl_set_intersect(half, set);
7971 return foreach_orthant(half, signs, first + 1, len, fn, user);
7972 error:
7973 isl_set_free(set);
7974 return -1;
7977 /* Call "fn" on the intersections of "set" with each of the orthants
7978 * (except for obviously empty intersections). The orthant is identified
7979 * by the signs array, with each entry having value 1 or -1 according
7980 * to the sign of the corresponding variable.
7982 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7983 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7984 void *user)
7986 unsigned nparam;
7987 unsigned nvar;
7988 int *signs;
7989 int r;
7991 if (!set)
7992 return -1;
7993 if (isl_set_plain_is_empty(set))
7994 return 0;
7996 nparam = isl_set_dim(set, isl_dim_param);
7997 nvar = isl_set_dim(set, isl_dim_set);
7999 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8001 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8002 fn, user);
8004 free(signs);
8006 return r;
8009 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8011 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8014 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8015 __isl_keep isl_basic_map *bmap2)
8017 int is_subset;
8018 struct isl_map *map1;
8019 struct isl_map *map2;
8021 if (!bmap1 || !bmap2)
8022 return isl_bool_error;
8024 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8025 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8027 is_subset = isl_map_is_subset(map1, map2);
8029 isl_map_free(map1);
8030 isl_map_free(map2);
8032 return is_subset;
8035 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8036 __isl_keep isl_basic_set *bset2)
8038 return isl_basic_map_is_subset(bset1, bset2);
8041 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8042 __isl_keep isl_basic_map *bmap2)
8044 isl_bool is_subset;
8046 if (!bmap1 || !bmap2)
8047 return isl_bool_error;
8048 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8049 if (is_subset != isl_bool_true)
8050 return is_subset;
8051 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8052 return is_subset;
8055 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8056 __isl_keep isl_basic_set *bset2)
8058 return isl_basic_map_is_equal(
8059 bset_to_bmap(bset1), bset_to_bmap(bset2));
8062 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8064 int i;
8065 int is_empty;
8067 if (!map)
8068 return isl_bool_error;
8069 for (i = 0; i < map->n; ++i) {
8070 is_empty = isl_basic_map_is_empty(map->p[i]);
8071 if (is_empty < 0)
8072 return isl_bool_error;
8073 if (!is_empty)
8074 return isl_bool_false;
8076 return isl_bool_true;
8079 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8081 return map ? map->n == 0 : isl_bool_error;
8084 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8086 return set ? set->n == 0 : isl_bool_error;
8089 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8091 return isl_map_is_empty(set_to_map(set));
8094 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8096 if (!map1 || !map2)
8097 return -1;
8099 return isl_space_is_equal(map1->dim, map2->dim);
8102 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8104 if (!set1 || !set2)
8105 return -1;
8107 return isl_space_is_equal(set1->dim, set2->dim);
8110 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8112 isl_bool is_subset;
8114 if (!map1 || !map2)
8115 return isl_bool_error;
8116 is_subset = isl_map_is_subset(map1, map2);
8117 if (is_subset != isl_bool_true)
8118 return is_subset;
8119 is_subset = isl_map_is_subset(map2, map1);
8120 return is_subset;
8123 /* Is "map1" equal to "map2"?
8125 * First check if they are obviously equal.
8126 * If not, then perform a more detailed analysis.
8128 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8130 isl_bool equal;
8132 equal = isl_map_plain_is_equal(map1, map2);
8133 if (equal < 0 || equal)
8134 return equal;
8135 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8138 isl_bool isl_basic_map_is_strict_subset(
8139 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8141 isl_bool is_subset;
8143 if (!bmap1 || !bmap2)
8144 return isl_bool_error;
8145 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8146 if (is_subset != isl_bool_true)
8147 return is_subset;
8148 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8149 if (is_subset == isl_bool_error)
8150 return is_subset;
8151 return !is_subset;
8154 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8155 __isl_keep isl_map *map2)
8157 isl_bool is_subset;
8159 if (!map1 || !map2)
8160 return isl_bool_error;
8161 is_subset = isl_map_is_subset(map1, map2);
8162 if (is_subset != isl_bool_true)
8163 return is_subset;
8164 is_subset = isl_map_is_subset(map2, map1);
8165 if (is_subset == isl_bool_error)
8166 return is_subset;
8167 return !is_subset;
8170 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8171 __isl_keep isl_set *set2)
8173 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8176 /* Is "bmap" obviously equal to the universe with the same space?
8178 * That is, does it not have any constraints?
8180 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8182 if (!bmap)
8183 return isl_bool_error;
8184 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8187 /* Is "bset" obviously equal to the universe with the same space?
8189 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8191 return isl_basic_map_plain_is_universe(bset);
8194 /* If "c" does not involve any existentially quantified variables,
8195 * then set *univ to false and abort
8197 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8199 isl_bool *univ = user;
8200 unsigned n;
8202 n = isl_constraint_dim(c, isl_dim_div);
8203 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8204 isl_constraint_free(c);
8205 if (*univ < 0 || !*univ)
8206 return isl_stat_error;
8207 return isl_stat_ok;
8210 /* Is "bmap" equal to the universe with the same space?
8212 * First check if it is obviously equal to the universe.
8213 * If not and if there are any constraints not involving
8214 * existentially quantified variables, then it is certainly
8215 * not equal to the universe.
8216 * Otherwise, check if the universe is a subset of "bmap".
8218 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8220 isl_bool univ;
8221 isl_basic_map *test;
8223 univ = isl_basic_map_plain_is_universe(bmap);
8224 if (univ < 0 || univ)
8225 return univ;
8226 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8227 return isl_bool_false;
8228 univ = isl_bool_true;
8229 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8230 univ)
8231 return isl_bool_error;
8232 if (univ < 0 || !univ)
8233 return univ;
8234 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8235 univ = isl_basic_map_is_subset(test, bmap);
8236 isl_basic_map_free(test);
8237 return univ;
8240 /* Is "bset" equal to the universe with the same space?
8242 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8244 return isl_basic_map_is_universe(bset);
8247 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8249 int i;
8251 if (!map)
8252 return isl_bool_error;
8254 for (i = 0; i < map->n; ++i) {
8255 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8256 if (r < 0 || r)
8257 return r;
8260 return isl_bool_false;
8263 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8265 return isl_map_plain_is_universe(set_to_map(set));
8268 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8270 struct isl_basic_set *bset = NULL;
8271 struct isl_vec *sample = NULL;
8272 isl_bool empty, non_empty;
8274 if (!bmap)
8275 return isl_bool_error;
8277 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8278 return isl_bool_true;
8280 if (isl_basic_map_plain_is_universe(bmap))
8281 return isl_bool_false;
8283 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8284 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8285 copy = isl_basic_map_remove_redundancies(copy);
8286 empty = isl_basic_map_plain_is_empty(copy);
8287 isl_basic_map_free(copy);
8288 return empty;
8291 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8292 if (non_empty < 0)
8293 return isl_bool_error;
8294 if (non_empty)
8295 return isl_bool_false;
8296 isl_vec_free(bmap->sample);
8297 bmap->sample = NULL;
8298 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8299 if (!bset)
8300 return isl_bool_error;
8301 sample = isl_basic_set_sample_vec(bset);
8302 if (!sample)
8303 return isl_bool_error;
8304 empty = sample->size == 0;
8305 isl_vec_free(bmap->sample);
8306 bmap->sample = sample;
8307 if (empty)
8308 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8310 return empty;
8313 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8315 if (!bmap)
8316 return isl_bool_error;
8317 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8320 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8322 if (!bset)
8323 return isl_bool_error;
8324 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8327 /* Is "bmap" known to be non-empty?
8329 * That is, is the cached sample still valid?
8331 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8333 unsigned total;
8335 if (!bmap)
8336 return isl_bool_error;
8337 if (!bmap->sample)
8338 return isl_bool_false;
8339 total = 1 + isl_basic_map_total_dim(bmap);
8340 if (bmap->sample->size != total)
8341 return isl_bool_false;
8342 return isl_basic_map_contains(bmap, bmap->sample);
8345 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8347 return isl_basic_map_is_empty(bset_to_bmap(bset));
8350 struct isl_map *isl_basic_map_union(
8351 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8353 struct isl_map *map;
8354 if (!bmap1 || !bmap2)
8355 goto error;
8357 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8359 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8360 if (!map)
8361 goto error;
8362 map = isl_map_add_basic_map(map, bmap1);
8363 map = isl_map_add_basic_map(map, bmap2);
8364 return map;
8365 error:
8366 isl_basic_map_free(bmap1);
8367 isl_basic_map_free(bmap2);
8368 return NULL;
8371 struct isl_set *isl_basic_set_union(
8372 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8374 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8375 bset_to_bmap(bset2)));
8378 /* Order divs such that any div only depends on previous divs */
8379 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8381 int i;
8382 unsigned off;
8384 if (!bmap)
8385 return NULL;
8387 off = isl_space_dim(bmap->dim, isl_dim_all);
8389 for (i = 0; i < bmap->n_div; ++i) {
8390 int pos;
8391 if (isl_int_is_zero(bmap->div[i][0]))
8392 continue;
8393 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8394 bmap->n_div-i);
8395 if (pos == -1)
8396 continue;
8397 if (pos == 0)
8398 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8399 "integer division depends on itself",
8400 return isl_basic_map_free(bmap));
8401 isl_basic_map_swap_div(bmap, i, i + pos);
8402 --i;
8404 return bmap;
8407 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8409 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8412 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8414 int i;
8416 if (!map)
8417 return 0;
8419 for (i = 0; i < map->n; ++i) {
8420 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8421 if (!map->p[i])
8422 goto error;
8425 return map;
8426 error:
8427 isl_map_free(map);
8428 return NULL;
8431 /* Sort the local variables of "bset".
8433 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8434 __isl_take isl_basic_set *bset)
8436 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8439 /* Apply the expansion computed by isl_merge_divs.
8440 * The expansion itself is given by "exp" while the resulting
8441 * list of divs is given by "div".
8443 * Move the integer divisions of "bmap" into the right position
8444 * according to "exp" and then introduce the additional integer
8445 * divisions, adding div constraints.
8446 * The moving should be done first to avoid moving coefficients
8447 * in the definitions of the extra integer divisions.
8449 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8450 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8452 int i, j;
8453 int n_div;
8455 bmap = isl_basic_map_cow(bmap);
8456 if (!bmap || !div)
8457 goto error;
8459 if (div->n_row < bmap->n_div)
8460 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8461 "not an expansion", goto error);
8463 n_div = bmap->n_div;
8464 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8465 div->n_row - n_div, 0,
8466 2 * (div->n_row - n_div));
8468 for (i = n_div; i < div->n_row; ++i)
8469 if (isl_basic_map_alloc_div(bmap) < 0)
8470 goto error;
8472 for (j = n_div - 1; j >= 0; --j) {
8473 if (exp[j] == j)
8474 break;
8475 isl_basic_map_swap_div(bmap, j, exp[j]);
8477 j = 0;
8478 for (i = 0; i < div->n_row; ++i) {
8479 if (j < n_div && exp[j] == i) {
8480 j++;
8481 } else {
8482 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8483 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8484 continue;
8485 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8486 goto error;
8490 isl_mat_free(div);
8491 return bmap;
8492 error:
8493 isl_basic_map_free(bmap);
8494 isl_mat_free(div);
8495 return NULL;
8498 /* Apply the expansion computed by isl_merge_divs.
8499 * The expansion itself is given by "exp" while the resulting
8500 * list of divs is given by "div".
8502 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8503 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8505 return isl_basic_map_expand_divs(bset, div, exp);
8508 /* Look for a div in dst that corresponds to the div "div" in src.
8509 * The divs before "div" in src and dst are assumed to be the same.
8511 * Returns -1 if no corresponding div was found and the position
8512 * of the corresponding div in dst otherwise.
8514 static int find_div(struct isl_basic_map *dst,
8515 struct isl_basic_map *src, unsigned div)
8517 int i;
8519 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8521 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8522 for (i = div; i < dst->n_div; ++i)
8523 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8524 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8525 dst->n_div - div) == -1)
8526 return i;
8527 return -1;
8530 /* Align the divs of "dst" to those of "src", adding divs from "src"
8531 * if needed. That is, make sure that the first src->n_div divs
8532 * of the result are equal to those of src.
8534 * The result is not finalized as by design it will have redundant
8535 * divs if any divs from "src" were copied.
8537 __isl_give isl_basic_map *isl_basic_map_align_divs(
8538 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8540 int i;
8541 int known, extended;
8542 unsigned total;
8544 if (!dst || !src)
8545 return isl_basic_map_free(dst);
8547 if (src->n_div == 0)
8548 return dst;
8550 known = isl_basic_map_divs_known(src);
8551 if (known < 0)
8552 return isl_basic_map_free(dst);
8553 if (!known)
8554 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8555 "some src divs are unknown",
8556 return isl_basic_map_free(dst));
8558 src = isl_basic_map_order_divs(src);
8560 extended = 0;
8561 total = isl_space_dim(src->dim, isl_dim_all);
8562 for (i = 0; i < src->n_div; ++i) {
8563 int j = find_div(dst, src, i);
8564 if (j < 0) {
8565 if (!extended) {
8566 int extra = src->n_div - i;
8567 dst = isl_basic_map_cow(dst);
8568 if (!dst)
8569 return NULL;
8570 dst = isl_basic_map_extend_space(dst,
8571 isl_space_copy(dst->dim),
8572 extra, 0, 2 * extra);
8573 extended = 1;
8575 j = isl_basic_map_alloc_div(dst);
8576 if (j < 0)
8577 return isl_basic_map_free(dst);
8578 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8579 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8580 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8581 return isl_basic_map_free(dst);
8583 if (j != i)
8584 isl_basic_map_swap_div(dst, i, j);
8586 return dst;
8589 struct isl_basic_set *isl_basic_set_align_divs(
8590 struct isl_basic_set *dst, struct isl_basic_set *src)
8592 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8593 bset_to_bmap(src)));
8596 struct isl_map *isl_map_align_divs(struct isl_map *map)
8598 int i;
8600 if (!map)
8601 return NULL;
8602 if (map->n == 0)
8603 return map;
8604 map = isl_map_compute_divs(map);
8605 map = isl_map_cow(map);
8606 if (!map)
8607 return NULL;
8609 for (i = 1; i < map->n; ++i)
8610 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8611 for (i = 1; i < map->n; ++i) {
8612 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8613 if (!map->p[i])
8614 return isl_map_free(map);
8617 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8618 return map;
8621 struct isl_set *isl_set_align_divs(struct isl_set *set)
8623 return set_from_map(isl_map_align_divs(set_to_map(set)));
8626 /* Align the divs of the basic maps in "map" to those
8627 * of the basic maps in "list", as well as to the other basic maps in "map".
8628 * The elements in "list" are assumed to have known divs.
8630 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8631 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8633 int i, n;
8635 map = isl_map_compute_divs(map);
8636 map = isl_map_cow(map);
8637 if (!map || !list)
8638 return isl_map_free(map);
8639 if (map->n == 0)
8640 return map;
8642 n = isl_basic_map_list_n_basic_map(list);
8643 for (i = 0; i < n; ++i) {
8644 isl_basic_map *bmap;
8646 bmap = isl_basic_map_list_get_basic_map(list, i);
8647 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8648 isl_basic_map_free(bmap);
8650 if (!map->p[0])
8651 return isl_map_free(map);
8653 return isl_map_align_divs(map);
8656 /* Align the divs of each element of "list" to those of "bmap".
8657 * Both "bmap" and the elements of "list" are assumed to have known divs.
8659 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8660 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8662 int i, n;
8664 if (!list || !bmap)
8665 return isl_basic_map_list_free(list);
8667 n = isl_basic_map_list_n_basic_map(list);
8668 for (i = 0; i < n; ++i) {
8669 isl_basic_map *bmap_i;
8671 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8672 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8673 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8676 return list;
8679 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8680 __isl_take isl_map *map)
8682 if (!set || !map)
8683 goto error;
8684 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8685 map = isl_map_intersect_domain(map, set);
8686 set = isl_map_range(map);
8687 return set;
8688 error:
8689 isl_set_free(set);
8690 isl_map_free(map);
8691 return NULL;
8694 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8695 __isl_take isl_map *map)
8697 return isl_map_align_params_map_map_and(set, map, &set_apply);
8700 /* There is no need to cow as removing empty parts doesn't change
8701 * the meaning of the set.
8703 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8705 int i;
8707 if (!map)
8708 return NULL;
8710 for (i = map->n - 1; i >= 0; --i)
8711 remove_if_empty(map, i);
8713 return map;
8716 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8718 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8721 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8723 struct isl_basic_map *bmap;
8724 if (!map || map->n == 0)
8725 return NULL;
8726 bmap = map->p[map->n-1];
8727 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8728 return isl_basic_map_copy(bmap);
8731 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8733 return map_copy_basic_map(map);
8736 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8738 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8741 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8742 __isl_keep isl_basic_map *bmap)
8744 int i;
8746 if (!map || !bmap)
8747 goto error;
8748 for (i = map->n-1; i >= 0; --i) {
8749 if (map->p[i] != bmap)
8750 continue;
8751 map = isl_map_cow(map);
8752 if (!map)
8753 goto error;
8754 isl_basic_map_free(map->p[i]);
8755 if (i != map->n-1) {
8756 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8757 map->p[i] = map->p[map->n-1];
8759 map->n--;
8760 return map;
8762 return map;
8763 error:
8764 isl_map_free(map);
8765 return NULL;
8768 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8769 __isl_keep isl_basic_map *bmap)
8771 return map_drop_basic_map(map, bmap);
8774 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8775 struct isl_basic_set *bset)
8777 return set_from_map(map_drop_basic_map(set_to_map(set),
8778 bset_to_bmap(bset)));
8781 /* Given two basic sets bset1 and bset2, compute the maximal difference
8782 * between the values of dimension pos in bset1 and those in bset2
8783 * for any common value of the parameters and dimensions preceding pos.
8785 static enum isl_lp_result basic_set_maximal_difference_at(
8786 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8787 int pos, isl_int *opt)
8789 isl_basic_map *bmap1;
8790 isl_basic_map *bmap2;
8791 struct isl_ctx *ctx;
8792 struct isl_vec *obj;
8793 unsigned total;
8794 unsigned nparam;
8795 unsigned dim1;
8796 enum isl_lp_result res;
8798 if (!bset1 || !bset2)
8799 return isl_lp_error;
8801 nparam = isl_basic_set_n_param(bset1);
8802 dim1 = isl_basic_set_n_dim(bset1);
8804 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8805 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8806 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8807 isl_dim_out, 0, pos);
8808 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8809 isl_dim_out, 0, pos);
8810 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8811 if (!bmap1)
8812 return isl_lp_error;
8814 total = isl_basic_map_total_dim(bmap1);
8815 ctx = bmap1->ctx;
8816 obj = isl_vec_alloc(ctx, 1 + total);
8817 if (!obj)
8818 goto error;
8819 isl_seq_clr(obj->block.data, 1 + total);
8820 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8821 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8822 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8823 opt, NULL, NULL);
8824 isl_basic_map_free(bmap1);
8825 isl_vec_free(obj);
8826 return res;
8827 error:
8828 isl_basic_map_free(bmap1);
8829 return isl_lp_error;
8832 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8833 * for any common value of the parameters and dimensions preceding pos
8834 * in both basic sets, the values of dimension pos in bset1 are
8835 * smaller or larger than those in bset2.
8837 * Returns
8838 * 1 if bset1 follows bset2
8839 * -1 if bset1 precedes bset2
8840 * 0 if bset1 and bset2 are incomparable
8841 * -2 if some error occurred.
8843 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8844 struct isl_basic_set *bset2, int pos)
8846 isl_int opt;
8847 enum isl_lp_result res;
8848 int cmp;
8850 isl_int_init(opt);
8852 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8854 if (res == isl_lp_empty)
8855 cmp = 0;
8856 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8857 res == isl_lp_unbounded)
8858 cmp = 1;
8859 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8860 cmp = -1;
8861 else
8862 cmp = -2;
8864 isl_int_clear(opt);
8865 return cmp;
8868 /* Given two basic sets bset1 and bset2, check whether
8869 * for any common value of the parameters and dimensions preceding pos
8870 * there is a value of dimension pos in bset1 that is larger
8871 * than a value of the same dimension in bset2.
8873 * Return
8874 * 1 if there exists such a pair
8875 * 0 if there is no such pair, but there is a pair of equal values
8876 * -1 otherwise
8877 * -2 if some error occurred.
8879 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8880 __isl_keep isl_basic_set *bset2, int pos)
8882 isl_int opt;
8883 enum isl_lp_result res;
8884 int cmp;
8886 isl_int_init(opt);
8888 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8890 if (res == isl_lp_empty)
8891 cmp = -1;
8892 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8893 res == isl_lp_unbounded)
8894 cmp = 1;
8895 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8896 cmp = -1;
8897 else if (res == isl_lp_ok)
8898 cmp = 0;
8899 else
8900 cmp = -2;
8902 isl_int_clear(opt);
8903 return cmp;
8906 /* Given two sets set1 and set2, check whether
8907 * for any common value of the parameters and dimensions preceding pos
8908 * there is a value of dimension pos in set1 that is larger
8909 * than a value of the same dimension in set2.
8911 * Return
8912 * 1 if there exists such a pair
8913 * 0 if there is no such pair, but there is a pair of equal values
8914 * -1 otherwise
8915 * -2 if some error occurred.
8917 int isl_set_follows_at(__isl_keep isl_set *set1,
8918 __isl_keep isl_set *set2, int pos)
8920 int i, j;
8921 int follows = -1;
8923 if (!set1 || !set2)
8924 return -2;
8926 for (i = 0; i < set1->n; ++i)
8927 for (j = 0; j < set2->n; ++j) {
8928 int f;
8929 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8930 if (f == 1 || f == -2)
8931 return f;
8932 if (f > follows)
8933 follows = f;
8936 return follows;
8939 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8940 unsigned pos, isl_int *val)
8942 int i;
8943 int d;
8944 unsigned total;
8946 if (!bmap)
8947 return -1;
8948 total = isl_basic_map_total_dim(bmap);
8949 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8950 for (; d+1 > pos; --d)
8951 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8952 break;
8953 if (d != pos)
8954 continue;
8955 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8956 return 0;
8957 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8958 return 0;
8959 if (!isl_int_is_one(bmap->eq[i][1+d]))
8960 return 0;
8961 if (val)
8962 isl_int_neg(*val, bmap->eq[i][0]);
8963 return 1;
8965 return 0;
8968 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8969 unsigned pos, isl_int *val)
8971 int i;
8972 isl_int v;
8973 isl_int tmp;
8974 int fixed;
8976 if (!map)
8977 return -1;
8978 if (map->n == 0)
8979 return 0;
8980 if (map->n == 1)
8981 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8982 isl_int_init(v);
8983 isl_int_init(tmp);
8984 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8985 for (i = 1; fixed == 1 && i < map->n; ++i) {
8986 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8987 if (fixed == 1 && isl_int_ne(tmp, v))
8988 fixed = 0;
8990 if (val)
8991 isl_int_set(*val, v);
8992 isl_int_clear(tmp);
8993 isl_int_clear(v);
8994 return fixed;
8997 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8998 unsigned pos, isl_int *val)
9000 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9001 pos, val);
9004 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
9005 isl_int *val)
9007 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
9010 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9011 enum isl_dim_type type, unsigned pos, isl_int *val)
9013 if (pos >= isl_basic_map_dim(bmap, type))
9014 return -1;
9015 return isl_basic_map_plain_has_fixed_var(bmap,
9016 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9019 /* If "bmap" obviously lies on a hyperplane where the given dimension
9020 * has a fixed value, then return that value.
9021 * Otherwise return NaN.
9023 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9024 __isl_keep isl_basic_map *bmap,
9025 enum isl_dim_type type, unsigned pos)
9027 isl_ctx *ctx;
9028 isl_val *v;
9029 int fixed;
9031 if (!bmap)
9032 return NULL;
9033 ctx = isl_basic_map_get_ctx(bmap);
9034 v = isl_val_alloc(ctx);
9035 if (!v)
9036 return NULL;
9037 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9038 if (fixed < 0)
9039 return isl_val_free(v);
9040 if (fixed) {
9041 isl_int_set_si(v->d, 1);
9042 return v;
9044 isl_val_free(v);
9045 return isl_val_nan(ctx);
9048 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
9049 enum isl_dim_type type, unsigned pos, isl_int *val)
9051 if (pos >= isl_map_dim(map, type))
9052 return -1;
9053 return isl_map_plain_has_fixed_var(map,
9054 map_offset(map, type) - 1 + pos, val);
9057 /* If "map" obviously lies on a hyperplane where the given dimension
9058 * has a fixed value, then return that value.
9059 * Otherwise return NaN.
9061 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9062 enum isl_dim_type type, unsigned pos)
9064 isl_ctx *ctx;
9065 isl_val *v;
9066 int fixed;
9068 if (!map)
9069 return NULL;
9070 ctx = isl_map_get_ctx(map);
9071 v = isl_val_alloc(ctx);
9072 if (!v)
9073 return NULL;
9074 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9075 if (fixed < 0)
9076 return isl_val_free(v);
9077 if (fixed) {
9078 isl_int_set_si(v->d, 1);
9079 return v;
9081 isl_val_free(v);
9082 return isl_val_nan(ctx);
9085 /* If "set" obviously lies on a hyperplane where the given dimension
9086 * has a fixed value, then return that value.
9087 * Otherwise return NaN.
9089 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9090 enum isl_dim_type type, unsigned pos)
9092 return isl_map_plain_get_val_if_fixed(set, type, pos);
9095 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
9096 enum isl_dim_type type, unsigned pos, isl_int *val)
9098 return isl_map_plain_is_fixed(set, type, pos, val);
9101 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9102 * then return this fixed value in *val.
9104 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9105 unsigned dim, isl_int *val)
9107 return isl_basic_set_plain_has_fixed_var(bset,
9108 isl_basic_set_n_param(bset) + dim, val);
9111 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9112 * then return this fixed value in *val.
9114 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
9115 unsigned dim, isl_int *val)
9117 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
9120 /* Check if input variable in has fixed value and if so and if val is not NULL,
9121 * then return this fixed value in *val.
9123 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
9124 unsigned in, isl_int *val)
9126 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
9129 /* Check if dimension dim has an (obvious) fixed lower bound and if so
9130 * and if val is not NULL, then return this lower bound in *val.
9132 int isl_basic_set_plain_dim_has_fixed_lower_bound(
9133 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
9135 int i, i_eq = -1, i_ineq = -1;
9136 isl_int *c;
9137 unsigned total;
9138 unsigned nparam;
9140 if (!bset)
9141 return -1;
9142 total = isl_basic_set_total_dim(bset);
9143 nparam = isl_basic_set_n_param(bset);
9144 for (i = 0; i < bset->n_eq; ++i) {
9145 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
9146 continue;
9147 if (i_eq != -1)
9148 return 0;
9149 i_eq = i;
9151 for (i = 0; i < bset->n_ineq; ++i) {
9152 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
9153 continue;
9154 if (i_eq != -1 || i_ineq != -1)
9155 return 0;
9156 i_ineq = i;
9158 if (i_eq == -1 && i_ineq == -1)
9159 return 0;
9160 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
9161 /* The coefficient should always be one due to normalization. */
9162 if (!isl_int_is_one(c[1+nparam+dim]))
9163 return 0;
9164 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
9165 return 0;
9166 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
9167 total - nparam - dim - 1) != -1)
9168 return 0;
9169 if (val)
9170 isl_int_neg(*val, c[0]);
9171 return 1;
9174 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9175 unsigned dim, isl_int *val)
9177 int i;
9178 isl_int v;
9179 isl_int tmp;
9180 int fixed;
9182 if (!set)
9183 return -1;
9184 if (set->n == 0)
9185 return 0;
9186 if (set->n == 1)
9187 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9188 dim, val);
9189 isl_int_init(v);
9190 isl_int_init(tmp);
9191 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9192 dim, &v);
9193 for (i = 1; fixed == 1 && i < set->n; ++i) {
9194 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9195 dim, &tmp);
9196 if (fixed == 1 && isl_int_ne(tmp, v))
9197 fixed = 0;
9199 if (val)
9200 isl_int_set(*val, v);
9201 isl_int_clear(tmp);
9202 isl_int_clear(v);
9203 return fixed;
9206 /* Return -1 if the constraint "c1" should be sorted before "c2"
9207 * and 1 if it should be sorted after "c2".
9208 * Return 0 if the two constraints are the same (up to the constant term).
9210 * In particular, if a constraint involves later variables than another
9211 * then it is sorted after this other constraint.
9212 * uset_gist depends on constraints without existentially quantified
9213 * variables sorting first.
9215 * For constraints that have the same latest variable, those
9216 * with the same coefficient for this latest variable (first in absolute value
9217 * and then in actual value) are grouped together.
9218 * This is useful for detecting pairs of constraints that can
9219 * be chained in their printed representation.
9221 * Finally, within a group, constraints are sorted according to
9222 * their coefficients (excluding the constant term).
9224 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9226 isl_int **c1 = (isl_int **) p1;
9227 isl_int **c2 = (isl_int **) p2;
9228 int l1, l2;
9229 unsigned size = *(unsigned *) arg;
9230 int cmp;
9232 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9233 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9235 if (l1 != l2)
9236 return l1 - l2;
9238 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9239 if (cmp != 0)
9240 return cmp;
9241 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9242 if (cmp != 0)
9243 return -cmp;
9245 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9248 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9249 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9250 * and 0 if the two constraints are the same (up to the constant term).
9252 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9253 isl_int *c1, isl_int *c2)
9255 unsigned total;
9257 if (!bmap)
9258 return -2;
9259 total = isl_basic_map_total_dim(bmap);
9260 return sort_constraint_cmp(&c1, &c2, &total);
9263 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9264 __isl_take isl_basic_map *bmap)
9266 unsigned total;
9268 if (!bmap)
9269 return NULL;
9270 if (bmap->n_ineq == 0)
9271 return bmap;
9272 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9273 return bmap;
9274 total = isl_basic_map_total_dim(bmap);
9275 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9276 &sort_constraint_cmp, &total) < 0)
9277 return isl_basic_map_free(bmap);
9278 return bmap;
9281 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9282 __isl_take isl_basic_set *bset)
9284 isl_basic_map *bmap = bset_to_bmap(bset);
9285 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9288 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9290 if (!bmap)
9291 return NULL;
9292 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9293 return bmap;
9294 bmap = isl_basic_map_remove_redundancies(bmap);
9295 bmap = isl_basic_map_sort_constraints(bmap);
9296 if (bmap)
9297 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9298 return bmap;
9301 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9303 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9306 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9307 const __isl_keep isl_basic_map *bmap2)
9309 int i, cmp;
9310 unsigned total;
9312 if (!bmap1 || !bmap2)
9313 return -1;
9315 if (bmap1 == bmap2)
9316 return 0;
9317 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9318 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9319 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9320 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9321 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9322 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9323 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9324 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9325 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9326 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9327 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9328 return 0;
9329 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9330 return 1;
9331 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9332 return -1;
9333 if (bmap1->n_eq != bmap2->n_eq)
9334 return bmap1->n_eq - bmap2->n_eq;
9335 if (bmap1->n_ineq != bmap2->n_ineq)
9336 return bmap1->n_ineq - bmap2->n_ineq;
9337 if (bmap1->n_div != bmap2->n_div)
9338 return bmap1->n_div - bmap2->n_div;
9339 total = isl_basic_map_total_dim(bmap1);
9340 for (i = 0; i < bmap1->n_eq; ++i) {
9341 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9342 if (cmp)
9343 return cmp;
9345 for (i = 0; i < bmap1->n_ineq; ++i) {
9346 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9347 if (cmp)
9348 return cmp;
9350 for (i = 0; i < bmap1->n_div; ++i) {
9351 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9352 if (cmp)
9353 return cmp;
9355 return 0;
9358 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9359 const __isl_keep isl_basic_set *bset2)
9361 return isl_basic_map_plain_cmp(bset1, bset2);
9364 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9366 int i, cmp;
9368 if (set1 == set2)
9369 return 0;
9370 if (set1->n != set2->n)
9371 return set1->n - set2->n;
9373 for (i = 0; i < set1->n; ++i) {
9374 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9375 if (cmp)
9376 return cmp;
9379 return 0;
9382 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9383 __isl_keep isl_basic_map *bmap2)
9385 if (!bmap1 || !bmap2)
9386 return isl_bool_error;
9387 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9390 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9391 __isl_keep isl_basic_set *bset2)
9393 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9394 bset_to_bmap(bset2));
9397 static int qsort_bmap_cmp(const void *p1, const void *p2)
9399 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9400 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9402 return isl_basic_map_plain_cmp(bmap1, bmap2);
9405 /* Sort the basic maps of "map" and remove duplicate basic maps.
9407 * While removing basic maps, we make sure that the basic maps remain
9408 * sorted because isl_map_normalize expects the basic maps of the result
9409 * to be sorted.
9411 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9413 int i, j;
9415 map = isl_map_remove_empty_parts(map);
9416 if (!map)
9417 return NULL;
9418 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9419 for (i = map->n - 1; i >= 1; --i) {
9420 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9421 continue;
9422 isl_basic_map_free(map->p[i-1]);
9423 for (j = i; j < map->n; ++j)
9424 map->p[j - 1] = map->p[j];
9425 map->n--;
9428 return map;
9431 /* Remove obvious duplicates among the basic maps of "map".
9433 * Unlike isl_map_normalize, this function does not remove redundant
9434 * constraints and only removes duplicates that have exactly the same
9435 * constraints in the input. It does sort the constraints and
9436 * the basic maps to ease the detection of duplicates.
9438 * If "map" has already been normalized or if the basic maps are
9439 * disjoint, then there can be no duplicates.
9441 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9443 int i;
9444 isl_basic_map *bmap;
9446 if (!map)
9447 return NULL;
9448 if (map->n <= 1)
9449 return map;
9450 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9451 return map;
9452 for (i = 0; i < map->n; ++i) {
9453 bmap = isl_basic_map_copy(map->p[i]);
9454 bmap = isl_basic_map_sort_constraints(bmap);
9455 if (!bmap)
9456 return isl_map_free(map);
9457 isl_basic_map_free(map->p[i]);
9458 map->p[i] = bmap;
9461 map = sort_and_remove_duplicates(map);
9462 return map;
9465 /* We normalize in place, but if anything goes wrong we need
9466 * to return NULL, so we need to make sure we don't change the
9467 * meaning of any possible other copies of map.
9469 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9471 int i;
9472 struct isl_basic_map *bmap;
9474 if (!map)
9475 return NULL;
9476 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9477 return map;
9478 for (i = 0; i < map->n; ++i) {
9479 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9480 if (!bmap)
9481 goto error;
9482 isl_basic_map_free(map->p[i]);
9483 map->p[i] = bmap;
9486 map = sort_and_remove_duplicates(map);
9487 if (map)
9488 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9489 return map;
9490 error:
9491 isl_map_free(map);
9492 return NULL;
9495 struct isl_set *isl_set_normalize(struct isl_set *set)
9497 return set_from_map(isl_map_normalize(set_to_map(set)));
9500 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9501 __isl_keep isl_map *map2)
9503 int i;
9504 isl_bool equal;
9506 if (!map1 || !map2)
9507 return isl_bool_error;
9509 if (map1 == map2)
9510 return isl_bool_true;
9511 if (!isl_space_is_equal(map1->dim, map2->dim))
9512 return isl_bool_false;
9514 map1 = isl_map_copy(map1);
9515 map2 = isl_map_copy(map2);
9516 map1 = isl_map_normalize(map1);
9517 map2 = isl_map_normalize(map2);
9518 if (!map1 || !map2)
9519 goto error;
9520 equal = map1->n == map2->n;
9521 for (i = 0; equal && i < map1->n; ++i) {
9522 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9523 if (equal < 0)
9524 goto error;
9526 isl_map_free(map1);
9527 isl_map_free(map2);
9528 return equal;
9529 error:
9530 isl_map_free(map1);
9531 isl_map_free(map2);
9532 return isl_bool_error;
9535 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9536 __isl_keep isl_set *set2)
9538 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9541 /* Return an interval that ranges from min to max (inclusive)
9543 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9544 isl_int min, isl_int max)
9546 int k;
9547 struct isl_basic_set *bset = NULL;
9549 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9550 if (!bset)
9551 goto error;
9553 k = isl_basic_set_alloc_inequality(bset);
9554 if (k < 0)
9555 goto error;
9556 isl_int_set_si(bset->ineq[k][1], 1);
9557 isl_int_neg(bset->ineq[k][0], min);
9559 k = isl_basic_set_alloc_inequality(bset);
9560 if (k < 0)
9561 goto error;
9562 isl_int_set_si(bset->ineq[k][1], -1);
9563 isl_int_set(bset->ineq[k][0], max);
9565 return bset;
9566 error:
9567 isl_basic_set_free(bset);
9568 return NULL;
9571 /* Return the basic maps in "map" as a list.
9573 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9574 __isl_keep isl_map *map)
9576 int i;
9577 isl_ctx *ctx;
9578 isl_basic_map_list *list;
9580 if (!map)
9581 return NULL;
9582 ctx = isl_map_get_ctx(map);
9583 list = isl_basic_map_list_alloc(ctx, map->n);
9585 for (i = 0; i < map->n; ++i) {
9586 isl_basic_map *bmap;
9588 bmap = isl_basic_map_copy(map->p[i]);
9589 list = isl_basic_map_list_add(list, bmap);
9592 return list;
9595 /* Return the intersection of the elements in the non-empty list "list".
9596 * All elements are assumed to live in the same space.
9598 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9599 __isl_take isl_basic_map_list *list)
9601 int i, n;
9602 isl_basic_map *bmap;
9604 if (!list)
9605 return NULL;
9606 n = isl_basic_map_list_n_basic_map(list);
9607 if (n < 1)
9608 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9609 "expecting non-empty list", goto error);
9611 bmap = isl_basic_map_list_get_basic_map(list, 0);
9612 for (i = 1; i < n; ++i) {
9613 isl_basic_map *bmap_i;
9615 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9616 bmap = isl_basic_map_intersect(bmap, bmap_i);
9619 isl_basic_map_list_free(list);
9620 return bmap;
9621 error:
9622 isl_basic_map_list_free(list);
9623 return NULL;
9626 /* Return the intersection of the elements in the non-empty list "list".
9627 * All elements are assumed to live in the same space.
9629 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9630 __isl_take isl_basic_set_list *list)
9632 return isl_basic_map_list_intersect(list);
9635 /* Return the union of the elements of "list".
9636 * The list is required to have at least one element.
9638 __isl_give isl_set *isl_basic_set_list_union(
9639 __isl_take isl_basic_set_list *list)
9641 int i, n;
9642 isl_space *space;
9643 isl_basic_set *bset;
9644 isl_set *set;
9646 if (!list)
9647 return NULL;
9648 n = isl_basic_set_list_n_basic_set(list);
9649 if (n < 1)
9650 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9651 "expecting non-empty list", goto error);
9653 bset = isl_basic_set_list_get_basic_set(list, 0);
9654 space = isl_basic_set_get_space(bset);
9655 isl_basic_set_free(bset);
9657 set = isl_set_alloc_space(space, n, 0);
9658 for (i = 0; i < n; ++i) {
9659 bset = isl_basic_set_list_get_basic_set(list, i);
9660 set = isl_set_add_basic_set(set, bset);
9663 isl_basic_set_list_free(list);
9664 return set;
9665 error:
9666 isl_basic_set_list_free(list);
9667 return NULL;
9670 /* Return the union of the elements in the non-empty list "list".
9671 * All elements are assumed to live in the same space.
9673 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9675 int i, n;
9676 isl_set *set;
9678 if (!list)
9679 return NULL;
9680 n = isl_set_list_n_set(list);
9681 if (n < 1)
9682 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9683 "expecting non-empty list", goto error);
9685 set = isl_set_list_get_set(list, 0);
9686 for (i = 1; i < n; ++i) {
9687 isl_set *set_i;
9689 set_i = isl_set_list_get_set(list, i);
9690 set = isl_set_union(set, set_i);
9693 isl_set_list_free(list);
9694 return set;
9695 error:
9696 isl_set_list_free(list);
9697 return NULL;
9700 /* Return the Cartesian product of the basic sets in list (in the given order).
9702 __isl_give isl_basic_set *isl_basic_set_list_product(
9703 __isl_take struct isl_basic_set_list *list)
9705 int i;
9706 unsigned dim;
9707 unsigned nparam;
9708 unsigned extra;
9709 unsigned n_eq;
9710 unsigned n_ineq;
9711 struct isl_basic_set *product = NULL;
9713 if (!list)
9714 goto error;
9715 isl_assert(list->ctx, list->n > 0, goto error);
9716 isl_assert(list->ctx, list->p[0], goto error);
9717 nparam = isl_basic_set_n_param(list->p[0]);
9718 dim = isl_basic_set_n_dim(list->p[0]);
9719 extra = list->p[0]->n_div;
9720 n_eq = list->p[0]->n_eq;
9721 n_ineq = list->p[0]->n_ineq;
9722 for (i = 1; i < list->n; ++i) {
9723 isl_assert(list->ctx, list->p[i], goto error);
9724 isl_assert(list->ctx,
9725 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9726 dim += isl_basic_set_n_dim(list->p[i]);
9727 extra += list->p[i]->n_div;
9728 n_eq += list->p[i]->n_eq;
9729 n_ineq += list->p[i]->n_ineq;
9731 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9732 n_eq, n_ineq);
9733 if (!product)
9734 goto error;
9735 dim = 0;
9736 for (i = 0; i < list->n; ++i) {
9737 isl_basic_set_add_constraints(product,
9738 isl_basic_set_copy(list->p[i]), dim);
9739 dim += isl_basic_set_n_dim(list->p[i]);
9741 isl_basic_set_list_free(list);
9742 return product;
9743 error:
9744 isl_basic_set_free(product);
9745 isl_basic_set_list_free(list);
9746 return NULL;
9749 struct isl_basic_map *isl_basic_map_product(
9750 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9752 isl_space *dim_result = NULL;
9753 struct isl_basic_map *bmap;
9754 unsigned in1, in2, out1, out2, nparam, total, pos;
9755 struct isl_dim_map *dim_map1, *dim_map2;
9757 if (!bmap1 || !bmap2)
9758 goto error;
9760 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9761 bmap2->dim, isl_dim_param), goto error);
9762 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9763 isl_space_copy(bmap2->dim));
9765 in1 = isl_basic_map_n_in(bmap1);
9766 in2 = isl_basic_map_n_in(bmap2);
9767 out1 = isl_basic_map_n_out(bmap1);
9768 out2 = isl_basic_map_n_out(bmap2);
9769 nparam = isl_basic_map_n_param(bmap1);
9771 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9772 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9773 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9774 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9775 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9776 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9777 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9778 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9779 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9780 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9781 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9783 bmap = isl_basic_map_alloc_space(dim_result,
9784 bmap1->n_div + bmap2->n_div,
9785 bmap1->n_eq + bmap2->n_eq,
9786 bmap1->n_ineq + bmap2->n_ineq);
9787 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9788 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9789 bmap = isl_basic_map_simplify(bmap);
9790 return isl_basic_map_finalize(bmap);
9791 error:
9792 isl_basic_map_free(bmap1);
9793 isl_basic_map_free(bmap2);
9794 return NULL;
9797 __isl_give isl_basic_map *isl_basic_map_flat_product(
9798 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9800 isl_basic_map *prod;
9802 prod = isl_basic_map_product(bmap1, bmap2);
9803 prod = isl_basic_map_flatten(prod);
9804 return prod;
9807 __isl_give isl_basic_set *isl_basic_set_flat_product(
9808 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9810 return isl_basic_map_flat_range_product(bset1, bset2);
9813 __isl_give isl_basic_map *isl_basic_map_domain_product(
9814 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9816 isl_space *space_result = NULL;
9817 isl_basic_map *bmap;
9818 unsigned in1, in2, out, nparam, total, pos;
9819 struct isl_dim_map *dim_map1, *dim_map2;
9821 if (!bmap1 || !bmap2)
9822 goto error;
9824 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9825 isl_space_copy(bmap2->dim));
9827 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9828 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9829 out = isl_basic_map_dim(bmap1, isl_dim_out);
9830 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9832 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9833 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9834 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9835 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9836 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9837 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9838 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9839 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9840 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9841 isl_dim_map_div(dim_map1, bmap1, pos += out);
9842 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9844 bmap = isl_basic_map_alloc_space(space_result,
9845 bmap1->n_div + bmap2->n_div,
9846 bmap1->n_eq + bmap2->n_eq,
9847 bmap1->n_ineq + bmap2->n_ineq);
9848 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9849 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9850 bmap = isl_basic_map_simplify(bmap);
9851 return isl_basic_map_finalize(bmap);
9852 error:
9853 isl_basic_map_free(bmap1);
9854 isl_basic_map_free(bmap2);
9855 return NULL;
9858 __isl_give isl_basic_map *isl_basic_map_range_product(
9859 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9861 int rational;
9862 isl_space *dim_result = NULL;
9863 isl_basic_map *bmap;
9864 unsigned in, out1, out2, nparam, total, pos;
9865 struct isl_dim_map *dim_map1, *dim_map2;
9867 rational = isl_basic_map_is_rational(bmap1);
9868 if (rational >= 0 && rational)
9869 rational = isl_basic_map_is_rational(bmap2);
9870 if (!bmap1 || !bmap2 || rational < 0)
9871 goto error;
9873 if (!isl_space_match(bmap1->dim, isl_dim_param,
9874 bmap2->dim, isl_dim_param))
9875 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9876 "parameters don't match", goto error);
9878 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9879 isl_space_copy(bmap2->dim));
9881 in = isl_basic_map_dim(bmap1, isl_dim_in);
9882 out1 = isl_basic_map_n_out(bmap1);
9883 out2 = isl_basic_map_n_out(bmap2);
9884 nparam = isl_basic_map_n_param(bmap1);
9886 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9887 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9888 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9889 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9890 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9891 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9892 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9893 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9894 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9895 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9896 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9898 bmap = isl_basic_map_alloc_space(dim_result,
9899 bmap1->n_div + bmap2->n_div,
9900 bmap1->n_eq + bmap2->n_eq,
9901 bmap1->n_ineq + bmap2->n_ineq);
9902 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9903 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9904 if (rational)
9905 bmap = isl_basic_map_set_rational(bmap);
9906 bmap = isl_basic_map_simplify(bmap);
9907 return isl_basic_map_finalize(bmap);
9908 error:
9909 isl_basic_map_free(bmap1);
9910 isl_basic_map_free(bmap2);
9911 return NULL;
9914 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9915 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9917 isl_basic_map *prod;
9919 prod = isl_basic_map_range_product(bmap1, bmap2);
9920 prod = isl_basic_map_flatten_range(prod);
9921 return prod;
9924 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9925 * and collect the results.
9926 * The result live in the space obtained by calling "space_product"
9927 * on the spaces of "map1" and "map2".
9928 * If "remove_duplicates" is set then the result may contain duplicates
9929 * (even if the inputs do not) and so we try and remove the obvious
9930 * duplicates.
9932 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9933 __isl_take isl_map *map2,
9934 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9935 __isl_take isl_space *right),
9936 __isl_give isl_basic_map *(*basic_map_product)(
9937 __isl_take isl_basic_map *left,
9938 __isl_take isl_basic_map *right),
9939 int remove_duplicates)
9941 unsigned flags = 0;
9942 struct isl_map *result;
9943 int i, j;
9945 if (!map1 || !map2)
9946 goto error;
9948 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9949 map2->dim, isl_dim_param), goto error);
9951 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9952 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9953 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9955 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9956 isl_space_copy(map2->dim)),
9957 map1->n * map2->n, flags);
9958 if (!result)
9959 goto error;
9960 for (i = 0; i < map1->n; ++i)
9961 for (j = 0; j < map2->n; ++j) {
9962 struct isl_basic_map *part;
9963 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9964 isl_basic_map_copy(map2->p[j]));
9965 if (isl_basic_map_is_empty(part))
9966 isl_basic_map_free(part);
9967 else
9968 result = isl_map_add_basic_map(result, part);
9969 if (!result)
9970 goto error;
9972 if (remove_duplicates)
9973 result = isl_map_remove_obvious_duplicates(result);
9974 isl_map_free(map1);
9975 isl_map_free(map2);
9976 return result;
9977 error:
9978 isl_map_free(map1);
9979 isl_map_free(map2);
9980 return NULL;
9983 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9985 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9986 __isl_take isl_map *map2)
9988 return map_product(map1, map2, &isl_space_product,
9989 &isl_basic_map_product, 0);
9992 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9993 __isl_take isl_map *map2)
9995 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9998 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10000 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10001 __isl_take isl_map *map2)
10003 isl_map *prod;
10005 prod = isl_map_product(map1, map2);
10006 prod = isl_map_flatten(prod);
10007 return prod;
10010 /* Given two set A and B, construct its Cartesian product A x B.
10012 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10014 return isl_map_range_product(set1, set2);
10017 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10018 __isl_take isl_set *set2)
10020 return isl_map_flat_range_product(set1, set2);
10023 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10025 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10026 __isl_take isl_map *map2)
10028 return map_product(map1, map2, &isl_space_domain_product,
10029 &isl_basic_map_domain_product, 1);
10032 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10034 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10035 __isl_take isl_map *map2)
10037 return map_product(map1, map2, &isl_space_range_product,
10038 &isl_basic_map_range_product, 1);
10041 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10042 __isl_take isl_map *map2)
10044 return isl_map_align_params_map_map_and(map1, map2,
10045 &map_domain_product_aligned);
10048 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10049 __isl_take isl_map *map2)
10051 return isl_map_align_params_map_map_and(map1, map2,
10052 &map_range_product_aligned);
10055 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10057 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10059 isl_space *space;
10060 int total1, keep1, total2, keep2;
10062 if (!map)
10063 return NULL;
10064 if (!isl_space_domain_is_wrapping(map->dim) ||
10065 !isl_space_range_is_wrapping(map->dim))
10066 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10067 "not a product", return isl_map_free(map));
10069 space = isl_map_get_space(map);
10070 total1 = isl_space_dim(space, isl_dim_in);
10071 total2 = isl_space_dim(space, isl_dim_out);
10072 space = isl_space_factor_domain(space);
10073 keep1 = isl_space_dim(space, isl_dim_in);
10074 keep2 = isl_space_dim(space, isl_dim_out);
10075 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10076 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10077 map = isl_map_reset_space(map, space);
10079 return map;
10082 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10084 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10086 isl_space *space;
10087 int total1, keep1, total2, keep2;
10089 if (!map)
10090 return NULL;
10091 if (!isl_space_domain_is_wrapping(map->dim) ||
10092 !isl_space_range_is_wrapping(map->dim))
10093 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10094 "not a product", return isl_map_free(map));
10096 space = isl_map_get_space(map);
10097 total1 = isl_space_dim(space, isl_dim_in);
10098 total2 = isl_space_dim(space, isl_dim_out);
10099 space = isl_space_factor_range(space);
10100 keep1 = isl_space_dim(space, isl_dim_in);
10101 keep2 = isl_space_dim(space, isl_dim_out);
10102 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10103 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10104 map = isl_map_reset_space(map, space);
10106 return map;
10109 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10111 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10113 isl_space *space;
10114 int total, keep;
10116 if (!map)
10117 return NULL;
10118 if (!isl_space_domain_is_wrapping(map->dim))
10119 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10120 "domain is not a product", return isl_map_free(map));
10122 space = isl_map_get_space(map);
10123 total = isl_space_dim(space, isl_dim_in);
10124 space = isl_space_domain_factor_domain(space);
10125 keep = isl_space_dim(space, isl_dim_in);
10126 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10127 map = isl_map_reset_space(map, space);
10129 return map;
10132 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10134 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10136 isl_space *space;
10137 int total, keep;
10139 if (!map)
10140 return NULL;
10141 if (!isl_space_domain_is_wrapping(map->dim))
10142 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10143 "domain is not a product", return isl_map_free(map));
10145 space = isl_map_get_space(map);
10146 total = isl_space_dim(space, isl_dim_in);
10147 space = isl_space_domain_factor_range(space);
10148 keep = isl_space_dim(space, isl_dim_in);
10149 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10150 map = isl_map_reset_space(map, space);
10152 return map;
10155 /* Given a map A -> [B -> C], extract the map A -> B.
10157 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10159 isl_space *space;
10160 int total, keep;
10162 if (!map)
10163 return NULL;
10164 if (!isl_space_range_is_wrapping(map->dim))
10165 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10166 "range is not a product", return isl_map_free(map));
10168 space = isl_map_get_space(map);
10169 total = isl_space_dim(space, isl_dim_out);
10170 space = isl_space_range_factor_domain(space);
10171 keep = isl_space_dim(space, isl_dim_out);
10172 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10173 map = isl_map_reset_space(map, space);
10175 return map;
10178 /* Given a map A -> [B -> C], extract the map A -> C.
10180 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10182 isl_space *space;
10183 int total, keep;
10185 if (!map)
10186 return NULL;
10187 if (!isl_space_range_is_wrapping(map->dim))
10188 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10189 "range is not a product", return isl_map_free(map));
10191 space = isl_map_get_space(map);
10192 total = isl_space_dim(space, isl_dim_out);
10193 space = isl_space_range_factor_range(space);
10194 keep = isl_space_dim(space, isl_dim_out);
10195 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10196 map = isl_map_reset_space(map, space);
10198 return map;
10201 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10203 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10204 __isl_take isl_map *map2)
10206 isl_map *prod;
10208 prod = isl_map_domain_product(map1, map2);
10209 prod = isl_map_flatten_domain(prod);
10210 return prod;
10213 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10215 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10216 __isl_take isl_map *map2)
10218 isl_map *prod;
10220 prod = isl_map_range_product(map1, map2);
10221 prod = isl_map_flatten_range(prod);
10222 return prod;
10225 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10227 int i;
10228 uint32_t hash = isl_hash_init();
10229 unsigned total;
10231 if (!bmap)
10232 return 0;
10233 bmap = isl_basic_map_copy(bmap);
10234 bmap = isl_basic_map_normalize(bmap);
10235 if (!bmap)
10236 return 0;
10237 total = isl_basic_map_total_dim(bmap);
10238 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10239 for (i = 0; i < bmap->n_eq; ++i) {
10240 uint32_t c_hash;
10241 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10242 isl_hash_hash(hash, c_hash);
10244 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10245 for (i = 0; i < bmap->n_ineq; ++i) {
10246 uint32_t c_hash;
10247 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10248 isl_hash_hash(hash, c_hash);
10250 isl_hash_byte(hash, bmap->n_div & 0xFF);
10251 for (i = 0; i < bmap->n_div; ++i) {
10252 uint32_t c_hash;
10253 if (isl_int_is_zero(bmap->div[i][0]))
10254 continue;
10255 isl_hash_byte(hash, i & 0xFF);
10256 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10257 isl_hash_hash(hash, c_hash);
10259 isl_basic_map_free(bmap);
10260 return hash;
10263 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10265 return isl_basic_map_get_hash(bset_to_bmap(bset));
10268 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10270 int i;
10271 uint32_t hash;
10273 if (!map)
10274 return 0;
10275 map = isl_map_copy(map);
10276 map = isl_map_normalize(map);
10277 if (!map)
10278 return 0;
10280 hash = isl_hash_init();
10281 for (i = 0; i < map->n; ++i) {
10282 uint32_t bmap_hash;
10283 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10284 isl_hash_hash(hash, bmap_hash);
10287 isl_map_free(map);
10289 return hash;
10292 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10294 return isl_map_get_hash(set_to_map(set));
10297 /* Check if the value for dimension dim is completely determined
10298 * by the values of the other parameters and variables.
10299 * That is, check if dimension dim is involved in an equality.
10301 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10303 int i;
10304 unsigned nparam;
10306 if (!bset)
10307 return -1;
10308 nparam = isl_basic_set_n_param(bset);
10309 for (i = 0; i < bset->n_eq; ++i)
10310 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10311 return 1;
10312 return 0;
10315 /* Check if the value for dimension dim is completely determined
10316 * by the values of the other parameters and variables.
10317 * That is, check if dimension dim is involved in an equality
10318 * for each of the subsets.
10320 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10322 int i;
10324 if (!set)
10325 return -1;
10326 for (i = 0; i < set->n; ++i) {
10327 int unique;
10328 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10329 if (unique != 1)
10330 return unique;
10332 return 1;
10335 /* Return the number of basic maps in the (current) representation of "map".
10337 int isl_map_n_basic_map(__isl_keep isl_map *map)
10339 return map ? map->n : 0;
10342 int isl_set_n_basic_set(__isl_keep isl_set *set)
10344 return set ? set->n : 0;
10347 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10348 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10350 int i;
10352 if (!map)
10353 return isl_stat_error;
10355 for (i = 0; i < map->n; ++i)
10356 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10357 return isl_stat_error;
10359 return isl_stat_ok;
10362 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10363 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10365 int i;
10367 if (!set)
10368 return isl_stat_error;
10370 for (i = 0; i < set->n; ++i)
10371 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10372 return isl_stat_error;
10374 return isl_stat_ok;
10377 /* Return a list of basic sets, the union of which is equal to "set".
10379 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10380 __isl_keep isl_set *set)
10382 int i;
10383 isl_basic_set_list *list;
10385 if (!set)
10386 return NULL;
10388 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10389 for (i = 0; i < set->n; ++i) {
10390 isl_basic_set *bset;
10392 bset = isl_basic_set_copy(set->p[i]);
10393 list = isl_basic_set_list_add(list, bset);
10396 return list;
10399 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10401 isl_space *dim;
10403 if (!bset)
10404 return NULL;
10406 bset = isl_basic_set_cow(bset);
10407 if (!bset)
10408 return NULL;
10410 dim = isl_basic_set_get_space(bset);
10411 dim = isl_space_lift(dim, bset->n_div);
10412 if (!dim)
10413 goto error;
10414 isl_space_free(bset->dim);
10415 bset->dim = dim;
10416 bset->extra -= bset->n_div;
10417 bset->n_div = 0;
10419 bset = isl_basic_set_finalize(bset);
10421 return bset;
10422 error:
10423 isl_basic_set_free(bset);
10424 return NULL;
10427 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10429 int i;
10430 isl_space *dim;
10431 unsigned n_div;
10433 set = isl_set_align_divs(set);
10435 if (!set)
10436 return NULL;
10438 set = isl_set_cow(set);
10439 if (!set)
10440 return NULL;
10442 n_div = set->p[0]->n_div;
10443 dim = isl_set_get_space(set);
10444 dim = isl_space_lift(dim, n_div);
10445 if (!dim)
10446 goto error;
10447 isl_space_free(set->dim);
10448 set->dim = dim;
10450 for (i = 0; i < set->n; ++i) {
10451 set->p[i] = isl_basic_set_lift(set->p[i]);
10452 if (!set->p[i])
10453 goto error;
10456 return set;
10457 error:
10458 isl_set_free(set);
10459 return NULL;
10462 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10464 isl_space *dim;
10465 struct isl_basic_map *bmap;
10466 unsigned n_set;
10467 unsigned n_div;
10468 unsigned n_param;
10469 unsigned total;
10470 int i, k, l;
10472 set = isl_set_align_divs(set);
10474 if (!set)
10475 return NULL;
10477 dim = isl_set_get_space(set);
10478 if (set->n == 0 || set->p[0]->n_div == 0) {
10479 isl_set_free(set);
10480 return isl_map_identity(isl_space_map_from_set(dim));
10483 n_div = set->p[0]->n_div;
10484 dim = isl_space_map_from_set(dim);
10485 n_param = isl_space_dim(dim, isl_dim_param);
10486 n_set = isl_space_dim(dim, isl_dim_in);
10487 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10488 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10489 for (i = 0; i < n_set; ++i)
10490 bmap = var_equal(bmap, i);
10492 total = n_param + n_set + n_set + n_div;
10493 for (i = 0; i < n_div; ++i) {
10494 k = isl_basic_map_alloc_inequality(bmap);
10495 if (k < 0)
10496 goto error;
10497 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10498 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10499 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10500 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10501 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10502 set->p[0]->div[i][0]);
10504 l = isl_basic_map_alloc_inequality(bmap);
10505 if (l < 0)
10506 goto error;
10507 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10508 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10509 set->p[0]->div[i][0]);
10510 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10513 isl_set_free(set);
10514 bmap = isl_basic_map_simplify(bmap);
10515 bmap = isl_basic_map_finalize(bmap);
10516 return isl_map_from_basic_map(bmap);
10517 error:
10518 isl_set_free(set);
10519 isl_basic_map_free(bmap);
10520 return NULL;
10523 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10525 unsigned dim;
10526 int size = 0;
10528 if (!bset)
10529 return -1;
10531 dim = isl_basic_set_total_dim(bset);
10532 size += bset->n_eq * (1 + dim);
10533 size += bset->n_ineq * (1 + dim);
10534 size += bset->n_div * (2 + dim);
10536 return size;
10539 int isl_set_size(__isl_keep isl_set *set)
10541 int i;
10542 int size = 0;
10544 if (!set)
10545 return -1;
10547 for (i = 0; i < set->n; ++i)
10548 size += isl_basic_set_size(set->p[i]);
10550 return size;
10553 /* Check if there is any lower bound (if lower == 0) and/or upper
10554 * bound (if upper == 0) on the specified dim.
10556 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10557 enum isl_dim_type type, unsigned pos, int lower, int upper)
10559 int i;
10561 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10562 return isl_bool_error;
10564 pos += isl_basic_map_offset(bmap, type);
10566 for (i = 0; i < bmap->n_div; ++i) {
10567 if (isl_int_is_zero(bmap->div[i][0]))
10568 continue;
10569 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10570 return isl_bool_true;
10573 for (i = 0; i < bmap->n_eq; ++i)
10574 if (!isl_int_is_zero(bmap->eq[i][pos]))
10575 return isl_bool_true;
10577 for (i = 0; i < bmap->n_ineq; ++i) {
10578 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10579 if (sgn > 0)
10580 lower = 1;
10581 if (sgn < 0)
10582 upper = 1;
10585 return lower && upper;
10588 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10589 enum isl_dim_type type, unsigned pos)
10591 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10594 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10595 enum isl_dim_type type, unsigned pos)
10597 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10600 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10601 enum isl_dim_type type, unsigned pos)
10603 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10606 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10607 enum isl_dim_type type, unsigned pos)
10609 int i;
10611 if (!map)
10612 return -1;
10614 for (i = 0; i < map->n; ++i) {
10615 int bounded;
10616 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10617 if (bounded < 0 || !bounded)
10618 return bounded;
10621 return 1;
10624 /* Return 1 if the specified dim is involved in both an upper bound
10625 * and a lower bound.
10627 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10628 enum isl_dim_type type, unsigned pos)
10630 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10633 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10635 static isl_bool has_any_bound(__isl_keep isl_map *map,
10636 enum isl_dim_type type, unsigned pos,
10637 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10638 enum isl_dim_type type, unsigned pos))
10640 int i;
10642 if (!map)
10643 return isl_bool_error;
10645 for (i = 0; i < map->n; ++i) {
10646 isl_bool bounded;
10647 bounded = fn(map->p[i], type, pos);
10648 if (bounded < 0 || bounded)
10649 return bounded;
10652 return isl_bool_false;
10655 /* Return 1 if the specified dim is involved in any lower bound.
10657 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10658 enum isl_dim_type type, unsigned pos)
10660 return has_any_bound(set, type, pos,
10661 &isl_basic_map_dim_has_lower_bound);
10664 /* Return 1 if the specified dim is involved in any upper bound.
10666 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10667 enum isl_dim_type type, unsigned pos)
10669 return has_any_bound(set, type, pos,
10670 &isl_basic_map_dim_has_upper_bound);
10673 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10675 static isl_bool has_bound(__isl_keep isl_map *map,
10676 enum isl_dim_type type, unsigned pos,
10677 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10678 enum isl_dim_type type, unsigned pos))
10680 int i;
10682 if (!map)
10683 return isl_bool_error;
10685 for (i = 0; i < map->n; ++i) {
10686 isl_bool bounded;
10687 bounded = fn(map->p[i], type, pos);
10688 if (bounded < 0 || !bounded)
10689 return bounded;
10692 return isl_bool_true;
10695 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10697 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10698 enum isl_dim_type type, unsigned pos)
10700 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10703 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10705 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10706 enum isl_dim_type type, unsigned pos)
10708 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10711 /* For each of the "n" variables starting at "first", determine
10712 * the sign of the variable and put the results in the first "n"
10713 * elements of the array "signs".
10714 * Sign
10715 * 1 means that the variable is non-negative
10716 * -1 means that the variable is non-positive
10717 * 0 means the variable attains both positive and negative values.
10719 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10720 unsigned first, unsigned n, int *signs)
10722 isl_vec *bound = NULL;
10723 struct isl_tab *tab = NULL;
10724 struct isl_tab_undo *snap;
10725 int i;
10727 if (!bset || !signs)
10728 return -1;
10730 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10731 tab = isl_tab_from_basic_set(bset, 0);
10732 if (!bound || !tab)
10733 goto error;
10735 isl_seq_clr(bound->el, bound->size);
10736 isl_int_set_si(bound->el[0], -1);
10738 snap = isl_tab_snap(tab);
10739 for (i = 0; i < n; ++i) {
10740 int empty;
10742 isl_int_set_si(bound->el[1 + first + i], -1);
10743 if (isl_tab_add_ineq(tab, bound->el) < 0)
10744 goto error;
10745 empty = tab->empty;
10746 isl_int_set_si(bound->el[1 + first + i], 0);
10747 if (isl_tab_rollback(tab, snap) < 0)
10748 goto error;
10750 if (empty) {
10751 signs[i] = 1;
10752 continue;
10755 isl_int_set_si(bound->el[1 + first + i], 1);
10756 if (isl_tab_add_ineq(tab, bound->el) < 0)
10757 goto error;
10758 empty = tab->empty;
10759 isl_int_set_si(bound->el[1 + first + i], 0);
10760 if (isl_tab_rollback(tab, snap) < 0)
10761 goto error;
10763 signs[i] = empty ? -1 : 0;
10766 isl_tab_free(tab);
10767 isl_vec_free(bound);
10768 return 0;
10769 error:
10770 isl_tab_free(tab);
10771 isl_vec_free(bound);
10772 return -1;
10775 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10776 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10778 if (!bset || !signs)
10779 return -1;
10780 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10781 return -1);
10783 first += pos(bset->dim, type) - 1;
10784 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10787 /* Is it possible for the integer division "div" to depend (possibly
10788 * indirectly) on any output dimensions?
10790 * If the div is undefined, then we conservatively assume that it
10791 * may depend on them.
10792 * Otherwise, we check if it actually depends on them or on any integer
10793 * divisions that may depend on them.
10795 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10797 int i;
10798 unsigned n_out, o_out;
10799 unsigned n_div, o_div;
10801 if (isl_int_is_zero(bmap->div[div][0]))
10802 return 1;
10804 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10805 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10807 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10808 return 1;
10810 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10811 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10813 for (i = 0; i < n_div; ++i) {
10814 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10815 continue;
10816 if (div_may_involve_output(bmap, i))
10817 return 1;
10820 return 0;
10823 /* Return the first integer division of "bmap" in the range
10824 * [first, first + n[ that may depend on any output dimensions and
10825 * that has a non-zero coefficient in "c" (where the first coefficient
10826 * in "c" corresponds to integer division "first").
10828 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10829 isl_int *c, int first, int n)
10831 int k;
10833 if (!bmap)
10834 return -1;
10836 for (k = first; k < first + n; ++k) {
10837 if (isl_int_is_zero(c[k]))
10838 continue;
10839 if (div_may_involve_output(bmap, k))
10840 return k;
10843 return first + n;
10846 /* Look for a pair of inequality constraints in "bmap" of the form
10848 * -l + i >= 0 or i >= l
10849 * and
10850 * n + l - i >= 0 or i <= l + n
10852 * with n < "m" and i the output dimension at position "pos".
10853 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10854 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10855 * and earlier output dimensions, as well as integer divisions that do
10856 * not involve any of the output dimensions.
10858 * Return the index of the first inequality constraint or bmap->n_ineq
10859 * if no such pair can be found.
10861 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10862 int pos, isl_int m)
10864 int i, j;
10865 isl_ctx *ctx;
10866 unsigned total;
10867 unsigned n_div, o_div;
10868 unsigned n_out, o_out;
10869 int less;
10871 if (!bmap)
10872 return -1;
10874 ctx = isl_basic_map_get_ctx(bmap);
10875 total = isl_basic_map_total_dim(bmap);
10876 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10877 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10878 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10879 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10880 for (i = 0; i < bmap->n_ineq; ++i) {
10881 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10882 continue;
10883 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10884 n_out - (pos + 1)) != -1)
10885 continue;
10886 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10887 0, n_div) < n_div)
10888 continue;
10889 for (j = i + 1; j < bmap->n_ineq; ++j) {
10890 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10891 ctx->one))
10892 continue;
10893 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10894 bmap->ineq[j] + 1, total))
10895 continue;
10896 break;
10898 if (j >= bmap->n_ineq)
10899 continue;
10900 isl_int_add(bmap->ineq[i][0],
10901 bmap->ineq[i][0], bmap->ineq[j][0]);
10902 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10903 isl_int_sub(bmap->ineq[i][0],
10904 bmap->ineq[i][0], bmap->ineq[j][0]);
10905 if (!less)
10906 continue;
10907 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10908 return i;
10909 else
10910 return j;
10913 return bmap->n_ineq;
10916 /* Return the index of the equality of "bmap" that defines
10917 * the output dimension "pos" in terms of earlier dimensions.
10918 * The equality may also involve integer divisions, as long
10919 * as those integer divisions are defined in terms of
10920 * parameters or input dimensions.
10921 * In this case, *div is set to the number of integer divisions and
10922 * *ineq is set to the number of inequality constraints (provided
10923 * div and ineq are not NULL).
10925 * The equality may also involve a single integer division involving
10926 * the output dimensions (typically only output dimension "pos") as
10927 * long as the coefficient of output dimension "pos" is 1 or -1 and
10928 * there is a pair of constraints i >= l and i <= l + n, with i referring
10929 * to output dimension "pos", l an expression involving only earlier
10930 * dimensions and n smaller than the coefficient of the integer division
10931 * in the equality. In this case, the output dimension can be defined
10932 * in terms of a modulo expression that does not involve the integer division.
10933 * *div is then set to this single integer division and
10934 * *ineq is set to the index of constraint i >= l.
10936 * Return bmap->n_eq if there is no such equality.
10937 * Return -1 on error.
10939 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10940 int pos, int *div, int *ineq)
10942 int j, k, l;
10943 unsigned n_out, o_out;
10944 unsigned n_div, o_div;
10946 if (!bmap)
10947 return -1;
10949 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10950 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10951 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10952 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10954 if (ineq)
10955 *ineq = bmap->n_ineq;
10956 if (div)
10957 *div = n_div;
10958 for (j = 0; j < bmap->n_eq; ++j) {
10959 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10960 continue;
10961 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10962 n_out - (pos + 1)) != -1)
10963 continue;
10964 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10965 0, n_div);
10966 if (k >= n_div)
10967 return j;
10968 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10969 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10970 continue;
10971 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10972 k + 1, n_div - (k+1)) < n_div)
10973 continue;
10974 l = find_modulo_constraint_pair(bmap, pos,
10975 bmap->eq[j][o_div + k]);
10976 if (l < 0)
10977 return -1;
10978 if (l >= bmap->n_ineq)
10979 continue;
10980 if (div)
10981 *div = k;
10982 if (ineq)
10983 *ineq = l;
10984 return j;
10987 return bmap->n_eq;
10990 /* Check if the given basic map is obviously single-valued.
10991 * In particular, for each output dimension, check that there is
10992 * an equality that defines the output dimension in terms of
10993 * earlier dimensions.
10995 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10997 int i;
10998 unsigned n_out;
11000 if (!bmap)
11001 return isl_bool_error;
11003 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11005 for (i = 0; i < n_out; ++i) {
11006 int eq;
11008 eq = isl_basic_map_output_defining_equality(bmap, i,
11009 NULL, NULL);
11010 if (eq < 0)
11011 return isl_bool_error;
11012 if (eq >= bmap->n_eq)
11013 return isl_bool_false;
11016 return isl_bool_true;
11019 /* Check if the given basic map is single-valued.
11020 * We simply compute
11022 * M \circ M^-1
11024 * and check if the result is a subset of the identity mapping.
11026 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11028 isl_space *space;
11029 isl_basic_map *test;
11030 isl_basic_map *id;
11031 isl_bool sv;
11033 sv = isl_basic_map_plain_is_single_valued(bmap);
11034 if (sv < 0 || sv)
11035 return sv;
11037 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11038 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11040 space = isl_basic_map_get_space(bmap);
11041 space = isl_space_map_from_set(isl_space_range(space));
11042 id = isl_basic_map_identity(space);
11044 sv = isl_basic_map_is_subset(test, id);
11046 isl_basic_map_free(test);
11047 isl_basic_map_free(id);
11049 return sv;
11052 /* Check if the given map is obviously single-valued.
11054 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11056 if (!map)
11057 return isl_bool_error;
11058 if (map->n == 0)
11059 return isl_bool_true;
11060 if (map->n >= 2)
11061 return isl_bool_false;
11063 return isl_basic_map_plain_is_single_valued(map->p[0]);
11066 /* Check if the given map is single-valued.
11067 * We simply compute
11069 * M \circ M^-1
11071 * and check if the result is a subset of the identity mapping.
11073 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11075 isl_space *dim;
11076 isl_map *test;
11077 isl_map *id;
11078 isl_bool sv;
11080 sv = isl_map_plain_is_single_valued(map);
11081 if (sv < 0 || sv)
11082 return sv;
11084 test = isl_map_reverse(isl_map_copy(map));
11085 test = isl_map_apply_range(test, isl_map_copy(map));
11087 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11088 id = isl_map_identity(dim);
11090 sv = isl_map_is_subset(test, id);
11092 isl_map_free(test);
11093 isl_map_free(id);
11095 return sv;
11098 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11100 isl_bool in;
11102 map = isl_map_copy(map);
11103 map = isl_map_reverse(map);
11104 in = isl_map_is_single_valued(map);
11105 isl_map_free(map);
11107 return in;
11110 /* Check if the given map is obviously injective.
11112 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11114 isl_bool in;
11116 map = isl_map_copy(map);
11117 map = isl_map_reverse(map);
11118 in = isl_map_plain_is_single_valued(map);
11119 isl_map_free(map);
11121 return in;
11124 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11126 isl_bool sv;
11128 sv = isl_map_is_single_valued(map);
11129 if (sv < 0 || !sv)
11130 return sv;
11132 return isl_map_is_injective(map);
11135 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11137 return isl_map_is_single_valued(set_to_map(set));
11140 /* Does "map" only map elements to themselves?
11142 * If the domain and range spaces are different, then "map"
11143 * is considered not to be an identity relation, even if it is empty.
11144 * Otherwise, construct the maximal identity relation and
11145 * check whether "map" is a subset of this relation.
11147 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11149 isl_space *space;
11150 isl_map *id;
11151 isl_bool equal, is_identity;
11153 space = isl_map_get_space(map);
11154 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11155 isl_space_free(space);
11156 if (equal < 0 || !equal)
11157 return equal;
11159 id = isl_map_identity(isl_map_get_space(map));
11160 is_identity = isl_map_is_subset(map, id);
11161 isl_map_free(id);
11163 return is_identity;
11166 int isl_map_is_translation(__isl_keep isl_map *map)
11168 int ok;
11169 isl_set *delta;
11171 delta = isl_map_deltas(isl_map_copy(map));
11172 ok = isl_set_is_singleton(delta);
11173 isl_set_free(delta);
11175 return ok;
11178 static int unique(isl_int *p, unsigned pos, unsigned len)
11180 if (isl_seq_first_non_zero(p, pos) != -1)
11181 return 0;
11182 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11183 return 0;
11184 return 1;
11187 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11189 int i, j;
11190 unsigned nvar;
11191 unsigned ovar;
11193 if (!bset)
11194 return -1;
11196 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11197 return 0;
11199 nvar = isl_basic_set_dim(bset, isl_dim_set);
11200 ovar = isl_space_offset(bset->dim, isl_dim_set);
11201 for (j = 0; j < nvar; ++j) {
11202 int lower = 0, upper = 0;
11203 for (i = 0; i < bset->n_eq; ++i) {
11204 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11205 continue;
11206 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11207 return 0;
11208 break;
11210 if (i < bset->n_eq)
11211 continue;
11212 for (i = 0; i < bset->n_ineq; ++i) {
11213 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11214 continue;
11215 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11216 return 0;
11217 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11218 lower = 1;
11219 else
11220 upper = 1;
11222 if (!lower || !upper)
11223 return 0;
11226 return 1;
11229 int isl_set_is_box(__isl_keep isl_set *set)
11231 if (!set)
11232 return -1;
11233 if (set->n != 1)
11234 return 0;
11236 return isl_basic_set_is_box(set->p[0]);
11239 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11241 if (!bset)
11242 return isl_bool_error;
11244 return isl_space_is_wrapping(bset->dim);
11247 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11249 if (!set)
11250 return isl_bool_error;
11252 return isl_space_is_wrapping(set->dim);
11255 /* Modify the space of "map" through a call to "change".
11256 * If "can_change" is set (not NULL), then first call it to check
11257 * if the modification is allowed, printing the error message "cannot_change"
11258 * if it is not.
11260 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11261 isl_bool (*can_change)(__isl_keep isl_map *map),
11262 const char *cannot_change,
11263 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11265 isl_bool ok;
11266 isl_space *space;
11268 if (!map)
11269 return NULL;
11271 ok = can_change ? can_change(map) : isl_bool_true;
11272 if (ok < 0)
11273 return isl_map_free(map);
11274 if (!ok)
11275 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11276 return isl_map_free(map));
11278 space = change(isl_map_get_space(map));
11279 map = isl_map_reset_space(map, space);
11281 return map;
11284 /* Is the domain of "map" a wrapped relation?
11286 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11288 if (!map)
11289 return isl_bool_error;
11291 return isl_space_domain_is_wrapping(map->dim);
11294 /* Is the range of "map" a wrapped relation?
11296 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11298 if (!map)
11299 return isl_bool_error;
11301 return isl_space_range_is_wrapping(map->dim);
11304 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11306 bmap = isl_basic_map_cow(bmap);
11307 if (!bmap)
11308 return NULL;
11310 bmap->dim = isl_space_wrap(bmap->dim);
11311 if (!bmap->dim)
11312 goto error;
11314 bmap = isl_basic_map_finalize(bmap);
11316 return bset_from_bmap(bmap);
11317 error:
11318 isl_basic_map_free(bmap);
11319 return NULL;
11322 /* Given a map A -> B, return the set (A -> B).
11324 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11326 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11329 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11331 bset = isl_basic_set_cow(bset);
11332 if (!bset)
11333 return NULL;
11335 bset->dim = isl_space_unwrap(bset->dim);
11336 if (!bset->dim)
11337 goto error;
11339 bset = isl_basic_set_finalize(bset);
11341 return bset_to_bmap(bset);
11342 error:
11343 isl_basic_set_free(bset);
11344 return NULL;
11347 /* Given a set (A -> B), return the map A -> B.
11348 * Error out if "set" is not of the form (A -> B).
11350 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11352 return isl_map_change_space(set, &isl_set_is_wrapping,
11353 "not a wrapping set", &isl_space_unwrap);
11356 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11357 enum isl_dim_type type)
11359 if (!bmap)
11360 return NULL;
11362 if (!isl_space_is_named_or_nested(bmap->dim, type))
11363 return bmap;
11365 bmap = isl_basic_map_cow(bmap);
11366 if (!bmap)
11367 return NULL;
11369 bmap->dim = isl_space_reset(bmap->dim, type);
11370 if (!bmap->dim)
11371 goto error;
11373 bmap = isl_basic_map_finalize(bmap);
11375 return bmap;
11376 error:
11377 isl_basic_map_free(bmap);
11378 return NULL;
11381 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11382 enum isl_dim_type type)
11384 int i;
11386 if (!map)
11387 return NULL;
11389 if (!isl_space_is_named_or_nested(map->dim, type))
11390 return map;
11392 map = isl_map_cow(map);
11393 if (!map)
11394 return NULL;
11396 for (i = 0; i < map->n; ++i) {
11397 map->p[i] = isl_basic_map_reset(map->p[i], type);
11398 if (!map->p[i])
11399 goto error;
11401 map->dim = isl_space_reset(map->dim, type);
11402 if (!map->dim)
11403 goto error;
11405 return map;
11406 error:
11407 isl_map_free(map);
11408 return NULL;
11411 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11413 if (!bmap)
11414 return NULL;
11416 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11417 return bmap;
11419 bmap = isl_basic_map_cow(bmap);
11420 if (!bmap)
11421 return NULL;
11423 bmap->dim = isl_space_flatten(bmap->dim);
11424 if (!bmap->dim)
11425 goto error;
11427 bmap = isl_basic_map_finalize(bmap);
11429 return bmap;
11430 error:
11431 isl_basic_map_free(bmap);
11432 return NULL;
11435 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11437 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11440 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11441 __isl_take isl_basic_map *bmap)
11443 if (!bmap)
11444 return NULL;
11446 if (!bmap->dim->nested[0])
11447 return bmap;
11449 bmap = isl_basic_map_cow(bmap);
11450 if (!bmap)
11451 return NULL;
11453 bmap->dim = isl_space_flatten_domain(bmap->dim);
11454 if (!bmap->dim)
11455 goto error;
11457 bmap = isl_basic_map_finalize(bmap);
11459 return bmap;
11460 error:
11461 isl_basic_map_free(bmap);
11462 return NULL;
11465 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11466 __isl_take isl_basic_map *bmap)
11468 if (!bmap)
11469 return NULL;
11471 if (!bmap->dim->nested[1])
11472 return bmap;
11474 bmap = isl_basic_map_cow(bmap);
11475 if (!bmap)
11476 return NULL;
11478 bmap->dim = isl_space_flatten_range(bmap->dim);
11479 if (!bmap->dim)
11480 goto error;
11482 bmap = isl_basic_map_finalize(bmap);
11484 return bmap;
11485 error:
11486 isl_basic_map_free(bmap);
11487 return NULL;
11490 /* Remove any internal structure from the spaces of domain and range of "map".
11492 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11494 if (!map)
11495 return NULL;
11497 if (!map->dim->nested[0] && !map->dim->nested[1])
11498 return map;
11500 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11503 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11505 return set_from_map(isl_map_flatten(set_to_map(set)));
11508 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11510 isl_space *dim, *flat_dim;
11511 isl_map *map;
11513 dim = isl_set_get_space(set);
11514 flat_dim = isl_space_flatten(isl_space_copy(dim));
11515 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11516 map = isl_map_intersect_domain(map, set);
11518 return map;
11521 /* Remove any internal structure from the space of the domain of "map".
11523 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11525 if (!map)
11526 return NULL;
11528 if (!map->dim->nested[0])
11529 return map;
11531 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11534 /* Remove any internal structure from the space of the range of "map".
11536 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11538 if (!map)
11539 return NULL;
11541 if (!map->dim->nested[1])
11542 return map;
11544 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11547 /* Reorder the dimensions of "bmap" according to the given dim_map
11548 * and set the dimension specification to "dim" and
11549 * perform Gaussian elimination on the result.
11551 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11552 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11554 isl_basic_map *res;
11555 unsigned flags;
11557 bmap = isl_basic_map_cow(bmap);
11558 if (!bmap || !dim || !dim_map)
11559 goto error;
11561 flags = bmap->flags;
11562 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11563 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11564 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11565 res = isl_basic_map_alloc_space(dim,
11566 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11567 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11568 if (res)
11569 res->flags = flags;
11570 res = isl_basic_map_gauss(res, NULL);
11571 res = isl_basic_map_finalize(res);
11572 return res;
11573 error:
11574 free(dim_map);
11575 isl_basic_map_free(bmap);
11576 isl_space_free(dim);
11577 return NULL;
11580 /* Reorder the dimensions of "map" according to given reordering.
11582 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11583 __isl_take isl_reordering *r)
11585 int i;
11586 struct isl_dim_map *dim_map;
11588 map = isl_map_cow(map);
11589 dim_map = isl_dim_map_from_reordering(r);
11590 if (!map || !r || !dim_map)
11591 goto error;
11593 for (i = 0; i < map->n; ++i) {
11594 struct isl_dim_map *dim_map_i;
11596 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11598 map->p[i] = isl_basic_map_realign(map->p[i],
11599 isl_space_copy(r->dim), dim_map_i);
11601 if (!map->p[i])
11602 goto error;
11605 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11607 isl_reordering_free(r);
11608 free(dim_map);
11609 return map;
11610 error:
11611 free(dim_map);
11612 isl_map_free(map);
11613 isl_reordering_free(r);
11614 return NULL;
11617 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11618 __isl_take isl_reordering *r)
11620 return set_from_map(isl_map_realign(set_to_map(set), r));
11623 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11624 __isl_take isl_space *model)
11626 isl_ctx *ctx;
11628 if (!map || !model)
11629 goto error;
11631 ctx = isl_space_get_ctx(model);
11632 if (!isl_space_has_named_params(model))
11633 isl_die(ctx, isl_error_invalid,
11634 "model has unnamed parameters", goto error);
11635 if (!isl_space_has_named_params(map->dim))
11636 isl_die(ctx, isl_error_invalid,
11637 "relation has unnamed parameters", goto error);
11638 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11639 isl_reordering *exp;
11641 model = isl_space_drop_dims(model, isl_dim_in,
11642 0, isl_space_dim(model, isl_dim_in));
11643 model = isl_space_drop_dims(model, isl_dim_out,
11644 0, isl_space_dim(model, isl_dim_out));
11645 exp = isl_parameter_alignment_reordering(map->dim, model);
11646 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11647 map = isl_map_realign(map, exp);
11650 isl_space_free(model);
11651 return map;
11652 error:
11653 isl_space_free(model);
11654 isl_map_free(map);
11655 return NULL;
11658 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11659 __isl_take isl_space *model)
11661 return isl_map_align_params(set, model);
11664 /* Align the parameters of "bmap" to those of "model", introducing
11665 * additional parameters if needed.
11667 __isl_give isl_basic_map *isl_basic_map_align_params(
11668 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11670 isl_ctx *ctx;
11672 if (!bmap || !model)
11673 goto error;
11675 ctx = isl_space_get_ctx(model);
11676 if (!isl_space_has_named_params(model))
11677 isl_die(ctx, isl_error_invalid,
11678 "model has unnamed parameters", goto error);
11679 if (!isl_space_has_named_params(bmap->dim))
11680 isl_die(ctx, isl_error_invalid,
11681 "relation has unnamed parameters", goto error);
11682 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11683 isl_reordering *exp;
11684 struct isl_dim_map *dim_map;
11686 model = isl_space_drop_dims(model, isl_dim_in,
11687 0, isl_space_dim(model, isl_dim_in));
11688 model = isl_space_drop_dims(model, isl_dim_out,
11689 0, isl_space_dim(model, isl_dim_out));
11690 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11691 exp = isl_reordering_extend_space(exp,
11692 isl_basic_map_get_space(bmap));
11693 dim_map = isl_dim_map_from_reordering(exp);
11694 bmap = isl_basic_map_realign(bmap,
11695 exp ? isl_space_copy(exp->dim) : NULL,
11696 isl_dim_map_extend(dim_map, bmap));
11697 isl_reordering_free(exp);
11698 free(dim_map);
11701 isl_space_free(model);
11702 return bmap;
11703 error:
11704 isl_space_free(model);
11705 isl_basic_map_free(bmap);
11706 return NULL;
11709 /* Align the parameters of "bset" to those of "model", introducing
11710 * additional parameters if needed.
11712 __isl_give isl_basic_set *isl_basic_set_align_params(
11713 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11715 return isl_basic_map_align_params(bset, model);
11718 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11719 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11720 enum isl_dim_type c2, enum isl_dim_type c3,
11721 enum isl_dim_type c4, enum isl_dim_type c5)
11723 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11724 struct isl_mat *mat;
11725 int i, j, k;
11726 int pos;
11728 if (!bmap)
11729 return NULL;
11730 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11731 isl_basic_map_total_dim(bmap) + 1);
11732 if (!mat)
11733 return NULL;
11734 for (i = 0; i < bmap->n_eq; ++i)
11735 for (j = 0, pos = 0; j < 5; ++j) {
11736 int off = isl_basic_map_offset(bmap, c[j]);
11737 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11738 isl_int_set(mat->row[i][pos],
11739 bmap->eq[i][off + k]);
11740 ++pos;
11744 return mat;
11747 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11748 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11749 enum isl_dim_type c2, enum isl_dim_type c3,
11750 enum isl_dim_type c4, enum isl_dim_type c5)
11752 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11753 struct isl_mat *mat;
11754 int i, j, k;
11755 int pos;
11757 if (!bmap)
11758 return NULL;
11759 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11760 isl_basic_map_total_dim(bmap) + 1);
11761 if (!mat)
11762 return NULL;
11763 for (i = 0; i < bmap->n_ineq; ++i)
11764 for (j = 0, pos = 0; j < 5; ++j) {
11765 int off = isl_basic_map_offset(bmap, c[j]);
11766 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11767 isl_int_set(mat->row[i][pos],
11768 bmap->ineq[i][off + k]);
11769 ++pos;
11773 return mat;
11776 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11777 __isl_take isl_space *dim,
11778 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11779 enum isl_dim_type c2, enum isl_dim_type c3,
11780 enum isl_dim_type c4, enum isl_dim_type c5)
11782 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11783 isl_basic_map *bmap;
11784 unsigned total;
11785 unsigned extra;
11786 int i, j, k, l;
11787 int pos;
11789 if (!dim || !eq || !ineq)
11790 goto error;
11792 if (eq->n_col != ineq->n_col)
11793 isl_die(dim->ctx, isl_error_invalid,
11794 "equalities and inequalities matrices should have "
11795 "same number of columns", goto error);
11797 total = 1 + isl_space_dim(dim, isl_dim_all);
11799 if (eq->n_col < total)
11800 isl_die(dim->ctx, isl_error_invalid,
11801 "number of columns too small", goto error);
11803 extra = eq->n_col - total;
11805 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11806 eq->n_row, ineq->n_row);
11807 if (!bmap)
11808 goto error;
11809 for (i = 0; i < extra; ++i) {
11810 k = isl_basic_map_alloc_div(bmap);
11811 if (k < 0)
11812 goto error;
11813 isl_int_set_si(bmap->div[k][0], 0);
11815 for (i = 0; i < eq->n_row; ++i) {
11816 l = isl_basic_map_alloc_equality(bmap);
11817 if (l < 0)
11818 goto error;
11819 for (j = 0, pos = 0; j < 5; ++j) {
11820 int off = isl_basic_map_offset(bmap, c[j]);
11821 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11822 isl_int_set(bmap->eq[l][off + k],
11823 eq->row[i][pos]);
11824 ++pos;
11828 for (i = 0; i < ineq->n_row; ++i) {
11829 l = isl_basic_map_alloc_inequality(bmap);
11830 if (l < 0)
11831 goto error;
11832 for (j = 0, pos = 0; j < 5; ++j) {
11833 int off = isl_basic_map_offset(bmap, c[j]);
11834 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11835 isl_int_set(bmap->ineq[l][off + k],
11836 ineq->row[i][pos]);
11837 ++pos;
11842 isl_space_free(dim);
11843 isl_mat_free(eq);
11844 isl_mat_free(ineq);
11846 bmap = isl_basic_map_simplify(bmap);
11847 return isl_basic_map_finalize(bmap);
11848 error:
11849 isl_space_free(dim);
11850 isl_mat_free(eq);
11851 isl_mat_free(ineq);
11852 return NULL;
11855 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11856 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11857 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11859 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11860 c1, c2, c3, c4, isl_dim_in);
11863 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11864 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11865 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11867 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11868 c1, c2, c3, c4, isl_dim_in);
11871 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11872 __isl_take isl_space *dim,
11873 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11874 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11876 isl_basic_map *bmap;
11877 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11878 c1, c2, c3, c4, isl_dim_in);
11879 return bset_from_bmap(bmap);
11882 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11884 if (!bmap)
11885 return isl_bool_error;
11887 return isl_space_can_zip(bmap->dim);
11890 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11892 if (!map)
11893 return isl_bool_error;
11895 return isl_space_can_zip(map->dim);
11898 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11899 * (A -> C) -> (B -> D).
11901 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11903 unsigned pos;
11904 unsigned n1;
11905 unsigned n2;
11907 if (!bmap)
11908 return NULL;
11910 if (!isl_basic_map_can_zip(bmap))
11911 isl_die(bmap->ctx, isl_error_invalid,
11912 "basic map cannot be zipped", goto error);
11913 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11914 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11915 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11916 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11917 bmap = isl_basic_map_cow(bmap);
11918 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11919 if (!bmap)
11920 return NULL;
11921 bmap->dim = isl_space_zip(bmap->dim);
11922 if (!bmap->dim)
11923 goto error;
11924 bmap = isl_basic_map_mark_final(bmap);
11925 return bmap;
11926 error:
11927 isl_basic_map_free(bmap);
11928 return NULL;
11931 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11932 * (A -> C) -> (B -> D).
11934 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11936 int i;
11938 if (!map)
11939 return NULL;
11941 if (!isl_map_can_zip(map))
11942 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11943 goto error);
11945 map = isl_map_cow(map);
11946 if (!map)
11947 return NULL;
11949 for (i = 0; i < map->n; ++i) {
11950 map->p[i] = isl_basic_map_zip(map->p[i]);
11951 if (!map->p[i])
11952 goto error;
11955 map->dim = isl_space_zip(map->dim);
11956 if (!map->dim)
11957 goto error;
11959 return map;
11960 error:
11961 isl_map_free(map);
11962 return NULL;
11965 /* Can we apply isl_basic_map_curry to "bmap"?
11966 * That is, does it have a nested relation in its domain?
11968 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11970 if (!bmap)
11971 return isl_bool_error;
11973 return isl_space_can_curry(bmap->dim);
11976 /* Can we apply isl_map_curry to "map"?
11977 * That is, does it have a nested relation in its domain?
11979 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11981 if (!map)
11982 return isl_bool_error;
11984 return isl_space_can_curry(map->dim);
11987 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11988 * A -> (B -> C).
11990 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11993 if (!bmap)
11994 return NULL;
11996 if (!isl_basic_map_can_curry(bmap))
11997 isl_die(bmap->ctx, isl_error_invalid,
11998 "basic map cannot be curried", goto error);
11999 bmap = isl_basic_map_cow(bmap);
12000 if (!bmap)
12001 return NULL;
12002 bmap->dim = isl_space_curry(bmap->dim);
12003 if (!bmap->dim)
12004 goto error;
12005 bmap = isl_basic_map_mark_final(bmap);
12006 return bmap;
12007 error:
12008 isl_basic_map_free(bmap);
12009 return NULL;
12012 /* Given a map (A -> B) -> C, return the corresponding map
12013 * A -> (B -> C).
12015 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12017 return isl_map_change_space(map, &isl_map_can_curry,
12018 "map cannot be curried", &isl_space_curry);
12021 /* Can isl_map_range_curry be applied to "map"?
12022 * That is, does it have a nested relation in its range,
12023 * the domain of which is itself a nested relation?
12025 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12027 if (!map)
12028 return isl_bool_error;
12030 return isl_space_can_range_curry(map->dim);
12033 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12034 * A -> (B -> (C -> D)).
12036 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12038 return isl_map_change_space(map, &isl_map_can_range_curry,
12039 "map range cannot be curried",
12040 &isl_space_range_curry);
12043 /* Can we apply isl_basic_map_uncurry to "bmap"?
12044 * That is, does it have a nested relation in its domain?
12046 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12048 if (!bmap)
12049 return isl_bool_error;
12051 return isl_space_can_uncurry(bmap->dim);
12054 /* Can we apply isl_map_uncurry to "map"?
12055 * That is, does it have a nested relation in its domain?
12057 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12059 if (!map)
12060 return isl_bool_error;
12062 return isl_space_can_uncurry(map->dim);
12065 /* Given a basic map A -> (B -> C), return the corresponding basic map
12066 * (A -> B) -> C.
12068 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12071 if (!bmap)
12072 return NULL;
12074 if (!isl_basic_map_can_uncurry(bmap))
12075 isl_die(bmap->ctx, isl_error_invalid,
12076 "basic map cannot be uncurried",
12077 return isl_basic_map_free(bmap));
12078 bmap = isl_basic_map_cow(bmap);
12079 if (!bmap)
12080 return NULL;
12081 bmap->dim = isl_space_uncurry(bmap->dim);
12082 if (!bmap->dim)
12083 return isl_basic_map_free(bmap);
12084 bmap = isl_basic_map_mark_final(bmap);
12085 return bmap;
12088 /* Given a map A -> (B -> C), return the corresponding map
12089 * (A -> B) -> C.
12091 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12093 return isl_map_change_space(map, &isl_map_can_uncurry,
12094 "map cannot be uncurried", &isl_space_uncurry);
12097 /* Construct a basic map mapping the domain of the affine expression
12098 * to a one-dimensional range prescribed by the affine expression.
12099 * If "rational" is set, then construct a rational basic map.
12101 * A NaN affine expression cannot be converted to a basic map.
12103 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12104 __isl_take isl_aff *aff, int rational)
12106 int k;
12107 int pos;
12108 isl_bool is_nan;
12109 isl_local_space *ls;
12110 isl_basic_map *bmap = NULL;
12112 if (!aff)
12113 return NULL;
12114 is_nan = isl_aff_is_nan(aff);
12115 if (is_nan < 0)
12116 goto error;
12117 if (is_nan)
12118 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12119 "cannot convert NaN", goto error);
12121 ls = isl_aff_get_local_space(aff);
12122 bmap = isl_basic_map_from_local_space(ls);
12123 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12124 k = isl_basic_map_alloc_equality(bmap);
12125 if (k < 0)
12126 goto error;
12128 pos = isl_basic_map_offset(bmap, isl_dim_out);
12129 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12130 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12131 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12132 aff->v->size - (pos + 1));
12134 isl_aff_free(aff);
12135 if (rational)
12136 bmap = isl_basic_map_set_rational(bmap);
12137 bmap = isl_basic_map_finalize(bmap);
12138 return bmap;
12139 error:
12140 isl_aff_free(aff);
12141 isl_basic_map_free(bmap);
12142 return NULL;
12145 /* Construct a basic map mapping the domain of the affine expression
12146 * to a one-dimensional range prescribed by the affine expression.
12148 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12150 return isl_basic_map_from_aff2(aff, 0);
12153 /* Construct a map mapping the domain of the affine expression
12154 * to a one-dimensional range prescribed by the affine expression.
12156 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12158 isl_basic_map *bmap;
12160 bmap = isl_basic_map_from_aff(aff);
12161 return isl_map_from_basic_map(bmap);
12164 /* Construct a basic map mapping the domain the multi-affine expression
12165 * to its range, with each dimension in the range equated to the
12166 * corresponding affine expression.
12167 * If "rational" is set, then construct a rational basic map.
12169 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12170 __isl_take isl_multi_aff *maff, int rational)
12172 int i;
12173 isl_space *space;
12174 isl_basic_map *bmap;
12176 if (!maff)
12177 return NULL;
12179 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12180 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12181 "invalid space", goto error);
12183 space = isl_space_domain(isl_multi_aff_get_space(maff));
12184 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12185 if (rational)
12186 bmap = isl_basic_map_set_rational(bmap);
12188 for (i = 0; i < maff->n; ++i) {
12189 isl_aff *aff;
12190 isl_basic_map *bmap_i;
12192 aff = isl_aff_copy(maff->p[i]);
12193 bmap_i = isl_basic_map_from_aff2(aff, rational);
12195 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12198 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12200 isl_multi_aff_free(maff);
12201 return bmap;
12202 error:
12203 isl_multi_aff_free(maff);
12204 return NULL;
12207 /* Construct a basic map mapping the domain the multi-affine expression
12208 * to its range, with each dimension in the range equated to the
12209 * corresponding affine expression.
12211 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12212 __isl_take isl_multi_aff *ma)
12214 return isl_basic_map_from_multi_aff2(ma, 0);
12217 /* Construct a map mapping the domain the multi-affine expression
12218 * to its range, with each dimension in the range equated to the
12219 * corresponding affine expression.
12221 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12223 isl_basic_map *bmap;
12225 bmap = isl_basic_map_from_multi_aff(maff);
12226 return isl_map_from_basic_map(bmap);
12229 /* Construct a basic map mapping a domain in the given space to
12230 * to an n-dimensional range, with n the number of elements in the list,
12231 * where each coordinate in the range is prescribed by the
12232 * corresponding affine expression.
12233 * The domains of all affine expressions in the list are assumed to match
12234 * domain_dim.
12236 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12237 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12239 int i;
12240 isl_space *dim;
12241 isl_basic_map *bmap;
12243 if (!list)
12244 return NULL;
12246 dim = isl_space_from_domain(domain_dim);
12247 bmap = isl_basic_map_universe(dim);
12249 for (i = 0; i < list->n; ++i) {
12250 isl_aff *aff;
12251 isl_basic_map *bmap_i;
12253 aff = isl_aff_copy(list->p[i]);
12254 bmap_i = isl_basic_map_from_aff(aff);
12256 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12259 isl_aff_list_free(list);
12260 return bmap;
12263 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12264 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12266 return isl_map_equate(set, type1, pos1, type2, pos2);
12269 /* Construct a basic map where the given dimensions are equal to each other.
12271 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12272 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12274 isl_basic_map *bmap = NULL;
12275 int i;
12277 if (!space)
12278 return NULL;
12280 if (pos1 >= isl_space_dim(space, type1))
12281 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12282 "index out of bounds", goto error);
12283 if (pos2 >= isl_space_dim(space, type2))
12284 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12285 "index out of bounds", goto error);
12287 if (type1 == type2 && pos1 == pos2)
12288 return isl_basic_map_universe(space);
12290 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12291 i = isl_basic_map_alloc_equality(bmap);
12292 if (i < 0)
12293 goto error;
12294 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12295 pos1 += isl_basic_map_offset(bmap, type1);
12296 pos2 += isl_basic_map_offset(bmap, type2);
12297 isl_int_set_si(bmap->eq[i][pos1], -1);
12298 isl_int_set_si(bmap->eq[i][pos2], 1);
12299 bmap = isl_basic_map_finalize(bmap);
12300 isl_space_free(space);
12301 return bmap;
12302 error:
12303 isl_space_free(space);
12304 isl_basic_map_free(bmap);
12305 return NULL;
12308 /* Add a constraint imposing that the given two dimensions are equal.
12310 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12311 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12313 isl_basic_map *eq;
12315 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12317 bmap = isl_basic_map_intersect(bmap, eq);
12319 return bmap;
12322 /* Add a constraint imposing that the given two dimensions are equal.
12324 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12325 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12327 isl_basic_map *bmap;
12329 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12331 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12333 return map;
12336 /* Add a constraint imposing that the given two dimensions have opposite values.
12338 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12339 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12341 isl_basic_map *bmap = NULL;
12342 int i;
12344 if (!map)
12345 return NULL;
12347 if (pos1 >= isl_map_dim(map, type1))
12348 isl_die(map->ctx, isl_error_invalid,
12349 "index out of bounds", goto error);
12350 if (pos2 >= isl_map_dim(map, type2))
12351 isl_die(map->ctx, isl_error_invalid,
12352 "index out of bounds", goto error);
12354 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12355 i = isl_basic_map_alloc_equality(bmap);
12356 if (i < 0)
12357 goto error;
12358 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12359 pos1 += isl_basic_map_offset(bmap, type1);
12360 pos2 += isl_basic_map_offset(bmap, type2);
12361 isl_int_set_si(bmap->eq[i][pos1], 1);
12362 isl_int_set_si(bmap->eq[i][pos2], 1);
12363 bmap = isl_basic_map_finalize(bmap);
12365 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12367 return map;
12368 error:
12369 isl_basic_map_free(bmap);
12370 isl_map_free(map);
12371 return NULL;
12374 /* Construct a constraint imposing that the value of the first dimension is
12375 * greater than or equal to that of the second.
12377 static __isl_give isl_constraint *constraint_order_ge(
12378 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12379 enum isl_dim_type type2, int pos2)
12381 isl_constraint *c;
12383 if (!space)
12384 return NULL;
12386 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12388 if (pos1 >= isl_constraint_dim(c, type1))
12389 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12390 "index out of bounds", return isl_constraint_free(c));
12391 if (pos2 >= isl_constraint_dim(c, type2))
12392 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12393 "index out of bounds", return isl_constraint_free(c));
12395 if (type1 == type2 && pos1 == pos2)
12396 return c;
12398 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12399 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12401 return c;
12404 /* Add a constraint imposing that the value of the first dimension is
12405 * greater than or equal to that of the second.
12407 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12408 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12410 isl_constraint *c;
12411 isl_space *space;
12413 if (type1 == type2 && pos1 == pos2)
12414 return bmap;
12415 space = isl_basic_map_get_space(bmap);
12416 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12417 bmap = isl_basic_map_add_constraint(bmap, c);
12419 return bmap;
12422 /* Add a constraint imposing that the value of the first dimension is
12423 * greater than or equal to that of the second.
12425 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12426 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12428 isl_constraint *c;
12429 isl_space *space;
12431 if (type1 == type2 && pos1 == pos2)
12432 return map;
12433 space = isl_map_get_space(map);
12434 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12435 map = isl_map_add_constraint(map, c);
12437 return map;
12440 /* Add a constraint imposing that the value of the first dimension is
12441 * less than or equal to that of the second.
12443 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12444 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12446 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12449 /* Construct a basic map where the value of the first dimension is
12450 * greater than that of the second.
12452 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12453 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12455 isl_basic_map *bmap = NULL;
12456 int i;
12458 if (!space)
12459 return NULL;
12461 if (pos1 >= isl_space_dim(space, type1))
12462 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12463 "index out of bounds", goto error);
12464 if (pos2 >= isl_space_dim(space, type2))
12465 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12466 "index out of bounds", goto error);
12468 if (type1 == type2 && pos1 == pos2)
12469 return isl_basic_map_empty(space);
12471 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12472 i = isl_basic_map_alloc_inequality(bmap);
12473 if (i < 0)
12474 return isl_basic_map_free(bmap);
12475 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12476 pos1 += isl_basic_map_offset(bmap, type1);
12477 pos2 += isl_basic_map_offset(bmap, type2);
12478 isl_int_set_si(bmap->ineq[i][pos1], 1);
12479 isl_int_set_si(bmap->ineq[i][pos2], -1);
12480 isl_int_set_si(bmap->ineq[i][0], -1);
12481 bmap = isl_basic_map_finalize(bmap);
12483 return bmap;
12484 error:
12485 isl_space_free(space);
12486 isl_basic_map_free(bmap);
12487 return NULL;
12490 /* Add a constraint imposing that the value of the first dimension is
12491 * greater than that of the second.
12493 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12494 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12496 isl_basic_map *gt;
12498 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12500 bmap = isl_basic_map_intersect(bmap, gt);
12502 return bmap;
12505 /* Add a constraint imposing that the value of the first dimension is
12506 * greater than that of the second.
12508 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12509 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12511 isl_basic_map *bmap;
12513 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12515 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12517 return map;
12520 /* Add a constraint imposing that the value of the first dimension is
12521 * smaller than that of the second.
12523 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12524 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12526 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12529 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12530 int pos)
12532 isl_aff *div;
12533 isl_local_space *ls;
12535 if (!bmap)
12536 return NULL;
12538 if (!isl_basic_map_divs_known(bmap))
12539 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12540 "some divs are unknown", return NULL);
12542 ls = isl_basic_map_get_local_space(bmap);
12543 div = isl_local_space_get_div(ls, pos);
12544 isl_local_space_free(ls);
12546 return div;
12549 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12550 int pos)
12552 return isl_basic_map_get_div(bset, pos);
12555 /* Plug in "subs" for dimension "type", "pos" of "bset".
12557 * Let i be the dimension to replace and let "subs" be of the form
12559 * f/d
12561 * Any integer division with a non-zero coefficient for i,
12563 * floor((a i + g)/m)
12565 * is replaced by
12567 * floor((a f + d g)/(m d))
12569 * Constraints of the form
12571 * a i + g
12573 * are replaced by
12575 * a f + d g
12577 * We currently require that "subs" is an integral expression.
12578 * Handling rational expressions may require us to add stride constraints
12579 * as we do in isl_basic_set_preimage_multi_aff.
12581 __isl_give isl_basic_set *isl_basic_set_substitute(
12582 __isl_take isl_basic_set *bset,
12583 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12585 int i;
12586 isl_int v;
12587 isl_ctx *ctx;
12589 if (bset && isl_basic_set_plain_is_empty(bset))
12590 return bset;
12592 bset = isl_basic_set_cow(bset);
12593 if (!bset || !subs)
12594 goto error;
12596 ctx = isl_basic_set_get_ctx(bset);
12597 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12598 isl_die(ctx, isl_error_invalid,
12599 "spaces don't match", goto error);
12600 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12601 isl_die(ctx, isl_error_unsupported,
12602 "cannot handle divs yet", goto error);
12603 if (!isl_int_is_one(subs->v->el[0]))
12604 isl_die(ctx, isl_error_invalid,
12605 "can only substitute integer expressions", goto error);
12607 pos += isl_basic_set_offset(bset, type);
12609 isl_int_init(v);
12611 for (i = 0; i < bset->n_eq; ++i) {
12612 if (isl_int_is_zero(bset->eq[i][pos]))
12613 continue;
12614 isl_int_set(v, bset->eq[i][pos]);
12615 isl_int_set_si(bset->eq[i][pos], 0);
12616 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12617 v, subs->v->el + 1, subs->v->size - 1);
12620 for (i = 0; i < bset->n_ineq; ++i) {
12621 if (isl_int_is_zero(bset->ineq[i][pos]))
12622 continue;
12623 isl_int_set(v, bset->ineq[i][pos]);
12624 isl_int_set_si(bset->ineq[i][pos], 0);
12625 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12626 v, subs->v->el + 1, subs->v->size - 1);
12629 for (i = 0; i < bset->n_div; ++i) {
12630 if (isl_int_is_zero(bset->div[i][1 + pos]))
12631 continue;
12632 isl_int_set(v, bset->div[i][1 + pos]);
12633 isl_int_set_si(bset->div[i][1 + pos], 0);
12634 isl_seq_combine(bset->div[i] + 1,
12635 subs->v->el[0], bset->div[i] + 1,
12636 v, subs->v->el + 1, subs->v->size - 1);
12637 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12640 isl_int_clear(v);
12642 bset = isl_basic_set_simplify(bset);
12643 return isl_basic_set_finalize(bset);
12644 error:
12645 isl_basic_set_free(bset);
12646 return NULL;
12649 /* Plug in "subs" for dimension "type", "pos" of "set".
12651 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12652 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12654 int i;
12656 if (set && isl_set_plain_is_empty(set))
12657 return set;
12659 set = isl_set_cow(set);
12660 if (!set || !subs)
12661 goto error;
12663 for (i = set->n - 1; i >= 0; --i) {
12664 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12665 if (remove_if_empty(set, i) < 0)
12666 goto error;
12669 return set;
12670 error:
12671 isl_set_free(set);
12672 return NULL;
12675 /* Check if the range of "ma" is compatible with the domain or range
12676 * (depending on "type") of "bmap".
12677 * Return -1 if anything is wrong.
12679 static int check_basic_map_compatible_range_multi_aff(
12680 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12681 __isl_keep isl_multi_aff *ma)
12683 int m;
12684 isl_space *ma_space;
12686 ma_space = isl_multi_aff_get_space(ma);
12688 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12689 if (m < 0)
12690 goto error;
12691 if (!m)
12692 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12693 "parameters don't match", goto error);
12694 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12695 if (m < 0)
12696 goto error;
12697 if (!m)
12698 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12699 "spaces don't match", goto error);
12701 isl_space_free(ma_space);
12702 return m;
12703 error:
12704 isl_space_free(ma_space);
12705 return -1;
12708 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12709 * coefficients before the transformed range of dimensions,
12710 * the "n_after" coefficients after the transformed range of dimensions
12711 * and the coefficients of the other divs in "bmap".
12713 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12714 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12716 int i;
12717 int n_param;
12718 int n_set;
12719 isl_local_space *ls;
12721 if (n_div == 0)
12722 return 0;
12724 ls = isl_aff_get_domain_local_space(ma->p[0]);
12725 if (!ls)
12726 return -1;
12728 n_param = isl_local_space_dim(ls, isl_dim_param);
12729 n_set = isl_local_space_dim(ls, isl_dim_set);
12730 for (i = 0; i < n_div; ++i) {
12731 int o_bmap = 0, o_ls = 0;
12733 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12734 o_bmap += 1 + 1 + n_param;
12735 o_ls += 1 + 1 + n_param;
12736 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12737 o_bmap += n_before;
12738 isl_seq_cpy(bmap->div[i] + o_bmap,
12739 ls->div->row[i] + o_ls, n_set);
12740 o_bmap += n_set;
12741 o_ls += n_set;
12742 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12743 o_bmap += n_after;
12744 isl_seq_cpy(bmap->div[i] + o_bmap,
12745 ls->div->row[i] + o_ls, n_div);
12746 o_bmap += n_div;
12747 o_ls += n_div;
12748 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12749 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12750 goto error;
12753 isl_local_space_free(ls);
12754 return 0;
12755 error:
12756 isl_local_space_free(ls);
12757 return -1;
12760 /* How many stride constraints does "ma" enforce?
12761 * That is, how many of the affine expressions have a denominator
12762 * different from one?
12764 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12766 int i;
12767 int strides = 0;
12769 for (i = 0; i < ma->n; ++i)
12770 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12771 strides++;
12773 return strides;
12776 /* For each affine expression in ma of the form
12778 * x_i = (f_i y + h_i)/m_i
12780 * with m_i different from one, add a constraint to "bmap"
12781 * of the form
12783 * f_i y + h_i = m_i alpha_i
12785 * with alpha_i an additional existentially quantified variable.
12787 * The input variables of "ma" correspond to a subset of the variables
12788 * of "bmap". There are "n_before" variables in "bmap" before this
12789 * subset and "n_after" variables after this subset.
12790 * The integer divisions of the affine expressions in "ma" are assumed
12791 * to have been aligned. There are "n_div_ma" of them and
12792 * they appear first in "bmap", straight after the "n_after" variables.
12794 static __isl_give isl_basic_map *add_ma_strides(
12795 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12796 int n_before, int n_after, int n_div_ma)
12798 int i, k;
12799 int div;
12800 int total;
12801 int n_param;
12802 int n_in;
12804 total = isl_basic_map_total_dim(bmap);
12805 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12806 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12807 for (i = 0; i < ma->n; ++i) {
12808 int o_bmap = 0, o_ma = 1;
12810 if (isl_int_is_one(ma->p[i]->v->el[0]))
12811 continue;
12812 div = isl_basic_map_alloc_div(bmap);
12813 k = isl_basic_map_alloc_equality(bmap);
12814 if (div < 0 || k < 0)
12815 goto error;
12816 isl_int_set_si(bmap->div[div][0], 0);
12817 isl_seq_cpy(bmap->eq[k] + o_bmap,
12818 ma->p[i]->v->el + o_ma, 1 + n_param);
12819 o_bmap += 1 + n_param;
12820 o_ma += 1 + n_param;
12821 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12822 o_bmap += n_before;
12823 isl_seq_cpy(bmap->eq[k] + o_bmap,
12824 ma->p[i]->v->el + o_ma, n_in);
12825 o_bmap += n_in;
12826 o_ma += n_in;
12827 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12828 o_bmap += n_after;
12829 isl_seq_cpy(bmap->eq[k] + o_bmap,
12830 ma->p[i]->v->el + o_ma, n_div_ma);
12831 o_bmap += n_div_ma;
12832 o_ma += n_div_ma;
12833 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12834 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12835 total++;
12838 return bmap;
12839 error:
12840 isl_basic_map_free(bmap);
12841 return NULL;
12844 /* Replace the domain or range space (depending on "type) of "space" by "set".
12846 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12847 enum isl_dim_type type, __isl_take isl_space *set)
12849 if (type == isl_dim_in) {
12850 space = isl_space_range(space);
12851 space = isl_space_map_from_domain_and_range(set, space);
12852 } else {
12853 space = isl_space_domain(space);
12854 space = isl_space_map_from_domain_and_range(space, set);
12857 return space;
12860 /* Compute the preimage of the domain or range (depending on "type")
12861 * of "bmap" under the function represented by "ma".
12862 * In other words, plug in "ma" in the domain or range of "bmap".
12863 * The result is a basic map that lives in the same space as "bmap"
12864 * except that the domain or range has been replaced by
12865 * the domain space of "ma".
12867 * If bmap is represented by
12869 * A(p) + S u + B x + T v + C(divs) >= 0,
12871 * where u and x are input and output dimensions if type == isl_dim_out
12872 * while x and v are input and output dimensions if type == isl_dim_in,
12873 * and ma is represented by
12875 * x = D(p) + F(y) + G(divs')
12877 * then the result is
12879 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12881 * The divs in the input set are similarly adjusted.
12882 * In particular
12884 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12886 * becomes
12888 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12889 * B_i G(divs') + c_i(divs))/n_i)
12891 * If bmap is not a rational map and if F(y) involves any denominators
12893 * x_i = (f_i y + h_i)/m_i
12895 * then additional constraints are added to ensure that we only
12896 * map back integer points. That is we enforce
12898 * f_i y + h_i = m_i alpha_i
12900 * with alpha_i an additional existentially quantified variable.
12902 * We first copy over the divs from "ma".
12903 * Then we add the modified constraints and divs from "bmap".
12904 * Finally, we add the stride constraints, if needed.
12906 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12907 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12908 __isl_take isl_multi_aff *ma)
12910 int i, k;
12911 isl_space *space;
12912 isl_basic_map *res = NULL;
12913 int n_before, n_after, n_div_bmap, n_div_ma;
12914 isl_int f, c1, c2, g;
12915 int rational, strides;
12917 isl_int_init(f);
12918 isl_int_init(c1);
12919 isl_int_init(c2);
12920 isl_int_init(g);
12922 ma = isl_multi_aff_align_divs(ma);
12923 if (!bmap || !ma)
12924 goto error;
12925 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12926 goto error;
12928 if (type == isl_dim_in) {
12929 n_before = 0;
12930 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12931 } else {
12932 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12933 n_after = 0;
12935 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12936 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12938 space = isl_multi_aff_get_domain_space(ma);
12939 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12940 rational = isl_basic_map_is_rational(bmap);
12941 strides = rational ? 0 : multi_aff_strides(ma);
12942 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12943 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12944 if (rational)
12945 res = isl_basic_map_set_rational(res);
12947 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12948 if (isl_basic_map_alloc_div(res) < 0)
12949 goto error;
12951 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12952 goto error;
12954 for (i = 0; i < bmap->n_eq; ++i) {
12955 k = isl_basic_map_alloc_equality(res);
12956 if (k < 0)
12957 goto error;
12958 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12959 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12962 for (i = 0; i < bmap->n_ineq; ++i) {
12963 k = isl_basic_map_alloc_inequality(res);
12964 if (k < 0)
12965 goto error;
12966 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12967 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12970 for (i = 0; i < bmap->n_div; ++i) {
12971 if (isl_int_is_zero(bmap->div[i][0])) {
12972 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12973 continue;
12975 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12976 n_before, n_after, n_div_ma, n_div_bmap,
12977 f, c1, c2, g, 1);
12980 if (strides)
12981 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12983 isl_int_clear(f);
12984 isl_int_clear(c1);
12985 isl_int_clear(c2);
12986 isl_int_clear(g);
12987 isl_basic_map_free(bmap);
12988 isl_multi_aff_free(ma);
12989 res = isl_basic_set_simplify(res);
12990 return isl_basic_map_finalize(res);
12991 error:
12992 isl_int_clear(f);
12993 isl_int_clear(c1);
12994 isl_int_clear(c2);
12995 isl_int_clear(g);
12996 isl_basic_map_free(bmap);
12997 isl_multi_aff_free(ma);
12998 isl_basic_map_free(res);
12999 return NULL;
13002 /* Compute the preimage of "bset" under the function represented by "ma".
13003 * In other words, plug in "ma" in "bset". The result is a basic set
13004 * that lives in the domain space of "ma".
13006 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13007 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13009 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13012 /* Compute the preimage of the domain of "bmap" under the function
13013 * represented by "ma".
13014 * In other words, plug in "ma" in the domain of "bmap".
13015 * The result is a basic map that lives in the same space as "bmap"
13016 * except that the domain has been replaced by the domain space of "ma".
13018 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13019 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13021 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13024 /* Compute the preimage of the range of "bmap" under the function
13025 * represented by "ma".
13026 * In other words, plug in "ma" in the range of "bmap".
13027 * The result is a basic map that lives in the same space as "bmap"
13028 * except that the range has been replaced by the domain space of "ma".
13030 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13031 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13033 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13036 /* Check if the range of "ma" is compatible with the domain or range
13037 * (depending on "type") of "map".
13038 * Return -1 if anything is wrong.
13040 static int check_map_compatible_range_multi_aff(
13041 __isl_keep isl_map *map, enum isl_dim_type type,
13042 __isl_keep isl_multi_aff *ma)
13044 int m;
13045 isl_space *ma_space;
13047 ma_space = isl_multi_aff_get_space(ma);
13048 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13049 isl_space_free(ma_space);
13050 if (m >= 0 && !m)
13051 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13052 "spaces don't match", return -1);
13053 return m;
13056 /* Compute the preimage of the domain or range (depending on "type")
13057 * of "map" under the function represented by "ma".
13058 * In other words, plug in "ma" in the domain or range of "map".
13059 * The result is a map that lives in the same space as "map"
13060 * except that the domain or range has been replaced by
13061 * the domain space of "ma".
13063 * The parameters are assumed to have been aligned.
13065 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13066 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13068 int i;
13069 isl_space *space;
13071 map = isl_map_cow(map);
13072 ma = isl_multi_aff_align_divs(ma);
13073 if (!map || !ma)
13074 goto error;
13075 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13076 goto error;
13078 for (i = 0; i < map->n; ++i) {
13079 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13080 isl_multi_aff_copy(ma));
13081 if (!map->p[i])
13082 goto error;
13085 space = isl_multi_aff_get_domain_space(ma);
13086 space = isl_space_set(isl_map_get_space(map), type, space);
13088 isl_space_free(map->dim);
13089 map->dim = space;
13090 if (!map->dim)
13091 goto error;
13093 isl_multi_aff_free(ma);
13094 if (map->n > 1)
13095 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13096 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13097 return map;
13098 error:
13099 isl_multi_aff_free(ma);
13100 isl_map_free(map);
13101 return NULL;
13104 /* Compute the preimage of the domain or range (depending on "type")
13105 * of "map" under the function represented by "ma".
13106 * In other words, plug in "ma" in the domain or range of "map".
13107 * The result is a map that lives in the same space as "map"
13108 * except that the domain or range has been replaced by
13109 * the domain space of "ma".
13111 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13112 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13114 if (!map || !ma)
13115 goto error;
13117 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
13118 return map_preimage_multi_aff(map, type, ma);
13120 if (!isl_space_has_named_params(map->dim) ||
13121 !isl_space_has_named_params(ma->space))
13122 isl_die(map->ctx, isl_error_invalid,
13123 "unaligned unnamed parameters", goto error);
13124 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13125 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13127 return map_preimage_multi_aff(map, type, ma);
13128 error:
13129 isl_multi_aff_free(ma);
13130 return isl_map_free(map);
13133 /* Compute the preimage of "set" under the function represented by "ma".
13134 * In other words, plug in "ma" in "set". The result is a set
13135 * that lives in the domain space of "ma".
13137 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13138 __isl_take isl_multi_aff *ma)
13140 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13143 /* Compute the preimage of the domain of "map" under the function
13144 * represented by "ma".
13145 * In other words, plug in "ma" in the domain of "map".
13146 * The result is a map that lives in the same space as "map"
13147 * except that the domain has been replaced by the domain space of "ma".
13149 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13150 __isl_take isl_multi_aff *ma)
13152 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13155 /* Compute the preimage of the range of "map" under the function
13156 * represented by "ma".
13157 * In other words, plug in "ma" in the range of "map".
13158 * The result is a map that lives in the same space as "map"
13159 * except that the range has been replaced by the domain space of "ma".
13161 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13162 __isl_take isl_multi_aff *ma)
13164 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13167 /* Compute the preimage of "map" under the function represented by "pma".
13168 * In other words, plug in "pma" in the domain or range of "map".
13169 * The result is a map that lives in the same space as "map",
13170 * except that the space of type "type" has been replaced by
13171 * the domain space of "pma".
13173 * The parameters of "map" and "pma" are assumed to have been aligned.
13175 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13176 __isl_take isl_map *map, enum isl_dim_type type,
13177 __isl_take isl_pw_multi_aff *pma)
13179 int i;
13180 isl_map *res;
13182 if (!pma)
13183 goto error;
13185 if (pma->n == 0) {
13186 isl_pw_multi_aff_free(pma);
13187 res = isl_map_empty(isl_map_get_space(map));
13188 isl_map_free(map);
13189 return res;
13192 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13193 isl_multi_aff_copy(pma->p[0].maff));
13194 if (type == isl_dim_in)
13195 res = isl_map_intersect_domain(res,
13196 isl_map_copy(pma->p[0].set));
13197 else
13198 res = isl_map_intersect_range(res,
13199 isl_map_copy(pma->p[0].set));
13201 for (i = 1; i < pma->n; ++i) {
13202 isl_map *res_i;
13204 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13205 isl_multi_aff_copy(pma->p[i].maff));
13206 if (type == isl_dim_in)
13207 res_i = isl_map_intersect_domain(res_i,
13208 isl_map_copy(pma->p[i].set));
13209 else
13210 res_i = isl_map_intersect_range(res_i,
13211 isl_map_copy(pma->p[i].set));
13212 res = isl_map_union(res, res_i);
13215 isl_pw_multi_aff_free(pma);
13216 isl_map_free(map);
13217 return res;
13218 error:
13219 isl_pw_multi_aff_free(pma);
13220 isl_map_free(map);
13221 return NULL;
13224 /* Compute the preimage of "map" under the function represented by "pma".
13225 * In other words, plug in "pma" in the domain or range of "map".
13226 * The result is a map that lives in the same space as "map",
13227 * except that the space of type "type" has been replaced by
13228 * the domain space of "pma".
13230 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13231 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13233 if (!map || !pma)
13234 goto error;
13236 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13237 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13239 if (!isl_space_has_named_params(map->dim) ||
13240 !isl_space_has_named_params(pma->dim))
13241 isl_die(map->ctx, isl_error_invalid,
13242 "unaligned unnamed parameters", goto error);
13243 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13244 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13246 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13247 error:
13248 isl_pw_multi_aff_free(pma);
13249 return isl_map_free(map);
13252 /* Compute the preimage of "set" under the function represented by "pma".
13253 * In other words, plug in "pma" in "set". The result is a set
13254 * that lives in the domain space of "pma".
13256 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13257 __isl_take isl_pw_multi_aff *pma)
13259 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13262 /* Compute the preimage of the domain of "map" under the function
13263 * represented by "pma".
13264 * In other words, plug in "pma" in the domain of "map".
13265 * The result is a map that lives in the same space as "map",
13266 * except that domain space has been replaced by the domain space of "pma".
13268 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13269 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13271 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13274 /* Compute the preimage of the range of "map" under the function
13275 * represented by "pma".
13276 * In other words, plug in "pma" in the range of "map".
13277 * The result is a map that lives in the same space as "map",
13278 * except that range space has been replaced by the domain space of "pma".
13280 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13281 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13283 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13286 /* Compute the preimage of "map" under the function represented by "mpa".
13287 * In other words, plug in "mpa" in the domain or range of "map".
13288 * The result is a map that lives in the same space as "map",
13289 * except that the space of type "type" has been replaced by
13290 * the domain space of "mpa".
13292 * If the map does not involve any constraints that refer to the
13293 * dimensions of the substituted space, then the only possible
13294 * effect of "mpa" on the map is to map the space to a different space.
13295 * We create a separate isl_multi_aff to effectuate this change
13296 * in order to avoid spurious splitting of the map along the pieces
13297 * of "mpa".
13299 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13300 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13302 int n;
13303 isl_pw_multi_aff *pma;
13305 if (!map || !mpa)
13306 goto error;
13308 n = isl_map_dim(map, type);
13309 if (!isl_map_involves_dims(map, type, 0, n)) {
13310 isl_space *space;
13311 isl_multi_aff *ma;
13313 space = isl_multi_pw_aff_get_space(mpa);
13314 isl_multi_pw_aff_free(mpa);
13315 ma = isl_multi_aff_zero(space);
13316 return isl_map_preimage_multi_aff(map, type, ma);
13319 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13320 return isl_map_preimage_pw_multi_aff(map, type, pma);
13321 error:
13322 isl_map_free(map);
13323 isl_multi_pw_aff_free(mpa);
13324 return NULL;
13327 /* Compute the preimage of "map" under the function represented by "mpa".
13328 * In other words, plug in "mpa" in the domain "map".
13329 * The result is a map that lives in the same space as "map",
13330 * except that domain space has been replaced by the domain space of "mpa".
13332 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13333 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13335 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13338 /* Compute the preimage of "set" by the function represented by "mpa".
13339 * In other words, plug in "mpa" in "set".
13341 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13342 __isl_take isl_multi_pw_aff *mpa)
13344 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13347 /* Are the "n" "coefficients" starting at "first" of the integer division
13348 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13349 * to each other?
13350 * The "coefficient" at position 0 is the denominator.
13351 * The "coefficient" at position 1 is the constant term.
13353 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13354 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13355 unsigned first, unsigned n)
13357 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13358 return isl_bool_error;
13359 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13360 return isl_bool_error;
13361 return isl_seq_eq(bmap1->div[pos1] + first,
13362 bmap2->div[pos2] + first, n);
13365 /* Are the integer division expressions at position "pos1" in "bmap1" and
13366 * "pos2" in "bmap2" equal to each other, except that the constant terms
13367 * are different?
13369 isl_bool isl_basic_map_equal_div_expr_except_constant(
13370 __isl_keep isl_basic_map *bmap1, int pos1,
13371 __isl_keep isl_basic_map *bmap2, int pos2)
13373 isl_bool equal;
13374 unsigned total;
13376 if (!bmap1 || !bmap2)
13377 return isl_bool_error;
13378 total = isl_basic_map_total_dim(bmap1);
13379 if (total != isl_basic_map_total_dim(bmap2))
13380 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13381 "incomparable div expressions", return isl_bool_error);
13382 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13383 0, 1);
13384 if (equal < 0 || !equal)
13385 return equal;
13386 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13387 1, 1);
13388 if (equal < 0 || equal)
13389 return isl_bool_not(equal);
13390 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13391 2, total);
13394 /* Replace the numerator of the constant term of the integer division
13395 * expression at position "div" in "bmap" by "value".
13396 * The caller guarantees that this does not change the meaning
13397 * of the input.
13399 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13400 __isl_take isl_basic_map *bmap, int div, int value)
13402 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13403 return isl_basic_map_free(bmap);
13405 isl_int_set_si(bmap->div[div][1], value);
13407 return bmap;
13410 /* Is the point "inner" internal to inequality constraint "ineq"
13411 * of "bset"?
13412 * The point is considered to be internal to the inequality constraint,
13413 * if it strictly lies on the positive side of the inequality constraint,
13414 * or if it lies on the constraint and the constraint is lexico-positive.
13416 static isl_bool is_internal(__isl_keep isl_vec *inner,
13417 __isl_keep isl_basic_set *bset, int ineq)
13419 isl_ctx *ctx;
13420 int pos;
13421 unsigned total;
13423 if (!inner || !bset)
13424 return isl_bool_error;
13426 ctx = isl_basic_set_get_ctx(bset);
13427 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13428 &ctx->normalize_gcd);
13429 if (!isl_int_is_zero(ctx->normalize_gcd))
13430 return isl_int_is_nonneg(ctx->normalize_gcd);
13432 total = isl_basic_set_dim(bset, isl_dim_all);
13433 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13434 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13437 /* Tighten the inequality constraints of "bset" that are outward with respect
13438 * to the point "vec".
13439 * That is, tighten the constraints that are not satisfied by "vec".
13441 * "vec" is a point internal to some superset S of "bset" that is used
13442 * to make the subsets of S disjoint, by tightening one half of the constraints
13443 * that separate two subsets. In particular, the constraints of S
13444 * are all satisfied by "vec" and should not be tightened.
13445 * Of the internal constraints, those that have "vec" on the outside
13446 * are tightened. The shared facet is included in the adjacent subset
13447 * with the opposite constraint.
13448 * For constraints that saturate "vec", this criterion cannot be used
13449 * to determine which of the two sides should be tightened.
13450 * Instead, the sign of the first non-zero coefficient is used
13451 * to make this choice. Note that this second criterion is never used
13452 * on the constraints of S since "vec" is interior to "S".
13454 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13455 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13457 int j;
13459 bset = isl_basic_set_cow(bset);
13460 if (!bset)
13461 return NULL;
13462 for (j = 0; j < bset->n_ineq; ++j) {
13463 isl_bool internal;
13465 internal = is_internal(vec, bset, j);
13466 if (internal < 0)
13467 return isl_basic_set_free(bset);
13468 if (internal)
13469 continue;
13470 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13473 return bset;