isl_ctx_reset_error: handle NULL input
[isl.git] / isl_map.c
blobbc80599ad8d357376cd01012b4f5c9283d826dbb
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_i;
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 /* Insert an extra integer division, prescribed by "div", to "bmap"
1487 * at (integer division) position "pos".
1489 * The integer division is first added at the end and then moved
1490 * into the right position.
1492 __isl_give isl_basic_map *isl_basic_map_insert_div(
1493 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1495 int i, k, n_div;
1497 bmap = isl_basic_map_cow(bmap);
1498 if (!bmap || !div)
1499 return isl_basic_map_free(bmap);
1501 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1502 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1503 "unexpected size", return isl_basic_map_free(bmap));
1504 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1505 if (pos < 0 || pos > n_div)
1506 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1507 "invalid position", return isl_basic_map_free(bmap));
1509 bmap = isl_basic_map_extend_space(bmap,
1510 isl_basic_map_get_space(bmap), 1, 0, 2);
1511 k = isl_basic_map_alloc_div(bmap);
1512 if (k < 0)
1513 return isl_basic_map_free(bmap);
1514 isl_seq_cpy(bmap->div[k], div->el, div->size);
1515 isl_int_set_si(bmap->div[k][div->size], 0);
1517 for (i = k; i > pos; --i)
1518 isl_basic_map_swap_div(bmap, i, i - 1);
1520 return bmap;
1523 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1525 if (!bmap)
1526 return -1;
1527 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1528 bmap->n_div -= n;
1529 return 0;
1532 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1534 return isl_basic_map_free_div(bset_to_bmap(bset), n);
1537 /* Copy constraint from src to dst, putting the vars of src at offset
1538 * dim_off in dst and the divs of src at offset div_off in dst.
1539 * If both sets are actually map, then dim_off applies to the input
1540 * variables.
1542 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1543 struct isl_basic_map *src_map, isl_int *src,
1544 unsigned in_off, unsigned out_off, unsigned div_off)
1546 unsigned src_nparam = isl_basic_map_n_param(src_map);
1547 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1548 unsigned src_in = isl_basic_map_n_in(src_map);
1549 unsigned dst_in = isl_basic_map_n_in(dst_map);
1550 unsigned src_out = isl_basic_map_n_out(src_map);
1551 unsigned dst_out = isl_basic_map_n_out(dst_map);
1552 isl_int_set(dst[0], src[0]);
1553 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1554 if (dst_nparam > src_nparam)
1555 isl_seq_clr(dst+1+src_nparam,
1556 dst_nparam - src_nparam);
1557 isl_seq_clr(dst+1+dst_nparam, in_off);
1558 isl_seq_cpy(dst+1+dst_nparam+in_off,
1559 src+1+src_nparam,
1560 isl_min(dst_in-in_off, src_in));
1561 if (dst_in-in_off > src_in)
1562 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1563 dst_in - in_off - src_in);
1564 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1565 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1566 src+1+src_nparam+src_in,
1567 isl_min(dst_out-out_off, src_out));
1568 if (dst_out-out_off > src_out)
1569 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1570 dst_out - out_off - src_out);
1571 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1572 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1573 src+1+src_nparam+src_in+src_out,
1574 isl_min(dst_map->extra-div_off, src_map->n_div));
1575 if (dst_map->n_div-div_off > src_map->n_div)
1576 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1577 div_off+src_map->n_div,
1578 dst_map->n_div - div_off - src_map->n_div);
1581 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1582 struct isl_basic_map *src_map, isl_int *src,
1583 unsigned in_off, unsigned out_off, unsigned div_off)
1585 isl_int_set(dst[0], src[0]);
1586 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1589 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1590 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1592 int i;
1593 unsigned div_off;
1595 if (!bmap1 || !bmap2)
1596 goto error;
1598 div_off = bmap1->n_div;
1600 for (i = 0; i < bmap2->n_eq; ++i) {
1601 int i1 = isl_basic_map_alloc_equality(bmap1);
1602 if (i1 < 0)
1603 goto error;
1604 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1605 i_pos, o_pos, div_off);
1608 for (i = 0; i < bmap2->n_ineq; ++i) {
1609 int i1 = isl_basic_map_alloc_inequality(bmap1);
1610 if (i1 < 0)
1611 goto error;
1612 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1613 i_pos, o_pos, div_off);
1616 for (i = 0; i < bmap2->n_div; ++i) {
1617 int i1 = isl_basic_map_alloc_div(bmap1);
1618 if (i1 < 0)
1619 goto error;
1620 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1621 i_pos, o_pos, div_off);
1624 isl_basic_map_free(bmap2);
1626 return bmap1;
1628 error:
1629 isl_basic_map_free(bmap1);
1630 isl_basic_map_free(bmap2);
1631 return NULL;
1634 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1635 struct isl_basic_set *bset2, unsigned pos)
1637 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1638 bset_to_bmap(bset2), 0, pos));
1641 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1642 __isl_take isl_space *dim, unsigned extra,
1643 unsigned n_eq, unsigned n_ineq)
1645 struct isl_basic_map *ext;
1646 unsigned flags;
1647 int dims_ok;
1649 if (!dim)
1650 goto error;
1652 if (!base)
1653 goto error;
1655 dims_ok = isl_space_is_equal(base->dim, dim) &&
1656 base->extra >= base->n_div + extra;
1658 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1659 room_for_ineq(base, n_ineq)) {
1660 isl_space_free(dim);
1661 return base;
1664 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1665 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1666 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1667 extra += base->extra;
1668 n_eq += base->n_eq;
1669 n_ineq += base->n_ineq;
1671 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1672 dim = NULL;
1673 if (!ext)
1674 goto error;
1676 if (dims_ok)
1677 ext->sample = isl_vec_copy(base->sample);
1678 flags = base->flags;
1679 ext = add_constraints(ext, base, 0, 0);
1680 if (ext) {
1681 ext->flags = flags;
1682 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1685 return ext;
1687 error:
1688 isl_space_free(dim);
1689 isl_basic_map_free(base);
1690 return NULL;
1693 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1694 __isl_take isl_space *dim, unsigned extra,
1695 unsigned n_eq, unsigned n_ineq)
1697 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1698 dim, extra, n_eq, n_ineq));
1701 struct isl_basic_map *isl_basic_map_extend_constraints(
1702 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1704 if (!base)
1705 return NULL;
1706 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1707 0, n_eq, n_ineq);
1710 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1711 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1712 unsigned n_eq, unsigned n_ineq)
1714 struct isl_basic_map *bmap;
1715 isl_space *dim;
1717 if (!base)
1718 return NULL;
1719 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1720 if (!dim)
1721 goto error;
1723 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1724 return bmap;
1725 error:
1726 isl_basic_map_free(base);
1727 return NULL;
1730 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1731 unsigned nparam, unsigned dim, unsigned extra,
1732 unsigned n_eq, unsigned n_ineq)
1734 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1735 nparam, 0, dim, extra, n_eq, n_ineq));
1738 struct isl_basic_set *isl_basic_set_extend_constraints(
1739 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1741 isl_basic_map *bmap = bset_to_bmap(base);
1742 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1743 return bset_from_bmap(bmap);
1746 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1748 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1751 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1753 if (!bmap)
1754 return NULL;
1756 if (bmap->ref > 1) {
1757 bmap->ref--;
1758 bmap = isl_basic_map_dup(bmap);
1760 if (bmap) {
1761 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1762 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1764 return bmap;
1767 /* Clear all cached information in "map", either because it is about
1768 * to be modified or because it is being freed.
1769 * Always return the same pointer that is passed in.
1770 * This is needed for the use in isl_map_free.
1772 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1774 isl_basic_map_free(map->cached_simple_hull[0]);
1775 isl_basic_map_free(map->cached_simple_hull[1]);
1776 map->cached_simple_hull[0] = NULL;
1777 map->cached_simple_hull[1] = NULL;
1778 return map;
1781 struct isl_set *isl_set_cow(struct isl_set *set)
1783 return isl_map_cow(set);
1786 /* Return an isl_map that is equal to "map" and that has only
1787 * a single reference.
1789 * If the original input already has only one reference, then
1790 * simply return it, but clear all cached information, since
1791 * it may be rendered invalid by the operations that will be
1792 * performed on the result.
1794 * Otherwise, create a duplicate (without any cached information).
1796 struct isl_map *isl_map_cow(struct isl_map *map)
1798 if (!map)
1799 return NULL;
1801 if (map->ref == 1)
1802 return clear_caches(map);
1803 map->ref--;
1804 return isl_map_dup(map);
1807 static void swap_vars(struct isl_blk blk, isl_int *a,
1808 unsigned a_len, unsigned b_len)
1810 isl_seq_cpy(blk.data, a+a_len, b_len);
1811 isl_seq_cpy(blk.data+b_len, a, a_len);
1812 isl_seq_cpy(a, blk.data, b_len+a_len);
1815 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1816 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1818 int i;
1819 struct isl_blk blk;
1821 if (!bmap)
1822 goto error;
1824 isl_assert(bmap->ctx,
1825 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1827 if (n1 == 0 || n2 == 0)
1828 return bmap;
1830 bmap = isl_basic_map_cow(bmap);
1831 if (!bmap)
1832 return NULL;
1834 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1835 if (isl_blk_is_error(blk))
1836 goto error;
1838 for (i = 0; i < bmap->n_eq; ++i)
1839 swap_vars(blk,
1840 bmap->eq[i] + pos, n1, n2);
1842 for (i = 0; i < bmap->n_ineq; ++i)
1843 swap_vars(blk,
1844 bmap->ineq[i] + pos, n1, n2);
1846 for (i = 0; i < bmap->n_div; ++i)
1847 swap_vars(blk,
1848 bmap->div[i]+1 + pos, n1, n2);
1850 isl_blk_free(bmap->ctx, blk);
1852 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1853 bmap = isl_basic_map_gauss(bmap, NULL);
1854 return isl_basic_map_finalize(bmap);
1855 error:
1856 isl_basic_map_free(bmap);
1857 return NULL;
1860 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1862 int i = 0;
1863 unsigned total;
1864 if (!bmap)
1865 goto error;
1866 total = isl_basic_map_total_dim(bmap);
1867 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1868 return isl_basic_map_free(bmap);
1869 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1870 if (bmap->n_eq > 0)
1871 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1872 else {
1873 i = isl_basic_map_alloc_equality(bmap);
1874 if (i < 0)
1875 goto error;
1877 isl_int_set_si(bmap->eq[i][0], 1);
1878 isl_seq_clr(bmap->eq[i]+1, total);
1879 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1880 isl_vec_free(bmap->sample);
1881 bmap->sample = NULL;
1882 return isl_basic_map_finalize(bmap);
1883 error:
1884 isl_basic_map_free(bmap);
1885 return NULL;
1888 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1890 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1893 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1894 * of "bmap").
1896 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1898 isl_int *t = bmap->div[a];
1899 bmap->div[a] = bmap->div[b];
1900 bmap->div[b] = t;
1903 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1904 * div definitions accordingly.
1906 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1908 int i;
1909 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1911 swap_div(bmap, a, b);
1913 for (i = 0; i < bmap->n_eq; ++i)
1914 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1916 for (i = 0; i < bmap->n_ineq; ++i)
1917 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1919 for (i = 0; i < bmap->n_div; ++i)
1920 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1921 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1924 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1925 * div definitions accordingly.
1927 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1929 isl_basic_map_swap_div(bset, a, b);
1932 /* Eliminate the specified n dimensions starting at first from the
1933 * constraints, without removing the dimensions from the space.
1934 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1936 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1937 enum isl_dim_type type, unsigned first, unsigned n)
1939 int i;
1941 if (!map)
1942 return NULL;
1943 if (n == 0)
1944 return map;
1946 if (first + n > isl_map_dim(map, type) || first + n < first)
1947 isl_die(map->ctx, isl_error_invalid,
1948 "index out of bounds", goto error);
1950 map = isl_map_cow(map);
1951 if (!map)
1952 return NULL;
1954 for (i = 0; i < map->n; ++i) {
1955 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1956 if (!map->p[i])
1957 goto error;
1959 return map;
1960 error:
1961 isl_map_free(map);
1962 return NULL;
1965 /* Eliminate the specified n dimensions starting at first from the
1966 * constraints, without removing the dimensions from the space.
1967 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1969 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1970 enum isl_dim_type type, unsigned first, unsigned n)
1972 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1975 /* Eliminate the specified n dimensions starting at first from the
1976 * constraints, without removing the dimensions from the space.
1977 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1979 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1980 unsigned first, unsigned n)
1982 return isl_set_eliminate(set, isl_dim_set, first, n);
1985 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1986 __isl_take isl_basic_map *bmap)
1988 if (!bmap)
1989 return NULL;
1990 bmap = isl_basic_map_eliminate_vars(bmap,
1991 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1992 if (!bmap)
1993 return NULL;
1994 bmap->n_div = 0;
1995 return isl_basic_map_finalize(bmap);
1998 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1999 __isl_take isl_basic_set *bset)
2001 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2004 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2006 int i;
2008 if (!map)
2009 return NULL;
2010 if (map->n == 0)
2011 return map;
2013 map = isl_map_cow(map);
2014 if (!map)
2015 return NULL;
2017 for (i = 0; i < map->n; ++i) {
2018 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2019 if (!map->p[i])
2020 goto error;
2022 return map;
2023 error:
2024 isl_map_free(map);
2025 return NULL;
2028 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2030 return isl_map_remove_divs(set);
2033 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2034 enum isl_dim_type type, unsigned first, unsigned n)
2036 if (!bmap)
2037 return NULL;
2038 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2039 goto error);
2040 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2041 return bmap;
2042 bmap = isl_basic_map_eliminate_vars(bmap,
2043 isl_basic_map_offset(bmap, type) - 1 + first, n);
2044 if (!bmap)
2045 return bmap;
2046 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2047 return bmap;
2048 bmap = isl_basic_map_drop(bmap, type, first, n);
2049 return bmap;
2050 error:
2051 isl_basic_map_free(bmap);
2052 return NULL;
2055 /* Return true if the definition of the given div (recursively) involves
2056 * any of the given variables.
2058 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2059 unsigned first, unsigned n)
2061 int i;
2062 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2064 if (isl_int_is_zero(bmap->div[div][0]))
2065 return 0;
2066 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2067 return 1;
2069 for (i = bmap->n_div - 1; i >= 0; --i) {
2070 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2071 continue;
2072 if (div_involves_vars(bmap, i, first, n))
2073 return 1;
2076 return 0;
2079 /* Try and add a lower and/or upper bound on "div" to "bmap"
2080 * based on inequality "i".
2081 * "total" is the total number of variables (excluding the divs).
2082 * "v" is a temporary object that can be used during the calculations.
2083 * If "lb" is set, then a lower bound should be constructed.
2084 * If "ub" is set, then an upper bound should be constructed.
2086 * The calling function has already checked that the inequality does not
2087 * reference "div", but we still need to check that the inequality is
2088 * of the right form. We'll consider the case where we want to construct
2089 * a lower bound. The construction of upper bounds is similar.
2091 * Let "div" be of the form
2093 * q = floor((a + f(x))/d)
2095 * We essentially check if constraint "i" is of the form
2097 * b + f(x) >= 0
2099 * so that we can use it to derive a lower bound on "div".
2100 * However, we allow a slightly more general form
2102 * b + g(x) >= 0
2104 * with the condition that the coefficients of g(x) - f(x) are all
2105 * divisible by d.
2106 * Rewriting this constraint as
2108 * 0 >= -b - g(x)
2110 * adding a + f(x) to both sides and dividing by d, we obtain
2112 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2114 * Taking the floor on both sides, we obtain
2116 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2118 * or
2120 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2122 * In the case of an upper bound, we construct the constraint
2124 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2127 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2128 __isl_take isl_basic_map *bmap, int div, int i,
2129 unsigned total, isl_int v, int lb, int ub)
2131 int j;
2133 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2134 if (lb) {
2135 isl_int_sub(v, bmap->ineq[i][1 + j],
2136 bmap->div[div][1 + 1 + j]);
2137 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2139 if (ub) {
2140 isl_int_add(v, bmap->ineq[i][1 + j],
2141 bmap->div[div][1 + 1 + j]);
2142 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2145 if (!lb && !ub)
2146 return bmap;
2148 bmap = isl_basic_map_cow(bmap);
2149 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2150 if (lb) {
2151 int k = isl_basic_map_alloc_inequality(bmap);
2152 if (k < 0)
2153 goto error;
2154 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2155 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2156 bmap->div[div][1 + j]);
2157 isl_int_cdiv_q(bmap->ineq[k][j],
2158 bmap->ineq[k][j], bmap->div[div][0]);
2160 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2162 if (ub) {
2163 int k = isl_basic_map_alloc_inequality(bmap);
2164 if (k < 0)
2165 goto error;
2166 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2167 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2168 bmap->div[div][1 + j]);
2169 isl_int_fdiv_q(bmap->ineq[k][j],
2170 bmap->ineq[k][j], bmap->div[div][0]);
2172 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2175 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2176 return bmap;
2177 error:
2178 isl_basic_map_free(bmap);
2179 return NULL;
2182 /* This function is called right before "div" is eliminated from "bmap"
2183 * using Fourier-Motzkin.
2184 * Look through the constraints of "bmap" for constraints on the argument
2185 * of the integer division and use them to construct constraints on the
2186 * integer division itself. These constraints can then be combined
2187 * during the Fourier-Motzkin elimination.
2188 * Note that it is only useful to introduce lower bounds on "div"
2189 * if "bmap" already contains upper bounds on "div" as the newly
2190 * introduce lower bounds can then be combined with the pre-existing
2191 * upper bounds. Similarly for upper bounds.
2192 * We therefore first check if "bmap" contains any lower and/or upper bounds
2193 * on "div".
2195 * It is interesting to note that the introduction of these constraints
2196 * can indeed lead to more accurate results, even when compared to
2197 * deriving constraints on the argument of "div" from constraints on "div".
2198 * Consider, for example, the set
2200 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2202 * The second constraint can be rewritten as
2204 * 2 * [(-i-2j+3)/4] + k >= 0
2206 * from which we can derive
2208 * -i - 2j + 3 >= -2k
2210 * or
2212 * i + 2j <= 3 + 2k
2214 * Combined with the first constraint, we obtain
2216 * -3 <= 3 + 2k or k >= -3
2218 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2219 * the first constraint, we obtain
2221 * [(i + 2j)/4] >= [-3/4] = -1
2223 * Combining this constraint with the second constraint, we obtain
2225 * k >= -2
2227 static __isl_give isl_basic_map *insert_bounds_on_div(
2228 __isl_take isl_basic_map *bmap, int div)
2230 int i;
2231 int check_lb, check_ub;
2232 isl_int v;
2233 unsigned total;
2235 if (!bmap)
2236 return NULL;
2238 if (isl_int_is_zero(bmap->div[div][0]))
2239 return bmap;
2241 total = isl_space_dim(bmap->dim, isl_dim_all);
2243 check_lb = 0;
2244 check_ub = 0;
2245 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2246 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2247 if (s > 0)
2248 check_ub = 1;
2249 if (s < 0)
2250 check_lb = 1;
2253 if (!check_lb && !check_ub)
2254 return bmap;
2256 isl_int_init(v);
2258 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2259 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2260 continue;
2262 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2263 check_lb, check_ub);
2266 isl_int_clear(v);
2268 return bmap;
2271 /* Remove all divs (recursively) involving any of the given dimensions
2272 * in their definitions.
2274 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2275 __isl_take isl_basic_map *bmap,
2276 enum isl_dim_type type, unsigned first, unsigned n)
2278 int i;
2280 if (!bmap)
2281 return NULL;
2282 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2283 goto error);
2284 first += isl_basic_map_offset(bmap, type);
2286 for (i = bmap->n_div - 1; i >= 0; --i) {
2287 if (!div_involves_vars(bmap, i, first, n))
2288 continue;
2289 bmap = insert_bounds_on_div(bmap, i);
2290 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2291 if (!bmap)
2292 return NULL;
2293 i = bmap->n_div;
2296 return bmap;
2297 error:
2298 isl_basic_map_free(bmap);
2299 return NULL;
2302 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2303 __isl_take isl_basic_set *bset,
2304 enum isl_dim_type type, unsigned first, unsigned n)
2306 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2309 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2310 enum isl_dim_type type, unsigned first, unsigned n)
2312 int i;
2314 if (!map)
2315 return NULL;
2316 if (map->n == 0)
2317 return map;
2319 map = isl_map_cow(map);
2320 if (!map)
2321 return NULL;
2323 for (i = 0; i < map->n; ++i) {
2324 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2325 type, first, n);
2326 if (!map->p[i])
2327 goto error;
2329 return map;
2330 error:
2331 isl_map_free(map);
2332 return NULL;
2335 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2336 enum isl_dim_type type, unsigned first, unsigned n)
2338 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2339 type, first, n));
2342 /* Does the description of "bmap" depend on the specified dimensions?
2343 * We also check whether the dimensions appear in any of the div definitions.
2344 * In principle there is no need for this check. If the dimensions appear
2345 * in a div definition, they also appear in the defining constraints of that
2346 * div.
2348 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2349 enum isl_dim_type type, unsigned first, unsigned n)
2351 int i;
2353 if (!bmap)
2354 return isl_bool_error;
2356 if (first + n > isl_basic_map_dim(bmap, type))
2357 isl_die(bmap->ctx, isl_error_invalid,
2358 "index out of bounds", return isl_bool_error);
2360 first += isl_basic_map_offset(bmap, type);
2361 for (i = 0; i < bmap->n_eq; ++i)
2362 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2363 return isl_bool_true;
2364 for (i = 0; i < bmap->n_ineq; ++i)
2365 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2366 return isl_bool_true;
2367 for (i = 0; i < bmap->n_div; ++i) {
2368 if (isl_int_is_zero(bmap->div[i][0]))
2369 continue;
2370 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2371 return isl_bool_true;
2374 return isl_bool_false;
2377 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2378 enum isl_dim_type type, unsigned first, unsigned n)
2380 int i;
2382 if (!map)
2383 return isl_bool_error;
2385 if (first + n > isl_map_dim(map, type))
2386 isl_die(map->ctx, isl_error_invalid,
2387 "index out of bounds", return isl_bool_error);
2389 for (i = 0; i < map->n; ++i) {
2390 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2391 type, first, n);
2392 if (involves < 0 || involves)
2393 return involves;
2396 return isl_bool_false;
2399 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2400 enum isl_dim_type type, unsigned first, unsigned n)
2402 return isl_basic_map_involves_dims(bset, type, first, n);
2405 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2406 enum isl_dim_type type, unsigned first, unsigned n)
2408 return isl_map_involves_dims(set, type, first, n);
2411 /* Does local variable "div" of "bmap" have a complete explicit representation?
2412 * Having a complete explicit representation requires not only
2413 * an explicit representation, but also that all local variables
2414 * that appear in this explicit representation in turn have
2415 * a complete explicit representation.
2417 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2419 int i;
2420 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2421 isl_bool marked;
2423 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2424 if (marked < 0 || marked)
2425 return isl_bool_not(marked);
2427 for (i = bmap->n_div - 1; i >= 0; --i) {
2428 isl_bool known;
2430 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2431 continue;
2432 known = isl_basic_map_div_is_known(bmap, i);
2433 if (known < 0 || !known)
2434 return known;
2437 return isl_bool_true;
2440 /* Does local variable "div" of "bset" have a complete explicit representation?
2442 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2444 return isl_basic_map_div_is_known(bset, div);
2447 /* Remove all divs that are unknown or defined in terms of unknown divs.
2449 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2450 __isl_take isl_basic_map *bmap)
2452 int i;
2454 if (!bmap)
2455 return NULL;
2457 for (i = bmap->n_div - 1; i >= 0; --i) {
2458 if (isl_basic_map_div_is_known(bmap, i))
2459 continue;
2460 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2461 if (!bmap)
2462 return NULL;
2463 i = bmap->n_div;
2466 return bmap;
2469 /* Remove all divs that are unknown or defined in terms of unknown divs.
2471 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2472 __isl_take isl_basic_set *bset)
2474 return isl_basic_map_remove_unknown_divs(bset);
2477 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2479 int i;
2481 if (!map)
2482 return NULL;
2483 if (map->n == 0)
2484 return map;
2486 map = isl_map_cow(map);
2487 if (!map)
2488 return NULL;
2490 for (i = 0; i < map->n; ++i) {
2491 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2492 if (!map->p[i])
2493 goto error;
2495 return map;
2496 error:
2497 isl_map_free(map);
2498 return NULL;
2501 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2503 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2506 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2507 __isl_take isl_basic_set *bset,
2508 enum isl_dim_type type, unsigned first, unsigned n)
2510 isl_basic_map *bmap = bset_to_bmap(bset);
2511 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2512 return bset_from_bmap(bmap);
2515 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2516 enum isl_dim_type type, unsigned first, unsigned n)
2518 int i;
2520 if (n == 0)
2521 return map;
2523 map = isl_map_cow(map);
2524 if (!map)
2525 return NULL;
2526 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2528 for (i = 0; i < map->n; ++i) {
2529 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2530 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2531 if (!map->p[i])
2532 goto error;
2534 map = isl_map_drop(map, type, first, n);
2535 return map;
2536 error:
2537 isl_map_free(map);
2538 return NULL;
2541 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2542 enum isl_dim_type type, unsigned first, unsigned n)
2544 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2545 type, first, n));
2548 /* Project out n inputs starting at first using Fourier-Motzkin */
2549 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2550 unsigned first, unsigned n)
2552 return isl_map_remove_dims(map, isl_dim_in, first, n);
2555 static void dump_term(struct isl_basic_map *bmap,
2556 isl_int c, int pos, FILE *out)
2558 const char *name;
2559 unsigned in = isl_basic_map_n_in(bmap);
2560 unsigned dim = in + isl_basic_map_n_out(bmap);
2561 unsigned nparam = isl_basic_map_n_param(bmap);
2562 if (!pos)
2563 isl_int_print(out, c, 0);
2564 else {
2565 if (!isl_int_is_one(c))
2566 isl_int_print(out, c, 0);
2567 if (pos < 1 + nparam) {
2568 name = isl_space_get_dim_name(bmap->dim,
2569 isl_dim_param, pos - 1);
2570 if (name)
2571 fprintf(out, "%s", name);
2572 else
2573 fprintf(out, "p%d", pos - 1);
2574 } else if (pos < 1 + nparam + in)
2575 fprintf(out, "i%d", pos - 1 - nparam);
2576 else if (pos < 1 + nparam + dim)
2577 fprintf(out, "o%d", pos - 1 - nparam - in);
2578 else
2579 fprintf(out, "e%d", pos - 1 - nparam - dim);
2583 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2584 int sign, FILE *out)
2586 int i;
2587 int first;
2588 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2589 isl_int v;
2591 isl_int_init(v);
2592 for (i = 0, first = 1; i < len; ++i) {
2593 if (isl_int_sgn(c[i]) * sign <= 0)
2594 continue;
2595 if (!first)
2596 fprintf(out, " + ");
2597 first = 0;
2598 isl_int_abs(v, c[i]);
2599 dump_term(bmap, v, i, out);
2601 isl_int_clear(v);
2602 if (first)
2603 fprintf(out, "0");
2606 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2607 const char *op, FILE *out, int indent)
2609 int i;
2611 fprintf(out, "%*s", indent, "");
2613 dump_constraint_sign(bmap, c, 1, out);
2614 fprintf(out, " %s ", op);
2615 dump_constraint_sign(bmap, c, -1, out);
2617 fprintf(out, "\n");
2619 for (i = bmap->n_div; i < bmap->extra; ++i) {
2620 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2621 continue;
2622 fprintf(out, "%*s", indent, "");
2623 fprintf(out, "ERROR: unused div coefficient not zero\n");
2624 abort();
2628 static void dump_constraints(struct isl_basic_map *bmap,
2629 isl_int **c, unsigned n,
2630 const char *op, FILE *out, int indent)
2632 int i;
2634 for (i = 0; i < n; ++i)
2635 dump_constraint(bmap, c[i], op, out, indent);
2638 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2640 int j;
2641 int first = 1;
2642 unsigned total = isl_basic_map_total_dim(bmap);
2644 for (j = 0; j < 1 + total; ++j) {
2645 if (isl_int_is_zero(exp[j]))
2646 continue;
2647 if (!first && isl_int_is_pos(exp[j]))
2648 fprintf(out, "+");
2649 dump_term(bmap, exp[j], j, out);
2650 first = 0;
2654 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2656 int i;
2658 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2659 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2661 for (i = 0; i < bmap->n_div; ++i) {
2662 fprintf(out, "%*s", indent, "");
2663 fprintf(out, "e%d = [(", i);
2664 dump_affine(bmap, bmap->div[i]+1, out);
2665 fprintf(out, ")/");
2666 isl_int_print(out, bmap->div[i][0], 0);
2667 fprintf(out, "]\n");
2671 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2672 FILE *out, int indent)
2674 if (!bset) {
2675 fprintf(out, "null basic set\n");
2676 return;
2679 fprintf(out, "%*s", indent, "");
2680 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2681 bset->ref, bset->dim->nparam, bset->dim->n_out,
2682 bset->extra, bset->flags);
2683 dump(bset_to_bmap(bset), out, indent);
2686 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2687 FILE *out, int indent)
2689 if (!bmap) {
2690 fprintf(out, "null basic map\n");
2691 return;
2694 fprintf(out, "%*s", indent, "");
2695 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2696 "flags: %x, n_name: %d\n",
2697 bmap->ref,
2698 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2699 bmap->extra, bmap->flags, bmap->dim->n_id);
2700 dump(bmap, out, indent);
2703 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2705 unsigned total;
2706 if (!bmap)
2707 return -1;
2708 total = isl_basic_map_total_dim(bmap);
2709 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2710 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2711 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2712 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2713 return 0;
2716 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2717 unsigned flags)
2719 if (!space)
2720 return NULL;
2721 if (isl_space_dim(space, isl_dim_in) != 0)
2722 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2723 "set cannot have input dimensions", goto error);
2724 return isl_map_alloc_space(space, n, flags);
2725 error:
2726 isl_space_free(space);
2727 return NULL;
2730 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2731 unsigned nparam, unsigned dim, int n, unsigned flags)
2733 struct isl_set *set;
2734 isl_space *dims;
2736 dims = isl_space_alloc(ctx, nparam, 0, dim);
2737 if (!dims)
2738 return NULL;
2740 set = isl_set_alloc_space(dims, n, flags);
2741 return set;
2744 /* Make sure "map" has room for at least "n" more basic maps.
2746 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2748 int i;
2749 struct isl_map *grown = NULL;
2751 if (!map)
2752 return NULL;
2753 isl_assert(map->ctx, n >= 0, goto error);
2754 if (map->n + n <= map->size)
2755 return map;
2756 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2757 if (!grown)
2758 goto error;
2759 for (i = 0; i < map->n; ++i) {
2760 grown->p[i] = isl_basic_map_copy(map->p[i]);
2761 if (!grown->p[i])
2762 goto error;
2763 grown->n++;
2765 isl_map_free(map);
2766 return grown;
2767 error:
2768 isl_map_free(grown);
2769 isl_map_free(map);
2770 return NULL;
2773 /* Make sure "set" has room for at least "n" more basic sets.
2775 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2777 return set_from_map(isl_map_grow(set_to_map(set), n));
2780 struct isl_set *isl_set_dup(struct isl_set *set)
2782 int i;
2783 struct isl_set *dup;
2785 if (!set)
2786 return NULL;
2788 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2789 if (!dup)
2790 return NULL;
2791 for (i = 0; i < set->n; ++i)
2792 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2793 return dup;
2796 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2798 return isl_map_from_basic_map(bset);
2801 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2803 struct isl_map *map;
2805 if (!bmap)
2806 return NULL;
2808 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2809 return isl_map_add_basic_map(map, bmap);
2812 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2813 __isl_take isl_basic_set *bset)
2815 return set_from_map(isl_map_add_basic_map(set_to_map(set),
2816 bset_to_bmap(bset)));
2819 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2821 return isl_map_free(set);
2824 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2826 int i;
2828 if (!set) {
2829 fprintf(out, "null set\n");
2830 return;
2833 fprintf(out, "%*s", indent, "");
2834 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2835 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2836 set->flags);
2837 for (i = 0; i < set->n; ++i) {
2838 fprintf(out, "%*s", indent, "");
2839 fprintf(out, "basic set %d:\n", i);
2840 isl_basic_set_print_internal(set->p[i], out, indent+4);
2844 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2846 int i;
2848 if (!map) {
2849 fprintf(out, "null map\n");
2850 return;
2853 fprintf(out, "%*s", indent, "");
2854 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2855 "flags: %x, n_name: %d\n",
2856 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2857 map->dim->n_out, map->flags, map->dim->n_id);
2858 for (i = 0; i < map->n; ++i) {
2859 fprintf(out, "%*s", indent, "");
2860 fprintf(out, "basic map %d:\n", i);
2861 isl_basic_map_print_internal(map->p[i], out, indent+4);
2865 struct isl_basic_map *isl_basic_map_intersect_domain(
2866 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2868 struct isl_basic_map *bmap_domain;
2870 if (!bmap || !bset)
2871 goto error;
2873 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2874 bset->dim, isl_dim_param), goto error);
2876 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2877 isl_assert(bset->ctx,
2878 isl_basic_map_compatible_domain(bmap, bset), goto error);
2880 bmap = isl_basic_map_cow(bmap);
2881 if (!bmap)
2882 goto error;
2883 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2884 bset->n_div, bset->n_eq, bset->n_ineq);
2885 bmap_domain = isl_basic_map_from_domain(bset);
2886 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2888 bmap = isl_basic_map_simplify(bmap);
2889 return isl_basic_map_finalize(bmap);
2890 error:
2891 isl_basic_map_free(bmap);
2892 isl_basic_set_free(bset);
2893 return NULL;
2896 struct isl_basic_map *isl_basic_map_intersect_range(
2897 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2899 struct isl_basic_map *bmap_range;
2901 if (!bmap || !bset)
2902 goto error;
2904 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2905 bset->dim, isl_dim_param), goto error);
2907 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2908 isl_assert(bset->ctx,
2909 isl_basic_map_compatible_range(bmap, bset), goto error);
2911 if (isl_basic_set_plain_is_universe(bset)) {
2912 isl_basic_set_free(bset);
2913 return bmap;
2916 bmap = isl_basic_map_cow(bmap);
2917 if (!bmap)
2918 goto error;
2919 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2920 bset->n_div, bset->n_eq, bset->n_ineq);
2921 bmap_range = bset_to_bmap(bset);
2922 bmap = add_constraints(bmap, bmap_range, 0, 0);
2924 bmap = isl_basic_map_simplify(bmap);
2925 return isl_basic_map_finalize(bmap);
2926 error:
2927 isl_basic_map_free(bmap);
2928 isl_basic_set_free(bset);
2929 return NULL;
2932 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2933 __isl_keep isl_vec *vec)
2935 int i;
2936 unsigned total;
2937 isl_int s;
2939 if (!bmap || !vec)
2940 return isl_bool_error;
2942 total = 1 + isl_basic_map_total_dim(bmap);
2943 if (total != vec->size)
2944 return isl_bool_false;
2946 isl_int_init(s);
2948 for (i = 0; i < bmap->n_eq; ++i) {
2949 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2950 if (!isl_int_is_zero(s)) {
2951 isl_int_clear(s);
2952 return isl_bool_false;
2956 for (i = 0; i < bmap->n_ineq; ++i) {
2957 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2958 if (isl_int_is_neg(s)) {
2959 isl_int_clear(s);
2960 return isl_bool_false;
2964 isl_int_clear(s);
2966 return isl_bool_true;
2969 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2970 __isl_keep isl_vec *vec)
2972 return isl_basic_map_contains(bset_to_bmap(bset), vec);
2975 struct isl_basic_map *isl_basic_map_intersect(
2976 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2978 struct isl_vec *sample = NULL;
2980 if (!bmap1 || !bmap2)
2981 goto error;
2983 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2984 bmap2->dim, isl_dim_param), goto error);
2985 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2986 isl_space_dim(bmap1->dim, isl_dim_param) &&
2987 isl_space_dim(bmap2->dim, isl_dim_all) !=
2988 isl_space_dim(bmap2->dim, isl_dim_param))
2989 return isl_basic_map_intersect(bmap2, bmap1);
2991 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2992 isl_space_dim(bmap2->dim, isl_dim_param))
2993 isl_assert(bmap1->ctx,
2994 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2996 if (isl_basic_map_plain_is_empty(bmap1)) {
2997 isl_basic_map_free(bmap2);
2998 return bmap1;
3000 if (isl_basic_map_plain_is_empty(bmap2)) {
3001 isl_basic_map_free(bmap1);
3002 return bmap2;
3005 if (bmap1->sample &&
3006 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3007 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3008 sample = isl_vec_copy(bmap1->sample);
3009 else if (bmap2->sample &&
3010 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3011 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3012 sample = isl_vec_copy(bmap2->sample);
3014 bmap1 = isl_basic_map_cow(bmap1);
3015 if (!bmap1)
3016 goto error;
3017 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3018 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3019 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3021 if (!bmap1)
3022 isl_vec_free(sample);
3023 else if (sample) {
3024 isl_vec_free(bmap1->sample);
3025 bmap1->sample = sample;
3028 bmap1 = isl_basic_map_simplify(bmap1);
3029 return isl_basic_map_finalize(bmap1);
3030 error:
3031 if (sample)
3032 isl_vec_free(sample);
3033 isl_basic_map_free(bmap1);
3034 isl_basic_map_free(bmap2);
3035 return NULL;
3038 struct isl_basic_set *isl_basic_set_intersect(
3039 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3041 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3042 bset_to_bmap(bset2)));
3045 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3046 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3048 return isl_basic_set_intersect(bset1, bset2);
3051 /* Special case of isl_map_intersect, where both map1 and map2
3052 * are convex, without any divs and such that either map1 or map2
3053 * contains a single constraint. This constraint is then simply
3054 * added to the other map.
3056 static __isl_give isl_map *map_intersect_add_constraint(
3057 __isl_take isl_map *map1, __isl_take isl_map *map2)
3059 isl_assert(map1->ctx, map1->n == 1, goto error);
3060 isl_assert(map2->ctx, map1->n == 1, goto error);
3061 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3062 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3064 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3065 return isl_map_intersect(map2, map1);
3067 isl_assert(map2->ctx,
3068 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3070 map1 = isl_map_cow(map1);
3071 if (!map1)
3072 goto error;
3073 if (isl_map_plain_is_empty(map1)) {
3074 isl_map_free(map2);
3075 return map1;
3077 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3078 if (map2->p[0]->n_eq == 1)
3079 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3080 else
3081 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3082 map2->p[0]->ineq[0]);
3084 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3085 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3086 if (!map1->p[0])
3087 goto error;
3089 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3090 isl_basic_map_free(map1->p[0]);
3091 map1->n = 0;
3094 isl_map_free(map2);
3096 return map1;
3097 error:
3098 isl_map_free(map1);
3099 isl_map_free(map2);
3100 return NULL;
3103 /* map2 may be either a parameter domain or a map living in the same
3104 * space as map1.
3106 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3107 __isl_take isl_map *map2)
3109 unsigned flags = 0;
3110 isl_map *result;
3111 int i, j;
3113 if (!map1 || !map2)
3114 goto error;
3116 if ((isl_map_plain_is_empty(map1) ||
3117 isl_map_plain_is_universe(map2)) &&
3118 isl_space_is_equal(map1->dim, map2->dim)) {
3119 isl_map_free(map2);
3120 return map1;
3122 if ((isl_map_plain_is_empty(map2) ||
3123 isl_map_plain_is_universe(map1)) &&
3124 isl_space_is_equal(map1->dim, map2->dim)) {
3125 isl_map_free(map1);
3126 return map2;
3129 if (map1->n == 1 && map2->n == 1 &&
3130 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3131 isl_space_is_equal(map1->dim, map2->dim) &&
3132 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3133 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3134 return map_intersect_add_constraint(map1, map2);
3136 if (isl_space_dim(map2->dim, isl_dim_all) !=
3137 isl_space_dim(map2->dim, isl_dim_param))
3138 isl_assert(map1->ctx,
3139 isl_space_is_equal(map1->dim, map2->dim), goto error);
3141 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3142 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3143 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3145 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3146 map1->n * map2->n, flags);
3147 if (!result)
3148 goto error;
3149 for (i = 0; i < map1->n; ++i)
3150 for (j = 0; j < map2->n; ++j) {
3151 struct isl_basic_map *part;
3152 part = isl_basic_map_intersect(
3153 isl_basic_map_copy(map1->p[i]),
3154 isl_basic_map_copy(map2->p[j]));
3155 if (isl_basic_map_is_empty(part) < 0)
3156 part = isl_basic_map_free(part);
3157 result = isl_map_add_basic_map(result, part);
3158 if (!result)
3159 goto error;
3161 isl_map_free(map1);
3162 isl_map_free(map2);
3163 return result;
3164 error:
3165 isl_map_free(map1);
3166 isl_map_free(map2);
3167 return NULL;
3170 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3171 __isl_take isl_map *map2)
3173 if (!map1 || !map2)
3174 goto error;
3175 if (!isl_space_is_equal(map1->dim, map2->dim))
3176 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3177 "spaces don't match", goto error);
3178 return map_intersect_internal(map1, map2);
3179 error:
3180 isl_map_free(map1);
3181 isl_map_free(map2);
3182 return NULL;
3185 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3186 __isl_take isl_map *map2)
3188 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3191 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3193 return set_from_map(isl_map_intersect(set_to_map(set1),
3194 set_to_map(set2)));
3197 /* map_intersect_internal accepts intersections
3198 * with parameter domains, so we can just call that function.
3200 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3201 __isl_take isl_set *params)
3203 return map_intersect_internal(map, params);
3206 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3207 __isl_take isl_map *map2)
3209 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3212 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3213 __isl_take isl_set *params)
3215 return isl_map_intersect_params(set, params);
3218 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3220 isl_space *space;
3221 unsigned pos, n1, n2;
3223 if (!bmap)
3224 return NULL;
3225 bmap = isl_basic_map_cow(bmap);
3226 if (!bmap)
3227 return NULL;
3228 space = isl_space_reverse(isl_space_copy(bmap->dim));
3229 pos = isl_basic_map_offset(bmap, isl_dim_in);
3230 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3231 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3232 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3233 return isl_basic_map_reset_space(bmap, space);
3236 static __isl_give isl_basic_map *basic_map_space_reset(
3237 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3239 isl_space *space;
3241 if (!bmap)
3242 return NULL;
3243 if (!isl_space_is_named_or_nested(bmap->dim, type))
3244 return bmap;
3246 space = isl_basic_map_get_space(bmap);
3247 space = isl_space_reset(space, type);
3248 bmap = isl_basic_map_reset_space(bmap, space);
3249 return bmap;
3252 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3253 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3254 unsigned pos, unsigned n)
3256 isl_space *res_dim;
3257 struct isl_basic_map *res;
3258 struct isl_dim_map *dim_map;
3259 unsigned total, off;
3260 enum isl_dim_type t;
3262 if (n == 0)
3263 return basic_map_space_reset(bmap, type);
3265 if (!bmap)
3266 return NULL;
3268 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3270 total = isl_basic_map_total_dim(bmap) + n;
3271 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3272 off = 0;
3273 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3274 if (t != type) {
3275 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3276 } else {
3277 unsigned size = isl_basic_map_dim(bmap, t);
3278 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3279 0, pos, off);
3280 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3281 pos, size - pos, off + pos + n);
3283 off += isl_space_dim(res_dim, t);
3285 isl_dim_map_div(dim_map, bmap, off);
3287 res = isl_basic_map_alloc_space(res_dim,
3288 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3289 if (isl_basic_map_is_rational(bmap))
3290 res = isl_basic_map_set_rational(res);
3291 if (isl_basic_map_plain_is_empty(bmap)) {
3292 isl_basic_map_free(bmap);
3293 free(dim_map);
3294 return isl_basic_map_set_to_empty(res);
3296 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3297 return isl_basic_map_finalize(res);
3300 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3301 __isl_take isl_basic_set *bset,
3302 enum isl_dim_type type, unsigned pos, unsigned n)
3304 return isl_basic_map_insert_dims(bset, type, pos, n);
3307 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3308 enum isl_dim_type type, unsigned n)
3310 if (!bmap)
3311 return NULL;
3312 return isl_basic_map_insert_dims(bmap, type,
3313 isl_basic_map_dim(bmap, type), n);
3316 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3317 enum isl_dim_type type, unsigned n)
3319 if (!bset)
3320 return NULL;
3321 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3322 return isl_basic_map_add_dims(bset, type, n);
3323 error:
3324 isl_basic_set_free(bset);
3325 return NULL;
3328 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3329 enum isl_dim_type type)
3331 isl_space *space;
3333 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3334 return map;
3336 space = isl_map_get_space(map);
3337 space = isl_space_reset(space, type);
3338 map = isl_map_reset_space(map, space);
3339 return map;
3342 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3343 enum isl_dim_type type, unsigned pos, unsigned n)
3345 int i;
3347 if (n == 0)
3348 return map_space_reset(map, type);
3350 map = isl_map_cow(map);
3351 if (!map)
3352 return NULL;
3354 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3355 if (!map->dim)
3356 goto error;
3358 for (i = 0; i < map->n; ++i) {
3359 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3360 if (!map->p[i])
3361 goto error;
3364 return map;
3365 error:
3366 isl_map_free(map);
3367 return NULL;
3370 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3371 enum isl_dim_type type, unsigned pos, unsigned n)
3373 return isl_map_insert_dims(set, type, pos, n);
3376 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3377 enum isl_dim_type type, unsigned n)
3379 if (!map)
3380 return NULL;
3381 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3384 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3385 enum isl_dim_type type, unsigned n)
3387 if (!set)
3388 return NULL;
3389 isl_assert(set->ctx, type != isl_dim_in, goto error);
3390 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3391 error:
3392 isl_set_free(set);
3393 return NULL;
3396 __isl_give isl_basic_map *isl_basic_map_move_dims(
3397 __isl_take isl_basic_map *bmap,
3398 enum isl_dim_type dst_type, unsigned dst_pos,
3399 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3401 struct isl_dim_map *dim_map;
3402 struct isl_basic_map *res;
3403 enum isl_dim_type t;
3404 unsigned total, off;
3406 if (!bmap)
3407 return NULL;
3408 if (n == 0) {
3409 bmap = isl_basic_map_reset(bmap, src_type);
3410 bmap = isl_basic_map_reset(bmap, dst_type);
3411 return bmap;
3414 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3415 goto error);
3417 if (dst_type == src_type && dst_pos == src_pos)
3418 return bmap;
3420 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3422 if (pos(bmap->dim, dst_type) + dst_pos ==
3423 pos(bmap->dim, src_type) + src_pos +
3424 ((src_type < dst_type) ? n : 0)) {
3425 bmap = isl_basic_map_cow(bmap);
3426 if (!bmap)
3427 return NULL;
3429 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3430 src_type, src_pos, n);
3431 if (!bmap->dim)
3432 goto error;
3434 bmap = isl_basic_map_finalize(bmap);
3436 return bmap;
3439 total = isl_basic_map_total_dim(bmap);
3440 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3442 off = 0;
3443 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3444 unsigned size = isl_space_dim(bmap->dim, t);
3445 if (t == dst_type) {
3446 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3447 0, dst_pos, off);
3448 off += dst_pos;
3449 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3450 src_pos, n, off);
3451 off += n;
3452 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3453 dst_pos, size - dst_pos, off);
3454 off += size - dst_pos;
3455 } else if (t == src_type) {
3456 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3457 0, src_pos, off);
3458 off += src_pos;
3459 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3460 src_pos + n, size - src_pos - n, off);
3461 off += size - src_pos - n;
3462 } else {
3463 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3464 off += size;
3467 isl_dim_map_div(dim_map, bmap, off);
3469 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3470 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3471 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3472 if (!bmap)
3473 goto error;
3475 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3476 src_type, src_pos, n);
3477 if (!bmap->dim)
3478 goto error;
3480 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3481 bmap = isl_basic_map_gauss(bmap, NULL);
3482 bmap = isl_basic_map_finalize(bmap);
3484 return bmap;
3485 error:
3486 isl_basic_map_free(bmap);
3487 return NULL;
3490 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3491 enum isl_dim_type dst_type, unsigned dst_pos,
3492 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3494 isl_basic_map *bmap = bset_to_bmap(bset);
3495 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3496 src_type, src_pos, n);
3497 return bset_from_bmap(bmap);
3500 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3501 enum isl_dim_type dst_type, unsigned dst_pos,
3502 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3504 if (!set)
3505 return NULL;
3506 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3507 return set_from_map(isl_map_move_dims(set_to_map(set),
3508 dst_type, dst_pos, src_type, src_pos, n));
3509 error:
3510 isl_set_free(set);
3511 return NULL;
3514 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3515 enum isl_dim_type dst_type, unsigned dst_pos,
3516 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3518 int i;
3520 if (!map)
3521 return NULL;
3522 if (n == 0) {
3523 map = isl_map_reset(map, src_type);
3524 map = isl_map_reset(map, dst_type);
3525 return map;
3528 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3529 goto error);
3531 if (dst_type == src_type && dst_pos == src_pos)
3532 return map;
3534 isl_assert(map->ctx, dst_type != src_type, goto error);
3536 map = isl_map_cow(map);
3537 if (!map)
3538 return NULL;
3540 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3541 if (!map->dim)
3542 goto error;
3544 for (i = 0; i < map->n; ++i) {
3545 map->p[i] = isl_basic_map_move_dims(map->p[i],
3546 dst_type, dst_pos,
3547 src_type, src_pos, n);
3548 if (!map->p[i])
3549 goto error;
3552 return map;
3553 error:
3554 isl_map_free(map);
3555 return NULL;
3558 /* Move the specified dimensions to the last columns right before
3559 * the divs. Don't change the dimension specification of bmap.
3560 * That's the responsibility of the caller.
3562 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3563 enum isl_dim_type type, unsigned first, unsigned n)
3565 struct isl_dim_map *dim_map;
3566 struct isl_basic_map *res;
3567 enum isl_dim_type t;
3568 unsigned total, off;
3570 if (!bmap)
3571 return NULL;
3572 if (pos(bmap->dim, type) + first + n ==
3573 1 + isl_space_dim(bmap->dim, isl_dim_all))
3574 return bmap;
3576 total = isl_basic_map_total_dim(bmap);
3577 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3579 off = 0;
3580 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3581 unsigned size = isl_space_dim(bmap->dim, t);
3582 if (t == type) {
3583 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3584 0, first, off);
3585 off += first;
3586 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3587 first, n, total - bmap->n_div - n);
3588 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3589 first + n, size - (first + n), off);
3590 off += size - (first + n);
3591 } else {
3592 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3593 off += size;
3596 isl_dim_map_div(dim_map, bmap, off + n);
3598 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3599 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3600 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3601 return res;
3604 /* Insert "n" rows in the divs of "bmap".
3606 * The number of columns is not changed, which means that the last
3607 * dimensions of "bmap" are being reintepreted as the new divs.
3608 * The space of "bmap" is not adjusted, however, which means
3609 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3610 * from the space of "bmap" is the responsibility of the caller.
3612 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3613 int n)
3615 int i;
3616 size_t row_size;
3617 isl_int **new_div;
3618 isl_int *old;
3620 bmap = isl_basic_map_cow(bmap);
3621 if (!bmap)
3622 return NULL;
3624 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3625 old = bmap->block2.data;
3626 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3627 (bmap->extra + n) * (1 + row_size));
3628 if (!bmap->block2.data)
3629 return isl_basic_map_free(bmap);
3630 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3631 if (!new_div)
3632 return isl_basic_map_free(bmap);
3633 for (i = 0; i < n; ++i) {
3634 new_div[i] = bmap->block2.data +
3635 (bmap->extra + i) * (1 + row_size);
3636 isl_seq_clr(new_div[i], 1 + row_size);
3638 for (i = 0; i < bmap->extra; ++i)
3639 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3640 free(bmap->div);
3641 bmap->div = new_div;
3642 bmap->n_div += n;
3643 bmap->extra += n;
3645 return bmap;
3648 /* Drop constraints from "bmap" that only involve the variables
3649 * of "type" in the range [first, first + n] that are not related
3650 * to any of the variables outside that interval.
3651 * These constraints cannot influence the values for the variables
3652 * outside the interval, except in case they cause "bmap" to be empty.
3653 * Only drop the constraints if "bmap" is known to be non-empty.
3655 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3656 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3657 unsigned first, unsigned n)
3659 int i;
3660 int *groups;
3661 unsigned dim, n_div;
3662 isl_bool non_empty;
3664 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3665 if (non_empty < 0)
3666 return isl_basic_map_free(bmap);
3667 if (!non_empty)
3668 return bmap;
3670 dim = isl_basic_map_dim(bmap, isl_dim_all);
3671 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3672 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3673 if (!groups)
3674 return isl_basic_map_free(bmap);
3675 first += isl_basic_map_offset(bmap, type) - 1;
3676 for (i = 0; i < first; ++i)
3677 groups[i] = -1;
3678 for (i = first + n; i < dim - n_div; ++i)
3679 groups[i] = -1;
3681 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3683 return bmap;
3686 /* Turn the n dimensions of type type, starting at first
3687 * into existentially quantified variables.
3689 * If a subset of the projected out variables are unrelated
3690 * to any of the variables that remain, then the constraints
3691 * involving this subset are simply dropped first.
3693 __isl_give isl_basic_map *isl_basic_map_project_out(
3694 __isl_take isl_basic_map *bmap,
3695 enum isl_dim_type type, unsigned first, unsigned n)
3697 if (n == 0)
3698 return basic_map_space_reset(bmap, type);
3699 if (type == isl_dim_div)
3700 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3701 "cannot project out existentially quantified variables",
3702 return isl_basic_map_free(bmap));
3704 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3705 if (!bmap)
3706 return NULL;
3708 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3709 return isl_basic_map_remove_dims(bmap, type, first, n);
3711 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3712 goto error);
3714 bmap = move_last(bmap, type, first, n);
3715 bmap = isl_basic_map_cow(bmap);
3716 bmap = insert_div_rows(bmap, n);
3717 if (!bmap)
3718 return NULL;
3720 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3721 if (!bmap->dim)
3722 goto error;
3723 bmap = isl_basic_map_simplify(bmap);
3724 bmap = isl_basic_map_drop_redundant_divs(bmap);
3725 return isl_basic_map_finalize(bmap);
3726 error:
3727 isl_basic_map_free(bmap);
3728 return NULL;
3731 /* Turn the n dimensions of type type, starting at first
3732 * into existentially quantified variables.
3734 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3735 enum isl_dim_type type, unsigned first, unsigned n)
3737 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3738 type, first, n));
3741 /* Turn the n dimensions of type type, starting at first
3742 * into existentially quantified variables.
3744 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3745 enum isl_dim_type type, unsigned first, unsigned n)
3747 int i;
3749 if (!map)
3750 return NULL;
3752 if (n == 0)
3753 return map_space_reset(map, type);
3755 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3757 map = isl_map_cow(map);
3758 if (!map)
3759 return NULL;
3761 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3762 if (!map->dim)
3763 goto error;
3765 for (i = 0; i < map->n; ++i) {
3766 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3767 if (!map->p[i])
3768 goto error;
3771 return map;
3772 error:
3773 isl_map_free(map);
3774 return NULL;
3777 /* Turn the n dimensions of type type, starting at first
3778 * into existentially quantified variables.
3780 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3781 enum isl_dim_type type, unsigned first, unsigned n)
3783 return set_from_map(isl_map_project_out(set_to_map(set),
3784 type, first, n));
3787 /* Return a map that projects the elements in "set" onto their
3788 * "n" set dimensions starting at "first".
3789 * "type" should be equal to isl_dim_set.
3791 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3792 enum isl_dim_type type, unsigned first, unsigned n)
3794 int i;
3795 int dim;
3796 isl_map *map;
3798 if (!set)
3799 return NULL;
3800 if (type != isl_dim_set)
3801 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3802 "only set dimensions can be projected out", goto error);
3803 dim = isl_set_dim(set, isl_dim_set);
3804 if (first + n > dim || first + n < first)
3805 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3806 "index out of bounds", goto error);
3808 map = isl_map_from_domain(set);
3809 map = isl_map_add_dims(map, isl_dim_out, n);
3810 for (i = 0; i < n; ++i)
3811 map = isl_map_equate(map, isl_dim_in, first + i,
3812 isl_dim_out, i);
3813 return map;
3814 error:
3815 isl_set_free(set);
3816 return NULL;
3819 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3821 int i, j;
3823 for (i = 0; i < n; ++i) {
3824 j = isl_basic_map_alloc_div(bmap);
3825 if (j < 0)
3826 goto error;
3827 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3829 return bmap;
3830 error:
3831 isl_basic_map_free(bmap);
3832 return NULL;
3835 struct isl_basic_map *isl_basic_map_apply_range(
3836 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3838 isl_space *dim_result = NULL;
3839 struct isl_basic_map *bmap;
3840 unsigned n_in, n_out, n, nparam, total, pos;
3841 struct isl_dim_map *dim_map1, *dim_map2;
3843 if (!bmap1 || !bmap2)
3844 goto error;
3845 if (!isl_space_match(bmap1->dim, isl_dim_param,
3846 bmap2->dim, isl_dim_param))
3847 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3848 "parameters don't match", goto error);
3849 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3850 bmap2->dim, isl_dim_in))
3851 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3852 "spaces don't match", goto error);
3854 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3855 isl_space_copy(bmap2->dim));
3857 n_in = isl_basic_map_n_in(bmap1);
3858 n_out = isl_basic_map_n_out(bmap2);
3859 n = isl_basic_map_n_out(bmap1);
3860 nparam = isl_basic_map_n_param(bmap1);
3862 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3863 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3864 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3865 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3866 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3867 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3868 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3869 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3870 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3871 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3872 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3874 bmap = isl_basic_map_alloc_space(dim_result,
3875 bmap1->n_div + bmap2->n_div + n,
3876 bmap1->n_eq + bmap2->n_eq,
3877 bmap1->n_ineq + bmap2->n_ineq);
3878 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3879 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3880 bmap = add_divs(bmap, n);
3881 bmap = isl_basic_map_simplify(bmap);
3882 bmap = isl_basic_map_drop_redundant_divs(bmap);
3883 return isl_basic_map_finalize(bmap);
3884 error:
3885 isl_basic_map_free(bmap1);
3886 isl_basic_map_free(bmap2);
3887 return NULL;
3890 struct isl_basic_set *isl_basic_set_apply(
3891 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3893 if (!bset || !bmap)
3894 goto error;
3896 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3897 goto error);
3899 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
3900 bmap));
3901 error:
3902 isl_basic_set_free(bset);
3903 isl_basic_map_free(bmap);
3904 return NULL;
3907 struct isl_basic_map *isl_basic_map_apply_domain(
3908 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3910 if (!bmap1 || !bmap2)
3911 goto error;
3913 if (!isl_space_match(bmap1->dim, isl_dim_param,
3914 bmap2->dim, isl_dim_param))
3915 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3916 "parameters don't match", goto error);
3917 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
3918 bmap2->dim, isl_dim_in))
3919 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3920 "spaces don't match", goto error);
3922 bmap1 = isl_basic_map_reverse(bmap1);
3923 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3924 return isl_basic_map_reverse(bmap1);
3925 error:
3926 isl_basic_map_free(bmap1);
3927 isl_basic_map_free(bmap2);
3928 return NULL;
3931 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3932 * A \cap B -> f(A) + f(B)
3934 struct isl_basic_map *isl_basic_map_sum(
3935 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3937 unsigned n_in, n_out, nparam, total, pos;
3938 struct isl_basic_map *bmap = NULL;
3939 struct isl_dim_map *dim_map1, *dim_map2;
3940 int i;
3942 if (!bmap1 || !bmap2)
3943 goto error;
3945 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3946 goto error);
3948 nparam = isl_basic_map_n_param(bmap1);
3949 n_in = isl_basic_map_n_in(bmap1);
3950 n_out = isl_basic_map_n_out(bmap1);
3952 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3953 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3954 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3955 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3956 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3957 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3958 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3959 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3960 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3961 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3962 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3964 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3965 bmap1->n_div + bmap2->n_div + 2 * n_out,
3966 bmap1->n_eq + bmap2->n_eq + n_out,
3967 bmap1->n_ineq + bmap2->n_ineq);
3968 for (i = 0; i < n_out; ++i) {
3969 int j = isl_basic_map_alloc_equality(bmap);
3970 if (j < 0)
3971 goto error;
3972 isl_seq_clr(bmap->eq[j], 1+total);
3973 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3974 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3975 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3977 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3978 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3979 bmap = add_divs(bmap, 2 * n_out);
3981 bmap = isl_basic_map_simplify(bmap);
3982 return isl_basic_map_finalize(bmap);
3983 error:
3984 isl_basic_map_free(bmap);
3985 isl_basic_map_free(bmap1);
3986 isl_basic_map_free(bmap2);
3987 return NULL;
3990 /* Given two maps A -> f(A) and B -> g(B), construct a map
3991 * A \cap B -> f(A) + f(B)
3993 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3995 struct isl_map *result;
3996 int i, j;
3998 if (!map1 || !map2)
3999 goto error;
4001 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4003 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4004 map1->n * map2->n, 0);
4005 if (!result)
4006 goto error;
4007 for (i = 0; i < map1->n; ++i)
4008 for (j = 0; j < map2->n; ++j) {
4009 struct isl_basic_map *part;
4010 part = isl_basic_map_sum(
4011 isl_basic_map_copy(map1->p[i]),
4012 isl_basic_map_copy(map2->p[j]));
4013 if (isl_basic_map_is_empty(part))
4014 isl_basic_map_free(part);
4015 else
4016 result = isl_map_add_basic_map(result, part);
4017 if (!result)
4018 goto error;
4020 isl_map_free(map1);
4021 isl_map_free(map2);
4022 return result;
4023 error:
4024 isl_map_free(map1);
4025 isl_map_free(map2);
4026 return NULL;
4029 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4030 __isl_take isl_set *set2)
4032 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4035 /* Given a basic map A -> f(A), construct A -> -f(A).
4037 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4039 int i, j;
4040 unsigned off, n;
4042 bmap = isl_basic_map_cow(bmap);
4043 if (!bmap)
4044 return NULL;
4046 n = isl_basic_map_dim(bmap, isl_dim_out);
4047 off = isl_basic_map_offset(bmap, isl_dim_out);
4048 for (i = 0; i < bmap->n_eq; ++i)
4049 for (j = 0; j < n; ++j)
4050 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4051 for (i = 0; i < bmap->n_ineq; ++i)
4052 for (j = 0; j < n; ++j)
4053 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4054 for (i = 0; i < bmap->n_div; ++i)
4055 for (j = 0; j < n; ++j)
4056 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4057 bmap = isl_basic_map_gauss(bmap, NULL);
4058 return isl_basic_map_finalize(bmap);
4061 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4063 return isl_basic_map_neg(bset);
4066 /* Given a map A -> f(A), construct A -> -f(A).
4068 struct isl_map *isl_map_neg(struct isl_map *map)
4070 int i;
4072 map = isl_map_cow(map);
4073 if (!map)
4074 return NULL;
4076 for (i = 0; i < map->n; ++i) {
4077 map->p[i] = isl_basic_map_neg(map->p[i]);
4078 if (!map->p[i])
4079 goto error;
4082 return map;
4083 error:
4084 isl_map_free(map);
4085 return NULL;
4088 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4090 return set_from_map(isl_map_neg(set_to_map(set)));
4093 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4094 * A -> floor(f(A)/d).
4096 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4097 isl_int d)
4099 unsigned n_in, n_out, nparam, total, pos;
4100 struct isl_basic_map *result = NULL;
4101 struct isl_dim_map *dim_map;
4102 int i;
4104 if (!bmap)
4105 return NULL;
4107 nparam = isl_basic_map_n_param(bmap);
4108 n_in = isl_basic_map_n_in(bmap);
4109 n_out = isl_basic_map_n_out(bmap);
4111 total = nparam + n_in + n_out + bmap->n_div + n_out;
4112 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4113 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4114 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4115 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4116 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4118 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4119 bmap->n_div + n_out,
4120 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4121 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4122 result = add_divs(result, n_out);
4123 for (i = 0; i < n_out; ++i) {
4124 int j;
4125 j = isl_basic_map_alloc_inequality(result);
4126 if (j < 0)
4127 goto error;
4128 isl_seq_clr(result->ineq[j], 1+total);
4129 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4130 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4131 j = isl_basic_map_alloc_inequality(result);
4132 if (j < 0)
4133 goto error;
4134 isl_seq_clr(result->ineq[j], 1+total);
4135 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4136 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4137 isl_int_sub_ui(result->ineq[j][0], d, 1);
4140 result = isl_basic_map_simplify(result);
4141 return isl_basic_map_finalize(result);
4142 error:
4143 isl_basic_map_free(result);
4144 return NULL;
4147 /* Given a map A -> f(A) and an integer d, construct a map
4148 * A -> floor(f(A)/d).
4150 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4152 int i;
4154 map = isl_map_cow(map);
4155 if (!map)
4156 return NULL;
4158 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4159 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4160 for (i = 0; i < map->n; ++i) {
4161 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4162 if (!map->p[i])
4163 goto error;
4166 return map;
4167 error:
4168 isl_map_free(map);
4169 return NULL;
4172 /* Given a map A -> f(A) and an integer d, construct a map
4173 * A -> floor(f(A)/d).
4175 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4176 __isl_take isl_val *d)
4178 if (!map || !d)
4179 goto error;
4180 if (!isl_val_is_int(d))
4181 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4182 "expecting integer denominator", goto error);
4183 map = isl_map_floordiv(map, d->n);
4184 isl_val_free(d);
4185 return map;
4186 error:
4187 isl_map_free(map);
4188 isl_val_free(d);
4189 return NULL;
4192 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4194 int i;
4195 unsigned nparam;
4196 unsigned n_in;
4198 i = isl_basic_map_alloc_equality(bmap);
4199 if (i < 0)
4200 goto error;
4201 nparam = isl_basic_map_n_param(bmap);
4202 n_in = isl_basic_map_n_in(bmap);
4203 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4204 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4205 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4206 return isl_basic_map_finalize(bmap);
4207 error:
4208 isl_basic_map_free(bmap);
4209 return NULL;
4212 /* Add a constraint to "bmap" expressing i_pos < o_pos
4214 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4216 int i;
4217 unsigned nparam;
4218 unsigned n_in;
4220 i = isl_basic_map_alloc_inequality(bmap);
4221 if (i < 0)
4222 goto error;
4223 nparam = isl_basic_map_n_param(bmap);
4224 n_in = isl_basic_map_n_in(bmap);
4225 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4226 isl_int_set_si(bmap->ineq[i][0], -1);
4227 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4228 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4229 return isl_basic_map_finalize(bmap);
4230 error:
4231 isl_basic_map_free(bmap);
4232 return NULL;
4235 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4237 static __isl_give isl_basic_map *var_less_or_equal(
4238 __isl_take isl_basic_map *bmap, unsigned pos)
4240 int i;
4241 unsigned nparam;
4242 unsigned n_in;
4244 i = isl_basic_map_alloc_inequality(bmap);
4245 if (i < 0)
4246 goto error;
4247 nparam = isl_basic_map_n_param(bmap);
4248 n_in = isl_basic_map_n_in(bmap);
4249 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4250 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4251 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4252 return isl_basic_map_finalize(bmap);
4253 error:
4254 isl_basic_map_free(bmap);
4255 return NULL;
4258 /* Add a constraint to "bmap" expressing i_pos > o_pos
4260 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4262 int i;
4263 unsigned nparam;
4264 unsigned n_in;
4266 i = isl_basic_map_alloc_inequality(bmap);
4267 if (i < 0)
4268 goto error;
4269 nparam = isl_basic_map_n_param(bmap);
4270 n_in = isl_basic_map_n_in(bmap);
4271 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4272 isl_int_set_si(bmap->ineq[i][0], -1);
4273 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4274 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4275 return isl_basic_map_finalize(bmap);
4276 error:
4277 isl_basic_map_free(bmap);
4278 return NULL;
4281 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4283 static __isl_give isl_basic_map *var_more_or_equal(
4284 __isl_take isl_basic_map *bmap, unsigned pos)
4286 int i;
4287 unsigned nparam;
4288 unsigned n_in;
4290 i = isl_basic_map_alloc_inequality(bmap);
4291 if (i < 0)
4292 goto error;
4293 nparam = isl_basic_map_n_param(bmap);
4294 n_in = isl_basic_map_n_in(bmap);
4295 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4296 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4297 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4298 return isl_basic_map_finalize(bmap);
4299 error:
4300 isl_basic_map_free(bmap);
4301 return NULL;
4304 __isl_give isl_basic_map *isl_basic_map_equal(
4305 __isl_take isl_space *dim, unsigned n_equal)
4307 int i;
4308 struct isl_basic_map *bmap;
4309 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4310 if (!bmap)
4311 return NULL;
4312 for (i = 0; i < n_equal && bmap; ++i)
4313 bmap = var_equal(bmap, i);
4314 return isl_basic_map_finalize(bmap);
4317 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4319 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4320 unsigned pos)
4322 int i;
4323 struct isl_basic_map *bmap;
4324 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4325 if (!bmap)
4326 return NULL;
4327 for (i = 0; i < pos && bmap; ++i)
4328 bmap = var_equal(bmap, i);
4329 if (bmap)
4330 bmap = var_less(bmap, pos);
4331 return isl_basic_map_finalize(bmap);
4334 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4336 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4337 __isl_take isl_space *dim, unsigned pos)
4339 int i;
4340 isl_basic_map *bmap;
4342 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4343 for (i = 0; i < pos; ++i)
4344 bmap = var_equal(bmap, i);
4345 bmap = var_less_or_equal(bmap, pos);
4346 return isl_basic_map_finalize(bmap);
4349 /* Return a relation on "dim" expressing i_pos > o_pos
4351 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4352 unsigned pos)
4354 int i;
4355 struct isl_basic_map *bmap;
4356 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4357 if (!bmap)
4358 return NULL;
4359 for (i = 0; i < pos && bmap; ++i)
4360 bmap = var_equal(bmap, i);
4361 if (bmap)
4362 bmap = var_more(bmap, pos);
4363 return isl_basic_map_finalize(bmap);
4366 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4368 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4369 __isl_take isl_space *dim, unsigned pos)
4371 int i;
4372 isl_basic_map *bmap;
4374 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4375 for (i = 0; i < pos; ++i)
4376 bmap = var_equal(bmap, i);
4377 bmap = var_more_or_equal(bmap, pos);
4378 return isl_basic_map_finalize(bmap);
4381 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4382 unsigned n, int equal)
4384 struct isl_map *map;
4385 int i;
4387 if (n == 0 && equal)
4388 return isl_map_universe(dims);
4390 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4392 for (i = 0; i + 1 < n; ++i)
4393 map = isl_map_add_basic_map(map,
4394 isl_basic_map_less_at(isl_space_copy(dims), i));
4395 if (n > 0) {
4396 if (equal)
4397 map = isl_map_add_basic_map(map,
4398 isl_basic_map_less_or_equal_at(dims, n - 1));
4399 else
4400 map = isl_map_add_basic_map(map,
4401 isl_basic_map_less_at(dims, n - 1));
4402 } else
4403 isl_space_free(dims);
4405 return map;
4408 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4410 if (!dims)
4411 return NULL;
4412 return map_lex_lte_first(dims, dims->n_out, equal);
4415 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4417 return map_lex_lte_first(dim, n, 0);
4420 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4422 return map_lex_lte_first(dim, n, 1);
4425 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4427 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4430 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4432 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4435 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4436 unsigned n, int equal)
4438 struct isl_map *map;
4439 int i;
4441 if (n == 0 && equal)
4442 return isl_map_universe(dims);
4444 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4446 for (i = 0; i + 1 < n; ++i)
4447 map = isl_map_add_basic_map(map,
4448 isl_basic_map_more_at(isl_space_copy(dims), i));
4449 if (n > 0) {
4450 if (equal)
4451 map = isl_map_add_basic_map(map,
4452 isl_basic_map_more_or_equal_at(dims, n - 1));
4453 else
4454 map = isl_map_add_basic_map(map,
4455 isl_basic_map_more_at(dims, n - 1));
4456 } else
4457 isl_space_free(dims);
4459 return map;
4462 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4464 if (!dims)
4465 return NULL;
4466 return map_lex_gte_first(dims, dims->n_out, equal);
4469 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4471 return map_lex_gte_first(dim, n, 0);
4474 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4476 return map_lex_gte_first(dim, n, 1);
4479 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4481 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4484 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4486 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4489 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4490 __isl_take isl_set *set2)
4492 isl_map *map;
4493 map = isl_map_lex_le(isl_set_get_space(set1));
4494 map = isl_map_intersect_domain(map, set1);
4495 map = isl_map_intersect_range(map, set2);
4496 return map;
4499 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4500 __isl_take isl_set *set2)
4502 isl_map *map;
4503 map = isl_map_lex_lt(isl_set_get_space(set1));
4504 map = isl_map_intersect_domain(map, set1);
4505 map = isl_map_intersect_range(map, set2);
4506 return map;
4509 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4510 __isl_take isl_set *set2)
4512 isl_map *map;
4513 map = isl_map_lex_ge(isl_set_get_space(set1));
4514 map = isl_map_intersect_domain(map, set1);
4515 map = isl_map_intersect_range(map, set2);
4516 return map;
4519 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4520 __isl_take isl_set *set2)
4522 isl_map *map;
4523 map = isl_map_lex_gt(isl_set_get_space(set1));
4524 map = isl_map_intersect_domain(map, set1);
4525 map = isl_map_intersect_range(map, set2);
4526 return map;
4529 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4530 __isl_take isl_map *map2)
4532 isl_map *map;
4533 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4534 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4535 map = isl_map_apply_range(map, isl_map_reverse(map2));
4536 return map;
4539 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4540 __isl_take isl_map *map2)
4542 isl_map *map;
4543 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4544 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4545 map = isl_map_apply_range(map, isl_map_reverse(map2));
4546 return map;
4549 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4550 __isl_take isl_map *map2)
4552 isl_map *map;
4553 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4554 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4555 map = isl_map_apply_range(map, isl_map_reverse(map2));
4556 return map;
4559 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4560 __isl_take isl_map *map2)
4562 isl_map *map;
4563 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4564 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4565 map = isl_map_apply_range(map, isl_map_reverse(map2));
4566 return map;
4569 static __isl_give isl_basic_map *basic_map_from_basic_set(
4570 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4572 struct isl_basic_map *bmap;
4574 bset = isl_basic_set_cow(bset);
4575 if (!bset || !dim)
4576 goto error;
4578 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4579 goto error);
4580 isl_space_free(bset->dim);
4581 bmap = bset_to_bmap(bset);
4582 bmap->dim = dim;
4583 return isl_basic_map_finalize(bmap);
4584 error:
4585 isl_basic_set_free(bset);
4586 isl_space_free(dim);
4587 return NULL;
4590 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4591 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4593 return basic_map_from_basic_set(bset, space);
4596 /* For a div d = floor(f/m), add the constraint
4598 * f - m d >= 0
4600 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4601 unsigned pos, isl_int *div)
4603 int i;
4604 unsigned total = isl_basic_map_total_dim(bmap);
4606 i = isl_basic_map_alloc_inequality(bmap);
4607 if (i < 0)
4608 return -1;
4609 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4610 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4612 return 0;
4615 /* For a div d = floor(f/m), add the constraint
4617 * -(f-(m-1)) + m d >= 0
4619 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4620 unsigned pos, isl_int *div)
4622 int i;
4623 unsigned total = isl_basic_map_total_dim(bmap);
4625 i = isl_basic_map_alloc_inequality(bmap);
4626 if (i < 0)
4627 return -1;
4628 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4629 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4630 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4631 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4633 return 0;
4636 /* For a div d = floor(f/m), add the constraints
4638 * f - m d >= 0
4639 * -(f-(m-1)) + m d >= 0
4641 * Note that the second constraint is the negation of
4643 * f - m d >= m
4645 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4646 unsigned pos, isl_int *div)
4648 if (add_upper_div_constraint(bmap, pos, div) < 0)
4649 return -1;
4650 if (add_lower_div_constraint(bmap, pos, div) < 0)
4651 return -1;
4652 return 0;
4655 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4656 unsigned pos, isl_int *div)
4658 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4659 pos, div);
4662 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4664 unsigned total = isl_basic_map_total_dim(bmap);
4665 unsigned div_pos = total - bmap->n_div + div;
4667 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4668 bmap->div[div]);
4671 /* For each known div d = floor(f/m), add the constraints
4673 * f - m d >= 0
4674 * -(f-(m-1)) + m d >= 0
4676 * Remove duplicate constraints in case of some these div constraints
4677 * already appear in "bmap".
4679 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4680 __isl_take isl_basic_map *bmap)
4682 unsigned n_div;
4684 if (!bmap)
4685 return NULL;
4686 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4687 if (n_div == 0)
4688 return bmap;
4690 bmap = add_known_div_constraints(bmap);
4691 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4692 bmap = isl_basic_map_finalize(bmap);
4693 return bmap;
4696 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4698 * In particular, if this div is of the form d = floor(f/m),
4699 * then add the constraint
4701 * f - m d >= 0
4703 * if sign < 0 or the constraint
4705 * -(f-(m-1)) + m d >= 0
4707 * if sign > 0.
4709 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4710 unsigned div, int sign)
4712 unsigned total;
4713 unsigned div_pos;
4715 if (!bmap)
4716 return -1;
4718 total = isl_basic_map_total_dim(bmap);
4719 div_pos = total - bmap->n_div + div;
4721 if (sign < 0)
4722 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4723 else
4724 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4727 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4729 return isl_basic_map_add_div_constraints(bset, div);
4732 struct isl_basic_set *isl_basic_map_underlying_set(
4733 struct isl_basic_map *bmap)
4735 if (!bmap)
4736 goto error;
4737 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4738 bmap->n_div == 0 &&
4739 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4740 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4741 return bset_from_bmap(bmap);
4742 bmap = isl_basic_map_cow(bmap);
4743 if (!bmap)
4744 goto error;
4745 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4746 if (!bmap->dim)
4747 goto error;
4748 bmap->extra -= bmap->n_div;
4749 bmap->n_div = 0;
4750 bmap = isl_basic_map_finalize(bmap);
4751 return bset_from_bmap(bmap);
4752 error:
4753 isl_basic_map_free(bmap);
4754 return NULL;
4757 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4758 __isl_take isl_basic_set *bset)
4760 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4763 /* Replace each element in "list" by the result of applying
4764 * isl_basic_map_underlying_set to the element.
4766 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4767 __isl_take isl_basic_map_list *list)
4769 int i, n;
4771 if (!list)
4772 return NULL;
4774 n = isl_basic_map_list_n_basic_map(list);
4775 for (i = 0; i < n; ++i) {
4776 isl_basic_map *bmap;
4777 isl_basic_set *bset;
4779 bmap = isl_basic_map_list_get_basic_map(list, i);
4780 bset = isl_basic_set_underlying_set(bmap);
4781 list = isl_basic_set_list_set_basic_set(list, i, bset);
4784 return list;
4787 struct isl_basic_map *isl_basic_map_overlying_set(
4788 struct isl_basic_set *bset, struct isl_basic_map *like)
4790 struct isl_basic_map *bmap;
4791 struct isl_ctx *ctx;
4792 unsigned total;
4793 int i;
4795 if (!bset || !like)
4796 goto error;
4797 ctx = bset->ctx;
4798 isl_assert(ctx, bset->n_div == 0, goto error);
4799 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4800 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4801 goto error);
4802 if (like->n_div == 0) {
4803 isl_space *space = isl_basic_map_get_space(like);
4804 isl_basic_map_free(like);
4805 return isl_basic_map_reset_space(bset, space);
4807 bset = isl_basic_set_cow(bset);
4808 if (!bset)
4809 goto error;
4810 total = bset->dim->n_out + bset->extra;
4811 bmap = bset_to_bmap(bset);
4812 isl_space_free(bmap->dim);
4813 bmap->dim = isl_space_copy(like->dim);
4814 if (!bmap->dim)
4815 goto error;
4816 bmap->n_div = like->n_div;
4817 bmap->extra += like->n_div;
4818 if (bmap->extra) {
4819 unsigned ltotal;
4820 isl_int **div;
4821 ltotal = total - bmap->extra + like->extra;
4822 if (ltotal > total)
4823 ltotal = total;
4824 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4825 bmap->extra * (1 + 1 + total));
4826 if (isl_blk_is_error(bmap->block2))
4827 goto error;
4828 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4829 if (!div)
4830 goto error;
4831 bmap->div = div;
4832 for (i = 0; i < bmap->extra; ++i)
4833 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4834 for (i = 0; i < like->n_div; ++i) {
4835 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4836 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4838 bmap = isl_basic_map_add_known_div_constraints(bmap);
4840 isl_basic_map_free(like);
4841 bmap = isl_basic_map_simplify(bmap);
4842 bmap = isl_basic_map_finalize(bmap);
4843 return bmap;
4844 error:
4845 isl_basic_map_free(like);
4846 isl_basic_set_free(bset);
4847 return NULL;
4850 struct isl_basic_set *isl_basic_set_from_underlying_set(
4851 struct isl_basic_set *bset, struct isl_basic_set *like)
4853 return bset_from_bmap(isl_basic_map_overlying_set(bset,
4854 bset_to_bmap(like)));
4857 struct isl_set *isl_set_from_underlying_set(
4858 struct isl_set *set, struct isl_basic_set *like)
4860 int i;
4862 if (!set || !like)
4863 goto error;
4864 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4865 goto error);
4866 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4867 isl_basic_set_free(like);
4868 return set;
4870 set = isl_set_cow(set);
4871 if (!set)
4872 goto error;
4873 for (i = 0; i < set->n; ++i) {
4874 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4875 isl_basic_set_copy(like));
4876 if (!set->p[i])
4877 goto error;
4879 isl_space_free(set->dim);
4880 set->dim = isl_space_copy(like->dim);
4881 if (!set->dim)
4882 goto error;
4883 isl_basic_set_free(like);
4884 return set;
4885 error:
4886 isl_basic_set_free(like);
4887 isl_set_free(set);
4888 return NULL;
4891 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4893 int i;
4895 map = isl_map_cow(map);
4896 if (!map)
4897 return NULL;
4898 map->dim = isl_space_cow(map->dim);
4899 if (!map->dim)
4900 goto error;
4902 for (i = 1; i < map->n; ++i)
4903 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4904 goto error);
4905 for (i = 0; i < map->n; ++i) {
4906 map->p[i] = bset_to_bmap(
4907 isl_basic_map_underlying_set(map->p[i]));
4908 if (!map->p[i])
4909 goto error;
4911 if (map->n == 0)
4912 map->dim = isl_space_underlying(map->dim, 0);
4913 else {
4914 isl_space_free(map->dim);
4915 map->dim = isl_space_copy(map->p[0]->dim);
4917 if (!map->dim)
4918 goto error;
4919 return set_from_map(map);
4920 error:
4921 isl_map_free(map);
4922 return NULL;
4925 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4927 return set_from_map(isl_map_underlying_set(set_to_map(set)));
4930 /* Replace the space of "bmap" by "space".
4932 * If the space of "bmap" is identical to "space" (including the identifiers
4933 * of the input and output dimensions), then simply return the original input.
4935 __isl_give isl_basic_map *isl_basic_map_reset_space(
4936 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4938 isl_bool equal;
4940 if (!bmap)
4941 goto error;
4942 equal = isl_space_is_equal(bmap->dim, space);
4943 if (equal >= 0 && equal)
4944 equal = isl_space_match(bmap->dim, isl_dim_in,
4945 space, isl_dim_in);
4946 if (equal >= 0 && equal)
4947 equal = isl_space_match(bmap->dim, isl_dim_out,
4948 space, isl_dim_out);
4949 if (equal < 0)
4950 goto error;
4951 if (equal) {
4952 isl_space_free(space);
4953 return bmap;
4955 bmap = isl_basic_map_cow(bmap);
4956 if (!bmap || !space)
4957 goto error;
4959 isl_space_free(bmap->dim);
4960 bmap->dim = space;
4962 bmap = isl_basic_map_finalize(bmap);
4964 return bmap;
4965 error:
4966 isl_basic_map_free(bmap);
4967 isl_space_free(space);
4968 return NULL;
4971 __isl_give isl_basic_set *isl_basic_set_reset_space(
4972 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4974 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
4975 dim));
4978 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4979 __isl_take isl_space *dim)
4981 int i;
4983 map = isl_map_cow(map);
4984 if (!map || !dim)
4985 goto error;
4987 for (i = 0; i < map->n; ++i) {
4988 map->p[i] = isl_basic_map_reset_space(map->p[i],
4989 isl_space_copy(dim));
4990 if (!map->p[i])
4991 goto error;
4993 isl_space_free(map->dim);
4994 map->dim = dim;
4996 return map;
4997 error:
4998 isl_map_free(map);
4999 isl_space_free(dim);
5000 return NULL;
5003 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5004 __isl_take isl_space *dim)
5006 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5009 /* Compute the parameter domain of the given basic set.
5011 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5013 isl_space *space;
5014 unsigned n;
5016 if (isl_basic_set_is_params(bset))
5017 return bset;
5019 n = isl_basic_set_dim(bset, isl_dim_set);
5020 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5021 space = isl_basic_set_get_space(bset);
5022 space = isl_space_params(space);
5023 bset = isl_basic_set_reset_space(bset, space);
5024 return bset;
5027 /* Construct a zero-dimensional basic set with the given parameter domain.
5029 __isl_give isl_basic_set *isl_basic_set_from_params(
5030 __isl_take isl_basic_set *bset)
5032 isl_space *space;
5033 space = isl_basic_set_get_space(bset);
5034 space = isl_space_set_from_params(space);
5035 bset = isl_basic_set_reset_space(bset, space);
5036 return bset;
5039 /* Compute the parameter domain of the given set.
5041 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5043 isl_space *space;
5044 unsigned n;
5046 if (isl_set_is_params(set))
5047 return set;
5049 n = isl_set_dim(set, isl_dim_set);
5050 set = isl_set_project_out(set, isl_dim_set, 0, n);
5051 space = isl_set_get_space(set);
5052 space = isl_space_params(space);
5053 set = isl_set_reset_space(set, space);
5054 return set;
5057 /* Construct a zero-dimensional set with the given parameter domain.
5059 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5061 isl_space *space;
5062 space = isl_set_get_space(set);
5063 space = isl_space_set_from_params(space);
5064 set = isl_set_reset_space(set, space);
5065 return set;
5068 /* Compute the parameter domain of the given map.
5070 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5072 isl_space *space;
5073 unsigned n;
5075 n = isl_map_dim(map, isl_dim_in);
5076 map = isl_map_project_out(map, isl_dim_in, 0, n);
5077 n = isl_map_dim(map, isl_dim_out);
5078 map = isl_map_project_out(map, isl_dim_out, 0, n);
5079 space = isl_map_get_space(map);
5080 space = isl_space_params(space);
5081 map = isl_map_reset_space(map, space);
5082 return map;
5085 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5087 isl_space *space;
5088 unsigned n_out;
5090 if (!bmap)
5091 return NULL;
5092 space = isl_space_domain(isl_basic_map_get_space(bmap));
5094 n_out = isl_basic_map_n_out(bmap);
5095 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5097 return isl_basic_map_reset_space(bmap, space);
5100 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5102 if (!bmap)
5103 return -1;
5104 return isl_space_may_be_set(bmap->dim);
5107 /* Is this basic map actually a set?
5108 * Users should never call this function. Outside of isl,
5109 * the type should indicate whether something is a set or a map.
5111 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5113 if (!bmap)
5114 return -1;
5115 return isl_space_is_set(bmap->dim);
5118 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5120 if (!bmap)
5121 return NULL;
5122 if (isl_basic_map_is_set(bmap))
5123 return bmap;
5124 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5127 __isl_give isl_basic_map *isl_basic_map_domain_map(
5128 __isl_take isl_basic_map *bmap)
5130 int i, k;
5131 isl_space *dim;
5132 isl_basic_map *domain;
5133 int nparam, n_in, n_out;
5134 unsigned total;
5136 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5137 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5138 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5140 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5141 domain = isl_basic_map_universe(dim);
5143 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5144 bmap = isl_basic_map_apply_range(bmap, domain);
5145 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5147 total = isl_basic_map_total_dim(bmap);
5149 for (i = 0; i < n_in; ++i) {
5150 k = isl_basic_map_alloc_equality(bmap);
5151 if (k < 0)
5152 goto error;
5153 isl_seq_clr(bmap->eq[k], 1 + total);
5154 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5155 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5158 bmap = isl_basic_map_gauss(bmap, NULL);
5159 return isl_basic_map_finalize(bmap);
5160 error:
5161 isl_basic_map_free(bmap);
5162 return NULL;
5165 __isl_give isl_basic_map *isl_basic_map_range_map(
5166 __isl_take isl_basic_map *bmap)
5168 int i, k;
5169 isl_space *dim;
5170 isl_basic_map *range;
5171 int nparam, n_in, n_out;
5172 unsigned total;
5174 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5175 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5176 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5178 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5179 range = isl_basic_map_universe(dim);
5181 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5182 bmap = isl_basic_map_apply_range(bmap, range);
5183 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5185 total = isl_basic_map_total_dim(bmap);
5187 for (i = 0; i < n_out; ++i) {
5188 k = isl_basic_map_alloc_equality(bmap);
5189 if (k < 0)
5190 goto error;
5191 isl_seq_clr(bmap->eq[k], 1 + total);
5192 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5193 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5196 bmap = isl_basic_map_gauss(bmap, NULL);
5197 return isl_basic_map_finalize(bmap);
5198 error:
5199 isl_basic_map_free(bmap);
5200 return NULL;
5203 int isl_map_may_be_set(__isl_keep isl_map *map)
5205 if (!map)
5206 return -1;
5207 return isl_space_may_be_set(map->dim);
5210 /* Is this map actually a set?
5211 * Users should never call this function. Outside of isl,
5212 * the type should indicate whether something is a set or a map.
5214 int isl_map_is_set(__isl_keep isl_map *map)
5216 if (!map)
5217 return -1;
5218 return isl_space_is_set(map->dim);
5221 struct isl_set *isl_map_range(struct isl_map *map)
5223 int i;
5224 struct isl_set *set;
5226 if (!map)
5227 goto error;
5228 if (isl_map_is_set(map))
5229 return set_from_map(map);
5231 map = isl_map_cow(map);
5232 if (!map)
5233 goto error;
5235 set = set_from_map(map);
5236 set->dim = isl_space_range(set->dim);
5237 if (!set->dim)
5238 goto error;
5239 for (i = 0; i < map->n; ++i) {
5240 set->p[i] = isl_basic_map_range(map->p[i]);
5241 if (!set->p[i])
5242 goto error;
5244 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5245 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5246 return set;
5247 error:
5248 isl_map_free(map);
5249 return NULL;
5252 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5254 int i;
5256 map = isl_map_cow(map);
5257 if (!map)
5258 return NULL;
5260 map->dim = isl_space_domain_map(map->dim);
5261 if (!map->dim)
5262 goto error;
5263 for (i = 0; i < map->n; ++i) {
5264 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5265 if (!map->p[i])
5266 goto error;
5268 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5269 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5270 return map;
5271 error:
5272 isl_map_free(map);
5273 return NULL;
5276 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5278 int i;
5279 isl_space *range_dim;
5281 map = isl_map_cow(map);
5282 if (!map)
5283 return NULL;
5285 range_dim = isl_space_range(isl_map_get_space(map));
5286 range_dim = isl_space_from_range(range_dim);
5287 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5288 map->dim = isl_space_join(map->dim, range_dim);
5289 if (!map->dim)
5290 goto error;
5291 for (i = 0; i < map->n; ++i) {
5292 map->p[i] = isl_basic_map_range_map(map->p[i]);
5293 if (!map->p[i])
5294 goto error;
5296 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5297 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5298 return map;
5299 error:
5300 isl_map_free(map);
5301 return NULL;
5304 /* Given a wrapped map of the form A[B -> C],
5305 * return the map A[B -> C] -> B.
5307 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5309 isl_id *id;
5310 isl_map *map;
5312 if (!set)
5313 return NULL;
5314 if (!isl_set_has_tuple_id(set))
5315 return isl_map_domain_map(isl_set_unwrap(set));
5317 id = isl_set_get_tuple_id(set);
5318 map = isl_map_domain_map(isl_set_unwrap(set));
5319 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5321 return map;
5324 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5325 __isl_take isl_space *dim)
5327 int i;
5328 struct isl_map *map = NULL;
5330 set = isl_set_cow(set);
5331 if (!set || !dim)
5332 goto error;
5333 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5334 goto error);
5335 map = set_to_map(set);
5336 for (i = 0; i < set->n; ++i) {
5337 map->p[i] = basic_map_from_basic_set(
5338 set->p[i], isl_space_copy(dim));
5339 if (!map->p[i])
5340 goto error;
5342 isl_space_free(map->dim);
5343 map->dim = dim;
5344 return map;
5345 error:
5346 isl_space_free(dim);
5347 isl_set_free(set);
5348 return NULL;
5351 __isl_give isl_basic_map *isl_basic_map_from_domain(
5352 __isl_take isl_basic_set *bset)
5354 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5357 __isl_give isl_basic_map *isl_basic_map_from_range(
5358 __isl_take isl_basic_set *bset)
5360 isl_space *space;
5361 space = isl_basic_set_get_space(bset);
5362 space = isl_space_from_range(space);
5363 bset = isl_basic_set_reset_space(bset, space);
5364 return bset_to_bmap(bset);
5367 /* Create a relation with the given set as range.
5368 * The domain of the created relation is a zero-dimensional
5369 * flat anonymous space.
5371 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5373 isl_space *space;
5374 space = isl_set_get_space(set);
5375 space = isl_space_from_range(space);
5376 set = isl_set_reset_space(set, space);
5377 return set_to_map(set);
5380 /* Create a relation with the given set as domain.
5381 * The range of the created relation is a zero-dimensional
5382 * flat anonymous space.
5384 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5386 return isl_map_reverse(isl_map_from_range(set));
5389 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5390 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5392 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5395 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5396 __isl_take isl_set *range)
5398 return isl_map_apply_range(isl_map_reverse(domain), range);
5401 /* Return a newly allocated isl_map with given space and flags and
5402 * room for "n" basic maps.
5403 * Make sure that all cached information is cleared.
5405 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5406 unsigned flags)
5408 struct isl_map *map;
5410 if (!space)
5411 return NULL;
5412 if (n < 0)
5413 isl_die(space->ctx, isl_error_internal,
5414 "negative number of basic maps", goto error);
5415 map = isl_calloc(space->ctx, struct isl_map,
5416 sizeof(struct isl_map) +
5417 (n - 1) * sizeof(struct isl_basic_map *));
5418 if (!map)
5419 goto error;
5421 map->ctx = space->ctx;
5422 isl_ctx_ref(map->ctx);
5423 map->ref = 1;
5424 map->size = n;
5425 map->n = 0;
5426 map->dim = space;
5427 map->flags = flags;
5428 return map;
5429 error:
5430 isl_space_free(space);
5431 return NULL;
5434 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5435 unsigned nparam, unsigned in, unsigned out, int n,
5436 unsigned flags)
5438 struct isl_map *map;
5439 isl_space *dims;
5441 dims = isl_space_alloc(ctx, nparam, in, out);
5442 if (!dims)
5443 return NULL;
5445 map = isl_map_alloc_space(dims, n, flags);
5446 return map;
5449 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5451 struct isl_basic_map *bmap;
5452 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5453 bmap = isl_basic_map_set_to_empty(bmap);
5454 return bmap;
5457 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5459 struct isl_basic_set *bset;
5460 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5461 bset = isl_basic_set_set_to_empty(bset);
5462 return bset;
5465 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5467 struct isl_basic_map *bmap;
5468 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5469 bmap = isl_basic_map_finalize(bmap);
5470 return bmap;
5473 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5475 struct isl_basic_set *bset;
5476 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5477 bset = isl_basic_set_finalize(bset);
5478 return bset;
5481 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5483 int i;
5484 unsigned total = isl_space_dim(dim, isl_dim_all);
5485 isl_basic_map *bmap;
5487 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5488 for (i = 0; i < total; ++i) {
5489 int k = isl_basic_map_alloc_inequality(bmap);
5490 if (k < 0)
5491 goto error;
5492 isl_seq_clr(bmap->ineq[k], 1 + total);
5493 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5495 return bmap;
5496 error:
5497 isl_basic_map_free(bmap);
5498 return NULL;
5501 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5503 return isl_basic_map_nat_universe(dim);
5506 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5508 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5511 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5513 return isl_map_nat_universe(dim);
5516 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5518 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5521 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5523 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5526 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5528 struct isl_map *map;
5529 if (!dim)
5530 return NULL;
5531 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5532 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5533 return map;
5536 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5538 struct isl_set *set;
5539 if (!dim)
5540 return NULL;
5541 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5542 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5543 return set;
5546 struct isl_map *isl_map_dup(struct isl_map *map)
5548 int i;
5549 struct isl_map *dup;
5551 if (!map)
5552 return NULL;
5553 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5554 for (i = 0; i < map->n; ++i)
5555 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5556 return dup;
5559 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5560 __isl_take isl_basic_map *bmap)
5562 if (!bmap || !map)
5563 goto error;
5564 if (isl_basic_map_plain_is_empty(bmap)) {
5565 isl_basic_map_free(bmap);
5566 return map;
5568 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5569 isl_assert(map->ctx, map->n < map->size, goto error);
5570 map->p[map->n] = bmap;
5571 map->n++;
5572 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5573 return map;
5574 error:
5575 if (map)
5576 isl_map_free(map);
5577 if (bmap)
5578 isl_basic_map_free(bmap);
5579 return NULL;
5582 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5584 int i;
5586 if (!map)
5587 return NULL;
5589 if (--map->ref > 0)
5590 return NULL;
5592 clear_caches(map);
5593 isl_ctx_deref(map->ctx);
5594 for (i = 0; i < map->n; ++i)
5595 isl_basic_map_free(map->p[i]);
5596 isl_space_free(map->dim);
5597 free(map);
5599 return NULL;
5602 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5603 struct isl_basic_map *bmap, unsigned pos, int value)
5605 int j;
5607 bmap = isl_basic_map_cow(bmap);
5608 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5609 j = isl_basic_map_alloc_equality(bmap);
5610 if (j < 0)
5611 goto error;
5612 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5613 isl_int_set_si(bmap->eq[j][pos], -1);
5614 isl_int_set_si(bmap->eq[j][0], value);
5615 bmap = isl_basic_map_simplify(bmap);
5616 return isl_basic_map_finalize(bmap);
5617 error:
5618 isl_basic_map_free(bmap);
5619 return NULL;
5622 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5623 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5625 int j;
5627 bmap = isl_basic_map_cow(bmap);
5628 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5629 j = isl_basic_map_alloc_equality(bmap);
5630 if (j < 0)
5631 goto error;
5632 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5633 isl_int_set_si(bmap->eq[j][pos], -1);
5634 isl_int_set(bmap->eq[j][0], value);
5635 bmap = isl_basic_map_simplify(bmap);
5636 return isl_basic_map_finalize(bmap);
5637 error:
5638 isl_basic_map_free(bmap);
5639 return NULL;
5642 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5643 enum isl_dim_type type, unsigned pos, int value)
5645 if (!bmap)
5646 return NULL;
5647 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5648 return isl_basic_map_fix_pos_si(bmap,
5649 isl_basic_map_offset(bmap, type) + pos, value);
5650 error:
5651 isl_basic_map_free(bmap);
5652 return NULL;
5655 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5656 enum isl_dim_type type, unsigned pos, isl_int value)
5658 if (!bmap)
5659 return NULL;
5660 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5661 return isl_basic_map_fix_pos(bmap,
5662 isl_basic_map_offset(bmap, type) + pos, value);
5663 error:
5664 isl_basic_map_free(bmap);
5665 return NULL;
5668 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5669 * to be equal to "v".
5671 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5672 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5674 if (!bmap || !v)
5675 goto error;
5676 if (!isl_val_is_int(v))
5677 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5678 "expecting integer value", goto error);
5679 if (pos >= isl_basic_map_dim(bmap, type))
5680 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5681 "index out of bounds", goto error);
5682 pos += isl_basic_map_offset(bmap, type);
5683 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5684 isl_val_free(v);
5685 return bmap;
5686 error:
5687 isl_basic_map_free(bmap);
5688 isl_val_free(v);
5689 return NULL;
5692 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5693 * to be equal to "v".
5695 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5696 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5698 return isl_basic_map_fix_val(bset, type, pos, v);
5701 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5702 enum isl_dim_type type, unsigned pos, int value)
5704 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5705 type, pos, value));
5708 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5709 enum isl_dim_type type, unsigned pos, isl_int value)
5711 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5712 type, pos, value));
5715 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5716 unsigned input, int value)
5718 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5721 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5722 unsigned dim, int value)
5724 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5725 isl_dim_set, dim, value));
5728 static int remove_if_empty(__isl_keep isl_map *map, int i)
5730 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5732 if (empty < 0)
5733 return -1;
5734 if (!empty)
5735 return 0;
5737 isl_basic_map_free(map->p[i]);
5738 if (i != map->n - 1) {
5739 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5740 map->p[i] = map->p[map->n - 1];
5742 map->n--;
5744 return 0;
5747 /* Perform "fn" on each basic map of "map", where we may not be holding
5748 * the only reference to "map".
5749 * In particular, "fn" should be a semantics preserving operation
5750 * that we want to apply to all copies of "map". We therefore need
5751 * to be careful not to modify "map" in a way that breaks "map"
5752 * in case anything goes wrong.
5754 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5755 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5757 struct isl_basic_map *bmap;
5758 int i;
5760 if (!map)
5761 return NULL;
5763 for (i = map->n - 1; i >= 0; --i) {
5764 bmap = isl_basic_map_copy(map->p[i]);
5765 bmap = fn(bmap);
5766 if (!bmap)
5767 goto error;
5768 isl_basic_map_free(map->p[i]);
5769 map->p[i] = bmap;
5770 if (remove_if_empty(map, i) < 0)
5771 goto error;
5774 return map;
5775 error:
5776 isl_map_free(map);
5777 return NULL;
5780 struct isl_map *isl_map_fix_si(struct isl_map *map,
5781 enum isl_dim_type type, unsigned pos, int value)
5783 int i;
5785 map = isl_map_cow(map);
5786 if (!map)
5787 return NULL;
5789 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5790 for (i = map->n - 1; i >= 0; --i) {
5791 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5792 if (remove_if_empty(map, i) < 0)
5793 goto error;
5795 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5796 return map;
5797 error:
5798 isl_map_free(map);
5799 return NULL;
5802 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5803 enum isl_dim_type type, unsigned pos, int value)
5805 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5808 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5809 enum isl_dim_type type, unsigned pos, isl_int value)
5811 int i;
5813 map = isl_map_cow(map);
5814 if (!map)
5815 return NULL;
5817 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5818 for (i = 0; i < map->n; ++i) {
5819 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5820 if (!map->p[i])
5821 goto error;
5823 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5824 return map;
5825 error:
5826 isl_map_free(map);
5827 return NULL;
5830 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5831 enum isl_dim_type type, unsigned pos, isl_int value)
5833 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5836 /* Fix the value of the variable at position "pos" of type "type" of "map"
5837 * to be equal to "v".
5839 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5840 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5842 int i;
5844 map = isl_map_cow(map);
5845 if (!map || !v)
5846 goto error;
5848 if (!isl_val_is_int(v))
5849 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5850 "expecting integer value", goto error);
5851 if (pos >= isl_map_dim(map, type))
5852 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5853 "index out of bounds", goto error);
5854 for (i = map->n - 1; i >= 0; --i) {
5855 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5856 isl_val_copy(v));
5857 if (remove_if_empty(map, i) < 0)
5858 goto error;
5860 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5861 isl_val_free(v);
5862 return map;
5863 error:
5864 isl_map_free(map);
5865 isl_val_free(v);
5866 return NULL;
5869 /* Fix the value of the variable at position "pos" of type "type" of "set"
5870 * to be equal to "v".
5872 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5873 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5875 return isl_map_fix_val(set, type, pos, v);
5878 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5879 unsigned input, int value)
5881 return isl_map_fix_si(map, isl_dim_in, input, value);
5884 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5886 return set_from_map(isl_map_fix_si(set_to_map(set),
5887 isl_dim_set, dim, value));
5890 static __isl_give isl_basic_map *basic_map_bound_si(
5891 __isl_take isl_basic_map *bmap,
5892 enum isl_dim_type type, unsigned pos, int value, int upper)
5894 int j;
5896 if (!bmap)
5897 return NULL;
5898 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5899 pos += isl_basic_map_offset(bmap, type);
5900 bmap = isl_basic_map_cow(bmap);
5901 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5902 j = isl_basic_map_alloc_inequality(bmap);
5903 if (j < 0)
5904 goto error;
5905 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5906 if (upper) {
5907 isl_int_set_si(bmap->ineq[j][pos], -1);
5908 isl_int_set_si(bmap->ineq[j][0], value);
5909 } else {
5910 isl_int_set_si(bmap->ineq[j][pos], 1);
5911 isl_int_set_si(bmap->ineq[j][0], -value);
5913 bmap = isl_basic_map_simplify(bmap);
5914 return isl_basic_map_finalize(bmap);
5915 error:
5916 isl_basic_map_free(bmap);
5917 return NULL;
5920 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5921 __isl_take isl_basic_map *bmap,
5922 enum isl_dim_type type, unsigned pos, int value)
5924 return basic_map_bound_si(bmap, type, pos, value, 0);
5927 /* Constrain the values of the given dimension to be no greater than "value".
5929 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5930 __isl_take isl_basic_map *bmap,
5931 enum isl_dim_type type, unsigned pos, int value)
5933 return basic_map_bound_si(bmap, type, pos, value, 1);
5936 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5937 unsigned dim, isl_int value)
5939 int j;
5941 bset = isl_basic_set_cow(bset);
5942 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5943 j = isl_basic_set_alloc_inequality(bset);
5944 if (j < 0)
5945 goto error;
5946 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5947 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5948 isl_int_neg(bset->ineq[j][0], value);
5949 bset = isl_basic_set_simplify(bset);
5950 return isl_basic_set_finalize(bset);
5951 error:
5952 isl_basic_set_free(bset);
5953 return NULL;
5956 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5957 enum isl_dim_type type, unsigned pos, int value, int upper)
5959 int i;
5961 map = isl_map_cow(map);
5962 if (!map)
5963 return NULL;
5965 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5966 for (i = 0; i < map->n; ++i) {
5967 map->p[i] = basic_map_bound_si(map->p[i],
5968 type, pos, value, upper);
5969 if (!map->p[i])
5970 goto error;
5972 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5973 return map;
5974 error:
5975 isl_map_free(map);
5976 return NULL;
5979 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5980 enum isl_dim_type type, unsigned pos, int value)
5982 return map_bound_si(map, type, pos, value, 0);
5985 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5986 enum isl_dim_type type, unsigned pos, int value)
5988 return map_bound_si(map, type, pos, value, 1);
5991 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5992 enum isl_dim_type type, unsigned pos, int value)
5994 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
5995 type, pos, value));
5998 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5999 enum isl_dim_type type, unsigned pos, int value)
6001 return isl_map_upper_bound_si(set, type, pos, value);
6004 /* Bound the given variable of "bmap" from below (or above is "upper"
6005 * is set) to "value".
6007 static __isl_give isl_basic_map *basic_map_bound(
6008 __isl_take isl_basic_map *bmap,
6009 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6011 int j;
6013 if (!bmap)
6014 return NULL;
6015 if (pos >= isl_basic_map_dim(bmap, type))
6016 isl_die(bmap->ctx, isl_error_invalid,
6017 "index out of bounds", goto error);
6018 pos += isl_basic_map_offset(bmap, type);
6019 bmap = isl_basic_map_cow(bmap);
6020 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6021 j = isl_basic_map_alloc_inequality(bmap);
6022 if (j < 0)
6023 goto error;
6024 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6025 if (upper) {
6026 isl_int_set_si(bmap->ineq[j][pos], -1);
6027 isl_int_set(bmap->ineq[j][0], value);
6028 } else {
6029 isl_int_set_si(bmap->ineq[j][pos], 1);
6030 isl_int_neg(bmap->ineq[j][0], value);
6032 bmap = isl_basic_map_simplify(bmap);
6033 return isl_basic_map_finalize(bmap);
6034 error:
6035 isl_basic_map_free(bmap);
6036 return NULL;
6039 /* Bound the given variable of "map" from below (or above is "upper"
6040 * is set) to "value".
6042 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6043 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6045 int i;
6047 map = isl_map_cow(map);
6048 if (!map)
6049 return NULL;
6051 if (pos >= isl_map_dim(map, type))
6052 isl_die(map->ctx, isl_error_invalid,
6053 "index out of bounds", goto error);
6054 for (i = map->n - 1; i >= 0; --i) {
6055 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6056 if (remove_if_empty(map, i) < 0)
6057 goto error;
6059 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6060 return map;
6061 error:
6062 isl_map_free(map);
6063 return NULL;
6066 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6067 enum isl_dim_type type, unsigned pos, isl_int value)
6069 return map_bound(map, type, pos, value, 0);
6072 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6073 enum isl_dim_type type, unsigned pos, isl_int value)
6075 return map_bound(map, type, pos, value, 1);
6078 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6079 enum isl_dim_type type, unsigned pos, isl_int value)
6081 return isl_map_lower_bound(set, type, pos, value);
6084 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6085 enum isl_dim_type type, unsigned pos, isl_int value)
6087 return isl_map_upper_bound(set, type, pos, value);
6090 /* Force the values of the variable at position "pos" of type "type" of "set"
6091 * to be no smaller than "value".
6093 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6094 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6096 if (!value)
6097 goto error;
6098 if (!isl_val_is_int(value))
6099 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6100 "expecting integer value", goto error);
6101 set = isl_set_lower_bound(set, type, pos, value->n);
6102 isl_val_free(value);
6103 return set;
6104 error:
6105 isl_val_free(value);
6106 isl_set_free(set);
6107 return NULL;
6110 /* Force the values of the variable at position "pos" of type "type" of "set"
6111 * to be no greater than "value".
6113 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6114 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6116 if (!value)
6117 goto error;
6118 if (!isl_val_is_int(value))
6119 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6120 "expecting integer value", goto error);
6121 set = isl_set_upper_bound(set, type, pos, value->n);
6122 isl_val_free(value);
6123 return set;
6124 error:
6125 isl_val_free(value);
6126 isl_set_free(set);
6127 return NULL;
6130 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6131 isl_int value)
6133 int i;
6135 set = isl_set_cow(set);
6136 if (!set)
6137 return NULL;
6139 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6140 for (i = 0; i < set->n; ++i) {
6141 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6142 if (!set->p[i])
6143 goto error;
6145 return set;
6146 error:
6147 isl_set_free(set);
6148 return NULL;
6151 struct isl_map *isl_map_reverse(struct isl_map *map)
6153 int i;
6155 map = isl_map_cow(map);
6156 if (!map)
6157 return NULL;
6159 map->dim = isl_space_reverse(map->dim);
6160 if (!map->dim)
6161 goto error;
6162 for (i = 0; i < map->n; ++i) {
6163 map->p[i] = isl_basic_map_reverse(map->p[i]);
6164 if (!map->p[i])
6165 goto error;
6167 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6168 return map;
6169 error:
6170 isl_map_free(map);
6171 return NULL;
6174 #undef TYPE
6175 #define TYPE isl_pw_multi_aff
6176 #undef SUFFIX
6177 #define SUFFIX _pw_multi_aff
6178 #undef EMPTY
6179 #define EMPTY isl_pw_multi_aff_empty
6180 #undef ADD
6181 #define ADD isl_pw_multi_aff_union_add
6182 #include "isl_map_lexopt_templ.c"
6184 /* Given a map "map", compute the lexicographically minimal
6185 * (or maximal) image element for each domain element in dom,
6186 * in the form of an isl_pw_multi_aff.
6187 * If "empty" is not NULL, then set *empty to those elements in dom that
6188 * do not have an image element.
6189 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6190 * should be computed over the domain of "map". "empty" is also NULL
6191 * in this case.
6193 * We first compute the lexicographically minimal or maximal element
6194 * in the first basic map. This results in a partial solution "res"
6195 * and a subset "todo" of dom that still need to be handled.
6196 * We then consider each of the remaining maps in "map" and successively
6197 * update both "res" and "todo".
6198 * If "empty" is NULL, then the todo sets are not needed and therefore
6199 * also not computed.
6201 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6202 __isl_take isl_map *map, __isl_take isl_set *dom,
6203 __isl_give isl_set **empty, unsigned flags)
6205 int i;
6206 int full;
6207 isl_pw_multi_aff *res;
6208 isl_set *todo;
6210 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6211 if (!map || (!full && !dom))
6212 goto error;
6214 if (isl_map_plain_is_empty(map)) {
6215 if (empty)
6216 *empty = dom;
6217 else
6218 isl_set_free(dom);
6219 return isl_pw_multi_aff_from_map(map);
6222 res = basic_map_partial_lexopt_pw_multi_aff(
6223 isl_basic_map_copy(map->p[0]),
6224 isl_set_copy(dom), empty, flags);
6226 if (empty)
6227 todo = *empty;
6228 for (i = 1; i < map->n; ++i) {
6229 isl_pw_multi_aff *res_i;
6231 res_i = basic_map_partial_lexopt_pw_multi_aff(
6232 isl_basic_map_copy(map->p[i]),
6233 isl_set_copy(dom), empty, flags);
6235 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6236 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6237 else
6238 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6240 if (empty)
6241 todo = isl_set_intersect(todo, *empty);
6244 isl_set_free(dom);
6245 isl_map_free(map);
6247 if (empty)
6248 *empty = todo;
6250 return res;
6251 error:
6252 if (empty)
6253 *empty = NULL;
6254 isl_set_free(dom);
6255 isl_map_free(map);
6256 return NULL;
6259 #undef TYPE
6260 #define TYPE isl_map
6261 #undef SUFFIX
6262 #define SUFFIX
6263 #undef EMPTY
6264 #define EMPTY isl_map_empty
6265 #undef ADD
6266 #define ADD isl_map_union_disjoint
6267 #include "isl_map_lexopt_templ.c"
6269 /* Given a map "map", compute the lexicographically minimal
6270 * (or maximal) image element for each domain element in "dom",
6271 * in the form of an isl_map.
6272 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6273 * do not have an image element.
6274 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6275 * should be computed over the domain of "map". "empty" is also NULL
6276 * in this case.
6278 * If the input consists of more than one disjunct, then first
6279 * compute the desired result in the form of an isl_pw_multi_aff and
6280 * then convert that into an isl_map.
6282 * This function used to have an explicit implementation in terms
6283 * of isl_maps, but it would continually intersect the domains of
6284 * partial results with the complement of the domain of the next
6285 * partial solution, potentially leading to an explosion in the number
6286 * of disjuncts if there are several disjuncts in the input.
6287 * An even earlier implementation of this function would look for
6288 * better results in the domain of the partial result and for extra
6289 * results in the complement of this domain, which would lead to
6290 * even more splintering.
6292 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6293 __isl_take isl_map *map, __isl_take isl_set *dom,
6294 __isl_give isl_set **empty, unsigned flags)
6296 int full;
6297 struct isl_map *res;
6298 isl_pw_multi_aff *pma;
6300 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6301 if (!map || (!full && !dom))
6302 goto error;
6304 if (isl_map_plain_is_empty(map)) {
6305 if (empty)
6306 *empty = dom;
6307 else
6308 isl_set_free(dom);
6309 return map;
6312 if (map->n == 1) {
6313 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6314 dom, empty, flags);
6315 isl_map_free(map);
6316 return res;
6319 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6320 flags);
6321 return isl_map_from_pw_multi_aff(pma);
6322 error:
6323 if (empty)
6324 *empty = NULL;
6325 isl_set_free(dom);
6326 isl_map_free(map);
6327 return NULL;
6330 __isl_give isl_map *isl_map_partial_lexmax(
6331 __isl_take isl_map *map, __isl_take isl_set *dom,
6332 __isl_give isl_set **empty)
6334 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6337 __isl_give isl_map *isl_map_partial_lexmin(
6338 __isl_take isl_map *map, __isl_take isl_set *dom,
6339 __isl_give isl_set **empty)
6341 return isl_map_partial_lexopt(map, dom, empty, 0);
6344 __isl_give isl_set *isl_set_partial_lexmin(
6345 __isl_take isl_set *set, __isl_take isl_set *dom,
6346 __isl_give isl_set **empty)
6348 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6349 dom, empty));
6352 __isl_give isl_set *isl_set_partial_lexmax(
6353 __isl_take isl_set *set, __isl_take isl_set *dom,
6354 __isl_give isl_set **empty)
6356 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6357 dom, empty));
6360 /* Compute the lexicographic minimum (or maximum if "flags" includes
6361 * ISL_OPT_MAX) of "bset" over its parametric domain.
6363 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6364 unsigned flags)
6366 return isl_basic_map_lexopt(bset, flags);
6369 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6371 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6374 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6376 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6379 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6381 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6384 /* Compute the lexicographic minimum of "bset" over its parametric domain
6385 * for the purpose of quantifier elimination.
6386 * That is, find an explicit representation for all the existentially
6387 * quantified variables in "bset" by computing their lexicographic
6388 * minimum.
6390 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6391 __isl_take isl_basic_set *bset)
6393 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6396 /* Extract the first and only affine expression from list
6397 * and then add it to *pwaff with the given dom.
6398 * This domain is known to be disjoint from other domains
6399 * because of the way isl_basic_map_foreach_lexmax works.
6401 static int update_dim_opt(__isl_take isl_basic_set *dom,
6402 __isl_take isl_aff_list *list, void *user)
6404 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6405 isl_aff *aff;
6406 isl_pw_aff **pwaff = user;
6407 isl_pw_aff *pwaff_i;
6409 if (!list)
6410 goto error;
6411 if (isl_aff_list_n_aff(list) != 1)
6412 isl_die(ctx, isl_error_internal,
6413 "expecting single element list", goto error);
6415 aff = isl_aff_list_get_aff(list, 0);
6416 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6418 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6420 isl_aff_list_free(list);
6422 return 0;
6423 error:
6424 isl_basic_set_free(dom);
6425 isl_aff_list_free(list);
6426 return -1;
6429 /* Given a basic map with one output dimension, compute the minimum or
6430 * maximum of that dimension as an isl_pw_aff.
6432 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6433 * call update_dim_opt on each leaf of the result.
6435 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6436 int max)
6438 isl_space *dim = isl_basic_map_get_space(bmap);
6439 isl_pw_aff *pwaff;
6440 int r;
6442 dim = isl_space_from_domain(isl_space_domain(dim));
6443 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6444 pwaff = isl_pw_aff_empty(dim);
6446 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6447 if (r < 0)
6448 return isl_pw_aff_free(pwaff);
6450 return pwaff;
6453 /* Compute the minimum or maximum of the given output dimension
6454 * as a function of the parameters and the input dimensions,
6455 * but independently of the other output dimensions.
6457 * We first project out the other output dimension and then compute
6458 * the "lexicographic" maximum in each basic map, combining the results
6459 * using isl_pw_aff_union_max.
6461 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6462 int max)
6464 int i;
6465 isl_pw_aff *pwaff;
6466 unsigned n_out;
6468 n_out = isl_map_dim(map, isl_dim_out);
6469 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6470 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6471 if (!map)
6472 return NULL;
6474 if (map->n == 0) {
6475 isl_space *dim = isl_map_get_space(map);
6476 isl_map_free(map);
6477 return isl_pw_aff_empty(dim);
6480 pwaff = basic_map_dim_opt(map->p[0], max);
6481 for (i = 1; i < map->n; ++i) {
6482 isl_pw_aff *pwaff_i;
6484 pwaff_i = basic_map_dim_opt(map->p[i], max);
6485 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6488 isl_map_free(map);
6490 return pwaff;
6493 /* Compute the minimum of the given output dimension as a function of the
6494 * parameters and input dimensions, but independently of
6495 * the other output dimensions.
6497 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6499 return map_dim_opt(map, pos, 0);
6502 /* Compute the maximum of the given output dimension as a function of the
6503 * parameters and input dimensions, but independently of
6504 * the other output dimensions.
6506 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6508 return map_dim_opt(map, pos, 1);
6511 /* Compute the minimum or maximum of the given set dimension
6512 * as a function of the parameters,
6513 * but independently of the other set dimensions.
6515 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6516 int max)
6518 return map_dim_opt(set, pos, max);
6521 /* Compute the maximum of the given set dimension as a function of the
6522 * parameters, but independently of the other set dimensions.
6524 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6526 return set_dim_opt(set, pos, 1);
6529 /* Compute the minimum of the given set dimension as a function of the
6530 * parameters, but independently of the other set dimensions.
6532 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6534 return set_dim_opt(set, pos, 0);
6537 /* Apply a preimage specified by "mat" on the parameters of "bset".
6538 * bset is assumed to have only parameters and divs.
6540 static struct isl_basic_set *basic_set_parameter_preimage(
6541 struct isl_basic_set *bset, struct isl_mat *mat)
6543 unsigned nparam;
6545 if (!bset || !mat)
6546 goto error;
6548 bset->dim = isl_space_cow(bset->dim);
6549 if (!bset->dim)
6550 goto error;
6552 nparam = isl_basic_set_dim(bset, isl_dim_param);
6554 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6556 bset->dim->nparam = 0;
6557 bset->dim->n_out = nparam;
6558 bset = isl_basic_set_preimage(bset, mat);
6559 if (bset) {
6560 bset->dim->nparam = bset->dim->n_out;
6561 bset->dim->n_out = 0;
6563 return bset;
6564 error:
6565 isl_mat_free(mat);
6566 isl_basic_set_free(bset);
6567 return NULL;
6570 /* Apply a preimage specified by "mat" on the parameters of "set".
6571 * set is assumed to have only parameters and divs.
6573 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6574 __isl_take isl_mat *mat)
6576 isl_space *space;
6577 unsigned nparam;
6579 if (!set || !mat)
6580 goto error;
6582 nparam = isl_set_dim(set, isl_dim_param);
6584 if (mat->n_row != 1 + nparam)
6585 isl_die(isl_set_get_ctx(set), isl_error_internal,
6586 "unexpected number of rows", goto error);
6588 space = isl_set_get_space(set);
6589 space = isl_space_move_dims(space, isl_dim_set, 0,
6590 isl_dim_param, 0, nparam);
6591 set = isl_set_reset_space(set, space);
6592 set = isl_set_preimage(set, mat);
6593 nparam = isl_set_dim(set, isl_dim_out);
6594 space = isl_set_get_space(set);
6595 space = isl_space_move_dims(space, isl_dim_param, 0,
6596 isl_dim_out, 0, nparam);
6597 set = isl_set_reset_space(set, space);
6598 return set;
6599 error:
6600 isl_mat_free(mat);
6601 isl_set_free(set);
6602 return NULL;
6605 /* Intersect the basic set "bset" with the affine space specified by the
6606 * equalities in "eq".
6608 static struct isl_basic_set *basic_set_append_equalities(
6609 struct isl_basic_set *bset, struct isl_mat *eq)
6611 int i, k;
6612 unsigned len;
6614 if (!bset || !eq)
6615 goto error;
6617 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6618 eq->n_row, 0);
6619 if (!bset)
6620 goto error;
6622 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6623 for (i = 0; i < eq->n_row; ++i) {
6624 k = isl_basic_set_alloc_equality(bset);
6625 if (k < 0)
6626 goto error;
6627 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6628 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6630 isl_mat_free(eq);
6632 bset = isl_basic_set_gauss(bset, NULL);
6633 bset = isl_basic_set_finalize(bset);
6635 return bset;
6636 error:
6637 isl_mat_free(eq);
6638 isl_basic_set_free(bset);
6639 return NULL;
6642 /* Intersect the set "set" with the affine space specified by the
6643 * equalities in "eq".
6645 static struct isl_set *set_append_equalities(struct isl_set *set,
6646 struct isl_mat *eq)
6648 int i;
6650 if (!set || !eq)
6651 goto error;
6653 for (i = 0; i < set->n; ++i) {
6654 set->p[i] = basic_set_append_equalities(set->p[i],
6655 isl_mat_copy(eq));
6656 if (!set->p[i])
6657 goto error;
6659 isl_mat_free(eq);
6660 return set;
6661 error:
6662 isl_mat_free(eq);
6663 isl_set_free(set);
6664 return NULL;
6667 /* Given a basic set "bset" that only involves parameters and existentially
6668 * quantified variables, return the index of the first equality
6669 * that only involves parameters. If there is no such equality then
6670 * return bset->n_eq.
6672 * This function assumes that isl_basic_set_gauss has been called on "bset".
6674 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6676 int i, j;
6677 unsigned nparam, n_div;
6679 if (!bset)
6680 return -1;
6682 nparam = isl_basic_set_dim(bset, isl_dim_param);
6683 n_div = isl_basic_set_dim(bset, isl_dim_div);
6685 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6686 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6687 ++i;
6690 return i;
6693 /* Compute an explicit representation for the existentially quantified
6694 * variables in "bset" by computing the "minimal value" of the set
6695 * variables. Since there are no set variables, the computation of
6696 * the minimal value essentially computes an explicit representation
6697 * of the non-empty part(s) of "bset".
6699 * The input only involves parameters and existentially quantified variables.
6700 * All equalities among parameters have been removed.
6702 * Since the existentially quantified variables in the result are in general
6703 * going to be different from those in the input, we first replace
6704 * them by the minimal number of variables based on their equalities.
6705 * This should simplify the parametric integer programming.
6707 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6709 isl_morph *morph1, *morph2;
6710 isl_set *set;
6711 unsigned n;
6713 if (!bset)
6714 return NULL;
6715 if (bset->n_eq == 0)
6716 return isl_basic_set_lexmin_compute_divs(bset);
6718 morph1 = isl_basic_set_parameter_compression(bset);
6719 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6720 bset = isl_basic_set_lift(bset);
6721 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6722 bset = isl_morph_basic_set(morph2, bset);
6723 n = isl_basic_set_dim(bset, isl_dim_set);
6724 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6726 set = isl_basic_set_lexmin_compute_divs(bset);
6728 set = isl_morph_set(isl_morph_inverse(morph1), set);
6730 return set;
6733 /* Project the given basic set onto its parameter domain, possibly introducing
6734 * new, explicit, existential variables in the constraints.
6735 * The input has parameters and (possibly implicit) existential variables.
6736 * The output has the same parameters, but only
6737 * explicit existentially quantified variables.
6739 * The actual projection is performed by pip, but pip doesn't seem
6740 * to like equalities very much, so we first remove the equalities
6741 * among the parameters by performing a variable compression on
6742 * the parameters. Afterward, an inverse transformation is performed
6743 * and the equalities among the parameters are inserted back in.
6745 * The variable compression on the parameters may uncover additional
6746 * equalities that were only implicit before. We therefore check
6747 * if there are any new parameter equalities in the result and
6748 * if so recurse. The removal of parameter equalities is required
6749 * for the parameter compression performed by base_compute_divs.
6751 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6753 int i;
6754 struct isl_mat *eq;
6755 struct isl_mat *T, *T2;
6756 struct isl_set *set;
6757 unsigned nparam;
6759 bset = isl_basic_set_cow(bset);
6760 if (!bset)
6761 return NULL;
6763 if (bset->n_eq == 0)
6764 return base_compute_divs(bset);
6766 bset = isl_basic_set_gauss(bset, NULL);
6767 if (!bset)
6768 return NULL;
6769 if (isl_basic_set_plain_is_empty(bset))
6770 return isl_set_from_basic_set(bset);
6772 i = first_parameter_equality(bset);
6773 if (i == bset->n_eq)
6774 return base_compute_divs(bset);
6776 nparam = isl_basic_set_dim(bset, isl_dim_param);
6777 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6778 0, 1 + nparam);
6779 eq = isl_mat_cow(eq);
6780 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6781 if (T && T->n_col == 0) {
6782 isl_mat_free(T);
6783 isl_mat_free(T2);
6784 isl_mat_free(eq);
6785 bset = isl_basic_set_set_to_empty(bset);
6786 return isl_set_from_basic_set(bset);
6788 bset = basic_set_parameter_preimage(bset, T);
6790 i = first_parameter_equality(bset);
6791 if (!bset)
6792 set = NULL;
6793 else if (i == bset->n_eq)
6794 set = base_compute_divs(bset);
6795 else
6796 set = parameter_compute_divs(bset);
6797 set = set_parameter_preimage(set, T2);
6798 set = set_append_equalities(set, eq);
6799 return set;
6802 /* Insert the divs from "ls" before those of "bmap".
6804 * The number of columns is not changed, which means that the last
6805 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6806 * The caller is responsible for removing the same number of dimensions
6807 * from the space of "bmap".
6809 static __isl_give isl_basic_map *insert_divs_from_local_space(
6810 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6812 int i;
6813 int n_div;
6814 int old_n_div;
6816 n_div = isl_local_space_dim(ls, isl_dim_div);
6817 if (n_div == 0)
6818 return bmap;
6820 old_n_div = bmap->n_div;
6821 bmap = insert_div_rows(bmap, n_div);
6822 if (!bmap)
6823 return NULL;
6825 for (i = 0; i < n_div; ++i) {
6826 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6827 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6830 return bmap;
6833 /* Replace the space of "bmap" by the space and divs of "ls".
6835 * If "ls" has any divs, then we simplify the result since we may
6836 * have discovered some additional equalities that could simplify
6837 * the div expressions.
6839 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6840 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6842 int n_div;
6844 bmap = isl_basic_map_cow(bmap);
6845 if (!bmap || !ls)
6846 goto error;
6848 n_div = isl_local_space_dim(ls, isl_dim_div);
6849 bmap = insert_divs_from_local_space(bmap, ls);
6850 if (!bmap)
6851 goto error;
6853 isl_space_free(bmap->dim);
6854 bmap->dim = isl_local_space_get_space(ls);
6855 if (!bmap->dim)
6856 goto error;
6858 isl_local_space_free(ls);
6859 if (n_div > 0)
6860 bmap = isl_basic_map_simplify(bmap);
6861 bmap = isl_basic_map_finalize(bmap);
6862 return bmap;
6863 error:
6864 isl_basic_map_free(bmap);
6865 isl_local_space_free(ls);
6866 return NULL;
6869 /* Replace the space of "map" by the space and divs of "ls".
6871 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6872 __isl_take isl_local_space *ls)
6874 int i;
6876 map = isl_map_cow(map);
6877 if (!map || !ls)
6878 goto error;
6880 for (i = 0; i < map->n; ++i) {
6881 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6882 isl_local_space_copy(ls));
6883 if (!map->p[i])
6884 goto error;
6886 isl_space_free(map->dim);
6887 map->dim = isl_local_space_get_space(ls);
6888 if (!map->dim)
6889 goto error;
6891 isl_local_space_free(ls);
6892 return map;
6893 error:
6894 isl_local_space_free(ls);
6895 isl_map_free(map);
6896 return NULL;
6899 /* Compute an explicit representation for the existentially
6900 * quantified variables for which do not know any explicit representation yet.
6902 * We first sort the existentially quantified variables so that the
6903 * existentially quantified variables for which we already have an explicit
6904 * representation are placed before those for which we do not.
6905 * The input dimensions, the output dimensions and the existentially
6906 * quantified variables for which we already have an explicit
6907 * representation are then turned into parameters.
6908 * compute_divs returns a map with the same parameters and
6909 * no input or output dimensions and the dimension specification
6910 * is reset to that of the input, including the existentially quantified
6911 * variables for which we already had an explicit representation.
6913 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6915 struct isl_basic_set *bset;
6916 struct isl_set *set;
6917 struct isl_map *map;
6918 isl_space *dim;
6919 isl_local_space *ls;
6920 unsigned nparam;
6921 unsigned n_in;
6922 unsigned n_out;
6923 int n_known;
6924 int i;
6926 bmap = isl_basic_map_sort_divs(bmap);
6927 bmap = isl_basic_map_cow(bmap);
6928 if (!bmap)
6929 return NULL;
6931 n_known = isl_basic_map_first_unknown_div(bmap);
6932 if (n_known < 0)
6933 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6935 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6936 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6937 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6938 dim = isl_space_set_alloc(bmap->ctx,
6939 nparam + n_in + n_out + n_known, 0);
6940 if (!dim)
6941 goto error;
6943 ls = isl_basic_map_get_local_space(bmap);
6944 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6945 n_known, bmap->n_div - n_known);
6946 if (n_known > 0) {
6947 for (i = n_known; i < bmap->n_div; ++i)
6948 swap_div(bmap, i - n_known, i);
6949 bmap->n_div -= n_known;
6950 bmap->extra -= n_known;
6952 bmap = isl_basic_map_reset_space(bmap, dim);
6953 bset = bset_from_bmap(bmap);
6955 set = parameter_compute_divs(bset);
6956 map = set_to_map(set);
6957 map = replace_space_by_local_space(map, ls);
6959 return map;
6960 error:
6961 isl_basic_map_free(bmap);
6962 return NULL;
6965 /* Remove the explicit representation of local variable "div",
6966 * if there is any.
6968 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6969 __isl_take isl_basic_map *bmap, int div)
6971 isl_bool unknown;
6973 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6974 if (unknown < 0)
6975 return isl_basic_map_free(bmap);
6976 if (unknown)
6977 return bmap;
6979 bmap = isl_basic_map_cow(bmap);
6980 if (!bmap)
6981 return NULL;
6982 isl_int_set_si(bmap->div[div][0], 0);
6983 return bmap;
6986 /* Is local variable "div" of "bmap" marked as not having an explicit
6987 * representation?
6988 * Note that even if "div" is not marked in this way and therefore
6989 * has an explicit representation, this representation may still
6990 * depend (indirectly) on other local variables that do not
6991 * have an explicit representation.
6993 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6994 int div)
6996 if (!bmap)
6997 return isl_bool_error;
6998 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6999 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
7000 "position out of bounds", return isl_bool_error);
7001 return isl_int_is_zero(bmap->div[div][0]);
7004 /* Return the position of the first local variable that does not
7005 * have an explicit representation.
7006 * Return the total number of local variables if they all have
7007 * an explicit representation.
7008 * Return -1 on error.
7010 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7012 int i;
7014 if (!bmap)
7015 return -1;
7017 for (i = 0; i < bmap->n_div; ++i) {
7018 if (!isl_basic_map_div_is_known(bmap, i))
7019 return i;
7021 return bmap->n_div;
7024 /* Return the position of the first local variable that does not
7025 * have an explicit representation.
7026 * Return the total number of local variables if they all have
7027 * an explicit representation.
7028 * Return -1 on error.
7030 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7032 return isl_basic_map_first_unknown_div(bset);
7035 /* Does "bmap" have an explicit representation for all local variables?
7037 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7039 int first, n;
7041 n = isl_basic_map_dim(bmap, isl_dim_div);
7042 first = isl_basic_map_first_unknown_div(bmap);
7043 if (first < 0)
7044 return isl_bool_error;
7045 return first == n;
7048 /* Do all basic maps in "map" have an explicit representation
7049 * for all local variables?
7051 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7053 int i;
7055 if (!map)
7056 return isl_bool_error;
7058 for (i = 0; i < map->n; ++i) {
7059 int known = isl_basic_map_divs_known(map->p[i]);
7060 if (known <= 0)
7061 return known;
7064 return isl_bool_true;
7067 /* If bmap contains any unknown divs, then compute explicit
7068 * expressions for them. However, this computation may be
7069 * quite expensive, so first try to remove divs that aren't
7070 * strictly needed.
7072 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7074 int known;
7075 struct isl_map *map;
7077 known = isl_basic_map_divs_known(bmap);
7078 if (known < 0)
7079 goto error;
7080 if (known)
7081 return isl_map_from_basic_map(bmap);
7083 bmap = isl_basic_map_drop_redundant_divs(bmap);
7085 known = isl_basic_map_divs_known(bmap);
7086 if (known < 0)
7087 goto error;
7088 if (known)
7089 return isl_map_from_basic_map(bmap);
7091 map = compute_divs(bmap);
7092 return map;
7093 error:
7094 isl_basic_map_free(bmap);
7095 return NULL;
7098 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7100 int i;
7101 int known;
7102 struct isl_map *res;
7104 if (!map)
7105 return NULL;
7106 if (map->n == 0)
7107 return map;
7109 known = isl_map_divs_known(map);
7110 if (known < 0) {
7111 isl_map_free(map);
7112 return NULL;
7114 if (known)
7115 return map;
7117 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7118 for (i = 1 ; i < map->n; ++i) {
7119 struct isl_map *r2;
7120 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7121 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7122 res = isl_map_union_disjoint(res, r2);
7123 else
7124 res = isl_map_union(res, r2);
7126 isl_map_free(map);
7128 return res;
7131 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7133 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7136 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7138 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7141 struct isl_set *isl_map_domain(struct isl_map *map)
7143 int i;
7144 struct isl_set *set;
7146 if (!map)
7147 goto error;
7149 map = isl_map_cow(map);
7150 if (!map)
7151 return NULL;
7153 set = set_from_map(map);
7154 set->dim = isl_space_domain(set->dim);
7155 if (!set->dim)
7156 goto error;
7157 for (i = 0; i < map->n; ++i) {
7158 set->p[i] = isl_basic_map_domain(map->p[i]);
7159 if (!set->p[i])
7160 goto error;
7162 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7163 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7164 return set;
7165 error:
7166 isl_map_free(map);
7167 return NULL;
7170 /* Return the union of "map1" and "map2", where we assume for now that
7171 * "map1" and "map2" are disjoint. Note that the basic maps inside
7172 * "map1" or "map2" may not be disjoint from each other.
7173 * Also note that this function is also called from isl_map_union,
7174 * which takes care of handling the situation where "map1" and "map2"
7175 * may not be disjoint.
7177 * If one of the inputs is empty, we can simply return the other input.
7178 * Similarly, if one of the inputs is universal, then it is equal to the union.
7180 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7181 __isl_take isl_map *map2)
7183 int i;
7184 unsigned flags = 0;
7185 struct isl_map *map = NULL;
7186 int is_universe;
7188 if (!map1 || !map2)
7189 goto error;
7191 if (!isl_space_is_equal(map1->dim, map2->dim))
7192 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7193 "spaces don't match", goto error);
7195 if (map1->n == 0) {
7196 isl_map_free(map1);
7197 return map2;
7199 if (map2->n == 0) {
7200 isl_map_free(map2);
7201 return map1;
7204 is_universe = isl_map_plain_is_universe(map1);
7205 if (is_universe < 0)
7206 goto error;
7207 if (is_universe) {
7208 isl_map_free(map2);
7209 return map1;
7212 is_universe = isl_map_plain_is_universe(map2);
7213 if (is_universe < 0)
7214 goto error;
7215 if (is_universe) {
7216 isl_map_free(map1);
7217 return map2;
7220 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7221 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7222 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7224 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7225 map1->n + map2->n, flags);
7226 if (!map)
7227 goto error;
7228 for (i = 0; i < map1->n; ++i) {
7229 map = isl_map_add_basic_map(map,
7230 isl_basic_map_copy(map1->p[i]));
7231 if (!map)
7232 goto error;
7234 for (i = 0; i < map2->n; ++i) {
7235 map = isl_map_add_basic_map(map,
7236 isl_basic_map_copy(map2->p[i]));
7237 if (!map)
7238 goto error;
7240 isl_map_free(map1);
7241 isl_map_free(map2);
7242 return map;
7243 error:
7244 isl_map_free(map);
7245 isl_map_free(map1);
7246 isl_map_free(map2);
7247 return NULL;
7250 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7251 * guaranteed to be disjoint by the caller.
7253 * Note that this functions is called from within isl_map_make_disjoint,
7254 * so we have to be careful not to touch the constraints of the inputs
7255 * in any way.
7257 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7258 __isl_take isl_map *map2)
7260 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7263 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7264 * not be disjoint. The parameters are assumed to have been aligned.
7266 * We currently simply call map_union_disjoint, the internal operation
7267 * of which does not really depend on the inputs being disjoint.
7268 * If the result contains more than one basic map, then we clear
7269 * the disjoint flag since the result may contain basic maps from
7270 * both inputs and these are not guaranteed to be disjoint.
7272 * As a special case, if "map1" and "map2" are obviously equal,
7273 * then we simply return "map1".
7275 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7276 __isl_take isl_map *map2)
7278 int equal;
7280 if (!map1 || !map2)
7281 goto error;
7283 equal = isl_map_plain_is_equal(map1, map2);
7284 if (equal < 0)
7285 goto error;
7286 if (equal) {
7287 isl_map_free(map2);
7288 return map1;
7291 map1 = map_union_disjoint(map1, map2);
7292 if (!map1)
7293 return NULL;
7294 if (map1->n > 1)
7295 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7296 return map1;
7297 error:
7298 isl_map_free(map1);
7299 isl_map_free(map2);
7300 return NULL;
7303 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7304 * not be disjoint.
7306 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7307 __isl_take isl_map *map2)
7309 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7312 struct isl_set *isl_set_union_disjoint(
7313 struct isl_set *set1, struct isl_set *set2)
7315 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7316 set_to_map(set2)));
7319 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7321 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7324 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7325 * the results.
7327 * "map" and "set" are assumed to be compatible and non-NULL.
7329 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7330 __isl_take isl_set *set,
7331 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7332 __isl_take isl_basic_set *bset))
7334 unsigned flags = 0;
7335 struct isl_map *result;
7336 int i, j;
7338 if (isl_set_plain_is_universe(set)) {
7339 isl_set_free(set);
7340 return map;
7343 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7344 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7345 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7347 result = isl_map_alloc_space(isl_space_copy(map->dim),
7348 map->n * set->n, flags);
7349 for (i = 0; result && i < map->n; ++i)
7350 for (j = 0; j < set->n; ++j) {
7351 result = isl_map_add_basic_map(result,
7352 fn(isl_basic_map_copy(map->p[i]),
7353 isl_basic_set_copy(set->p[j])));
7354 if (!result)
7355 break;
7358 isl_map_free(map);
7359 isl_set_free(set);
7360 return result;
7363 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7364 __isl_take isl_set *set)
7366 if (!map || !set)
7367 goto error;
7369 if (!isl_map_compatible_range(map, set))
7370 isl_die(set->ctx, isl_error_invalid,
7371 "incompatible spaces", goto error);
7373 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7374 error:
7375 isl_map_free(map);
7376 isl_set_free(set);
7377 return NULL;
7380 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7381 __isl_take isl_set *set)
7383 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7386 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7387 __isl_take isl_set *set)
7389 if (!map || !set)
7390 goto error;
7392 if (!isl_map_compatible_domain(map, set))
7393 isl_die(set->ctx, isl_error_invalid,
7394 "incompatible spaces", goto error);
7396 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7397 error:
7398 isl_map_free(map);
7399 isl_set_free(set);
7400 return NULL;
7403 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7404 __isl_take isl_set *set)
7406 return isl_map_align_params_map_map_and(map, set,
7407 &map_intersect_domain);
7410 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7411 __isl_take isl_map *map2)
7413 if (!map1 || !map2)
7414 goto error;
7415 map1 = isl_map_reverse(map1);
7416 map1 = isl_map_apply_range(map1, map2);
7417 return isl_map_reverse(map1);
7418 error:
7419 isl_map_free(map1);
7420 isl_map_free(map2);
7421 return NULL;
7424 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7425 __isl_take isl_map *map2)
7427 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7430 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7431 __isl_take isl_map *map2)
7433 isl_space *dim_result;
7434 struct isl_map *result;
7435 int i, j;
7437 if (!map1 || !map2)
7438 goto error;
7440 dim_result = isl_space_join(isl_space_copy(map1->dim),
7441 isl_space_copy(map2->dim));
7443 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7444 if (!result)
7445 goto error;
7446 for (i = 0; i < map1->n; ++i)
7447 for (j = 0; j < map2->n; ++j) {
7448 result = isl_map_add_basic_map(result,
7449 isl_basic_map_apply_range(
7450 isl_basic_map_copy(map1->p[i]),
7451 isl_basic_map_copy(map2->p[j])));
7452 if (!result)
7453 goto error;
7455 isl_map_free(map1);
7456 isl_map_free(map2);
7457 if (result && result->n <= 1)
7458 ISL_F_SET(result, ISL_MAP_DISJOINT);
7459 return result;
7460 error:
7461 isl_map_free(map1);
7462 isl_map_free(map2);
7463 return NULL;
7466 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7467 __isl_take isl_map *map2)
7469 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7473 * returns range - domain
7475 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7477 isl_space *target_space;
7478 struct isl_basic_set *bset;
7479 unsigned dim;
7480 unsigned nparam;
7481 int i;
7483 if (!bmap)
7484 goto error;
7485 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7486 bmap->dim, isl_dim_out),
7487 goto error);
7488 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7489 dim = isl_basic_map_n_in(bmap);
7490 nparam = isl_basic_map_n_param(bmap);
7491 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7492 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7493 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7494 for (i = 0; i < dim; ++i) {
7495 int j = isl_basic_map_alloc_equality(bmap);
7496 if (j < 0) {
7497 bmap = isl_basic_map_free(bmap);
7498 break;
7500 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7501 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7502 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7503 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7505 bset = isl_basic_map_domain(bmap);
7506 bset = isl_basic_set_reset_space(bset, target_space);
7507 return bset;
7508 error:
7509 isl_basic_map_free(bmap);
7510 return NULL;
7514 * returns range - domain
7516 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7518 int i;
7519 isl_space *dim;
7520 struct isl_set *result;
7522 if (!map)
7523 return NULL;
7525 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7526 map->dim, isl_dim_out),
7527 goto error);
7528 dim = isl_map_get_space(map);
7529 dim = isl_space_domain(dim);
7530 result = isl_set_alloc_space(dim, map->n, 0);
7531 if (!result)
7532 goto error;
7533 for (i = 0; i < map->n; ++i)
7534 result = isl_set_add_basic_set(result,
7535 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7536 isl_map_free(map);
7537 return result;
7538 error:
7539 isl_map_free(map);
7540 return NULL;
7544 * returns [domain -> range] -> range - domain
7546 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7547 __isl_take isl_basic_map *bmap)
7549 int i, k;
7550 isl_space *dim;
7551 isl_basic_map *domain;
7552 int nparam, n;
7553 unsigned total;
7555 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7556 bmap->dim, isl_dim_out))
7557 isl_die(bmap->ctx, isl_error_invalid,
7558 "domain and range don't match", goto error);
7560 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7561 n = isl_basic_map_dim(bmap, isl_dim_in);
7563 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7564 domain = isl_basic_map_universe(dim);
7566 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7567 bmap = isl_basic_map_apply_range(bmap, domain);
7568 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7570 total = isl_basic_map_total_dim(bmap);
7572 for (i = 0; i < n; ++i) {
7573 k = isl_basic_map_alloc_equality(bmap);
7574 if (k < 0)
7575 goto error;
7576 isl_seq_clr(bmap->eq[k], 1 + total);
7577 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7578 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7579 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7582 bmap = isl_basic_map_gauss(bmap, NULL);
7583 return isl_basic_map_finalize(bmap);
7584 error:
7585 isl_basic_map_free(bmap);
7586 return NULL;
7590 * returns [domain -> range] -> range - domain
7592 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7594 int i;
7595 isl_space *domain_dim;
7597 if (!map)
7598 return NULL;
7600 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7601 map->dim, isl_dim_out))
7602 isl_die(map->ctx, isl_error_invalid,
7603 "domain and range don't match", goto error);
7605 map = isl_map_cow(map);
7606 if (!map)
7607 return NULL;
7609 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7610 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7611 map->dim = isl_space_join(map->dim, domain_dim);
7612 if (!map->dim)
7613 goto error;
7614 for (i = 0; i < map->n; ++i) {
7615 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7616 if (!map->p[i])
7617 goto error;
7619 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7620 return map;
7621 error:
7622 isl_map_free(map);
7623 return NULL;
7626 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7628 struct isl_basic_map *bmap;
7629 unsigned nparam;
7630 unsigned dim;
7631 int i;
7633 if (!dims)
7634 return NULL;
7636 nparam = dims->nparam;
7637 dim = dims->n_out;
7638 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7639 if (!bmap)
7640 goto error;
7642 for (i = 0; i < dim; ++i) {
7643 int j = isl_basic_map_alloc_equality(bmap);
7644 if (j < 0)
7645 goto error;
7646 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7647 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7648 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7650 return isl_basic_map_finalize(bmap);
7651 error:
7652 isl_basic_map_free(bmap);
7653 return NULL;
7656 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7658 if (!dim)
7659 return NULL;
7660 if (dim->n_in != dim->n_out)
7661 isl_die(dim->ctx, isl_error_invalid,
7662 "number of input and output dimensions needs to be "
7663 "the same", goto error);
7664 return basic_map_identity(dim);
7665 error:
7666 isl_space_free(dim);
7667 return NULL;
7670 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7672 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7675 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7677 isl_space *dim = isl_set_get_space(set);
7678 isl_map *id;
7679 id = isl_map_identity(isl_space_map_from_set(dim));
7680 return isl_map_intersect_range(id, set);
7683 /* Construct a basic set with all set dimensions having only non-negative
7684 * values.
7686 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7687 __isl_take isl_space *space)
7689 int i;
7690 unsigned nparam;
7691 unsigned dim;
7692 struct isl_basic_set *bset;
7694 if (!space)
7695 return NULL;
7696 nparam = space->nparam;
7697 dim = space->n_out;
7698 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7699 if (!bset)
7700 return NULL;
7701 for (i = 0; i < dim; ++i) {
7702 int k = isl_basic_set_alloc_inequality(bset);
7703 if (k < 0)
7704 goto error;
7705 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7706 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7708 return bset;
7709 error:
7710 isl_basic_set_free(bset);
7711 return NULL;
7714 /* Construct the half-space x_pos >= 0.
7716 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7717 int pos)
7719 int k;
7720 isl_basic_set *nonneg;
7722 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7723 k = isl_basic_set_alloc_inequality(nonneg);
7724 if (k < 0)
7725 goto error;
7726 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7727 isl_int_set_si(nonneg->ineq[k][pos], 1);
7729 return isl_basic_set_finalize(nonneg);
7730 error:
7731 isl_basic_set_free(nonneg);
7732 return NULL;
7735 /* Construct the half-space x_pos <= -1.
7737 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7739 int k;
7740 isl_basic_set *neg;
7742 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7743 k = isl_basic_set_alloc_inequality(neg);
7744 if (k < 0)
7745 goto error;
7746 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7747 isl_int_set_si(neg->ineq[k][0], -1);
7748 isl_int_set_si(neg->ineq[k][pos], -1);
7750 return isl_basic_set_finalize(neg);
7751 error:
7752 isl_basic_set_free(neg);
7753 return NULL;
7756 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7757 enum isl_dim_type type, unsigned first, unsigned n)
7759 int i;
7760 unsigned offset;
7761 isl_basic_set *nonneg;
7762 isl_basic_set *neg;
7764 if (!set)
7765 return NULL;
7766 if (n == 0)
7767 return set;
7769 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7771 offset = pos(set->dim, type);
7772 for (i = 0; i < n; ++i) {
7773 nonneg = nonneg_halfspace(isl_set_get_space(set),
7774 offset + first + i);
7775 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7777 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7780 return set;
7781 error:
7782 isl_set_free(set);
7783 return NULL;
7786 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7787 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7788 void *user)
7790 isl_set *half;
7792 if (!set)
7793 return -1;
7794 if (isl_set_plain_is_empty(set)) {
7795 isl_set_free(set);
7796 return 0;
7798 if (first == len)
7799 return fn(set, signs, user);
7801 signs[first] = 1;
7802 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7803 1 + first));
7804 half = isl_set_intersect(half, isl_set_copy(set));
7805 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7806 goto error;
7808 signs[first] = -1;
7809 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7810 1 + first));
7811 half = isl_set_intersect(half, set);
7812 return foreach_orthant(half, signs, first + 1, len, fn, user);
7813 error:
7814 isl_set_free(set);
7815 return -1;
7818 /* Call "fn" on the intersections of "set" with each of the orthants
7819 * (except for obviously empty intersections). The orthant is identified
7820 * by the signs array, with each entry having value 1 or -1 according
7821 * to the sign of the corresponding variable.
7823 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7824 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7825 void *user)
7827 unsigned nparam;
7828 unsigned nvar;
7829 int *signs;
7830 int r;
7832 if (!set)
7833 return -1;
7834 if (isl_set_plain_is_empty(set))
7835 return 0;
7837 nparam = isl_set_dim(set, isl_dim_param);
7838 nvar = isl_set_dim(set, isl_dim_set);
7840 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7842 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7843 fn, user);
7845 free(signs);
7847 return r;
7850 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7852 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7855 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7856 __isl_keep isl_basic_map *bmap2)
7858 int is_subset;
7859 struct isl_map *map1;
7860 struct isl_map *map2;
7862 if (!bmap1 || !bmap2)
7863 return isl_bool_error;
7865 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7866 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7868 is_subset = isl_map_is_subset(map1, map2);
7870 isl_map_free(map1);
7871 isl_map_free(map2);
7873 return is_subset;
7876 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7877 __isl_keep isl_basic_set *bset2)
7879 return isl_basic_map_is_subset(bset1, bset2);
7882 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7883 __isl_keep isl_basic_map *bmap2)
7885 isl_bool is_subset;
7887 if (!bmap1 || !bmap2)
7888 return isl_bool_error;
7889 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7890 if (is_subset != isl_bool_true)
7891 return is_subset;
7892 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7893 return is_subset;
7896 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7897 __isl_keep isl_basic_set *bset2)
7899 return isl_basic_map_is_equal(
7900 bset_to_bmap(bset1), bset_to_bmap(bset2));
7903 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7905 int i;
7906 int is_empty;
7908 if (!map)
7909 return isl_bool_error;
7910 for (i = 0; i < map->n; ++i) {
7911 is_empty = isl_basic_map_is_empty(map->p[i]);
7912 if (is_empty < 0)
7913 return isl_bool_error;
7914 if (!is_empty)
7915 return isl_bool_false;
7917 return isl_bool_true;
7920 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7922 return map ? map->n == 0 : isl_bool_error;
7925 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7927 return set ? set->n == 0 : isl_bool_error;
7930 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7932 return isl_map_is_empty(set_to_map(set));
7935 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7937 if (!map1 || !map2)
7938 return -1;
7940 return isl_space_is_equal(map1->dim, map2->dim);
7943 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7945 if (!set1 || !set2)
7946 return -1;
7948 return isl_space_is_equal(set1->dim, set2->dim);
7951 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7953 isl_bool is_subset;
7955 if (!map1 || !map2)
7956 return isl_bool_error;
7957 is_subset = isl_map_is_subset(map1, map2);
7958 if (is_subset != isl_bool_true)
7959 return is_subset;
7960 is_subset = isl_map_is_subset(map2, map1);
7961 return is_subset;
7964 /* Is "map1" equal to "map2"?
7966 * First check if they are obviously equal.
7967 * If not, then perform a more detailed analysis.
7969 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7971 isl_bool equal;
7973 equal = isl_map_plain_is_equal(map1, map2);
7974 if (equal < 0 || equal)
7975 return equal;
7976 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7979 isl_bool isl_basic_map_is_strict_subset(
7980 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7982 isl_bool is_subset;
7984 if (!bmap1 || !bmap2)
7985 return isl_bool_error;
7986 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7987 if (is_subset != isl_bool_true)
7988 return is_subset;
7989 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7990 if (is_subset == isl_bool_error)
7991 return is_subset;
7992 return !is_subset;
7995 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7996 __isl_keep isl_map *map2)
7998 isl_bool is_subset;
8000 if (!map1 || !map2)
8001 return isl_bool_error;
8002 is_subset = isl_map_is_subset(map1, map2);
8003 if (is_subset != isl_bool_true)
8004 return is_subset;
8005 is_subset = isl_map_is_subset(map2, map1);
8006 if (is_subset == isl_bool_error)
8007 return is_subset;
8008 return !is_subset;
8011 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8012 __isl_keep isl_set *set2)
8014 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8017 /* Is "bmap" obviously equal to the universe with the same space?
8019 * That is, does it not have any constraints?
8021 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8023 if (!bmap)
8024 return isl_bool_error;
8025 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8028 /* Is "bset" obviously equal to the universe with the same space?
8030 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8032 return isl_basic_map_plain_is_universe(bset);
8035 /* If "c" does not involve any existentially quantified variables,
8036 * then set *univ to false and abort
8038 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8040 isl_bool *univ = user;
8041 unsigned n;
8043 n = isl_constraint_dim(c, isl_dim_div);
8044 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8045 isl_constraint_free(c);
8046 if (*univ < 0 || !*univ)
8047 return isl_stat_error;
8048 return isl_stat_ok;
8051 /* Is "bmap" equal to the universe with the same space?
8053 * First check if it is obviously equal to the universe.
8054 * If not and if there are any constraints not involving
8055 * existentially quantified variables, then it is certainly
8056 * not equal to the universe.
8057 * Otherwise, check if the universe is a subset of "bmap".
8059 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8061 isl_bool univ;
8062 isl_basic_map *test;
8064 univ = isl_basic_map_plain_is_universe(bmap);
8065 if (univ < 0 || univ)
8066 return univ;
8067 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8068 return isl_bool_false;
8069 univ = isl_bool_true;
8070 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8071 univ)
8072 return isl_bool_error;
8073 if (univ < 0 || !univ)
8074 return univ;
8075 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8076 univ = isl_basic_map_is_subset(test, bmap);
8077 isl_basic_map_free(test);
8078 return univ;
8081 /* Is "bset" equal to the universe with the same space?
8083 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8085 return isl_basic_map_is_universe(bset);
8088 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8090 int i;
8092 if (!map)
8093 return isl_bool_error;
8095 for (i = 0; i < map->n; ++i) {
8096 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8097 if (r < 0 || r)
8098 return r;
8101 return isl_bool_false;
8104 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8106 return isl_map_plain_is_universe(set_to_map(set));
8109 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8111 struct isl_basic_set *bset = NULL;
8112 struct isl_vec *sample = NULL;
8113 isl_bool empty, non_empty;
8115 if (!bmap)
8116 return isl_bool_error;
8118 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8119 return isl_bool_true;
8121 if (isl_basic_map_plain_is_universe(bmap))
8122 return isl_bool_false;
8124 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8125 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8126 copy = isl_basic_map_remove_redundancies(copy);
8127 empty = isl_basic_map_plain_is_empty(copy);
8128 isl_basic_map_free(copy);
8129 return empty;
8132 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8133 if (non_empty < 0)
8134 return isl_bool_error;
8135 if (non_empty)
8136 return isl_bool_false;
8137 isl_vec_free(bmap->sample);
8138 bmap->sample = NULL;
8139 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8140 if (!bset)
8141 return isl_bool_error;
8142 sample = isl_basic_set_sample_vec(bset);
8143 if (!sample)
8144 return isl_bool_error;
8145 empty = sample->size == 0;
8146 isl_vec_free(bmap->sample);
8147 bmap->sample = sample;
8148 if (empty)
8149 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8151 return empty;
8154 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8156 if (!bmap)
8157 return isl_bool_error;
8158 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8161 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8163 if (!bset)
8164 return isl_bool_error;
8165 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8168 /* Is "bmap" known to be non-empty?
8170 * That is, is the cached sample still valid?
8172 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8174 unsigned total;
8176 if (!bmap)
8177 return isl_bool_error;
8178 if (!bmap->sample)
8179 return isl_bool_false;
8180 total = 1 + isl_basic_map_total_dim(bmap);
8181 if (bmap->sample->size != total)
8182 return isl_bool_false;
8183 return isl_basic_map_contains(bmap, bmap->sample);
8186 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8188 return isl_basic_map_is_empty(bset_to_bmap(bset));
8191 struct isl_map *isl_basic_map_union(
8192 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8194 struct isl_map *map;
8195 if (!bmap1 || !bmap2)
8196 goto error;
8198 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8200 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8201 if (!map)
8202 goto error;
8203 map = isl_map_add_basic_map(map, bmap1);
8204 map = isl_map_add_basic_map(map, bmap2);
8205 return map;
8206 error:
8207 isl_basic_map_free(bmap1);
8208 isl_basic_map_free(bmap2);
8209 return NULL;
8212 struct isl_set *isl_basic_set_union(
8213 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8215 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8216 bset_to_bmap(bset2)));
8219 /* Order divs such that any div only depends on previous divs */
8220 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8222 int i;
8223 unsigned off;
8225 if (!bmap)
8226 return NULL;
8228 off = isl_space_dim(bmap->dim, isl_dim_all);
8230 for (i = 0; i < bmap->n_div; ++i) {
8231 int pos;
8232 if (isl_int_is_zero(bmap->div[i][0]))
8233 continue;
8234 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8235 bmap->n_div-i);
8236 if (pos == -1)
8237 continue;
8238 if (pos == 0)
8239 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8240 "integer division depends on itself",
8241 return isl_basic_map_free(bmap));
8242 isl_basic_map_swap_div(bmap, i, i + pos);
8243 --i;
8245 return bmap;
8248 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8250 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8253 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8255 int i;
8257 if (!map)
8258 return 0;
8260 for (i = 0; i < map->n; ++i) {
8261 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8262 if (!map->p[i])
8263 goto error;
8266 return map;
8267 error:
8268 isl_map_free(map);
8269 return NULL;
8272 /* Sort the local variables of "bset".
8274 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8275 __isl_take isl_basic_set *bset)
8277 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8280 /* Apply the expansion computed by isl_merge_divs.
8281 * The expansion itself is given by "exp" while the resulting
8282 * list of divs is given by "div".
8284 * Move the integer divisions of "bmap" into the right position
8285 * according to "exp" and then introduce the additional integer
8286 * divisions, adding div constraints.
8287 * The moving should be done first to avoid moving coefficients
8288 * in the definitions of the extra integer divisions.
8290 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8291 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8293 int i, j;
8294 int n_div;
8296 bmap = isl_basic_map_cow(bmap);
8297 if (!bmap || !div)
8298 goto error;
8300 if (div->n_row < bmap->n_div)
8301 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8302 "not an expansion", goto error);
8304 n_div = bmap->n_div;
8305 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8306 div->n_row - n_div, 0,
8307 2 * (div->n_row - n_div));
8309 for (i = n_div; i < div->n_row; ++i)
8310 if (isl_basic_map_alloc_div(bmap) < 0)
8311 goto error;
8313 for (j = n_div - 1; j >= 0; --j) {
8314 if (exp[j] == j)
8315 break;
8316 isl_basic_map_swap_div(bmap, j, exp[j]);
8318 j = 0;
8319 for (i = 0; i < div->n_row; ++i) {
8320 if (j < n_div && exp[j] == i) {
8321 j++;
8322 } else {
8323 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8324 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8325 continue;
8326 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8327 goto error;
8331 isl_mat_free(div);
8332 return bmap;
8333 error:
8334 isl_basic_map_free(bmap);
8335 isl_mat_free(div);
8336 return NULL;
8339 /* Apply the expansion computed by isl_merge_divs.
8340 * The expansion itself is given by "exp" while the resulting
8341 * list of divs is given by "div".
8343 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8344 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8346 return isl_basic_map_expand_divs(bset, div, exp);
8349 /* Look for a div in dst that corresponds to the div "div" in src.
8350 * The divs before "div" in src and dst are assumed to be the same.
8352 * Returns -1 if no corresponding div was found and the position
8353 * of the corresponding div in dst otherwise.
8355 static int find_div(struct isl_basic_map *dst,
8356 struct isl_basic_map *src, unsigned div)
8358 int i;
8360 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8362 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8363 for (i = div; i < dst->n_div; ++i)
8364 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8365 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8366 dst->n_div - div) == -1)
8367 return i;
8368 return -1;
8371 /* Align the divs of "dst" to those of "src", adding divs from "src"
8372 * if needed. That is, make sure that the first src->n_div divs
8373 * of the result are equal to those of src.
8375 * The result is not finalized as by design it will have redundant
8376 * divs if any divs from "src" were copied.
8378 __isl_give isl_basic_map *isl_basic_map_align_divs(
8379 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8381 int i;
8382 int known, extended;
8383 unsigned total;
8385 if (!dst || !src)
8386 return isl_basic_map_free(dst);
8388 if (src->n_div == 0)
8389 return dst;
8391 known = isl_basic_map_divs_known(src);
8392 if (known < 0)
8393 return isl_basic_map_free(dst);
8394 if (!known)
8395 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8396 "some src divs are unknown",
8397 return isl_basic_map_free(dst));
8399 src = isl_basic_map_order_divs(src);
8401 extended = 0;
8402 total = isl_space_dim(src->dim, isl_dim_all);
8403 for (i = 0; i < src->n_div; ++i) {
8404 int j = find_div(dst, src, i);
8405 if (j < 0) {
8406 if (!extended) {
8407 int extra = src->n_div - i;
8408 dst = isl_basic_map_cow(dst);
8409 if (!dst)
8410 return NULL;
8411 dst = isl_basic_map_extend_space(dst,
8412 isl_space_copy(dst->dim),
8413 extra, 0, 2 * extra);
8414 extended = 1;
8416 j = isl_basic_map_alloc_div(dst);
8417 if (j < 0)
8418 return isl_basic_map_free(dst);
8419 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8420 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8421 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8422 return isl_basic_map_free(dst);
8424 if (j != i)
8425 isl_basic_map_swap_div(dst, i, j);
8427 return dst;
8430 struct isl_basic_set *isl_basic_set_align_divs(
8431 struct isl_basic_set *dst, struct isl_basic_set *src)
8433 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8434 bset_to_bmap(src)));
8437 struct isl_map *isl_map_align_divs(struct isl_map *map)
8439 int i;
8441 if (!map)
8442 return NULL;
8443 if (map->n == 0)
8444 return map;
8445 map = isl_map_compute_divs(map);
8446 map = isl_map_cow(map);
8447 if (!map)
8448 return NULL;
8450 for (i = 1; i < map->n; ++i)
8451 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8452 for (i = 1; i < map->n; ++i) {
8453 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8454 if (!map->p[i])
8455 return isl_map_free(map);
8458 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8459 return map;
8462 struct isl_set *isl_set_align_divs(struct isl_set *set)
8464 return set_from_map(isl_map_align_divs(set_to_map(set)));
8467 /* Align the divs of the basic maps in "map" to those
8468 * of the basic maps in "list", as well as to the other basic maps in "map".
8469 * The elements in "list" are assumed to have known divs.
8471 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8472 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8474 int i, n;
8476 map = isl_map_compute_divs(map);
8477 map = isl_map_cow(map);
8478 if (!map || !list)
8479 return isl_map_free(map);
8480 if (map->n == 0)
8481 return map;
8483 n = isl_basic_map_list_n_basic_map(list);
8484 for (i = 0; i < n; ++i) {
8485 isl_basic_map *bmap;
8487 bmap = isl_basic_map_list_get_basic_map(list, i);
8488 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8489 isl_basic_map_free(bmap);
8491 if (!map->p[0])
8492 return isl_map_free(map);
8494 return isl_map_align_divs(map);
8497 /* Align the divs of each element of "list" to those of "bmap".
8498 * Both "bmap" and the elements of "list" are assumed to have known divs.
8500 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8501 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8503 int i, n;
8505 if (!list || !bmap)
8506 return isl_basic_map_list_free(list);
8508 n = isl_basic_map_list_n_basic_map(list);
8509 for (i = 0; i < n; ++i) {
8510 isl_basic_map *bmap_i;
8512 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8513 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8514 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8517 return list;
8520 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8521 __isl_take isl_map *map)
8523 if (!set || !map)
8524 goto error;
8525 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8526 map = isl_map_intersect_domain(map, set);
8527 set = isl_map_range(map);
8528 return set;
8529 error:
8530 isl_set_free(set);
8531 isl_map_free(map);
8532 return NULL;
8535 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8536 __isl_take isl_map *map)
8538 return isl_map_align_params_map_map_and(set, map, &set_apply);
8541 /* There is no need to cow as removing empty parts doesn't change
8542 * the meaning of the set.
8544 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8546 int i;
8548 if (!map)
8549 return NULL;
8551 for (i = map->n - 1; i >= 0; --i)
8552 remove_if_empty(map, i);
8554 return map;
8557 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8559 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8562 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8564 struct isl_basic_map *bmap;
8565 if (!map || map->n == 0)
8566 return NULL;
8567 bmap = map->p[map->n-1];
8568 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8569 return isl_basic_map_copy(bmap);
8572 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8574 return map_copy_basic_map(map);
8577 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8579 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8582 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8583 __isl_keep isl_basic_map *bmap)
8585 int i;
8587 if (!map || !bmap)
8588 goto error;
8589 for (i = map->n-1; i >= 0; --i) {
8590 if (map->p[i] != bmap)
8591 continue;
8592 map = isl_map_cow(map);
8593 if (!map)
8594 goto error;
8595 isl_basic_map_free(map->p[i]);
8596 if (i != map->n-1) {
8597 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8598 map->p[i] = map->p[map->n-1];
8600 map->n--;
8601 return map;
8603 return map;
8604 error:
8605 isl_map_free(map);
8606 return NULL;
8609 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8610 __isl_keep isl_basic_map *bmap)
8612 return map_drop_basic_map(map, bmap);
8615 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8616 struct isl_basic_set *bset)
8618 return set_from_map(map_drop_basic_map(set_to_map(set),
8619 bset_to_bmap(bset)));
8622 /* Given two basic sets bset1 and bset2, compute the maximal difference
8623 * between the values of dimension pos in bset1 and those in bset2
8624 * for any common value of the parameters and dimensions preceding pos.
8626 static enum isl_lp_result basic_set_maximal_difference_at(
8627 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8628 int pos, isl_int *opt)
8630 isl_basic_map *bmap1;
8631 isl_basic_map *bmap2;
8632 struct isl_ctx *ctx;
8633 struct isl_vec *obj;
8634 unsigned total;
8635 unsigned nparam;
8636 unsigned dim1;
8637 enum isl_lp_result res;
8639 if (!bset1 || !bset2)
8640 return isl_lp_error;
8642 nparam = isl_basic_set_n_param(bset1);
8643 dim1 = isl_basic_set_n_dim(bset1);
8645 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8646 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8647 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8648 isl_dim_out, 0, pos);
8649 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8650 isl_dim_out, 0, pos);
8651 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8652 if (!bmap1)
8653 return isl_lp_error;
8655 total = isl_basic_map_total_dim(bmap1);
8656 ctx = bmap1->ctx;
8657 obj = isl_vec_alloc(ctx, 1 + total);
8658 if (!obj)
8659 goto error;
8660 isl_seq_clr(obj->block.data, 1 + total);
8661 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8662 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8663 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8664 opt, NULL, NULL);
8665 isl_basic_map_free(bmap1);
8666 isl_vec_free(obj);
8667 return res;
8668 error:
8669 isl_basic_map_free(bmap1);
8670 return isl_lp_error;
8673 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8674 * for any common value of the parameters and dimensions preceding pos
8675 * in both basic sets, the values of dimension pos in bset1 are
8676 * smaller or larger than those in bset2.
8678 * Returns
8679 * 1 if bset1 follows bset2
8680 * -1 if bset1 precedes bset2
8681 * 0 if bset1 and bset2 are incomparable
8682 * -2 if some error occurred.
8684 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8685 struct isl_basic_set *bset2, int pos)
8687 isl_int opt;
8688 enum isl_lp_result res;
8689 int cmp;
8691 isl_int_init(opt);
8693 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8695 if (res == isl_lp_empty)
8696 cmp = 0;
8697 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8698 res == isl_lp_unbounded)
8699 cmp = 1;
8700 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8701 cmp = -1;
8702 else
8703 cmp = -2;
8705 isl_int_clear(opt);
8706 return cmp;
8709 /* Given two basic sets bset1 and bset2, check whether
8710 * for any common value of the parameters and dimensions preceding pos
8711 * there is a value of dimension pos in bset1 that is larger
8712 * than a value of the same dimension in bset2.
8714 * Return
8715 * 1 if there exists such a pair
8716 * 0 if there is no such pair, but there is a pair of equal values
8717 * -1 otherwise
8718 * -2 if some error occurred.
8720 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8721 __isl_keep isl_basic_set *bset2, int pos)
8723 isl_int opt;
8724 enum isl_lp_result res;
8725 int cmp;
8727 isl_int_init(opt);
8729 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8731 if (res == isl_lp_empty)
8732 cmp = -1;
8733 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8734 res == isl_lp_unbounded)
8735 cmp = 1;
8736 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8737 cmp = -1;
8738 else if (res == isl_lp_ok)
8739 cmp = 0;
8740 else
8741 cmp = -2;
8743 isl_int_clear(opt);
8744 return cmp;
8747 /* Given two sets set1 and set2, check whether
8748 * for any common value of the parameters and dimensions preceding pos
8749 * there is a value of dimension pos in set1 that is larger
8750 * than a value of the same dimension in set2.
8752 * Return
8753 * 1 if there exists such a pair
8754 * 0 if there is no such pair, but there is a pair of equal values
8755 * -1 otherwise
8756 * -2 if some error occurred.
8758 int isl_set_follows_at(__isl_keep isl_set *set1,
8759 __isl_keep isl_set *set2, int pos)
8761 int i, j;
8762 int follows = -1;
8764 if (!set1 || !set2)
8765 return -2;
8767 for (i = 0; i < set1->n; ++i)
8768 for (j = 0; j < set2->n; ++j) {
8769 int f;
8770 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8771 if (f == 1 || f == -2)
8772 return f;
8773 if (f > follows)
8774 follows = f;
8777 return follows;
8780 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8781 unsigned pos, isl_int *val)
8783 int i;
8784 int d;
8785 unsigned total;
8787 if (!bmap)
8788 return -1;
8789 total = isl_basic_map_total_dim(bmap);
8790 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8791 for (; d+1 > pos; --d)
8792 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8793 break;
8794 if (d != pos)
8795 continue;
8796 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8797 return 0;
8798 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8799 return 0;
8800 if (!isl_int_is_one(bmap->eq[i][1+d]))
8801 return 0;
8802 if (val)
8803 isl_int_neg(*val, bmap->eq[i][0]);
8804 return 1;
8806 return 0;
8809 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8810 unsigned pos, isl_int *val)
8812 int i;
8813 isl_int v;
8814 isl_int tmp;
8815 int fixed;
8817 if (!map)
8818 return -1;
8819 if (map->n == 0)
8820 return 0;
8821 if (map->n == 1)
8822 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8823 isl_int_init(v);
8824 isl_int_init(tmp);
8825 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8826 for (i = 1; fixed == 1 && i < map->n; ++i) {
8827 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8828 if (fixed == 1 && isl_int_ne(tmp, v))
8829 fixed = 0;
8831 if (val)
8832 isl_int_set(*val, v);
8833 isl_int_clear(tmp);
8834 isl_int_clear(v);
8835 return fixed;
8838 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8839 unsigned pos, isl_int *val)
8841 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8842 pos, val);
8845 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8846 isl_int *val)
8848 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
8851 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8852 enum isl_dim_type type, unsigned pos, isl_int *val)
8854 if (pos >= isl_basic_map_dim(bmap, type))
8855 return -1;
8856 return isl_basic_map_plain_has_fixed_var(bmap,
8857 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8860 /* If "bmap" obviously lies on a hyperplane where the given dimension
8861 * has a fixed value, then return that value.
8862 * Otherwise return NaN.
8864 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8865 __isl_keep isl_basic_map *bmap,
8866 enum isl_dim_type type, unsigned pos)
8868 isl_ctx *ctx;
8869 isl_val *v;
8870 int fixed;
8872 if (!bmap)
8873 return NULL;
8874 ctx = isl_basic_map_get_ctx(bmap);
8875 v = isl_val_alloc(ctx);
8876 if (!v)
8877 return NULL;
8878 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8879 if (fixed < 0)
8880 return isl_val_free(v);
8881 if (fixed) {
8882 isl_int_set_si(v->d, 1);
8883 return v;
8885 isl_val_free(v);
8886 return isl_val_nan(ctx);
8889 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8890 enum isl_dim_type type, unsigned pos, isl_int *val)
8892 if (pos >= isl_map_dim(map, type))
8893 return -1;
8894 return isl_map_plain_has_fixed_var(map,
8895 map_offset(map, type) - 1 + pos, val);
8898 /* If "map" obviously lies on a hyperplane where the given dimension
8899 * has a fixed value, then return that value.
8900 * Otherwise return NaN.
8902 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8903 enum isl_dim_type type, unsigned pos)
8905 isl_ctx *ctx;
8906 isl_val *v;
8907 int fixed;
8909 if (!map)
8910 return NULL;
8911 ctx = isl_map_get_ctx(map);
8912 v = isl_val_alloc(ctx);
8913 if (!v)
8914 return NULL;
8915 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8916 if (fixed < 0)
8917 return isl_val_free(v);
8918 if (fixed) {
8919 isl_int_set_si(v->d, 1);
8920 return v;
8922 isl_val_free(v);
8923 return isl_val_nan(ctx);
8926 /* If "set" obviously lies on a hyperplane where the given dimension
8927 * has a fixed value, then return that value.
8928 * Otherwise return NaN.
8930 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8931 enum isl_dim_type type, unsigned pos)
8933 return isl_map_plain_get_val_if_fixed(set, type, pos);
8936 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8937 enum isl_dim_type type, unsigned pos, isl_int *val)
8939 return isl_map_plain_is_fixed(set, type, pos, val);
8942 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8943 * then return this fixed value in *val.
8945 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8946 unsigned dim, isl_int *val)
8948 return isl_basic_set_plain_has_fixed_var(bset,
8949 isl_basic_set_n_param(bset) + dim, val);
8952 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8953 * then return this fixed value in *val.
8955 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8956 unsigned dim, isl_int *val)
8958 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8961 /* Check if input variable in has fixed value and if so and if val is not NULL,
8962 * then return this fixed value in *val.
8964 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8965 unsigned in, isl_int *val)
8967 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8970 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8971 * and if val is not NULL, then return this lower bound in *val.
8973 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8974 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8976 int i, i_eq = -1, i_ineq = -1;
8977 isl_int *c;
8978 unsigned total;
8979 unsigned nparam;
8981 if (!bset)
8982 return -1;
8983 total = isl_basic_set_total_dim(bset);
8984 nparam = isl_basic_set_n_param(bset);
8985 for (i = 0; i < bset->n_eq; ++i) {
8986 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8987 continue;
8988 if (i_eq != -1)
8989 return 0;
8990 i_eq = i;
8992 for (i = 0; i < bset->n_ineq; ++i) {
8993 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8994 continue;
8995 if (i_eq != -1 || i_ineq != -1)
8996 return 0;
8997 i_ineq = i;
8999 if (i_eq == -1 && i_ineq == -1)
9000 return 0;
9001 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
9002 /* The coefficient should always be one due to normalization. */
9003 if (!isl_int_is_one(c[1+nparam+dim]))
9004 return 0;
9005 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
9006 return 0;
9007 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
9008 total - nparam - dim - 1) != -1)
9009 return 0;
9010 if (val)
9011 isl_int_neg(*val, c[0]);
9012 return 1;
9015 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9016 unsigned dim, isl_int *val)
9018 int i;
9019 isl_int v;
9020 isl_int tmp;
9021 int fixed;
9023 if (!set)
9024 return -1;
9025 if (set->n == 0)
9026 return 0;
9027 if (set->n == 1)
9028 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9029 dim, val);
9030 isl_int_init(v);
9031 isl_int_init(tmp);
9032 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9033 dim, &v);
9034 for (i = 1; fixed == 1 && i < set->n; ++i) {
9035 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9036 dim, &tmp);
9037 if (fixed == 1 && isl_int_ne(tmp, v))
9038 fixed = 0;
9040 if (val)
9041 isl_int_set(*val, v);
9042 isl_int_clear(tmp);
9043 isl_int_clear(v);
9044 return fixed;
9047 /* Return -1 if the constraint "c1" should be sorted before "c2"
9048 * and 1 if it should be sorted after "c2".
9049 * Return 0 if the two constraints are the same (up to the constant term).
9051 * In particular, if a constraint involves later variables than another
9052 * then it is sorted after this other constraint.
9053 * uset_gist depends on constraints without existentially quantified
9054 * variables sorting first.
9056 * For constraints that have the same latest variable, those
9057 * with the same coefficient for this latest variable (first in absolute value
9058 * and then in actual value) are grouped together.
9059 * This is useful for detecting pairs of constraints that can
9060 * be chained in their printed representation.
9062 * Finally, within a group, constraints are sorted according to
9063 * their coefficients (excluding the constant term).
9065 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9067 isl_int **c1 = (isl_int **) p1;
9068 isl_int **c2 = (isl_int **) p2;
9069 int l1, l2;
9070 unsigned size = *(unsigned *) arg;
9071 int cmp;
9073 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9074 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9076 if (l1 != l2)
9077 return l1 - l2;
9079 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9080 if (cmp != 0)
9081 return cmp;
9082 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9083 if (cmp != 0)
9084 return -cmp;
9086 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9089 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9090 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9091 * and 0 if the two constraints are the same (up to the constant term).
9093 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9094 isl_int *c1, isl_int *c2)
9096 unsigned total;
9098 if (!bmap)
9099 return -2;
9100 total = isl_basic_map_total_dim(bmap);
9101 return sort_constraint_cmp(&c1, &c2, &total);
9104 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9105 __isl_take isl_basic_map *bmap)
9107 unsigned total;
9109 if (!bmap)
9110 return NULL;
9111 if (bmap->n_ineq == 0)
9112 return bmap;
9113 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9114 return bmap;
9115 total = isl_basic_map_total_dim(bmap);
9116 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9117 &sort_constraint_cmp, &total) < 0)
9118 return isl_basic_map_free(bmap);
9119 return bmap;
9122 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9123 __isl_take isl_basic_set *bset)
9125 isl_basic_map *bmap = bset_to_bmap(bset);
9126 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9129 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9131 if (!bmap)
9132 return NULL;
9133 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9134 return bmap;
9135 bmap = isl_basic_map_remove_redundancies(bmap);
9136 bmap = isl_basic_map_sort_constraints(bmap);
9137 if (bmap)
9138 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9139 return bmap;
9142 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9144 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9147 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9148 const __isl_keep isl_basic_map *bmap2)
9150 int i, cmp;
9151 unsigned total;
9153 if (!bmap1 || !bmap2)
9154 return -1;
9156 if (bmap1 == bmap2)
9157 return 0;
9158 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9159 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9160 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9161 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9162 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9163 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9164 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9165 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9166 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9167 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9168 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9169 return 0;
9170 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9171 return 1;
9172 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9173 return -1;
9174 if (bmap1->n_eq != bmap2->n_eq)
9175 return bmap1->n_eq - bmap2->n_eq;
9176 if (bmap1->n_ineq != bmap2->n_ineq)
9177 return bmap1->n_ineq - bmap2->n_ineq;
9178 if (bmap1->n_div != bmap2->n_div)
9179 return bmap1->n_div - bmap2->n_div;
9180 total = isl_basic_map_total_dim(bmap1);
9181 for (i = 0; i < bmap1->n_eq; ++i) {
9182 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9183 if (cmp)
9184 return cmp;
9186 for (i = 0; i < bmap1->n_ineq; ++i) {
9187 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9188 if (cmp)
9189 return cmp;
9191 for (i = 0; i < bmap1->n_div; ++i) {
9192 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9193 if (cmp)
9194 return cmp;
9196 return 0;
9199 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9200 const __isl_keep isl_basic_set *bset2)
9202 return isl_basic_map_plain_cmp(bset1, bset2);
9205 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9207 int i, cmp;
9209 if (set1 == set2)
9210 return 0;
9211 if (set1->n != set2->n)
9212 return set1->n - set2->n;
9214 for (i = 0; i < set1->n; ++i) {
9215 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9216 if (cmp)
9217 return cmp;
9220 return 0;
9223 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9224 __isl_keep isl_basic_map *bmap2)
9226 if (!bmap1 || !bmap2)
9227 return isl_bool_error;
9228 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9231 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9232 __isl_keep isl_basic_set *bset2)
9234 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9235 bset_to_bmap(bset2));
9238 static int qsort_bmap_cmp(const void *p1, const void *p2)
9240 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9241 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9243 return isl_basic_map_plain_cmp(bmap1, bmap2);
9246 /* Sort the basic maps of "map" and remove duplicate basic maps.
9248 * While removing basic maps, we make sure that the basic maps remain
9249 * sorted because isl_map_normalize expects the basic maps of the result
9250 * to be sorted.
9252 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9254 int i, j;
9256 map = isl_map_remove_empty_parts(map);
9257 if (!map)
9258 return NULL;
9259 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9260 for (i = map->n - 1; i >= 1; --i) {
9261 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9262 continue;
9263 isl_basic_map_free(map->p[i-1]);
9264 for (j = i; j < map->n; ++j)
9265 map->p[j - 1] = map->p[j];
9266 map->n--;
9269 return map;
9272 /* Remove obvious duplicates among the basic maps of "map".
9274 * Unlike isl_map_normalize, this function does not remove redundant
9275 * constraints and only removes duplicates that have exactly the same
9276 * constraints in the input. It does sort the constraints and
9277 * the basic maps to ease the detection of duplicates.
9279 * If "map" has already been normalized or if the basic maps are
9280 * disjoint, then there can be no duplicates.
9282 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9284 int i;
9285 isl_basic_map *bmap;
9287 if (!map)
9288 return NULL;
9289 if (map->n <= 1)
9290 return map;
9291 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9292 return map;
9293 for (i = 0; i < map->n; ++i) {
9294 bmap = isl_basic_map_copy(map->p[i]);
9295 bmap = isl_basic_map_sort_constraints(bmap);
9296 if (!bmap)
9297 return isl_map_free(map);
9298 isl_basic_map_free(map->p[i]);
9299 map->p[i] = bmap;
9302 map = sort_and_remove_duplicates(map);
9303 return map;
9306 /* We normalize in place, but if anything goes wrong we need
9307 * to return NULL, so we need to make sure we don't change the
9308 * meaning of any possible other copies of map.
9310 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9312 int i;
9313 struct isl_basic_map *bmap;
9315 if (!map)
9316 return NULL;
9317 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9318 return map;
9319 for (i = 0; i < map->n; ++i) {
9320 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9321 if (!bmap)
9322 goto error;
9323 isl_basic_map_free(map->p[i]);
9324 map->p[i] = bmap;
9327 map = sort_and_remove_duplicates(map);
9328 if (map)
9329 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9330 return map;
9331 error:
9332 isl_map_free(map);
9333 return NULL;
9336 struct isl_set *isl_set_normalize(struct isl_set *set)
9338 return set_from_map(isl_map_normalize(set_to_map(set)));
9341 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9342 __isl_keep isl_map *map2)
9344 int i;
9345 isl_bool equal;
9347 if (!map1 || !map2)
9348 return isl_bool_error;
9350 if (map1 == map2)
9351 return isl_bool_true;
9352 if (!isl_space_is_equal(map1->dim, map2->dim))
9353 return isl_bool_false;
9355 map1 = isl_map_copy(map1);
9356 map2 = isl_map_copy(map2);
9357 map1 = isl_map_normalize(map1);
9358 map2 = isl_map_normalize(map2);
9359 if (!map1 || !map2)
9360 goto error;
9361 equal = map1->n == map2->n;
9362 for (i = 0; equal && i < map1->n; ++i) {
9363 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9364 if (equal < 0)
9365 goto error;
9367 isl_map_free(map1);
9368 isl_map_free(map2);
9369 return equal;
9370 error:
9371 isl_map_free(map1);
9372 isl_map_free(map2);
9373 return isl_bool_error;
9376 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9377 __isl_keep isl_set *set2)
9379 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9382 /* Return an interval that ranges from min to max (inclusive)
9384 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9385 isl_int min, isl_int max)
9387 int k;
9388 struct isl_basic_set *bset = NULL;
9390 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9391 if (!bset)
9392 goto error;
9394 k = isl_basic_set_alloc_inequality(bset);
9395 if (k < 0)
9396 goto error;
9397 isl_int_set_si(bset->ineq[k][1], 1);
9398 isl_int_neg(bset->ineq[k][0], min);
9400 k = isl_basic_set_alloc_inequality(bset);
9401 if (k < 0)
9402 goto error;
9403 isl_int_set_si(bset->ineq[k][1], -1);
9404 isl_int_set(bset->ineq[k][0], max);
9406 return bset;
9407 error:
9408 isl_basic_set_free(bset);
9409 return NULL;
9412 /* Return the basic maps in "map" as a list.
9414 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9415 __isl_keep isl_map *map)
9417 int i;
9418 isl_ctx *ctx;
9419 isl_basic_map_list *list;
9421 if (!map)
9422 return NULL;
9423 ctx = isl_map_get_ctx(map);
9424 list = isl_basic_map_list_alloc(ctx, map->n);
9426 for (i = 0; i < map->n; ++i) {
9427 isl_basic_map *bmap;
9429 bmap = isl_basic_map_copy(map->p[i]);
9430 list = isl_basic_map_list_add(list, bmap);
9433 return list;
9436 /* Return the intersection of the elements in the non-empty list "list".
9437 * All elements are assumed to live in the same space.
9439 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9440 __isl_take isl_basic_map_list *list)
9442 int i, n;
9443 isl_basic_map *bmap;
9445 if (!list)
9446 return NULL;
9447 n = isl_basic_map_list_n_basic_map(list);
9448 if (n < 1)
9449 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9450 "expecting non-empty list", goto error);
9452 bmap = isl_basic_map_list_get_basic_map(list, 0);
9453 for (i = 1; i < n; ++i) {
9454 isl_basic_map *bmap_i;
9456 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9457 bmap = isl_basic_map_intersect(bmap, bmap_i);
9460 isl_basic_map_list_free(list);
9461 return bmap;
9462 error:
9463 isl_basic_map_list_free(list);
9464 return NULL;
9467 /* Return the intersection of the elements in the non-empty list "list".
9468 * All elements are assumed to live in the same space.
9470 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9471 __isl_take isl_basic_set_list *list)
9473 return isl_basic_map_list_intersect(list);
9476 /* Return the union of the elements of "list".
9477 * The list is required to have at least one element.
9479 __isl_give isl_set *isl_basic_set_list_union(
9480 __isl_take isl_basic_set_list *list)
9482 int i, n;
9483 isl_space *space;
9484 isl_basic_set *bset;
9485 isl_set *set;
9487 if (!list)
9488 return NULL;
9489 n = isl_basic_set_list_n_basic_set(list);
9490 if (n < 1)
9491 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9492 "expecting non-empty list", goto error);
9494 bset = isl_basic_set_list_get_basic_set(list, 0);
9495 space = isl_basic_set_get_space(bset);
9496 isl_basic_set_free(bset);
9498 set = isl_set_alloc_space(space, n, 0);
9499 for (i = 0; i < n; ++i) {
9500 bset = isl_basic_set_list_get_basic_set(list, i);
9501 set = isl_set_add_basic_set(set, bset);
9504 isl_basic_set_list_free(list);
9505 return set;
9506 error:
9507 isl_basic_set_list_free(list);
9508 return NULL;
9511 /* Return the union of the elements in the non-empty list "list".
9512 * All elements are assumed to live in the same space.
9514 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9516 int i, n;
9517 isl_set *set;
9519 if (!list)
9520 return NULL;
9521 n = isl_set_list_n_set(list);
9522 if (n < 1)
9523 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9524 "expecting non-empty list", goto error);
9526 set = isl_set_list_get_set(list, 0);
9527 for (i = 1; i < n; ++i) {
9528 isl_set *set_i;
9530 set_i = isl_set_list_get_set(list, i);
9531 set = isl_set_union(set, set_i);
9534 isl_set_list_free(list);
9535 return set;
9536 error:
9537 isl_set_list_free(list);
9538 return NULL;
9541 /* Return the Cartesian product of the basic sets in list (in the given order).
9543 __isl_give isl_basic_set *isl_basic_set_list_product(
9544 __isl_take struct isl_basic_set_list *list)
9546 int i;
9547 unsigned dim;
9548 unsigned nparam;
9549 unsigned extra;
9550 unsigned n_eq;
9551 unsigned n_ineq;
9552 struct isl_basic_set *product = NULL;
9554 if (!list)
9555 goto error;
9556 isl_assert(list->ctx, list->n > 0, goto error);
9557 isl_assert(list->ctx, list->p[0], goto error);
9558 nparam = isl_basic_set_n_param(list->p[0]);
9559 dim = isl_basic_set_n_dim(list->p[0]);
9560 extra = list->p[0]->n_div;
9561 n_eq = list->p[0]->n_eq;
9562 n_ineq = list->p[0]->n_ineq;
9563 for (i = 1; i < list->n; ++i) {
9564 isl_assert(list->ctx, list->p[i], goto error);
9565 isl_assert(list->ctx,
9566 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9567 dim += isl_basic_set_n_dim(list->p[i]);
9568 extra += list->p[i]->n_div;
9569 n_eq += list->p[i]->n_eq;
9570 n_ineq += list->p[i]->n_ineq;
9572 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9573 n_eq, n_ineq);
9574 if (!product)
9575 goto error;
9576 dim = 0;
9577 for (i = 0; i < list->n; ++i) {
9578 isl_basic_set_add_constraints(product,
9579 isl_basic_set_copy(list->p[i]), dim);
9580 dim += isl_basic_set_n_dim(list->p[i]);
9582 isl_basic_set_list_free(list);
9583 return product;
9584 error:
9585 isl_basic_set_free(product);
9586 isl_basic_set_list_free(list);
9587 return NULL;
9590 struct isl_basic_map *isl_basic_map_product(
9591 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9593 isl_space *dim_result = NULL;
9594 struct isl_basic_map *bmap;
9595 unsigned in1, in2, out1, out2, nparam, total, pos;
9596 struct isl_dim_map *dim_map1, *dim_map2;
9598 if (!bmap1 || !bmap2)
9599 goto error;
9601 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9602 bmap2->dim, isl_dim_param), goto error);
9603 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9604 isl_space_copy(bmap2->dim));
9606 in1 = isl_basic_map_n_in(bmap1);
9607 in2 = isl_basic_map_n_in(bmap2);
9608 out1 = isl_basic_map_n_out(bmap1);
9609 out2 = isl_basic_map_n_out(bmap2);
9610 nparam = isl_basic_map_n_param(bmap1);
9612 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9613 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9614 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9615 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9616 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9617 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9618 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9619 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9620 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9621 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9622 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9624 bmap = isl_basic_map_alloc_space(dim_result,
9625 bmap1->n_div + bmap2->n_div,
9626 bmap1->n_eq + bmap2->n_eq,
9627 bmap1->n_ineq + bmap2->n_ineq);
9628 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9629 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9630 bmap = isl_basic_map_simplify(bmap);
9631 return isl_basic_map_finalize(bmap);
9632 error:
9633 isl_basic_map_free(bmap1);
9634 isl_basic_map_free(bmap2);
9635 return NULL;
9638 __isl_give isl_basic_map *isl_basic_map_flat_product(
9639 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9641 isl_basic_map *prod;
9643 prod = isl_basic_map_product(bmap1, bmap2);
9644 prod = isl_basic_map_flatten(prod);
9645 return prod;
9648 __isl_give isl_basic_set *isl_basic_set_flat_product(
9649 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9651 return isl_basic_map_flat_range_product(bset1, bset2);
9654 __isl_give isl_basic_map *isl_basic_map_domain_product(
9655 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9657 isl_space *space_result = NULL;
9658 isl_basic_map *bmap;
9659 unsigned in1, in2, out, nparam, total, pos;
9660 struct isl_dim_map *dim_map1, *dim_map2;
9662 if (!bmap1 || !bmap2)
9663 goto error;
9665 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9666 isl_space_copy(bmap2->dim));
9668 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9669 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9670 out = isl_basic_map_dim(bmap1, isl_dim_out);
9671 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9673 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9674 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9675 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9676 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9677 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9678 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9679 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9680 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9681 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9682 isl_dim_map_div(dim_map1, bmap1, pos += out);
9683 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9685 bmap = isl_basic_map_alloc_space(space_result,
9686 bmap1->n_div + bmap2->n_div,
9687 bmap1->n_eq + bmap2->n_eq,
9688 bmap1->n_ineq + bmap2->n_ineq);
9689 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9690 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9691 bmap = isl_basic_map_simplify(bmap);
9692 return isl_basic_map_finalize(bmap);
9693 error:
9694 isl_basic_map_free(bmap1);
9695 isl_basic_map_free(bmap2);
9696 return NULL;
9699 __isl_give isl_basic_map *isl_basic_map_range_product(
9700 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9702 int rational;
9703 isl_space *dim_result = NULL;
9704 isl_basic_map *bmap;
9705 unsigned in, out1, out2, nparam, total, pos;
9706 struct isl_dim_map *dim_map1, *dim_map2;
9708 rational = isl_basic_map_is_rational(bmap1);
9709 if (rational >= 0 && rational)
9710 rational = isl_basic_map_is_rational(bmap2);
9711 if (!bmap1 || !bmap2 || rational < 0)
9712 goto error;
9714 if (!isl_space_match(bmap1->dim, isl_dim_param,
9715 bmap2->dim, isl_dim_param))
9716 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9717 "parameters don't match", goto error);
9719 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9720 isl_space_copy(bmap2->dim));
9722 in = isl_basic_map_dim(bmap1, isl_dim_in);
9723 out1 = isl_basic_map_n_out(bmap1);
9724 out2 = isl_basic_map_n_out(bmap2);
9725 nparam = isl_basic_map_n_param(bmap1);
9727 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9728 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9729 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9730 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9731 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9732 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9733 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9734 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9735 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9736 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9737 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9739 bmap = isl_basic_map_alloc_space(dim_result,
9740 bmap1->n_div + bmap2->n_div,
9741 bmap1->n_eq + bmap2->n_eq,
9742 bmap1->n_ineq + bmap2->n_ineq);
9743 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9744 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9745 if (rational)
9746 bmap = isl_basic_map_set_rational(bmap);
9747 bmap = isl_basic_map_simplify(bmap);
9748 return isl_basic_map_finalize(bmap);
9749 error:
9750 isl_basic_map_free(bmap1);
9751 isl_basic_map_free(bmap2);
9752 return NULL;
9755 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9756 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9758 isl_basic_map *prod;
9760 prod = isl_basic_map_range_product(bmap1, bmap2);
9761 prod = isl_basic_map_flatten_range(prod);
9762 return prod;
9765 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9766 * and collect the results.
9767 * The result live in the space obtained by calling "space_product"
9768 * on the spaces of "map1" and "map2".
9769 * If "remove_duplicates" is set then the result may contain duplicates
9770 * (even if the inputs do not) and so we try and remove the obvious
9771 * duplicates.
9773 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9774 __isl_take isl_map *map2,
9775 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9776 __isl_take isl_space *right),
9777 __isl_give isl_basic_map *(*basic_map_product)(
9778 __isl_take isl_basic_map *left,
9779 __isl_take isl_basic_map *right),
9780 int remove_duplicates)
9782 unsigned flags = 0;
9783 struct isl_map *result;
9784 int i, j;
9786 if (!map1 || !map2)
9787 goto error;
9789 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9790 map2->dim, isl_dim_param), goto error);
9792 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9793 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9794 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9796 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9797 isl_space_copy(map2->dim)),
9798 map1->n * map2->n, flags);
9799 if (!result)
9800 goto error;
9801 for (i = 0; i < map1->n; ++i)
9802 for (j = 0; j < map2->n; ++j) {
9803 struct isl_basic_map *part;
9804 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9805 isl_basic_map_copy(map2->p[j]));
9806 if (isl_basic_map_is_empty(part))
9807 isl_basic_map_free(part);
9808 else
9809 result = isl_map_add_basic_map(result, part);
9810 if (!result)
9811 goto error;
9813 if (remove_duplicates)
9814 result = isl_map_remove_obvious_duplicates(result);
9815 isl_map_free(map1);
9816 isl_map_free(map2);
9817 return result;
9818 error:
9819 isl_map_free(map1);
9820 isl_map_free(map2);
9821 return NULL;
9824 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9826 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9827 __isl_take isl_map *map2)
9829 return map_product(map1, map2, &isl_space_product,
9830 &isl_basic_map_product, 0);
9833 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9834 __isl_take isl_map *map2)
9836 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9839 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9841 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9842 __isl_take isl_map *map2)
9844 isl_map *prod;
9846 prod = isl_map_product(map1, map2);
9847 prod = isl_map_flatten(prod);
9848 return prod;
9851 /* Given two set A and B, construct its Cartesian product A x B.
9853 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9855 return isl_map_range_product(set1, set2);
9858 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9859 __isl_take isl_set *set2)
9861 return isl_map_flat_range_product(set1, set2);
9864 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9866 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9867 __isl_take isl_map *map2)
9869 return map_product(map1, map2, &isl_space_domain_product,
9870 &isl_basic_map_domain_product, 1);
9873 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9875 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9876 __isl_take isl_map *map2)
9878 return map_product(map1, map2, &isl_space_range_product,
9879 &isl_basic_map_range_product, 1);
9882 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9883 __isl_take isl_map *map2)
9885 return isl_map_align_params_map_map_and(map1, map2,
9886 &map_domain_product_aligned);
9889 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9890 __isl_take isl_map *map2)
9892 return isl_map_align_params_map_map_and(map1, map2,
9893 &map_range_product_aligned);
9896 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9898 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9900 isl_space *space;
9901 int total1, keep1, total2, keep2;
9903 if (!map)
9904 return NULL;
9905 if (!isl_space_domain_is_wrapping(map->dim) ||
9906 !isl_space_range_is_wrapping(map->dim))
9907 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9908 "not a product", return isl_map_free(map));
9910 space = isl_map_get_space(map);
9911 total1 = isl_space_dim(space, isl_dim_in);
9912 total2 = isl_space_dim(space, isl_dim_out);
9913 space = isl_space_factor_domain(space);
9914 keep1 = isl_space_dim(space, isl_dim_in);
9915 keep2 = isl_space_dim(space, isl_dim_out);
9916 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9917 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9918 map = isl_map_reset_space(map, space);
9920 return map;
9923 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9925 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9927 isl_space *space;
9928 int total1, keep1, total2, keep2;
9930 if (!map)
9931 return NULL;
9932 if (!isl_space_domain_is_wrapping(map->dim) ||
9933 !isl_space_range_is_wrapping(map->dim))
9934 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9935 "not a product", return isl_map_free(map));
9937 space = isl_map_get_space(map);
9938 total1 = isl_space_dim(space, isl_dim_in);
9939 total2 = isl_space_dim(space, isl_dim_out);
9940 space = isl_space_factor_range(space);
9941 keep1 = isl_space_dim(space, isl_dim_in);
9942 keep2 = isl_space_dim(space, isl_dim_out);
9943 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9944 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9945 map = isl_map_reset_space(map, space);
9947 return map;
9950 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9952 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9954 isl_space *space;
9955 int total, keep;
9957 if (!map)
9958 return NULL;
9959 if (!isl_space_domain_is_wrapping(map->dim))
9960 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9961 "domain is not a product", return isl_map_free(map));
9963 space = isl_map_get_space(map);
9964 total = isl_space_dim(space, isl_dim_in);
9965 space = isl_space_domain_factor_domain(space);
9966 keep = isl_space_dim(space, isl_dim_in);
9967 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9968 map = isl_map_reset_space(map, space);
9970 return map;
9973 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9975 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9977 isl_space *space;
9978 int total, keep;
9980 if (!map)
9981 return NULL;
9982 if (!isl_space_domain_is_wrapping(map->dim))
9983 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9984 "domain is not a product", return isl_map_free(map));
9986 space = isl_map_get_space(map);
9987 total = isl_space_dim(space, isl_dim_in);
9988 space = isl_space_domain_factor_range(space);
9989 keep = isl_space_dim(space, isl_dim_in);
9990 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9991 map = isl_map_reset_space(map, space);
9993 return map;
9996 /* Given a map A -> [B -> C], extract the map A -> B.
9998 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10000 isl_space *space;
10001 int total, keep;
10003 if (!map)
10004 return NULL;
10005 if (!isl_space_range_is_wrapping(map->dim))
10006 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10007 "range is not a product", return isl_map_free(map));
10009 space = isl_map_get_space(map);
10010 total = isl_space_dim(space, isl_dim_out);
10011 space = isl_space_range_factor_domain(space);
10012 keep = isl_space_dim(space, isl_dim_out);
10013 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10014 map = isl_map_reset_space(map, space);
10016 return map;
10019 /* Given a map A -> [B -> C], extract the map A -> C.
10021 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10023 isl_space *space;
10024 int total, keep;
10026 if (!map)
10027 return NULL;
10028 if (!isl_space_range_is_wrapping(map->dim))
10029 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10030 "range is not a product", return isl_map_free(map));
10032 space = isl_map_get_space(map);
10033 total = isl_space_dim(space, isl_dim_out);
10034 space = isl_space_range_factor_range(space);
10035 keep = isl_space_dim(space, isl_dim_out);
10036 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10037 map = isl_map_reset_space(map, space);
10039 return map;
10042 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10044 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10045 __isl_take isl_map *map2)
10047 isl_map *prod;
10049 prod = isl_map_domain_product(map1, map2);
10050 prod = isl_map_flatten_domain(prod);
10051 return prod;
10054 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10056 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10057 __isl_take isl_map *map2)
10059 isl_map *prod;
10061 prod = isl_map_range_product(map1, map2);
10062 prod = isl_map_flatten_range(prod);
10063 return prod;
10066 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10068 int i;
10069 uint32_t hash = isl_hash_init();
10070 unsigned total;
10072 if (!bmap)
10073 return 0;
10074 bmap = isl_basic_map_copy(bmap);
10075 bmap = isl_basic_map_normalize(bmap);
10076 if (!bmap)
10077 return 0;
10078 total = isl_basic_map_total_dim(bmap);
10079 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10080 for (i = 0; i < bmap->n_eq; ++i) {
10081 uint32_t c_hash;
10082 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10083 isl_hash_hash(hash, c_hash);
10085 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10086 for (i = 0; i < bmap->n_ineq; ++i) {
10087 uint32_t c_hash;
10088 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10089 isl_hash_hash(hash, c_hash);
10091 isl_hash_byte(hash, bmap->n_div & 0xFF);
10092 for (i = 0; i < bmap->n_div; ++i) {
10093 uint32_t c_hash;
10094 if (isl_int_is_zero(bmap->div[i][0]))
10095 continue;
10096 isl_hash_byte(hash, i & 0xFF);
10097 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10098 isl_hash_hash(hash, c_hash);
10100 isl_basic_map_free(bmap);
10101 return hash;
10104 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10106 return isl_basic_map_get_hash(bset_to_bmap(bset));
10109 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10111 int i;
10112 uint32_t hash;
10114 if (!map)
10115 return 0;
10116 map = isl_map_copy(map);
10117 map = isl_map_normalize(map);
10118 if (!map)
10119 return 0;
10121 hash = isl_hash_init();
10122 for (i = 0; i < map->n; ++i) {
10123 uint32_t bmap_hash;
10124 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10125 isl_hash_hash(hash, bmap_hash);
10128 isl_map_free(map);
10130 return hash;
10133 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10135 return isl_map_get_hash(set_to_map(set));
10138 /* Check if the value for dimension dim is completely determined
10139 * by the values of the other parameters and variables.
10140 * That is, check if dimension dim is involved in an equality.
10142 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10144 int i;
10145 unsigned nparam;
10147 if (!bset)
10148 return -1;
10149 nparam = isl_basic_set_n_param(bset);
10150 for (i = 0; i < bset->n_eq; ++i)
10151 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10152 return 1;
10153 return 0;
10156 /* Check if the value for dimension dim is completely determined
10157 * by the values of the other parameters and variables.
10158 * That is, check if dimension dim is involved in an equality
10159 * for each of the subsets.
10161 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10163 int i;
10165 if (!set)
10166 return -1;
10167 for (i = 0; i < set->n; ++i) {
10168 int unique;
10169 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10170 if (unique != 1)
10171 return unique;
10173 return 1;
10176 /* Return the number of basic maps in the (current) representation of "map".
10178 int isl_map_n_basic_map(__isl_keep isl_map *map)
10180 return map ? map->n : 0;
10183 int isl_set_n_basic_set(__isl_keep isl_set *set)
10185 return set ? set->n : 0;
10188 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10189 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10191 int i;
10193 if (!map)
10194 return isl_stat_error;
10196 for (i = 0; i < map->n; ++i)
10197 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10198 return isl_stat_error;
10200 return isl_stat_ok;
10203 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10204 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10206 int i;
10208 if (!set)
10209 return isl_stat_error;
10211 for (i = 0; i < set->n; ++i)
10212 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10213 return isl_stat_error;
10215 return isl_stat_ok;
10218 /* Return a list of basic sets, the union of which is equal to "set".
10220 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10221 __isl_keep isl_set *set)
10223 int i;
10224 isl_basic_set_list *list;
10226 if (!set)
10227 return NULL;
10229 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10230 for (i = 0; i < set->n; ++i) {
10231 isl_basic_set *bset;
10233 bset = isl_basic_set_copy(set->p[i]);
10234 list = isl_basic_set_list_add(list, bset);
10237 return list;
10240 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10242 isl_space *dim;
10244 if (!bset)
10245 return NULL;
10247 bset = isl_basic_set_cow(bset);
10248 if (!bset)
10249 return NULL;
10251 dim = isl_basic_set_get_space(bset);
10252 dim = isl_space_lift(dim, bset->n_div);
10253 if (!dim)
10254 goto error;
10255 isl_space_free(bset->dim);
10256 bset->dim = dim;
10257 bset->extra -= bset->n_div;
10258 bset->n_div = 0;
10260 bset = isl_basic_set_finalize(bset);
10262 return bset;
10263 error:
10264 isl_basic_set_free(bset);
10265 return NULL;
10268 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10270 int i;
10271 isl_space *dim;
10272 unsigned n_div;
10274 set = isl_set_align_divs(set);
10276 if (!set)
10277 return NULL;
10279 set = isl_set_cow(set);
10280 if (!set)
10281 return NULL;
10283 n_div = set->p[0]->n_div;
10284 dim = isl_set_get_space(set);
10285 dim = isl_space_lift(dim, n_div);
10286 if (!dim)
10287 goto error;
10288 isl_space_free(set->dim);
10289 set->dim = dim;
10291 for (i = 0; i < set->n; ++i) {
10292 set->p[i] = isl_basic_set_lift(set->p[i]);
10293 if (!set->p[i])
10294 goto error;
10297 return set;
10298 error:
10299 isl_set_free(set);
10300 return NULL;
10303 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10305 isl_space *dim;
10306 struct isl_basic_map *bmap;
10307 unsigned n_set;
10308 unsigned n_div;
10309 unsigned n_param;
10310 unsigned total;
10311 int i, k, l;
10313 set = isl_set_align_divs(set);
10315 if (!set)
10316 return NULL;
10318 dim = isl_set_get_space(set);
10319 if (set->n == 0 || set->p[0]->n_div == 0) {
10320 isl_set_free(set);
10321 return isl_map_identity(isl_space_map_from_set(dim));
10324 n_div = set->p[0]->n_div;
10325 dim = isl_space_map_from_set(dim);
10326 n_param = isl_space_dim(dim, isl_dim_param);
10327 n_set = isl_space_dim(dim, isl_dim_in);
10328 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10329 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10330 for (i = 0; i < n_set; ++i)
10331 bmap = var_equal(bmap, i);
10333 total = n_param + n_set + n_set + n_div;
10334 for (i = 0; i < n_div; ++i) {
10335 k = isl_basic_map_alloc_inequality(bmap);
10336 if (k < 0)
10337 goto error;
10338 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10339 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10340 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10341 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10342 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10343 set->p[0]->div[i][0]);
10345 l = isl_basic_map_alloc_inequality(bmap);
10346 if (l < 0)
10347 goto error;
10348 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10349 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10350 set->p[0]->div[i][0]);
10351 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10354 isl_set_free(set);
10355 bmap = isl_basic_map_simplify(bmap);
10356 bmap = isl_basic_map_finalize(bmap);
10357 return isl_map_from_basic_map(bmap);
10358 error:
10359 isl_set_free(set);
10360 isl_basic_map_free(bmap);
10361 return NULL;
10364 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10366 unsigned dim;
10367 int size = 0;
10369 if (!bset)
10370 return -1;
10372 dim = isl_basic_set_total_dim(bset);
10373 size += bset->n_eq * (1 + dim);
10374 size += bset->n_ineq * (1 + dim);
10375 size += bset->n_div * (2 + dim);
10377 return size;
10380 int isl_set_size(__isl_keep isl_set *set)
10382 int i;
10383 int size = 0;
10385 if (!set)
10386 return -1;
10388 for (i = 0; i < set->n; ++i)
10389 size += isl_basic_set_size(set->p[i]);
10391 return size;
10394 /* Check if there is any lower bound (if lower == 0) and/or upper
10395 * bound (if upper == 0) on the specified dim.
10397 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10398 enum isl_dim_type type, unsigned pos, int lower, int upper)
10400 int i;
10402 if (!bmap)
10403 return isl_bool_error;
10405 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10406 return isl_bool_error);
10408 pos += isl_basic_map_offset(bmap, type);
10410 for (i = 0; i < bmap->n_div; ++i) {
10411 if (isl_int_is_zero(bmap->div[i][0]))
10412 continue;
10413 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10414 return isl_bool_true;
10417 for (i = 0; i < bmap->n_eq; ++i)
10418 if (!isl_int_is_zero(bmap->eq[i][pos]))
10419 return isl_bool_true;
10421 for (i = 0; i < bmap->n_ineq; ++i) {
10422 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10423 if (sgn > 0)
10424 lower = 1;
10425 if (sgn < 0)
10426 upper = 1;
10429 return lower && upper;
10432 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10433 enum isl_dim_type type, unsigned pos)
10435 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10438 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10439 enum isl_dim_type type, unsigned pos)
10441 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10444 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10445 enum isl_dim_type type, unsigned pos)
10447 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10450 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10451 enum isl_dim_type type, unsigned pos)
10453 int i;
10455 if (!map)
10456 return -1;
10458 for (i = 0; i < map->n; ++i) {
10459 int bounded;
10460 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10461 if (bounded < 0 || !bounded)
10462 return bounded;
10465 return 1;
10468 /* Return 1 if the specified dim is involved in both an upper bound
10469 * and a lower bound.
10471 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10472 enum isl_dim_type type, unsigned pos)
10474 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10477 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10479 static isl_bool has_any_bound(__isl_keep isl_map *map,
10480 enum isl_dim_type type, unsigned pos,
10481 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10482 enum isl_dim_type type, unsigned pos))
10484 int i;
10486 if (!map)
10487 return isl_bool_error;
10489 for (i = 0; i < map->n; ++i) {
10490 isl_bool bounded;
10491 bounded = fn(map->p[i], type, pos);
10492 if (bounded < 0 || bounded)
10493 return bounded;
10496 return isl_bool_false;
10499 /* Return 1 if the specified dim is involved in any lower bound.
10501 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10502 enum isl_dim_type type, unsigned pos)
10504 return has_any_bound(set, type, pos,
10505 &isl_basic_map_dim_has_lower_bound);
10508 /* Return 1 if the specified dim is involved in any upper bound.
10510 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10511 enum isl_dim_type type, unsigned pos)
10513 return has_any_bound(set, type, pos,
10514 &isl_basic_map_dim_has_upper_bound);
10517 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10519 static isl_bool has_bound(__isl_keep isl_map *map,
10520 enum isl_dim_type type, unsigned pos,
10521 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10522 enum isl_dim_type type, unsigned pos))
10524 int i;
10526 if (!map)
10527 return isl_bool_error;
10529 for (i = 0; i < map->n; ++i) {
10530 isl_bool bounded;
10531 bounded = fn(map->p[i], type, pos);
10532 if (bounded < 0 || !bounded)
10533 return bounded;
10536 return isl_bool_true;
10539 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10541 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10542 enum isl_dim_type type, unsigned pos)
10544 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10547 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10549 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10550 enum isl_dim_type type, unsigned pos)
10552 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10555 /* For each of the "n" variables starting at "first", determine
10556 * the sign of the variable and put the results in the first "n"
10557 * elements of the array "signs".
10558 * Sign
10559 * 1 means that the variable is non-negative
10560 * -1 means that the variable is non-positive
10561 * 0 means the variable attains both positive and negative values.
10563 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10564 unsigned first, unsigned n, int *signs)
10566 isl_vec *bound = NULL;
10567 struct isl_tab *tab = NULL;
10568 struct isl_tab_undo *snap;
10569 int i;
10571 if (!bset || !signs)
10572 return -1;
10574 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10575 tab = isl_tab_from_basic_set(bset, 0);
10576 if (!bound || !tab)
10577 goto error;
10579 isl_seq_clr(bound->el, bound->size);
10580 isl_int_set_si(bound->el[0], -1);
10582 snap = isl_tab_snap(tab);
10583 for (i = 0; i < n; ++i) {
10584 int empty;
10586 isl_int_set_si(bound->el[1 + first + i], -1);
10587 if (isl_tab_add_ineq(tab, bound->el) < 0)
10588 goto error;
10589 empty = tab->empty;
10590 isl_int_set_si(bound->el[1 + first + i], 0);
10591 if (isl_tab_rollback(tab, snap) < 0)
10592 goto error;
10594 if (empty) {
10595 signs[i] = 1;
10596 continue;
10599 isl_int_set_si(bound->el[1 + first + i], 1);
10600 if (isl_tab_add_ineq(tab, bound->el) < 0)
10601 goto error;
10602 empty = tab->empty;
10603 isl_int_set_si(bound->el[1 + first + i], 0);
10604 if (isl_tab_rollback(tab, snap) < 0)
10605 goto error;
10607 signs[i] = empty ? -1 : 0;
10610 isl_tab_free(tab);
10611 isl_vec_free(bound);
10612 return 0;
10613 error:
10614 isl_tab_free(tab);
10615 isl_vec_free(bound);
10616 return -1;
10619 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10620 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10622 if (!bset || !signs)
10623 return -1;
10624 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10625 return -1);
10627 first += pos(bset->dim, type) - 1;
10628 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10631 /* Is it possible for the integer division "div" to depend (possibly
10632 * indirectly) on any output dimensions?
10634 * If the div is undefined, then we conservatively assume that it
10635 * may depend on them.
10636 * Otherwise, we check if it actually depends on them or on any integer
10637 * divisions that may depend on them.
10639 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10641 int i;
10642 unsigned n_out, o_out;
10643 unsigned n_div, o_div;
10645 if (isl_int_is_zero(bmap->div[div][0]))
10646 return 1;
10648 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10649 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10651 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10652 return 1;
10654 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10655 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10657 for (i = 0; i < n_div; ++i) {
10658 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10659 continue;
10660 if (div_may_involve_output(bmap, i))
10661 return 1;
10664 return 0;
10667 /* Return the first integer division of "bmap" in the range
10668 * [first, first + n[ that may depend on any output dimensions and
10669 * that has a non-zero coefficient in "c" (where the first coefficient
10670 * in "c" corresponds to integer division "first").
10672 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10673 isl_int *c, int first, int n)
10675 int k;
10677 if (!bmap)
10678 return -1;
10680 for (k = first; k < first + n; ++k) {
10681 if (isl_int_is_zero(c[k]))
10682 continue;
10683 if (div_may_involve_output(bmap, k))
10684 return k;
10687 return first + n;
10690 /* Look for a pair of inequality constraints in "bmap" of the form
10692 * -l + i >= 0 or i >= l
10693 * and
10694 * n + l - i >= 0 or i <= l + n
10696 * with n < "m" and i the output dimension at position "pos".
10697 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10698 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10699 * and earlier output dimensions, as well as integer divisions that do
10700 * not involve any of the output dimensions.
10702 * Return the index of the first inequality constraint or bmap->n_ineq
10703 * if no such pair can be found.
10705 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10706 int pos, isl_int m)
10708 int i, j;
10709 isl_ctx *ctx;
10710 unsigned total;
10711 unsigned n_div, o_div;
10712 unsigned n_out, o_out;
10713 int less;
10715 if (!bmap)
10716 return -1;
10718 ctx = isl_basic_map_get_ctx(bmap);
10719 total = isl_basic_map_total_dim(bmap);
10720 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10721 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10722 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10723 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10724 for (i = 0; i < bmap->n_ineq; ++i) {
10725 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10726 continue;
10727 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10728 n_out - (pos + 1)) != -1)
10729 continue;
10730 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10731 0, n_div) < n_div)
10732 continue;
10733 for (j = i + 1; j < bmap->n_ineq; ++j) {
10734 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10735 ctx->one))
10736 continue;
10737 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10738 bmap->ineq[j] + 1, total))
10739 continue;
10740 break;
10742 if (j >= bmap->n_ineq)
10743 continue;
10744 isl_int_add(bmap->ineq[i][0],
10745 bmap->ineq[i][0], bmap->ineq[j][0]);
10746 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10747 isl_int_sub(bmap->ineq[i][0],
10748 bmap->ineq[i][0], bmap->ineq[j][0]);
10749 if (!less)
10750 continue;
10751 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10752 return i;
10753 else
10754 return j;
10757 return bmap->n_ineq;
10760 /* Return the index of the equality of "bmap" that defines
10761 * the output dimension "pos" in terms of earlier dimensions.
10762 * The equality may also involve integer divisions, as long
10763 * as those integer divisions are defined in terms of
10764 * parameters or input dimensions.
10765 * In this case, *div is set to the number of integer divisions and
10766 * *ineq is set to the number of inequality constraints (provided
10767 * div and ineq are not NULL).
10769 * The equality may also involve a single integer division involving
10770 * the output dimensions (typically only output dimension "pos") as
10771 * long as the coefficient of output dimension "pos" is 1 or -1 and
10772 * there is a pair of constraints i >= l and i <= l + n, with i referring
10773 * to output dimension "pos", l an expression involving only earlier
10774 * dimensions and n smaller than the coefficient of the integer division
10775 * in the equality. In this case, the output dimension can be defined
10776 * in terms of a modulo expression that does not involve the integer division.
10777 * *div is then set to this single integer division and
10778 * *ineq is set to the index of constraint i >= l.
10780 * Return bmap->n_eq if there is no such equality.
10781 * Return -1 on error.
10783 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10784 int pos, int *div, int *ineq)
10786 int j, k, l;
10787 unsigned n_out, o_out;
10788 unsigned n_div, o_div;
10790 if (!bmap)
10791 return -1;
10793 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10794 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10795 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10796 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10798 if (ineq)
10799 *ineq = bmap->n_ineq;
10800 if (div)
10801 *div = n_div;
10802 for (j = 0; j < bmap->n_eq; ++j) {
10803 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10804 continue;
10805 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10806 n_out - (pos + 1)) != -1)
10807 continue;
10808 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10809 0, n_div);
10810 if (k >= n_div)
10811 return j;
10812 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10813 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10814 continue;
10815 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10816 k + 1, n_div - (k+1)) < n_div)
10817 continue;
10818 l = find_modulo_constraint_pair(bmap, pos,
10819 bmap->eq[j][o_div + k]);
10820 if (l < 0)
10821 return -1;
10822 if (l >= bmap->n_ineq)
10823 continue;
10824 if (div)
10825 *div = k;
10826 if (ineq)
10827 *ineq = l;
10828 return j;
10831 return bmap->n_eq;
10834 /* Check if the given basic map is obviously single-valued.
10835 * In particular, for each output dimension, check that there is
10836 * an equality that defines the output dimension in terms of
10837 * earlier dimensions.
10839 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10841 int i;
10842 unsigned n_out;
10844 if (!bmap)
10845 return isl_bool_error;
10847 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10849 for (i = 0; i < n_out; ++i) {
10850 int eq;
10852 eq = isl_basic_map_output_defining_equality(bmap, i,
10853 NULL, NULL);
10854 if (eq < 0)
10855 return isl_bool_error;
10856 if (eq >= bmap->n_eq)
10857 return isl_bool_false;
10860 return isl_bool_true;
10863 /* Check if the given basic map is single-valued.
10864 * We simply compute
10866 * M \circ M^-1
10868 * and check if the result is a subset of the identity mapping.
10870 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10872 isl_space *space;
10873 isl_basic_map *test;
10874 isl_basic_map *id;
10875 isl_bool sv;
10877 sv = isl_basic_map_plain_is_single_valued(bmap);
10878 if (sv < 0 || sv)
10879 return sv;
10881 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10882 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10884 space = isl_basic_map_get_space(bmap);
10885 space = isl_space_map_from_set(isl_space_range(space));
10886 id = isl_basic_map_identity(space);
10888 sv = isl_basic_map_is_subset(test, id);
10890 isl_basic_map_free(test);
10891 isl_basic_map_free(id);
10893 return sv;
10896 /* Check if the given map is obviously single-valued.
10898 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10900 if (!map)
10901 return isl_bool_error;
10902 if (map->n == 0)
10903 return isl_bool_true;
10904 if (map->n >= 2)
10905 return isl_bool_false;
10907 return isl_basic_map_plain_is_single_valued(map->p[0]);
10910 /* Check if the given map is single-valued.
10911 * We simply compute
10913 * M \circ M^-1
10915 * and check if the result is a subset of the identity mapping.
10917 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10919 isl_space *dim;
10920 isl_map *test;
10921 isl_map *id;
10922 isl_bool sv;
10924 sv = isl_map_plain_is_single_valued(map);
10925 if (sv < 0 || sv)
10926 return sv;
10928 test = isl_map_reverse(isl_map_copy(map));
10929 test = isl_map_apply_range(test, isl_map_copy(map));
10931 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10932 id = isl_map_identity(dim);
10934 sv = isl_map_is_subset(test, id);
10936 isl_map_free(test);
10937 isl_map_free(id);
10939 return sv;
10942 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10944 isl_bool in;
10946 map = isl_map_copy(map);
10947 map = isl_map_reverse(map);
10948 in = isl_map_is_single_valued(map);
10949 isl_map_free(map);
10951 return in;
10954 /* Check if the given map is obviously injective.
10956 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10958 isl_bool in;
10960 map = isl_map_copy(map);
10961 map = isl_map_reverse(map);
10962 in = isl_map_plain_is_single_valued(map);
10963 isl_map_free(map);
10965 return in;
10968 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10970 isl_bool sv;
10972 sv = isl_map_is_single_valued(map);
10973 if (sv < 0 || !sv)
10974 return sv;
10976 return isl_map_is_injective(map);
10979 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10981 return isl_map_is_single_valued(set_to_map(set));
10984 /* Does "map" only map elements to themselves?
10986 * If the domain and range spaces are different, then "map"
10987 * is considered not to be an identity relation, even if it is empty.
10988 * Otherwise, construct the maximal identity relation and
10989 * check whether "map" is a subset of this relation.
10991 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10993 isl_space *space;
10994 isl_map *id;
10995 isl_bool equal, is_identity;
10997 space = isl_map_get_space(map);
10998 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10999 isl_space_free(space);
11000 if (equal < 0 || !equal)
11001 return equal;
11003 id = isl_map_identity(isl_map_get_space(map));
11004 is_identity = isl_map_is_subset(map, id);
11005 isl_map_free(id);
11007 return is_identity;
11010 int isl_map_is_translation(__isl_keep isl_map *map)
11012 int ok;
11013 isl_set *delta;
11015 delta = isl_map_deltas(isl_map_copy(map));
11016 ok = isl_set_is_singleton(delta);
11017 isl_set_free(delta);
11019 return ok;
11022 static int unique(isl_int *p, unsigned pos, unsigned len)
11024 if (isl_seq_first_non_zero(p, pos) != -1)
11025 return 0;
11026 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11027 return 0;
11028 return 1;
11031 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11033 int i, j;
11034 unsigned nvar;
11035 unsigned ovar;
11037 if (!bset)
11038 return -1;
11040 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11041 return 0;
11043 nvar = isl_basic_set_dim(bset, isl_dim_set);
11044 ovar = isl_space_offset(bset->dim, isl_dim_set);
11045 for (j = 0; j < nvar; ++j) {
11046 int lower = 0, upper = 0;
11047 for (i = 0; i < bset->n_eq; ++i) {
11048 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11049 continue;
11050 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11051 return 0;
11052 break;
11054 if (i < bset->n_eq)
11055 continue;
11056 for (i = 0; i < bset->n_ineq; ++i) {
11057 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11058 continue;
11059 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11060 return 0;
11061 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11062 lower = 1;
11063 else
11064 upper = 1;
11066 if (!lower || !upper)
11067 return 0;
11070 return 1;
11073 int isl_set_is_box(__isl_keep isl_set *set)
11075 if (!set)
11076 return -1;
11077 if (set->n != 1)
11078 return 0;
11080 return isl_basic_set_is_box(set->p[0]);
11083 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11085 if (!bset)
11086 return isl_bool_error;
11088 return isl_space_is_wrapping(bset->dim);
11091 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11093 if (!set)
11094 return isl_bool_error;
11096 return isl_space_is_wrapping(set->dim);
11099 /* Modify the space of "map" through a call to "change".
11100 * If "can_change" is set (not NULL), then first call it to check
11101 * if the modification is allowed, printing the error message "cannot_change"
11102 * if it is not.
11104 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11105 isl_bool (*can_change)(__isl_keep isl_map *map),
11106 const char *cannot_change,
11107 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11109 isl_bool ok;
11110 isl_space *space;
11112 if (!map)
11113 return NULL;
11115 ok = can_change ? can_change(map) : isl_bool_true;
11116 if (ok < 0)
11117 return isl_map_free(map);
11118 if (!ok)
11119 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11120 return isl_map_free(map));
11122 space = change(isl_map_get_space(map));
11123 map = isl_map_reset_space(map, space);
11125 return map;
11128 /* Is the domain of "map" a wrapped relation?
11130 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11132 if (!map)
11133 return isl_bool_error;
11135 return isl_space_domain_is_wrapping(map->dim);
11138 /* Is the range of "map" a wrapped relation?
11140 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11142 if (!map)
11143 return isl_bool_error;
11145 return isl_space_range_is_wrapping(map->dim);
11148 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11150 bmap = isl_basic_map_cow(bmap);
11151 if (!bmap)
11152 return NULL;
11154 bmap->dim = isl_space_wrap(bmap->dim);
11155 if (!bmap->dim)
11156 goto error;
11158 bmap = isl_basic_map_finalize(bmap);
11160 return bset_from_bmap(bmap);
11161 error:
11162 isl_basic_map_free(bmap);
11163 return NULL;
11166 /* Given a map A -> B, return the set (A -> B).
11168 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11170 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11173 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11175 bset = isl_basic_set_cow(bset);
11176 if (!bset)
11177 return NULL;
11179 bset->dim = isl_space_unwrap(bset->dim);
11180 if (!bset->dim)
11181 goto error;
11183 bset = isl_basic_set_finalize(bset);
11185 return bset_to_bmap(bset);
11186 error:
11187 isl_basic_set_free(bset);
11188 return NULL;
11191 /* Given a set (A -> B), return the map A -> B.
11192 * Error out if "set" is not of the form (A -> B).
11194 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11196 return isl_map_change_space(set, &isl_set_is_wrapping,
11197 "not a wrapping set", &isl_space_unwrap);
11200 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11201 enum isl_dim_type type)
11203 if (!bmap)
11204 return NULL;
11206 if (!isl_space_is_named_or_nested(bmap->dim, type))
11207 return bmap;
11209 bmap = isl_basic_map_cow(bmap);
11210 if (!bmap)
11211 return NULL;
11213 bmap->dim = isl_space_reset(bmap->dim, type);
11214 if (!bmap->dim)
11215 goto error;
11217 bmap = isl_basic_map_finalize(bmap);
11219 return bmap;
11220 error:
11221 isl_basic_map_free(bmap);
11222 return NULL;
11225 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11226 enum isl_dim_type type)
11228 int i;
11230 if (!map)
11231 return NULL;
11233 if (!isl_space_is_named_or_nested(map->dim, type))
11234 return map;
11236 map = isl_map_cow(map);
11237 if (!map)
11238 return NULL;
11240 for (i = 0; i < map->n; ++i) {
11241 map->p[i] = isl_basic_map_reset(map->p[i], type);
11242 if (!map->p[i])
11243 goto error;
11245 map->dim = isl_space_reset(map->dim, type);
11246 if (!map->dim)
11247 goto error;
11249 return map;
11250 error:
11251 isl_map_free(map);
11252 return NULL;
11255 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11257 if (!bmap)
11258 return NULL;
11260 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11261 return bmap;
11263 bmap = isl_basic_map_cow(bmap);
11264 if (!bmap)
11265 return NULL;
11267 bmap->dim = isl_space_flatten(bmap->dim);
11268 if (!bmap->dim)
11269 goto error;
11271 bmap = isl_basic_map_finalize(bmap);
11273 return bmap;
11274 error:
11275 isl_basic_map_free(bmap);
11276 return NULL;
11279 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11281 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11284 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11285 __isl_take isl_basic_map *bmap)
11287 if (!bmap)
11288 return NULL;
11290 if (!bmap->dim->nested[0])
11291 return bmap;
11293 bmap = isl_basic_map_cow(bmap);
11294 if (!bmap)
11295 return NULL;
11297 bmap->dim = isl_space_flatten_domain(bmap->dim);
11298 if (!bmap->dim)
11299 goto error;
11301 bmap = isl_basic_map_finalize(bmap);
11303 return bmap;
11304 error:
11305 isl_basic_map_free(bmap);
11306 return NULL;
11309 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11310 __isl_take isl_basic_map *bmap)
11312 if (!bmap)
11313 return NULL;
11315 if (!bmap->dim->nested[1])
11316 return bmap;
11318 bmap = isl_basic_map_cow(bmap);
11319 if (!bmap)
11320 return NULL;
11322 bmap->dim = isl_space_flatten_range(bmap->dim);
11323 if (!bmap->dim)
11324 goto error;
11326 bmap = isl_basic_map_finalize(bmap);
11328 return bmap;
11329 error:
11330 isl_basic_map_free(bmap);
11331 return NULL;
11334 /* Remove any internal structure from the spaces of domain and range of "map".
11336 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11338 if (!map)
11339 return NULL;
11341 if (!map->dim->nested[0] && !map->dim->nested[1])
11342 return map;
11344 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11347 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11349 return set_from_map(isl_map_flatten(set_to_map(set)));
11352 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11354 isl_space *dim, *flat_dim;
11355 isl_map *map;
11357 dim = isl_set_get_space(set);
11358 flat_dim = isl_space_flatten(isl_space_copy(dim));
11359 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11360 map = isl_map_intersect_domain(map, set);
11362 return map;
11365 /* Remove any internal structure from the space of the domain of "map".
11367 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11369 if (!map)
11370 return NULL;
11372 if (!map->dim->nested[0])
11373 return map;
11375 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11378 /* Remove any internal structure from the space of the range of "map".
11380 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11382 if (!map)
11383 return NULL;
11385 if (!map->dim->nested[1])
11386 return map;
11388 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11391 /* Reorder the dimensions of "bmap" according to the given dim_map
11392 * and set the dimension specification to "dim" and
11393 * perform Gaussian elimination on the result.
11395 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11396 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11398 isl_basic_map *res;
11399 unsigned flags;
11401 bmap = isl_basic_map_cow(bmap);
11402 if (!bmap || !dim || !dim_map)
11403 goto error;
11405 flags = bmap->flags;
11406 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11407 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11408 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11409 res = isl_basic_map_alloc_space(dim,
11410 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11411 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11412 if (res)
11413 res->flags = flags;
11414 res = isl_basic_map_gauss(res, NULL);
11415 res = isl_basic_map_finalize(res);
11416 return res;
11417 error:
11418 free(dim_map);
11419 isl_basic_map_free(bmap);
11420 isl_space_free(dim);
11421 return NULL;
11424 /* Reorder the dimensions of "map" according to given reordering.
11426 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11427 __isl_take isl_reordering *r)
11429 int i;
11430 struct isl_dim_map *dim_map;
11432 map = isl_map_cow(map);
11433 dim_map = isl_dim_map_from_reordering(r);
11434 if (!map || !r || !dim_map)
11435 goto error;
11437 for (i = 0; i < map->n; ++i) {
11438 struct isl_dim_map *dim_map_i;
11440 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11442 map->p[i] = isl_basic_map_realign(map->p[i],
11443 isl_space_copy(r->dim), dim_map_i);
11445 if (!map->p[i])
11446 goto error;
11449 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11451 isl_reordering_free(r);
11452 free(dim_map);
11453 return map;
11454 error:
11455 free(dim_map);
11456 isl_map_free(map);
11457 isl_reordering_free(r);
11458 return NULL;
11461 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11462 __isl_take isl_reordering *r)
11464 return set_from_map(isl_map_realign(set_to_map(set), r));
11467 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11468 __isl_take isl_space *model)
11470 isl_ctx *ctx;
11472 if (!map || !model)
11473 goto error;
11475 ctx = isl_space_get_ctx(model);
11476 if (!isl_space_has_named_params(model))
11477 isl_die(ctx, isl_error_invalid,
11478 "model has unnamed parameters", goto error);
11479 if (!isl_space_has_named_params(map->dim))
11480 isl_die(ctx, isl_error_invalid,
11481 "relation has unnamed parameters", goto error);
11482 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11483 isl_reordering *exp;
11485 model = isl_space_drop_dims(model, isl_dim_in,
11486 0, isl_space_dim(model, isl_dim_in));
11487 model = isl_space_drop_dims(model, isl_dim_out,
11488 0, isl_space_dim(model, isl_dim_out));
11489 exp = isl_parameter_alignment_reordering(map->dim, model);
11490 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11491 map = isl_map_realign(map, exp);
11494 isl_space_free(model);
11495 return map;
11496 error:
11497 isl_space_free(model);
11498 isl_map_free(map);
11499 return NULL;
11502 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11503 __isl_take isl_space *model)
11505 return isl_map_align_params(set, model);
11508 /* Align the parameters of "bmap" to those of "model", introducing
11509 * additional parameters if needed.
11511 __isl_give isl_basic_map *isl_basic_map_align_params(
11512 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11514 isl_ctx *ctx;
11516 if (!bmap || !model)
11517 goto error;
11519 ctx = isl_space_get_ctx(model);
11520 if (!isl_space_has_named_params(model))
11521 isl_die(ctx, isl_error_invalid,
11522 "model has unnamed parameters", goto error);
11523 if (!isl_space_has_named_params(bmap->dim))
11524 isl_die(ctx, isl_error_invalid,
11525 "relation has unnamed parameters", goto error);
11526 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11527 isl_reordering *exp;
11528 struct isl_dim_map *dim_map;
11530 model = isl_space_drop_dims(model, isl_dim_in,
11531 0, isl_space_dim(model, isl_dim_in));
11532 model = isl_space_drop_dims(model, isl_dim_out,
11533 0, isl_space_dim(model, isl_dim_out));
11534 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11535 exp = isl_reordering_extend_space(exp,
11536 isl_basic_map_get_space(bmap));
11537 dim_map = isl_dim_map_from_reordering(exp);
11538 bmap = isl_basic_map_realign(bmap,
11539 exp ? isl_space_copy(exp->dim) : NULL,
11540 isl_dim_map_extend(dim_map, bmap));
11541 isl_reordering_free(exp);
11542 free(dim_map);
11545 isl_space_free(model);
11546 return bmap;
11547 error:
11548 isl_space_free(model);
11549 isl_basic_map_free(bmap);
11550 return NULL;
11553 /* Align the parameters of "bset" to those of "model", introducing
11554 * additional parameters if needed.
11556 __isl_give isl_basic_set *isl_basic_set_align_params(
11557 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11559 return isl_basic_map_align_params(bset, model);
11562 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11563 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11564 enum isl_dim_type c2, enum isl_dim_type c3,
11565 enum isl_dim_type c4, enum isl_dim_type c5)
11567 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11568 struct isl_mat *mat;
11569 int i, j, k;
11570 int pos;
11572 if (!bmap)
11573 return NULL;
11574 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11575 isl_basic_map_total_dim(bmap) + 1);
11576 if (!mat)
11577 return NULL;
11578 for (i = 0; i < bmap->n_eq; ++i)
11579 for (j = 0, pos = 0; j < 5; ++j) {
11580 int off = isl_basic_map_offset(bmap, c[j]);
11581 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11582 isl_int_set(mat->row[i][pos],
11583 bmap->eq[i][off + k]);
11584 ++pos;
11588 return mat;
11591 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11592 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11593 enum isl_dim_type c2, enum isl_dim_type c3,
11594 enum isl_dim_type c4, enum isl_dim_type c5)
11596 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11597 struct isl_mat *mat;
11598 int i, j, k;
11599 int pos;
11601 if (!bmap)
11602 return NULL;
11603 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11604 isl_basic_map_total_dim(bmap) + 1);
11605 if (!mat)
11606 return NULL;
11607 for (i = 0; i < bmap->n_ineq; ++i)
11608 for (j = 0, pos = 0; j < 5; ++j) {
11609 int off = isl_basic_map_offset(bmap, c[j]);
11610 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11611 isl_int_set(mat->row[i][pos],
11612 bmap->ineq[i][off + k]);
11613 ++pos;
11617 return mat;
11620 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11621 __isl_take isl_space *dim,
11622 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11623 enum isl_dim_type c2, enum isl_dim_type c3,
11624 enum isl_dim_type c4, enum isl_dim_type c5)
11626 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11627 isl_basic_map *bmap;
11628 unsigned total;
11629 unsigned extra;
11630 int i, j, k, l;
11631 int pos;
11633 if (!dim || !eq || !ineq)
11634 goto error;
11636 if (eq->n_col != ineq->n_col)
11637 isl_die(dim->ctx, isl_error_invalid,
11638 "equalities and inequalities matrices should have "
11639 "same number of columns", goto error);
11641 total = 1 + isl_space_dim(dim, isl_dim_all);
11643 if (eq->n_col < total)
11644 isl_die(dim->ctx, isl_error_invalid,
11645 "number of columns too small", goto error);
11647 extra = eq->n_col - total;
11649 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11650 eq->n_row, ineq->n_row);
11651 if (!bmap)
11652 goto error;
11653 for (i = 0; i < extra; ++i) {
11654 k = isl_basic_map_alloc_div(bmap);
11655 if (k < 0)
11656 goto error;
11657 isl_int_set_si(bmap->div[k][0], 0);
11659 for (i = 0; i < eq->n_row; ++i) {
11660 l = isl_basic_map_alloc_equality(bmap);
11661 if (l < 0)
11662 goto error;
11663 for (j = 0, pos = 0; j < 5; ++j) {
11664 int off = isl_basic_map_offset(bmap, c[j]);
11665 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11666 isl_int_set(bmap->eq[l][off + k],
11667 eq->row[i][pos]);
11668 ++pos;
11672 for (i = 0; i < ineq->n_row; ++i) {
11673 l = isl_basic_map_alloc_inequality(bmap);
11674 if (l < 0)
11675 goto error;
11676 for (j = 0, pos = 0; j < 5; ++j) {
11677 int off = isl_basic_map_offset(bmap, c[j]);
11678 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11679 isl_int_set(bmap->ineq[l][off + k],
11680 ineq->row[i][pos]);
11681 ++pos;
11686 isl_space_free(dim);
11687 isl_mat_free(eq);
11688 isl_mat_free(ineq);
11690 bmap = isl_basic_map_simplify(bmap);
11691 return isl_basic_map_finalize(bmap);
11692 error:
11693 isl_space_free(dim);
11694 isl_mat_free(eq);
11695 isl_mat_free(ineq);
11696 return NULL;
11699 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11700 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11701 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11703 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11704 c1, c2, c3, c4, isl_dim_in);
11707 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11708 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11709 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11711 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11712 c1, c2, c3, c4, isl_dim_in);
11715 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11716 __isl_take isl_space *dim,
11717 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11718 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11720 isl_basic_map *bmap;
11721 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11722 c1, c2, c3, c4, isl_dim_in);
11723 return bset_from_bmap(bmap);
11726 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11728 if (!bmap)
11729 return isl_bool_error;
11731 return isl_space_can_zip(bmap->dim);
11734 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11736 if (!map)
11737 return isl_bool_error;
11739 return isl_space_can_zip(map->dim);
11742 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11743 * (A -> C) -> (B -> D).
11745 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11747 unsigned pos;
11748 unsigned n1;
11749 unsigned n2;
11751 if (!bmap)
11752 return NULL;
11754 if (!isl_basic_map_can_zip(bmap))
11755 isl_die(bmap->ctx, isl_error_invalid,
11756 "basic map cannot be zipped", goto error);
11757 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11758 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11759 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11760 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11761 bmap = isl_basic_map_cow(bmap);
11762 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11763 if (!bmap)
11764 return NULL;
11765 bmap->dim = isl_space_zip(bmap->dim);
11766 if (!bmap->dim)
11767 goto error;
11768 bmap = isl_basic_map_mark_final(bmap);
11769 return bmap;
11770 error:
11771 isl_basic_map_free(bmap);
11772 return NULL;
11775 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11776 * (A -> C) -> (B -> D).
11778 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11780 int i;
11782 if (!map)
11783 return NULL;
11785 if (!isl_map_can_zip(map))
11786 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11787 goto error);
11789 map = isl_map_cow(map);
11790 if (!map)
11791 return NULL;
11793 for (i = 0; i < map->n; ++i) {
11794 map->p[i] = isl_basic_map_zip(map->p[i]);
11795 if (!map->p[i])
11796 goto error;
11799 map->dim = isl_space_zip(map->dim);
11800 if (!map->dim)
11801 goto error;
11803 return map;
11804 error:
11805 isl_map_free(map);
11806 return NULL;
11809 /* Can we apply isl_basic_map_curry to "bmap"?
11810 * That is, does it have a nested relation in its domain?
11812 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11814 if (!bmap)
11815 return isl_bool_error;
11817 return isl_space_can_curry(bmap->dim);
11820 /* Can we apply isl_map_curry to "map"?
11821 * That is, does it have a nested relation in its domain?
11823 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11825 if (!map)
11826 return isl_bool_error;
11828 return isl_space_can_curry(map->dim);
11831 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11832 * A -> (B -> C).
11834 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11837 if (!bmap)
11838 return NULL;
11840 if (!isl_basic_map_can_curry(bmap))
11841 isl_die(bmap->ctx, isl_error_invalid,
11842 "basic map cannot be curried", goto error);
11843 bmap = isl_basic_map_cow(bmap);
11844 if (!bmap)
11845 return NULL;
11846 bmap->dim = isl_space_curry(bmap->dim);
11847 if (!bmap->dim)
11848 goto error;
11849 bmap = isl_basic_map_mark_final(bmap);
11850 return bmap;
11851 error:
11852 isl_basic_map_free(bmap);
11853 return NULL;
11856 /* Given a map (A -> B) -> C, return the corresponding map
11857 * A -> (B -> C).
11859 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11861 return isl_map_change_space(map, &isl_map_can_curry,
11862 "map cannot be curried", &isl_space_curry);
11865 /* Can isl_map_range_curry be applied to "map"?
11866 * That is, does it have a nested relation in its range,
11867 * the domain of which is itself a nested relation?
11869 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11871 if (!map)
11872 return isl_bool_error;
11874 return isl_space_can_range_curry(map->dim);
11877 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11878 * A -> (B -> (C -> D)).
11880 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11882 return isl_map_change_space(map, &isl_map_can_range_curry,
11883 "map range cannot be curried",
11884 &isl_space_range_curry);
11887 /* Can we apply isl_basic_map_uncurry to "bmap"?
11888 * That is, does it have a nested relation in its domain?
11890 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11892 if (!bmap)
11893 return isl_bool_error;
11895 return isl_space_can_uncurry(bmap->dim);
11898 /* Can we apply isl_map_uncurry to "map"?
11899 * That is, does it have a nested relation in its domain?
11901 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11903 if (!map)
11904 return isl_bool_error;
11906 return isl_space_can_uncurry(map->dim);
11909 /* Given a basic map A -> (B -> C), return the corresponding basic map
11910 * (A -> B) -> C.
11912 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11915 if (!bmap)
11916 return NULL;
11918 if (!isl_basic_map_can_uncurry(bmap))
11919 isl_die(bmap->ctx, isl_error_invalid,
11920 "basic map cannot be uncurried",
11921 return isl_basic_map_free(bmap));
11922 bmap = isl_basic_map_cow(bmap);
11923 if (!bmap)
11924 return NULL;
11925 bmap->dim = isl_space_uncurry(bmap->dim);
11926 if (!bmap->dim)
11927 return isl_basic_map_free(bmap);
11928 bmap = isl_basic_map_mark_final(bmap);
11929 return bmap;
11932 /* Given a map A -> (B -> C), return the corresponding map
11933 * (A -> B) -> C.
11935 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11937 return isl_map_change_space(map, &isl_map_can_uncurry,
11938 "map cannot be uncurried", &isl_space_uncurry);
11941 /* Construct a basic map mapping the domain of the affine expression
11942 * to a one-dimensional range prescribed by the affine expression.
11943 * If "rational" is set, then construct a rational basic map.
11945 * A NaN affine expression cannot be converted to a basic map.
11947 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11948 __isl_take isl_aff *aff, int rational)
11950 int k;
11951 int pos;
11952 isl_bool is_nan;
11953 isl_local_space *ls;
11954 isl_basic_map *bmap = NULL;
11956 if (!aff)
11957 return NULL;
11958 is_nan = isl_aff_is_nan(aff);
11959 if (is_nan < 0)
11960 goto error;
11961 if (is_nan)
11962 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11963 "cannot convert NaN", goto error);
11965 ls = isl_aff_get_local_space(aff);
11966 bmap = isl_basic_map_from_local_space(ls);
11967 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11968 k = isl_basic_map_alloc_equality(bmap);
11969 if (k < 0)
11970 goto error;
11972 pos = isl_basic_map_offset(bmap, isl_dim_out);
11973 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11974 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11975 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11976 aff->v->size - (pos + 1));
11978 isl_aff_free(aff);
11979 if (rational)
11980 bmap = isl_basic_map_set_rational(bmap);
11981 bmap = isl_basic_map_gauss(bmap, NULL);
11982 bmap = isl_basic_map_finalize(bmap);
11983 return bmap;
11984 error:
11985 isl_aff_free(aff);
11986 isl_basic_map_free(bmap);
11987 return NULL;
11990 /* Construct a basic map mapping the domain of the affine expression
11991 * to a one-dimensional range prescribed by the affine expression.
11993 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11995 return isl_basic_map_from_aff2(aff, 0);
11998 /* Construct a map mapping the domain of the affine expression
11999 * to a one-dimensional range prescribed by the affine expression.
12001 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12003 isl_basic_map *bmap;
12005 bmap = isl_basic_map_from_aff(aff);
12006 return isl_map_from_basic_map(bmap);
12009 /* Construct a basic map mapping the domain the multi-affine expression
12010 * to its range, with each dimension in the range equated to the
12011 * corresponding affine expression.
12012 * If "rational" is set, then construct a rational basic map.
12014 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12015 __isl_take isl_multi_aff *maff, int rational)
12017 int i;
12018 isl_space *space;
12019 isl_basic_map *bmap;
12021 if (!maff)
12022 return NULL;
12024 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12025 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12026 "invalid space", goto error);
12028 space = isl_space_domain(isl_multi_aff_get_space(maff));
12029 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12030 if (rational)
12031 bmap = isl_basic_map_set_rational(bmap);
12033 for (i = 0; i < maff->n; ++i) {
12034 isl_aff *aff;
12035 isl_basic_map *bmap_i;
12037 aff = isl_aff_copy(maff->p[i]);
12038 bmap_i = isl_basic_map_from_aff2(aff, rational);
12040 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12043 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12045 isl_multi_aff_free(maff);
12046 return bmap;
12047 error:
12048 isl_multi_aff_free(maff);
12049 return NULL;
12052 /* Construct a basic map mapping the domain the multi-affine expression
12053 * to its range, with each dimension in the range equated to the
12054 * corresponding affine expression.
12056 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12057 __isl_take isl_multi_aff *ma)
12059 return isl_basic_map_from_multi_aff2(ma, 0);
12062 /* Construct a map mapping the domain the multi-affine expression
12063 * to its range, with each dimension in the range equated to the
12064 * corresponding affine expression.
12066 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12068 isl_basic_map *bmap;
12070 bmap = isl_basic_map_from_multi_aff(maff);
12071 return isl_map_from_basic_map(bmap);
12074 /* Construct a basic map mapping a domain in the given space to
12075 * to an n-dimensional range, with n the number of elements in the list,
12076 * where each coordinate in the range is prescribed by the
12077 * corresponding affine expression.
12078 * The domains of all affine expressions in the list are assumed to match
12079 * domain_dim.
12081 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12082 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12084 int i;
12085 isl_space *dim;
12086 isl_basic_map *bmap;
12088 if (!list)
12089 return NULL;
12091 dim = isl_space_from_domain(domain_dim);
12092 bmap = isl_basic_map_universe(dim);
12094 for (i = 0; i < list->n; ++i) {
12095 isl_aff *aff;
12096 isl_basic_map *bmap_i;
12098 aff = isl_aff_copy(list->p[i]);
12099 bmap_i = isl_basic_map_from_aff(aff);
12101 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12104 isl_aff_list_free(list);
12105 return bmap;
12108 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12109 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12111 return isl_map_equate(set, type1, pos1, type2, pos2);
12114 /* Construct a basic map where the given dimensions are equal to each other.
12116 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12117 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12119 isl_basic_map *bmap = NULL;
12120 int i;
12122 if (!space)
12123 return NULL;
12125 if (pos1 >= isl_space_dim(space, type1))
12126 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12127 "index out of bounds", goto error);
12128 if (pos2 >= isl_space_dim(space, type2))
12129 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12130 "index out of bounds", goto error);
12132 if (type1 == type2 && pos1 == pos2)
12133 return isl_basic_map_universe(space);
12135 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12136 i = isl_basic_map_alloc_equality(bmap);
12137 if (i < 0)
12138 goto error;
12139 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12140 pos1 += isl_basic_map_offset(bmap, type1);
12141 pos2 += isl_basic_map_offset(bmap, type2);
12142 isl_int_set_si(bmap->eq[i][pos1], -1);
12143 isl_int_set_si(bmap->eq[i][pos2], 1);
12144 bmap = isl_basic_map_finalize(bmap);
12145 isl_space_free(space);
12146 return bmap;
12147 error:
12148 isl_space_free(space);
12149 isl_basic_map_free(bmap);
12150 return NULL;
12153 /* Add a constraint imposing that the given two dimensions are equal.
12155 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12156 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12158 isl_basic_map *eq;
12160 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12162 bmap = isl_basic_map_intersect(bmap, eq);
12164 return bmap;
12167 /* Add a constraint imposing that the given two dimensions are equal.
12169 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12170 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12172 isl_basic_map *bmap;
12174 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12176 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12178 return map;
12181 /* Add a constraint imposing that the given two dimensions have opposite values.
12183 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12184 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12186 isl_basic_map *bmap = NULL;
12187 int i;
12189 if (!map)
12190 return NULL;
12192 if (pos1 >= isl_map_dim(map, type1))
12193 isl_die(map->ctx, isl_error_invalid,
12194 "index out of bounds", goto error);
12195 if (pos2 >= isl_map_dim(map, type2))
12196 isl_die(map->ctx, isl_error_invalid,
12197 "index out of bounds", goto error);
12199 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12200 i = isl_basic_map_alloc_equality(bmap);
12201 if (i < 0)
12202 goto error;
12203 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12204 pos1 += isl_basic_map_offset(bmap, type1);
12205 pos2 += isl_basic_map_offset(bmap, type2);
12206 isl_int_set_si(bmap->eq[i][pos1], 1);
12207 isl_int_set_si(bmap->eq[i][pos2], 1);
12208 bmap = isl_basic_map_finalize(bmap);
12210 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12212 return map;
12213 error:
12214 isl_basic_map_free(bmap);
12215 isl_map_free(map);
12216 return NULL;
12219 /* Construct a constraint imposing that the value of the first dimension is
12220 * greater than or equal to that of the second.
12222 static __isl_give isl_constraint *constraint_order_ge(
12223 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12224 enum isl_dim_type type2, int pos2)
12226 isl_constraint *c;
12228 if (!space)
12229 return NULL;
12231 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12233 if (pos1 >= isl_constraint_dim(c, type1))
12234 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12235 "index out of bounds", return isl_constraint_free(c));
12236 if (pos2 >= isl_constraint_dim(c, type2))
12237 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12238 "index out of bounds", return isl_constraint_free(c));
12240 if (type1 == type2 && pos1 == pos2)
12241 return c;
12243 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12244 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12246 return c;
12249 /* Add a constraint imposing that the value of the first dimension is
12250 * greater than or equal to that of the second.
12252 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12253 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12255 isl_constraint *c;
12256 isl_space *space;
12258 if (type1 == type2 && pos1 == pos2)
12259 return bmap;
12260 space = isl_basic_map_get_space(bmap);
12261 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12262 bmap = isl_basic_map_add_constraint(bmap, c);
12264 return bmap;
12267 /* Add a constraint imposing that the value of the first dimension is
12268 * greater than or equal to that of the second.
12270 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12271 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12273 isl_constraint *c;
12274 isl_space *space;
12276 if (type1 == type2 && pos1 == pos2)
12277 return map;
12278 space = isl_map_get_space(map);
12279 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12280 map = isl_map_add_constraint(map, c);
12282 return map;
12285 /* Add a constraint imposing that the value of the first dimension is
12286 * less than or equal to that of the second.
12288 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12289 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12291 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12294 /* Construct a basic map where the value of the first dimension is
12295 * greater than that of the second.
12297 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12298 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12300 isl_basic_map *bmap = NULL;
12301 int i;
12303 if (!space)
12304 return NULL;
12306 if (pos1 >= isl_space_dim(space, type1))
12307 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12308 "index out of bounds", goto error);
12309 if (pos2 >= isl_space_dim(space, type2))
12310 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12311 "index out of bounds", goto error);
12313 if (type1 == type2 && pos1 == pos2)
12314 return isl_basic_map_empty(space);
12316 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12317 i = isl_basic_map_alloc_inequality(bmap);
12318 if (i < 0)
12319 return isl_basic_map_free(bmap);
12320 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12321 pos1 += isl_basic_map_offset(bmap, type1);
12322 pos2 += isl_basic_map_offset(bmap, type2);
12323 isl_int_set_si(bmap->ineq[i][pos1], 1);
12324 isl_int_set_si(bmap->ineq[i][pos2], -1);
12325 isl_int_set_si(bmap->ineq[i][0], -1);
12326 bmap = isl_basic_map_finalize(bmap);
12328 return bmap;
12329 error:
12330 isl_space_free(space);
12331 isl_basic_map_free(bmap);
12332 return NULL;
12335 /* Add a constraint imposing that the value of the first dimension is
12336 * greater than that of the second.
12338 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12339 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12341 isl_basic_map *gt;
12343 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12345 bmap = isl_basic_map_intersect(bmap, gt);
12347 return bmap;
12350 /* Add a constraint imposing that the value of the first dimension is
12351 * greater than that of the second.
12353 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12354 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12356 isl_basic_map *bmap;
12358 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12360 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12362 return map;
12365 /* Add a constraint imposing that the value of the first dimension is
12366 * smaller than that of the second.
12368 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12369 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12371 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12374 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12375 int pos)
12377 isl_aff *div;
12378 isl_local_space *ls;
12380 if (!bmap)
12381 return NULL;
12383 if (!isl_basic_map_divs_known(bmap))
12384 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12385 "some divs are unknown", return NULL);
12387 ls = isl_basic_map_get_local_space(bmap);
12388 div = isl_local_space_get_div(ls, pos);
12389 isl_local_space_free(ls);
12391 return div;
12394 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12395 int pos)
12397 return isl_basic_map_get_div(bset, pos);
12400 /* Plug in "subs" for dimension "type", "pos" of "bset".
12402 * Let i be the dimension to replace and let "subs" be of the form
12404 * f/d
12406 * Any integer division with a non-zero coefficient for i,
12408 * floor((a i + g)/m)
12410 * is replaced by
12412 * floor((a f + d g)/(m d))
12414 * Constraints of the form
12416 * a i + g
12418 * are replaced by
12420 * a f + d g
12422 * We currently require that "subs" is an integral expression.
12423 * Handling rational expressions may require us to add stride constraints
12424 * as we do in isl_basic_set_preimage_multi_aff.
12426 __isl_give isl_basic_set *isl_basic_set_substitute(
12427 __isl_take isl_basic_set *bset,
12428 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12430 int i;
12431 isl_int v;
12432 isl_ctx *ctx;
12434 if (bset && isl_basic_set_plain_is_empty(bset))
12435 return bset;
12437 bset = isl_basic_set_cow(bset);
12438 if (!bset || !subs)
12439 goto error;
12441 ctx = isl_basic_set_get_ctx(bset);
12442 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12443 isl_die(ctx, isl_error_invalid,
12444 "spaces don't match", goto error);
12445 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12446 isl_die(ctx, isl_error_unsupported,
12447 "cannot handle divs yet", goto error);
12448 if (!isl_int_is_one(subs->v->el[0]))
12449 isl_die(ctx, isl_error_invalid,
12450 "can only substitute integer expressions", goto error);
12452 pos += isl_basic_set_offset(bset, type);
12454 isl_int_init(v);
12456 for (i = 0; i < bset->n_eq; ++i) {
12457 if (isl_int_is_zero(bset->eq[i][pos]))
12458 continue;
12459 isl_int_set(v, bset->eq[i][pos]);
12460 isl_int_set_si(bset->eq[i][pos], 0);
12461 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12462 v, subs->v->el + 1, subs->v->size - 1);
12465 for (i = 0; i < bset->n_ineq; ++i) {
12466 if (isl_int_is_zero(bset->ineq[i][pos]))
12467 continue;
12468 isl_int_set(v, bset->ineq[i][pos]);
12469 isl_int_set_si(bset->ineq[i][pos], 0);
12470 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12471 v, subs->v->el + 1, subs->v->size - 1);
12474 for (i = 0; i < bset->n_div; ++i) {
12475 if (isl_int_is_zero(bset->div[i][1 + pos]))
12476 continue;
12477 isl_int_set(v, bset->div[i][1 + pos]);
12478 isl_int_set_si(bset->div[i][1 + pos], 0);
12479 isl_seq_combine(bset->div[i] + 1,
12480 subs->v->el[0], bset->div[i] + 1,
12481 v, subs->v->el + 1, subs->v->size - 1);
12482 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12485 isl_int_clear(v);
12487 bset = isl_basic_set_simplify(bset);
12488 return isl_basic_set_finalize(bset);
12489 error:
12490 isl_basic_set_free(bset);
12491 return NULL;
12494 /* Plug in "subs" for dimension "type", "pos" of "set".
12496 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12497 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12499 int i;
12501 if (set && isl_set_plain_is_empty(set))
12502 return set;
12504 set = isl_set_cow(set);
12505 if (!set || !subs)
12506 goto error;
12508 for (i = set->n - 1; i >= 0; --i) {
12509 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12510 if (remove_if_empty(set, i) < 0)
12511 goto error;
12514 return set;
12515 error:
12516 isl_set_free(set);
12517 return NULL;
12520 /* Check if the range of "ma" is compatible with the domain or range
12521 * (depending on "type") of "bmap".
12522 * Return -1 if anything is wrong.
12524 static int check_basic_map_compatible_range_multi_aff(
12525 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12526 __isl_keep isl_multi_aff *ma)
12528 int m;
12529 isl_space *ma_space;
12531 ma_space = isl_multi_aff_get_space(ma);
12533 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12534 if (m < 0)
12535 goto error;
12536 if (!m)
12537 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12538 "parameters don't match", goto error);
12539 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12540 if (m < 0)
12541 goto error;
12542 if (!m)
12543 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12544 "spaces don't match", goto error);
12546 isl_space_free(ma_space);
12547 return m;
12548 error:
12549 isl_space_free(ma_space);
12550 return -1;
12553 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12554 * coefficients before the transformed range of dimensions,
12555 * the "n_after" coefficients after the transformed range of dimensions
12556 * and the coefficients of the other divs in "bmap".
12558 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12559 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12561 int i;
12562 int n_param;
12563 int n_set;
12564 isl_local_space *ls;
12566 if (n_div == 0)
12567 return 0;
12569 ls = isl_aff_get_domain_local_space(ma->p[0]);
12570 if (!ls)
12571 return -1;
12573 n_param = isl_local_space_dim(ls, isl_dim_param);
12574 n_set = isl_local_space_dim(ls, isl_dim_set);
12575 for (i = 0; i < n_div; ++i) {
12576 int o_bmap = 0, o_ls = 0;
12578 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12579 o_bmap += 1 + 1 + n_param;
12580 o_ls += 1 + 1 + n_param;
12581 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12582 o_bmap += n_before;
12583 isl_seq_cpy(bmap->div[i] + o_bmap,
12584 ls->div->row[i] + o_ls, n_set);
12585 o_bmap += n_set;
12586 o_ls += n_set;
12587 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12588 o_bmap += n_after;
12589 isl_seq_cpy(bmap->div[i] + o_bmap,
12590 ls->div->row[i] + o_ls, n_div);
12591 o_bmap += n_div;
12592 o_ls += n_div;
12593 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12594 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12595 goto error;
12598 isl_local_space_free(ls);
12599 return 0;
12600 error:
12601 isl_local_space_free(ls);
12602 return -1;
12605 /* How many stride constraints does "ma" enforce?
12606 * That is, how many of the affine expressions have a denominator
12607 * different from one?
12609 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12611 int i;
12612 int strides = 0;
12614 for (i = 0; i < ma->n; ++i)
12615 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12616 strides++;
12618 return strides;
12621 /* For each affine expression in ma of the form
12623 * x_i = (f_i y + h_i)/m_i
12625 * with m_i different from one, add a constraint to "bmap"
12626 * of the form
12628 * f_i y + h_i = m_i alpha_i
12630 * with alpha_i an additional existentially quantified variable.
12632 * The input variables of "ma" correspond to a subset of the variables
12633 * of "bmap". There are "n_before" variables in "bmap" before this
12634 * subset and "n_after" variables after this subset.
12635 * The integer divisions of the affine expressions in "ma" are assumed
12636 * to have been aligned. There are "n_div_ma" of them and
12637 * they appear first in "bmap", straight after the "n_after" variables.
12639 static __isl_give isl_basic_map *add_ma_strides(
12640 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12641 int n_before, int n_after, int n_div_ma)
12643 int i, k;
12644 int div;
12645 int total;
12646 int n_param;
12647 int n_in;
12649 total = isl_basic_map_total_dim(bmap);
12650 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12651 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12652 for (i = 0; i < ma->n; ++i) {
12653 int o_bmap = 0, o_ma = 1;
12655 if (isl_int_is_one(ma->p[i]->v->el[0]))
12656 continue;
12657 div = isl_basic_map_alloc_div(bmap);
12658 k = isl_basic_map_alloc_equality(bmap);
12659 if (div < 0 || k < 0)
12660 goto error;
12661 isl_int_set_si(bmap->div[div][0], 0);
12662 isl_seq_cpy(bmap->eq[k] + o_bmap,
12663 ma->p[i]->v->el + o_ma, 1 + n_param);
12664 o_bmap += 1 + n_param;
12665 o_ma += 1 + n_param;
12666 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12667 o_bmap += n_before;
12668 isl_seq_cpy(bmap->eq[k] + o_bmap,
12669 ma->p[i]->v->el + o_ma, n_in);
12670 o_bmap += n_in;
12671 o_ma += n_in;
12672 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12673 o_bmap += n_after;
12674 isl_seq_cpy(bmap->eq[k] + o_bmap,
12675 ma->p[i]->v->el + o_ma, n_div_ma);
12676 o_bmap += n_div_ma;
12677 o_ma += n_div_ma;
12678 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12679 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12680 total++;
12683 return bmap;
12684 error:
12685 isl_basic_map_free(bmap);
12686 return NULL;
12689 /* Replace the domain or range space (depending on "type) of "space" by "set".
12691 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12692 enum isl_dim_type type, __isl_take isl_space *set)
12694 if (type == isl_dim_in) {
12695 space = isl_space_range(space);
12696 space = isl_space_map_from_domain_and_range(set, space);
12697 } else {
12698 space = isl_space_domain(space);
12699 space = isl_space_map_from_domain_and_range(space, set);
12702 return space;
12705 /* Compute the preimage of the domain or range (depending on "type")
12706 * of "bmap" under the function represented by "ma".
12707 * In other words, plug in "ma" in the domain or range of "bmap".
12708 * The result is a basic map that lives in the same space as "bmap"
12709 * except that the domain or range has been replaced by
12710 * the domain space of "ma".
12712 * If bmap is represented by
12714 * A(p) + S u + B x + T v + C(divs) >= 0,
12716 * where u and x are input and output dimensions if type == isl_dim_out
12717 * while x and v are input and output dimensions if type == isl_dim_in,
12718 * and ma is represented by
12720 * x = D(p) + F(y) + G(divs')
12722 * then the result is
12724 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12726 * The divs in the input set are similarly adjusted.
12727 * In particular
12729 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12731 * becomes
12733 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12734 * B_i G(divs') + c_i(divs))/n_i)
12736 * If bmap is not a rational map and if F(y) involves any denominators
12738 * x_i = (f_i y + h_i)/m_i
12740 * then additional constraints are added to ensure that we only
12741 * map back integer points. That is we enforce
12743 * f_i y + h_i = m_i alpha_i
12745 * with alpha_i an additional existentially quantified variable.
12747 * We first copy over the divs from "ma".
12748 * Then we add the modified constraints and divs from "bmap".
12749 * Finally, we add the stride constraints, if needed.
12751 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12752 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12753 __isl_take isl_multi_aff *ma)
12755 int i, k;
12756 isl_space *space;
12757 isl_basic_map *res = NULL;
12758 int n_before, n_after, n_div_bmap, n_div_ma;
12759 isl_int f, c1, c2, g;
12760 int rational, strides;
12762 isl_int_init(f);
12763 isl_int_init(c1);
12764 isl_int_init(c2);
12765 isl_int_init(g);
12767 ma = isl_multi_aff_align_divs(ma);
12768 if (!bmap || !ma)
12769 goto error;
12770 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12771 goto error;
12773 if (type == isl_dim_in) {
12774 n_before = 0;
12775 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12776 } else {
12777 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12778 n_after = 0;
12780 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12781 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12783 space = isl_multi_aff_get_domain_space(ma);
12784 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12785 rational = isl_basic_map_is_rational(bmap);
12786 strides = rational ? 0 : multi_aff_strides(ma);
12787 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12788 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12789 if (rational)
12790 res = isl_basic_map_set_rational(res);
12792 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12793 if (isl_basic_map_alloc_div(res) < 0)
12794 goto error;
12796 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12797 goto error;
12799 for (i = 0; i < bmap->n_eq; ++i) {
12800 k = isl_basic_map_alloc_equality(res);
12801 if (k < 0)
12802 goto error;
12803 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12804 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12807 for (i = 0; i < bmap->n_ineq; ++i) {
12808 k = isl_basic_map_alloc_inequality(res);
12809 if (k < 0)
12810 goto error;
12811 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12812 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12815 for (i = 0; i < bmap->n_div; ++i) {
12816 if (isl_int_is_zero(bmap->div[i][0])) {
12817 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12818 continue;
12820 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12821 n_before, n_after, n_div_ma, n_div_bmap,
12822 f, c1, c2, g, 1);
12825 if (strides)
12826 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12828 isl_int_clear(f);
12829 isl_int_clear(c1);
12830 isl_int_clear(c2);
12831 isl_int_clear(g);
12832 isl_basic_map_free(bmap);
12833 isl_multi_aff_free(ma);
12834 res = isl_basic_map_simplify(res);
12835 return isl_basic_map_finalize(res);
12836 error:
12837 isl_int_clear(f);
12838 isl_int_clear(c1);
12839 isl_int_clear(c2);
12840 isl_int_clear(g);
12841 isl_basic_map_free(bmap);
12842 isl_multi_aff_free(ma);
12843 isl_basic_map_free(res);
12844 return NULL;
12847 /* Compute the preimage of "bset" under the function represented by "ma".
12848 * In other words, plug in "ma" in "bset". The result is a basic set
12849 * that lives in the domain space of "ma".
12851 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12852 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12854 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12857 /* Compute the preimage of the domain of "bmap" under the function
12858 * represented by "ma".
12859 * In other words, plug in "ma" in the domain of "bmap".
12860 * The result is a basic map that lives in the same space as "bmap"
12861 * except that the domain has been replaced by the domain space of "ma".
12863 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12864 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12866 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12869 /* Compute the preimage of the range of "bmap" under the function
12870 * represented by "ma".
12871 * In other words, plug in "ma" in the range of "bmap".
12872 * The result is a basic map that lives in the same space as "bmap"
12873 * except that the range has been replaced by the domain space of "ma".
12875 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12876 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12878 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12881 /* Check if the range of "ma" is compatible with the domain or range
12882 * (depending on "type") of "map".
12883 * Return -1 if anything is wrong.
12885 static int check_map_compatible_range_multi_aff(
12886 __isl_keep isl_map *map, enum isl_dim_type type,
12887 __isl_keep isl_multi_aff *ma)
12889 int m;
12890 isl_space *ma_space;
12892 ma_space = isl_multi_aff_get_space(ma);
12893 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12894 isl_space_free(ma_space);
12895 if (m >= 0 && !m)
12896 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12897 "spaces don't match", return -1);
12898 return m;
12901 /* Compute the preimage of the domain or range (depending on "type")
12902 * of "map" under the function represented by "ma".
12903 * In other words, plug in "ma" in the domain or range of "map".
12904 * The result is a map that lives in the same space as "map"
12905 * except that the domain or range has been replaced by
12906 * the domain space of "ma".
12908 * The parameters are assumed to have been aligned.
12910 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12911 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12913 int i;
12914 isl_space *space;
12916 map = isl_map_cow(map);
12917 ma = isl_multi_aff_align_divs(ma);
12918 if (!map || !ma)
12919 goto error;
12920 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12921 goto error;
12923 for (i = 0; i < map->n; ++i) {
12924 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12925 isl_multi_aff_copy(ma));
12926 if (!map->p[i])
12927 goto error;
12930 space = isl_multi_aff_get_domain_space(ma);
12931 space = isl_space_set(isl_map_get_space(map), type, space);
12933 isl_space_free(map->dim);
12934 map->dim = space;
12935 if (!map->dim)
12936 goto error;
12938 isl_multi_aff_free(ma);
12939 if (map->n > 1)
12940 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12941 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12942 return map;
12943 error:
12944 isl_multi_aff_free(ma);
12945 isl_map_free(map);
12946 return NULL;
12949 /* Compute the preimage of the domain or range (depending on "type")
12950 * of "map" under the function represented by "ma".
12951 * In other words, plug in "ma" in the domain or range of "map".
12952 * The result is a map that lives in the same space as "map"
12953 * except that the domain or range has been replaced by
12954 * the domain space of "ma".
12956 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12957 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12959 if (!map || !ma)
12960 goto error;
12962 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12963 return map_preimage_multi_aff(map, type, ma);
12965 if (!isl_space_has_named_params(map->dim) ||
12966 !isl_space_has_named_params(ma->space))
12967 isl_die(map->ctx, isl_error_invalid,
12968 "unaligned unnamed parameters", goto error);
12969 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12970 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12972 return map_preimage_multi_aff(map, type, ma);
12973 error:
12974 isl_multi_aff_free(ma);
12975 return isl_map_free(map);
12978 /* Compute the preimage of "set" under the function represented by "ma".
12979 * In other words, plug in "ma" in "set". The result is a set
12980 * that lives in the domain space of "ma".
12982 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12983 __isl_take isl_multi_aff *ma)
12985 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12988 /* Compute the preimage of the domain of "map" under the function
12989 * represented by "ma".
12990 * In other words, plug in "ma" in the domain of "map".
12991 * The result is a map that lives in the same space as "map"
12992 * except that the domain has been replaced by the domain space of "ma".
12994 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12995 __isl_take isl_multi_aff *ma)
12997 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13000 /* Compute the preimage of the range of "map" under the function
13001 * represented by "ma".
13002 * In other words, plug in "ma" in the range of "map".
13003 * The result is a map that lives in the same space as "map"
13004 * except that the range has been replaced by the domain space of "ma".
13006 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13007 __isl_take isl_multi_aff *ma)
13009 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13012 /* Compute the preimage of "map" under the function represented by "pma".
13013 * In other words, plug in "pma" in the domain or range of "map".
13014 * The result is a map that lives in the same space as "map",
13015 * except that the space of type "type" has been replaced by
13016 * the domain space of "pma".
13018 * The parameters of "map" and "pma" are assumed to have been aligned.
13020 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13021 __isl_take isl_map *map, enum isl_dim_type type,
13022 __isl_take isl_pw_multi_aff *pma)
13024 int i;
13025 isl_map *res;
13027 if (!pma)
13028 goto error;
13030 if (pma->n == 0) {
13031 isl_pw_multi_aff_free(pma);
13032 res = isl_map_empty(isl_map_get_space(map));
13033 isl_map_free(map);
13034 return res;
13037 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13038 isl_multi_aff_copy(pma->p[0].maff));
13039 if (type == isl_dim_in)
13040 res = isl_map_intersect_domain(res,
13041 isl_map_copy(pma->p[0].set));
13042 else
13043 res = isl_map_intersect_range(res,
13044 isl_map_copy(pma->p[0].set));
13046 for (i = 1; i < pma->n; ++i) {
13047 isl_map *res_i;
13049 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13050 isl_multi_aff_copy(pma->p[i].maff));
13051 if (type == isl_dim_in)
13052 res_i = isl_map_intersect_domain(res_i,
13053 isl_map_copy(pma->p[i].set));
13054 else
13055 res_i = isl_map_intersect_range(res_i,
13056 isl_map_copy(pma->p[i].set));
13057 res = isl_map_union(res, res_i);
13060 isl_pw_multi_aff_free(pma);
13061 isl_map_free(map);
13062 return res;
13063 error:
13064 isl_pw_multi_aff_free(pma);
13065 isl_map_free(map);
13066 return NULL;
13069 /* Compute the preimage of "map" under the function represented by "pma".
13070 * In other words, plug in "pma" in the domain or range of "map".
13071 * The result is a map that lives in the same space as "map",
13072 * except that the space of type "type" has been replaced by
13073 * the domain space of "pma".
13075 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13076 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13078 if (!map || !pma)
13079 goto error;
13081 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13082 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13084 if (!isl_space_has_named_params(map->dim) ||
13085 !isl_space_has_named_params(pma->dim))
13086 isl_die(map->ctx, isl_error_invalid,
13087 "unaligned unnamed parameters", goto error);
13088 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13089 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13091 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13092 error:
13093 isl_pw_multi_aff_free(pma);
13094 return isl_map_free(map);
13097 /* Compute the preimage of "set" under the function represented by "pma".
13098 * In other words, plug in "pma" in "set". The result is a set
13099 * that lives in the domain space of "pma".
13101 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13102 __isl_take isl_pw_multi_aff *pma)
13104 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13107 /* Compute the preimage of the domain of "map" under the function
13108 * represented by "pma".
13109 * In other words, plug in "pma" in the domain of "map".
13110 * The result is a map that lives in the same space as "map",
13111 * except that domain space has been replaced by the domain space of "pma".
13113 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13114 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13116 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13119 /* Compute the preimage of the range of "map" under the function
13120 * represented by "pma".
13121 * In other words, plug in "pma" in the range of "map".
13122 * The result is a map that lives in the same space as "map",
13123 * except that range space has been replaced by the domain space of "pma".
13125 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13126 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13128 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13131 /* Compute the preimage of "map" under the function represented by "mpa".
13132 * In other words, plug in "mpa" in the domain or range of "map".
13133 * The result is a map that lives in the same space as "map",
13134 * except that the space of type "type" has been replaced by
13135 * the domain space of "mpa".
13137 * If the map does not involve any constraints that refer to the
13138 * dimensions of the substituted space, then the only possible
13139 * effect of "mpa" on the map is to map the space to a different space.
13140 * We create a separate isl_multi_aff to effectuate this change
13141 * in order to avoid spurious splitting of the map along the pieces
13142 * of "mpa".
13144 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13145 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13147 int n;
13148 isl_pw_multi_aff *pma;
13150 if (!map || !mpa)
13151 goto error;
13153 n = isl_map_dim(map, type);
13154 if (!isl_map_involves_dims(map, type, 0, n)) {
13155 isl_space *space;
13156 isl_multi_aff *ma;
13158 space = isl_multi_pw_aff_get_space(mpa);
13159 isl_multi_pw_aff_free(mpa);
13160 ma = isl_multi_aff_zero(space);
13161 return isl_map_preimage_multi_aff(map, type, ma);
13164 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13165 return isl_map_preimage_pw_multi_aff(map, type, pma);
13166 error:
13167 isl_map_free(map);
13168 isl_multi_pw_aff_free(mpa);
13169 return NULL;
13172 /* Compute the preimage of "map" under the function represented by "mpa".
13173 * In other words, plug in "mpa" in the domain "map".
13174 * The result is a map that lives in the same space as "map",
13175 * except that domain space has been replaced by the domain space of "mpa".
13177 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13178 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13180 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13183 /* Compute the preimage of "set" by the function represented by "mpa".
13184 * In other words, plug in "mpa" in "set".
13186 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13187 __isl_take isl_multi_pw_aff *mpa)
13189 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13192 /* Is the point "inner" internal to inequality constraint "ineq"
13193 * of "bset"?
13194 * The point is considered to be internal to the inequality constraint,
13195 * if it strictly lies on the positive side of the inequality constraint,
13196 * or if it lies on the constraint and the constraint is lexico-positive.
13198 static isl_bool is_internal(__isl_keep isl_vec *inner,
13199 __isl_keep isl_basic_set *bset, int ineq)
13201 isl_ctx *ctx;
13202 int pos;
13203 unsigned total;
13205 if (!inner || !bset)
13206 return isl_bool_error;
13208 ctx = isl_basic_set_get_ctx(bset);
13209 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13210 &ctx->normalize_gcd);
13211 if (!isl_int_is_zero(ctx->normalize_gcd))
13212 return isl_int_is_nonneg(ctx->normalize_gcd);
13214 total = isl_basic_set_dim(bset, isl_dim_all);
13215 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13216 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13219 /* Tighten the inequality constraints of "bset" that are outward with respect
13220 * to the point "vec".
13221 * That is, tighten the constraints that are not satisfied by "vec".
13223 * "vec" is a point internal to some superset S of "bset" that is used
13224 * to make the subsets of S disjoint, by tightening one half of the constraints
13225 * that separate two subsets. In particular, the constraints of S
13226 * are all satisfied by "vec" and should not be tightened.
13227 * Of the internal constraints, those that have "vec" on the outside
13228 * are tightened. The shared facet is included in the adjacent subset
13229 * with the opposite constraint.
13230 * For constraints that saturate "vec", this criterion cannot be used
13231 * to determine which of the two sides should be tightened.
13232 * Instead, the sign of the first non-zero coefficient is used
13233 * to make this choice. Note that this second criterion is never used
13234 * on the constraints of S since "vec" is interior to "S".
13236 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13237 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13239 int j;
13241 bset = isl_basic_set_cow(bset);
13242 if (!bset)
13243 return NULL;
13244 for (j = 0; j < bset->n_ineq; ++j) {
13245 isl_bool internal;
13247 internal = is_internal(vec, bset, j);
13248 if (internal < 0)
13249 return isl_basic_set_free(bset);
13250 if (internal)
13251 continue;
13252 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13255 return bset;