isl_tab.c: move up select_marked
[isl.git] / isl_map.c
blob2f1c906a176e3858d6d33cdf40d9548b9936dba2
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, k;
5328 isl_space *dim;
5329 isl_basic_map *domain;
5330 int nparam, n_in, n_out;
5331 unsigned total;
5333 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5334 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5335 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5337 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5338 domain = isl_basic_map_universe(dim);
5340 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5341 bmap = isl_basic_map_apply_range(bmap, domain);
5342 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5344 total = isl_basic_map_total_dim(bmap);
5346 for (i = 0; i < n_in; ++i) {
5347 k = isl_basic_map_alloc_equality(bmap);
5348 if (k < 0)
5349 goto error;
5350 isl_seq_clr(bmap->eq[k], 1 + total);
5351 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5352 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5355 bmap = isl_basic_map_gauss(bmap, NULL);
5356 return isl_basic_map_finalize(bmap);
5357 error:
5358 isl_basic_map_free(bmap);
5359 return NULL;
5362 __isl_give isl_basic_map *isl_basic_map_range_map(
5363 __isl_take isl_basic_map *bmap)
5365 int i, k;
5366 isl_space *dim;
5367 isl_basic_map *range;
5368 int nparam, n_in, n_out;
5369 unsigned total;
5371 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5372 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5373 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5375 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5376 range = isl_basic_map_universe(dim);
5378 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5379 bmap = isl_basic_map_apply_range(bmap, range);
5380 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5382 total = isl_basic_map_total_dim(bmap);
5384 for (i = 0; i < n_out; ++i) {
5385 k = isl_basic_map_alloc_equality(bmap);
5386 if (k < 0)
5387 goto error;
5388 isl_seq_clr(bmap->eq[k], 1 + total);
5389 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5390 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5393 bmap = isl_basic_map_gauss(bmap, NULL);
5394 return isl_basic_map_finalize(bmap);
5395 error:
5396 isl_basic_map_free(bmap);
5397 return NULL;
5400 int isl_map_may_be_set(__isl_keep isl_map *map)
5402 if (!map)
5403 return -1;
5404 return isl_space_may_be_set(map->dim);
5407 /* Is this map actually a set?
5408 * Users should never call this function. Outside of isl,
5409 * the type should indicate whether something is a set or a map.
5411 int isl_map_is_set(__isl_keep isl_map *map)
5413 if (!map)
5414 return -1;
5415 return isl_space_is_set(map->dim);
5418 struct isl_set *isl_map_range(struct isl_map *map)
5420 int i;
5421 struct isl_set *set;
5423 if (!map)
5424 goto error;
5425 if (isl_map_is_set(map))
5426 return set_from_map(map);
5428 map = isl_map_cow(map);
5429 if (!map)
5430 goto error;
5432 set = set_from_map(map);
5433 set->dim = isl_space_range(set->dim);
5434 if (!set->dim)
5435 goto error;
5436 for (i = 0; i < map->n; ++i) {
5437 set->p[i] = isl_basic_map_range(map->p[i]);
5438 if (!set->p[i])
5439 goto error;
5441 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5442 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5443 return set;
5444 error:
5445 isl_map_free(map);
5446 return NULL;
5449 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5451 int i;
5453 map = isl_map_cow(map);
5454 if (!map)
5455 return NULL;
5457 map->dim = isl_space_domain_map(map->dim);
5458 if (!map->dim)
5459 goto error;
5460 for (i = 0; i < map->n; ++i) {
5461 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5462 if (!map->p[i])
5463 goto error;
5465 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5466 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5467 return map;
5468 error:
5469 isl_map_free(map);
5470 return NULL;
5473 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5475 int i;
5476 isl_space *range_dim;
5478 map = isl_map_cow(map);
5479 if (!map)
5480 return NULL;
5482 range_dim = isl_space_range(isl_map_get_space(map));
5483 range_dim = isl_space_from_range(range_dim);
5484 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5485 map->dim = isl_space_join(map->dim, range_dim);
5486 if (!map->dim)
5487 goto error;
5488 for (i = 0; i < map->n; ++i) {
5489 map->p[i] = isl_basic_map_range_map(map->p[i]);
5490 if (!map->p[i])
5491 goto error;
5493 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5494 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5495 return map;
5496 error:
5497 isl_map_free(map);
5498 return NULL;
5501 /* Given a wrapped map of the form A[B -> C],
5502 * return the map A[B -> C] -> B.
5504 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5506 isl_id *id;
5507 isl_map *map;
5509 if (!set)
5510 return NULL;
5511 if (!isl_set_has_tuple_id(set))
5512 return isl_map_domain_map(isl_set_unwrap(set));
5514 id = isl_set_get_tuple_id(set);
5515 map = isl_map_domain_map(isl_set_unwrap(set));
5516 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5518 return map;
5521 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5522 __isl_take isl_space *dim)
5524 int i;
5525 struct isl_map *map = NULL;
5527 set = isl_set_cow(set);
5528 if (!set || !dim)
5529 goto error;
5530 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5531 goto error);
5532 map = set_to_map(set);
5533 for (i = 0; i < set->n; ++i) {
5534 map->p[i] = basic_map_from_basic_set(
5535 set->p[i], isl_space_copy(dim));
5536 if (!map->p[i])
5537 goto error;
5539 isl_space_free(map->dim);
5540 map->dim = dim;
5541 return map;
5542 error:
5543 isl_space_free(dim);
5544 isl_set_free(set);
5545 return NULL;
5548 __isl_give isl_basic_map *isl_basic_map_from_domain(
5549 __isl_take isl_basic_set *bset)
5551 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5554 __isl_give isl_basic_map *isl_basic_map_from_range(
5555 __isl_take isl_basic_set *bset)
5557 isl_space *space;
5558 space = isl_basic_set_get_space(bset);
5559 space = isl_space_from_range(space);
5560 bset = isl_basic_set_reset_space(bset, space);
5561 return bset_to_bmap(bset);
5564 /* Create a relation with the given set as range.
5565 * The domain of the created relation is a zero-dimensional
5566 * flat anonymous space.
5568 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5570 isl_space *space;
5571 space = isl_set_get_space(set);
5572 space = isl_space_from_range(space);
5573 set = isl_set_reset_space(set, space);
5574 return set_to_map(set);
5577 /* Create a relation with the given set as domain.
5578 * The range of the created relation is a zero-dimensional
5579 * flat anonymous space.
5581 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5583 return isl_map_reverse(isl_map_from_range(set));
5586 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5587 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5589 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5592 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5593 __isl_take isl_set *range)
5595 return isl_map_apply_range(isl_map_reverse(domain), range);
5598 /* Return a newly allocated isl_map with given space and flags and
5599 * room for "n" basic maps.
5600 * Make sure that all cached information is cleared.
5602 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5603 unsigned flags)
5605 struct isl_map *map;
5607 if (!space)
5608 return NULL;
5609 if (n < 0)
5610 isl_die(space->ctx, isl_error_internal,
5611 "negative number of basic maps", goto error);
5612 map = isl_calloc(space->ctx, struct isl_map,
5613 sizeof(struct isl_map) +
5614 (n - 1) * sizeof(struct isl_basic_map *));
5615 if (!map)
5616 goto error;
5618 map->ctx = space->ctx;
5619 isl_ctx_ref(map->ctx);
5620 map->ref = 1;
5621 map->size = n;
5622 map->n = 0;
5623 map->dim = space;
5624 map->flags = flags;
5625 return map;
5626 error:
5627 isl_space_free(space);
5628 return NULL;
5631 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5632 unsigned nparam, unsigned in, unsigned out, int n,
5633 unsigned flags)
5635 struct isl_map *map;
5636 isl_space *dims;
5638 dims = isl_space_alloc(ctx, nparam, in, out);
5639 if (!dims)
5640 return NULL;
5642 map = isl_map_alloc_space(dims, n, flags);
5643 return map;
5646 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5648 struct isl_basic_map *bmap;
5649 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5650 bmap = isl_basic_map_set_to_empty(bmap);
5651 return bmap;
5654 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5656 struct isl_basic_set *bset;
5657 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5658 bset = isl_basic_set_set_to_empty(bset);
5659 return bset;
5662 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5664 struct isl_basic_map *bmap;
5665 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5666 bmap = isl_basic_map_finalize(bmap);
5667 return bmap;
5670 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5672 struct isl_basic_set *bset;
5673 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5674 bset = isl_basic_set_finalize(bset);
5675 return bset;
5678 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5680 int i;
5681 unsigned total = isl_space_dim(dim, isl_dim_all);
5682 isl_basic_map *bmap;
5684 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5685 for (i = 0; i < total; ++i) {
5686 int k = isl_basic_map_alloc_inequality(bmap);
5687 if (k < 0)
5688 goto error;
5689 isl_seq_clr(bmap->ineq[k], 1 + total);
5690 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5692 return bmap;
5693 error:
5694 isl_basic_map_free(bmap);
5695 return NULL;
5698 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5700 return isl_basic_map_nat_universe(dim);
5703 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5705 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5708 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5710 return isl_map_nat_universe(dim);
5713 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5715 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5718 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5720 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5723 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5725 struct isl_map *map;
5726 if (!dim)
5727 return NULL;
5728 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5729 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5730 return map;
5733 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5735 struct isl_set *set;
5736 if (!dim)
5737 return NULL;
5738 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5739 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5740 return set;
5743 struct isl_map *isl_map_dup(struct isl_map *map)
5745 int i;
5746 struct isl_map *dup;
5748 if (!map)
5749 return NULL;
5750 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5751 for (i = 0; i < map->n; ++i)
5752 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5753 return dup;
5756 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5757 __isl_take isl_basic_map *bmap)
5759 if (!bmap || !map)
5760 goto error;
5761 if (isl_basic_map_plain_is_empty(bmap)) {
5762 isl_basic_map_free(bmap);
5763 return map;
5765 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5766 isl_assert(map->ctx, map->n < map->size, goto error);
5767 map->p[map->n] = bmap;
5768 map->n++;
5769 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5770 return map;
5771 error:
5772 if (map)
5773 isl_map_free(map);
5774 if (bmap)
5775 isl_basic_map_free(bmap);
5776 return NULL;
5779 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5781 int i;
5783 if (!map)
5784 return NULL;
5786 if (--map->ref > 0)
5787 return NULL;
5789 clear_caches(map);
5790 isl_ctx_deref(map->ctx);
5791 for (i = 0; i < map->n; ++i)
5792 isl_basic_map_free(map->p[i]);
5793 isl_space_free(map->dim);
5794 free(map);
5796 return NULL;
5799 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5800 struct isl_basic_map *bmap, unsigned pos, int value)
5802 int j;
5804 bmap = isl_basic_map_cow(bmap);
5805 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5806 j = isl_basic_map_alloc_equality(bmap);
5807 if (j < 0)
5808 goto error;
5809 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5810 isl_int_set_si(bmap->eq[j][pos], -1);
5811 isl_int_set_si(bmap->eq[j][0], value);
5812 bmap = isl_basic_map_simplify(bmap);
5813 return isl_basic_map_finalize(bmap);
5814 error:
5815 isl_basic_map_free(bmap);
5816 return NULL;
5819 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5820 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5822 int j;
5824 bmap = isl_basic_map_cow(bmap);
5825 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5826 j = isl_basic_map_alloc_equality(bmap);
5827 if (j < 0)
5828 goto error;
5829 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5830 isl_int_set_si(bmap->eq[j][pos], -1);
5831 isl_int_set(bmap->eq[j][0], value);
5832 bmap = isl_basic_map_simplify(bmap);
5833 return isl_basic_map_finalize(bmap);
5834 error:
5835 isl_basic_map_free(bmap);
5836 return NULL;
5839 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5840 enum isl_dim_type type, unsigned pos, int value)
5842 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5843 return isl_basic_map_free(bmap);
5844 return isl_basic_map_fix_pos_si(bmap,
5845 isl_basic_map_offset(bmap, type) + pos, value);
5848 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5849 enum isl_dim_type type, unsigned pos, isl_int value)
5851 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5852 return isl_basic_map_free(bmap);
5853 return isl_basic_map_fix_pos(bmap,
5854 isl_basic_map_offset(bmap, type) + pos, value);
5857 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5858 * to be equal to "v".
5860 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5861 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5863 if (!bmap || !v)
5864 goto error;
5865 if (!isl_val_is_int(v))
5866 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5867 "expecting integer value", goto error);
5868 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5869 goto error;
5870 pos += isl_basic_map_offset(bmap, type);
5871 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5872 isl_val_free(v);
5873 return bmap;
5874 error:
5875 isl_basic_map_free(bmap);
5876 isl_val_free(v);
5877 return NULL;
5880 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5881 * to be equal to "v".
5883 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5884 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5886 return isl_basic_map_fix_val(bset, type, pos, v);
5889 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5890 enum isl_dim_type type, unsigned pos, int value)
5892 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5893 type, pos, value));
5896 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5897 enum isl_dim_type type, unsigned pos, isl_int value)
5899 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5900 type, pos, value));
5903 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5904 unsigned input, int value)
5906 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5909 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5910 unsigned dim, int value)
5912 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5913 isl_dim_set, dim, value));
5916 static int remove_if_empty(__isl_keep isl_map *map, int i)
5918 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5920 if (empty < 0)
5921 return -1;
5922 if (!empty)
5923 return 0;
5925 isl_basic_map_free(map->p[i]);
5926 if (i != map->n - 1) {
5927 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5928 map->p[i] = map->p[map->n - 1];
5930 map->n--;
5932 return 0;
5935 /* Perform "fn" on each basic map of "map", where we may not be holding
5936 * the only reference to "map".
5937 * In particular, "fn" should be a semantics preserving operation
5938 * that we want to apply to all copies of "map". We therefore need
5939 * to be careful not to modify "map" in a way that breaks "map"
5940 * in case anything goes wrong.
5942 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5943 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5945 struct isl_basic_map *bmap;
5946 int i;
5948 if (!map)
5949 return NULL;
5951 for (i = map->n - 1; i >= 0; --i) {
5952 bmap = isl_basic_map_copy(map->p[i]);
5953 bmap = fn(bmap);
5954 if (!bmap)
5955 goto error;
5956 isl_basic_map_free(map->p[i]);
5957 map->p[i] = bmap;
5958 if (remove_if_empty(map, i) < 0)
5959 goto error;
5962 return map;
5963 error:
5964 isl_map_free(map);
5965 return NULL;
5968 struct isl_map *isl_map_fix_si(struct isl_map *map,
5969 enum isl_dim_type type, unsigned pos, int value)
5971 int i;
5973 map = isl_map_cow(map);
5974 if (!map)
5975 return NULL;
5977 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5978 for (i = map->n - 1; i >= 0; --i) {
5979 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5980 if (remove_if_empty(map, i) < 0)
5981 goto error;
5983 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5984 return map;
5985 error:
5986 isl_map_free(map);
5987 return NULL;
5990 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5991 enum isl_dim_type type, unsigned pos, int value)
5993 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5996 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5997 enum isl_dim_type type, unsigned pos, isl_int value)
5999 int i;
6001 map = isl_map_cow(map);
6002 if (!map)
6003 return NULL;
6005 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6006 for (i = 0; i < map->n; ++i) {
6007 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6008 if (!map->p[i])
6009 goto error;
6011 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6012 return map;
6013 error:
6014 isl_map_free(map);
6015 return NULL;
6018 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6019 enum isl_dim_type type, unsigned pos, isl_int value)
6021 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6024 /* Fix the value of the variable at position "pos" of type "type" of "map"
6025 * to be equal to "v".
6027 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6028 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6030 int i;
6032 map = isl_map_cow(map);
6033 if (!map || !v)
6034 goto error;
6036 if (!isl_val_is_int(v))
6037 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6038 "expecting integer value", goto error);
6039 if (pos >= isl_map_dim(map, type))
6040 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6041 "index out of bounds", goto error);
6042 for (i = map->n - 1; i >= 0; --i) {
6043 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6044 isl_val_copy(v));
6045 if (remove_if_empty(map, i) < 0)
6046 goto error;
6048 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6049 isl_val_free(v);
6050 return map;
6051 error:
6052 isl_map_free(map);
6053 isl_val_free(v);
6054 return NULL;
6057 /* Fix the value of the variable at position "pos" of type "type" of "set"
6058 * to be equal to "v".
6060 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6061 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6063 return isl_map_fix_val(set, type, pos, v);
6066 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6067 unsigned input, int value)
6069 return isl_map_fix_si(map, isl_dim_in, input, value);
6072 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6074 return set_from_map(isl_map_fix_si(set_to_map(set),
6075 isl_dim_set, dim, value));
6078 static __isl_give isl_basic_map *basic_map_bound_si(
6079 __isl_take isl_basic_map *bmap,
6080 enum isl_dim_type type, unsigned pos, int value, int upper)
6082 int j;
6084 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6085 return isl_basic_map_free(bmap);
6086 pos += isl_basic_map_offset(bmap, type);
6087 bmap = isl_basic_map_cow(bmap);
6088 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6089 j = isl_basic_map_alloc_inequality(bmap);
6090 if (j < 0)
6091 goto error;
6092 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6093 if (upper) {
6094 isl_int_set_si(bmap->ineq[j][pos], -1);
6095 isl_int_set_si(bmap->ineq[j][0], value);
6096 } else {
6097 isl_int_set_si(bmap->ineq[j][pos], 1);
6098 isl_int_set_si(bmap->ineq[j][0], -value);
6100 bmap = isl_basic_map_simplify(bmap);
6101 return isl_basic_map_finalize(bmap);
6102 error:
6103 isl_basic_map_free(bmap);
6104 return NULL;
6107 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6108 __isl_take isl_basic_map *bmap,
6109 enum isl_dim_type type, unsigned pos, int value)
6111 return basic_map_bound_si(bmap, type, pos, value, 0);
6114 /* Constrain the values of the given dimension to be no greater than "value".
6116 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6117 __isl_take isl_basic_map *bmap,
6118 enum isl_dim_type type, unsigned pos, int value)
6120 return basic_map_bound_si(bmap, type, pos, value, 1);
6123 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
6124 unsigned dim, isl_int value)
6126 int j;
6128 bset = isl_basic_set_cow(bset);
6129 bset = isl_basic_set_extend_constraints(bset, 0, 1);
6130 j = isl_basic_set_alloc_inequality(bset);
6131 if (j < 0)
6132 goto error;
6133 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
6134 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
6135 isl_int_neg(bset->ineq[j][0], value);
6136 bset = isl_basic_set_simplify(bset);
6137 return isl_basic_set_finalize(bset);
6138 error:
6139 isl_basic_set_free(bset);
6140 return NULL;
6143 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6144 enum isl_dim_type type, unsigned pos, int value, int upper)
6146 int i;
6148 map = isl_map_cow(map);
6149 if (!map)
6150 return NULL;
6152 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6153 for (i = 0; i < map->n; ++i) {
6154 map->p[i] = basic_map_bound_si(map->p[i],
6155 type, pos, value, upper);
6156 if (!map->p[i])
6157 goto error;
6159 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6160 return map;
6161 error:
6162 isl_map_free(map);
6163 return NULL;
6166 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6167 enum isl_dim_type type, unsigned pos, int value)
6169 return map_bound_si(map, type, pos, value, 0);
6172 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6173 enum isl_dim_type type, unsigned pos, int value)
6175 return map_bound_si(map, type, pos, value, 1);
6178 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6179 enum isl_dim_type type, unsigned pos, int value)
6181 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6182 type, pos, value));
6185 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6186 enum isl_dim_type type, unsigned pos, int value)
6188 return isl_map_upper_bound_si(set, type, pos, value);
6191 /* Bound the given variable of "bmap" from below (or above is "upper"
6192 * is set) to "value".
6194 static __isl_give isl_basic_map *basic_map_bound(
6195 __isl_take isl_basic_map *bmap,
6196 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6198 int j;
6200 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6201 return isl_basic_map_free(bmap);
6202 pos += isl_basic_map_offset(bmap, type);
6203 bmap = isl_basic_map_cow(bmap);
6204 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6205 j = isl_basic_map_alloc_inequality(bmap);
6206 if (j < 0)
6207 goto error;
6208 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6209 if (upper) {
6210 isl_int_set_si(bmap->ineq[j][pos], -1);
6211 isl_int_set(bmap->ineq[j][0], value);
6212 } else {
6213 isl_int_set_si(bmap->ineq[j][pos], 1);
6214 isl_int_neg(bmap->ineq[j][0], value);
6216 bmap = isl_basic_map_simplify(bmap);
6217 return isl_basic_map_finalize(bmap);
6218 error:
6219 isl_basic_map_free(bmap);
6220 return NULL;
6223 /* Bound the given variable of "map" from below (or above is "upper"
6224 * is set) to "value".
6226 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6227 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6229 int i;
6231 map = isl_map_cow(map);
6232 if (!map)
6233 return NULL;
6235 if (pos >= isl_map_dim(map, type))
6236 isl_die(map->ctx, isl_error_invalid,
6237 "index out of bounds", goto error);
6238 for (i = map->n - 1; i >= 0; --i) {
6239 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6240 if (remove_if_empty(map, i) < 0)
6241 goto error;
6243 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6244 return map;
6245 error:
6246 isl_map_free(map);
6247 return NULL;
6250 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6251 enum isl_dim_type type, unsigned pos, isl_int value)
6253 return map_bound(map, type, pos, value, 0);
6256 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6257 enum isl_dim_type type, unsigned pos, isl_int value)
6259 return map_bound(map, type, pos, value, 1);
6262 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6263 enum isl_dim_type type, unsigned pos, isl_int value)
6265 return isl_map_lower_bound(set, type, pos, value);
6268 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6269 enum isl_dim_type type, unsigned pos, isl_int value)
6271 return isl_map_upper_bound(set, type, pos, value);
6274 /* Force the values of the variable at position "pos" of type "type" of "set"
6275 * to be no smaller than "value".
6277 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6278 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6280 if (!value)
6281 goto error;
6282 if (!isl_val_is_int(value))
6283 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6284 "expecting integer value", goto error);
6285 set = isl_set_lower_bound(set, type, pos, value->n);
6286 isl_val_free(value);
6287 return set;
6288 error:
6289 isl_val_free(value);
6290 isl_set_free(set);
6291 return NULL;
6294 /* Force the values of the variable at position "pos" of type "type" of "set"
6295 * to be no greater than "value".
6297 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6298 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6300 if (!value)
6301 goto error;
6302 if (!isl_val_is_int(value))
6303 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6304 "expecting integer value", goto error);
6305 set = isl_set_upper_bound(set, type, pos, value->n);
6306 isl_val_free(value);
6307 return set;
6308 error:
6309 isl_val_free(value);
6310 isl_set_free(set);
6311 return NULL;
6314 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6315 isl_int value)
6317 int i;
6319 set = isl_set_cow(set);
6320 if (!set)
6321 return NULL;
6323 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6324 for (i = 0; i < set->n; ++i) {
6325 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6326 if (!set->p[i])
6327 goto error;
6329 return set;
6330 error:
6331 isl_set_free(set);
6332 return NULL;
6335 struct isl_map *isl_map_reverse(struct isl_map *map)
6337 int i;
6339 map = isl_map_cow(map);
6340 if (!map)
6341 return NULL;
6343 map->dim = isl_space_reverse(map->dim);
6344 if (!map->dim)
6345 goto error;
6346 for (i = 0; i < map->n; ++i) {
6347 map->p[i] = isl_basic_map_reverse(map->p[i]);
6348 if (!map->p[i])
6349 goto error;
6351 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6352 return map;
6353 error:
6354 isl_map_free(map);
6355 return NULL;
6358 #undef TYPE
6359 #define TYPE isl_pw_multi_aff
6360 #undef SUFFIX
6361 #define SUFFIX _pw_multi_aff
6362 #undef EMPTY
6363 #define EMPTY isl_pw_multi_aff_empty
6364 #undef ADD
6365 #define ADD isl_pw_multi_aff_union_add
6366 #include "isl_map_lexopt_templ.c"
6368 /* Given a map "map", compute the lexicographically minimal
6369 * (or maximal) image element for each domain element in dom,
6370 * in the form of an isl_pw_multi_aff.
6371 * If "empty" is not NULL, then set *empty to those elements in dom that
6372 * do not have an image element.
6373 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6374 * should be computed over the domain of "map". "empty" is also NULL
6375 * in this case.
6377 * We first compute the lexicographically minimal or maximal element
6378 * in the first basic map. This results in a partial solution "res"
6379 * and a subset "todo" of dom that still need to be handled.
6380 * We then consider each of the remaining maps in "map" and successively
6381 * update both "res" and "todo".
6382 * If "empty" is NULL, then the todo sets are not needed and therefore
6383 * also not computed.
6385 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6386 __isl_take isl_map *map, __isl_take isl_set *dom,
6387 __isl_give isl_set **empty, unsigned flags)
6389 int i;
6390 int full;
6391 isl_pw_multi_aff *res;
6392 isl_set *todo;
6394 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6395 if (!map || (!full && !dom))
6396 goto error;
6398 if (isl_map_plain_is_empty(map)) {
6399 if (empty)
6400 *empty = dom;
6401 else
6402 isl_set_free(dom);
6403 return isl_pw_multi_aff_from_map(map);
6406 res = basic_map_partial_lexopt_pw_multi_aff(
6407 isl_basic_map_copy(map->p[0]),
6408 isl_set_copy(dom), empty, flags);
6410 if (empty)
6411 todo = *empty;
6412 for (i = 1; i < map->n; ++i) {
6413 isl_pw_multi_aff *res_i;
6415 res_i = basic_map_partial_lexopt_pw_multi_aff(
6416 isl_basic_map_copy(map->p[i]),
6417 isl_set_copy(dom), empty, flags);
6419 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6420 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6421 else
6422 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6424 if (empty)
6425 todo = isl_set_intersect(todo, *empty);
6428 isl_set_free(dom);
6429 isl_map_free(map);
6431 if (empty)
6432 *empty = todo;
6434 return res;
6435 error:
6436 if (empty)
6437 *empty = NULL;
6438 isl_set_free(dom);
6439 isl_map_free(map);
6440 return NULL;
6443 #undef TYPE
6444 #define TYPE isl_map
6445 #undef SUFFIX
6446 #define SUFFIX
6447 #undef EMPTY
6448 #define EMPTY isl_map_empty
6449 #undef ADD
6450 #define ADD isl_map_union_disjoint
6451 #include "isl_map_lexopt_templ.c"
6453 /* Given a map "map", compute the lexicographically minimal
6454 * (or maximal) image element for each domain element in "dom",
6455 * in the form of an isl_map.
6456 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6457 * do not have an image element.
6458 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6459 * should be computed over the domain of "map". "empty" is also NULL
6460 * in this case.
6462 * If the input consists of more than one disjunct, then first
6463 * compute the desired result in the form of an isl_pw_multi_aff and
6464 * then convert that into an isl_map.
6466 * This function used to have an explicit implementation in terms
6467 * of isl_maps, but it would continually intersect the domains of
6468 * partial results with the complement of the domain of the next
6469 * partial solution, potentially leading to an explosion in the number
6470 * of disjuncts if there are several disjuncts in the input.
6471 * An even earlier implementation of this function would look for
6472 * better results in the domain of the partial result and for extra
6473 * results in the complement of this domain, which would lead to
6474 * even more splintering.
6476 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6477 __isl_take isl_map *map, __isl_take isl_set *dom,
6478 __isl_give isl_set **empty, unsigned flags)
6480 int full;
6481 struct isl_map *res;
6482 isl_pw_multi_aff *pma;
6484 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6485 if (!map || (!full && !dom))
6486 goto error;
6488 if (isl_map_plain_is_empty(map)) {
6489 if (empty)
6490 *empty = dom;
6491 else
6492 isl_set_free(dom);
6493 return map;
6496 if (map->n == 1) {
6497 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6498 dom, empty, flags);
6499 isl_map_free(map);
6500 return res;
6503 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6504 flags);
6505 return isl_map_from_pw_multi_aff(pma);
6506 error:
6507 if (empty)
6508 *empty = NULL;
6509 isl_set_free(dom);
6510 isl_map_free(map);
6511 return NULL;
6514 __isl_give isl_map *isl_map_partial_lexmax(
6515 __isl_take isl_map *map, __isl_take isl_set *dom,
6516 __isl_give isl_set **empty)
6518 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6521 __isl_give isl_map *isl_map_partial_lexmin(
6522 __isl_take isl_map *map, __isl_take isl_set *dom,
6523 __isl_give isl_set **empty)
6525 return isl_map_partial_lexopt(map, dom, empty, 0);
6528 __isl_give isl_set *isl_set_partial_lexmin(
6529 __isl_take isl_set *set, __isl_take isl_set *dom,
6530 __isl_give isl_set **empty)
6532 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6533 dom, empty));
6536 __isl_give isl_set *isl_set_partial_lexmax(
6537 __isl_take isl_set *set, __isl_take isl_set *dom,
6538 __isl_give isl_set **empty)
6540 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6541 dom, empty));
6544 /* Compute the lexicographic minimum (or maximum if "flags" includes
6545 * ISL_OPT_MAX) of "bset" over its parametric domain.
6547 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6548 unsigned flags)
6550 return isl_basic_map_lexopt(bset, flags);
6553 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6555 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6558 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6560 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6563 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6565 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6568 /* Compute the lexicographic minimum of "bset" over its parametric domain
6569 * for the purpose of quantifier elimination.
6570 * That is, find an explicit representation for all the existentially
6571 * quantified variables in "bset" by computing their lexicographic
6572 * minimum.
6574 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6575 __isl_take isl_basic_set *bset)
6577 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6580 /* Extract the first and only affine expression from list
6581 * and then add it to *pwaff with the given dom.
6582 * This domain is known to be disjoint from other domains
6583 * because of the way isl_basic_map_foreach_lexmax works.
6585 static int update_dim_opt(__isl_take isl_basic_set *dom,
6586 __isl_take isl_aff_list *list, void *user)
6588 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6589 isl_aff *aff;
6590 isl_pw_aff **pwaff = user;
6591 isl_pw_aff *pwaff_i;
6593 if (!list)
6594 goto error;
6595 if (isl_aff_list_n_aff(list) != 1)
6596 isl_die(ctx, isl_error_internal,
6597 "expecting single element list", goto error);
6599 aff = isl_aff_list_get_aff(list, 0);
6600 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6602 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6604 isl_aff_list_free(list);
6606 return 0;
6607 error:
6608 isl_basic_set_free(dom);
6609 isl_aff_list_free(list);
6610 return -1;
6613 /* Given a basic map with one output dimension, compute the minimum or
6614 * maximum of that dimension as an isl_pw_aff.
6616 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6617 * call update_dim_opt on each leaf of the result.
6619 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6620 int max)
6622 isl_space *dim = isl_basic_map_get_space(bmap);
6623 isl_pw_aff *pwaff;
6624 int r;
6626 dim = isl_space_from_domain(isl_space_domain(dim));
6627 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6628 pwaff = isl_pw_aff_empty(dim);
6630 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6631 if (r < 0)
6632 return isl_pw_aff_free(pwaff);
6634 return pwaff;
6637 /* Compute the minimum or maximum of the given output dimension
6638 * as a function of the parameters and the input dimensions,
6639 * but independently of the other output dimensions.
6641 * We first project out the other output dimension and then compute
6642 * the "lexicographic" maximum in each basic map, combining the results
6643 * using isl_pw_aff_union_max.
6645 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6646 int max)
6648 int i;
6649 isl_pw_aff *pwaff;
6650 unsigned n_out;
6652 n_out = isl_map_dim(map, isl_dim_out);
6653 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6654 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6655 if (!map)
6656 return NULL;
6658 if (map->n == 0) {
6659 isl_space *dim = isl_map_get_space(map);
6660 isl_map_free(map);
6661 return isl_pw_aff_empty(dim);
6664 pwaff = basic_map_dim_opt(map->p[0], max);
6665 for (i = 1; i < map->n; ++i) {
6666 isl_pw_aff *pwaff_i;
6668 pwaff_i = basic_map_dim_opt(map->p[i], max);
6669 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6672 isl_map_free(map);
6674 return pwaff;
6677 /* Compute the minimum of the given output dimension as a function of the
6678 * parameters and input dimensions, but independently of
6679 * the other output dimensions.
6681 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6683 return map_dim_opt(map, pos, 0);
6686 /* Compute the maximum of the given output dimension as a function of the
6687 * parameters and input dimensions, but independently of
6688 * the other output dimensions.
6690 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6692 return map_dim_opt(map, pos, 1);
6695 /* Compute the minimum or maximum of the given set dimension
6696 * as a function of the parameters,
6697 * but independently of the other set dimensions.
6699 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6700 int max)
6702 return map_dim_opt(set, pos, max);
6705 /* Compute the maximum of the given set dimension as a function of the
6706 * parameters, but independently of the other set dimensions.
6708 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6710 return set_dim_opt(set, pos, 1);
6713 /* Compute the minimum of the given set dimension as a function of the
6714 * parameters, but independently of the other set dimensions.
6716 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6718 return set_dim_opt(set, pos, 0);
6721 /* Apply a preimage specified by "mat" on the parameters of "bset".
6722 * bset is assumed to have only parameters and divs.
6724 static struct isl_basic_set *basic_set_parameter_preimage(
6725 struct isl_basic_set *bset, struct isl_mat *mat)
6727 unsigned nparam;
6729 if (!bset || !mat)
6730 goto error;
6732 bset->dim = isl_space_cow(bset->dim);
6733 if (!bset->dim)
6734 goto error;
6736 nparam = isl_basic_set_dim(bset, isl_dim_param);
6738 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6740 bset->dim->nparam = 0;
6741 bset->dim->n_out = nparam;
6742 bset = isl_basic_set_preimage(bset, mat);
6743 if (bset) {
6744 bset->dim->nparam = bset->dim->n_out;
6745 bset->dim->n_out = 0;
6747 return bset;
6748 error:
6749 isl_mat_free(mat);
6750 isl_basic_set_free(bset);
6751 return NULL;
6754 /* Apply a preimage specified by "mat" on the parameters of "set".
6755 * set is assumed to have only parameters and divs.
6757 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6758 __isl_take isl_mat *mat)
6760 isl_space *space;
6761 unsigned nparam;
6763 if (!set || !mat)
6764 goto error;
6766 nparam = isl_set_dim(set, isl_dim_param);
6768 if (mat->n_row != 1 + nparam)
6769 isl_die(isl_set_get_ctx(set), isl_error_internal,
6770 "unexpected number of rows", goto error);
6772 space = isl_set_get_space(set);
6773 space = isl_space_move_dims(space, isl_dim_set, 0,
6774 isl_dim_param, 0, nparam);
6775 set = isl_set_reset_space(set, space);
6776 set = isl_set_preimage(set, mat);
6777 nparam = isl_set_dim(set, isl_dim_out);
6778 space = isl_set_get_space(set);
6779 space = isl_space_move_dims(space, isl_dim_param, 0,
6780 isl_dim_out, 0, nparam);
6781 set = isl_set_reset_space(set, space);
6782 return set;
6783 error:
6784 isl_mat_free(mat);
6785 isl_set_free(set);
6786 return NULL;
6789 /* Intersect the basic set "bset" with the affine space specified by the
6790 * equalities in "eq".
6792 static struct isl_basic_set *basic_set_append_equalities(
6793 struct isl_basic_set *bset, struct isl_mat *eq)
6795 int i, k;
6796 unsigned len;
6798 if (!bset || !eq)
6799 goto error;
6801 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6802 eq->n_row, 0);
6803 if (!bset)
6804 goto error;
6806 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6807 for (i = 0; i < eq->n_row; ++i) {
6808 k = isl_basic_set_alloc_equality(bset);
6809 if (k < 0)
6810 goto error;
6811 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6812 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6814 isl_mat_free(eq);
6816 bset = isl_basic_set_gauss(bset, NULL);
6817 bset = isl_basic_set_finalize(bset);
6819 return bset;
6820 error:
6821 isl_mat_free(eq);
6822 isl_basic_set_free(bset);
6823 return NULL;
6826 /* Intersect the set "set" with the affine space specified by the
6827 * equalities in "eq".
6829 static struct isl_set *set_append_equalities(struct isl_set *set,
6830 struct isl_mat *eq)
6832 int i;
6834 if (!set || !eq)
6835 goto error;
6837 for (i = 0; i < set->n; ++i) {
6838 set->p[i] = basic_set_append_equalities(set->p[i],
6839 isl_mat_copy(eq));
6840 if (!set->p[i])
6841 goto error;
6843 isl_mat_free(eq);
6844 return set;
6845 error:
6846 isl_mat_free(eq);
6847 isl_set_free(set);
6848 return NULL;
6851 /* Given a basic set "bset" that only involves parameters and existentially
6852 * quantified variables, return the index of the first equality
6853 * that only involves parameters. If there is no such equality then
6854 * return bset->n_eq.
6856 * This function assumes that isl_basic_set_gauss has been called on "bset".
6858 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6860 int i, j;
6861 unsigned nparam, n_div;
6863 if (!bset)
6864 return -1;
6866 nparam = isl_basic_set_dim(bset, isl_dim_param);
6867 n_div = isl_basic_set_dim(bset, isl_dim_div);
6869 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6870 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6871 ++i;
6874 return i;
6877 /* Compute an explicit representation for the existentially quantified
6878 * variables in "bset" by computing the "minimal value" of the set
6879 * variables. Since there are no set variables, the computation of
6880 * the minimal value essentially computes an explicit representation
6881 * of the non-empty part(s) of "bset".
6883 * The input only involves parameters and existentially quantified variables.
6884 * All equalities among parameters have been removed.
6886 * Since the existentially quantified variables in the result are in general
6887 * going to be different from those in the input, we first replace
6888 * them by the minimal number of variables based on their equalities.
6889 * This should simplify the parametric integer programming.
6891 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6893 isl_morph *morph1, *morph2;
6894 isl_set *set;
6895 unsigned n;
6897 if (!bset)
6898 return NULL;
6899 if (bset->n_eq == 0)
6900 return isl_basic_set_lexmin_compute_divs(bset);
6902 morph1 = isl_basic_set_parameter_compression(bset);
6903 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6904 bset = isl_basic_set_lift(bset);
6905 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6906 bset = isl_morph_basic_set(morph2, bset);
6907 n = isl_basic_set_dim(bset, isl_dim_set);
6908 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6910 set = isl_basic_set_lexmin_compute_divs(bset);
6912 set = isl_morph_set(isl_morph_inverse(morph1), set);
6914 return set;
6917 /* Project the given basic set onto its parameter domain, possibly introducing
6918 * new, explicit, existential variables in the constraints.
6919 * The input has parameters and (possibly implicit) existential variables.
6920 * The output has the same parameters, but only
6921 * explicit existentially quantified variables.
6923 * The actual projection is performed by pip, but pip doesn't seem
6924 * to like equalities very much, so we first remove the equalities
6925 * among the parameters by performing a variable compression on
6926 * the parameters. Afterward, an inverse transformation is performed
6927 * and the equalities among the parameters are inserted back in.
6929 * The variable compression on the parameters may uncover additional
6930 * equalities that were only implicit before. We therefore check
6931 * if there are any new parameter equalities in the result and
6932 * if so recurse. The removal of parameter equalities is required
6933 * for the parameter compression performed by base_compute_divs.
6935 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6937 int i;
6938 struct isl_mat *eq;
6939 struct isl_mat *T, *T2;
6940 struct isl_set *set;
6941 unsigned nparam;
6943 bset = isl_basic_set_cow(bset);
6944 if (!bset)
6945 return NULL;
6947 if (bset->n_eq == 0)
6948 return base_compute_divs(bset);
6950 bset = isl_basic_set_gauss(bset, NULL);
6951 if (!bset)
6952 return NULL;
6953 if (isl_basic_set_plain_is_empty(bset))
6954 return isl_set_from_basic_set(bset);
6956 i = first_parameter_equality(bset);
6957 if (i == bset->n_eq)
6958 return base_compute_divs(bset);
6960 nparam = isl_basic_set_dim(bset, isl_dim_param);
6961 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6962 0, 1 + nparam);
6963 eq = isl_mat_cow(eq);
6964 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6965 if (T && T->n_col == 0) {
6966 isl_mat_free(T);
6967 isl_mat_free(T2);
6968 isl_mat_free(eq);
6969 bset = isl_basic_set_set_to_empty(bset);
6970 return isl_set_from_basic_set(bset);
6972 bset = basic_set_parameter_preimage(bset, T);
6974 i = first_parameter_equality(bset);
6975 if (!bset)
6976 set = NULL;
6977 else if (i == bset->n_eq)
6978 set = base_compute_divs(bset);
6979 else
6980 set = parameter_compute_divs(bset);
6981 set = set_parameter_preimage(set, T2);
6982 set = set_append_equalities(set, eq);
6983 return set;
6986 /* Insert the divs from "ls" before those of "bmap".
6988 * The number of columns is not changed, which means that the last
6989 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6990 * The caller is responsible for removing the same number of dimensions
6991 * from the space of "bmap".
6993 static __isl_give isl_basic_map *insert_divs_from_local_space(
6994 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6996 int i;
6997 int n_div;
6998 int old_n_div;
7000 n_div = isl_local_space_dim(ls, isl_dim_div);
7001 if (n_div == 0)
7002 return bmap;
7004 old_n_div = bmap->n_div;
7005 bmap = insert_div_rows(bmap, n_div);
7006 if (!bmap)
7007 return NULL;
7009 for (i = 0; i < n_div; ++i) {
7010 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7011 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7014 return bmap;
7017 /* Replace the space of "bmap" by the space and divs of "ls".
7019 * If "ls" has any divs, then we simplify the result since we may
7020 * have discovered some additional equalities that could simplify
7021 * the div expressions.
7023 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7024 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7026 int n_div;
7028 bmap = isl_basic_map_cow(bmap);
7029 if (!bmap || !ls)
7030 goto error;
7032 n_div = isl_local_space_dim(ls, isl_dim_div);
7033 bmap = insert_divs_from_local_space(bmap, ls);
7034 if (!bmap)
7035 goto error;
7037 isl_space_free(bmap->dim);
7038 bmap->dim = isl_local_space_get_space(ls);
7039 if (!bmap->dim)
7040 goto error;
7042 isl_local_space_free(ls);
7043 if (n_div > 0)
7044 bmap = isl_basic_map_simplify(bmap);
7045 bmap = isl_basic_map_finalize(bmap);
7046 return bmap;
7047 error:
7048 isl_basic_map_free(bmap);
7049 isl_local_space_free(ls);
7050 return NULL;
7053 /* Replace the space of "map" by the space and divs of "ls".
7055 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7056 __isl_take isl_local_space *ls)
7058 int i;
7060 map = isl_map_cow(map);
7061 if (!map || !ls)
7062 goto error;
7064 for (i = 0; i < map->n; ++i) {
7065 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7066 isl_local_space_copy(ls));
7067 if (!map->p[i])
7068 goto error;
7070 isl_space_free(map->dim);
7071 map->dim = isl_local_space_get_space(ls);
7072 if (!map->dim)
7073 goto error;
7075 isl_local_space_free(ls);
7076 return map;
7077 error:
7078 isl_local_space_free(ls);
7079 isl_map_free(map);
7080 return NULL;
7083 /* Compute an explicit representation for the existentially
7084 * quantified variables for which do not know any explicit representation yet.
7086 * We first sort the existentially quantified variables so that the
7087 * existentially quantified variables for which we already have an explicit
7088 * representation are placed before those for which we do not.
7089 * The input dimensions, the output dimensions and the existentially
7090 * quantified variables for which we already have an explicit
7091 * representation are then turned into parameters.
7092 * compute_divs returns a map with the same parameters and
7093 * no input or output dimensions and the dimension specification
7094 * is reset to that of the input, including the existentially quantified
7095 * variables for which we already had an explicit representation.
7097 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7099 struct isl_basic_set *bset;
7100 struct isl_set *set;
7101 struct isl_map *map;
7102 isl_space *dim;
7103 isl_local_space *ls;
7104 unsigned nparam;
7105 unsigned n_in;
7106 unsigned n_out;
7107 int n_known;
7108 int i;
7110 bmap = isl_basic_map_sort_divs(bmap);
7111 bmap = isl_basic_map_cow(bmap);
7112 if (!bmap)
7113 return NULL;
7115 n_known = isl_basic_map_first_unknown_div(bmap);
7116 if (n_known < 0)
7117 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7119 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7120 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7121 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7122 dim = isl_space_set_alloc(bmap->ctx,
7123 nparam + n_in + n_out + n_known, 0);
7124 if (!dim)
7125 goto error;
7127 ls = isl_basic_map_get_local_space(bmap);
7128 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7129 n_known, bmap->n_div - n_known);
7130 if (n_known > 0) {
7131 for (i = n_known; i < bmap->n_div; ++i)
7132 swap_div(bmap, i - n_known, i);
7133 bmap->n_div -= n_known;
7134 bmap->extra -= n_known;
7136 bmap = isl_basic_map_reset_space(bmap, dim);
7137 bset = bset_from_bmap(bmap);
7139 set = parameter_compute_divs(bset);
7140 map = set_to_map(set);
7141 map = replace_space_by_local_space(map, ls);
7143 return map;
7144 error:
7145 isl_basic_map_free(bmap);
7146 return NULL;
7149 /* Remove the explicit representation of local variable "div",
7150 * if there is any.
7152 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7153 __isl_take isl_basic_map *bmap, int div)
7155 isl_bool unknown;
7157 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7158 if (unknown < 0)
7159 return isl_basic_map_free(bmap);
7160 if (unknown)
7161 return bmap;
7163 bmap = isl_basic_map_cow(bmap);
7164 if (!bmap)
7165 return NULL;
7166 isl_int_set_si(bmap->div[div][0], 0);
7167 return bmap;
7170 /* Is local variable "div" of "bmap" marked as not having an explicit
7171 * representation?
7172 * Note that even if "div" is not marked in this way and therefore
7173 * has an explicit representation, this representation may still
7174 * depend (indirectly) on other local variables that do not
7175 * have an explicit representation.
7177 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7178 int div)
7180 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7181 return isl_bool_error;
7182 return isl_int_is_zero(bmap->div[div][0]);
7185 /* Return the position of the first local variable that does not
7186 * have an explicit representation.
7187 * Return the total number of local variables if they all have
7188 * an explicit representation.
7189 * Return -1 on error.
7191 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7193 int i;
7195 if (!bmap)
7196 return -1;
7198 for (i = 0; i < bmap->n_div; ++i) {
7199 if (!isl_basic_map_div_is_known(bmap, i))
7200 return i;
7202 return bmap->n_div;
7205 /* Return the position of the first local variable that does not
7206 * have an explicit representation.
7207 * Return the total number of local variables if they all have
7208 * an explicit representation.
7209 * Return -1 on error.
7211 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7213 return isl_basic_map_first_unknown_div(bset);
7216 /* Does "bmap" have an explicit representation for all local variables?
7218 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7220 int first, n;
7222 n = isl_basic_map_dim(bmap, isl_dim_div);
7223 first = isl_basic_map_first_unknown_div(bmap);
7224 if (first < 0)
7225 return isl_bool_error;
7226 return first == n;
7229 /* Do all basic maps in "map" have an explicit representation
7230 * for all local variables?
7232 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7234 int i;
7236 if (!map)
7237 return isl_bool_error;
7239 for (i = 0; i < map->n; ++i) {
7240 int known = isl_basic_map_divs_known(map->p[i]);
7241 if (known <= 0)
7242 return known;
7245 return isl_bool_true;
7248 /* If bmap contains any unknown divs, then compute explicit
7249 * expressions for them. However, this computation may be
7250 * quite expensive, so first try to remove divs that aren't
7251 * strictly needed.
7253 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7255 int known;
7256 struct isl_map *map;
7258 known = isl_basic_map_divs_known(bmap);
7259 if (known < 0)
7260 goto error;
7261 if (known)
7262 return isl_map_from_basic_map(bmap);
7264 bmap = isl_basic_map_drop_redundant_divs(bmap);
7266 known = isl_basic_map_divs_known(bmap);
7267 if (known < 0)
7268 goto error;
7269 if (known)
7270 return isl_map_from_basic_map(bmap);
7272 map = compute_divs(bmap);
7273 return map;
7274 error:
7275 isl_basic_map_free(bmap);
7276 return NULL;
7279 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7281 int i;
7282 int known;
7283 struct isl_map *res;
7285 if (!map)
7286 return NULL;
7287 if (map->n == 0)
7288 return map;
7290 known = isl_map_divs_known(map);
7291 if (known < 0) {
7292 isl_map_free(map);
7293 return NULL;
7295 if (known)
7296 return map;
7298 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7299 for (i = 1 ; i < map->n; ++i) {
7300 struct isl_map *r2;
7301 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7302 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7303 res = isl_map_union_disjoint(res, r2);
7304 else
7305 res = isl_map_union(res, r2);
7307 isl_map_free(map);
7309 return res;
7312 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7314 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7317 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7319 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7322 struct isl_set *isl_map_domain(struct isl_map *map)
7324 int i;
7325 struct isl_set *set;
7327 if (!map)
7328 goto error;
7330 map = isl_map_cow(map);
7331 if (!map)
7332 return NULL;
7334 set = set_from_map(map);
7335 set->dim = isl_space_domain(set->dim);
7336 if (!set->dim)
7337 goto error;
7338 for (i = 0; i < map->n; ++i) {
7339 set->p[i] = isl_basic_map_domain(map->p[i]);
7340 if (!set->p[i])
7341 goto error;
7343 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7344 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7345 return set;
7346 error:
7347 isl_map_free(map);
7348 return NULL;
7351 /* Return the union of "map1" and "map2", where we assume for now that
7352 * "map1" and "map2" are disjoint. Note that the basic maps inside
7353 * "map1" or "map2" may not be disjoint from each other.
7354 * Also note that this function is also called from isl_map_union,
7355 * which takes care of handling the situation where "map1" and "map2"
7356 * may not be disjoint.
7358 * If one of the inputs is empty, we can simply return the other input.
7359 * Similarly, if one of the inputs is universal, then it is equal to the union.
7361 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7362 __isl_take isl_map *map2)
7364 int i;
7365 unsigned flags = 0;
7366 struct isl_map *map = NULL;
7367 int is_universe;
7369 if (!map1 || !map2)
7370 goto error;
7372 if (!isl_space_is_equal(map1->dim, map2->dim))
7373 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7374 "spaces don't match", goto error);
7376 if (map1->n == 0) {
7377 isl_map_free(map1);
7378 return map2;
7380 if (map2->n == 0) {
7381 isl_map_free(map2);
7382 return map1;
7385 is_universe = isl_map_plain_is_universe(map1);
7386 if (is_universe < 0)
7387 goto error;
7388 if (is_universe) {
7389 isl_map_free(map2);
7390 return map1;
7393 is_universe = isl_map_plain_is_universe(map2);
7394 if (is_universe < 0)
7395 goto error;
7396 if (is_universe) {
7397 isl_map_free(map1);
7398 return map2;
7401 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7402 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7403 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7405 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7406 map1->n + map2->n, flags);
7407 if (!map)
7408 goto error;
7409 for (i = 0; i < map1->n; ++i) {
7410 map = isl_map_add_basic_map(map,
7411 isl_basic_map_copy(map1->p[i]));
7412 if (!map)
7413 goto error;
7415 for (i = 0; i < map2->n; ++i) {
7416 map = isl_map_add_basic_map(map,
7417 isl_basic_map_copy(map2->p[i]));
7418 if (!map)
7419 goto error;
7421 isl_map_free(map1);
7422 isl_map_free(map2);
7423 return map;
7424 error:
7425 isl_map_free(map);
7426 isl_map_free(map1);
7427 isl_map_free(map2);
7428 return NULL;
7431 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7432 * guaranteed to be disjoint by the caller.
7434 * Note that this functions is called from within isl_map_make_disjoint,
7435 * so we have to be careful not to touch the constraints of the inputs
7436 * in any way.
7438 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7439 __isl_take isl_map *map2)
7441 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7444 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7445 * not be disjoint. The parameters are assumed to have been aligned.
7447 * We currently simply call map_union_disjoint, the internal operation
7448 * of which does not really depend on the inputs being disjoint.
7449 * If the result contains more than one basic map, then we clear
7450 * the disjoint flag since the result may contain basic maps from
7451 * both inputs and these are not guaranteed to be disjoint.
7453 * As a special case, if "map1" and "map2" are obviously equal,
7454 * then we simply return "map1".
7456 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7457 __isl_take isl_map *map2)
7459 int equal;
7461 if (!map1 || !map2)
7462 goto error;
7464 equal = isl_map_plain_is_equal(map1, map2);
7465 if (equal < 0)
7466 goto error;
7467 if (equal) {
7468 isl_map_free(map2);
7469 return map1;
7472 map1 = map_union_disjoint(map1, map2);
7473 if (!map1)
7474 return NULL;
7475 if (map1->n > 1)
7476 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7477 return map1;
7478 error:
7479 isl_map_free(map1);
7480 isl_map_free(map2);
7481 return NULL;
7484 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7485 * not be disjoint.
7487 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7488 __isl_take isl_map *map2)
7490 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7493 struct isl_set *isl_set_union_disjoint(
7494 struct isl_set *set1, struct isl_set *set2)
7496 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7497 set_to_map(set2)));
7500 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7502 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7505 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7506 * the results.
7508 * "map" and "set" are assumed to be compatible and non-NULL.
7510 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7511 __isl_take isl_set *set,
7512 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7513 __isl_take isl_basic_set *bset))
7515 unsigned flags = 0;
7516 struct isl_map *result;
7517 int i, j;
7519 if (isl_set_plain_is_universe(set)) {
7520 isl_set_free(set);
7521 return map;
7524 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7525 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7526 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7528 result = isl_map_alloc_space(isl_space_copy(map->dim),
7529 map->n * set->n, flags);
7530 for (i = 0; result && i < map->n; ++i)
7531 for (j = 0; j < set->n; ++j) {
7532 result = isl_map_add_basic_map(result,
7533 fn(isl_basic_map_copy(map->p[i]),
7534 isl_basic_set_copy(set->p[j])));
7535 if (!result)
7536 break;
7539 isl_map_free(map);
7540 isl_set_free(set);
7541 return result;
7544 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7545 __isl_take isl_set *set)
7547 if (!map || !set)
7548 goto error;
7550 if (!isl_map_compatible_range(map, set))
7551 isl_die(set->ctx, isl_error_invalid,
7552 "incompatible spaces", goto error);
7554 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7555 error:
7556 isl_map_free(map);
7557 isl_set_free(set);
7558 return NULL;
7561 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7562 __isl_take isl_set *set)
7564 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7567 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7568 __isl_take isl_set *set)
7570 if (!map || !set)
7571 goto error;
7573 if (!isl_map_compatible_domain(map, set))
7574 isl_die(set->ctx, isl_error_invalid,
7575 "incompatible spaces", goto error);
7577 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7578 error:
7579 isl_map_free(map);
7580 isl_set_free(set);
7581 return NULL;
7584 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7585 __isl_take isl_set *set)
7587 return isl_map_align_params_map_map_and(map, set,
7588 &map_intersect_domain);
7591 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7592 __isl_take isl_map *map2)
7594 if (!map1 || !map2)
7595 goto error;
7596 map1 = isl_map_reverse(map1);
7597 map1 = isl_map_apply_range(map1, map2);
7598 return isl_map_reverse(map1);
7599 error:
7600 isl_map_free(map1);
7601 isl_map_free(map2);
7602 return NULL;
7605 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7606 __isl_take isl_map *map2)
7608 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7611 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7612 __isl_take isl_map *map2)
7614 isl_space *dim_result;
7615 struct isl_map *result;
7616 int i, j;
7618 if (!map1 || !map2)
7619 goto error;
7621 dim_result = isl_space_join(isl_space_copy(map1->dim),
7622 isl_space_copy(map2->dim));
7624 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7625 if (!result)
7626 goto error;
7627 for (i = 0; i < map1->n; ++i)
7628 for (j = 0; j < map2->n; ++j) {
7629 result = isl_map_add_basic_map(result,
7630 isl_basic_map_apply_range(
7631 isl_basic_map_copy(map1->p[i]),
7632 isl_basic_map_copy(map2->p[j])));
7633 if (!result)
7634 goto error;
7636 isl_map_free(map1);
7637 isl_map_free(map2);
7638 if (result && result->n <= 1)
7639 ISL_F_SET(result, ISL_MAP_DISJOINT);
7640 return result;
7641 error:
7642 isl_map_free(map1);
7643 isl_map_free(map2);
7644 return NULL;
7647 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7648 __isl_take isl_map *map2)
7650 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7654 * returns range - domain
7656 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7658 isl_space *target_space;
7659 struct isl_basic_set *bset;
7660 unsigned dim;
7661 unsigned nparam;
7662 int i;
7664 if (!bmap)
7665 goto error;
7666 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7667 bmap->dim, isl_dim_out),
7668 goto error);
7669 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7670 dim = isl_basic_map_n_in(bmap);
7671 nparam = isl_basic_map_n_param(bmap);
7672 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7673 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7674 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7675 for (i = 0; i < dim; ++i) {
7676 int j = isl_basic_map_alloc_equality(bmap);
7677 if (j < 0) {
7678 bmap = isl_basic_map_free(bmap);
7679 break;
7681 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7682 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7683 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7684 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7686 bset = isl_basic_map_domain(bmap);
7687 bset = isl_basic_set_reset_space(bset, target_space);
7688 return bset;
7689 error:
7690 isl_basic_map_free(bmap);
7691 return NULL;
7695 * returns range - domain
7697 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7699 int i;
7700 isl_space *dim;
7701 struct isl_set *result;
7703 if (!map)
7704 return NULL;
7706 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7707 map->dim, isl_dim_out),
7708 goto error);
7709 dim = isl_map_get_space(map);
7710 dim = isl_space_domain(dim);
7711 result = isl_set_alloc_space(dim, map->n, 0);
7712 if (!result)
7713 goto error;
7714 for (i = 0; i < map->n; ++i)
7715 result = isl_set_add_basic_set(result,
7716 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7717 isl_map_free(map);
7718 return result;
7719 error:
7720 isl_map_free(map);
7721 return NULL;
7725 * returns [domain -> range] -> range - domain
7727 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7728 __isl_take isl_basic_map *bmap)
7730 int i, k;
7731 isl_space *dim;
7732 isl_basic_map *domain;
7733 int nparam, n;
7734 unsigned total;
7736 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7737 bmap->dim, isl_dim_out))
7738 isl_die(bmap->ctx, isl_error_invalid,
7739 "domain and range don't match", goto error);
7741 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7742 n = isl_basic_map_dim(bmap, isl_dim_in);
7744 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7745 domain = isl_basic_map_universe(dim);
7747 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7748 bmap = isl_basic_map_apply_range(bmap, domain);
7749 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7751 total = isl_basic_map_total_dim(bmap);
7753 for (i = 0; i < n; ++i) {
7754 k = isl_basic_map_alloc_equality(bmap);
7755 if (k < 0)
7756 goto error;
7757 isl_seq_clr(bmap->eq[k], 1 + total);
7758 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7759 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7760 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7763 bmap = isl_basic_map_gauss(bmap, NULL);
7764 return isl_basic_map_finalize(bmap);
7765 error:
7766 isl_basic_map_free(bmap);
7767 return NULL;
7771 * returns [domain -> range] -> range - domain
7773 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7775 int i;
7776 isl_space *domain_dim;
7778 if (!map)
7779 return NULL;
7781 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7782 map->dim, isl_dim_out))
7783 isl_die(map->ctx, isl_error_invalid,
7784 "domain and range don't match", goto error);
7786 map = isl_map_cow(map);
7787 if (!map)
7788 return NULL;
7790 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7791 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7792 map->dim = isl_space_join(map->dim, domain_dim);
7793 if (!map->dim)
7794 goto error;
7795 for (i = 0; i < map->n; ++i) {
7796 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7797 if (!map->p[i])
7798 goto error;
7800 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7801 return map;
7802 error:
7803 isl_map_free(map);
7804 return NULL;
7807 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7809 struct isl_basic_map *bmap;
7810 unsigned nparam;
7811 unsigned dim;
7812 int i;
7814 if (!dims)
7815 return NULL;
7817 nparam = dims->nparam;
7818 dim = dims->n_out;
7819 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7820 if (!bmap)
7821 goto error;
7823 for (i = 0; i < dim; ++i) {
7824 int j = isl_basic_map_alloc_equality(bmap);
7825 if (j < 0)
7826 goto error;
7827 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7828 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7829 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7831 return isl_basic_map_finalize(bmap);
7832 error:
7833 isl_basic_map_free(bmap);
7834 return NULL;
7837 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7839 if (!dim)
7840 return NULL;
7841 if (dim->n_in != dim->n_out)
7842 isl_die(dim->ctx, isl_error_invalid,
7843 "number of input and output dimensions needs to be "
7844 "the same", goto error);
7845 return basic_map_identity(dim);
7846 error:
7847 isl_space_free(dim);
7848 return NULL;
7851 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7853 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7856 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7858 isl_space *dim = isl_set_get_space(set);
7859 isl_map *id;
7860 id = isl_map_identity(isl_space_map_from_set(dim));
7861 return isl_map_intersect_range(id, set);
7864 /* Construct a basic set with all set dimensions having only non-negative
7865 * values.
7867 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7868 __isl_take isl_space *space)
7870 int i;
7871 unsigned nparam;
7872 unsigned dim;
7873 struct isl_basic_set *bset;
7875 if (!space)
7876 return NULL;
7877 nparam = space->nparam;
7878 dim = space->n_out;
7879 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7880 if (!bset)
7881 return NULL;
7882 for (i = 0; i < dim; ++i) {
7883 int k = isl_basic_set_alloc_inequality(bset);
7884 if (k < 0)
7885 goto error;
7886 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7887 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7889 return bset;
7890 error:
7891 isl_basic_set_free(bset);
7892 return NULL;
7895 /* Construct the half-space x_pos >= 0.
7897 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7898 int pos)
7900 int k;
7901 isl_basic_set *nonneg;
7903 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7904 k = isl_basic_set_alloc_inequality(nonneg);
7905 if (k < 0)
7906 goto error;
7907 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7908 isl_int_set_si(nonneg->ineq[k][pos], 1);
7910 return isl_basic_set_finalize(nonneg);
7911 error:
7912 isl_basic_set_free(nonneg);
7913 return NULL;
7916 /* Construct the half-space x_pos <= -1.
7918 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7920 int k;
7921 isl_basic_set *neg;
7923 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7924 k = isl_basic_set_alloc_inequality(neg);
7925 if (k < 0)
7926 goto error;
7927 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7928 isl_int_set_si(neg->ineq[k][0], -1);
7929 isl_int_set_si(neg->ineq[k][pos], -1);
7931 return isl_basic_set_finalize(neg);
7932 error:
7933 isl_basic_set_free(neg);
7934 return NULL;
7937 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7938 enum isl_dim_type type, unsigned first, unsigned n)
7940 int i;
7941 unsigned offset;
7942 isl_basic_set *nonneg;
7943 isl_basic_set *neg;
7945 if (!set)
7946 return NULL;
7947 if (n == 0)
7948 return set;
7950 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7952 offset = pos(set->dim, type);
7953 for (i = 0; i < n; ++i) {
7954 nonneg = nonneg_halfspace(isl_set_get_space(set),
7955 offset + first + i);
7956 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7958 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7961 return set;
7962 error:
7963 isl_set_free(set);
7964 return NULL;
7967 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7968 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7969 void *user)
7971 isl_set *half;
7973 if (!set)
7974 return -1;
7975 if (isl_set_plain_is_empty(set)) {
7976 isl_set_free(set);
7977 return 0;
7979 if (first == len)
7980 return fn(set, signs, user);
7982 signs[first] = 1;
7983 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7984 1 + first));
7985 half = isl_set_intersect(half, isl_set_copy(set));
7986 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7987 goto error;
7989 signs[first] = -1;
7990 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7991 1 + first));
7992 half = isl_set_intersect(half, set);
7993 return foreach_orthant(half, signs, first + 1, len, fn, user);
7994 error:
7995 isl_set_free(set);
7996 return -1;
7999 /* Call "fn" on the intersections of "set" with each of the orthants
8000 * (except for obviously empty intersections). The orthant is identified
8001 * by the signs array, with each entry having value 1 or -1 according
8002 * to the sign of the corresponding variable.
8004 int isl_set_foreach_orthant(__isl_keep isl_set *set,
8005 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8006 void *user)
8008 unsigned nparam;
8009 unsigned nvar;
8010 int *signs;
8011 int r;
8013 if (!set)
8014 return -1;
8015 if (isl_set_plain_is_empty(set))
8016 return 0;
8018 nparam = isl_set_dim(set, isl_dim_param);
8019 nvar = isl_set_dim(set, isl_dim_set);
8021 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8023 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8024 fn, user);
8026 free(signs);
8028 return r;
8031 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8033 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8036 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8037 __isl_keep isl_basic_map *bmap2)
8039 int is_subset;
8040 struct isl_map *map1;
8041 struct isl_map *map2;
8043 if (!bmap1 || !bmap2)
8044 return isl_bool_error;
8046 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8047 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8049 is_subset = isl_map_is_subset(map1, map2);
8051 isl_map_free(map1);
8052 isl_map_free(map2);
8054 return is_subset;
8057 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8058 __isl_keep isl_basic_set *bset2)
8060 return isl_basic_map_is_subset(bset1, bset2);
8063 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8064 __isl_keep isl_basic_map *bmap2)
8066 isl_bool is_subset;
8068 if (!bmap1 || !bmap2)
8069 return isl_bool_error;
8070 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8071 if (is_subset != isl_bool_true)
8072 return is_subset;
8073 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8074 return is_subset;
8077 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8078 __isl_keep isl_basic_set *bset2)
8080 return isl_basic_map_is_equal(
8081 bset_to_bmap(bset1), bset_to_bmap(bset2));
8084 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8086 int i;
8087 int is_empty;
8089 if (!map)
8090 return isl_bool_error;
8091 for (i = 0; i < map->n; ++i) {
8092 is_empty = isl_basic_map_is_empty(map->p[i]);
8093 if (is_empty < 0)
8094 return isl_bool_error;
8095 if (!is_empty)
8096 return isl_bool_false;
8098 return isl_bool_true;
8101 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8103 return map ? map->n == 0 : isl_bool_error;
8106 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8108 return set ? set->n == 0 : isl_bool_error;
8111 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8113 return isl_map_is_empty(set_to_map(set));
8116 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8118 if (!map1 || !map2)
8119 return -1;
8121 return isl_space_is_equal(map1->dim, map2->dim);
8124 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8126 if (!set1 || !set2)
8127 return -1;
8129 return isl_space_is_equal(set1->dim, set2->dim);
8132 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8134 isl_bool is_subset;
8136 if (!map1 || !map2)
8137 return isl_bool_error;
8138 is_subset = isl_map_is_subset(map1, map2);
8139 if (is_subset != isl_bool_true)
8140 return is_subset;
8141 is_subset = isl_map_is_subset(map2, map1);
8142 return is_subset;
8145 /* Is "map1" equal to "map2"?
8147 * First check if they are obviously equal.
8148 * If not, then perform a more detailed analysis.
8150 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8152 isl_bool equal;
8154 equal = isl_map_plain_is_equal(map1, map2);
8155 if (equal < 0 || equal)
8156 return equal;
8157 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8160 isl_bool isl_basic_map_is_strict_subset(
8161 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8163 isl_bool is_subset;
8165 if (!bmap1 || !bmap2)
8166 return isl_bool_error;
8167 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8168 if (is_subset != isl_bool_true)
8169 return is_subset;
8170 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8171 if (is_subset == isl_bool_error)
8172 return is_subset;
8173 return !is_subset;
8176 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8177 __isl_keep isl_map *map2)
8179 isl_bool is_subset;
8181 if (!map1 || !map2)
8182 return isl_bool_error;
8183 is_subset = isl_map_is_subset(map1, map2);
8184 if (is_subset != isl_bool_true)
8185 return is_subset;
8186 is_subset = isl_map_is_subset(map2, map1);
8187 if (is_subset == isl_bool_error)
8188 return is_subset;
8189 return !is_subset;
8192 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8193 __isl_keep isl_set *set2)
8195 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8198 /* Is "bmap" obviously equal to the universe with the same space?
8200 * That is, does it not have any constraints?
8202 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8204 if (!bmap)
8205 return isl_bool_error;
8206 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8209 /* Is "bset" obviously equal to the universe with the same space?
8211 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8213 return isl_basic_map_plain_is_universe(bset);
8216 /* If "c" does not involve any existentially quantified variables,
8217 * then set *univ to false and abort
8219 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8221 isl_bool *univ = user;
8222 unsigned n;
8224 n = isl_constraint_dim(c, isl_dim_div);
8225 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8226 isl_constraint_free(c);
8227 if (*univ < 0 || !*univ)
8228 return isl_stat_error;
8229 return isl_stat_ok;
8232 /* Is "bmap" equal to the universe with the same space?
8234 * First check if it is obviously equal to the universe.
8235 * If not and if there are any constraints not involving
8236 * existentially quantified variables, then it is certainly
8237 * not equal to the universe.
8238 * Otherwise, check if the universe is a subset of "bmap".
8240 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8242 isl_bool univ;
8243 isl_basic_map *test;
8245 univ = isl_basic_map_plain_is_universe(bmap);
8246 if (univ < 0 || univ)
8247 return univ;
8248 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8249 return isl_bool_false;
8250 univ = isl_bool_true;
8251 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8252 univ)
8253 return isl_bool_error;
8254 if (univ < 0 || !univ)
8255 return univ;
8256 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8257 univ = isl_basic_map_is_subset(test, bmap);
8258 isl_basic_map_free(test);
8259 return univ;
8262 /* Is "bset" equal to the universe with the same space?
8264 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8266 return isl_basic_map_is_universe(bset);
8269 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8271 int i;
8273 if (!map)
8274 return isl_bool_error;
8276 for (i = 0; i < map->n; ++i) {
8277 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8278 if (r < 0 || r)
8279 return r;
8282 return isl_bool_false;
8285 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8287 return isl_map_plain_is_universe(set_to_map(set));
8290 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8292 struct isl_basic_set *bset = NULL;
8293 struct isl_vec *sample = NULL;
8294 isl_bool empty, non_empty;
8296 if (!bmap)
8297 return isl_bool_error;
8299 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8300 return isl_bool_true;
8302 if (isl_basic_map_plain_is_universe(bmap))
8303 return isl_bool_false;
8305 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8306 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8307 copy = isl_basic_map_remove_redundancies(copy);
8308 empty = isl_basic_map_plain_is_empty(copy);
8309 isl_basic_map_free(copy);
8310 return empty;
8313 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8314 if (non_empty < 0)
8315 return isl_bool_error;
8316 if (non_empty)
8317 return isl_bool_false;
8318 isl_vec_free(bmap->sample);
8319 bmap->sample = NULL;
8320 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8321 if (!bset)
8322 return isl_bool_error;
8323 sample = isl_basic_set_sample_vec(bset);
8324 if (!sample)
8325 return isl_bool_error;
8326 empty = sample->size == 0;
8327 isl_vec_free(bmap->sample);
8328 bmap->sample = sample;
8329 if (empty)
8330 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8332 return empty;
8335 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8337 if (!bmap)
8338 return isl_bool_error;
8339 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8342 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8344 if (!bset)
8345 return isl_bool_error;
8346 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8349 /* Is "bmap" known to be non-empty?
8351 * That is, is the cached sample still valid?
8353 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8355 unsigned total;
8357 if (!bmap)
8358 return isl_bool_error;
8359 if (!bmap->sample)
8360 return isl_bool_false;
8361 total = 1 + isl_basic_map_total_dim(bmap);
8362 if (bmap->sample->size != total)
8363 return isl_bool_false;
8364 return isl_basic_map_contains(bmap, bmap->sample);
8367 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8369 return isl_basic_map_is_empty(bset_to_bmap(bset));
8372 struct isl_map *isl_basic_map_union(
8373 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8375 struct isl_map *map;
8376 if (!bmap1 || !bmap2)
8377 goto error;
8379 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8381 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8382 if (!map)
8383 goto error;
8384 map = isl_map_add_basic_map(map, bmap1);
8385 map = isl_map_add_basic_map(map, bmap2);
8386 return map;
8387 error:
8388 isl_basic_map_free(bmap1);
8389 isl_basic_map_free(bmap2);
8390 return NULL;
8393 struct isl_set *isl_basic_set_union(
8394 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8396 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8397 bset_to_bmap(bset2)));
8400 /* Order divs such that any div only depends on previous divs */
8401 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8403 int i;
8404 unsigned off;
8406 if (!bmap)
8407 return NULL;
8409 off = isl_space_dim(bmap->dim, isl_dim_all);
8411 for (i = 0; i < bmap->n_div; ++i) {
8412 int pos;
8413 if (isl_int_is_zero(bmap->div[i][0]))
8414 continue;
8415 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8416 bmap->n_div-i);
8417 if (pos == -1)
8418 continue;
8419 if (pos == 0)
8420 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8421 "integer division depends on itself",
8422 return isl_basic_map_free(bmap));
8423 isl_basic_map_swap_div(bmap, i, i + pos);
8424 --i;
8426 return bmap;
8429 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8431 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8434 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8436 int i;
8438 if (!map)
8439 return 0;
8441 for (i = 0; i < map->n; ++i) {
8442 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8443 if (!map->p[i])
8444 goto error;
8447 return map;
8448 error:
8449 isl_map_free(map);
8450 return NULL;
8453 /* Sort the local variables of "bset".
8455 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8456 __isl_take isl_basic_set *bset)
8458 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8461 /* Apply the expansion computed by isl_merge_divs.
8462 * The expansion itself is given by "exp" while the resulting
8463 * list of divs is given by "div".
8465 * Move the integer divisions of "bmap" into the right position
8466 * according to "exp" and then introduce the additional integer
8467 * divisions, adding div constraints.
8468 * The moving should be done first to avoid moving coefficients
8469 * in the definitions of the extra integer divisions.
8471 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8472 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8474 int i, j;
8475 int n_div;
8477 bmap = isl_basic_map_cow(bmap);
8478 if (!bmap || !div)
8479 goto error;
8481 if (div->n_row < bmap->n_div)
8482 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8483 "not an expansion", goto error);
8485 n_div = bmap->n_div;
8486 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8487 div->n_row - n_div, 0,
8488 2 * (div->n_row - n_div));
8490 for (i = n_div; i < div->n_row; ++i)
8491 if (isl_basic_map_alloc_div(bmap) < 0)
8492 goto error;
8494 for (j = n_div - 1; j >= 0; --j) {
8495 if (exp[j] == j)
8496 break;
8497 isl_basic_map_swap_div(bmap, j, exp[j]);
8499 j = 0;
8500 for (i = 0; i < div->n_row; ++i) {
8501 if (j < n_div && exp[j] == i) {
8502 j++;
8503 } else {
8504 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8505 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8506 continue;
8507 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8508 goto error;
8512 isl_mat_free(div);
8513 return bmap;
8514 error:
8515 isl_basic_map_free(bmap);
8516 isl_mat_free(div);
8517 return NULL;
8520 /* Apply the expansion computed by isl_merge_divs.
8521 * The expansion itself is given by "exp" while the resulting
8522 * list of divs is given by "div".
8524 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8525 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8527 return isl_basic_map_expand_divs(bset, div, exp);
8530 /* Look for a div in dst that corresponds to the div "div" in src.
8531 * The divs before "div" in src and dst are assumed to be the same.
8533 * Returns -1 if no corresponding div was found and the position
8534 * of the corresponding div in dst otherwise.
8536 static int find_div(struct isl_basic_map *dst,
8537 struct isl_basic_map *src, unsigned div)
8539 int i;
8541 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8543 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8544 for (i = div; i < dst->n_div; ++i)
8545 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8546 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8547 dst->n_div - div) == -1)
8548 return i;
8549 return -1;
8552 /* Align the divs of "dst" to those of "src", adding divs from "src"
8553 * if needed. That is, make sure that the first src->n_div divs
8554 * of the result are equal to those of src.
8556 * The result is not finalized as by design it will have redundant
8557 * divs if any divs from "src" were copied.
8559 __isl_give isl_basic_map *isl_basic_map_align_divs(
8560 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8562 int i;
8563 int known, extended;
8564 unsigned total;
8566 if (!dst || !src)
8567 return isl_basic_map_free(dst);
8569 if (src->n_div == 0)
8570 return dst;
8572 known = isl_basic_map_divs_known(src);
8573 if (known < 0)
8574 return isl_basic_map_free(dst);
8575 if (!known)
8576 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8577 "some src divs are unknown",
8578 return isl_basic_map_free(dst));
8580 src = isl_basic_map_order_divs(src);
8582 extended = 0;
8583 total = isl_space_dim(src->dim, isl_dim_all);
8584 for (i = 0; i < src->n_div; ++i) {
8585 int j = find_div(dst, src, i);
8586 if (j < 0) {
8587 if (!extended) {
8588 int extra = src->n_div - i;
8589 dst = isl_basic_map_cow(dst);
8590 if (!dst)
8591 return NULL;
8592 dst = isl_basic_map_extend_space(dst,
8593 isl_space_copy(dst->dim),
8594 extra, 0, 2 * extra);
8595 extended = 1;
8597 j = isl_basic_map_alloc_div(dst);
8598 if (j < 0)
8599 return isl_basic_map_free(dst);
8600 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8601 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8602 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8603 return isl_basic_map_free(dst);
8605 if (j != i)
8606 isl_basic_map_swap_div(dst, i, j);
8608 return dst;
8611 struct isl_basic_set *isl_basic_set_align_divs(
8612 struct isl_basic_set *dst, struct isl_basic_set *src)
8614 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8615 bset_to_bmap(src)));
8618 struct isl_map *isl_map_align_divs(struct isl_map *map)
8620 int i;
8622 if (!map)
8623 return NULL;
8624 if (map->n == 0)
8625 return map;
8626 map = isl_map_compute_divs(map);
8627 map = isl_map_cow(map);
8628 if (!map)
8629 return NULL;
8631 for (i = 1; i < map->n; ++i)
8632 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8633 for (i = 1; i < map->n; ++i) {
8634 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8635 if (!map->p[i])
8636 return isl_map_free(map);
8639 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8640 return map;
8643 struct isl_set *isl_set_align_divs(struct isl_set *set)
8645 return set_from_map(isl_map_align_divs(set_to_map(set)));
8648 /* Align the divs of the basic maps in "map" to those
8649 * of the basic maps in "list", as well as to the other basic maps in "map".
8650 * The elements in "list" are assumed to have known divs.
8652 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8653 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8655 int i, n;
8657 map = isl_map_compute_divs(map);
8658 map = isl_map_cow(map);
8659 if (!map || !list)
8660 return isl_map_free(map);
8661 if (map->n == 0)
8662 return map;
8664 n = isl_basic_map_list_n_basic_map(list);
8665 for (i = 0; i < n; ++i) {
8666 isl_basic_map *bmap;
8668 bmap = isl_basic_map_list_get_basic_map(list, i);
8669 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8670 isl_basic_map_free(bmap);
8672 if (!map->p[0])
8673 return isl_map_free(map);
8675 return isl_map_align_divs(map);
8678 /* Align the divs of each element of "list" to those of "bmap".
8679 * Both "bmap" and the elements of "list" are assumed to have known divs.
8681 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8682 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8684 int i, n;
8686 if (!list || !bmap)
8687 return isl_basic_map_list_free(list);
8689 n = isl_basic_map_list_n_basic_map(list);
8690 for (i = 0; i < n; ++i) {
8691 isl_basic_map *bmap_i;
8693 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8694 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8695 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8698 return list;
8701 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8702 __isl_take isl_map *map)
8704 if (!set || !map)
8705 goto error;
8706 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8707 map = isl_map_intersect_domain(map, set);
8708 set = isl_map_range(map);
8709 return set;
8710 error:
8711 isl_set_free(set);
8712 isl_map_free(map);
8713 return NULL;
8716 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8717 __isl_take isl_map *map)
8719 return isl_map_align_params_map_map_and(set, map, &set_apply);
8722 /* There is no need to cow as removing empty parts doesn't change
8723 * the meaning of the set.
8725 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8727 int i;
8729 if (!map)
8730 return NULL;
8732 for (i = map->n - 1; i >= 0; --i)
8733 remove_if_empty(map, i);
8735 return map;
8738 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8740 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8743 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8745 struct isl_basic_map *bmap;
8746 if (!map || map->n == 0)
8747 return NULL;
8748 bmap = map->p[map->n-1];
8749 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8750 return isl_basic_map_copy(bmap);
8753 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8755 return map_copy_basic_map(map);
8758 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8760 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8763 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8764 __isl_keep isl_basic_map *bmap)
8766 int i;
8768 if (!map || !bmap)
8769 goto error;
8770 for (i = map->n-1; i >= 0; --i) {
8771 if (map->p[i] != bmap)
8772 continue;
8773 map = isl_map_cow(map);
8774 if (!map)
8775 goto error;
8776 isl_basic_map_free(map->p[i]);
8777 if (i != map->n-1) {
8778 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8779 map->p[i] = map->p[map->n-1];
8781 map->n--;
8782 return map;
8784 return map;
8785 error:
8786 isl_map_free(map);
8787 return NULL;
8790 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8791 __isl_keep isl_basic_map *bmap)
8793 return map_drop_basic_map(map, bmap);
8796 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8797 struct isl_basic_set *bset)
8799 return set_from_map(map_drop_basic_map(set_to_map(set),
8800 bset_to_bmap(bset)));
8803 /* Given two basic sets bset1 and bset2, compute the maximal difference
8804 * between the values of dimension pos in bset1 and those in bset2
8805 * for any common value of the parameters and dimensions preceding pos.
8807 static enum isl_lp_result basic_set_maximal_difference_at(
8808 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8809 int pos, isl_int *opt)
8811 isl_basic_map *bmap1;
8812 isl_basic_map *bmap2;
8813 struct isl_ctx *ctx;
8814 struct isl_vec *obj;
8815 unsigned total;
8816 unsigned nparam;
8817 unsigned dim1;
8818 enum isl_lp_result res;
8820 if (!bset1 || !bset2)
8821 return isl_lp_error;
8823 nparam = isl_basic_set_n_param(bset1);
8824 dim1 = isl_basic_set_n_dim(bset1);
8826 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8827 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8828 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8829 isl_dim_out, 0, pos);
8830 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8831 isl_dim_out, 0, pos);
8832 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8833 if (!bmap1)
8834 return isl_lp_error;
8836 total = isl_basic_map_total_dim(bmap1);
8837 ctx = bmap1->ctx;
8838 obj = isl_vec_alloc(ctx, 1 + total);
8839 if (!obj)
8840 goto error;
8841 isl_seq_clr(obj->block.data, 1 + total);
8842 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8843 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8844 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8845 opt, NULL, NULL);
8846 isl_basic_map_free(bmap1);
8847 isl_vec_free(obj);
8848 return res;
8849 error:
8850 isl_basic_map_free(bmap1);
8851 return isl_lp_error;
8854 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8855 * for any common value of the parameters and dimensions preceding pos
8856 * in both basic sets, the values of dimension pos in bset1 are
8857 * smaller or larger than those in bset2.
8859 * Returns
8860 * 1 if bset1 follows bset2
8861 * -1 if bset1 precedes bset2
8862 * 0 if bset1 and bset2 are incomparable
8863 * -2 if some error occurred.
8865 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8866 struct isl_basic_set *bset2, int pos)
8868 isl_int opt;
8869 enum isl_lp_result res;
8870 int cmp;
8872 isl_int_init(opt);
8874 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8876 if (res == isl_lp_empty)
8877 cmp = 0;
8878 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8879 res == isl_lp_unbounded)
8880 cmp = 1;
8881 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8882 cmp = -1;
8883 else
8884 cmp = -2;
8886 isl_int_clear(opt);
8887 return cmp;
8890 /* Given two basic sets bset1 and bset2, check whether
8891 * for any common value of the parameters and dimensions preceding pos
8892 * there is a value of dimension pos in bset1 that is larger
8893 * than a value of the same dimension in bset2.
8895 * Return
8896 * 1 if there exists such a pair
8897 * 0 if there is no such pair, but there is a pair of equal values
8898 * -1 otherwise
8899 * -2 if some error occurred.
8901 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8902 __isl_keep isl_basic_set *bset2, int pos)
8904 isl_int opt;
8905 enum isl_lp_result res;
8906 int cmp;
8908 isl_int_init(opt);
8910 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8912 if (res == isl_lp_empty)
8913 cmp = -1;
8914 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8915 res == isl_lp_unbounded)
8916 cmp = 1;
8917 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8918 cmp = -1;
8919 else if (res == isl_lp_ok)
8920 cmp = 0;
8921 else
8922 cmp = -2;
8924 isl_int_clear(opt);
8925 return cmp;
8928 /* Given two sets set1 and set2, check whether
8929 * for any common value of the parameters and dimensions preceding pos
8930 * there is a value of dimension pos in set1 that is larger
8931 * than a value of the same dimension in set2.
8933 * Return
8934 * 1 if there exists such a pair
8935 * 0 if there is no such pair, but there is a pair of equal values
8936 * -1 otherwise
8937 * -2 if some error occurred.
8939 int isl_set_follows_at(__isl_keep isl_set *set1,
8940 __isl_keep isl_set *set2, int pos)
8942 int i, j;
8943 int follows = -1;
8945 if (!set1 || !set2)
8946 return -2;
8948 for (i = 0; i < set1->n; ++i)
8949 for (j = 0; j < set2->n; ++j) {
8950 int f;
8951 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8952 if (f == 1 || f == -2)
8953 return f;
8954 if (f > follows)
8955 follows = f;
8958 return follows;
8961 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8962 unsigned pos, isl_int *val)
8964 int i;
8965 int d;
8966 unsigned total;
8968 if (!bmap)
8969 return -1;
8970 total = isl_basic_map_total_dim(bmap);
8971 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8972 for (; d+1 > pos; --d)
8973 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8974 break;
8975 if (d != pos)
8976 continue;
8977 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8978 return 0;
8979 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8980 return 0;
8981 if (!isl_int_is_one(bmap->eq[i][1+d]))
8982 return 0;
8983 if (val)
8984 isl_int_neg(*val, bmap->eq[i][0]);
8985 return 1;
8987 return 0;
8990 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8991 unsigned pos, isl_int *val)
8993 int i;
8994 isl_int v;
8995 isl_int tmp;
8996 int fixed;
8998 if (!map)
8999 return -1;
9000 if (map->n == 0)
9001 return 0;
9002 if (map->n == 1)
9003 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9004 isl_int_init(v);
9005 isl_int_init(tmp);
9006 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9007 for (i = 1; fixed == 1 && i < map->n; ++i) {
9008 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9009 if (fixed == 1 && isl_int_ne(tmp, v))
9010 fixed = 0;
9012 if (val)
9013 isl_int_set(*val, v);
9014 isl_int_clear(tmp);
9015 isl_int_clear(v);
9016 return fixed;
9019 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
9020 unsigned pos, isl_int *val)
9022 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9023 pos, val);
9026 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
9027 isl_int *val)
9029 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
9032 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9033 enum isl_dim_type type, unsigned pos, isl_int *val)
9035 if (pos >= isl_basic_map_dim(bmap, type))
9036 return -1;
9037 return isl_basic_map_plain_has_fixed_var(bmap,
9038 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9041 /* If "bmap" obviously lies on a hyperplane where the given dimension
9042 * has a fixed value, then return that value.
9043 * Otherwise return NaN.
9045 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9046 __isl_keep isl_basic_map *bmap,
9047 enum isl_dim_type type, unsigned pos)
9049 isl_ctx *ctx;
9050 isl_val *v;
9051 int fixed;
9053 if (!bmap)
9054 return NULL;
9055 ctx = isl_basic_map_get_ctx(bmap);
9056 v = isl_val_alloc(ctx);
9057 if (!v)
9058 return NULL;
9059 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9060 if (fixed < 0)
9061 return isl_val_free(v);
9062 if (fixed) {
9063 isl_int_set_si(v->d, 1);
9064 return v;
9066 isl_val_free(v);
9067 return isl_val_nan(ctx);
9070 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
9071 enum isl_dim_type type, unsigned pos, isl_int *val)
9073 if (pos >= isl_map_dim(map, type))
9074 return -1;
9075 return isl_map_plain_has_fixed_var(map,
9076 map_offset(map, type) - 1 + pos, val);
9079 /* If "map" obviously lies on a hyperplane where the given dimension
9080 * has a fixed value, then return that value.
9081 * Otherwise return NaN.
9083 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9084 enum isl_dim_type type, unsigned pos)
9086 isl_ctx *ctx;
9087 isl_val *v;
9088 int fixed;
9090 if (!map)
9091 return NULL;
9092 ctx = isl_map_get_ctx(map);
9093 v = isl_val_alloc(ctx);
9094 if (!v)
9095 return NULL;
9096 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9097 if (fixed < 0)
9098 return isl_val_free(v);
9099 if (fixed) {
9100 isl_int_set_si(v->d, 1);
9101 return v;
9103 isl_val_free(v);
9104 return isl_val_nan(ctx);
9107 /* If "set" obviously lies on a hyperplane where the given dimension
9108 * has a fixed value, then return that value.
9109 * Otherwise return NaN.
9111 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9112 enum isl_dim_type type, unsigned pos)
9114 return isl_map_plain_get_val_if_fixed(set, type, pos);
9117 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
9118 enum isl_dim_type type, unsigned pos, isl_int *val)
9120 return isl_map_plain_is_fixed(set, type, pos, val);
9123 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9124 * then return this fixed value in *val.
9126 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9127 unsigned dim, isl_int *val)
9129 return isl_basic_set_plain_has_fixed_var(bset,
9130 isl_basic_set_n_param(bset) + dim, val);
9133 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9134 * then return this fixed value in *val.
9136 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
9137 unsigned dim, isl_int *val)
9139 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
9142 /* Check if input variable in has fixed value and if so and if val is not NULL,
9143 * then return this fixed value in *val.
9145 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
9146 unsigned in, isl_int *val)
9148 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
9151 /* Check if dimension dim has an (obvious) fixed lower bound and if so
9152 * and if val is not NULL, then return this lower bound in *val.
9154 int isl_basic_set_plain_dim_has_fixed_lower_bound(
9155 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
9157 int i, i_eq = -1, i_ineq = -1;
9158 isl_int *c;
9159 unsigned total;
9160 unsigned nparam;
9162 if (!bset)
9163 return -1;
9164 total = isl_basic_set_total_dim(bset);
9165 nparam = isl_basic_set_n_param(bset);
9166 for (i = 0; i < bset->n_eq; ++i) {
9167 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
9168 continue;
9169 if (i_eq != -1)
9170 return 0;
9171 i_eq = i;
9173 for (i = 0; i < bset->n_ineq; ++i) {
9174 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
9175 continue;
9176 if (i_eq != -1 || i_ineq != -1)
9177 return 0;
9178 i_ineq = i;
9180 if (i_eq == -1 && i_ineq == -1)
9181 return 0;
9182 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
9183 /* The coefficient should always be one due to normalization. */
9184 if (!isl_int_is_one(c[1+nparam+dim]))
9185 return 0;
9186 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
9187 return 0;
9188 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
9189 total - nparam - dim - 1) != -1)
9190 return 0;
9191 if (val)
9192 isl_int_neg(*val, c[0]);
9193 return 1;
9196 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9197 unsigned dim, isl_int *val)
9199 int i;
9200 isl_int v;
9201 isl_int tmp;
9202 int fixed;
9204 if (!set)
9205 return -1;
9206 if (set->n == 0)
9207 return 0;
9208 if (set->n == 1)
9209 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9210 dim, val);
9211 isl_int_init(v);
9212 isl_int_init(tmp);
9213 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9214 dim, &v);
9215 for (i = 1; fixed == 1 && i < set->n; ++i) {
9216 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9217 dim, &tmp);
9218 if (fixed == 1 && isl_int_ne(tmp, v))
9219 fixed = 0;
9221 if (val)
9222 isl_int_set(*val, v);
9223 isl_int_clear(tmp);
9224 isl_int_clear(v);
9225 return fixed;
9228 /* Return -1 if the constraint "c1" should be sorted before "c2"
9229 * and 1 if it should be sorted after "c2".
9230 * Return 0 if the two constraints are the same (up to the constant term).
9232 * In particular, if a constraint involves later variables than another
9233 * then it is sorted after this other constraint.
9234 * uset_gist depends on constraints without existentially quantified
9235 * variables sorting first.
9237 * For constraints that have the same latest variable, those
9238 * with the same coefficient for this latest variable (first in absolute value
9239 * and then in actual value) are grouped together.
9240 * This is useful for detecting pairs of constraints that can
9241 * be chained in their printed representation.
9243 * Finally, within a group, constraints are sorted according to
9244 * their coefficients (excluding the constant term).
9246 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9248 isl_int **c1 = (isl_int **) p1;
9249 isl_int **c2 = (isl_int **) p2;
9250 int l1, l2;
9251 unsigned size = *(unsigned *) arg;
9252 int cmp;
9254 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9255 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9257 if (l1 != l2)
9258 return l1 - l2;
9260 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9261 if (cmp != 0)
9262 return cmp;
9263 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9264 if (cmp != 0)
9265 return -cmp;
9267 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9270 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9271 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9272 * and 0 if the two constraints are the same (up to the constant term).
9274 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9275 isl_int *c1, isl_int *c2)
9277 unsigned total;
9279 if (!bmap)
9280 return -2;
9281 total = isl_basic_map_total_dim(bmap);
9282 return sort_constraint_cmp(&c1, &c2, &total);
9285 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9286 __isl_take isl_basic_map *bmap)
9288 unsigned total;
9290 if (!bmap)
9291 return NULL;
9292 if (bmap->n_ineq == 0)
9293 return bmap;
9294 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9295 return bmap;
9296 total = isl_basic_map_total_dim(bmap);
9297 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9298 &sort_constraint_cmp, &total) < 0)
9299 return isl_basic_map_free(bmap);
9300 return bmap;
9303 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9304 __isl_take isl_basic_set *bset)
9306 isl_basic_map *bmap = bset_to_bmap(bset);
9307 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9310 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9312 if (!bmap)
9313 return NULL;
9314 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9315 return bmap;
9316 bmap = isl_basic_map_remove_redundancies(bmap);
9317 bmap = isl_basic_map_sort_constraints(bmap);
9318 if (bmap)
9319 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9320 return bmap;
9323 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9325 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9328 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9329 const __isl_keep isl_basic_map *bmap2)
9331 int i, cmp;
9332 unsigned total;
9334 if (!bmap1 || !bmap2)
9335 return -1;
9337 if (bmap1 == bmap2)
9338 return 0;
9339 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9340 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9341 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9342 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9343 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9344 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9345 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9346 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9347 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9348 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9349 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9350 return 0;
9351 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9352 return 1;
9353 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9354 return -1;
9355 if (bmap1->n_eq != bmap2->n_eq)
9356 return bmap1->n_eq - bmap2->n_eq;
9357 if (bmap1->n_ineq != bmap2->n_ineq)
9358 return bmap1->n_ineq - bmap2->n_ineq;
9359 if (bmap1->n_div != bmap2->n_div)
9360 return bmap1->n_div - bmap2->n_div;
9361 total = isl_basic_map_total_dim(bmap1);
9362 for (i = 0; i < bmap1->n_eq; ++i) {
9363 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9364 if (cmp)
9365 return cmp;
9367 for (i = 0; i < bmap1->n_ineq; ++i) {
9368 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9369 if (cmp)
9370 return cmp;
9372 for (i = 0; i < bmap1->n_div; ++i) {
9373 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9374 if (cmp)
9375 return cmp;
9377 return 0;
9380 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9381 const __isl_keep isl_basic_set *bset2)
9383 return isl_basic_map_plain_cmp(bset1, bset2);
9386 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9388 int i, cmp;
9390 if (set1 == set2)
9391 return 0;
9392 if (set1->n != set2->n)
9393 return set1->n - set2->n;
9395 for (i = 0; i < set1->n; ++i) {
9396 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9397 if (cmp)
9398 return cmp;
9401 return 0;
9404 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9405 __isl_keep isl_basic_map *bmap2)
9407 if (!bmap1 || !bmap2)
9408 return isl_bool_error;
9409 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9412 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9413 __isl_keep isl_basic_set *bset2)
9415 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9416 bset_to_bmap(bset2));
9419 static int qsort_bmap_cmp(const void *p1, const void *p2)
9421 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9422 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9424 return isl_basic_map_plain_cmp(bmap1, bmap2);
9427 /* Sort the basic maps of "map" and remove duplicate basic maps.
9429 * While removing basic maps, we make sure that the basic maps remain
9430 * sorted because isl_map_normalize expects the basic maps of the result
9431 * to be sorted.
9433 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9435 int i, j;
9437 map = isl_map_remove_empty_parts(map);
9438 if (!map)
9439 return NULL;
9440 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9441 for (i = map->n - 1; i >= 1; --i) {
9442 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9443 continue;
9444 isl_basic_map_free(map->p[i-1]);
9445 for (j = i; j < map->n; ++j)
9446 map->p[j - 1] = map->p[j];
9447 map->n--;
9450 return map;
9453 /* Remove obvious duplicates among the basic maps of "map".
9455 * Unlike isl_map_normalize, this function does not remove redundant
9456 * constraints and only removes duplicates that have exactly the same
9457 * constraints in the input. It does sort the constraints and
9458 * the basic maps to ease the detection of duplicates.
9460 * If "map" has already been normalized or if the basic maps are
9461 * disjoint, then there can be no duplicates.
9463 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9465 int i;
9466 isl_basic_map *bmap;
9468 if (!map)
9469 return NULL;
9470 if (map->n <= 1)
9471 return map;
9472 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9473 return map;
9474 for (i = 0; i < map->n; ++i) {
9475 bmap = isl_basic_map_copy(map->p[i]);
9476 bmap = isl_basic_map_sort_constraints(bmap);
9477 if (!bmap)
9478 return isl_map_free(map);
9479 isl_basic_map_free(map->p[i]);
9480 map->p[i] = bmap;
9483 map = sort_and_remove_duplicates(map);
9484 return map;
9487 /* We normalize in place, but if anything goes wrong we need
9488 * to return NULL, so we need to make sure we don't change the
9489 * meaning of any possible other copies of map.
9491 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9493 int i;
9494 struct isl_basic_map *bmap;
9496 if (!map)
9497 return NULL;
9498 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9499 return map;
9500 for (i = 0; i < map->n; ++i) {
9501 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9502 if (!bmap)
9503 goto error;
9504 isl_basic_map_free(map->p[i]);
9505 map->p[i] = bmap;
9508 map = sort_and_remove_duplicates(map);
9509 if (map)
9510 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9511 return map;
9512 error:
9513 isl_map_free(map);
9514 return NULL;
9517 struct isl_set *isl_set_normalize(struct isl_set *set)
9519 return set_from_map(isl_map_normalize(set_to_map(set)));
9522 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9523 __isl_keep isl_map *map2)
9525 int i;
9526 isl_bool equal;
9528 if (!map1 || !map2)
9529 return isl_bool_error;
9531 if (map1 == map2)
9532 return isl_bool_true;
9533 if (!isl_space_is_equal(map1->dim, map2->dim))
9534 return isl_bool_false;
9536 map1 = isl_map_copy(map1);
9537 map2 = isl_map_copy(map2);
9538 map1 = isl_map_normalize(map1);
9539 map2 = isl_map_normalize(map2);
9540 if (!map1 || !map2)
9541 goto error;
9542 equal = map1->n == map2->n;
9543 for (i = 0; equal && i < map1->n; ++i) {
9544 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9545 if (equal < 0)
9546 goto error;
9548 isl_map_free(map1);
9549 isl_map_free(map2);
9550 return equal;
9551 error:
9552 isl_map_free(map1);
9553 isl_map_free(map2);
9554 return isl_bool_error;
9557 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9558 __isl_keep isl_set *set2)
9560 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9563 /* Return an interval that ranges from min to max (inclusive)
9565 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9566 isl_int min, isl_int max)
9568 int k;
9569 struct isl_basic_set *bset = NULL;
9571 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9572 if (!bset)
9573 goto error;
9575 k = isl_basic_set_alloc_inequality(bset);
9576 if (k < 0)
9577 goto error;
9578 isl_int_set_si(bset->ineq[k][1], 1);
9579 isl_int_neg(bset->ineq[k][0], min);
9581 k = isl_basic_set_alloc_inequality(bset);
9582 if (k < 0)
9583 goto error;
9584 isl_int_set_si(bset->ineq[k][1], -1);
9585 isl_int_set(bset->ineq[k][0], max);
9587 return bset;
9588 error:
9589 isl_basic_set_free(bset);
9590 return NULL;
9593 /* Return the basic maps in "map" as a list.
9595 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9596 __isl_keep isl_map *map)
9598 int i;
9599 isl_ctx *ctx;
9600 isl_basic_map_list *list;
9602 if (!map)
9603 return NULL;
9604 ctx = isl_map_get_ctx(map);
9605 list = isl_basic_map_list_alloc(ctx, map->n);
9607 for (i = 0; i < map->n; ++i) {
9608 isl_basic_map *bmap;
9610 bmap = isl_basic_map_copy(map->p[i]);
9611 list = isl_basic_map_list_add(list, bmap);
9614 return list;
9617 /* Return the intersection of the elements in the non-empty list "list".
9618 * All elements are assumed to live in the same space.
9620 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9621 __isl_take isl_basic_map_list *list)
9623 int i, n;
9624 isl_basic_map *bmap;
9626 if (!list)
9627 return NULL;
9628 n = isl_basic_map_list_n_basic_map(list);
9629 if (n < 1)
9630 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9631 "expecting non-empty list", goto error);
9633 bmap = isl_basic_map_list_get_basic_map(list, 0);
9634 for (i = 1; i < n; ++i) {
9635 isl_basic_map *bmap_i;
9637 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9638 bmap = isl_basic_map_intersect(bmap, bmap_i);
9641 isl_basic_map_list_free(list);
9642 return bmap;
9643 error:
9644 isl_basic_map_list_free(list);
9645 return NULL;
9648 /* Return the intersection of the elements in the non-empty list "list".
9649 * All elements are assumed to live in the same space.
9651 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9652 __isl_take isl_basic_set_list *list)
9654 return isl_basic_map_list_intersect(list);
9657 /* Return the union of the elements of "list".
9658 * The list is required to have at least one element.
9660 __isl_give isl_set *isl_basic_set_list_union(
9661 __isl_take isl_basic_set_list *list)
9663 int i, n;
9664 isl_space *space;
9665 isl_basic_set *bset;
9666 isl_set *set;
9668 if (!list)
9669 return NULL;
9670 n = isl_basic_set_list_n_basic_set(list);
9671 if (n < 1)
9672 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9673 "expecting non-empty list", goto error);
9675 bset = isl_basic_set_list_get_basic_set(list, 0);
9676 space = isl_basic_set_get_space(bset);
9677 isl_basic_set_free(bset);
9679 set = isl_set_alloc_space(space, n, 0);
9680 for (i = 0; i < n; ++i) {
9681 bset = isl_basic_set_list_get_basic_set(list, i);
9682 set = isl_set_add_basic_set(set, bset);
9685 isl_basic_set_list_free(list);
9686 return set;
9687 error:
9688 isl_basic_set_list_free(list);
9689 return NULL;
9692 /* Return the union of the elements in the non-empty list "list".
9693 * All elements are assumed to live in the same space.
9695 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9697 int i, n;
9698 isl_set *set;
9700 if (!list)
9701 return NULL;
9702 n = isl_set_list_n_set(list);
9703 if (n < 1)
9704 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9705 "expecting non-empty list", goto error);
9707 set = isl_set_list_get_set(list, 0);
9708 for (i = 1; i < n; ++i) {
9709 isl_set *set_i;
9711 set_i = isl_set_list_get_set(list, i);
9712 set = isl_set_union(set, set_i);
9715 isl_set_list_free(list);
9716 return set;
9717 error:
9718 isl_set_list_free(list);
9719 return NULL;
9722 /* Return the Cartesian product of the basic sets in list (in the given order).
9724 __isl_give isl_basic_set *isl_basic_set_list_product(
9725 __isl_take struct isl_basic_set_list *list)
9727 int i;
9728 unsigned dim;
9729 unsigned nparam;
9730 unsigned extra;
9731 unsigned n_eq;
9732 unsigned n_ineq;
9733 struct isl_basic_set *product = NULL;
9735 if (!list)
9736 goto error;
9737 isl_assert(list->ctx, list->n > 0, goto error);
9738 isl_assert(list->ctx, list->p[0], goto error);
9739 nparam = isl_basic_set_n_param(list->p[0]);
9740 dim = isl_basic_set_n_dim(list->p[0]);
9741 extra = list->p[0]->n_div;
9742 n_eq = list->p[0]->n_eq;
9743 n_ineq = list->p[0]->n_ineq;
9744 for (i = 1; i < list->n; ++i) {
9745 isl_assert(list->ctx, list->p[i], goto error);
9746 isl_assert(list->ctx,
9747 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9748 dim += isl_basic_set_n_dim(list->p[i]);
9749 extra += list->p[i]->n_div;
9750 n_eq += list->p[i]->n_eq;
9751 n_ineq += list->p[i]->n_ineq;
9753 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9754 n_eq, n_ineq);
9755 if (!product)
9756 goto error;
9757 dim = 0;
9758 for (i = 0; i < list->n; ++i) {
9759 isl_basic_set_add_constraints(product,
9760 isl_basic_set_copy(list->p[i]), dim);
9761 dim += isl_basic_set_n_dim(list->p[i]);
9763 isl_basic_set_list_free(list);
9764 return product;
9765 error:
9766 isl_basic_set_free(product);
9767 isl_basic_set_list_free(list);
9768 return NULL;
9771 struct isl_basic_map *isl_basic_map_product(
9772 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9774 isl_space *dim_result = NULL;
9775 struct isl_basic_map *bmap;
9776 unsigned in1, in2, out1, out2, nparam, total, pos;
9777 struct isl_dim_map *dim_map1, *dim_map2;
9779 if (!bmap1 || !bmap2)
9780 goto error;
9782 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9783 bmap2->dim, isl_dim_param), goto error);
9784 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9785 isl_space_copy(bmap2->dim));
9787 in1 = isl_basic_map_n_in(bmap1);
9788 in2 = isl_basic_map_n_in(bmap2);
9789 out1 = isl_basic_map_n_out(bmap1);
9790 out2 = isl_basic_map_n_out(bmap2);
9791 nparam = isl_basic_map_n_param(bmap1);
9793 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9794 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9795 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9796 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9797 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9798 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9799 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9800 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9801 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9802 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9803 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9805 bmap = isl_basic_map_alloc_space(dim_result,
9806 bmap1->n_div + bmap2->n_div,
9807 bmap1->n_eq + bmap2->n_eq,
9808 bmap1->n_ineq + bmap2->n_ineq);
9809 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9810 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9811 bmap = isl_basic_map_simplify(bmap);
9812 return isl_basic_map_finalize(bmap);
9813 error:
9814 isl_basic_map_free(bmap1);
9815 isl_basic_map_free(bmap2);
9816 return NULL;
9819 __isl_give isl_basic_map *isl_basic_map_flat_product(
9820 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9822 isl_basic_map *prod;
9824 prod = isl_basic_map_product(bmap1, bmap2);
9825 prod = isl_basic_map_flatten(prod);
9826 return prod;
9829 __isl_give isl_basic_set *isl_basic_set_flat_product(
9830 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9832 return isl_basic_map_flat_range_product(bset1, bset2);
9835 __isl_give isl_basic_map *isl_basic_map_domain_product(
9836 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9838 isl_space *space_result = NULL;
9839 isl_basic_map *bmap;
9840 unsigned in1, in2, out, nparam, total, pos;
9841 struct isl_dim_map *dim_map1, *dim_map2;
9843 if (!bmap1 || !bmap2)
9844 goto error;
9846 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9847 isl_space_copy(bmap2->dim));
9849 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9850 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9851 out = isl_basic_map_dim(bmap1, isl_dim_out);
9852 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9854 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9855 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9856 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9857 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9858 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9859 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9860 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9861 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9862 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9863 isl_dim_map_div(dim_map1, bmap1, pos += out);
9864 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9866 bmap = isl_basic_map_alloc_space(space_result,
9867 bmap1->n_div + bmap2->n_div,
9868 bmap1->n_eq + bmap2->n_eq,
9869 bmap1->n_ineq + bmap2->n_ineq);
9870 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9871 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9872 bmap = isl_basic_map_simplify(bmap);
9873 return isl_basic_map_finalize(bmap);
9874 error:
9875 isl_basic_map_free(bmap1);
9876 isl_basic_map_free(bmap2);
9877 return NULL;
9880 __isl_give isl_basic_map *isl_basic_map_range_product(
9881 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9883 int rational;
9884 isl_space *dim_result = NULL;
9885 isl_basic_map *bmap;
9886 unsigned in, out1, out2, nparam, total, pos;
9887 struct isl_dim_map *dim_map1, *dim_map2;
9889 rational = isl_basic_map_is_rational(bmap1);
9890 if (rational >= 0 && rational)
9891 rational = isl_basic_map_is_rational(bmap2);
9892 if (!bmap1 || !bmap2 || rational < 0)
9893 goto error;
9895 if (!isl_space_match(bmap1->dim, isl_dim_param,
9896 bmap2->dim, isl_dim_param))
9897 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9898 "parameters don't match", goto error);
9900 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9901 isl_space_copy(bmap2->dim));
9903 in = isl_basic_map_dim(bmap1, isl_dim_in);
9904 out1 = isl_basic_map_n_out(bmap1);
9905 out2 = isl_basic_map_n_out(bmap2);
9906 nparam = isl_basic_map_n_param(bmap1);
9908 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9909 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9910 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9911 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9912 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9913 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9914 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9915 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9916 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9917 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9918 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9920 bmap = isl_basic_map_alloc_space(dim_result,
9921 bmap1->n_div + bmap2->n_div,
9922 bmap1->n_eq + bmap2->n_eq,
9923 bmap1->n_ineq + bmap2->n_ineq);
9924 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9925 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9926 if (rational)
9927 bmap = isl_basic_map_set_rational(bmap);
9928 bmap = isl_basic_map_simplify(bmap);
9929 return isl_basic_map_finalize(bmap);
9930 error:
9931 isl_basic_map_free(bmap1);
9932 isl_basic_map_free(bmap2);
9933 return NULL;
9936 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9937 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9939 isl_basic_map *prod;
9941 prod = isl_basic_map_range_product(bmap1, bmap2);
9942 prod = isl_basic_map_flatten_range(prod);
9943 return prod;
9946 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9947 * and collect the results.
9948 * The result live in the space obtained by calling "space_product"
9949 * on the spaces of "map1" and "map2".
9950 * If "remove_duplicates" is set then the result may contain duplicates
9951 * (even if the inputs do not) and so we try and remove the obvious
9952 * duplicates.
9954 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9955 __isl_take isl_map *map2,
9956 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9957 __isl_take isl_space *right),
9958 __isl_give isl_basic_map *(*basic_map_product)(
9959 __isl_take isl_basic_map *left,
9960 __isl_take isl_basic_map *right),
9961 int remove_duplicates)
9963 unsigned flags = 0;
9964 struct isl_map *result;
9965 int i, j;
9967 if (!map1 || !map2)
9968 goto error;
9970 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9971 map2->dim, isl_dim_param), goto error);
9973 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9974 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9975 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9977 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9978 isl_space_copy(map2->dim)),
9979 map1->n * map2->n, flags);
9980 if (!result)
9981 goto error;
9982 for (i = 0; i < map1->n; ++i)
9983 for (j = 0; j < map2->n; ++j) {
9984 struct isl_basic_map *part;
9985 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9986 isl_basic_map_copy(map2->p[j]));
9987 if (isl_basic_map_is_empty(part))
9988 isl_basic_map_free(part);
9989 else
9990 result = isl_map_add_basic_map(result, part);
9991 if (!result)
9992 goto error;
9994 if (remove_duplicates)
9995 result = isl_map_remove_obvious_duplicates(result);
9996 isl_map_free(map1);
9997 isl_map_free(map2);
9998 return result;
9999 error:
10000 isl_map_free(map1);
10001 isl_map_free(map2);
10002 return NULL;
10005 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10007 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10008 __isl_take isl_map *map2)
10010 return map_product(map1, map2, &isl_space_product,
10011 &isl_basic_map_product, 0);
10014 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10015 __isl_take isl_map *map2)
10017 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10020 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10022 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10023 __isl_take isl_map *map2)
10025 isl_map *prod;
10027 prod = isl_map_product(map1, map2);
10028 prod = isl_map_flatten(prod);
10029 return prod;
10032 /* Given two set A and B, construct its Cartesian product A x B.
10034 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10036 return isl_map_range_product(set1, set2);
10039 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10040 __isl_take isl_set *set2)
10042 return isl_map_flat_range_product(set1, set2);
10045 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10047 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10048 __isl_take isl_map *map2)
10050 return map_product(map1, map2, &isl_space_domain_product,
10051 &isl_basic_map_domain_product, 1);
10054 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10056 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10057 __isl_take isl_map *map2)
10059 return map_product(map1, map2, &isl_space_range_product,
10060 &isl_basic_map_range_product, 1);
10063 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10064 __isl_take isl_map *map2)
10066 return isl_map_align_params_map_map_and(map1, map2,
10067 &map_domain_product_aligned);
10070 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10071 __isl_take isl_map *map2)
10073 return isl_map_align_params_map_map_and(map1, map2,
10074 &map_range_product_aligned);
10077 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10079 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10081 isl_space *space;
10082 int total1, keep1, total2, keep2;
10084 if (!map)
10085 return NULL;
10086 if (!isl_space_domain_is_wrapping(map->dim) ||
10087 !isl_space_range_is_wrapping(map->dim))
10088 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10089 "not a product", return isl_map_free(map));
10091 space = isl_map_get_space(map);
10092 total1 = isl_space_dim(space, isl_dim_in);
10093 total2 = isl_space_dim(space, isl_dim_out);
10094 space = isl_space_factor_domain(space);
10095 keep1 = isl_space_dim(space, isl_dim_in);
10096 keep2 = isl_space_dim(space, isl_dim_out);
10097 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10098 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10099 map = isl_map_reset_space(map, space);
10101 return map;
10104 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10106 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10108 isl_space *space;
10109 int total1, keep1, total2, keep2;
10111 if (!map)
10112 return NULL;
10113 if (!isl_space_domain_is_wrapping(map->dim) ||
10114 !isl_space_range_is_wrapping(map->dim))
10115 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10116 "not a product", return isl_map_free(map));
10118 space = isl_map_get_space(map);
10119 total1 = isl_space_dim(space, isl_dim_in);
10120 total2 = isl_space_dim(space, isl_dim_out);
10121 space = isl_space_factor_range(space);
10122 keep1 = isl_space_dim(space, isl_dim_in);
10123 keep2 = isl_space_dim(space, isl_dim_out);
10124 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10125 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10126 map = isl_map_reset_space(map, space);
10128 return map;
10131 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10133 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10135 isl_space *space;
10136 int total, keep;
10138 if (!map)
10139 return NULL;
10140 if (!isl_space_domain_is_wrapping(map->dim))
10141 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10142 "domain is not a product", return isl_map_free(map));
10144 space = isl_map_get_space(map);
10145 total = isl_space_dim(space, isl_dim_in);
10146 space = isl_space_domain_factor_domain(space);
10147 keep = isl_space_dim(space, isl_dim_in);
10148 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10149 map = isl_map_reset_space(map, space);
10151 return map;
10154 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10156 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10158 isl_space *space;
10159 int total, keep;
10161 if (!map)
10162 return NULL;
10163 if (!isl_space_domain_is_wrapping(map->dim))
10164 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10165 "domain is not a product", return isl_map_free(map));
10167 space = isl_map_get_space(map);
10168 total = isl_space_dim(space, isl_dim_in);
10169 space = isl_space_domain_factor_range(space);
10170 keep = isl_space_dim(space, isl_dim_in);
10171 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10172 map = isl_map_reset_space(map, space);
10174 return map;
10177 /* Given a map A -> [B -> C], extract the map A -> B.
10179 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10181 isl_space *space;
10182 int total, keep;
10184 if (!map)
10185 return NULL;
10186 if (!isl_space_range_is_wrapping(map->dim))
10187 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10188 "range is not a product", return isl_map_free(map));
10190 space = isl_map_get_space(map);
10191 total = isl_space_dim(space, isl_dim_out);
10192 space = isl_space_range_factor_domain(space);
10193 keep = isl_space_dim(space, isl_dim_out);
10194 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10195 map = isl_map_reset_space(map, space);
10197 return map;
10200 /* Given a map A -> [B -> C], extract the map A -> C.
10202 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10204 isl_space *space;
10205 int total, keep;
10207 if (!map)
10208 return NULL;
10209 if (!isl_space_range_is_wrapping(map->dim))
10210 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10211 "range is not a product", return isl_map_free(map));
10213 space = isl_map_get_space(map);
10214 total = isl_space_dim(space, isl_dim_out);
10215 space = isl_space_range_factor_range(space);
10216 keep = isl_space_dim(space, isl_dim_out);
10217 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10218 map = isl_map_reset_space(map, space);
10220 return map;
10223 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10225 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10226 __isl_take isl_map *map2)
10228 isl_map *prod;
10230 prod = isl_map_domain_product(map1, map2);
10231 prod = isl_map_flatten_domain(prod);
10232 return prod;
10235 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10237 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10238 __isl_take isl_map *map2)
10240 isl_map *prod;
10242 prod = isl_map_range_product(map1, map2);
10243 prod = isl_map_flatten_range(prod);
10244 return prod;
10247 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10249 int i;
10250 uint32_t hash = isl_hash_init();
10251 unsigned total;
10253 if (!bmap)
10254 return 0;
10255 bmap = isl_basic_map_copy(bmap);
10256 bmap = isl_basic_map_normalize(bmap);
10257 if (!bmap)
10258 return 0;
10259 total = isl_basic_map_total_dim(bmap);
10260 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10261 for (i = 0; i < bmap->n_eq; ++i) {
10262 uint32_t c_hash;
10263 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10264 isl_hash_hash(hash, c_hash);
10266 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10267 for (i = 0; i < bmap->n_ineq; ++i) {
10268 uint32_t c_hash;
10269 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10270 isl_hash_hash(hash, c_hash);
10272 isl_hash_byte(hash, bmap->n_div & 0xFF);
10273 for (i = 0; i < bmap->n_div; ++i) {
10274 uint32_t c_hash;
10275 if (isl_int_is_zero(bmap->div[i][0]))
10276 continue;
10277 isl_hash_byte(hash, i & 0xFF);
10278 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10279 isl_hash_hash(hash, c_hash);
10281 isl_basic_map_free(bmap);
10282 return hash;
10285 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10287 return isl_basic_map_get_hash(bset_to_bmap(bset));
10290 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10292 int i;
10293 uint32_t hash;
10295 if (!map)
10296 return 0;
10297 map = isl_map_copy(map);
10298 map = isl_map_normalize(map);
10299 if (!map)
10300 return 0;
10302 hash = isl_hash_init();
10303 for (i = 0; i < map->n; ++i) {
10304 uint32_t bmap_hash;
10305 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10306 isl_hash_hash(hash, bmap_hash);
10309 isl_map_free(map);
10311 return hash;
10314 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10316 return isl_map_get_hash(set_to_map(set));
10319 /* Check if the value for dimension dim is completely determined
10320 * by the values of the other parameters and variables.
10321 * That is, check if dimension dim is involved in an equality.
10323 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10325 int i;
10326 unsigned nparam;
10328 if (!bset)
10329 return -1;
10330 nparam = isl_basic_set_n_param(bset);
10331 for (i = 0; i < bset->n_eq; ++i)
10332 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10333 return 1;
10334 return 0;
10337 /* Check if the value for dimension dim is completely determined
10338 * by the values of the other parameters and variables.
10339 * That is, check if dimension dim is involved in an equality
10340 * for each of the subsets.
10342 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10344 int i;
10346 if (!set)
10347 return -1;
10348 for (i = 0; i < set->n; ++i) {
10349 int unique;
10350 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10351 if (unique != 1)
10352 return unique;
10354 return 1;
10357 /* Return the number of basic maps in the (current) representation of "map".
10359 int isl_map_n_basic_map(__isl_keep isl_map *map)
10361 return map ? map->n : 0;
10364 int isl_set_n_basic_set(__isl_keep isl_set *set)
10366 return set ? set->n : 0;
10369 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10370 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10372 int i;
10374 if (!map)
10375 return isl_stat_error;
10377 for (i = 0; i < map->n; ++i)
10378 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10379 return isl_stat_error;
10381 return isl_stat_ok;
10384 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10385 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10387 int i;
10389 if (!set)
10390 return isl_stat_error;
10392 for (i = 0; i < set->n; ++i)
10393 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10394 return isl_stat_error;
10396 return isl_stat_ok;
10399 /* Return a list of basic sets, the union of which is equal to "set".
10401 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10402 __isl_keep isl_set *set)
10404 int i;
10405 isl_basic_set_list *list;
10407 if (!set)
10408 return NULL;
10410 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10411 for (i = 0; i < set->n; ++i) {
10412 isl_basic_set *bset;
10414 bset = isl_basic_set_copy(set->p[i]);
10415 list = isl_basic_set_list_add(list, bset);
10418 return list;
10421 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10423 isl_space *dim;
10425 if (!bset)
10426 return NULL;
10428 bset = isl_basic_set_cow(bset);
10429 if (!bset)
10430 return NULL;
10432 dim = isl_basic_set_get_space(bset);
10433 dim = isl_space_lift(dim, bset->n_div);
10434 if (!dim)
10435 goto error;
10436 isl_space_free(bset->dim);
10437 bset->dim = dim;
10438 bset->extra -= bset->n_div;
10439 bset->n_div = 0;
10441 bset = isl_basic_set_finalize(bset);
10443 return bset;
10444 error:
10445 isl_basic_set_free(bset);
10446 return NULL;
10449 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10451 int i;
10452 isl_space *dim;
10453 unsigned n_div;
10455 set = isl_set_align_divs(set);
10457 if (!set)
10458 return NULL;
10460 set = isl_set_cow(set);
10461 if (!set)
10462 return NULL;
10464 n_div = set->p[0]->n_div;
10465 dim = isl_set_get_space(set);
10466 dim = isl_space_lift(dim, n_div);
10467 if (!dim)
10468 goto error;
10469 isl_space_free(set->dim);
10470 set->dim = dim;
10472 for (i = 0; i < set->n; ++i) {
10473 set->p[i] = isl_basic_set_lift(set->p[i]);
10474 if (!set->p[i])
10475 goto error;
10478 return set;
10479 error:
10480 isl_set_free(set);
10481 return NULL;
10484 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10486 isl_space *dim;
10487 struct isl_basic_map *bmap;
10488 unsigned n_set;
10489 unsigned n_div;
10490 unsigned n_param;
10491 unsigned total;
10492 int i, k, l;
10494 set = isl_set_align_divs(set);
10496 if (!set)
10497 return NULL;
10499 dim = isl_set_get_space(set);
10500 if (set->n == 0 || set->p[0]->n_div == 0) {
10501 isl_set_free(set);
10502 return isl_map_identity(isl_space_map_from_set(dim));
10505 n_div = set->p[0]->n_div;
10506 dim = isl_space_map_from_set(dim);
10507 n_param = isl_space_dim(dim, isl_dim_param);
10508 n_set = isl_space_dim(dim, isl_dim_in);
10509 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10510 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10511 for (i = 0; i < n_set; ++i)
10512 bmap = var_equal(bmap, i);
10514 total = n_param + n_set + n_set + n_div;
10515 for (i = 0; i < n_div; ++i) {
10516 k = isl_basic_map_alloc_inequality(bmap);
10517 if (k < 0)
10518 goto error;
10519 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10520 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10521 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10522 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10523 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10524 set->p[0]->div[i][0]);
10526 l = isl_basic_map_alloc_inequality(bmap);
10527 if (l < 0)
10528 goto error;
10529 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10530 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10531 set->p[0]->div[i][0]);
10532 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10535 isl_set_free(set);
10536 bmap = isl_basic_map_simplify(bmap);
10537 bmap = isl_basic_map_finalize(bmap);
10538 return isl_map_from_basic_map(bmap);
10539 error:
10540 isl_set_free(set);
10541 isl_basic_map_free(bmap);
10542 return NULL;
10545 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10547 unsigned dim;
10548 int size = 0;
10550 if (!bset)
10551 return -1;
10553 dim = isl_basic_set_total_dim(bset);
10554 size += bset->n_eq * (1 + dim);
10555 size += bset->n_ineq * (1 + dim);
10556 size += bset->n_div * (2 + dim);
10558 return size;
10561 int isl_set_size(__isl_keep isl_set *set)
10563 int i;
10564 int size = 0;
10566 if (!set)
10567 return -1;
10569 for (i = 0; i < set->n; ++i)
10570 size += isl_basic_set_size(set->p[i]);
10572 return size;
10575 /* Check if there is any lower bound (if lower == 0) and/or upper
10576 * bound (if upper == 0) on the specified dim.
10578 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10579 enum isl_dim_type type, unsigned pos, int lower, int upper)
10581 int i;
10583 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10584 return isl_bool_error;
10586 pos += isl_basic_map_offset(bmap, type);
10588 for (i = 0; i < bmap->n_div; ++i) {
10589 if (isl_int_is_zero(bmap->div[i][0]))
10590 continue;
10591 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10592 return isl_bool_true;
10595 for (i = 0; i < bmap->n_eq; ++i)
10596 if (!isl_int_is_zero(bmap->eq[i][pos]))
10597 return isl_bool_true;
10599 for (i = 0; i < bmap->n_ineq; ++i) {
10600 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10601 if (sgn > 0)
10602 lower = 1;
10603 if (sgn < 0)
10604 upper = 1;
10607 return lower && upper;
10610 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10611 enum isl_dim_type type, unsigned pos)
10613 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10616 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10617 enum isl_dim_type type, unsigned pos)
10619 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10622 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10623 enum isl_dim_type type, unsigned pos)
10625 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10628 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10629 enum isl_dim_type type, unsigned pos)
10631 int i;
10633 if (!map)
10634 return -1;
10636 for (i = 0; i < map->n; ++i) {
10637 int bounded;
10638 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10639 if (bounded < 0 || !bounded)
10640 return bounded;
10643 return 1;
10646 /* Return 1 if the specified dim is involved in both an upper bound
10647 * and a lower bound.
10649 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10650 enum isl_dim_type type, unsigned pos)
10652 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10655 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10657 static isl_bool has_any_bound(__isl_keep isl_map *map,
10658 enum isl_dim_type type, unsigned pos,
10659 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10660 enum isl_dim_type type, unsigned pos))
10662 int i;
10664 if (!map)
10665 return isl_bool_error;
10667 for (i = 0; i < map->n; ++i) {
10668 isl_bool bounded;
10669 bounded = fn(map->p[i], type, pos);
10670 if (bounded < 0 || bounded)
10671 return bounded;
10674 return isl_bool_false;
10677 /* Return 1 if the specified dim is involved in any lower bound.
10679 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10680 enum isl_dim_type type, unsigned pos)
10682 return has_any_bound(set, type, pos,
10683 &isl_basic_map_dim_has_lower_bound);
10686 /* Return 1 if the specified dim is involved in any upper bound.
10688 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10689 enum isl_dim_type type, unsigned pos)
10691 return has_any_bound(set, type, pos,
10692 &isl_basic_map_dim_has_upper_bound);
10695 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10697 static isl_bool has_bound(__isl_keep isl_map *map,
10698 enum isl_dim_type type, unsigned pos,
10699 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10700 enum isl_dim_type type, unsigned pos))
10702 int i;
10704 if (!map)
10705 return isl_bool_error;
10707 for (i = 0; i < map->n; ++i) {
10708 isl_bool bounded;
10709 bounded = fn(map->p[i], type, pos);
10710 if (bounded < 0 || !bounded)
10711 return bounded;
10714 return isl_bool_true;
10717 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10719 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10720 enum isl_dim_type type, unsigned pos)
10722 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10725 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10727 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10728 enum isl_dim_type type, unsigned pos)
10730 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10733 /* For each of the "n" variables starting at "first", determine
10734 * the sign of the variable and put the results in the first "n"
10735 * elements of the array "signs".
10736 * Sign
10737 * 1 means that the variable is non-negative
10738 * -1 means that the variable is non-positive
10739 * 0 means the variable attains both positive and negative values.
10741 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10742 unsigned first, unsigned n, int *signs)
10744 isl_vec *bound = NULL;
10745 struct isl_tab *tab = NULL;
10746 struct isl_tab_undo *snap;
10747 int i;
10749 if (!bset || !signs)
10750 return -1;
10752 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10753 tab = isl_tab_from_basic_set(bset, 0);
10754 if (!bound || !tab)
10755 goto error;
10757 isl_seq_clr(bound->el, bound->size);
10758 isl_int_set_si(bound->el[0], -1);
10760 snap = isl_tab_snap(tab);
10761 for (i = 0; i < n; ++i) {
10762 int empty;
10764 isl_int_set_si(bound->el[1 + first + i], -1);
10765 if (isl_tab_add_ineq(tab, bound->el) < 0)
10766 goto error;
10767 empty = tab->empty;
10768 isl_int_set_si(bound->el[1 + first + i], 0);
10769 if (isl_tab_rollback(tab, snap) < 0)
10770 goto error;
10772 if (empty) {
10773 signs[i] = 1;
10774 continue;
10777 isl_int_set_si(bound->el[1 + first + i], 1);
10778 if (isl_tab_add_ineq(tab, bound->el) < 0)
10779 goto error;
10780 empty = tab->empty;
10781 isl_int_set_si(bound->el[1 + first + i], 0);
10782 if (isl_tab_rollback(tab, snap) < 0)
10783 goto error;
10785 signs[i] = empty ? -1 : 0;
10788 isl_tab_free(tab);
10789 isl_vec_free(bound);
10790 return 0;
10791 error:
10792 isl_tab_free(tab);
10793 isl_vec_free(bound);
10794 return -1;
10797 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10798 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10800 if (!bset || !signs)
10801 return -1;
10802 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10803 return -1);
10805 first += pos(bset->dim, type) - 1;
10806 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10809 /* Is it possible for the integer division "div" to depend (possibly
10810 * indirectly) on any output dimensions?
10812 * If the div is undefined, then we conservatively assume that it
10813 * may depend on them.
10814 * Otherwise, we check if it actually depends on them or on any integer
10815 * divisions that may depend on them.
10817 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10819 int i;
10820 unsigned n_out, o_out;
10821 unsigned n_div, o_div;
10823 if (isl_int_is_zero(bmap->div[div][0]))
10824 return 1;
10826 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10827 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10829 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10830 return 1;
10832 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10833 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10835 for (i = 0; i < n_div; ++i) {
10836 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10837 continue;
10838 if (div_may_involve_output(bmap, i))
10839 return 1;
10842 return 0;
10845 /* Return the first integer division of "bmap" in the range
10846 * [first, first + n[ that may depend on any output dimensions and
10847 * that has a non-zero coefficient in "c" (where the first coefficient
10848 * in "c" corresponds to integer division "first").
10850 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10851 isl_int *c, int first, int n)
10853 int k;
10855 if (!bmap)
10856 return -1;
10858 for (k = first; k < first + n; ++k) {
10859 if (isl_int_is_zero(c[k]))
10860 continue;
10861 if (div_may_involve_output(bmap, k))
10862 return k;
10865 return first + n;
10868 /* Look for a pair of inequality constraints in "bmap" of the form
10870 * -l + i >= 0 or i >= l
10871 * and
10872 * n + l - i >= 0 or i <= l + n
10874 * with n < "m" and i the output dimension at position "pos".
10875 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10876 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10877 * and earlier output dimensions, as well as integer divisions that do
10878 * not involve any of the output dimensions.
10880 * Return the index of the first inequality constraint or bmap->n_ineq
10881 * if no such pair can be found.
10883 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10884 int pos, isl_int m)
10886 int i, j;
10887 isl_ctx *ctx;
10888 unsigned total;
10889 unsigned n_div, o_div;
10890 unsigned n_out, o_out;
10891 int less;
10893 if (!bmap)
10894 return -1;
10896 ctx = isl_basic_map_get_ctx(bmap);
10897 total = isl_basic_map_total_dim(bmap);
10898 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10899 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10900 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10901 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10902 for (i = 0; i < bmap->n_ineq; ++i) {
10903 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10904 continue;
10905 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10906 n_out - (pos + 1)) != -1)
10907 continue;
10908 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10909 0, n_div) < n_div)
10910 continue;
10911 for (j = i + 1; j < bmap->n_ineq; ++j) {
10912 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10913 ctx->one))
10914 continue;
10915 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10916 bmap->ineq[j] + 1, total))
10917 continue;
10918 break;
10920 if (j >= bmap->n_ineq)
10921 continue;
10922 isl_int_add(bmap->ineq[i][0],
10923 bmap->ineq[i][0], bmap->ineq[j][0]);
10924 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10925 isl_int_sub(bmap->ineq[i][0],
10926 bmap->ineq[i][0], bmap->ineq[j][0]);
10927 if (!less)
10928 continue;
10929 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10930 return i;
10931 else
10932 return j;
10935 return bmap->n_ineq;
10938 /* Return the index of the equality of "bmap" that defines
10939 * the output dimension "pos" in terms of earlier dimensions.
10940 * The equality may also involve integer divisions, as long
10941 * as those integer divisions are defined in terms of
10942 * parameters or input dimensions.
10943 * In this case, *div is set to the number of integer divisions and
10944 * *ineq is set to the number of inequality constraints (provided
10945 * div and ineq are not NULL).
10947 * The equality may also involve a single integer division involving
10948 * the output dimensions (typically only output dimension "pos") as
10949 * long as the coefficient of output dimension "pos" is 1 or -1 and
10950 * there is a pair of constraints i >= l and i <= l + n, with i referring
10951 * to output dimension "pos", l an expression involving only earlier
10952 * dimensions and n smaller than the coefficient of the integer division
10953 * in the equality. In this case, the output dimension can be defined
10954 * in terms of a modulo expression that does not involve the integer division.
10955 * *div is then set to this single integer division and
10956 * *ineq is set to the index of constraint i >= l.
10958 * Return bmap->n_eq if there is no such equality.
10959 * Return -1 on error.
10961 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10962 int pos, int *div, int *ineq)
10964 int j, k, l;
10965 unsigned n_out, o_out;
10966 unsigned n_div, o_div;
10968 if (!bmap)
10969 return -1;
10971 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10972 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10973 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10974 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10976 if (ineq)
10977 *ineq = bmap->n_ineq;
10978 if (div)
10979 *div = n_div;
10980 for (j = 0; j < bmap->n_eq; ++j) {
10981 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10982 continue;
10983 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10984 n_out - (pos + 1)) != -1)
10985 continue;
10986 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10987 0, n_div);
10988 if (k >= n_div)
10989 return j;
10990 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10991 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10992 continue;
10993 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10994 k + 1, n_div - (k+1)) < n_div)
10995 continue;
10996 l = find_modulo_constraint_pair(bmap, pos,
10997 bmap->eq[j][o_div + k]);
10998 if (l < 0)
10999 return -1;
11000 if (l >= bmap->n_ineq)
11001 continue;
11002 if (div)
11003 *div = k;
11004 if (ineq)
11005 *ineq = l;
11006 return j;
11009 return bmap->n_eq;
11012 /* Check if the given basic map is obviously single-valued.
11013 * In particular, for each output dimension, check that there is
11014 * an equality that defines the output dimension in terms of
11015 * earlier dimensions.
11017 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11019 int i;
11020 unsigned n_out;
11022 if (!bmap)
11023 return isl_bool_error;
11025 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11027 for (i = 0; i < n_out; ++i) {
11028 int eq;
11030 eq = isl_basic_map_output_defining_equality(bmap, i,
11031 NULL, NULL);
11032 if (eq < 0)
11033 return isl_bool_error;
11034 if (eq >= bmap->n_eq)
11035 return isl_bool_false;
11038 return isl_bool_true;
11041 /* Check if the given basic map is single-valued.
11042 * We simply compute
11044 * M \circ M^-1
11046 * and check if the result is a subset of the identity mapping.
11048 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11050 isl_space *space;
11051 isl_basic_map *test;
11052 isl_basic_map *id;
11053 isl_bool sv;
11055 sv = isl_basic_map_plain_is_single_valued(bmap);
11056 if (sv < 0 || sv)
11057 return sv;
11059 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11060 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11062 space = isl_basic_map_get_space(bmap);
11063 space = isl_space_map_from_set(isl_space_range(space));
11064 id = isl_basic_map_identity(space);
11066 sv = isl_basic_map_is_subset(test, id);
11068 isl_basic_map_free(test);
11069 isl_basic_map_free(id);
11071 return sv;
11074 /* Check if the given map is obviously single-valued.
11076 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11078 if (!map)
11079 return isl_bool_error;
11080 if (map->n == 0)
11081 return isl_bool_true;
11082 if (map->n >= 2)
11083 return isl_bool_false;
11085 return isl_basic_map_plain_is_single_valued(map->p[0]);
11088 /* Check if the given map is single-valued.
11089 * We simply compute
11091 * M \circ M^-1
11093 * and check if the result is a subset of the identity mapping.
11095 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11097 isl_space *dim;
11098 isl_map *test;
11099 isl_map *id;
11100 isl_bool sv;
11102 sv = isl_map_plain_is_single_valued(map);
11103 if (sv < 0 || sv)
11104 return sv;
11106 test = isl_map_reverse(isl_map_copy(map));
11107 test = isl_map_apply_range(test, isl_map_copy(map));
11109 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11110 id = isl_map_identity(dim);
11112 sv = isl_map_is_subset(test, id);
11114 isl_map_free(test);
11115 isl_map_free(id);
11117 return sv;
11120 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11122 isl_bool in;
11124 map = isl_map_copy(map);
11125 map = isl_map_reverse(map);
11126 in = isl_map_is_single_valued(map);
11127 isl_map_free(map);
11129 return in;
11132 /* Check if the given map is obviously injective.
11134 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11136 isl_bool in;
11138 map = isl_map_copy(map);
11139 map = isl_map_reverse(map);
11140 in = isl_map_plain_is_single_valued(map);
11141 isl_map_free(map);
11143 return in;
11146 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11148 isl_bool sv;
11150 sv = isl_map_is_single_valued(map);
11151 if (sv < 0 || !sv)
11152 return sv;
11154 return isl_map_is_injective(map);
11157 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11159 return isl_map_is_single_valued(set_to_map(set));
11162 /* Does "map" only map elements to themselves?
11164 * If the domain and range spaces are different, then "map"
11165 * is considered not to be an identity relation, even if it is empty.
11166 * Otherwise, construct the maximal identity relation and
11167 * check whether "map" is a subset of this relation.
11169 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11171 isl_space *space;
11172 isl_map *id;
11173 isl_bool equal, is_identity;
11175 space = isl_map_get_space(map);
11176 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11177 isl_space_free(space);
11178 if (equal < 0 || !equal)
11179 return equal;
11181 id = isl_map_identity(isl_map_get_space(map));
11182 is_identity = isl_map_is_subset(map, id);
11183 isl_map_free(id);
11185 return is_identity;
11188 int isl_map_is_translation(__isl_keep isl_map *map)
11190 int ok;
11191 isl_set *delta;
11193 delta = isl_map_deltas(isl_map_copy(map));
11194 ok = isl_set_is_singleton(delta);
11195 isl_set_free(delta);
11197 return ok;
11200 static int unique(isl_int *p, unsigned pos, unsigned len)
11202 if (isl_seq_first_non_zero(p, pos) != -1)
11203 return 0;
11204 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11205 return 0;
11206 return 1;
11209 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11211 int i, j;
11212 unsigned nvar;
11213 unsigned ovar;
11215 if (!bset)
11216 return -1;
11218 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11219 return 0;
11221 nvar = isl_basic_set_dim(bset, isl_dim_set);
11222 ovar = isl_space_offset(bset->dim, isl_dim_set);
11223 for (j = 0; j < nvar; ++j) {
11224 int lower = 0, upper = 0;
11225 for (i = 0; i < bset->n_eq; ++i) {
11226 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11227 continue;
11228 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11229 return 0;
11230 break;
11232 if (i < bset->n_eq)
11233 continue;
11234 for (i = 0; i < bset->n_ineq; ++i) {
11235 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11236 continue;
11237 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11238 return 0;
11239 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11240 lower = 1;
11241 else
11242 upper = 1;
11244 if (!lower || !upper)
11245 return 0;
11248 return 1;
11251 int isl_set_is_box(__isl_keep isl_set *set)
11253 if (!set)
11254 return -1;
11255 if (set->n != 1)
11256 return 0;
11258 return isl_basic_set_is_box(set->p[0]);
11261 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11263 if (!bset)
11264 return isl_bool_error;
11266 return isl_space_is_wrapping(bset->dim);
11269 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11271 if (!set)
11272 return isl_bool_error;
11274 return isl_space_is_wrapping(set->dim);
11277 /* Modify the space of "map" through a call to "change".
11278 * If "can_change" is set (not NULL), then first call it to check
11279 * if the modification is allowed, printing the error message "cannot_change"
11280 * if it is not.
11282 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11283 isl_bool (*can_change)(__isl_keep isl_map *map),
11284 const char *cannot_change,
11285 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11287 isl_bool ok;
11288 isl_space *space;
11290 if (!map)
11291 return NULL;
11293 ok = can_change ? can_change(map) : isl_bool_true;
11294 if (ok < 0)
11295 return isl_map_free(map);
11296 if (!ok)
11297 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11298 return isl_map_free(map));
11300 space = change(isl_map_get_space(map));
11301 map = isl_map_reset_space(map, space);
11303 return map;
11306 /* Is the domain of "map" a wrapped relation?
11308 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11310 if (!map)
11311 return isl_bool_error;
11313 return isl_space_domain_is_wrapping(map->dim);
11316 /* Is the range of "map" a wrapped relation?
11318 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11320 if (!map)
11321 return isl_bool_error;
11323 return isl_space_range_is_wrapping(map->dim);
11326 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11328 bmap = isl_basic_map_cow(bmap);
11329 if (!bmap)
11330 return NULL;
11332 bmap->dim = isl_space_wrap(bmap->dim);
11333 if (!bmap->dim)
11334 goto error;
11336 bmap = isl_basic_map_finalize(bmap);
11338 return bset_from_bmap(bmap);
11339 error:
11340 isl_basic_map_free(bmap);
11341 return NULL;
11344 /* Given a map A -> B, return the set (A -> B).
11346 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11348 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11351 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11353 bset = isl_basic_set_cow(bset);
11354 if (!bset)
11355 return NULL;
11357 bset->dim = isl_space_unwrap(bset->dim);
11358 if (!bset->dim)
11359 goto error;
11361 bset = isl_basic_set_finalize(bset);
11363 return bset_to_bmap(bset);
11364 error:
11365 isl_basic_set_free(bset);
11366 return NULL;
11369 /* Given a set (A -> B), return the map A -> B.
11370 * Error out if "set" is not of the form (A -> B).
11372 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11374 return isl_map_change_space(set, &isl_set_is_wrapping,
11375 "not a wrapping set", &isl_space_unwrap);
11378 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11379 enum isl_dim_type type)
11381 if (!bmap)
11382 return NULL;
11384 if (!isl_space_is_named_or_nested(bmap->dim, type))
11385 return bmap;
11387 bmap = isl_basic_map_cow(bmap);
11388 if (!bmap)
11389 return NULL;
11391 bmap->dim = isl_space_reset(bmap->dim, type);
11392 if (!bmap->dim)
11393 goto error;
11395 bmap = isl_basic_map_finalize(bmap);
11397 return bmap;
11398 error:
11399 isl_basic_map_free(bmap);
11400 return NULL;
11403 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11404 enum isl_dim_type type)
11406 int i;
11408 if (!map)
11409 return NULL;
11411 if (!isl_space_is_named_or_nested(map->dim, type))
11412 return map;
11414 map = isl_map_cow(map);
11415 if (!map)
11416 return NULL;
11418 for (i = 0; i < map->n; ++i) {
11419 map->p[i] = isl_basic_map_reset(map->p[i], type);
11420 if (!map->p[i])
11421 goto error;
11423 map->dim = isl_space_reset(map->dim, type);
11424 if (!map->dim)
11425 goto error;
11427 return map;
11428 error:
11429 isl_map_free(map);
11430 return NULL;
11433 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11435 if (!bmap)
11436 return NULL;
11438 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11439 return bmap;
11441 bmap = isl_basic_map_cow(bmap);
11442 if (!bmap)
11443 return NULL;
11445 bmap->dim = isl_space_flatten(bmap->dim);
11446 if (!bmap->dim)
11447 goto error;
11449 bmap = isl_basic_map_finalize(bmap);
11451 return bmap;
11452 error:
11453 isl_basic_map_free(bmap);
11454 return NULL;
11457 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11459 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11462 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11463 __isl_take isl_basic_map *bmap)
11465 if (!bmap)
11466 return NULL;
11468 if (!bmap->dim->nested[0])
11469 return bmap;
11471 bmap = isl_basic_map_cow(bmap);
11472 if (!bmap)
11473 return NULL;
11475 bmap->dim = isl_space_flatten_domain(bmap->dim);
11476 if (!bmap->dim)
11477 goto error;
11479 bmap = isl_basic_map_finalize(bmap);
11481 return bmap;
11482 error:
11483 isl_basic_map_free(bmap);
11484 return NULL;
11487 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11488 __isl_take isl_basic_map *bmap)
11490 if (!bmap)
11491 return NULL;
11493 if (!bmap->dim->nested[1])
11494 return bmap;
11496 bmap = isl_basic_map_cow(bmap);
11497 if (!bmap)
11498 return NULL;
11500 bmap->dim = isl_space_flatten_range(bmap->dim);
11501 if (!bmap->dim)
11502 goto error;
11504 bmap = isl_basic_map_finalize(bmap);
11506 return bmap;
11507 error:
11508 isl_basic_map_free(bmap);
11509 return NULL;
11512 /* Remove any internal structure from the spaces of domain and range of "map".
11514 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11516 if (!map)
11517 return NULL;
11519 if (!map->dim->nested[0] && !map->dim->nested[1])
11520 return map;
11522 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11525 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11527 return set_from_map(isl_map_flatten(set_to_map(set)));
11530 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11532 isl_space *dim, *flat_dim;
11533 isl_map *map;
11535 dim = isl_set_get_space(set);
11536 flat_dim = isl_space_flatten(isl_space_copy(dim));
11537 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11538 map = isl_map_intersect_domain(map, set);
11540 return map;
11543 /* Remove any internal structure from the space of the domain of "map".
11545 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11547 if (!map)
11548 return NULL;
11550 if (!map->dim->nested[0])
11551 return map;
11553 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11556 /* Remove any internal structure from the space of the range of "map".
11558 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11560 if (!map)
11561 return NULL;
11563 if (!map->dim->nested[1])
11564 return map;
11566 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11569 /* Reorder the dimensions of "bmap" according to the given dim_map
11570 * and set the dimension specification to "dim" and
11571 * perform Gaussian elimination on the result.
11573 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11574 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11576 isl_basic_map *res;
11577 unsigned flags;
11579 bmap = isl_basic_map_cow(bmap);
11580 if (!bmap || !dim || !dim_map)
11581 goto error;
11583 flags = bmap->flags;
11584 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11585 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11586 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11587 res = isl_basic_map_alloc_space(dim,
11588 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11589 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11590 if (res)
11591 res->flags = flags;
11592 res = isl_basic_map_gauss(res, NULL);
11593 res = isl_basic_map_finalize(res);
11594 return res;
11595 error:
11596 free(dim_map);
11597 isl_basic_map_free(bmap);
11598 isl_space_free(dim);
11599 return NULL;
11602 /* Reorder the dimensions of "map" according to given reordering.
11604 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11605 __isl_take isl_reordering *r)
11607 int i;
11608 struct isl_dim_map *dim_map;
11610 map = isl_map_cow(map);
11611 dim_map = isl_dim_map_from_reordering(r);
11612 if (!map || !r || !dim_map)
11613 goto error;
11615 for (i = 0; i < map->n; ++i) {
11616 struct isl_dim_map *dim_map_i;
11618 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11620 map->p[i] = isl_basic_map_realign(map->p[i],
11621 isl_space_copy(r->dim), dim_map_i);
11623 if (!map->p[i])
11624 goto error;
11627 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11629 isl_reordering_free(r);
11630 free(dim_map);
11631 return map;
11632 error:
11633 free(dim_map);
11634 isl_map_free(map);
11635 isl_reordering_free(r);
11636 return NULL;
11639 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11640 __isl_take isl_reordering *r)
11642 return set_from_map(isl_map_realign(set_to_map(set), r));
11645 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11646 __isl_take isl_space *model)
11648 isl_ctx *ctx;
11650 if (!map || !model)
11651 goto error;
11653 ctx = isl_space_get_ctx(model);
11654 if (!isl_space_has_named_params(model))
11655 isl_die(ctx, isl_error_invalid,
11656 "model has unnamed parameters", goto error);
11657 if (!isl_space_has_named_params(map->dim))
11658 isl_die(ctx, isl_error_invalid,
11659 "relation has unnamed parameters", goto error);
11660 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11661 isl_reordering *exp;
11663 model = isl_space_drop_dims(model, isl_dim_in,
11664 0, isl_space_dim(model, isl_dim_in));
11665 model = isl_space_drop_dims(model, isl_dim_out,
11666 0, isl_space_dim(model, isl_dim_out));
11667 exp = isl_parameter_alignment_reordering(map->dim, model);
11668 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11669 map = isl_map_realign(map, exp);
11672 isl_space_free(model);
11673 return map;
11674 error:
11675 isl_space_free(model);
11676 isl_map_free(map);
11677 return NULL;
11680 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11681 __isl_take isl_space *model)
11683 return isl_map_align_params(set, model);
11686 /* Align the parameters of "bmap" to those of "model", introducing
11687 * additional parameters if needed.
11689 __isl_give isl_basic_map *isl_basic_map_align_params(
11690 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11692 isl_ctx *ctx;
11694 if (!bmap || !model)
11695 goto error;
11697 ctx = isl_space_get_ctx(model);
11698 if (!isl_space_has_named_params(model))
11699 isl_die(ctx, isl_error_invalid,
11700 "model has unnamed parameters", goto error);
11701 if (!isl_space_has_named_params(bmap->dim))
11702 isl_die(ctx, isl_error_invalid,
11703 "relation has unnamed parameters", goto error);
11704 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11705 isl_reordering *exp;
11706 struct isl_dim_map *dim_map;
11708 model = isl_space_drop_dims(model, isl_dim_in,
11709 0, isl_space_dim(model, isl_dim_in));
11710 model = isl_space_drop_dims(model, isl_dim_out,
11711 0, isl_space_dim(model, isl_dim_out));
11712 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11713 exp = isl_reordering_extend_space(exp,
11714 isl_basic_map_get_space(bmap));
11715 dim_map = isl_dim_map_from_reordering(exp);
11716 bmap = isl_basic_map_realign(bmap,
11717 exp ? isl_space_copy(exp->dim) : NULL,
11718 isl_dim_map_extend(dim_map, bmap));
11719 isl_reordering_free(exp);
11720 free(dim_map);
11723 isl_space_free(model);
11724 return bmap;
11725 error:
11726 isl_space_free(model);
11727 isl_basic_map_free(bmap);
11728 return NULL;
11731 /* Align the parameters of "bset" to those of "model", introducing
11732 * additional parameters if needed.
11734 __isl_give isl_basic_set *isl_basic_set_align_params(
11735 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11737 return isl_basic_map_align_params(bset, model);
11740 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11741 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11742 enum isl_dim_type c2, enum isl_dim_type c3,
11743 enum isl_dim_type c4, enum isl_dim_type c5)
11745 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11746 struct isl_mat *mat;
11747 int i, j, k;
11748 int pos;
11750 if (!bmap)
11751 return NULL;
11752 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11753 isl_basic_map_total_dim(bmap) + 1);
11754 if (!mat)
11755 return NULL;
11756 for (i = 0; i < bmap->n_eq; ++i)
11757 for (j = 0, pos = 0; j < 5; ++j) {
11758 int off = isl_basic_map_offset(bmap, c[j]);
11759 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11760 isl_int_set(mat->row[i][pos],
11761 bmap->eq[i][off + k]);
11762 ++pos;
11766 return mat;
11769 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11770 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11771 enum isl_dim_type c2, enum isl_dim_type c3,
11772 enum isl_dim_type c4, enum isl_dim_type c5)
11774 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11775 struct isl_mat *mat;
11776 int i, j, k;
11777 int pos;
11779 if (!bmap)
11780 return NULL;
11781 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11782 isl_basic_map_total_dim(bmap) + 1);
11783 if (!mat)
11784 return NULL;
11785 for (i = 0; i < bmap->n_ineq; ++i)
11786 for (j = 0, pos = 0; j < 5; ++j) {
11787 int off = isl_basic_map_offset(bmap, c[j]);
11788 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11789 isl_int_set(mat->row[i][pos],
11790 bmap->ineq[i][off + k]);
11791 ++pos;
11795 return mat;
11798 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11799 __isl_take isl_space *dim,
11800 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11801 enum isl_dim_type c2, enum isl_dim_type c3,
11802 enum isl_dim_type c4, enum isl_dim_type c5)
11804 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11805 isl_basic_map *bmap;
11806 unsigned total;
11807 unsigned extra;
11808 int i, j, k, l;
11809 int pos;
11811 if (!dim || !eq || !ineq)
11812 goto error;
11814 if (eq->n_col != ineq->n_col)
11815 isl_die(dim->ctx, isl_error_invalid,
11816 "equalities and inequalities matrices should have "
11817 "same number of columns", goto error);
11819 total = 1 + isl_space_dim(dim, isl_dim_all);
11821 if (eq->n_col < total)
11822 isl_die(dim->ctx, isl_error_invalid,
11823 "number of columns too small", goto error);
11825 extra = eq->n_col - total;
11827 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11828 eq->n_row, ineq->n_row);
11829 if (!bmap)
11830 goto error;
11831 for (i = 0; i < extra; ++i) {
11832 k = isl_basic_map_alloc_div(bmap);
11833 if (k < 0)
11834 goto error;
11835 isl_int_set_si(bmap->div[k][0], 0);
11837 for (i = 0; i < eq->n_row; ++i) {
11838 l = isl_basic_map_alloc_equality(bmap);
11839 if (l < 0)
11840 goto error;
11841 for (j = 0, pos = 0; j < 5; ++j) {
11842 int off = isl_basic_map_offset(bmap, c[j]);
11843 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11844 isl_int_set(bmap->eq[l][off + k],
11845 eq->row[i][pos]);
11846 ++pos;
11850 for (i = 0; i < ineq->n_row; ++i) {
11851 l = isl_basic_map_alloc_inequality(bmap);
11852 if (l < 0)
11853 goto error;
11854 for (j = 0, pos = 0; j < 5; ++j) {
11855 int off = isl_basic_map_offset(bmap, c[j]);
11856 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11857 isl_int_set(bmap->ineq[l][off + k],
11858 ineq->row[i][pos]);
11859 ++pos;
11864 isl_space_free(dim);
11865 isl_mat_free(eq);
11866 isl_mat_free(ineq);
11868 bmap = isl_basic_map_simplify(bmap);
11869 return isl_basic_map_finalize(bmap);
11870 error:
11871 isl_space_free(dim);
11872 isl_mat_free(eq);
11873 isl_mat_free(ineq);
11874 return NULL;
11877 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11878 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11879 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11881 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11882 c1, c2, c3, c4, isl_dim_in);
11885 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11886 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11887 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11889 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11890 c1, c2, c3, c4, isl_dim_in);
11893 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11894 __isl_take isl_space *dim,
11895 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11896 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11898 isl_basic_map *bmap;
11899 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11900 c1, c2, c3, c4, isl_dim_in);
11901 return bset_from_bmap(bmap);
11904 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11906 if (!bmap)
11907 return isl_bool_error;
11909 return isl_space_can_zip(bmap->dim);
11912 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11914 if (!map)
11915 return isl_bool_error;
11917 return isl_space_can_zip(map->dim);
11920 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11921 * (A -> C) -> (B -> D).
11923 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11925 unsigned pos;
11926 unsigned n1;
11927 unsigned n2;
11929 if (!bmap)
11930 return NULL;
11932 if (!isl_basic_map_can_zip(bmap))
11933 isl_die(bmap->ctx, isl_error_invalid,
11934 "basic map cannot be zipped", goto error);
11935 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11936 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11937 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11938 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11939 bmap = isl_basic_map_cow(bmap);
11940 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11941 if (!bmap)
11942 return NULL;
11943 bmap->dim = isl_space_zip(bmap->dim);
11944 if (!bmap->dim)
11945 goto error;
11946 bmap = isl_basic_map_mark_final(bmap);
11947 return bmap;
11948 error:
11949 isl_basic_map_free(bmap);
11950 return NULL;
11953 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11954 * (A -> C) -> (B -> D).
11956 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11958 int i;
11960 if (!map)
11961 return NULL;
11963 if (!isl_map_can_zip(map))
11964 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11965 goto error);
11967 map = isl_map_cow(map);
11968 if (!map)
11969 return NULL;
11971 for (i = 0; i < map->n; ++i) {
11972 map->p[i] = isl_basic_map_zip(map->p[i]);
11973 if (!map->p[i])
11974 goto error;
11977 map->dim = isl_space_zip(map->dim);
11978 if (!map->dim)
11979 goto error;
11981 return map;
11982 error:
11983 isl_map_free(map);
11984 return NULL;
11987 /* Can we apply isl_basic_map_curry to "bmap"?
11988 * That is, does it have a nested relation in its domain?
11990 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11992 if (!bmap)
11993 return isl_bool_error;
11995 return isl_space_can_curry(bmap->dim);
11998 /* Can we apply isl_map_curry to "map"?
11999 * That is, does it have a nested relation in its domain?
12001 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12003 if (!map)
12004 return isl_bool_error;
12006 return isl_space_can_curry(map->dim);
12009 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12010 * A -> (B -> C).
12012 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12015 if (!bmap)
12016 return NULL;
12018 if (!isl_basic_map_can_curry(bmap))
12019 isl_die(bmap->ctx, isl_error_invalid,
12020 "basic map cannot be curried", goto error);
12021 bmap = isl_basic_map_cow(bmap);
12022 if (!bmap)
12023 return NULL;
12024 bmap->dim = isl_space_curry(bmap->dim);
12025 if (!bmap->dim)
12026 goto error;
12027 bmap = isl_basic_map_mark_final(bmap);
12028 return bmap;
12029 error:
12030 isl_basic_map_free(bmap);
12031 return NULL;
12034 /* Given a map (A -> B) -> C, return the corresponding map
12035 * A -> (B -> C).
12037 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12039 return isl_map_change_space(map, &isl_map_can_curry,
12040 "map cannot be curried", &isl_space_curry);
12043 /* Can isl_map_range_curry be applied to "map"?
12044 * That is, does it have a nested relation in its range,
12045 * the domain of which is itself a nested relation?
12047 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12049 if (!map)
12050 return isl_bool_error;
12052 return isl_space_can_range_curry(map->dim);
12055 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12056 * A -> (B -> (C -> D)).
12058 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12060 return isl_map_change_space(map, &isl_map_can_range_curry,
12061 "map range cannot be curried",
12062 &isl_space_range_curry);
12065 /* Can we apply isl_basic_map_uncurry to "bmap"?
12066 * That is, does it have a nested relation in its domain?
12068 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12070 if (!bmap)
12071 return isl_bool_error;
12073 return isl_space_can_uncurry(bmap->dim);
12076 /* Can we apply isl_map_uncurry to "map"?
12077 * That is, does it have a nested relation in its domain?
12079 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12081 if (!map)
12082 return isl_bool_error;
12084 return isl_space_can_uncurry(map->dim);
12087 /* Given a basic map A -> (B -> C), return the corresponding basic map
12088 * (A -> B) -> C.
12090 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12093 if (!bmap)
12094 return NULL;
12096 if (!isl_basic_map_can_uncurry(bmap))
12097 isl_die(bmap->ctx, isl_error_invalid,
12098 "basic map cannot be uncurried",
12099 return isl_basic_map_free(bmap));
12100 bmap = isl_basic_map_cow(bmap);
12101 if (!bmap)
12102 return NULL;
12103 bmap->dim = isl_space_uncurry(bmap->dim);
12104 if (!bmap->dim)
12105 return isl_basic_map_free(bmap);
12106 bmap = isl_basic_map_mark_final(bmap);
12107 return bmap;
12110 /* Given a map A -> (B -> C), return the corresponding map
12111 * (A -> B) -> C.
12113 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12115 return isl_map_change_space(map, &isl_map_can_uncurry,
12116 "map cannot be uncurried", &isl_space_uncurry);
12119 /* Construct a basic map mapping the domain of the affine expression
12120 * to a one-dimensional range prescribed by the affine expression.
12121 * If "rational" is set, then construct a rational basic map.
12123 * A NaN affine expression cannot be converted to a basic map.
12125 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12126 __isl_take isl_aff *aff, int rational)
12128 int k;
12129 int pos;
12130 isl_bool is_nan;
12131 isl_local_space *ls;
12132 isl_basic_map *bmap = NULL;
12134 if (!aff)
12135 return NULL;
12136 is_nan = isl_aff_is_nan(aff);
12137 if (is_nan < 0)
12138 goto error;
12139 if (is_nan)
12140 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12141 "cannot convert NaN", goto error);
12143 ls = isl_aff_get_local_space(aff);
12144 bmap = isl_basic_map_from_local_space(ls);
12145 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12146 k = isl_basic_map_alloc_equality(bmap);
12147 if (k < 0)
12148 goto error;
12150 pos = isl_basic_map_offset(bmap, isl_dim_out);
12151 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12152 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12153 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12154 aff->v->size - (pos + 1));
12156 isl_aff_free(aff);
12157 if (rational)
12158 bmap = isl_basic_map_set_rational(bmap);
12159 bmap = isl_basic_map_finalize(bmap);
12160 return bmap;
12161 error:
12162 isl_aff_free(aff);
12163 isl_basic_map_free(bmap);
12164 return NULL;
12167 /* Construct a basic map mapping the domain of the affine expression
12168 * to a one-dimensional range prescribed by the affine expression.
12170 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12172 return isl_basic_map_from_aff2(aff, 0);
12175 /* Construct a map mapping the domain of the affine expression
12176 * to a one-dimensional range prescribed by the affine expression.
12178 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12180 isl_basic_map *bmap;
12182 bmap = isl_basic_map_from_aff(aff);
12183 return isl_map_from_basic_map(bmap);
12186 /* Construct a basic map mapping the domain the multi-affine expression
12187 * to its range, with each dimension in the range equated to the
12188 * corresponding affine expression.
12189 * If "rational" is set, then construct a rational basic map.
12191 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12192 __isl_take isl_multi_aff *maff, int rational)
12194 int i;
12195 isl_space *space;
12196 isl_basic_map *bmap;
12198 if (!maff)
12199 return NULL;
12201 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12202 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12203 "invalid space", goto error);
12205 space = isl_space_domain(isl_multi_aff_get_space(maff));
12206 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12207 if (rational)
12208 bmap = isl_basic_map_set_rational(bmap);
12210 for (i = 0; i < maff->n; ++i) {
12211 isl_aff *aff;
12212 isl_basic_map *bmap_i;
12214 aff = isl_aff_copy(maff->p[i]);
12215 bmap_i = isl_basic_map_from_aff2(aff, rational);
12217 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12220 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12222 isl_multi_aff_free(maff);
12223 return bmap;
12224 error:
12225 isl_multi_aff_free(maff);
12226 return NULL;
12229 /* Construct a basic map mapping the domain the multi-affine expression
12230 * to its range, with each dimension in the range equated to the
12231 * corresponding affine expression.
12233 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12234 __isl_take isl_multi_aff *ma)
12236 return isl_basic_map_from_multi_aff2(ma, 0);
12239 /* Construct a map mapping the domain the multi-affine expression
12240 * to its range, with each dimension in the range equated to the
12241 * corresponding affine expression.
12243 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12245 isl_basic_map *bmap;
12247 bmap = isl_basic_map_from_multi_aff(maff);
12248 return isl_map_from_basic_map(bmap);
12251 /* Construct a basic map mapping a domain in the given space to
12252 * to an n-dimensional range, with n the number of elements in the list,
12253 * where each coordinate in the range is prescribed by the
12254 * corresponding affine expression.
12255 * The domains of all affine expressions in the list are assumed to match
12256 * domain_dim.
12258 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12259 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12261 int i;
12262 isl_space *dim;
12263 isl_basic_map *bmap;
12265 if (!list)
12266 return NULL;
12268 dim = isl_space_from_domain(domain_dim);
12269 bmap = isl_basic_map_universe(dim);
12271 for (i = 0; i < list->n; ++i) {
12272 isl_aff *aff;
12273 isl_basic_map *bmap_i;
12275 aff = isl_aff_copy(list->p[i]);
12276 bmap_i = isl_basic_map_from_aff(aff);
12278 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12281 isl_aff_list_free(list);
12282 return bmap;
12285 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12286 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12288 return isl_map_equate(set, type1, pos1, type2, pos2);
12291 /* Construct a basic map where the given dimensions are equal to each other.
12293 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12294 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12296 isl_basic_map *bmap = NULL;
12297 int i;
12299 if (!space)
12300 return NULL;
12302 if (pos1 >= isl_space_dim(space, type1))
12303 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12304 "index out of bounds", goto error);
12305 if (pos2 >= isl_space_dim(space, type2))
12306 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12307 "index out of bounds", goto error);
12309 if (type1 == type2 && pos1 == pos2)
12310 return isl_basic_map_universe(space);
12312 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12313 i = isl_basic_map_alloc_equality(bmap);
12314 if (i < 0)
12315 goto error;
12316 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12317 pos1 += isl_basic_map_offset(bmap, type1);
12318 pos2 += isl_basic_map_offset(bmap, type2);
12319 isl_int_set_si(bmap->eq[i][pos1], -1);
12320 isl_int_set_si(bmap->eq[i][pos2], 1);
12321 bmap = isl_basic_map_finalize(bmap);
12322 isl_space_free(space);
12323 return bmap;
12324 error:
12325 isl_space_free(space);
12326 isl_basic_map_free(bmap);
12327 return NULL;
12330 /* Add a constraint imposing that the given two dimensions are equal.
12332 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12333 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12335 isl_basic_map *eq;
12337 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12339 bmap = isl_basic_map_intersect(bmap, eq);
12341 return bmap;
12344 /* Add a constraint imposing that the given two dimensions are equal.
12346 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12347 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12349 isl_basic_map *bmap;
12351 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12353 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12355 return map;
12358 /* Add a constraint imposing that the given two dimensions have opposite values.
12360 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12361 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12363 isl_basic_map *bmap = NULL;
12364 int i;
12366 if (!map)
12367 return NULL;
12369 if (pos1 >= isl_map_dim(map, type1))
12370 isl_die(map->ctx, isl_error_invalid,
12371 "index out of bounds", goto error);
12372 if (pos2 >= isl_map_dim(map, type2))
12373 isl_die(map->ctx, isl_error_invalid,
12374 "index out of bounds", goto error);
12376 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12377 i = isl_basic_map_alloc_equality(bmap);
12378 if (i < 0)
12379 goto error;
12380 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12381 pos1 += isl_basic_map_offset(bmap, type1);
12382 pos2 += isl_basic_map_offset(bmap, type2);
12383 isl_int_set_si(bmap->eq[i][pos1], 1);
12384 isl_int_set_si(bmap->eq[i][pos2], 1);
12385 bmap = isl_basic_map_finalize(bmap);
12387 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12389 return map;
12390 error:
12391 isl_basic_map_free(bmap);
12392 isl_map_free(map);
12393 return NULL;
12396 /* Construct a constraint imposing that the value of the first dimension is
12397 * greater than or equal to that of the second.
12399 static __isl_give isl_constraint *constraint_order_ge(
12400 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12401 enum isl_dim_type type2, int pos2)
12403 isl_constraint *c;
12405 if (!space)
12406 return NULL;
12408 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12410 if (pos1 >= isl_constraint_dim(c, type1))
12411 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12412 "index out of bounds", return isl_constraint_free(c));
12413 if (pos2 >= isl_constraint_dim(c, type2))
12414 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12415 "index out of bounds", return isl_constraint_free(c));
12417 if (type1 == type2 && pos1 == pos2)
12418 return c;
12420 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12421 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12423 return c;
12426 /* Add a constraint imposing that the value of the first dimension is
12427 * greater than or equal to that of the second.
12429 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12430 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12432 isl_constraint *c;
12433 isl_space *space;
12435 if (type1 == type2 && pos1 == pos2)
12436 return bmap;
12437 space = isl_basic_map_get_space(bmap);
12438 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12439 bmap = isl_basic_map_add_constraint(bmap, c);
12441 return bmap;
12444 /* Add a constraint imposing that the value of the first dimension is
12445 * greater than or equal to that of the second.
12447 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12448 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12450 isl_constraint *c;
12451 isl_space *space;
12453 if (type1 == type2 && pos1 == pos2)
12454 return map;
12455 space = isl_map_get_space(map);
12456 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12457 map = isl_map_add_constraint(map, c);
12459 return map;
12462 /* Add a constraint imposing that the value of the first dimension is
12463 * less than or equal to that of the second.
12465 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12466 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12468 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12471 /* Construct a basic map where the value of the first dimension is
12472 * greater than that of the second.
12474 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12475 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12477 isl_basic_map *bmap = NULL;
12478 int i;
12480 if (!space)
12481 return NULL;
12483 if (pos1 >= isl_space_dim(space, type1))
12484 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12485 "index out of bounds", goto error);
12486 if (pos2 >= isl_space_dim(space, type2))
12487 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12488 "index out of bounds", goto error);
12490 if (type1 == type2 && pos1 == pos2)
12491 return isl_basic_map_empty(space);
12493 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12494 i = isl_basic_map_alloc_inequality(bmap);
12495 if (i < 0)
12496 return isl_basic_map_free(bmap);
12497 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12498 pos1 += isl_basic_map_offset(bmap, type1);
12499 pos2 += isl_basic_map_offset(bmap, type2);
12500 isl_int_set_si(bmap->ineq[i][pos1], 1);
12501 isl_int_set_si(bmap->ineq[i][pos2], -1);
12502 isl_int_set_si(bmap->ineq[i][0], -1);
12503 bmap = isl_basic_map_finalize(bmap);
12505 return bmap;
12506 error:
12507 isl_space_free(space);
12508 isl_basic_map_free(bmap);
12509 return NULL;
12512 /* Add a constraint imposing that the value of the first dimension is
12513 * greater than that of the second.
12515 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12516 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12518 isl_basic_map *gt;
12520 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12522 bmap = isl_basic_map_intersect(bmap, gt);
12524 return bmap;
12527 /* Add a constraint imposing that the value of the first dimension is
12528 * greater than that of the second.
12530 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12531 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12533 isl_basic_map *bmap;
12535 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12537 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12539 return map;
12542 /* Add a constraint imposing that the value of the first dimension is
12543 * smaller than that of the second.
12545 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12546 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12548 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12551 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12552 int pos)
12554 isl_aff *div;
12555 isl_local_space *ls;
12557 if (!bmap)
12558 return NULL;
12560 if (!isl_basic_map_divs_known(bmap))
12561 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12562 "some divs are unknown", return NULL);
12564 ls = isl_basic_map_get_local_space(bmap);
12565 div = isl_local_space_get_div(ls, pos);
12566 isl_local_space_free(ls);
12568 return div;
12571 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12572 int pos)
12574 return isl_basic_map_get_div(bset, pos);
12577 /* Plug in "subs" for dimension "type", "pos" of "bset".
12579 * Let i be the dimension to replace and let "subs" be of the form
12581 * f/d
12583 * Any integer division with a non-zero coefficient for i,
12585 * floor((a i + g)/m)
12587 * is replaced by
12589 * floor((a f + d g)/(m d))
12591 * Constraints of the form
12593 * a i + g
12595 * are replaced by
12597 * a f + d g
12599 * We currently require that "subs" is an integral expression.
12600 * Handling rational expressions may require us to add stride constraints
12601 * as we do in isl_basic_set_preimage_multi_aff.
12603 __isl_give isl_basic_set *isl_basic_set_substitute(
12604 __isl_take isl_basic_set *bset,
12605 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12607 int i;
12608 isl_int v;
12609 isl_ctx *ctx;
12611 if (bset && isl_basic_set_plain_is_empty(bset))
12612 return bset;
12614 bset = isl_basic_set_cow(bset);
12615 if (!bset || !subs)
12616 goto error;
12618 ctx = isl_basic_set_get_ctx(bset);
12619 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12620 isl_die(ctx, isl_error_invalid,
12621 "spaces don't match", goto error);
12622 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12623 isl_die(ctx, isl_error_unsupported,
12624 "cannot handle divs yet", goto error);
12625 if (!isl_int_is_one(subs->v->el[0]))
12626 isl_die(ctx, isl_error_invalid,
12627 "can only substitute integer expressions", goto error);
12629 pos += isl_basic_set_offset(bset, type);
12631 isl_int_init(v);
12633 for (i = 0; i < bset->n_eq; ++i) {
12634 if (isl_int_is_zero(bset->eq[i][pos]))
12635 continue;
12636 isl_int_set(v, bset->eq[i][pos]);
12637 isl_int_set_si(bset->eq[i][pos], 0);
12638 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12639 v, subs->v->el + 1, subs->v->size - 1);
12642 for (i = 0; i < bset->n_ineq; ++i) {
12643 if (isl_int_is_zero(bset->ineq[i][pos]))
12644 continue;
12645 isl_int_set(v, bset->ineq[i][pos]);
12646 isl_int_set_si(bset->ineq[i][pos], 0);
12647 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12648 v, subs->v->el + 1, subs->v->size - 1);
12651 for (i = 0; i < bset->n_div; ++i) {
12652 if (isl_int_is_zero(bset->div[i][1 + pos]))
12653 continue;
12654 isl_int_set(v, bset->div[i][1 + pos]);
12655 isl_int_set_si(bset->div[i][1 + pos], 0);
12656 isl_seq_combine(bset->div[i] + 1,
12657 subs->v->el[0], bset->div[i] + 1,
12658 v, subs->v->el + 1, subs->v->size - 1);
12659 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12662 isl_int_clear(v);
12664 bset = isl_basic_set_simplify(bset);
12665 return isl_basic_set_finalize(bset);
12666 error:
12667 isl_basic_set_free(bset);
12668 return NULL;
12671 /* Plug in "subs" for dimension "type", "pos" of "set".
12673 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12674 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12676 int i;
12678 if (set && isl_set_plain_is_empty(set))
12679 return set;
12681 set = isl_set_cow(set);
12682 if (!set || !subs)
12683 goto error;
12685 for (i = set->n - 1; i >= 0; --i) {
12686 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12687 if (remove_if_empty(set, i) < 0)
12688 goto error;
12691 return set;
12692 error:
12693 isl_set_free(set);
12694 return NULL;
12697 /* Check if the range of "ma" is compatible with the domain or range
12698 * (depending on "type") of "bmap".
12699 * Return -1 if anything is wrong.
12701 static int check_basic_map_compatible_range_multi_aff(
12702 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12703 __isl_keep isl_multi_aff *ma)
12705 int m;
12706 isl_space *ma_space;
12708 ma_space = isl_multi_aff_get_space(ma);
12710 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12711 if (m < 0)
12712 goto error;
12713 if (!m)
12714 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12715 "parameters don't match", goto error);
12716 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12717 if (m < 0)
12718 goto error;
12719 if (!m)
12720 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12721 "spaces don't match", goto error);
12723 isl_space_free(ma_space);
12724 return m;
12725 error:
12726 isl_space_free(ma_space);
12727 return -1;
12730 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12731 * coefficients before the transformed range of dimensions,
12732 * the "n_after" coefficients after the transformed range of dimensions
12733 * and the coefficients of the other divs in "bmap".
12735 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12736 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12738 int i;
12739 int n_param;
12740 int n_set;
12741 isl_local_space *ls;
12743 if (n_div == 0)
12744 return 0;
12746 ls = isl_aff_get_domain_local_space(ma->p[0]);
12747 if (!ls)
12748 return -1;
12750 n_param = isl_local_space_dim(ls, isl_dim_param);
12751 n_set = isl_local_space_dim(ls, isl_dim_set);
12752 for (i = 0; i < n_div; ++i) {
12753 int o_bmap = 0, o_ls = 0;
12755 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12756 o_bmap += 1 + 1 + n_param;
12757 o_ls += 1 + 1 + n_param;
12758 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12759 o_bmap += n_before;
12760 isl_seq_cpy(bmap->div[i] + o_bmap,
12761 ls->div->row[i] + o_ls, n_set);
12762 o_bmap += n_set;
12763 o_ls += n_set;
12764 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12765 o_bmap += n_after;
12766 isl_seq_cpy(bmap->div[i] + o_bmap,
12767 ls->div->row[i] + o_ls, n_div);
12768 o_bmap += n_div;
12769 o_ls += n_div;
12770 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12771 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12772 goto error;
12775 isl_local_space_free(ls);
12776 return 0;
12777 error:
12778 isl_local_space_free(ls);
12779 return -1;
12782 /* How many stride constraints does "ma" enforce?
12783 * That is, how many of the affine expressions have a denominator
12784 * different from one?
12786 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12788 int i;
12789 int strides = 0;
12791 for (i = 0; i < ma->n; ++i)
12792 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12793 strides++;
12795 return strides;
12798 /* For each affine expression in ma of the form
12800 * x_i = (f_i y + h_i)/m_i
12802 * with m_i different from one, add a constraint to "bmap"
12803 * of the form
12805 * f_i y + h_i = m_i alpha_i
12807 * with alpha_i an additional existentially quantified variable.
12809 * The input variables of "ma" correspond to a subset of the variables
12810 * of "bmap". There are "n_before" variables in "bmap" before this
12811 * subset and "n_after" variables after this subset.
12812 * The integer divisions of the affine expressions in "ma" are assumed
12813 * to have been aligned. There are "n_div_ma" of them and
12814 * they appear first in "bmap", straight after the "n_after" variables.
12816 static __isl_give isl_basic_map *add_ma_strides(
12817 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12818 int n_before, int n_after, int n_div_ma)
12820 int i, k;
12821 int div;
12822 int total;
12823 int n_param;
12824 int n_in;
12826 total = isl_basic_map_total_dim(bmap);
12827 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12828 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12829 for (i = 0; i < ma->n; ++i) {
12830 int o_bmap = 0, o_ma = 1;
12832 if (isl_int_is_one(ma->p[i]->v->el[0]))
12833 continue;
12834 div = isl_basic_map_alloc_div(bmap);
12835 k = isl_basic_map_alloc_equality(bmap);
12836 if (div < 0 || k < 0)
12837 goto error;
12838 isl_int_set_si(bmap->div[div][0], 0);
12839 isl_seq_cpy(bmap->eq[k] + o_bmap,
12840 ma->p[i]->v->el + o_ma, 1 + n_param);
12841 o_bmap += 1 + n_param;
12842 o_ma += 1 + n_param;
12843 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12844 o_bmap += n_before;
12845 isl_seq_cpy(bmap->eq[k] + o_bmap,
12846 ma->p[i]->v->el + o_ma, n_in);
12847 o_bmap += n_in;
12848 o_ma += n_in;
12849 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12850 o_bmap += n_after;
12851 isl_seq_cpy(bmap->eq[k] + o_bmap,
12852 ma->p[i]->v->el + o_ma, n_div_ma);
12853 o_bmap += n_div_ma;
12854 o_ma += n_div_ma;
12855 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12856 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12857 total++;
12860 return bmap;
12861 error:
12862 isl_basic_map_free(bmap);
12863 return NULL;
12866 /* Replace the domain or range space (depending on "type) of "space" by "set".
12868 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12869 enum isl_dim_type type, __isl_take isl_space *set)
12871 if (type == isl_dim_in) {
12872 space = isl_space_range(space);
12873 space = isl_space_map_from_domain_and_range(set, space);
12874 } else {
12875 space = isl_space_domain(space);
12876 space = isl_space_map_from_domain_and_range(space, set);
12879 return space;
12882 /* Compute the preimage of the domain or range (depending on "type")
12883 * of "bmap" under the function represented by "ma".
12884 * In other words, plug in "ma" in the domain or range of "bmap".
12885 * The result is a basic map that lives in the same space as "bmap"
12886 * except that the domain or range has been replaced by
12887 * the domain space of "ma".
12889 * If bmap is represented by
12891 * A(p) + S u + B x + T v + C(divs) >= 0,
12893 * where u and x are input and output dimensions if type == isl_dim_out
12894 * while x and v are input and output dimensions if type == isl_dim_in,
12895 * and ma is represented by
12897 * x = D(p) + F(y) + G(divs')
12899 * then the result is
12901 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12903 * The divs in the input set are similarly adjusted.
12904 * In particular
12906 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12908 * becomes
12910 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12911 * B_i G(divs') + c_i(divs))/n_i)
12913 * If bmap is not a rational map and if F(y) involves any denominators
12915 * x_i = (f_i y + h_i)/m_i
12917 * then additional constraints are added to ensure that we only
12918 * map back integer points. That is we enforce
12920 * f_i y + h_i = m_i alpha_i
12922 * with alpha_i an additional existentially quantified variable.
12924 * We first copy over the divs from "ma".
12925 * Then we add the modified constraints and divs from "bmap".
12926 * Finally, we add the stride constraints, if needed.
12928 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12929 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12930 __isl_take isl_multi_aff *ma)
12932 int i, k;
12933 isl_space *space;
12934 isl_basic_map *res = NULL;
12935 int n_before, n_after, n_div_bmap, n_div_ma;
12936 isl_int f, c1, c2, g;
12937 int rational, strides;
12939 isl_int_init(f);
12940 isl_int_init(c1);
12941 isl_int_init(c2);
12942 isl_int_init(g);
12944 ma = isl_multi_aff_align_divs(ma);
12945 if (!bmap || !ma)
12946 goto error;
12947 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12948 goto error;
12950 if (type == isl_dim_in) {
12951 n_before = 0;
12952 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12953 } else {
12954 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12955 n_after = 0;
12957 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12958 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12960 space = isl_multi_aff_get_domain_space(ma);
12961 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12962 rational = isl_basic_map_is_rational(bmap);
12963 strides = rational ? 0 : multi_aff_strides(ma);
12964 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12965 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12966 if (rational)
12967 res = isl_basic_map_set_rational(res);
12969 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12970 if (isl_basic_map_alloc_div(res) < 0)
12971 goto error;
12973 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12974 goto error;
12976 for (i = 0; i < bmap->n_eq; ++i) {
12977 k = isl_basic_map_alloc_equality(res);
12978 if (k < 0)
12979 goto error;
12980 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12981 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12984 for (i = 0; i < bmap->n_ineq; ++i) {
12985 k = isl_basic_map_alloc_inequality(res);
12986 if (k < 0)
12987 goto error;
12988 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12989 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12992 for (i = 0; i < bmap->n_div; ++i) {
12993 if (isl_int_is_zero(bmap->div[i][0])) {
12994 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12995 continue;
12997 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12998 n_before, n_after, n_div_ma, n_div_bmap,
12999 f, c1, c2, g, 1);
13002 if (strides)
13003 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13005 isl_int_clear(f);
13006 isl_int_clear(c1);
13007 isl_int_clear(c2);
13008 isl_int_clear(g);
13009 isl_basic_map_free(bmap);
13010 isl_multi_aff_free(ma);
13011 res = isl_basic_set_simplify(res);
13012 return isl_basic_map_finalize(res);
13013 error:
13014 isl_int_clear(f);
13015 isl_int_clear(c1);
13016 isl_int_clear(c2);
13017 isl_int_clear(g);
13018 isl_basic_map_free(bmap);
13019 isl_multi_aff_free(ma);
13020 isl_basic_map_free(res);
13021 return NULL;
13024 /* Compute the preimage of "bset" under the function represented by "ma".
13025 * In other words, plug in "ma" in "bset". The result is a basic set
13026 * that lives in the domain space of "ma".
13028 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13029 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13031 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13034 /* Compute the preimage of the domain of "bmap" under the function
13035 * represented by "ma".
13036 * In other words, plug in "ma" in the domain of "bmap".
13037 * The result is a basic map that lives in the same space as "bmap"
13038 * except that the domain has been replaced by the domain space of "ma".
13040 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13041 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13043 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13046 /* Compute the preimage of the range of "bmap" under the function
13047 * represented by "ma".
13048 * In other words, plug in "ma" in the range of "bmap".
13049 * The result is a basic map that lives in the same space as "bmap"
13050 * except that the range has been replaced by the domain space of "ma".
13052 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13053 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13055 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13058 /* Check if the range of "ma" is compatible with the domain or range
13059 * (depending on "type") of "map".
13060 * Return -1 if anything is wrong.
13062 static int check_map_compatible_range_multi_aff(
13063 __isl_keep isl_map *map, enum isl_dim_type type,
13064 __isl_keep isl_multi_aff *ma)
13066 int m;
13067 isl_space *ma_space;
13069 ma_space = isl_multi_aff_get_space(ma);
13070 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13071 isl_space_free(ma_space);
13072 if (m >= 0 && !m)
13073 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13074 "spaces don't match", return -1);
13075 return m;
13078 /* Compute the preimage of the domain or range (depending on "type")
13079 * of "map" under the function represented by "ma".
13080 * In other words, plug in "ma" in the domain or range of "map".
13081 * The result is a map that lives in the same space as "map"
13082 * except that the domain or range has been replaced by
13083 * the domain space of "ma".
13085 * The parameters are assumed to have been aligned.
13087 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13088 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13090 int i;
13091 isl_space *space;
13093 map = isl_map_cow(map);
13094 ma = isl_multi_aff_align_divs(ma);
13095 if (!map || !ma)
13096 goto error;
13097 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13098 goto error;
13100 for (i = 0; i < map->n; ++i) {
13101 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13102 isl_multi_aff_copy(ma));
13103 if (!map->p[i])
13104 goto error;
13107 space = isl_multi_aff_get_domain_space(ma);
13108 space = isl_space_set(isl_map_get_space(map), type, space);
13110 isl_space_free(map->dim);
13111 map->dim = space;
13112 if (!map->dim)
13113 goto error;
13115 isl_multi_aff_free(ma);
13116 if (map->n > 1)
13117 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13118 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13119 return map;
13120 error:
13121 isl_multi_aff_free(ma);
13122 isl_map_free(map);
13123 return NULL;
13126 /* Compute the preimage of the domain or range (depending on "type")
13127 * of "map" under the function represented by "ma".
13128 * In other words, plug in "ma" in the domain or range of "map".
13129 * The result is a map that lives in the same space as "map"
13130 * except that the domain or range has been replaced by
13131 * the domain space of "ma".
13133 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13134 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13136 if (!map || !ma)
13137 goto error;
13139 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
13140 return map_preimage_multi_aff(map, type, ma);
13142 if (!isl_space_has_named_params(map->dim) ||
13143 !isl_space_has_named_params(ma->space))
13144 isl_die(map->ctx, isl_error_invalid,
13145 "unaligned unnamed parameters", goto error);
13146 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13147 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13149 return map_preimage_multi_aff(map, type, ma);
13150 error:
13151 isl_multi_aff_free(ma);
13152 return isl_map_free(map);
13155 /* Compute the preimage of "set" under the function represented by "ma".
13156 * In other words, plug in "ma" in "set". The result is a set
13157 * that lives in the domain space of "ma".
13159 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13160 __isl_take isl_multi_aff *ma)
13162 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13165 /* Compute the preimage of the domain of "map" under the function
13166 * represented by "ma".
13167 * In other words, plug in "ma" in the domain of "map".
13168 * The result is a map that lives in the same space as "map"
13169 * except that the domain has been replaced by the domain space of "ma".
13171 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13172 __isl_take isl_multi_aff *ma)
13174 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13177 /* Compute the preimage of the range of "map" under the function
13178 * represented by "ma".
13179 * In other words, plug in "ma" in the range of "map".
13180 * The result is a map that lives in the same space as "map"
13181 * except that the range has been replaced by the domain space of "ma".
13183 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13184 __isl_take isl_multi_aff *ma)
13186 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13189 /* Compute the preimage of "map" under the function represented by "pma".
13190 * In other words, plug in "pma" in the domain or range of "map".
13191 * The result is a map that lives in the same space as "map",
13192 * except that the space of type "type" has been replaced by
13193 * the domain space of "pma".
13195 * The parameters of "map" and "pma" are assumed to have been aligned.
13197 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13198 __isl_take isl_map *map, enum isl_dim_type type,
13199 __isl_take isl_pw_multi_aff *pma)
13201 int i;
13202 isl_map *res;
13204 if (!pma)
13205 goto error;
13207 if (pma->n == 0) {
13208 isl_pw_multi_aff_free(pma);
13209 res = isl_map_empty(isl_map_get_space(map));
13210 isl_map_free(map);
13211 return res;
13214 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13215 isl_multi_aff_copy(pma->p[0].maff));
13216 if (type == isl_dim_in)
13217 res = isl_map_intersect_domain(res,
13218 isl_map_copy(pma->p[0].set));
13219 else
13220 res = isl_map_intersect_range(res,
13221 isl_map_copy(pma->p[0].set));
13223 for (i = 1; i < pma->n; ++i) {
13224 isl_map *res_i;
13226 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13227 isl_multi_aff_copy(pma->p[i].maff));
13228 if (type == isl_dim_in)
13229 res_i = isl_map_intersect_domain(res_i,
13230 isl_map_copy(pma->p[i].set));
13231 else
13232 res_i = isl_map_intersect_range(res_i,
13233 isl_map_copy(pma->p[i].set));
13234 res = isl_map_union(res, res_i);
13237 isl_pw_multi_aff_free(pma);
13238 isl_map_free(map);
13239 return res;
13240 error:
13241 isl_pw_multi_aff_free(pma);
13242 isl_map_free(map);
13243 return NULL;
13246 /* Compute the preimage of "map" under the function represented by "pma".
13247 * In other words, plug in "pma" in the domain or range of "map".
13248 * The result is a map that lives in the same space as "map",
13249 * except that the space of type "type" has been replaced by
13250 * the domain space of "pma".
13252 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13253 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13255 if (!map || !pma)
13256 goto error;
13258 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13259 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13261 if (!isl_space_has_named_params(map->dim) ||
13262 !isl_space_has_named_params(pma->dim))
13263 isl_die(map->ctx, isl_error_invalid,
13264 "unaligned unnamed parameters", goto error);
13265 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13266 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13268 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13269 error:
13270 isl_pw_multi_aff_free(pma);
13271 return isl_map_free(map);
13274 /* Compute the preimage of "set" under the function represented by "pma".
13275 * In other words, plug in "pma" in "set". The result is a set
13276 * that lives in the domain space of "pma".
13278 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13279 __isl_take isl_pw_multi_aff *pma)
13281 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13284 /* Compute the preimage of the domain of "map" under the function
13285 * represented by "pma".
13286 * In other words, plug in "pma" in the domain of "map".
13287 * The result is a map that lives in the same space as "map",
13288 * except that domain space has been replaced by the domain space of "pma".
13290 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13291 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13293 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13296 /* Compute the preimage of the range of "map" under the function
13297 * represented by "pma".
13298 * In other words, plug in "pma" in the range of "map".
13299 * The result is a map that lives in the same space as "map",
13300 * except that range space has been replaced by the domain space of "pma".
13302 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13303 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13305 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13308 /* Compute the preimage of "map" under the function represented by "mpa".
13309 * In other words, plug in "mpa" in the domain or range of "map".
13310 * The result is a map that lives in the same space as "map",
13311 * except that the space of type "type" has been replaced by
13312 * the domain space of "mpa".
13314 * If the map does not involve any constraints that refer to the
13315 * dimensions of the substituted space, then the only possible
13316 * effect of "mpa" on the map is to map the space to a different space.
13317 * We create a separate isl_multi_aff to effectuate this change
13318 * in order to avoid spurious splitting of the map along the pieces
13319 * of "mpa".
13321 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13322 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13324 int n;
13325 isl_pw_multi_aff *pma;
13327 if (!map || !mpa)
13328 goto error;
13330 n = isl_map_dim(map, type);
13331 if (!isl_map_involves_dims(map, type, 0, n)) {
13332 isl_space *space;
13333 isl_multi_aff *ma;
13335 space = isl_multi_pw_aff_get_space(mpa);
13336 isl_multi_pw_aff_free(mpa);
13337 ma = isl_multi_aff_zero(space);
13338 return isl_map_preimage_multi_aff(map, type, ma);
13341 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13342 return isl_map_preimage_pw_multi_aff(map, type, pma);
13343 error:
13344 isl_map_free(map);
13345 isl_multi_pw_aff_free(mpa);
13346 return NULL;
13349 /* Compute the preimage of "map" under the function represented by "mpa".
13350 * In other words, plug in "mpa" in the domain "map".
13351 * The result is a map that lives in the same space as "map",
13352 * except that domain space has been replaced by the domain space of "mpa".
13354 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13355 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13357 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13360 /* Compute the preimage of "set" by the function represented by "mpa".
13361 * In other words, plug in "mpa" in "set".
13363 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13364 __isl_take isl_multi_pw_aff *mpa)
13366 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13369 /* Are the "n" "coefficients" starting at "first" of the integer division
13370 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13371 * to each other?
13372 * The "coefficient" at position 0 is the denominator.
13373 * The "coefficient" at position 1 is the constant term.
13375 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13376 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13377 unsigned first, unsigned n)
13379 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13380 return isl_bool_error;
13381 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13382 return isl_bool_error;
13383 return isl_seq_eq(bmap1->div[pos1] + first,
13384 bmap2->div[pos2] + first, n);
13387 /* Are the integer division expressions at position "pos1" in "bmap1" and
13388 * "pos2" in "bmap2" equal to each other, except that the constant terms
13389 * are different?
13391 isl_bool isl_basic_map_equal_div_expr_except_constant(
13392 __isl_keep isl_basic_map *bmap1, int pos1,
13393 __isl_keep isl_basic_map *bmap2, int pos2)
13395 isl_bool equal;
13396 unsigned total;
13398 if (!bmap1 || !bmap2)
13399 return isl_bool_error;
13400 total = isl_basic_map_total_dim(bmap1);
13401 if (total != isl_basic_map_total_dim(bmap2))
13402 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13403 "incomparable div expressions", return isl_bool_error);
13404 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13405 0, 1);
13406 if (equal < 0 || !equal)
13407 return equal;
13408 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13409 1, 1);
13410 if (equal < 0 || equal)
13411 return isl_bool_not(equal);
13412 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13413 2, total);
13416 /* Replace the numerator of the constant term of the integer division
13417 * expression at position "div" in "bmap" by "value".
13418 * The caller guarantees that this does not change the meaning
13419 * of the input.
13421 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13422 __isl_take isl_basic_map *bmap, int div, int value)
13424 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13425 return isl_basic_map_free(bmap);
13427 isl_int_set_si(bmap->div[div][1], value);
13429 return bmap;
13432 /* Is the point "inner" internal to inequality constraint "ineq"
13433 * of "bset"?
13434 * The point is considered to be internal to the inequality constraint,
13435 * if it strictly lies on the positive side of the inequality constraint,
13436 * or if it lies on the constraint and the constraint is lexico-positive.
13438 static isl_bool is_internal(__isl_keep isl_vec *inner,
13439 __isl_keep isl_basic_set *bset, int ineq)
13441 isl_ctx *ctx;
13442 int pos;
13443 unsigned total;
13445 if (!inner || !bset)
13446 return isl_bool_error;
13448 ctx = isl_basic_set_get_ctx(bset);
13449 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13450 &ctx->normalize_gcd);
13451 if (!isl_int_is_zero(ctx->normalize_gcd))
13452 return isl_int_is_nonneg(ctx->normalize_gcd);
13454 total = isl_basic_set_dim(bset, isl_dim_all);
13455 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13456 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13459 /* Tighten the inequality constraints of "bset" that are outward with respect
13460 * to the point "vec".
13461 * That is, tighten the constraints that are not satisfied by "vec".
13463 * "vec" is a point internal to some superset S of "bset" that is used
13464 * to make the subsets of S disjoint, by tightening one half of the constraints
13465 * that separate two subsets. In particular, the constraints of S
13466 * are all satisfied by "vec" and should not be tightened.
13467 * Of the internal constraints, those that have "vec" on the outside
13468 * are tightened. The shared facet is included in the adjacent subset
13469 * with the opposite constraint.
13470 * For constraints that saturate "vec", this criterion cannot be used
13471 * to determine which of the two sides should be tightened.
13472 * Instead, the sign of the first non-zero coefficient is used
13473 * to make this choice. Note that this second criterion is never used
13474 * on the constraints of S since "vec" is interior to "S".
13476 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13477 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13479 int j;
13481 bset = isl_basic_set_cow(bset);
13482 if (!bset)
13483 return NULL;
13484 for (j = 0; j < bset->n_ineq; ++j) {
13485 isl_bool internal;
13487 internal = is_internal(vec, bset, j);
13488 if (internal < 0)
13489 return isl_basic_set_free(bset);
13490 if (internal)
13491 continue;
13492 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13495 return bset;