isl_map_coalesce: add a note about a possible improvement wrt extensions
[isl.git] / isl_map.c
blob5d22aa2d5f9960ab7f8947328c3a1608586c124e
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl/map.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
35 #include <isl_sort.h>
36 #include "isl_tab.h"
37 #include <isl/vec.h>
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
65 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
91 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
93 return map ? n(map->dim, type) : 0;
96 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
98 return set ? n(set->dim, type) : 0;
101 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
102 enum isl_dim_type type)
104 isl_space *space;
106 if (!bmap)
107 return 0;
109 space = bmap->dim;
110 switch (type) {
111 case isl_dim_cst: return 0;
112 case isl_dim_param: return 1;
113 case isl_dim_in: return 1 + space->nparam;
114 case isl_dim_out: return 1 + space->nparam + space->n_in;
115 case isl_dim_div: return 1 + space->nparam + space->n_in +
116 space->n_out;
117 default: return 0;
121 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
122 enum isl_dim_type type)
124 return isl_basic_map_offset(bset, type);
127 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
129 return pos(map->dim, type);
132 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
133 enum isl_dim_type type)
135 return isl_basic_map_dim(bset, type);
138 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
140 return isl_basic_set_dim(bset, isl_dim_set);
143 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
145 return isl_basic_set_dim(bset, isl_dim_param);
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
150 if (!bset)
151 return 0;
152 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
155 unsigned isl_set_n_dim(__isl_keep isl_set *set)
157 return isl_set_dim(set, isl_dim_set);
160 unsigned isl_set_n_param(__isl_keep isl_set *set)
162 return isl_set_dim(set, isl_dim_param);
165 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
167 return bmap ? bmap->dim->n_in : 0;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
172 return bmap ? bmap->dim->n_out : 0;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
177 return bmap ? bmap->dim->nparam : 0;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
182 return bmap ? bmap->n_div : 0;
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
187 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
190 unsigned isl_map_n_in(const struct isl_map *map)
192 return map ? map->dim->n_in : 0;
195 unsigned isl_map_n_out(const struct isl_map *map)
197 return map ? map->dim->n_out : 0;
200 unsigned isl_map_n_param(const struct isl_map *map)
202 return map ? map->dim->nparam : 0;
205 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
207 int m;
208 if (!map || !set)
209 return -1;
210 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
211 if (m < 0 || !m)
212 return m;
213 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
214 set->dim, isl_dim_set);
217 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
218 __isl_keep isl_basic_set *bset)
220 isl_bool m;
221 if (!bmap || !bset)
222 return isl_bool_error;
223 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
224 if (m < 0 || !m)
225 return m;
226 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
227 bset->dim, isl_dim_set);
230 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
232 int m;
233 if (!map || !set)
234 return -1;
235 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
236 if (m < 0 || !m)
237 return m;
238 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
239 set->dim, isl_dim_set);
242 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
243 struct isl_basic_set *bset)
245 int m;
246 if (!bmap || !bset)
247 return -1;
248 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
249 if (m < 0 || !m)
250 return m;
251 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
252 bset->dim, isl_dim_set);
255 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
257 return bmap ? bmap->ctx : NULL;
260 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
262 return bset ? bset->ctx : NULL;
265 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
267 return map ? map->ctx : NULL;
270 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
272 return set ? set->ctx : NULL;
275 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
277 if (!bmap)
278 return NULL;
279 return isl_space_copy(bmap->dim);
282 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
284 if (!bset)
285 return NULL;
286 return isl_space_copy(bset->dim);
289 /* Extract the divs in "bmap" as a matrix.
291 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
293 int i;
294 isl_ctx *ctx;
295 isl_mat *div;
296 unsigned total;
297 unsigned cols;
299 if (!bmap)
300 return NULL;
302 ctx = isl_basic_map_get_ctx(bmap);
303 total = isl_space_dim(bmap->dim, isl_dim_all);
304 cols = 1 + 1 + total + bmap->n_div;
305 div = isl_mat_alloc(ctx, bmap->n_div, cols);
306 if (!div)
307 return NULL;
309 for (i = 0; i < bmap->n_div; ++i)
310 isl_seq_cpy(div->row[i], bmap->div[i], cols);
312 return div;
315 /* Extract the divs in "bset" as a matrix.
317 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
319 return isl_basic_map_get_divs(bset);
322 __isl_give isl_local_space *isl_basic_map_get_local_space(
323 __isl_keep isl_basic_map *bmap)
325 isl_mat *div;
327 if (!bmap)
328 return NULL;
330 div = isl_basic_map_get_divs(bmap);
331 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
334 __isl_give isl_local_space *isl_basic_set_get_local_space(
335 __isl_keep isl_basic_set *bset)
337 return isl_basic_map_get_local_space(bset);
340 /* For each known div d = floor(f/m), add the constraints
342 * f - m d >= 0
343 * -(f-(m-1)) + m d >= 0
345 * Do not finalize the result.
347 static __isl_give isl_basic_map *add_known_div_constraints(
348 __isl_take isl_basic_map *bmap)
350 int i;
351 unsigned n_div;
353 if (!bmap)
354 return NULL;
355 n_div = isl_basic_map_dim(bmap, isl_dim_div);
356 if (n_div == 0)
357 return bmap;
358 bmap = isl_basic_map_cow(bmap);
359 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
360 if (!bmap)
361 return NULL;
362 for (i = 0; i < n_div; ++i) {
363 if (isl_int_is_zero(bmap->div[i][0]))
364 continue;
365 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
366 return isl_basic_map_free(bmap);
369 return bmap;
372 __isl_give isl_basic_map *isl_basic_map_from_local_space(
373 __isl_take isl_local_space *ls)
375 int i;
376 int n_div;
377 isl_basic_map *bmap;
379 if (!ls)
380 return NULL;
382 n_div = isl_local_space_dim(ls, isl_dim_div);
383 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
384 n_div, 0, 2 * n_div);
386 for (i = 0; i < n_div; ++i)
387 if (isl_basic_map_alloc_div(bmap) < 0)
388 goto error;
390 for (i = 0; i < n_div; ++i)
391 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
392 bmap = add_known_div_constraints(bmap);
394 isl_local_space_free(ls);
395 return bmap;
396 error:
397 isl_local_space_free(ls);
398 isl_basic_map_free(bmap);
399 return NULL;
402 __isl_give isl_basic_set *isl_basic_set_from_local_space(
403 __isl_take isl_local_space *ls)
405 return isl_basic_map_from_local_space(ls);
408 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
410 if (!map)
411 return NULL;
412 return isl_space_copy(map->dim);
415 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
417 if (!set)
418 return NULL;
419 return isl_space_copy(set->dim);
422 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
423 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
425 bmap = isl_basic_map_cow(bmap);
426 if (!bmap)
427 return NULL;
428 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
429 if (!bmap->dim)
430 goto error;
431 bmap = isl_basic_map_finalize(bmap);
432 return bmap;
433 error:
434 isl_basic_map_free(bmap);
435 return NULL;
438 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
439 __isl_take isl_basic_set *bset, const char *s)
441 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
444 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
445 enum isl_dim_type type)
447 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
450 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
451 enum isl_dim_type type, const char *s)
453 int i;
455 map = isl_map_cow(map);
456 if (!map)
457 return NULL;
459 map->dim = isl_space_set_tuple_name(map->dim, type, s);
460 if (!map->dim)
461 goto error;
463 for (i = 0; i < map->n; ++i) {
464 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
465 if (!map->p[i])
466 goto error;
469 return map;
470 error:
471 isl_map_free(map);
472 return NULL;
475 /* Replace the identifier of the tuple of type "type" by "id".
477 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
478 __isl_take isl_basic_map *bmap,
479 enum isl_dim_type type, __isl_take isl_id *id)
481 bmap = isl_basic_map_cow(bmap);
482 if (!bmap)
483 goto error;
484 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
485 if (!bmap->dim)
486 return isl_basic_map_free(bmap);
487 bmap = isl_basic_map_finalize(bmap);
488 return bmap;
489 error:
490 isl_id_free(id);
491 return NULL;
494 /* Replace the identifier of the tuple by "id".
496 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
497 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
499 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
502 /* Does the input or output tuple have a name?
504 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
506 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
509 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
510 enum isl_dim_type type)
512 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
515 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
516 const char *s)
518 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
519 isl_dim_set, s));
522 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
523 enum isl_dim_type type, __isl_take isl_id *id)
525 map = isl_map_cow(map);
526 if (!map)
527 goto error;
529 map->dim = isl_space_set_tuple_id(map->dim, type, id);
531 return isl_map_reset_space(map, isl_space_copy(map->dim));
532 error:
533 isl_id_free(id);
534 return NULL;
537 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
538 __isl_take isl_id *id)
540 return isl_map_set_tuple_id(set, isl_dim_set, id);
543 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
544 enum isl_dim_type type)
546 map = isl_map_cow(map);
547 if (!map)
548 return NULL;
550 map->dim = isl_space_reset_tuple_id(map->dim, type);
552 return isl_map_reset_space(map, isl_space_copy(map->dim));
555 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
557 return isl_map_reset_tuple_id(set, isl_dim_set);
560 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
562 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
565 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
566 enum isl_dim_type type)
568 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
571 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
573 return isl_map_has_tuple_id(set, isl_dim_set);
576 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
578 return isl_map_get_tuple_id(set, isl_dim_set);
581 /* Does the set tuple have a name?
583 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
585 if (!set)
586 return isl_bool_error;
587 return isl_space_has_tuple_name(set->dim, isl_dim_set);
591 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
593 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
596 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
598 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
601 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
602 enum isl_dim_type type, unsigned pos)
604 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
607 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
608 enum isl_dim_type type, unsigned pos)
610 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
613 /* Does the given dimension have a name?
615 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
616 enum isl_dim_type type, unsigned pos)
618 if (!map)
619 return isl_bool_error;
620 return isl_space_has_dim_name(map->dim, type, pos);
623 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
624 enum isl_dim_type type, unsigned pos)
626 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
629 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
630 enum isl_dim_type type, unsigned pos)
632 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
635 /* Does the given dimension have a name?
637 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
638 enum isl_dim_type type, unsigned pos)
640 if (!set)
641 return isl_bool_error;
642 return isl_space_has_dim_name(set->dim, type, pos);
645 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
646 __isl_take isl_basic_map *bmap,
647 enum isl_dim_type type, unsigned pos, const char *s)
649 bmap = isl_basic_map_cow(bmap);
650 if (!bmap)
651 return NULL;
652 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
653 if (!bmap->dim)
654 goto error;
655 return isl_basic_map_finalize(bmap);
656 error:
657 isl_basic_map_free(bmap);
658 return NULL;
661 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
662 enum isl_dim_type type, unsigned pos, const char *s)
664 int i;
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
670 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
671 if (!map->dim)
672 goto error;
674 for (i = 0; i < map->n; ++i) {
675 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
676 if (!map->p[i])
677 goto error;
680 return map;
681 error:
682 isl_map_free(map);
683 return NULL;
686 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
687 __isl_take isl_basic_set *bset,
688 enum isl_dim_type type, unsigned pos, const char *s)
690 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
691 type, pos, s));
694 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
695 enum isl_dim_type type, unsigned pos, const char *s)
697 return set_from_map(isl_map_set_dim_name(set_to_map(set),
698 type, pos, s));
701 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
702 enum isl_dim_type type, unsigned pos)
704 if (!bmap)
705 return isl_bool_error;
706 return isl_space_has_dim_id(bmap->dim, type, pos);
709 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
710 enum isl_dim_type type, unsigned pos)
712 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
715 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
716 enum isl_dim_type type, unsigned pos)
718 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
721 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
722 enum isl_dim_type type, unsigned pos)
724 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
727 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
728 enum isl_dim_type type, unsigned pos)
730 return isl_map_has_dim_id(set, type, pos);
733 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
734 enum isl_dim_type type, unsigned pos)
736 return isl_map_get_dim_id(set, type, pos);
739 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
740 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
742 map = isl_map_cow(map);
743 if (!map)
744 goto error;
746 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
748 return isl_map_reset_space(map, isl_space_copy(map->dim));
749 error:
750 isl_id_free(id);
751 return NULL;
754 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
755 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
757 return isl_map_set_dim_id(set, type, pos, id);
760 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
761 __isl_keep isl_id *id)
763 if (!map)
764 return -1;
765 return isl_space_find_dim_by_id(map->dim, type, id);
768 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
769 __isl_keep isl_id *id)
771 return isl_map_find_dim_by_id(set, type, id);
774 /* Return the position of the dimension of the given type and name
775 * in "bmap".
776 * Return -1 if no such dimension can be found.
778 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
779 enum isl_dim_type type, const char *name)
781 if (!bmap)
782 return -1;
783 return isl_space_find_dim_by_name(bmap->dim, type, name);
786 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
787 const char *name)
789 if (!map)
790 return -1;
791 return isl_space_find_dim_by_name(map->dim, type, name);
794 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
795 const char *name)
797 return isl_map_find_dim_by_name(set, type, name);
800 /* Reset the user pointer on all identifiers of parameters and tuples
801 * of the space of "map".
803 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
805 isl_space *space;
807 space = isl_map_get_space(map);
808 space = isl_space_reset_user(space);
809 map = isl_map_reset_space(map, space);
811 return map;
814 /* Reset the user pointer on all identifiers of parameters and tuples
815 * of the space of "set".
817 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
819 return isl_map_reset_user(set);
822 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
824 if (!bmap)
825 return -1;
826 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
829 /* Has "map" been marked as a rational map?
830 * In particular, have all basic maps in "map" been marked this way?
831 * An empty map is not considered to be rational.
832 * Maps where only some of the basic maps are marked rational
833 * are not allowed.
835 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
837 int i;
838 isl_bool rational;
840 if (!map)
841 return isl_bool_error;
842 if (map->n == 0)
843 return isl_bool_false;
844 rational = isl_basic_map_is_rational(map->p[0]);
845 if (rational < 0)
846 return rational;
847 for (i = 1; i < map->n; ++i) {
848 isl_bool rational_i;
850 rational_i = isl_basic_map_is_rational(map->p[i]);
851 if (rational_i < 0)
852 return rational;
853 if (rational != rational_i)
854 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
855 "mixed rational and integer basic maps "
856 "not supported", return isl_bool_error);
859 return rational;
862 /* Has "set" been marked as a rational set?
863 * In particular, have all basic set in "set" been marked this way?
864 * An empty set is not considered to be rational.
865 * Sets where only some of the basic sets are marked rational
866 * are not allowed.
868 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
870 return isl_map_is_rational(set);
873 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
875 return isl_basic_map_is_rational(bset);
878 /* Does "bmap" contain any rational points?
880 * If "bmap" has an equality for each dimension, equating the dimension
881 * to an integer constant, then it has no rational points, even if it
882 * is marked as rational.
884 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
886 int has_rational = 1;
887 unsigned total;
889 if (!bmap)
890 return -1;
891 if (isl_basic_map_plain_is_empty(bmap))
892 return 0;
893 if (!isl_basic_map_is_rational(bmap))
894 return 0;
895 bmap = isl_basic_map_copy(bmap);
896 bmap = isl_basic_map_implicit_equalities(bmap);
897 if (!bmap)
898 return -1;
899 total = isl_basic_map_total_dim(bmap);
900 if (bmap->n_eq == total) {
901 int i, j;
902 for (i = 0; i < bmap->n_eq; ++i) {
903 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
904 if (j < 0)
905 break;
906 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
907 !isl_int_is_negone(bmap->eq[i][1 + j]))
908 break;
909 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
910 total - j - 1);
911 if (j >= 0)
912 break;
914 if (i == bmap->n_eq)
915 has_rational = 0;
917 isl_basic_map_free(bmap);
919 return has_rational;
922 /* Does "map" contain any rational points?
924 int isl_map_has_rational(__isl_keep isl_map *map)
926 int i;
927 int has_rational;
929 if (!map)
930 return -1;
931 for (i = 0; i < map->n; ++i) {
932 has_rational = isl_basic_map_has_rational(map->p[i]);
933 if (has_rational < 0)
934 return -1;
935 if (has_rational)
936 return 1;
938 return 0;
941 /* Does "set" contain any rational points?
943 int isl_set_has_rational(__isl_keep isl_set *set)
945 return isl_map_has_rational(set);
948 /* Is this basic set a parameter domain?
950 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
952 if (!bset)
953 return -1;
954 return isl_space_is_params(bset->dim);
957 /* Is this set a parameter domain?
959 isl_bool isl_set_is_params(__isl_keep isl_set *set)
961 if (!set)
962 return isl_bool_error;
963 return isl_space_is_params(set->dim);
966 /* Is this map actually a parameter domain?
967 * Users should never call this function. Outside of isl,
968 * a map can never be a parameter domain.
970 int isl_map_is_params(__isl_keep isl_map *map)
972 if (!map)
973 return -1;
974 return isl_space_is_params(map->dim);
977 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
978 struct isl_basic_map *bmap, unsigned extra,
979 unsigned n_eq, unsigned n_ineq)
981 int i;
982 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
984 bmap->ctx = ctx;
985 isl_ctx_ref(ctx);
987 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
988 if (isl_blk_is_error(bmap->block))
989 goto error;
991 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
992 if ((n_ineq + n_eq) && !bmap->ineq)
993 goto error;
995 if (extra == 0) {
996 bmap->block2 = isl_blk_empty();
997 bmap->div = NULL;
998 } else {
999 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1000 if (isl_blk_is_error(bmap->block2))
1001 goto error;
1003 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1004 if (!bmap->div)
1005 goto error;
1008 for (i = 0; i < n_ineq + n_eq; ++i)
1009 bmap->ineq[i] = bmap->block.data + i * row_size;
1011 for (i = 0; i < extra; ++i)
1012 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1014 bmap->ref = 1;
1015 bmap->flags = 0;
1016 bmap->c_size = n_eq + n_ineq;
1017 bmap->eq = bmap->ineq + n_ineq;
1018 bmap->extra = extra;
1019 bmap->n_eq = 0;
1020 bmap->n_ineq = 0;
1021 bmap->n_div = 0;
1022 bmap->sample = NULL;
1024 return bmap;
1025 error:
1026 isl_basic_map_free(bmap);
1027 return NULL;
1030 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1031 unsigned nparam, unsigned dim, unsigned extra,
1032 unsigned n_eq, unsigned n_ineq)
1034 struct isl_basic_map *bmap;
1035 isl_space *space;
1037 space = isl_space_set_alloc(ctx, nparam, dim);
1038 if (!space)
1039 return NULL;
1041 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1042 return bset_from_bmap(bmap);
1045 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1046 unsigned extra, unsigned n_eq, unsigned n_ineq)
1048 struct isl_basic_map *bmap;
1049 if (!dim)
1050 return NULL;
1051 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1052 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1053 return bset_from_bmap(bmap);
1054 error:
1055 isl_space_free(dim);
1056 return NULL;
1059 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1060 unsigned extra, unsigned n_eq, unsigned n_ineq)
1062 struct isl_basic_map *bmap;
1064 if (!dim)
1065 return NULL;
1066 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1067 if (!bmap)
1068 goto error;
1069 bmap->dim = dim;
1071 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1072 error:
1073 isl_space_free(dim);
1074 return NULL;
1077 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1078 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1079 unsigned n_eq, unsigned n_ineq)
1081 struct isl_basic_map *bmap;
1082 isl_space *dim;
1084 dim = isl_space_alloc(ctx, nparam, in, out);
1085 if (!dim)
1086 return NULL;
1088 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1089 return bmap;
1092 static void dup_constraints(
1093 struct isl_basic_map *dst, struct isl_basic_map *src)
1095 int i;
1096 unsigned total = isl_basic_map_total_dim(src);
1098 for (i = 0; i < src->n_eq; ++i) {
1099 int j = isl_basic_map_alloc_equality(dst);
1100 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1103 for (i = 0; i < src->n_ineq; ++i) {
1104 int j = isl_basic_map_alloc_inequality(dst);
1105 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1108 for (i = 0; i < src->n_div; ++i) {
1109 int j = isl_basic_map_alloc_div(dst);
1110 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1112 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1115 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1117 struct isl_basic_map *dup;
1119 if (!bmap)
1120 return NULL;
1121 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1122 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1123 if (!dup)
1124 return NULL;
1125 dup_constraints(dup, bmap);
1126 dup->flags = bmap->flags;
1127 dup->sample = isl_vec_copy(bmap->sample);
1128 return dup;
1131 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1133 struct isl_basic_map *dup;
1135 dup = isl_basic_map_dup(bset_to_bmap(bset));
1136 return bset_from_bmap(dup);
1139 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1141 if (!bset)
1142 return NULL;
1144 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1145 bset->ref++;
1146 return bset;
1148 return isl_basic_set_dup(bset);
1151 struct isl_set *isl_set_copy(struct isl_set *set)
1153 if (!set)
1154 return NULL;
1156 set->ref++;
1157 return set;
1160 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1162 if (!bmap)
1163 return NULL;
1165 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1166 bmap->ref++;
1167 return bmap;
1169 bmap = isl_basic_map_dup(bmap);
1170 if (bmap)
1171 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1172 return bmap;
1175 struct isl_map *isl_map_copy(struct isl_map *map)
1177 if (!map)
1178 return NULL;
1180 map->ref++;
1181 return map;
1184 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1186 if (!bmap)
1187 return NULL;
1189 if (--bmap->ref > 0)
1190 return NULL;
1192 isl_ctx_deref(bmap->ctx);
1193 free(bmap->div);
1194 isl_blk_free(bmap->ctx, bmap->block2);
1195 free(bmap->ineq);
1196 isl_blk_free(bmap->ctx, bmap->block);
1197 isl_vec_free(bmap->sample);
1198 isl_space_free(bmap->dim);
1199 free(bmap);
1201 return NULL;
1204 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1206 return isl_basic_map_free(bset_to_bmap(bset));
1209 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1211 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1214 __isl_give isl_map *isl_map_align_params_map_map_and(
1215 __isl_take isl_map *map1, __isl_take isl_map *map2,
1216 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1217 __isl_take isl_map *map2))
1219 if (!map1 || !map2)
1220 goto error;
1221 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1222 return fn(map1, map2);
1223 if (!isl_space_has_named_params(map1->dim) ||
1224 !isl_space_has_named_params(map2->dim))
1225 isl_die(map1->ctx, isl_error_invalid,
1226 "unaligned unnamed parameters", goto error);
1227 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1228 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1229 return fn(map1, map2);
1230 error:
1231 isl_map_free(map1);
1232 isl_map_free(map2);
1233 return NULL;
1236 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1237 __isl_keep isl_map *map2,
1238 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1240 isl_bool r;
1242 if (!map1 || !map2)
1243 return isl_bool_error;
1244 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1245 return fn(map1, map2);
1246 if (!isl_space_has_named_params(map1->dim) ||
1247 !isl_space_has_named_params(map2->dim))
1248 isl_die(map1->ctx, isl_error_invalid,
1249 "unaligned unnamed parameters", return isl_bool_error);
1250 map1 = isl_map_copy(map1);
1251 map2 = isl_map_copy(map2);
1252 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1253 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1254 r = fn(map1, map2);
1255 isl_map_free(map1);
1256 isl_map_free(map2);
1257 return r;
1260 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1262 struct isl_ctx *ctx;
1263 if (!bmap)
1264 return -1;
1265 ctx = bmap->ctx;
1266 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1267 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1268 return -1);
1269 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1274 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1275 isl_int *t;
1276 int j = isl_basic_map_alloc_inequality(bmap);
1277 if (j < 0)
1278 return -1;
1279 t = bmap->ineq[j];
1280 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1281 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1282 bmap->eq[-1] = t;
1283 bmap->n_eq++;
1284 bmap->n_ineq--;
1285 bmap->eq--;
1286 return 0;
1288 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1289 bmap->extra - bmap->n_div);
1290 return bmap->n_eq++;
1293 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1295 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1298 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1300 if (!bmap)
1301 return -1;
1302 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1303 bmap->n_eq -= n;
1304 return 0;
1307 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1309 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1312 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1314 isl_int *t;
1315 if (!bmap)
1316 return -1;
1317 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1319 if (pos != bmap->n_eq - 1) {
1320 t = bmap->eq[pos];
1321 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1322 bmap->eq[bmap->n_eq - 1] = t;
1324 bmap->n_eq--;
1325 return 0;
1328 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1330 return isl_basic_map_drop_equality(bset_to_bmap(bset), pos);
1333 /* Turn inequality "pos" of "bmap" into an equality.
1335 * In particular, we move the inequality in front of the equalities
1336 * and move the last inequality in the position of the moved inequality.
1337 * Note that isl_tab_make_equalities_explicit depends on this particular
1338 * change in the ordering of the constraints.
1340 void isl_basic_map_inequality_to_equality(
1341 struct isl_basic_map *bmap, unsigned pos)
1343 isl_int *t;
1345 t = bmap->ineq[pos];
1346 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1347 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1348 bmap->eq[-1] = t;
1349 bmap->n_eq++;
1350 bmap->n_ineq--;
1351 bmap->eq--;
1352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1353 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1355 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1358 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1360 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1363 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1365 struct isl_ctx *ctx;
1366 if (!bmap)
1367 return -1;
1368 ctx = bmap->ctx;
1369 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1370 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1371 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1372 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1373 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1374 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1375 1 + isl_basic_map_total_dim(bmap),
1376 bmap->extra - bmap->n_div);
1377 return bmap->n_ineq++;
1380 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1382 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1385 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1387 if (!bmap)
1388 return -1;
1389 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1390 bmap->n_ineq -= n;
1391 return 0;
1394 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1396 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1399 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1401 isl_int *t;
1402 if (!bmap)
1403 return -1;
1404 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1406 if (pos != bmap->n_ineq - 1) {
1407 t = bmap->ineq[pos];
1408 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1409 bmap->ineq[bmap->n_ineq - 1] = t;
1410 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1412 bmap->n_ineq--;
1413 return 0;
1416 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1418 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1421 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1422 isl_int *eq)
1424 int k;
1426 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1427 if (!bmap)
1428 return NULL;
1429 k = isl_basic_map_alloc_equality(bmap);
1430 if (k < 0)
1431 goto error;
1432 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1433 return bmap;
1434 error:
1435 isl_basic_map_free(bmap);
1436 return NULL;
1439 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1440 isl_int *eq)
1442 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1445 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1446 isl_int *ineq)
1448 int k;
1450 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1451 if (!bmap)
1452 return NULL;
1453 k = isl_basic_map_alloc_inequality(bmap);
1454 if (k < 0)
1455 goto error;
1456 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1457 return bmap;
1458 error:
1459 isl_basic_map_free(bmap);
1460 return NULL;
1463 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1464 isl_int *ineq)
1466 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1469 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1471 if (!bmap)
1472 return -1;
1473 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1474 isl_seq_clr(bmap->div[bmap->n_div] +
1475 1 + 1 + isl_basic_map_total_dim(bmap),
1476 bmap->extra - bmap->n_div);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1478 return bmap->n_div++;
1481 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1483 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1486 /* 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 isl_basic_map_free_div(bmap, bmap->n_div);
1868 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1869 if (bmap->n_eq > 0)
1870 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1871 else {
1872 i = isl_basic_map_alloc_equality(bmap);
1873 if (i < 0)
1874 goto error;
1876 isl_int_set_si(bmap->eq[i][0], 1);
1877 isl_seq_clr(bmap->eq[i]+1, total);
1878 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1879 isl_vec_free(bmap->sample);
1880 bmap->sample = NULL;
1881 return isl_basic_map_finalize(bmap);
1882 error:
1883 isl_basic_map_free(bmap);
1884 return NULL;
1887 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1889 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1892 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1893 * of "bmap").
1895 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1897 isl_int *t = bmap->div[a];
1898 bmap->div[a] = bmap->div[b];
1899 bmap->div[b] = t;
1902 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1903 * div definitions accordingly.
1905 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1907 int i;
1908 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1910 swap_div(bmap, a, b);
1912 for (i = 0; i < bmap->n_eq; ++i)
1913 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1915 for (i = 0; i < bmap->n_ineq; ++i)
1916 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1918 for (i = 0; i < bmap->n_div; ++i)
1919 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1920 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1923 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1924 * div definitions accordingly.
1926 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1928 isl_basic_map_swap_div(bset, a, b);
1931 /* Eliminate the specified n dimensions starting at first from the
1932 * constraints, without removing the dimensions from the space.
1933 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1935 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1936 enum isl_dim_type type, unsigned first, unsigned n)
1938 int i;
1940 if (!map)
1941 return NULL;
1942 if (n == 0)
1943 return map;
1945 if (first + n > isl_map_dim(map, type) || first + n < first)
1946 isl_die(map->ctx, isl_error_invalid,
1947 "index out of bounds", goto error);
1949 map = isl_map_cow(map);
1950 if (!map)
1951 return NULL;
1953 for (i = 0; i < map->n; ++i) {
1954 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1955 if (!map->p[i])
1956 goto error;
1958 return map;
1959 error:
1960 isl_map_free(map);
1961 return NULL;
1964 /* Eliminate the specified n dimensions starting at first from the
1965 * constraints, without removing the dimensions from the space.
1966 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1968 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1969 enum isl_dim_type type, unsigned first, unsigned n)
1971 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1974 /* Eliminate the specified n dimensions starting at first from the
1975 * constraints, without removing the dimensions from the space.
1976 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1978 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1979 unsigned first, unsigned n)
1981 return isl_set_eliminate(set, isl_dim_set, first, n);
1984 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1985 __isl_take isl_basic_map *bmap)
1987 if (!bmap)
1988 return NULL;
1989 bmap = isl_basic_map_eliminate_vars(bmap,
1990 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1991 if (!bmap)
1992 return NULL;
1993 bmap->n_div = 0;
1994 return isl_basic_map_finalize(bmap);
1997 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1998 __isl_take isl_basic_set *bset)
2000 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2003 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2005 int i;
2007 if (!map)
2008 return NULL;
2009 if (map->n == 0)
2010 return map;
2012 map = isl_map_cow(map);
2013 if (!map)
2014 return NULL;
2016 for (i = 0; i < map->n; ++i) {
2017 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2018 if (!map->p[i])
2019 goto error;
2021 return map;
2022 error:
2023 isl_map_free(map);
2024 return NULL;
2027 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2029 return isl_map_remove_divs(set);
2032 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2033 enum isl_dim_type type, unsigned first, unsigned n)
2035 if (!bmap)
2036 return NULL;
2037 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2038 goto error);
2039 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2040 return bmap;
2041 bmap = isl_basic_map_eliminate_vars(bmap,
2042 isl_basic_map_offset(bmap, type) - 1 + first, n);
2043 if (!bmap)
2044 return bmap;
2045 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2046 return bmap;
2047 bmap = isl_basic_map_drop(bmap, type, first, n);
2048 return bmap;
2049 error:
2050 isl_basic_map_free(bmap);
2051 return NULL;
2054 /* Return true if the definition of the given div (recursively) involves
2055 * any of the given variables.
2057 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2058 unsigned first, unsigned n)
2060 int i;
2061 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2063 if (isl_int_is_zero(bmap->div[div][0]))
2064 return 0;
2065 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2066 return 1;
2068 for (i = bmap->n_div - 1; i >= 0; --i) {
2069 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2070 continue;
2071 if (div_involves_vars(bmap, i, first, n))
2072 return 1;
2075 return 0;
2078 /* Try and add a lower and/or upper bound on "div" to "bmap"
2079 * based on inequality "i".
2080 * "total" is the total number of variables (excluding the divs).
2081 * "v" is a temporary object that can be used during the calculations.
2082 * If "lb" is set, then a lower bound should be constructed.
2083 * If "ub" is set, then an upper bound should be constructed.
2085 * The calling function has already checked that the inequality does not
2086 * reference "div", but we still need to check that the inequality is
2087 * of the right form. We'll consider the case where we want to construct
2088 * a lower bound. The construction of upper bounds is similar.
2090 * Let "div" be of the form
2092 * q = floor((a + f(x))/d)
2094 * We essentially check if constraint "i" is of the form
2096 * b + f(x) >= 0
2098 * so that we can use it to derive a lower bound on "div".
2099 * However, we allow a slightly more general form
2101 * b + g(x) >= 0
2103 * with the condition that the coefficients of g(x) - f(x) are all
2104 * divisible by d.
2105 * Rewriting this constraint as
2107 * 0 >= -b - g(x)
2109 * adding a + f(x) to both sides and dividing by d, we obtain
2111 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2113 * Taking the floor on both sides, we obtain
2115 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2117 * or
2119 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2121 * In the case of an upper bound, we construct the constraint
2123 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2126 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2127 __isl_take isl_basic_map *bmap, int div, int i,
2128 unsigned total, isl_int v, int lb, int ub)
2130 int j;
2132 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2133 if (lb) {
2134 isl_int_sub(v, bmap->ineq[i][1 + j],
2135 bmap->div[div][1 + 1 + j]);
2136 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2138 if (ub) {
2139 isl_int_add(v, bmap->ineq[i][1 + j],
2140 bmap->div[div][1 + 1 + j]);
2141 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2144 if (!lb && !ub)
2145 return bmap;
2147 bmap = isl_basic_map_cow(bmap);
2148 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2149 if (lb) {
2150 int k = isl_basic_map_alloc_inequality(bmap);
2151 if (k < 0)
2152 goto error;
2153 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2154 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2155 bmap->div[div][1 + j]);
2156 isl_int_cdiv_q(bmap->ineq[k][j],
2157 bmap->ineq[k][j], bmap->div[div][0]);
2159 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2161 if (ub) {
2162 int k = isl_basic_map_alloc_inequality(bmap);
2163 if (k < 0)
2164 goto error;
2165 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2166 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2167 bmap->div[div][1 + j]);
2168 isl_int_fdiv_q(bmap->ineq[k][j],
2169 bmap->ineq[k][j], bmap->div[div][0]);
2171 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2174 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2175 return bmap;
2176 error:
2177 isl_basic_map_free(bmap);
2178 return NULL;
2181 /* This function is called right before "div" is eliminated from "bmap"
2182 * using Fourier-Motzkin.
2183 * Look through the constraints of "bmap" for constraints on the argument
2184 * of the integer division and use them to construct constraints on the
2185 * integer division itself. These constraints can then be combined
2186 * during the Fourier-Motzkin elimination.
2187 * Note that it is only useful to introduce lower bounds on "div"
2188 * if "bmap" already contains upper bounds on "div" as the newly
2189 * introduce lower bounds can then be combined with the pre-existing
2190 * upper bounds. Similarly for upper bounds.
2191 * We therefore first check if "bmap" contains any lower and/or upper bounds
2192 * on "div".
2194 * It is interesting to note that the introduction of these constraints
2195 * can indeed lead to more accurate results, even when compared to
2196 * deriving constraints on the argument of "div" from constraints on "div".
2197 * Consider, for example, the set
2199 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2201 * The second constraint can be rewritten as
2203 * 2 * [(-i-2j+3)/4] + k >= 0
2205 * from which we can derive
2207 * -i - 2j + 3 >= -2k
2209 * or
2211 * i + 2j <= 3 + 2k
2213 * Combined with the first constraint, we obtain
2215 * -3 <= 3 + 2k or k >= -3
2217 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2218 * the first constraint, we obtain
2220 * [(i + 2j)/4] >= [-3/4] = -1
2222 * Combining this constraint with the second constraint, we obtain
2224 * k >= -2
2226 static __isl_give isl_basic_map *insert_bounds_on_div(
2227 __isl_take isl_basic_map *bmap, int div)
2229 int i;
2230 int check_lb, check_ub;
2231 isl_int v;
2232 unsigned total;
2234 if (!bmap)
2235 return NULL;
2237 if (isl_int_is_zero(bmap->div[div][0]))
2238 return bmap;
2240 total = isl_space_dim(bmap->dim, isl_dim_all);
2242 check_lb = 0;
2243 check_ub = 0;
2244 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2245 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2246 if (s > 0)
2247 check_ub = 1;
2248 if (s < 0)
2249 check_lb = 1;
2252 if (!check_lb && !check_ub)
2253 return bmap;
2255 isl_int_init(v);
2257 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2258 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2259 continue;
2261 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2262 check_lb, check_ub);
2265 isl_int_clear(v);
2267 return bmap;
2270 /* Remove all divs (recursively) involving any of the given dimensions
2271 * in their definitions.
2273 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2274 __isl_take isl_basic_map *bmap,
2275 enum isl_dim_type type, unsigned first, unsigned n)
2277 int i;
2279 if (!bmap)
2280 return NULL;
2281 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2282 goto error);
2283 first += isl_basic_map_offset(bmap, type);
2285 for (i = bmap->n_div - 1; i >= 0; --i) {
2286 if (!div_involves_vars(bmap, i, first, n))
2287 continue;
2288 bmap = insert_bounds_on_div(bmap, i);
2289 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2290 if (!bmap)
2291 return NULL;
2292 i = bmap->n_div;
2295 return bmap;
2296 error:
2297 isl_basic_map_free(bmap);
2298 return NULL;
2301 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2302 __isl_take isl_basic_set *bset,
2303 enum isl_dim_type type, unsigned first, unsigned n)
2305 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2308 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2309 enum isl_dim_type type, unsigned first, unsigned n)
2311 int i;
2313 if (!map)
2314 return NULL;
2315 if (map->n == 0)
2316 return map;
2318 map = isl_map_cow(map);
2319 if (!map)
2320 return NULL;
2322 for (i = 0; i < map->n; ++i) {
2323 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2324 type, first, n);
2325 if (!map->p[i])
2326 goto error;
2328 return map;
2329 error:
2330 isl_map_free(map);
2331 return NULL;
2334 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2335 enum isl_dim_type type, unsigned first, unsigned n)
2337 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2338 type, first, n));
2341 /* Does the description of "bmap" depend on the specified dimensions?
2342 * We also check whether the dimensions appear in any of the div definitions.
2343 * In principle there is no need for this check. If the dimensions appear
2344 * in a div definition, they also appear in the defining constraints of that
2345 * div.
2347 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2348 enum isl_dim_type type, unsigned first, unsigned n)
2350 int i;
2352 if (!bmap)
2353 return isl_bool_error;
2355 if (first + n > isl_basic_map_dim(bmap, type))
2356 isl_die(bmap->ctx, isl_error_invalid,
2357 "index out of bounds", return isl_bool_error);
2359 first += isl_basic_map_offset(bmap, type);
2360 for (i = 0; i < bmap->n_eq; ++i)
2361 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2362 return isl_bool_true;
2363 for (i = 0; i < bmap->n_ineq; ++i)
2364 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2365 return isl_bool_true;
2366 for (i = 0; i < bmap->n_div; ++i) {
2367 if (isl_int_is_zero(bmap->div[i][0]))
2368 continue;
2369 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2370 return isl_bool_true;
2373 return isl_bool_false;
2376 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2377 enum isl_dim_type type, unsigned first, unsigned n)
2379 int i;
2381 if (!map)
2382 return isl_bool_error;
2384 if (first + n > isl_map_dim(map, type))
2385 isl_die(map->ctx, isl_error_invalid,
2386 "index out of bounds", return isl_bool_error);
2388 for (i = 0; i < map->n; ++i) {
2389 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2390 type, first, n);
2391 if (involves < 0 || involves)
2392 return involves;
2395 return isl_bool_false;
2398 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2399 enum isl_dim_type type, unsigned first, unsigned n)
2401 return isl_basic_map_involves_dims(bset, type, first, n);
2404 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2405 enum isl_dim_type type, unsigned first, unsigned n)
2407 return isl_map_involves_dims(set, type, first, n);
2410 /* Does local variable "div" of "bmap" have a complete explicit representation?
2411 * Having a complete explicit representation requires not only
2412 * an explicit representation, but also that all local variables
2413 * that appear in this explicit representation in turn have
2414 * a complete explicit representation.
2416 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2418 int i;
2419 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2420 isl_bool marked;
2422 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2423 if (marked < 0 || marked)
2424 return isl_bool_not(marked);
2426 for (i = bmap->n_div - 1; i >= 0; --i) {
2427 isl_bool known;
2429 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2430 continue;
2431 known = isl_basic_map_div_is_known(bmap, i);
2432 if (known < 0 || !known)
2433 return known;
2436 return isl_bool_true;
2439 /* Does local variable "div" of "bset" have a complete explicit representation?
2441 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2443 return isl_basic_map_div_is_known(bset, div);
2446 /* Remove all divs that are unknown or defined in terms of unknown divs.
2448 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2449 __isl_take isl_basic_map *bmap)
2451 int i;
2453 if (!bmap)
2454 return NULL;
2456 for (i = bmap->n_div - 1; i >= 0; --i) {
2457 if (isl_basic_map_div_is_known(bmap, i))
2458 continue;
2459 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2460 if (!bmap)
2461 return NULL;
2462 i = bmap->n_div;
2465 return bmap;
2468 /* Remove all divs that are unknown or defined in terms of unknown divs.
2470 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2471 __isl_take isl_basic_set *bset)
2473 return isl_basic_map_remove_unknown_divs(bset);
2476 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2478 int i;
2480 if (!map)
2481 return NULL;
2482 if (map->n == 0)
2483 return map;
2485 map = isl_map_cow(map);
2486 if (!map)
2487 return NULL;
2489 for (i = 0; i < map->n; ++i) {
2490 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2491 if (!map->p[i])
2492 goto error;
2494 return map;
2495 error:
2496 isl_map_free(map);
2497 return NULL;
2500 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2502 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2505 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2506 __isl_take isl_basic_set *bset,
2507 enum isl_dim_type type, unsigned first, unsigned n)
2509 isl_basic_map *bmap = bset_to_bmap(bset);
2510 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2511 return bset_from_bmap(bmap);
2514 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2515 enum isl_dim_type type, unsigned first, unsigned n)
2517 int i;
2519 if (n == 0)
2520 return map;
2522 map = isl_map_cow(map);
2523 if (!map)
2524 return NULL;
2525 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2527 for (i = 0; i < map->n; ++i) {
2528 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2529 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2530 if (!map->p[i])
2531 goto error;
2533 map = isl_map_drop(map, type, first, n);
2534 return map;
2535 error:
2536 isl_map_free(map);
2537 return NULL;
2540 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2541 enum isl_dim_type type, unsigned first, unsigned n)
2543 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2544 type, first, n));
2547 /* Project out n inputs starting at first using Fourier-Motzkin */
2548 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2549 unsigned first, unsigned n)
2551 return isl_map_remove_dims(map, isl_dim_in, first, n);
2554 static void dump_term(struct isl_basic_map *bmap,
2555 isl_int c, int pos, FILE *out)
2557 const char *name;
2558 unsigned in = isl_basic_map_n_in(bmap);
2559 unsigned dim = in + isl_basic_map_n_out(bmap);
2560 unsigned nparam = isl_basic_map_n_param(bmap);
2561 if (!pos)
2562 isl_int_print(out, c, 0);
2563 else {
2564 if (!isl_int_is_one(c))
2565 isl_int_print(out, c, 0);
2566 if (pos < 1 + nparam) {
2567 name = isl_space_get_dim_name(bmap->dim,
2568 isl_dim_param, pos - 1);
2569 if (name)
2570 fprintf(out, "%s", name);
2571 else
2572 fprintf(out, "p%d", pos - 1);
2573 } else if (pos < 1 + nparam + in)
2574 fprintf(out, "i%d", pos - 1 - nparam);
2575 else if (pos < 1 + nparam + dim)
2576 fprintf(out, "o%d", pos - 1 - nparam - in);
2577 else
2578 fprintf(out, "e%d", pos - 1 - nparam - dim);
2582 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2583 int sign, FILE *out)
2585 int i;
2586 int first;
2587 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2588 isl_int v;
2590 isl_int_init(v);
2591 for (i = 0, first = 1; i < len; ++i) {
2592 if (isl_int_sgn(c[i]) * sign <= 0)
2593 continue;
2594 if (!first)
2595 fprintf(out, " + ");
2596 first = 0;
2597 isl_int_abs(v, c[i]);
2598 dump_term(bmap, v, i, out);
2600 isl_int_clear(v);
2601 if (first)
2602 fprintf(out, "0");
2605 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2606 const char *op, FILE *out, int indent)
2608 int i;
2610 fprintf(out, "%*s", indent, "");
2612 dump_constraint_sign(bmap, c, 1, out);
2613 fprintf(out, " %s ", op);
2614 dump_constraint_sign(bmap, c, -1, out);
2616 fprintf(out, "\n");
2618 for (i = bmap->n_div; i < bmap->extra; ++i) {
2619 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2620 continue;
2621 fprintf(out, "%*s", indent, "");
2622 fprintf(out, "ERROR: unused div coefficient not zero\n");
2623 abort();
2627 static void dump_constraints(struct isl_basic_map *bmap,
2628 isl_int **c, unsigned n,
2629 const char *op, FILE *out, int indent)
2631 int i;
2633 for (i = 0; i < n; ++i)
2634 dump_constraint(bmap, c[i], op, out, indent);
2637 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2639 int j;
2640 int first = 1;
2641 unsigned total = isl_basic_map_total_dim(bmap);
2643 for (j = 0; j < 1 + total; ++j) {
2644 if (isl_int_is_zero(exp[j]))
2645 continue;
2646 if (!first && isl_int_is_pos(exp[j]))
2647 fprintf(out, "+");
2648 dump_term(bmap, exp[j], j, out);
2649 first = 0;
2653 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2655 int i;
2657 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2658 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2660 for (i = 0; i < bmap->n_div; ++i) {
2661 fprintf(out, "%*s", indent, "");
2662 fprintf(out, "e%d = [(", i);
2663 dump_affine(bmap, bmap->div[i]+1, out);
2664 fprintf(out, ")/");
2665 isl_int_print(out, bmap->div[i][0], 0);
2666 fprintf(out, "]\n");
2670 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2671 FILE *out, int indent)
2673 if (!bset) {
2674 fprintf(out, "null basic set\n");
2675 return;
2678 fprintf(out, "%*s", indent, "");
2679 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2680 bset->ref, bset->dim->nparam, bset->dim->n_out,
2681 bset->extra, bset->flags);
2682 dump(bset_to_bmap(bset), out, indent);
2685 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2686 FILE *out, int indent)
2688 if (!bmap) {
2689 fprintf(out, "null basic map\n");
2690 return;
2693 fprintf(out, "%*s", indent, "");
2694 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2695 "flags: %x, n_name: %d\n",
2696 bmap->ref,
2697 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2698 bmap->extra, bmap->flags, bmap->dim->n_id);
2699 dump(bmap, out, indent);
2702 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2704 unsigned total;
2705 if (!bmap)
2706 return -1;
2707 total = isl_basic_map_total_dim(bmap);
2708 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2709 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2710 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2711 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2712 return 0;
2715 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2716 unsigned flags)
2718 if (!space)
2719 return NULL;
2720 if (isl_space_dim(space, isl_dim_in) != 0)
2721 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2722 "set cannot have input dimensions", goto error);
2723 return isl_map_alloc_space(space, n, flags);
2724 error:
2725 isl_space_free(space);
2726 return NULL;
2729 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2730 unsigned nparam, unsigned dim, int n, unsigned flags)
2732 struct isl_set *set;
2733 isl_space *dims;
2735 dims = isl_space_alloc(ctx, nparam, 0, dim);
2736 if (!dims)
2737 return NULL;
2739 set = isl_set_alloc_space(dims, n, flags);
2740 return set;
2743 /* Make sure "map" has room for at least "n" more basic maps.
2745 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2747 int i;
2748 struct isl_map *grown = NULL;
2750 if (!map)
2751 return NULL;
2752 isl_assert(map->ctx, n >= 0, goto error);
2753 if (map->n + n <= map->size)
2754 return map;
2755 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2756 if (!grown)
2757 goto error;
2758 for (i = 0; i < map->n; ++i) {
2759 grown->p[i] = isl_basic_map_copy(map->p[i]);
2760 if (!grown->p[i])
2761 goto error;
2762 grown->n++;
2764 isl_map_free(map);
2765 return grown;
2766 error:
2767 isl_map_free(grown);
2768 isl_map_free(map);
2769 return NULL;
2772 /* Make sure "set" has room for at least "n" more basic sets.
2774 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2776 return set_from_map(isl_map_grow(set_to_map(set), n));
2779 struct isl_set *isl_set_dup(struct isl_set *set)
2781 int i;
2782 struct isl_set *dup;
2784 if (!set)
2785 return NULL;
2787 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2788 if (!dup)
2789 return NULL;
2790 for (i = 0; i < set->n; ++i)
2791 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2792 return dup;
2795 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2797 return isl_map_from_basic_map(bset);
2800 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2802 struct isl_map *map;
2804 if (!bmap)
2805 return NULL;
2807 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2808 return isl_map_add_basic_map(map, bmap);
2811 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2812 __isl_take isl_basic_set *bset)
2814 return set_from_map(isl_map_add_basic_map(set_to_map(set),
2815 bset_to_bmap(bset)));
2818 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2820 return isl_map_free(set);
2823 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2825 int i;
2827 if (!set) {
2828 fprintf(out, "null set\n");
2829 return;
2832 fprintf(out, "%*s", indent, "");
2833 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2834 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2835 set->flags);
2836 for (i = 0; i < set->n; ++i) {
2837 fprintf(out, "%*s", indent, "");
2838 fprintf(out, "basic set %d:\n", i);
2839 isl_basic_set_print_internal(set->p[i], out, indent+4);
2843 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2845 int i;
2847 if (!map) {
2848 fprintf(out, "null map\n");
2849 return;
2852 fprintf(out, "%*s", indent, "");
2853 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2854 "flags: %x, n_name: %d\n",
2855 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2856 map->dim->n_out, map->flags, map->dim->n_id);
2857 for (i = 0; i < map->n; ++i) {
2858 fprintf(out, "%*s", indent, "");
2859 fprintf(out, "basic map %d:\n", i);
2860 isl_basic_map_print_internal(map->p[i], out, indent+4);
2864 struct isl_basic_map *isl_basic_map_intersect_domain(
2865 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2867 struct isl_basic_map *bmap_domain;
2869 if (!bmap || !bset)
2870 goto error;
2872 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2873 bset->dim, isl_dim_param), goto error);
2875 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2876 isl_assert(bset->ctx,
2877 isl_basic_map_compatible_domain(bmap, bset), goto error);
2879 bmap = isl_basic_map_cow(bmap);
2880 if (!bmap)
2881 goto error;
2882 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2883 bset->n_div, bset->n_eq, bset->n_ineq);
2884 bmap_domain = isl_basic_map_from_domain(bset);
2885 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2887 bmap = isl_basic_map_simplify(bmap);
2888 return isl_basic_map_finalize(bmap);
2889 error:
2890 isl_basic_map_free(bmap);
2891 isl_basic_set_free(bset);
2892 return NULL;
2895 struct isl_basic_map *isl_basic_map_intersect_range(
2896 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2898 struct isl_basic_map *bmap_range;
2900 if (!bmap || !bset)
2901 goto error;
2903 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2904 bset->dim, isl_dim_param), goto error);
2906 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2907 isl_assert(bset->ctx,
2908 isl_basic_map_compatible_range(bmap, bset), goto error);
2910 if (isl_basic_set_plain_is_universe(bset)) {
2911 isl_basic_set_free(bset);
2912 return bmap;
2915 bmap = isl_basic_map_cow(bmap);
2916 if (!bmap)
2917 goto error;
2918 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2919 bset->n_div, bset->n_eq, bset->n_ineq);
2920 bmap_range = bset_to_bmap(bset);
2921 bmap = add_constraints(bmap, bmap_range, 0, 0);
2923 bmap = isl_basic_map_simplify(bmap);
2924 return isl_basic_map_finalize(bmap);
2925 error:
2926 isl_basic_map_free(bmap);
2927 isl_basic_set_free(bset);
2928 return NULL;
2931 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2932 __isl_keep isl_vec *vec)
2934 int i;
2935 unsigned total;
2936 isl_int s;
2938 if (!bmap || !vec)
2939 return isl_bool_error;
2941 total = 1 + isl_basic_map_total_dim(bmap);
2942 if (total != vec->size)
2943 return isl_bool_error;
2945 isl_int_init(s);
2947 for (i = 0; i < bmap->n_eq; ++i) {
2948 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2949 if (!isl_int_is_zero(s)) {
2950 isl_int_clear(s);
2951 return isl_bool_false;
2955 for (i = 0; i < bmap->n_ineq; ++i) {
2956 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2957 if (isl_int_is_neg(s)) {
2958 isl_int_clear(s);
2959 return isl_bool_false;
2963 isl_int_clear(s);
2965 return isl_bool_true;
2968 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2969 __isl_keep isl_vec *vec)
2971 return isl_basic_map_contains(bset_to_bmap(bset), vec);
2974 struct isl_basic_map *isl_basic_map_intersect(
2975 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2977 struct isl_vec *sample = NULL;
2979 if (!bmap1 || !bmap2)
2980 goto error;
2982 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2983 bmap2->dim, isl_dim_param), goto error);
2984 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2985 isl_space_dim(bmap1->dim, isl_dim_param) &&
2986 isl_space_dim(bmap2->dim, isl_dim_all) !=
2987 isl_space_dim(bmap2->dim, isl_dim_param))
2988 return isl_basic_map_intersect(bmap2, bmap1);
2990 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2991 isl_space_dim(bmap2->dim, isl_dim_param))
2992 isl_assert(bmap1->ctx,
2993 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2995 if (isl_basic_map_plain_is_empty(bmap1)) {
2996 isl_basic_map_free(bmap2);
2997 return bmap1;
2999 if (isl_basic_map_plain_is_empty(bmap2)) {
3000 isl_basic_map_free(bmap1);
3001 return bmap2;
3004 if (bmap1->sample &&
3005 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3006 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3007 sample = isl_vec_copy(bmap1->sample);
3008 else if (bmap2->sample &&
3009 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3010 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3011 sample = isl_vec_copy(bmap2->sample);
3013 bmap1 = isl_basic_map_cow(bmap1);
3014 if (!bmap1)
3015 goto error;
3016 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3017 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3018 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3020 if (!bmap1)
3021 isl_vec_free(sample);
3022 else if (sample) {
3023 isl_vec_free(bmap1->sample);
3024 bmap1->sample = sample;
3027 bmap1 = isl_basic_map_simplify(bmap1);
3028 return isl_basic_map_finalize(bmap1);
3029 error:
3030 if (sample)
3031 isl_vec_free(sample);
3032 isl_basic_map_free(bmap1);
3033 isl_basic_map_free(bmap2);
3034 return NULL;
3037 struct isl_basic_set *isl_basic_set_intersect(
3038 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3040 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3041 bset_to_bmap(bset2)));
3044 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3045 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3047 return isl_basic_set_intersect(bset1, bset2);
3050 /* Special case of isl_map_intersect, where both map1 and map2
3051 * are convex, without any divs and such that either map1 or map2
3052 * contains a single constraint. This constraint is then simply
3053 * added to the other map.
3055 static __isl_give isl_map *map_intersect_add_constraint(
3056 __isl_take isl_map *map1, __isl_take isl_map *map2)
3058 isl_assert(map1->ctx, map1->n == 1, goto error);
3059 isl_assert(map2->ctx, map1->n == 1, goto error);
3060 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3061 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3063 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3064 return isl_map_intersect(map2, map1);
3066 isl_assert(map2->ctx,
3067 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3069 map1 = isl_map_cow(map1);
3070 if (!map1)
3071 goto error;
3072 if (isl_map_plain_is_empty(map1)) {
3073 isl_map_free(map2);
3074 return map1;
3076 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3077 if (map2->p[0]->n_eq == 1)
3078 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3079 else
3080 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3081 map2->p[0]->ineq[0]);
3083 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3084 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3085 if (!map1->p[0])
3086 goto error;
3088 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3089 isl_basic_map_free(map1->p[0]);
3090 map1->n = 0;
3093 isl_map_free(map2);
3095 return map1;
3096 error:
3097 isl_map_free(map1);
3098 isl_map_free(map2);
3099 return NULL;
3102 /* map2 may be either a parameter domain or a map living in the same
3103 * space as map1.
3105 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3106 __isl_take isl_map *map2)
3108 unsigned flags = 0;
3109 isl_map *result;
3110 int i, j;
3112 if (!map1 || !map2)
3113 goto error;
3115 if ((isl_map_plain_is_empty(map1) ||
3116 isl_map_plain_is_universe(map2)) &&
3117 isl_space_is_equal(map1->dim, map2->dim)) {
3118 isl_map_free(map2);
3119 return map1;
3121 if ((isl_map_plain_is_empty(map2) ||
3122 isl_map_plain_is_universe(map1)) &&
3123 isl_space_is_equal(map1->dim, map2->dim)) {
3124 isl_map_free(map1);
3125 return map2;
3128 if (map1->n == 1 && map2->n == 1 &&
3129 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3130 isl_space_is_equal(map1->dim, map2->dim) &&
3131 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3132 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3133 return map_intersect_add_constraint(map1, map2);
3135 if (isl_space_dim(map2->dim, isl_dim_all) !=
3136 isl_space_dim(map2->dim, isl_dim_param))
3137 isl_assert(map1->ctx,
3138 isl_space_is_equal(map1->dim, map2->dim), goto error);
3140 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3141 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3142 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3144 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3145 map1->n * map2->n, flags);
3146 if (!result)
3147 goto error;
3148 for (i = 0; i < map1->n; ++i)
3149 for (j = 0; j < map2->n; ++j) {
3150 struct isl_basic_map *part;
3151 part = isl_basic_map_intersect(
3152 isl_basic_map_copy(map1->p[i]),
3153 isl_basic_map_copy(map2->p[j]));
3154 if (isl_basic_map_is_empty(part) < 0)
3155 part = isl_basic_map_free(part);
3156 result = isl_map_add_basic_map(result, part);
3157 if (!result)
3158 goto error;
3160 isl_map_free(map1);
3161 isl_map_free(map2);
3162 return result;
3163 error:
3164 isl_map_free(map1);
3165 isl_map_free(map2);
3166 return NULL;
3169 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3170 __isl_take isl_map *map2)
3172 if (!map1 || !map2)
3173 goto error;
3174 if (!isl_space_is_equal(map1->dim, map2->dim))
3175 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3176 "spaces don't match", goto error);
3177 return map_intersect_internal(map1, map2);
3178 error:
3179 isl_map_free(map1);
3180 isl_map_free(map2);
3181 return NULL;
3184 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3185 __isl_take isl_map *map2)
3187 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3190 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3192 return set_from_map(isl_map_intersect(set_to_map(set1),
3193 set_to_map(set2)));
3196 /* map_intersect_internal accepts intersections
3197 * with parameter domains, so we can just call that function.
3199 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3200 __isl_take isl_set *params)
3202 return map_intersect_internal(map, params);
3205 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3206 __isl_take isl_map *map2)
3208 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3211 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3212 __isl_take isl_set *params)
3214 return isl_map_intersect_params(set, params);
3217 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3219 isl_space *space;
3220 unsigned pos, n1, n2;
3222 if (!bmap)
3223 return NULL;
3224 bmap = isl_basic_map_cow(bmap);
3225 if (!bmap)
3226 return NULL;
3227 space = isl_space_reverse(isl_space_copy(bmap->dim));
3228 pos = isl_basic_map_offset(bmap, isl_dim_in);
3229 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3230 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3231 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3232 return isl_basic_map_reset_space(bmap, space);
3235 static __isl_give isl_basic_map *basic_map_space_reset(
3236 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3238 isl_space *space;
3240 if (!bmap)
3241 return NULL;
3242 if (!isl_space_is_named_or_nested(bmap->dim, type))
3243 return bmap;
3245 space = isl_basic_map_get_space(bmap);
3246 space = isl_space_reset(space, type);
3247 bmap = isl_basic_map_reset_space(bmap, space);
3248 return bmap;
3251 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3252 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3253 unsigned pos, unsigned n)
3255 isl_space *res_dim;
3256 struct isl_basic_map *res;
3257 struct isl_dim_map *dim_map;
3258 unsigned total, off;
3259 enum isl_dim_type t;
3261 if (n == 0)
3262 return basic_map_space_reset(bmap, type);
3264 if (!bmap)
3265 return NULL;
3267 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3269 total = isl_basic_map_total_dim(bmap) + n;
3270 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3271 off = 0;
3272 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3273 if (t != type) {
3274 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3275 } else {
3276 unsigned size = isl_basic_map_dim(bmap, t);
3277 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3278 0, pos, off);
3279 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3280 pos, size - pos, off + pos + n);
3282 off += isl_space_dim(res_dim, t);
3284 isl_dim_map_div(dim_map, bmap, off);
3286 res = isl_basic_map_alloc_space(res_dim,
3287 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3288 if (isl_basic_map_is_rational(bmap))
3289 res = isl_basic_map_set_rational(res);
3290 if (isl_basic_map_plain_is_empty(bmap)) {
3291 isl_basic_map_free(bmap);
3292 free(dim_map);
3293 return isl_basic_map_set_to_empty(res);
3295 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3296 return isl_basic_map_finalize(res);
3299 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3300 __isl_take isl_basic_set *bset,
3301 enum isl_dim_type type, unsigned pos, unsigned n)
3303 return isl_basic_map_insert_dims(bset, type, pos, n);
3306 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3307 enum isl_dim_type type, unsigned n)
3309 if (!bmap)
3310 return NULL;
3311 return isl_basic_map_insert_dims(bmap, type,
3312 isl_basic_map_dim(bmap, type), n);
3315 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3316 enum isl_dim_type type, unsigned n)
3318 if (!bset)
3319 return NULL;
3320 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3321 return isl_basic_map_add_dims(bset, type, n);
3322 error:
3323 isl_basic_set_free(bset);
3324 return NULL;
3327 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3328 enum isl_dim_type type)
3330 isl_space *space;
3332 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3333 return map;
3335 space = isl_map_get_space(map);
3336 space = isl_space_reset(space, type);
3337 map = isl_map_reset_space(map, space);
3338 return map;
3341 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3342 enum isl_dim_type type, unsigned pos, unsigned n)
3344 int i;
3346 if (n == 0)
3347 return map_space_reset(map, type);
3349 map = isl_map_cow(map);
3350 if (!map)
3351 return NULL;
3353 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3354 if (!map->dim)
3355 goto error;
3357 for (i = 0; i < map->n; ++i) {
3358 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3359 if (!map->p[i])
3360 goto error;
3363 return map;
3364 error:
3365 isl_map_free(map);
3366 return NULL;
3369 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3370 enum isl_dim_type type, unsigned pos, unsigned n)
3372 return isl_map_insert_dims(set, type, pos, n);
3375 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3376 enum isl_dim_type type, unsigned n)
3378 if (!map)
3379 return NULL;
3380 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3383 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3384 enum isl_dim_type type, unsigned n)
3386 if (!set)
3387 return NULL;
3388 isl_assert(set->ctx, type != isl_dim_in, goto error);
3389 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3390 error:
3391 isl_set_free(set);
3392 return NULL;
3395 __isl_give isl_basic_map *isl_basic_map_move_dims(
3396 __isl_take isl_basic_map *bmap,
3397 enum isl_dim_type dst_type, unsigned dst_pos,
3398 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3400 struct isl_dim_map *dim_map;
3401 struct isl_basic_map *res;
3402 enum isl_dim_type t;
3403 unsigned total, off;
3405 if (!bmap)
3406 return NULL;
3407 if (n == 0)
3408 return bmap;
3410 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3411 goto error);
3413 if (dst_type == src_type && dst_pos == src_pos)
3414 return bmap;
3416 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3418 if (pos(bmap->dim, dst_type) + dst_pos ==
3419 pos(bmap->dim, src_type) + src_pos +
3420 ((src_type < dst_type) ? n : 0)) {
3421 bmap = isl_basic_map_cow(bmap);
3422 if (!bmap)
3423 return NULL;
3425 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3426 src_type, src_pos, n);
3427 if (!bmap->dim)
3428 goto error;
3430 bmap = isl_basic_map_finalize(bmap);
3432 return bmap;
3435 total = isl_basic_map_total_dim(bmap);
3436 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3438 off = 0;
3439 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3440 unsigned size = isl_space_dim(bmap->dim, t);
3441 if (t == dst_type) {
3442 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3443 0, dst_pos, off);
3444 off += dst_pos;
3445 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3446 src_pos, n, off);
3447 off += n;
3448 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3449 dst_pos, size - dst_pos, off);
3450 off += size - dst_pos;
3451 } else if (t == src_type) {
3452 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3453 0, src_pos, off);
3454 off += src_pos;
3455 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3456 src_pos + n, size - src_pos - n, off);
3457 off += size - src_pos - n;
3458 } else {
3459 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3460 off += size;
3463 isl_dim_map_div(dim_map, bmap, off);
3465 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3466 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3467 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3468 if (!bmap)
3469 goto error;
3471 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3472 src_type, src_pos, n);
3473 if (!bmap->dim)
3474 goto error;
3476 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3477 bmap = isl_basic_map_gauss(bmap, NULL);
3478 bmap = isl_basic_map_finalize(bmap);
3480 return bmap;
3481 error:
3482 isl_basic_map_free(bmap);
3483 return NULL;
3486 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3487 enum isl_dim_type dst_type, unsigned dst_pos,
3488 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3490 isl_basic_map *bmap = bset_to_bmap(bset);
3491 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3492 src_type, src_pos, n);
3493 return bset_from_bmap(bmap);
3496 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3497 enum isl_dim_type dst_type, unsigned dst_pos,
3498 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3500 if (!set)
3501 return NULL;
3502 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3503 return set_from_map(isl_map_move_dims(set_to_map(set),
3504 dst_type, dst_pos, src_type, src_pos, n));
3505 error:
3506 isl_set_free(set);
3507 return NULL;
3510 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3511 enum isl_dim_type dst_type, unsigned dst_pos,
3512 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3514 int i;
3516 if (!map)
3517 return NULL;
3518 if (n == 0)
3519 return map;
3521 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3522 goto error);
3524 if (dst_type == src_type && dst_pos == src_pos)
3525 return map;
3527 isl_assert(map->ctx, dst_type != src_type, goto error);
3529 map = isl_map_cow(map);
3530 if (!map)
3531 return NULL;
3533 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3534 if (!map->dim)
3535 goto error;
3537 for (i = 0; i < map->n; ++i) {
3538 map->p[i] = isl_basic_map_move_dims(map->p[i],
3539 dst_type, dst_pos,
3540 src_type, src_pos, n);
3541 if (!map->p[i])
3542 goto error;
3545 return map;
3546 error:
3547 isl_map_free(map);
3548 return NULL;
3551 /* Move the specified dimensions to the last columns right before
3552 * the divs. Don't change the dimension specification of bmap.
3553 * That's the responsibility of the caller.
3555 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3556 enum isl_dim_type type, unsigned first, unsigned n)
3558 struct isl_dim_map *dim_map;
3559 struct isl_basic_map *res;
3560 enum isl_dim_type t;
3561 unsigned total, off;
3563 if (!bmap)
3564 return NULL;
3565 if (pos(bmap->dim, type) + first + n ==
3566 1 + isl_space_dim(bmap->dim, isl_dim_all))
3567 return bmap;
3569 total = isl_basic_map_total_dim(bmap);
3570 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3572 off = 0;
3573 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3574 unsigned size = isl_space_dim(bmap->dim, t);
3575 if (t == type) {
3576 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3577 0, first, off);
3578 off += first;
3579 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3580 first, n, total - bmap->n_div - n);
3581 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3582 first + n, size - (first + n), off);
3583 off += size - (first + n);
3584 } else {
3585 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3586 off += size;
3589 isl_dim_map_div(dim_map, bmap, off + n);
3591 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3592 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3593 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3594 return res;
3597 /* Insert "n" rows in the divs of "bmap".
3599 * The number of columns is not changed, which means that the last
3600 * dimensions of "bmap" are being reintepreted as the new divs.
3601 * The space of "bmap" is not adjusted, however, which means
3602 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3603 * from the space of "bmap" is the responsibility of the caller.
3605 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3606 int n)
3608 int i;
3609 size_t row_size;
3610 isl_int **new_div;
3611 isl_int *old;
3613 bmap = isl_basic_map_cow(bmap);
3614 if (!bmap)
3615 return NULL;
3617 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3618 old = bmap->block2.data;
3619 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3620 (bmap->extra + n) * (1 + row_size));
3621 if (!bmap->block2.data)
3622 return isl_basic_map_free(bmap);
3623 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3624 if (!new_div)
3625 return isl_basic_map_free(bmap);
3626 for (i = 0; i < n; ++i) {
3627 new_div[i] = bmap->block2.data +
3628 (bmap->extra + i) * (1 + row_size);
3629 isl_seq_clr(new_div[i], 1 + row_size);
3631 for (i = 0; i < bmap->extra; ++i)
3632 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3633 free(bmap->div);
3634 bmap->div = new_div;
3635 bmap->n_div += n;
3636 bmap->extra += n;
3638 return bmap;
3641 /* Drop constraints from "bmap" that only involve the variables
3642 * of "type" in the range [first, first + n] that are not related
3643 * to any of the variables outside that interval.
3644 * These constraints cannot influence the values for the variables
3645 * outside the interval, except in case they cause "bmap" to be empty.
3646 * Only drop the constraints if "bmap" is known to be non-empty.
3648 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3649 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3650 unsigned first, unsigned n)
3652 int i;
3653 int *groups;
3654 unsigned dim, n_div;
3655 isl_bool non_empty;
3657 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3658 if (non_empty < 0)
3659 return isl_basic_map_free(bmap);
3660 if (!non_empty)
3661 return bmap;
3663 dim = isl_basic_map_dim(bmap, isl_dim_all);
3664 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3665 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3666 if (!groups)
3667 return isl_basic_map_free(bmap);
3668 first += isl_basic_map_offset(bmap, type) - 1;
3669 for (i = 0; i < first; ++i)
3670 groups[i] = -1;
3671 for (i = first + n; i < dim - n_div; ++i)
3672 groups[i] = -1;
3674 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3676 return bmap;
3679 /* Turn the n dimensions of type type, starting at first
3680 * into existentially quantified variables.
3682 * If a subset of the projected out variables are unrelated
3683 * to any of the variables that remain, then the constraints
3684 * involving this subset are simply dropped first.
3686 __isl_give isl_basic_map *isl_basic_map_project_out(
3687 __isl_take isl_basic_map *bmap,
3688 enum isl_dim_type type, unsigned first, unsigned n)
3690 if (n == 0)
3691 return basic_map_space_reset(bmap, type);
3692 if (type == isl_dim_div)
3693 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3694 "cannot project out existentially quantified variables",
3695 return isl_basic_map_free(bmap));
3697 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3698 if (!bmap)
3699 return NULL;
3701 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3702 return isl_basic_map_remove_dims(bmap, type, first, n);
3704 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3705 goto error);
3707 bmap = move_last(bmap, type, first, n);
3708 bmap = isl_basic_map_cow(bmap);
3709 bmap = insert_div_rows(bmap, n);
3710 if (!bmap)
3711 return NULL;
3713 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3714 if (!bmap->dim)
3715 goto error;
3716 bmap = isl_basic_map_simplify(bmap);
3717 bmap = isl_basic_map_drop_redundant_divs(bmap);
3718 return isl_basic_map_finalize(bmap);
3719 error:
3720 isl_basic_map_free(bmap);
3721 return NULL;
3724 /* Turn the n dimensions of type type, starting at first
3725 * into existentially quantified variables.
3727 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3728 enum isl_dim_type type, unsigned first, unsigned n)
3730 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3731 type, first, n));
3734 /* Turn the n dimensions of type type, starting at first
3735 * into existentially quantified variables.
3737 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3738 enum isl_dim_type type, unsigned first, unsigned n)
3740 int i;
3742 if (!map)
3743 return NULL;
3745 if (n == 0)
3746 return map_space_reset(map, type);
3748 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3750 map = isl_map_cow(map);
3751 if (!map)
3752 return NULL;
3754 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3755 if (!map->dim)
3756 goto error;
3758 for (i = 0; i < map->n; ++i) {
3759 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3760 if (!map->p[i])
3761 goto error;
3764 return map;
3765 error:
3766 isl_map_free(map);
3767 return NULL;
3770 /* Turn the n dimensions of type type, starting at first
3771 * into existentially quantified variables.
3773 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3774 enum isl_dim_type type, unsigned first, unsigned n)
3776 return set_from_map(isl_map_project_out(set_to_map(set),
3777 type, first, n));
3780 /* Return a map that projects the elements in "set" onto their
3781 * "n" set dimensions starting at "first".
3782 * "type" should be equal to isl_dim_set.
3784 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3785 enum isl_dim_type type, unsigned first, unsigned n)
3787 int i;
3788 int dim;
3789 isl_map *map;
3791 if (!set)
3792 return NULL;
3793 if (type != isl_dim_set)
3794 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3795 "only set dimensions can be projected out", goto error);
3796 dim = isl_set_dim(set, isl_dim_set);
3797 if (first + n > dim || first + n < first)
3798 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3799 "index out of bounds", goto error);
3801 map = isl_map_from_domain(set);
3802 map = isl_map_add_dims(map, isl_dim_out, n);
3803 for (i = 0; i < n; ++i)
3804 map = isl_map_equate(map, isl_dim_in, first + i,
3805 isl_dim_out, i);
3806 return map;
3807 error:
3808 isl_set_free(set);
3809 return NULL;
3812 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3814 int i, j;
3816 for (i = 0; i < n; ++i) {
3817 j = isl_basic_map_alloc_div(bmap);
3818 if (j < 0)
3819 goto error;
3820 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3822 return bmap;
3823 error:
3824 isl_basic_map_free(bmap);
3825 return NULL;
3828 struct isl_basic_map *isl_basic_map_apply_range(
3829 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3831 isl_space *dim_result = NULL;
3832 struct isl_basic_map *bmap;
3833 unsigned n_in, n_out, n, nparam, total, pos;
3834 struct isl_dim_map *dim_map1, *dim_map2;
3836 if (!bmap1 || !bmap2)
3837 goto error;
3838 if (!isl_space_match(bmap1->dim, isl_dim_param,
3839 bmap2->dim, isl_dim_param))
3840 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3841 "parameters don't match", goto error);
3842 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3843 bmap2->dim, isl_dim_in))
3844 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3845 "spaces don't match", goto error);
3847 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3848 isl_space_copy(bmap2->dim));
3850 n_in = isl_basic_map_n_in(bmap1);
3851 n_out = isl_basic_map_n_out(bmap2);
3852 n = isl_basic_map_n_out(bmap1);
3853 nparam = isl_basic_map_n_param(bmap1);
3855 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3856 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3857 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3858 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3859 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3860 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3861 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3862 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3863 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3864 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3865 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3867 bmap = isl_basic_map_alloc_space(dim_result,
3868 bmap1->n_div + bmap2->n_div + n,
3869 bmap1->n_eq + bmap2->n_eq,
3870 bmap1->n_ineq + bmap2->n_ineq);
3871 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3872 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3873 bmap = add_divs(bmap, n);
3874 bmap = isl_basic_map_simplify(bmap);
3875 bmap = isl_basic_map_drop_redundant_divs(bmap);
3876 return isl_basic_map_finalize(bmap);
3877 error:
3878 isl_basic_map_free(bmap1);
3879 isl_basic_map_free(bmap2);
3880 return NULL;
3883 struct isl_basic_set *isl_basic_set_apply(
3884 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3886 if (!bset || !bmap)
3887 goto error;
3889 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3890 goto error);
3892 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
3893 bmap));
3894 error:
3895 isl_basic_set_free(bset);
3896 isl_basic_map_free(bmap);
3897 return NULL;
3900 struct isl_basic_map *isl_basic_map_apply_domain(
3901 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3903 if (!bmap1 || !bmap2)
3904 goto error;
3906 isl_assert(bmap1->ctx,
3907 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3908 isl_assert(bmap1->ctx,
3909 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3910 goto error);
3912 bmap1 = isl_basic_map_reverse(bmap1);
3913 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3914 return isl_basic_map_reverse(bmap1);
3915 error:
3916 isl_basic_map_free(bmap1);
3917 isl_basic_map_free(bmap2);
3918 return NULL;
3921 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3922 * A \cap B -> f(A) + f(B)
3924 struct isl_basic_map *isl_basic_map_sum(
3925 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3927 unsigned n_in, n_out, nparam, total, pos;
3928 struct isl_basic_map *bmap = NULL;
3929 struct isl_dim_map *dim_map1, *dim_map2;
3930 int i;
3932 if (!bmap1 || !bmap2)
3933 goto error;
3935 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3936 goto error);
3938 nparam = isl_basic_map_n_param(bmap1);
3939 n_in = isl_basic_map_n_in(bmap1);
3940 n_out = isl_basic_map_n_out(bmap1);
3942 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3943 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3944 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3945 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3946 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3947 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3948 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3949 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3950 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3951 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3952 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3954 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3955 bmap1->n_div + bmap2->n_div + 2 * n_out,
3956 bmap1->n_eq + bmap2->n_eq + n_out,
3957 bmap1->n_ineq + bmap2->n_ineq);
3958 for (i = 0; i < n_out; ++i) {
3959 int j = isl_basic_map_alloc_equality(bmap);
3960 if (j < 0)
3961 goto error;
3962 isl_seq_clr(bmap->eq[j], 1+total);
3963 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3964 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3965 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3967 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3968 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3969 bmap = add_divs(bmap, 2 * n_out);
3971 bmap = isl_basic_map_simplify(bmap);
3972 return isl_basic_map_finalize(bmap);
3973 error:
3974 isl_basic_map_free(bmap);
3975 isl_basic_map_free(bmap1);
3976 isl_basic_map_free(bmap2);
3977 return NULL;
3980 /* Given two maps A -> f(A) and B -> g(B), construct a map
3981 * A \cap B -> f(A) + f(B)
3983 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3985 struct isl_map *result;
3986 int i, j;
3988 if (!map1 || !map2)
3989 goto error;
3991 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3993 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3994 map1->n * map2->n, 0);
3995 if (!result)
3996 goto error;
3997 for (i = 0; i < map1->n; ++i)
3998 for (j = 0; j < map2->n; ++j) {
3999 struct isl_basic_map *part;
4000 part = isl_basic_map_sum(
4001 isl_basic_map_copy(map1->p[i]),
4002 isl_basic_map_copy(map2->p[j]));
4003 if (isl_basic_map_is_empty(part))
4004 isl_basic_map_free(part);
4005 else
4006 result = isl_map_add_basic_map(result, part);
4007 if (!result)
4008 goto error;
4010 isl_map_free(map1);
4011 isl_map_free(map2);
4012 return result;
4013 error:
4014 isl_map_free(map1);
4015 isl_map_free(map2);
4016 return NULL;
4019 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4020 __isl_take isl_set *set2)
4022 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4025 /* Given a basic map A -> f(A), construct A -> -f(A).
4027 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4029 int i, j;
4030 unsigned off, n;
4032 bmap = isl_basic_map_cow(bmap);
4033 if (!bmap)
4034 return NULL;
4036 n = isl_basic_map_dim(bmap, isl_dim_out);
4037 off = isl_basic_map_offset(bmap, isl_dim_out);
4038 for (i = 0; i < bmap->n_eq; ++i)
4039 for (j = 0; j < n; ++j)
4040 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4041 for (i = 0; i < bmap->n_ineq; ++i)
4042 for (j = 0; j < n; ++j)
4043 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4044 for (i = 0; i < bmap->n_div; ++i)
4045 for (j = 0; j < n; ++j)
4046 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4047 bmap = isl_basic_map_gauss(bmap, NULL);
4048 return isl_basic_map_finalize(bmap);
4051 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4053 return isl_basic_map_neg(bset);
4056 /* Given a map A -> f(A), construct A -> -f(A).
4058 struct isl_map *isl_map_neg(struct isl_map *map)
4060 int i;
4062 map = isl_map_cow(map);
4063 if (!map)
4064 return NULL;
4066 for (i = 0; i < map->n; ++i) {
4067 map->p[i] = isl_basic_map_neg(map->p[i]);
4068 if (!map->p[i])
4069 goto error;
4072 return map;
4073 error:
4074 isl_map_free(map);
4075 return NULL;
4078 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4080 return set_from_map(isl_map_neg(set_to_map(set)));
4083 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4084 * A -> floor(f(A)/d).
4086 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4087 isl_int d)
4089 unsigned n_in, n_out, nparam, total, pos;
4090 struct isl_basic_map *result = NULL;
4091 struct isl_dim_map *dim_map;
4092 int i;
4094 if (!bmap)
4095 return NULL;
4097 nparam = isl_basic_map_n_param(bmap);
4098 n_in = isl_basic_map_n_in(bmap);
4099 n_out = isl_basic_map_n_out(bmap);
4101 total = nparam + n_in + n_out + bmap->n_div + n_out;
4102 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4103 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4104 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4105 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4106 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4108 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4109 bmap->n_div + n_out,
4110 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4111 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4112 result = add_divs(result, n_out);
4113 for (i = 0; i < n_out; ++i) {
4114 int j;
4115 j = isl_basic_map_alloc_inequality(result);
4116 if (j < 0)
4117 goto error;
4118 isl_seq_clr(result->ineq[j], 1+total);
4119 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4120 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4121 j = isl_basic_map_alloc_inequality(result);
4122 if (j < 0)
4123 goto error;
4124 isl_seq_clr(result->ineq[j], 1+total);
4125 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4126 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4127 isl_int_sub_ui(result->ineq[j][0], d, 1);
4130 result = isl_basic_map_simplify(result);
4131 return isl_basic_map_finalize(result);
4132 error:
4133 isl_basic_map_free(result);
4134 return NULL;
4137 /* Given a map A -> f(A) and an integer d, construct a map
4138 * A -> floor(f(A)/d).
4140 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4142 int i;
4144 map = isl_map_cow(map);
4145 if (!map)
4146 return NULL;
4148 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4149 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4150 for (i = 0; i < map->n; ++i) {
4151 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4152 if (!map->p[i])
4153 goto error;
4156 return map;
4157 error:
4158 isl_map_free(map);
4159 return NULL;
4162 /* Given a map A -> f(A) and an integer d, construct a map
4163 * A -> floor(f(A)/d).
4165 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4166 __isl_take isl_val *d)
4168 if (!map || !d)
4169 goto error;
4170 if (!isl_val_is_int(d))
4171 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4172 "expecting integer denominator", goto error);
4173 map = isl_map_floordiv(map, d->n);
4174 isl_val_free(d);
4175 return map;
4176 error:
4177 isl_map_free(map);
4178 isl_val_free(d);
4179 return NULL;
4182 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4184 int i;
4185 unsigned nparam;
4186 unsigned n_in;
4188 i = isl_basic_map_alloc_equality(bmap);
4189 if (i < 0)
4190 goto error;
4191 nparam = isl_basic_map_n_param(bmap);
4192 n_in = isl_basic_map_n_in(bmap);
4193 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4194 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4195 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4196 return isl_basic_map_finalize(bmap);
4197 error:
4198 isl_basic_map_free(bmap);
4199 return NULL;
4202 /* Add a constraint to "bmap" expressing i_pos < o_pos
4204 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4206 int i;
4207 unsigned nparam;
4208 unsigned n_in;
4210 i = isl_basic_map_alloc_inequality(bmap);
4211 if (i < 0)
4212 goto error;
4213 nparam = isl_basic_map_n_param(bmap);
4214 n_in = isl_basic_map_n_in(bmap);
4215 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4216 isl_int_set_si(bmap->ineq[i][0], -1);
4217 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4218 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4219 return isl_basic_map_finalize(bmap);
4220 error:
4221 isl_basic_map_free(bmap);
4222 return NULL;
4225 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4227 static __isl_give isl_basic_map *var_less_or_equal(
4228 __isl_take isl_basic_map *bmap, unsigned pos)
4230 int i;
4231 unsigned nparam;
4232 unsigned n_in;
4234 i = isl_basic_map_alloc_inequality(bmap);
4235 if (i < 0)
4236 goto error;
4237 nparam = isl_basic_map_n_param(bmap);
4238 n_in = isl_basic_map_n_in(bmap);
4239 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4240 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4241 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4242 return isl_basic_map_finalize(bmap);
4243 error:
4244 isl_basic_map_free(bmap);
4245 return NULL;
4248 /* Add a constraint to "bmap" expressing i_pos > o_pos
4250 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4252 int i;
4253 unsigned nparam;
4254 unsigned n_in;
4256 i = isl_basic_map_alloc_inequality(bmap);
4257 if (i < 0)
4258 goto error;
4259 nparam = isl_basic_map_n_param(bmap);
4260 n_in = isl_basic_map_n_in(bmap);
4261 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4262 isl_int_set_si(bmap->ineq[i][0], -1);
4263 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4264 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4265 return isl_basic_map_finalize(bmap);
4266 error:
4267 isl_basic_map_free(bmap);
4268 return NULL;
4271 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4273 static __isl_give isl_basic_map *var_more_or_equal(
4274 __isl_take isl_basic_map *bmap, unsigned pos)
4276 int i;
4277 unsigned nparam;
4278 unsigned n_in;
4280 i = isl_basic_map_alloc_inequality(bmap);
4281 if (i < 0)
4282 goto error;
4283 nparam = isl_basic_map_n_param(bmap);
4284 n_in = isl_basic_map_n_in(bmap);
4285 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4286 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4287 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4288 return isl_basic_map_finalize(bmap);
4289 error:
4290 isl_basic_map_free(bmap);
4291 return NULL;
4294 __isl_give isl_basic_map *isl_basic_map_equal(
4295 __isl_take isl_space *dim, unsigned n_equal)
4297 int i;
4298 struct isl_basic_map *bmap;
4299 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4300 if (!bmap)
4301 return NULL;
4302 for (i = 0; i < n_equal && bmap; ++i)
4303 bmap = var_equal(bmap, i);
4304 return isl_basic_map_finalize(bmap);
4307 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4309 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4310 unsigned pos)
4312 int i;
4313 struct isl_basic_map *bmap;
4314 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4315 if (!bmap)
4316 return NULL;
4317 for (i = 0; i < pos && bmap; ++i)
4318 bmap = var_equal(bmap, i);
4319 if (bmap)
4320 bmap = var_less(bmap, pos);
4321 return isl_basic_map_finalize(bmap);
4324 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4326 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4327 __isl_take isl_space *dim, unsigned pos)
4329 int i;
4330 isl_basic_map *bmap;
4332 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4333 for (i = 0; i < pos; ++i)
4334 bmap = var_equal(bmap, i);
4335 bmap = var_less_or_equal(bmap, pos);
4336 return isl_basic_map_finalize(bmap);
4339 /* Return a relation on "dim" expressing i_pos > o_pos
4341 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4342 unsigned pos)
4344 int i;
4345 struct isl_basic_map *bmap;
4346 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4347 if (!bmap)
4348 return NULL;
4349 for (i = 0; i < pos && bmap; ++i)
4350 bmap = var_equal(bmap, i);
4351 if (bmap)
4352 bmap = var_more(bmap, pos);
4353 return isl_basic_map_finalize(bmap);
4356 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4358 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4359 __isl_take isl_space *dim, unsigned pos)
4361 int i;
4362 isl_basic_map *bmap;
4364 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4365 for (i = 0; i < pos; ++i)
4366 bmap = var_equal(bmap, i);
4367 bmap = var_more_or_equal(bmap, pos);
4368 return isl_basic_map_finalize(bmap);
4371 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4372 unsigned n, int equal)
4374 struct isl_map *map;
4375 int i;
4377 if (n == 0 && equal)
4378 return isl_map_universe(dims);
4380 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4382 for (i = 0; i + 1 < n; ++i)
4383 map = isl_map_add_basic_map(map,
4384 isl_basic_map_less_at(isl_space_copy(dims), i));
4385 if (n > 0) {
4386 if (equal)
4387 map = isl_map_add_basic_map(map,
4388 isl_basic_map_less_or_equal_at(dims, n - 1));
4389 else
4390 map = isl_map_add_basic_map(map,
4391 isl_basic_map_less_at(dims, n - 1));
4392 } else
4393 isl_space_free(dims);
4395 return map;
4398 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4400 if (!dims)
4401 return NULL;
4402 return map_lex_lte_first(dims, dims->n_out, equal);
4405 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4407 return map_lex_lte_first(dim, n, 0);
4410 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4412 return map_lex_lte_first(dim, n, 1);
4415 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4417 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4420 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4422 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4425 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4426 unsigned n, int equal)
4428 struct isl_map *map;
4429 int i;
4431 if (n == 0 && equal)
4432 return isl_map_universe(dims);
4434 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4436 for (i = 0; i + 1 < n; ++i)
4437 map = isl_map_add_basic_map(map,
4438 isl_basic_map_more_at(isl_space_copy(dims), i));
4439 if (n > 0) {
4440 if (equal)
4441 map = isl_map_add_basic_map(map,
4442 isl_basic_map_more_or_equal_at(dims, n - 1));
4443 else
4444 map = isl_map_add_basic_map(map,
4445 isl_basic_map_more_at(dims, n - 1));
4446 } else
4447 isl_space_free(dims);
4449 return map;
4452 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4454 if (!dims)
4455 return NULL;
4456 return map_lex_gte_first(dims, dims->n_out, equal);
4459 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4461 return map_lex_gte_first(dim, n, 0);
4464 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4466 return map_lex_gte_first(dim, n, 1);
4469 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4471 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4474 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4476 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4479 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4480 __isl_take isl_set *set2)
4482 isl_map *map;
4483 map = isl_map_lex_le(isl_set_get_space(set1));
4484 map = isl_map_intersect_domain(map, set1);
4485 map = isl_map_intersect_range(map, set2);
4486 return map;
4489 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4490 __isl_take isl_set *set2)
4492 isl_map *map;
4493 map = isl_map_lex_lt(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_ge_set(__isl_take isl_set *set1,
4500 __isl_take isl_set *set2)
4502 isl_map *map;
4503 map = isl_map_lex_ge(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_gt_set(__isl_take isl_set *set1,
4510 __isl_take isl_set *set2)
4512 isl_map *map;
4513 map = isl_map_lex_gt(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_map_lex_le_map(__isl_take isl_map *map1,
4520 __isl_take isl_map *map2)
4522 isl_map *map;
4523 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4524 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4525 map = isl_map_apply_range(map, isl_map_reverse(map2));
4526 return map;
4529 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4530 __isl_take isl_map *map2)
4532 isl_map *map;
4533 map = isl_map_lex_lt(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_ge_map(__isl_take isl_map *map1,
4540 __isl_take isl_map *map2)
4542 isl_map *map;
4543 map = isl_map_lex_ge(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_gt_map(__isl_take isl_map *map1,
4550 __isl_take isl_map *map2)
4552 isl_map *map;
4553 map = isl_map_lex_gt(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 static __isl_give isl_basic_map *basic_map_from_basic_set(
4560 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4562 struct isl_basic_map *bmap;
4564 bset = isl_basic_set_cow(bset);
4565 if (!bset || !dim)
4566 goto error;
4568 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4569 goto error);
4570 isl_space_free(bset->dim);
4571 bmap = bset_to_bmap(bset);
4572 bmap->dim = dim;
4573 return isl_basic_map_finalize(bmap);
4574 error:
4575 isl_basic_set_free(bset);
4576 isl_space_free(dim);
4577 return NULL;
4580 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4581 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4583 return basic_map_from_basic_set(bset, space);
4586 /* For a div d = floor(f/m), add the constraint
4588 * f - m d >= 0
4590 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4591 unsigned pos, isl_int *div)
4593 int i;
4594 unsigned total = isl_basic_map_total_dim(bmap);
4596 i = isl_basic_map_alloc_inequality(bmap);
4597 if (i < 0)
4598 return -1;
4599 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4600 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4602 return 0;
4605 /* For a div d = floor(f/m), add the constraint
4607 * -(f-(m-1)) + m d >= 0
4609 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4610 unsigned pos, isl_int *div)
4612 int i;
4613 unsigned total = isl_basic_map_total_dim(bmap);
4615 i = isl_basic_map_alloc_inequality(bmap);
4616 if (i < 0)
4617 return -1;
4618 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4619 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4620 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4621 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4623 return 0;
4626 /* For a div d = floor(f/m), add the constraints
4628 * f - m d >= 0
4629 * -(f-(m-1)) + m d >= 0
4631 * Note that the second constraint is the negation of
4633 * f - m d >= m
4635 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4636 unsigned pos, isl_int *div)
4638 if (add_upper_div_constraint(bmap, pos, div) < 0)
4639 return -1;
4640 if (add_lower_div_constraint(bmap, pos, div) < 0)
4641 return -1;
4642 return 0;
4645 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4646 unsigned pos, isl_int *div)
4648 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4649 pos, div);
4652 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4654 unsigned total = isl_basic_map_total_dim(bmap);
4655 unsigned div_pos = total - bmap->n_div + div;
4657 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4658 bmap->div[div]);
4661 /* For each known div d = floor(f/m), add the constraints
4663 * f - m d >= 0
4664 * -(f-(m-1)) + m d >= 0
4666 * Remove duplicate constraints in case of some these div constraints
4667 * already appear in "bmap".
4669 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4670 __isl_take isl_basic_map *bmap)
4672 unsigned n_div;
4674 if (!bmap)
4675 return NULL;
4676 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4677 if (n_div == 0)
4678 return bmap;
4680 bmap = add_known_div_constraints(bmap);
4681 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4682 bmap = isl_basic_map_finalize(bmap);
4683 return bmap;
4686 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4688 * In particular, if this div is of the form d = floor(f/m),
4689 * then add the constraint
4691 * f - m d >= 0
4693 * if sign < 0 or the constraint
4695 * -(f-(m-1)) + m d >= 0
4697 * if sign > 0.
4699 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4700 unsigned div, int sign)
4702 unsigned total;
4703 unsigned div_pos;
4705 if (!bmap)
4706 return -1;
4708 total = isl_basic_map_total_dim(bmap);
4709 div_pos = total - bmap->n_div + div;
4711 if (sign < 0)
4712 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4713 else
4714 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4717 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4719 return isl_basic_map_add_div_constraints(bset, div);
4722 struct isl_basic_set *isl_basic_map_underlying_set(
4723 struct isl_basic_map *bmap)
4725 if (!bmap)
4726 goto error;
4727 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4728 bmap->n_div == 0 &&
4729 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4730 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4731 return bset_from_bmap(bmap);
4732 bmap = isl_basic_map_cow(bmap);
4733 if (!bmap)
4734 goto error;
4735 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4736 if (!bmap->dim)
4737 goto error;
4738 bmap->extra -= bmap->n_div;
4739 bmap->n_div = 0;
4740 bmap = isl_basic_map_finalize(bmap);
4741 return bset_from_bmap(bmap);
4742 error:
4743 isl_basic_map_free(bmap);
4744 return NULL;
4747 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4748 __isl_take isl_basic_set *bset)
4750 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4753 /* Replace each element in "list" by the result of applying
4754 * isl_basic_map_underlying_set to the element.
4756 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4757 __isl_take isl_basic_map_list *list)
4759 int i, n;
4761 if (!list)
4762 return NULL;
4764 n = isl_basic_map_list_n_basic_map(list);
4765 for (i = 0; i < n; ++i) {
4766 isl_basic_map *bmap;
4767 isl_basic_set *bset;
4769 bmap = isl_basic_map_list_get_basic_map(list, i);
4770 bset = isl_basic_set_underlying_set(bmap);
4771 list = isl_basic_set_list_set_basic_set(list, i, bset);
4774 return list;
4777 struct isl_basic_map *isl_basic_map_overlying_set(
4778 struct isl_basic_set *bset, struct isl_basic_map *like)
4780 struct isl_basic_map *bmap;
4781 struct isl_ctx *ctx;
4782 unsigned total;
4783 int i;
4785 if (!bset || !like)
4786 goto error;
4787 ctx = bset->ctx;
4788 isl_assert(ctx, bset->n_div == 0, goto error);
4789 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4790 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4791 goto error);
4792 if (like->n_div == 0) {
4793 isl_space *space = isl_basic_map_get_space(like);
4794 isl_basic_map_free(like);
4795 return isl_basic_map_reset_space(bset, space);
4797 bset = isl_basic_set_cow(bset);
4798 if (!bset)
4799 goto error;
4800 total = bset->dim->n_out + bset->extra;
4801 bmap = bset_to_bmap(bset);
4802 isl_space_free(bmap->dim);
4803 bmap->dim = isl_space_copy(like->dim);
4804 if (!bmap->dim)
4805 goto error;
4806 bmap->n_div = like->n_div;
4807 bmap->extra += like->n_div;
4808 if (bmap->extra) {
4809 unsigned ltotal;
4810 isl_int **div;
4811 ltotal = total - bmap->extra + like->extra;
4812 if (ltotal > total)
4813 ltotal = total;
4814 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4815 bmap->extra * (1 + 1 + total));
4816 if (isl_blk_is_error(bmap->block2))
4817 goto error;
4818 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4819 if (!div)
4820 goto error;
4821 bmap->div = div;
4822 for (i = 0; i < bmap->extra; ++i)
4823 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4824 for (i = 0; i < like->n_div; ++i) {
4825 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4826 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4828 bmap = isl_basic_map_add_known_div_constraints(bmap);
4830 isl_basic_map_free(like);
4831 bmap = isl_basic_map_simplify(bmap);
4832 bmap = isl_basic_map_finalize(bmap);
4833 return bmap;
4834 error:
4835 isl_basic_map_free(like);
4836 isl_basic_set_free(bset);
4837 return NULL;
4840 struct isl_basic_set *isl_basic_set_from_underlying_set(
4841 struct isl_basic_set *bset, struct isl_basic_set *like)
4843 return bset_from_bmap(isl_basic_map_overlying_set(bset,
4844 bset_to_bmap(like)));
4847 struct isl_set *isl_set_from_underlying_set(
4848 struct isl_set *set, struct isl_basic_set *like)
4850 int i;
4852 if (!set || !like)
4853 goto error;
4854 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4855 goto error);
4856 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4857 isl_basic_set_free(like);
4858 return set;
4860 set = isl_set_cow(set);
4861 if (!set)
4862 goto error;
4863 for (i = 0; i < set->n; ++i) {
4864 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4865 isl_basic_set_copy(like));
4866 if (!set->p[i])
4867 goto error;
4869 isl_space_free(set->dim);
4870 set->dim = isl_space_copy(like->dim);
4871 if (!set->dim)
4872 goto error;
4873 isl_basic_set_free(like);
4874 return set;
4875 error:
4876 isl_basic_set_free(like);
4877 isl_set_free(set);
4878 return NULL;
4881 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4883 int i;
4885 map = isl_map_cow(map);
4886 if (!map)
4887 return NULL;
4888 map->dim = isl_space_cow(map->dim);
4889 if (!map->dim)
4890 goto error;
4892 for (i = 1; i < map->n; ++i)
4893 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4894 goto error);
4895 for (i = 0; i < map->n; ++i) {
4896 map->p[i] = bset_to_bmap(
4897 isl_basic_map_underlying_set(map->p[i]));
4898 if (!map->p[i])
4899 goto error;
4901 if (map->n == 0)
4902 map->dim = isl_space_underlying(map->dim, 0);
4903 else {
4904 isl_space_free(map->dim);
4905 map->dim = isl_space_copy(map->p[0]->dim);
4907 if (!map->dim)
4908 goto error;
4909 return set_from_map(map);
4910 error:
4911 isl_map_free(map);
4912 return NULL;
4915 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4917 return set_from_map(isl_map_underlying_set(set_to_map(set)));
4920 /* Replace the space of "bmap" by "space".
4922 * If the space of "bmap" is identical to "space" (including the identifiers
4923 * of the input and output dimensions), then simply return the original input.
4925 __isl_give isl_basic_map *isl_basic_map_reset_space(
4926 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4928 isl_bool equal;
4930 if (!bmap)
4931 goto error;
4932 equal = isl_space_is_equal(bmap->dim, space);
4933 if (equal >= 0 && equal)
4934 equal = isl_space_match(bmap->dim, isl_dim_in,
4935 space, isl_dim_in);
4936 if (equal >= 0 && equal)
4937 equal = isl_space_match(bmap->dim, isl_dim_out,
4938 space, isl_dim_out);
4939 if (equal < 0)
4940 goto error;
4941 if (equal) {
4942 isl_space_free(space);
4943 return bmap;
4945 bmap = isl_basic_map_cow(bmap);
4946 if (!bmap || !space)
4947 goto error;
4949 isl_space_free(bmap->dim);
4950 bmap->dim = space;
4952 bmap = isl_basic_map_finalize(bmap);
4954 return bmap;
4955 error:
4956 isl_basic_map_free(bmap);
4957 isl_space_free(space);
4958 return NULL;
4961 __isl_give isl_basic_set *isl_basic_set_reset_space(
4962 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4964 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
4965 dim));
4968 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4969 __isl_take isl_space *dim)
4971 int i;
4973 map = isl_map_cow(map);
4974 if (!map || !dim)
4975 goto error;
4977 for (i = 0; i < map->n; ++i) {
4978 map->p[i] = isl_basic_map_reset_space(map->p[i],
4979 isl_space_copy(dim));
4980 if (!map->p[i])
4981 goto error;
4983 isl_space_free(map->dim);
4984 map->dim = dim;
4986 return map;
4987 error:
4988 isl_map_free(map);
4989 isl_space_free(dim);
4990 return NULL;
4993 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4994 __isl_take isl_space *dim)
4996 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
4999 /* Compute the parameter domain of the given basic set.
5001 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5003 isl_space *space;
5004 unsigned n;
5006 if (isl_basic_set_is_params(bset))
5007 return bset;
5009 n = isl_basic_set_dim(bset, isl_dim_set);
5010 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5011 space = isl_basic_set_get_space(bset);
5012 space = isl_space_params(space);
5013 bset = isl_basic_set_reset_space(bset, space);
5014 return bset;
5017 /* Construct a zero-dimensional basic set with the given parameter domain.
5019 __isl_give isl_basic_set *isl_basic_set_from_params(
5020 __isl_take isl_basic_set *bset)
5022 isl_space *space;
5023 space = isl_basic_set_get_space(bset);
5024 space = isl_space_set_from_params(space);
5025 bset = isl_basic_set_reset_space(bset, space);
5026 return bset;
5029 /* Compute the parameter domain of the given set.
5031 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5033 isl_space *space;
5034 unsigned n;
5036 if (isl_set_is_params(set))
5037 return set;
5039 n = isl_set_dim(set, isl_dim_set);
5040 set = isl_set_project_out(set, isl_dim_set, 0, n);
5041 space = isl_set_get_space(set);
5042 space = isl_space_params(space);
5043 set = isl_set_reset_space(set, space);
5044 return set;
5047 /* Construct a zero-dimensional set with the given parameter domain.
5049 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5051 isl_space *space;
5052 space = isl_set_get_space(set);
5053 space = isl_space_set_from_params(space);
5054 set = isl_set_reset_space(set, space);
5055 return set;
5058 /* Compute the parameter domain of the given map.
5060 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5062 isl_space *space;
5063 unsigned n;
5065 n = isl_map_dim(map, isl_dim_in);
5066 map = isl_map_project_out(map, isl_dim_in, 0, n);
5067 n = isl_map_dim(map, isl_dim_out);
5068 map = isl_map_project_out(map, isl_dim_out, 0, n);
5069 space = isl_map_get_space(map);
5070 space = isl_space_params(space);
5071 map = isl_map_reset_space(map, space);
5072 return map;
5075 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5077 isl_space *space;
5078 unsigned n_out;
5080 if (!bmap)
5081 return NULL;
5082 space = isl_space_domain(isl_basic_map_get_space(bmap));
5084 n_out = isl_basic_map_n_out(bmap);
5085 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5087 return isl_basic_map_reset_space(bmap, space);
5090 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5092 if (!bmap)
5093 return -1;
5094 return isl_space_may_be_set(bmap->dim);
5097 /* Is this basic map actually a set?
5098 * Users should never call this function. Outside of isl,
5099 * the type should indicate whether something is a set or a map.
5101 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5103 if (!bmap)
5104 return -1;
5105 return isl_space_is_set(bmap->dim);
5108 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5110 if (!bmap)
5111 return NULL;
5112 if (isl_basic_map_is_set(bmap))
5113 return bmap;
5114 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5117 __isl_give isl_basic_map *isl_basic_map_domain_map(
5118 __isl_take isl_basic_map *bmap)
5120 int i, k;
5121 isl_space *dim;
5122 isl_basic_map *domain;
5123 int nparam, n_in, n_out;
5124 unsigned total;
5126 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5127 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5128 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5130 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5131 domain = isl_basic_map_universe(dim);
5133 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5134 bmap = isl_basic_map_apply_range(bmap, domain);
5135 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5137 total = isl_basic_map_total_dim(bmap);
5139 for (i = 0; i < n_in; ++i) {
5140 k = isl_basic_map_alloc_equality(bmap);
5141 if (k < 0)
5142 goto error;
5143 isl_seq_clr(bmap->eq[k], 1 + total);
5144 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5145 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5148 bmap = isl_basic_map_gauss(bmap, NULL);
5149 return isl_basic_map_finalize(bmap);
5150 error:
5151 isl_basic_map_free(bmap);
5152 return NULL;
5155 __isl_give isl_basic_map *isl_basic_map_range_map(
5156 __isl_take isl_basic_map *bmap)
5158 int i, k;
5159 isl_space *dim;
5160 isl_basic_map *range;
5161 int nparam, n_in, n_out;
5162 unsigned total;
5164 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5165 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5166 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5168 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5169 range = isl_basic_map_universe(dim);
5171 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5172 bmap = isl_basic_map_apply_range(bmap, range);
5173 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5175 total = isl_basic_map_total_dim(bmap);
5177 for (i = 0; i < n_out; ++i) {
5178 k = isl_basic_map_alloc_equality(bmap);
5179 if (k < 0)
5180 goto error;
5181 isl_seq_clr(bmap->eq[k], 1 + total);
5182 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5183 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5186 bmap = isl_basic_map_gauss(bmap, NULL);
5187 return isl_basic_map_finalize(bmap);
5188 error:
5189 isl_basic_map_free(bmap);
5190 return NULL;
5193 int isl_map_may_be_set(__isl_keep isl_map *map)
5195 if (!map)
5196 return -1;
5197 return isl_space_may_be_set(map->dim);
5200 /* Is this map actually a set?
5201 * Users should never call this function. Outside of isl,
5202 * the type should indicate whether something is a set or a map.
5204 int isl_map_is_set(__isl_keep isl_map *map)
5206 if (!map)
5207 return -1;
5208 return isl_space_is_set(map->dim);
5211 struct isl_set *isl_map_range(struct isl_map *map)
5213 int i;
5214 struct isl_set *set;
5216 if (!map)
5217 goto error;
5218 if (isl_map_is_set(map))
5219 return set_from_map(map);
5221 map = isl_map_cow(map);
5222 if (!map)
5223 goto error;
5225 set = set_from_map(map);
5226 set->dim = isl_space_range(set->dim);
5227 if (!set->dim)
5228 goto error;
5229 for (i = 0; i < map->n; ++i) {
5230 set->p[i] = isl_basic_map_range(map->p[i]);
5231 if (!set->p[i])
5232 goto error;
5234 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5235 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5236 return set;
5237 error:
5238 isl_map_free(map);
5239 return NULL;
5242 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5244 int i;
5246 map = isl_map_cow(map);
5247 if (!map)
5248 return NULL;
5250 map->dim = isl_space_domain_map(map->dim);
5251 if (!map->dim)
5252 goto error;
5253 for (i = 0; i < map->n; ++i) {
5254 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5255 if (!map->p[i])
5256 goto error;
5258 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5259 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5260 return map;
5261 error:
5262 isl_map_free(map);
5263 return NULL;
5266 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5268 int i;
5269 isl_space *range_dim;
5271 map = isl_map_cow(map);
5272 if (!map)
5273 return NULL;
5275 range_dim = isl_space_range(isl_map_get_space(map));
5276 range_dim = isl_space_from_range(range_dim);
5277 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5278 map->dim = isl_space_join(map->dim, range_dim);
5279 if (!map->dim)
5280 goto error;
5281 for (i = 0; i < map->n; ++i) {
5282 map->p[i] = isl_basic_map_range_map(map->p[i]);
5283 if (!map->p[i])
5284 goto error;
5286 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5287 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5288 return map;
5289 error:
5290 isl_map_free(map);
5291 return NULL;
5294 /* Given a wrapped map of the form A[B -> C],
5295 * return the map A[B -> C] -> B.
5297 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5299 isl_id *id;
5300 isl_map *map;
5302 if (!set)
5303 return NULL;
5304 if (!isl_set_has_tuple_id(set))
5305 return isl_map_domain_map(isl_set_unwrap(set));
5307 id = isl_set_get_tuple_id(set);
5308 map = isl_map_domain_map(isl_set_unwrap(set));
5309 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5311 return map;
5314 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5315 __isl_take isl_space *dim)
5317 int i;
5318 struct isl_map *map = NULL;
5320 set = isl_set_cow(set);
5321 if (!set || !dim)
5322 goto error;
5323 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5324 goto error);
5325 map = set_to_map(set);
5326 for (i = 0; i < set->n; ++i) {
5327 map->p[i] = basic_map_from_basic_set(
5328 set->p[i], isl_space_copy(dim));
5329 if (!map->p[i])
5330 goto error;
5332 isl_space_free(map->dim);
5333 map->dim = dim;
5334 return map;
5335 error:
5336 isl_space_free(dim);
5337 isl_set_free(set);
5338 return NULL;
5341 __isl_give isl_basic_map *isl_basic_map_from_domain(
5342 __isl_take isl_basic_set *bset)
5344 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5347 __isl_give isl_basic_map *isl_basic_map_from_range(
5348 __isl_take isl_basic_set *bset)
5350 isl_space *space;
5351 space = isl_basic_set_get_space(bset);
5352 space = isl_space_from_range(space);
5353 bset = isl_basic_set_reset_space(bset, space);
5354 return bset_to_bmap(bset);
5357 /* Create a relation with the given set as range.
5358 * The domain of the created relation is a zero-dimensional
5359 * flat anonymous space.
5361 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5363 isl_space *space;
5364 space = isl_set_get_space(set);
5365 space = isl_space_from_range(space);
5366 set = isl_set_reset_space(set, space);
5367 return set_to_map(set);
5370 /* Create a relation with the given set as domain.
5371 * The range of the created relation is a zero-dimensional
5372 * flat anonymous space.
5374 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5376 return isl_map_reverse(isl_map_from_range(set));
5379 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5380 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5382 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5385 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5386 __isl_take isl_set *range)
5388 return isl_map_apply_range(isl_map_reverse(domain), range);
5391 /* Return a newly allocated isl_map with given space and flags and
5392 * room for "n" basic maps.
5393 * Make sure that all cached information is cleared.
5395 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5396 unsigned flags)
5398 struct isl_map *map;
5400 if (!space)
5401 return NULL;
5402 if (n < 0)
5403 isl_die(space->ctx, isl_error_internal,
5404 "negative number of basic maps", goto error);
5405 map = isl_calloc(space->ctx, struct isl_map,
5406 sizeof(struct isl_map) +
5407 (n - 1) * sizeof(struct isl_basic_map *));
5408 if (!map)
5409 goto error;
5411 map->ctx = space->ctx;
5412 isl_ctx_ref(map->ctx);
5413 map->ref = 1;
5414 map->size = n;
5415 map->n = 0;
5416 map->dim = space;
5417 map->flags = flags;
5418 return map;
5419 error:
5420 isl_space_free(space);
5421 return NULL;
5424 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5425 unsigned nparam, unsigned in, unsigned out, int n,
5426 unsigned flags)
5428 struct isl_map *map;
5429 isl_space *dims;
5431 dims = isl_space_alloc(ctx, nparam, in, out);
5432 if (!dims)
5433 return NULL;
5435 map = isl_map_alloc_space(dims, n, flags);
5436 return map;
5439 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5441 struct isl_basic_map *bmap;
5442 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5443 bmap = isl_basic_map_set_to_empty(bmap);
5444 return bmap;
5447 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5449 struct isl_basic_set *bset;
5450 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5451 bset = isl_basic_set_set_to_empty(bset);
5452 return bset;
5455 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5457 struct isl_basic_map *bmap;
5458 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5459 bmap = isl_basic_map_finalize(bmap);
5460 return bmap;
5463 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5465 struct isl_basic_set *bset;
5466 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5467 bset = isl_basic_set_finalize(bset);
5468 return bset;
5471 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5473 int i;
5474 unsigned total = isl_space_dim(dim, isl_dim_all);
5475 isl_basic_map *bmap;
5477 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5478 for (i = 0; i < total; ++i) {
5479 int k = isl_basic_map_alloc_inequality(bmap);
5480 if (k < 0)
5481 goto error;
5482 isl_seq_clr(bmap->ineq[k], 1 + total);
5483 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5485 return bmap;
5486 error:
5487 isl_basic_map_free(bmap);
5488 return NULL;
5491 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5493 return isl_basic_map_nat_universe(dim);
5496 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5498 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5501 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5503 return isl_map_nat_universe(dim);
5506 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5508 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5511 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5513 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5516 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5518 struct isl_map *map;
5519 if (!dim)
5520 return NULL;
5521 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5522 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5523 return map;
5526 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5528 struct isl_set *set;
5529 if (!dim)
5530 return NULL;
5531 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5532 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5533 return set;
5536 struct isl_map *isl_map_dup(struct isl_map *map)
5538 int i;
5539 struct isl_map *dup;
5541 if (!map)
5542 return NULL;
5543 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5544 for (i = 0; i < map->n; ++i)
5545 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5546 return dup;
5549 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5550 __isl_take isl_basic_map *bmap)
5552 if (!bmap || !map)
5553 goto error;
5554 if (isl_basic_map_plain_is_empty(bmap)) {
5555 isl_basic_map_free(bmap);
5556 return map;
5558 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5559 isl_assert(map->ctx, map->n < map->size, goto error);
5560 map->p[map->n] = bmap;
5561 map->n++;
5562 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5563 return map;
5564 error:
5565 if (map)
5566 isl_map_free(map);
5567 if (bmap)
5568 isl_basic_map_free(bmap);
5569 return NULL;
5572 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5574 int i;
5576 if (!map)
5577 return NULL;
5579 if (--map->ref > 0)
5580 return NULL;
5582 clear_caches(map);
5583 isl_ctx_deref(map->ctx);
5584 for (i = 0; i < map->n; ++i)
5585 isl_basic_map_free(map->p[i]);
5586 isl_space_free(map->dim);
5587 free(map);
5589 return NULL;
5592 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5593 struct isl_basic_map *bmap, unsigned pos, int value)
5595 int j;
5597 bmap = isl_basic_map_cow(bmap);
5598 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5599 j = isl_basic_map_alloc_equality(bmap);
5600 if (j < 0)
5601 goto error;
5602 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5603 isl_int_set_si(bmap->eq[j][pos], -1);
5604 isl_int_set_si(bmap->eq[j][0], value);
5605 bmap = isl_basic_map_simplify(bmap);
5606 return isl_basic_map_finalize(bmap);
5607 error:
5608 isl_basic_map_free(bmap);
5609 return NULL;
5612 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5613 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5615 int j;
5617 bmap = isl_basic_map_cow(bmap);
5618 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5619 j = isl_basic_map_alloc_equality(bmap);
5620 if (j < 0)
5621 goto error;
5622 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5623 isl_int_set_si(bmap->eq[j][pos], -1);
5624 isl_int_set(bmap->eq[j][0], value);
5625 bmap = isl_basic_map_simplify(bmap);
5626 return isl_basic_map_finalize(bmap);
5627 error:
5628 isl_basic_map_free(bmap);
5629 return NULL;
5632 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5633 enum isl_dim_type type, unsigned pos, int value)
5635 if (!bmap)
5636 return NULL;
5637 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5638 return isl_basic_map_fix_pos_si(bmap,
5639 isl_basic_map_offset(bmap, type) + pos, value);
5640 error:
5641 isl_basic_map_free(bmap);
5642 return NULL;
5645 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5646 enum isl_dim_type type, unsigned pos, isl_int value)
5648 if (!bmap)
5649 return NULL;
5650 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5651 return isl_basic_map_fix_pos(bmap,
5652 isl_basic_map_offset(bmap, type) + pos, value);
5653 error:
5654 isl_basic_map_free(bmap);
5655 return NULL;
5658 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5659 * to be equal to "v".
5661 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5662 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5664 if (!bmap || !v)
5665 goto error;
5666 if (!isl_val_is_int(v))
5667 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5668 "expecting integer value", goto error);
5669 if (pos >= isl_basic_map_dim(bmap, type))
5670 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5671 "index out of bounds", goto error);
5672 pos += isl_basic_map_offset(bmap, type);
5673 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5674 isl_val_free(v);
5675 return bmap;
5676 error:
5677 isl_basic_map_free(bmap);
5678 isl_val_free(v);
5679 return NULL;
5682 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5683 * to be equal to "v".
5685 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5686 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5688 return isl_basic_map_fix_val(bset, type, pos, v);
5691 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5692 enum isl_dim_type type, unsigned pos, int value)
5694 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5695 type, pos, value));
5698 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5699 enum isl_dim_type type, unsigned pos, isl_int value)
5701 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5702 type, pos, value));
5705 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5706 unsigned input, int value)
5708 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5711 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5712 unsigned dim, int value)
5714 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5715 isl_dim_set, dim, value));
5718 static int remove_if_empty(__isl_keep isl_map *map, int i)
5720 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5722 if (empty < 0)
5723 return -1;
5724 if (!empty)
5725 return 0;
5727 isl_basic_map_free(map->p[i]);
5728 if (i != map->n - 1) {
5729 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5730 map->p[i] = map->p[map->n - 1];
5732 map->n--;
5734 return 0;
5737 /* Perform "fn" on each basic map of "map", where we may not be holding
5738 * the only reference to "map".
5739 * In particular, "fn" should be a semantics preserving operation
5740 * that we want to apply to all copies of "map". We therefore need
5741 * to be careful not to modify "map" in a way that breaks "map"
5742 * in case anything goes wrong.
5744 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5745 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5747 struct isl_basic_map *bmap;
5748 int i;
5750 if (!map)
5751 return NULL;
5753 for (i = map->n - 1; i >= 0; --i) {
5754 bmap = isl_basic_map_copy(map->p[i]);
5755 bmap = fn(bmap);
5756 if (!bmap)
5757 goto error;
5758 isl_basic_map_free(map->p[i]);
5759 map->p[i] = bmap;
5760 if (remove_if_empty(map, i) < 0)
5761 goto error;
5764 return map;
5765 error:
5766 isl_map_free(map);
5767 return NULL;
5770 struct isl_map *isl_map_fix_si(struct isl_map *map,
5771 enum isl_dim_type type, unsigned pos, int value)
5773 int i;
5775 map = isl_map_cow(map);
5776 if (!map)
5777 return NULL;
5779 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5780 for (i = map->n - 1; i >= 0; --i) {
5781 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5782 if (remove_if_empty(map, i) < 0)
5783 goto error;
5785 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5786 return map;
5787 error:
5788 isl_map_free(map);
5789 return NULL;
5792 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5793 enum isl_dim_type type, unsigned pos, int value)
5795 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5798 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5799 enum isl_dim_type type, unsigned pos, isl_int value)
5801 int i;
5803 map = isl_map_cow(map);
5804 if (!map)
5805 return NULL;
5807 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5808 for (i = 0; i < map->n; ++i) {
5809 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5810 if (!map->p[i])
5811 goto error;
5813 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5814 return map;
5815 error:
5816 isl_map_free(map);
5817 return NULL;
5820 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5821 enum isl_dim_type type, unsigned pos, isl_int value)
5823 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5826 /* Fix the value of the variable at position "pos" of type "type" of "map"
5827 * to be equal to "v".
5829 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5830 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5832 int i;
5834 map = isl_map_cow(map);
5835 if (!map || !v)
5836 goto error;
5838 if (!isl_val_is_int(v))
5839 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5840 "expecting integer value", goto error);
5841 if (pos >= isl_map_dim(map, type))
5842 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5843 "index out of bounds", goto error);
5844 for (i = map->n - 1; i >= 0; --i) {
5845 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5846 isl_val_copy(v));
5847 if (remove_if_empty(map, i) < 0)
5848 goto error;
5850 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5851 isl_val_free(v);
5852 return map;
5853 error:
5854 isl_map_free(map);
5855 isl_val_free(v);
5856 return NULL;
5859 /* Fix the value of the variable at position "pos" of type "type" of "set"
5860 * to be equal to "v".
5862 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5863 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5865 return isl_map_fix_val(set, type, pos, v);
5868 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5869 unsigned input, int value)
5871 return isl_map_fix_si(map, isl_dim_in, input, value);
5874 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5876 return set_from_map(isl_map_fix_si(set_to_map(set),
5877 isl_dim_set, dim, value));
5880 static __isl_give isl_basic_map *basic_map_bound_si(
5881 __isl_take isl_basic_map *bmap,
5882 enum isl_dim_type type, unsigned pos, int value, int upper)
5884 int j;
5886 if (!bmap)
5887 return NULL;
5888 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5889 pos += isl_basic_map_offset(bmap, type);
5890 bmap = isl_basic_map_cow(bmap);
5891 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5892 j = isl_basic_map_alloc_inequality(bmap);
5893 if (j < 0)
5894 goto error;
5895 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5896 if (upper) {
5897 isl_int_set_si(bmap->ineq[j][pos], -1);
5898 isl_int_set_si(bmap->ineq[j][0], value);
5899 } else {
5900 isl_int_set_si(bmap->ineq[j][pos], 1);
5901 isl_int_set_si(bmap->ineq[j][0], -value);
5903 bmap = isl_basic_map_simplify(bmap);
5904 return isl_basic_map_finalize(bmap);
5905 error:
5906 isl_basic_map_free(bmap);
5907 return NULL;
5910 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5911 __isl_take isl_basic_map *bmap,
5912 enum isl_dim_type type, unsigned pos, int value)
5914 return basic_map_bound_si(bmap, type, pos, value, 0);
5917 /* Constrain the values of the given dimension to be no greater than "value".
5919 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5920 __isl_take isl_basic_map *bmap,
5921 enum isl_dim_type type, unsigned pos, int value)
5923 return basic_map_bound_si(bmap, type, pos, value, 1);
5926 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5927 unsigned dim, isl_int value)
5929 int j;
5931 bset = isl_basic_set_cow(bset);
5932 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5933 j = isl_basic_set_alloc_inequality(bset);
5934 if (j < 0)
5935 goto error;
5936 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5937 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5938 isl_int_neg(bset->ineq[j][0], value);
5939 bset = isl_basic_set_simplify(bset);
5940 return isl_basic_set_finalize(bset);
5941 error:
5942 isl_basic_set_free(bset);
5943 return NULL;
5946 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5947 enum isl_dim_type type, unsigned pos, int value, int upper)
5949 int i;
5951 map = isl_map_cow(map);
5952 if (!map)
5953 return NULL;
5955 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5956 for (i = 0; i < map->n; ++i) {
5957 map->p[i] = basic_map_bound_si(map->p[i],
5958 type, pos, value, upper);
5959 if (!map->p[i])
5960 goto error;
5962 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5963 return map;
5964 error:
5965 isl_map_free(map);
5966 return NULL;
5969 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5970 enum isl_dim_type type, unsigned pos, int value)
5972 return map_bound_si(map, type, pos, value, 0);
5975 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5976 enum isl_dim_type type, unsigned pos, int value)
5978 return map_bound_si(map, type, pos, value, 1);
5981 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5982 enum isl_dim_type type, unsigned pos, int value)
5984 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
5985 type, pos, value));
5988 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5989 enum isl_dim_type type, unsigned pos, int value)
5991 return isl_map_upper_bound_si(set, type, pos, value);
5994 /* Bound the given variable of "bmap" from below (or above is "upper"
5995 * is set) to "value".
5997 static __isl_give isl_basic_map *basic_map_bound(
5998 __isl_take isl_basic_map *bmap,
5999 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6001 int j;
6003 if (!bmap)
6004 return NULL;
6005 if (pos >= isl_basic_map_dim(bmap, type))
6006 isl_die(bmap->ctx, isl_error_invalid,
6007 "index out of bounds", goto error);
6008 pos += isl_basic_map_offset(bmap, type);
6009 bmap = isl_basic_map_cow(bmap);
6010 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6011 j = isl_basic_map_alloc_inequality(bmap);
6012 if (j < 0)
6013 goto error;
6014 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6015 if (upper) {
6016 isl_int_set_si(bmap->ineq[j][pos], -1);
6017 isl_int_set(bmap->ineq[j][0], value);
6018 } else {
6019 isl_int_set_si(bmap->ineq[j][pos], 1);
6020 isl_int_neg(bmap->ineq[j][0], value);
6022 bmap = isl_basic_map_simplify(bmap);
6023 return isl_basic_map_finalize(bmap);
6024 error:
6025 isl_basic_map_free(bmap);
6026 return NULL;
6029 /* Bound the given variable of "map" from below (or above is "upper"
6030 * is set) to "value".
6032 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6033 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6035 int i;
6037 map = isl_map_cow(map);
6038 if (!map)
6039 return NULL;
6041 if (pos >= isl_map_dim(map, type))
6042 isl_die(map->ctx, isl_error_invalid,
6043 "index out of bounds", goto error);
6044 for (i = map->n - 1; i >= 0; --i) {
6045 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6046 if (remove_if_empty(map, i) < 0)
6047 goto error;
6049 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6050 return map;
6051 error:
6052 isl_map_free(map);
6053 return NULL;
6056 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6057 enum isl_dim_type type, unsigned pos, isl_int value)
6059 return map_bound(map, type, pos, value, 0);
6062 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6063 enum isl_dim_type type, unsigned pos, isl_int value)
6065 return map_bound(map, type, pos, value, 1);
6068 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6069 enum isl_dim_type type, unsigned pos, isl_int value)
6071 return isl_map_lower_bound(set, type, pos, value);
6074 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6075 enum isl_dim_type type, unsigned pos, isl_int value)
6077 return isl_map_upper_bound(set, type, pos, value);
6080 /* Force the values of the variable at position "pos" of type "type" of "set"
6081 * to be no smaller than "value".
6083 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6084 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6086 if (!value)
6087 goto error;
6088 if (!isl_val_is_int(value))
6089 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6090 "expecting integer value", goto error);
6091 set = isl_set_lower_bound(set, type, pos, value->n);
6092 isl_val_free(value);
6093 return set;
6094 error:
6095 isl_val_free(value);
6096 isl_set_free(set);
6097 return NULL;
6100 /* Force the values of the variable at position "pos" of type "type" of "set"
6101 * to be no greater than "value".
6103 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6104 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6106 if (!value)
6107 goto error;
6108 if (!isl_val_is_int(value))
6109 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6110 "expecting integer value", goto error);
6111 set = isl_set_upper_bound(set, type, pos, value->n);
6112 isl_val_free(value);
6113 return set;
6114 error:
6115 isl_val_free(value);
6116 isl_set_free(set);
6117 return NULL;
6120 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6121 isl_int value)
6123 int i;
6125 set = isl_set_cow(set);
6126 if (!set)
6127 return NULL;
6129 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6130 for (i = 0; i < set->n; ++i) {
6131 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6132 if (!set->p[i])
6133 goto error;
6135 return set;
6136 error:
6137 isl_set_free(set);
6138 return NULL;
6141 struct isl_map *isl_map_reverse(struct isl_map *map)
6143 int i;
6145 map = isl_map_cow(map);
6146 if (!map)
6147 return NULL;
6149 map->dim = isl_space_reverse(map->dim);
6150 if (!map->dim)
6151 goto error;
6152 for (i = 0; i < map->n; ++i) {
6153 map->p[i] = isl_basic_map_reverse(map->p[i]);
6154 if (!map->p[i])
6155 goto error;
6157 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6158 return map;
6159 error:
6160 isl_map_free(map);
6161 return NULL;
6164 #undef TYPE
6165 #define TYPE isl_pw_multi_aff
6166 #undef SUFFIX
6167 #define SUFFIX _pw_multi_aff
6168 #undef EMPTY
6169 #define EMPTY isl_pw_multi_aff_empty
6170 #undef ADD
6171 #define ADD isl_pw_multi_aff_union_add
6172 #include "isl_map_lexopt_templ.c"
6174 /* Given a map "map", compute the lexicographically minimal
6175 * (or maximal) image element for each domain element in dom,
6176 * in the form of an isl_pw_multi_aff.
6177 * If "empty" is not NULL, then set *empty to those elements in dom that
6178 * do not have an image element.
6179 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6180 * should be computed over the domain of "map". "empty" is also NULL
6181 * in this case.
6183 * We first compute the lexicographically minimal or maximal element
6184 * in the first basic map. This results in a partial solution "res"
6185 * and a subset "todo" of dom that still need to be handled.
6186 * We then consider each of the remaining maps in "map" and successively
6187 * update both "res" and "todo".
6188 * If "empty" is NULL, then the todo sets are not needed and therefore
6189 * also not computed.
6191 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6192 __isl_take isl_map *map, __isl_take isl_set *dom,
6193 __isl_give isl_set **empty, unsigned flags)
6195 int i;
6196 int full;
6197 isl_pw_multi_aff *res;
6198 isl_set *todo;
6200 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6201 if (!map || (!full && !dom))
6202 goto error;
6204 if (isl_map_plain_is_empty(map)) {
6205 if (empty)
6206 *empty = dom;
6207 else
6208 isl_set_free(dom);
6209 return isl_pw_multi_aff_from_map(map);
6212 res = basic_map_partial_lexopt_pw_multi_aff(
6213 isl_basic_map_copy(map->p[0]),
6214 isl_set_copy(dom), empty, flags);
6216 if (empty)
6217 todo = *empty;
6218 for (i = 1; i < map->n; ++i) {
6219 isl_pw_multi_aff *res_i;
6221 res_i = basic_map_partial_lexopt_pw_multi_aff(
6222 isl_basic_map_copy(map->p[i]),
6223 isl_set_copy(dom), empty, flags);
6225 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6226 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6227 else
6228 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6230 if (empty)
6231 todo = isl_set_intersect(todo, *empty);
6234 isl_set_free(dom);
6235 isl_map_free(map);
6237 if (empty)
6238 *empty = todo;
6240 return res;
6241 error:
6242 if (empty)
6243 *empty = NULL;
6244 isl_set_free(dom);
6245 isl_map_free(map);
6246 return NULL;
6249 #undef TYPE
6250 #define TYPE isl_map
6251 #undef SUFFIX
6252 #define SUFFIX
6253 #undef EMPTY
6254 #define EMPTY isl_map_empty
6255 #undef ADD
6256 #define ADD isl_map_union_disjoint
6257 #include "isl_map_lexopt_templ.c"
6259 /* Given a map "map", compute the lexicographically minimal
6260 * (or maximal) image element for each domain element in "dom",
6261 * in the form of an isl_map.
6262 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6263 * do not have an image element.
6264 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6265 * should be computed over the domain of "map". "empty" is also NULL
6266 * in this case.
6268 * If the input consists of more than one disjunct, then first
6269 * compute the desired result in the form of an isl_pw_multi_aff and
6270 * then convert that into an isl_map.
6272 * This function used to have an explicit implementation in terms
6273 * of isl_maps, but it would continually intersect the domains of
6274 * partial results with the complement of the domain of the next
6275 * partial solution, potentially leading to an explosion in the number
6276 * of disjuncts if there are several disjuncts in the input.
6277 * An even earlier implementation of this function would look for
6278 * better results in the domain of the partial result and for extra
6279 * results in the complement of this domain, which would lead to
6280 * even more splintering.
6282 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6283 __isl_take isl_map *map, __isl_take isl_set *dom,
6284 __isl_give isl_set **empty, unsigned flags)
6286 int full;
6287 struct isl_map *res;
6288 isl_pw_multi_aff *pma;
6290 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6291 if (!map || (!full && !dom))
6292 goto error;
6294 if (isl_map_plain_is_empty(map)) {
6295 if (empty)
6296 *empty = dom;
6297 else
6298 isl_set_free(dom);
6299 return map;
6302 if (map->n == 1) {
6303 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6304 dom, empty, flags);
6305 isl_map_free(map);
6306 return res;
6309 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6310 flags);
6311 return isl_map_from_pw_multi_aff(pma);
6312 error:
6313 if (empty)
6314 *empty = NULL;
6315 isl_set_free(dom);
6316 isl_map_free(map);
6317 return NULL;
6320 __isl_give isl_map *isl_map_partial_lexmax(
6321 __isl_take isl_map *map, __isl_take isl_set *dom,
6322 __isl_give isl_set **empty)
6324 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6327 __isl_give isl_map *isl_map_partial_lexmin(
6328 __isl_take isl_map *map, __isl_take isl_set *dom,
6329 __isl_give isl_set **empty)
6331 return isl_map_partial_lexopt(map, dom, empty, 0);
6334 __isl_give isl_set *isl_set_partial_lexmin(
6335 __isl_take isl_set *set, __isl_take isl_set *dom,
6336 __isl_give isl_set **empty)
6338 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6339 dom, empty));
6342 __isl_give isl_set *isl_set_partial_lexmax(
6343 __isl_take isl_set *set, __isl_take isl_set *dom,
6344 __isl_give isl_set **empty)
6346 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6347 dom, empty));
6350 /* Compute the lexicographic minimum (or maximum if "flags" includes
6351 * ISL_OPT_MAX) of "bset" over its parametric domain.
6353 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6354 unsigned flags)
6356 return isl_basic_map_lexopt(bset, flags);
6359 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6361 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6364 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6366 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6369 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6371 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6374 /* Compute the lexicographic minimum of "bset" over its parametric domain
6375 * for the purpose of quantifier elimination.
6376 * That is, find an explicit representation for all the existentially
6377 * quantified variables in "bset" by computing their lexicographic
6378 * minimum.
6380 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6381 __isl_take isl_basic_set *bset)
6383 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6386 /* Extract the first and only affine expression from list
6387 * and then add it to *pwaff with the given dom.
6388 * This domain is known to be disjoint from other domains
6389 * because of the way isl_basic_map_foreach_lexmax works.
6391 static int update_dim_opt(__isl_take isl_basic_set *dom,
6392 __isl_take isl_aff_list *list, void *user)
6394 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6395 isl_aff *aff;
6396 isl_pw_aff **pwaff = user;
6397 isl_pw_aff *pwaff_i;
6399 if (!list)
6400 goto error;
6401 if (isl_aff_list_n_aff(list) != 1)
6402 isl_die(ctx, isl_error_internal,
6403 "expecting single element list", goto error);
6405 aff = isl_aff_list_get_aff(list, 0);
6406 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6408 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6410 isl_aff_list_free(list);
6412 return 0;
6413 error:
6414 isl_basic_set_free(dom);
6415 isl_aff_list_free(list);
6416 return -1;
6419 /* Given a basic map with one output dimension, compute the minimum or
6420 * maximum of that dimension as an isl_pw_aff.
6422 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6423 * call update_dim_opt on each leaf of the result.
6425 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6426 int max)
6428 isl_space *dim = isl_basic_map_get_space(bmap);
6429 isl_pw_aff *pwaff;
6430 int r;
6432 dim = isl_space_from_domain(isl_space_domain(dim));
6433 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6434 pwaff = isl_pw_aff_empty(dim);
6436 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6437 if (r < 0)
6438 return isl_pw_aff_free(pwaff);
6440 return pwaff;
6443 /* Compute the minimum or maximum of the given output dimension
6444 * as a function of the parameters and the input dimensions,
6445 * but independently of the other output dimensions.
6447 * We first project out the other output dimension and then compute
6448 * the "lexicographic" maximum in each basic map, combining the results
6449 * using isl_pw_aff_union_max.
6451 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6452 int max)
6454 int i;
6455 isl_pw_aff *pwaff;
6456 unsigned n_out;
6458 n_out = isl_map_dim(map, isl_dim_out);
6459 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6460 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6461 if (!map)
6462 return NULL;
6464 if (map->n == 0) {
6465 isl_space *dim = isl_map_get_space(map);
6466 isl_map_free(map);
6467 return isl_pw_aff_empty(dim);
6470 pwaff = basic_map_dim_opt(map->p[0], max);
6471 for (i = 1; i < map->n; ++i) {
6472 isl_pw_aff *pwaff_i;
6474 pwaff_i = basic_map_dim_opt(map->p[i], max);
6475 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6478 isl_map_free(map);
6480 return pwaff;
6483 /* Compute the minimum of the given output dimension as a function of the
6484 * parameters and input dimensions, but independently of
6485 * the other output dimensions.
6487 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6489 return map_dim_opt(map, pos, 0);
6492 /* Compute the maximum of the given output dimension as a function of the
6493 * parameters and input dimensions, but independently of
6494 * the other output dimensions.
6496 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6498 return map_dim_opt(map, pos, 1);
6501 /* Compute the minimum or maximum of the given set dimension
6502 * as a function of the parameters,
6503 * but independently of the other set dimensions.
6505 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6506 int max)
6508 return map_dim_opt(set, pos, max);
6511 /* Compute the maximum of the given set dimension as a function of the
6512 * parameters, but independently of the other set dimensions.
6514 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6516 return set_dim_opt(set, pos, 1);
6519 /* Compute the minimum of the given set dimension as a function of the
6520 * parameters, but independently of the other set dimensions.
6522 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6524 return set_dim_opt(set, pos, 0);
6527 /* Apply a preimage specified by "mat" on the parameters of "bset".
6528 * bset is assumed to have only parameters and divs.
6530 static struct isl_basic_set *basic_set_parameter_preimage(
6531 struct isl_basic_set *bset, struct isl_mat *mat)
6533 unsigned nparam;
6535 if (!bset || !mat)
6536 goto error;
6538 bset->dim = isl_space_cow(bset->dim);
6539 if (!bset->dim)
6540 goto error;
6542 nparam = isl_basic_set_dim(bset, isl_dim_param);
6544 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6546 bset->dim->nparam = 0;
6547 bset->dim->n_out = nparam;
6548 bset = isl_basic_set_preimage(bset, mat);
6549 if (bset) {
6550 bset->dim->nparam = bset->dim->n_out;
6551 bset->dim->n_out = 0;
6553 return bset;
6554 error:
6555 isl_mat_free(mat);
6556 isl_basic_set_free(bset);
6557 return NULL;
6560 /* Apply a preimage specified by "mat" on the parameters of "set".
6561 * set is assumed to have only parameters and divs.
6563 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6564 __isl_take isl_mat *mat)
6566 isl_space *space;
6567 unsigned nparam;
6569 if (!set || !mat)
6570 goto error;
6572 nparam = isl_set_dim(set, isl_dim_param);
6574 if (mat->n_row != 1 + nparam)
6575 isl_die(isl_set_get_ctx(set), isl_error_internal,
6576 "unexpected number of rows", goto error);
6578 space = isl_set_get_space(set);
6579 space = isl_space_move_dims(space, isl_dim_set, 0,
6580 isl_dim_param, 0, nparam);
6581 set = isl_set_reset_space(set, space);
6582 set = isl_set_preimage(set, mat);
6583 nparam = isl_set_dim(set, isl_dim_out);
6584 space = isl_set_get_space(set);
6585 space = isl_space_move_dims(space, isl_dim_param, 0,
6586 isl_dim_out, 0, nparam);
6587 set = isl_set_reset_space(set, space);
6588 return set;
6589 error:
6590 isl_mat_free(mat);
6591 isl_set_free(set);
6592 return NULL;
6595 /* Intersect the basic set "bset" with the affine space specified by the
6596 * equalities in "eq".
6598 static struct isl_basic_set *basic_set_append_equalities(
6599 struct isl_basic_set *bset, struct isl_mat *eq)
6601 int i, k;
6602 unsigned len;
6604 if (!bset || !eq)
6605 goto error;
6607 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6608 eq->n_row, 0);
6609 if (!bset)
6610 goto error;
6612 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6613 for (i = 0; i < eq->n_row; ++i) {
6614 k = isl_basic_set_alloc_equality(bset);
6615 if (k < 0)
6616 goto error;
6617 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6618 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6620 isl_mat_free(eq);
6622 bset = isl_basic_set_gauss(bset, NULL);
6623 bset = isl_basic_set_finalize(bset);
6625 return bset;
6626 error:
6627 isl_mat_free(eq);
6628 isl_basic_set_free(bset);
6629 return NULL;
6632 /* Intersect the set "set" with the affine space specified by the
6633 * equalities in "eq".
6635 static struct isl_set *set_append_equalities(struct isl_set *set,
6636 struct isl_mat *eq)
6638 int i;
6640 if (!set || !eq)
6641 goto error;
6643 for (i = 0; i < set->n; ++i) {
6644 set->p[i] = basic_set_append_equalities(set->p[i],
6645 isl_mat_copy(eq));
6646 if (!set->p[i])
6647 goto error;
6649 isl_mat_free(eq);
6650 return set;
6651 error:
6652 isl_mat_free(eq);
6653 isl_set_free(set);
6654 return NULL;
6657 /* Given a basic set "bset" that only involves parameters and existentially
6658 * quantified variables, return the index of the first equality
6659 * that only involves parameters. If there is no such equality then
6660 * return bset->n_eq.
6662 * This function assumes that isl_basic_set_gauss has been called on "bset".
6664 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6666 int i, j;
6667 unsigned nparam, n_div;
6669 if (!bset)
6670 return -1;
6672 nparam = isl_basic_set_dim(bset, isl_dim_param);
6673 n_div = isl_basic_set_dim(bset, isl_dim_div);
6675 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6676 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6677 ++i;
6680 return i;
6683 /* Compute an explicit representation for the existentially quantified
6684 * variables in "bset" by computing the "minimal value" of the set
6685 * variables. Since there are no set variables, the computation of
6686 * the minimal value essentially computes an explicit representation
6687 * of the non-empty part(s) of "bset".
6689 * The input only involves parameters and existentially quantified variables.
6690 * All equalities among parameters have been removed.
6692 * Since the existentially quantified variables in the result are in general
6693 * going to be different from those in the input, we first replace
6694 * them by the minimal number of variables based on their equalities.
6695 * This should simplify the parametric integer programming.
6697 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6699 isl_morph *morph1, *morph2;
6700 isl_set *set;
6701 unsigned n;
6703 if (!bset)
6704 return NULL;
6705 if (bset->n_eq == 0)
6706 return isl_basic_set_lexmin_compute_divs(bset);
6708 morph1 = isl_basic_set_parameter_compression(bset);
6709 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6710 bset = isl_basic_set_lift(bset);
6711 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6712 bset = isl_morph_basic_set(morph2, bset);
6713 n = isl_basic_set_dim(bset, isl_dim_set);
6714 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6716 set = isl_basic_set_lexmin_compute_divs(bset);
6718 set = isl_morph_set(isl_morph_inverse(morph1), set);
6720 return set;
6723 /* Project the given basic set onto its parameter domain, possibly introducing
6724 * new, explicit, existential variables in the constraints.
6725 * The input has parameters and (possibly implicit) existential variables.
6726 * The output has the same parameters, but only
6727 * explicit existentially quantified variables.
6729 * The actual projection is performed by pip, but pip doesn't seem
6730 * to like equalities very much, so we first remove the equalities
6731 * among the parameters by performing a variable compression on
6732 * the parameters. Afterward, an inverse transformation is performed
6733 * and the equalities among the parameters are inserted back in.
6735 * The variable compression on the parameters may uncover additional
6736 * equalities that were only implicit before. We therefore check
6737 * if there are any new parameter equalities in the result and
6738 * if so recurse. The removal of parameter equalities is required
6739 * for the parameter compression performed by base_compute_divs.
6741 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6743 int i;
6744 struct isl_mat *eq;
6745 struct isl_mat *T, *T2;
6746 struct isl_set *set;
6747 unsigned nparam;
6749 bset = isl_basic_set_cow(bset);
6750 if (!bset)
6751 return NULL;
6753 if (bset->n_eq == 0)
6754 return base_compute_divs(bset);
6756 bset = isl_basic_set_gauss(bset, NULL);
6757 if (!bset)
6758 return NULL;
6759 if (isl_basic_set_plain_is_empty(bset))
6760 return isl_set_from_basic_set(bset);
6762 i = first_parameter_equality(bset);
6763 if (i == bset->n_eq)
6764 return base_compute_divs(bset);
6766 nparam = isl_basic_set_dim(bset, isl_dim_param);
6767 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6768 0, 1 + nparam);
6769 eq = isl_mat_cow(eq);
6770 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6771 if (T && T->n_col == 0) {
6772 isl_mat_free(T);
6773 isl_mat_free(T2);
6774 isl_mat_free(eq);
6775 bset = isl_basic_set_set_to_empty(bset);
6776 return isl_set_from_basic_set(bset);
6778 bset = basic_set_parameter_preimage(bset, T);
6780 i = first_parameter_equality(bset);
6781 if (!bset)
6782 set = NULL;
6783 else if (i == bset->n_eq)
6784 set = base_compute_divs(bset);
6785 else
6786 set = parameter_compute_divs(bset);
6787 set = set_parameter_preimage(set, T2);
6788 set = set_append_equalities(set, eq);
6789 return set;
6792 /* Insert the divs from "ls" before those of "bmap".
6794 * The number of columns is not changed, which means that the last
6795 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6796 * The caller is responsible for removing the same number of dimensions
6797 * from the space of "bmap".
6799 static __isl_give isl_basic_map *insert_divs_from_local_space(
6800 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6802 int i;
6803 int n_div;
6804 int old_n_div;
6806 n_div = isl_local_space_dim(ls, isl_dim_div);
6807 if (n_div == 0)
6808 return bmap;
6810 old_n_div = bmap->n_div;
6811 bmap = insert_div_rows(bmap, n_div);
6812 if (!bmap)
6813 return NULL;
6815 for (i = 0; i < n_div; ++i) {
6816 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6817 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6820 return bmap;
6823 /* Replace the space of "bmap" by the space and divs of "ls".
6825 * If "ls" has any divs, then we simplify the result since we may
6826 * have discovered some additional equalities that could simplify
6827 * the div expressions.
6829 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6830 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6832 int n_div;
6834 bmap = isl_basic_map_cow(bmap);
6835 if (!bmap || !ls)
6836 goto error;
6838 n_div = isl_local_space_dim(ls, isl_dim_div);
6839 bmap = insert_divs_from_local_space(bmap, ls);
6840 if (!bmap)
6841 goto error;
6843 isl_space_free(bmap->dim);
6844 bmap->dim = isl_local_space_get_space(ls);
6845 if (!bmap->dim)
6846 goto error;
6848 isl_local_space_free(ls);
6849 if (n_div > 0)
6850 bmap = isl_basic_map_simplify(bmap);
6851 bmap = isl_basic_map_finalize(bmap);
6852 return bmap;
6853 error:
6854 isl_basic_map_free(bmap);
6855 isl_local_space_free(ls);
6856 return NULL;
6859 /* Replace the space of "map" by the space and divs of "ls".
6861 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6862 __isl_take isl_local_space *ls)
6864 int i;
6866 map = isl_map_cow(map);
6867 if (!map || !ls)
6868 goto error;
6870 for (i = 0; i < map->n; ++i) {
6871 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6872 isl_local_space_copy(ls));
6873 if (!map->p[i])
6874 goto error;
6876 isl_space_free(map->dim);
6877 map->dim = isl_local_space_get_space(ls);
6878 if (!map->dim)
6879 goto error;
6881 isl_local_space_free(ls);
6882 return map;
6883 error:
6884 isl_local_space_free(ls);
6885 isl_map_free(map);
6886 return NULL;
6889 /* Compute an explicit representation for the existentially
6890 * quantified variables for which do not know any explicit representation yet.
6892 * We first sort the existentially quantified variables so that the
6893 * existentially quantified variables for which we already have an explicit
6894 * representation are placed before those for which we do not.
6895 * The input dimensions, the output dimensions and the existentially
6896 * quantified variables for which we already have an explicit
6897 * representation are then turned into parameters.
6898 * compute_divs returns a map with the same parameters and
6899 * no input or output dimensions and the dimension specification
6900 * is reset to that of the input, including the existentially quantified
6901 * variables for which we already had an explicit representation.
6903 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6905 struct isl_basic_set *bset;
6906 struct isl_set *set;
6907 struct isl_map *map;
6908 isl_space *dim;
6909 isl_local_space *ls;
6910 unsigned nparam;
6911 unsigned n_in;
6912 unsigned n_out;
6913 int n_known;
6914 int i;
6916 bmap = isl_basic_map_sort_divs(bmap);
6917 bmap = isl_basic_map_cow(bmap);
6918 if (!bmap)
6919 return NULL;
6921 n_known = isl_basic_map_first_unknown_div(bmap);
6922 if (n_known < 0)
6923 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6925 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6926 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6927 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6928 dim = isl_space_set_alloc(bmap->ctx,
6929 nparam + n_in + n_out + n_known, 0);
6930 if (!dim)
6931 goto error;
6933 ls = isl_basic_map_get_local_space(bmap);
6934 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6935 n_known, bmap->n_div - n_known);
6936 if (n_known > 0) {
6937 for (i = n_known; i < bmap->n_div; ++i)
6938 swap_div(bmap, i - n_known, i);
6939 bmap->n_div -= n_known;
6940 bmap->extra -= n_known;
6942 bmap = isl_basic_map_reset_space(bmap, dim);
6943 bset = bset_from_bmap(bmap);
6945 set = parameter_compute_divs(bset);
6946 map = set_to_map(set);
6947 map = replace_space_by_local_space(map, ls);
6949 return map;
6950 error:
6951 isl_basic_map_free(bmap);
6952 return NULL;
6955 /* Remove the explicit representation of local variable "div",
6956 * if there is any.
6958 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6959 __isl_take isl_basic_map *bmap, int div)
6961 isl_bool unknown;
6963 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6964 if (unknown < 0)
6965 return isl_basic_map_free(bmap);
6966 if (unknown)
6967 return bmap;
6969 bmap = isl_basic_map_cow(bmap);
6970 if (!bmap)
6971 return NULL;
6972 isl_int_set_si(bmap->div[div][0], 0);
6973 return bmap;
6976 /* Is local variable "div" of "bmap" marked as not having an explicit
6977 * representation?
6978 * Note that even if "div" is not marked in this way and therefore
6979 * has an explicit representation, this representation may still
6980 * depend (indirectly) on other local variables that do not
6981 * have an explicit representation.
6983 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6984 int div)
6986 if (!bmap)
6987 return isl_bool_error;
6988 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6989 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6990 "position out of bounds", return isl_bool_error);
6991 return isl_int_is_zero(bmap->div[div][0]);
6994 /* Return the position of the first local variable that does not
6995 * have an explicit representation.
6996 * Return the total number of local variables if they all have
6997 * an explicit representation.
6998 * Return -1 on error.
7000 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7002 int i;
7004 if (!bmap)
7005 return -1;
7007 for (i = 0; i < bmap->n_div; ++i) {
7008 if (!isl_basic_map_div_is_known(bmap, i))
7009 return i;
7011 return bmap->n_div;
7014 /* Return the position of the first local variable that does not
7015 * have an explicit representation.
7016 * Return the total number of local variables if they all have
7017 * an explicit representation.
7018 * Return -1 on error.
7020 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7022 return isl_basic_map_first_unknown_div(bset);
7025 /* Does "bmap" have an explicit representation for all local variables?
7027 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7029 int first, n;
7031 n = isl_basic_map_dim(bmap, isl_dim_div);
7032 first = isl_basic_map_first_unknown_div(bmap);
7033 if (first < 0)
7034 return isl_bool_error;
7035 return first == n;
7038 /* Do all basic maps in "map" have an explicit representation
7039 * for all local variables?
7041 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7043 int i;
7045 if (!map)
7046 return isl_bool_error;
7048 for (i = 0; i < map->n; ++i) {
7049 int known = isl_basic_map_divs_known(map->p[i]);
7050 if (known <= 0)
7051 return known;
7054 return isl_bool_true;
7057 /* If bmap contains any unknown divs, then compute explicit
7058 * expressions for them. However, this computation may be
7059 * quite expensive, so first try to remove divs that aren't
7060 * strictly needed.
7062 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7064 int known;
7065 struct isl_map *map;
7067 known = isl_basic_map_divs_known(bmap);
7068 if (known < 0)
7069 goto error;
7070 if (known)
7071 return isl_map_from_basic_map(bmap);
7073 bmap = isl_basic_map_drop_redundant_divs(bmap);
7075 known = isl_basic_map_divs_known(bmap);
7076 if (known < 0)
7077 goto error;
7078 if (known)
7079 return isl_map_from_basic_map(bmap);
7081 map = compute_divs(bmap);
7082 return map;
7083 error:
7084 isl_basic_map_free(bmap);
7085 return NULL;
7088 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7090 int i;
7091 int known;
7092 struct isl_map *res;
7094 if (!map)
7095 return NULL;
7096 if (map->n == 0)
7097 return map;
7099 known = isl_map_divs_known(map);
7100 if (known < 0) {
7101 isl_map_free(map);
7102 return NULL;
7104 if (known)
7105 return map;
7107 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7108 for (i = 1 ; i < map->n; ++i) {
7109 struct isl_map *r2;
7110 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7111 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7112 res = isl_map_union_disjoint(res, r2);
7113 else
7114 res = isl_map_union(res, r2);
7116 isl_map_free(map);
7118 return res;
7121 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7123 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7126 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7128 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7131 struct isl_set *isl_map_domain(struct isl_map *map)
7133 int i;
7134 struct isl_set *set;
7136 if (!map)
7137 goto error;
7139 map = isl_map_cow(map);
7140 if (!map)
7141 return NULL;
7143 set = set_from_map(map);
7144 set->dim = isl_space_domain(set->dim);
7145 if (!set->dim)
7146 goto error;
7147 for (i = 0; i < map->n; ++i) {
7148 set->p[i] = isl_basic_map_domain(map->p[i]);
7149 if (!set->p[i])
7150 goto error;
7152 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7153 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7154 return set;
7155 error:
7156 isl_map_free(map);
7157 return NULL;
7160 /* Return the union of "map1" and "map2", where we assume for now that
7161 * "map1" and "map2" are disjoint. Note that the basic maps inside
7162 * "map1" or "map2" may not be disjoint from each other.
7163 * Also note that this function is also called from isl_map_union,
7164 * which takes care of handling the situation where "map1" and "map2"
7165 * may not be disjoint.
7167 * If one of the inputs is empty, we can simply return the other input.
7168 * Similarly, if one of the inputs is universal, then it is equal to the union.
7170 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7171 __isl_take isl_map *map2)
7173 int i;
7174 unsigned flags = 0;
7175 struct isl_map *map = NULL;
7176 int is_universe;
7178 if (!map1 || !map2)
7179 goto error;
7181 if (!isl_space_is_equal(map1->dim, map2->dim))
7182 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7183 "spaces don't match", goto error);
7185 if (map1->n == 0) {
7186 isl_map_free(map1);
7187 return map2;
7189 if (map2->n == 0) {
7190 isl_map_free(map2);
7191 return map1;
7194 is_universe = isl_map_plain_is_universe(map1);
7195 if (is_universe < 0)
7196 goto error;
7197 if (is_universe) {
7198 isl_map_free(map2);
7199 return map1;
7202 is_universe = isl_map_plain_is_universe(map2);
7203 if (is_universe < 0)
7204 goto error;
7205 if (is_universe) {
7206 isl_map_free(map1);
7207 return map2;
7210 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7211 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7212 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7214 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7215 map1->n + map2->n, flags);
7216 if (!map)
7217 goto error;
7218 for (i = 0; i < map1->n; ++i) {
7219 map = isl_map_add_basic_map(map,
7220 isl_basic_map_copy(map1->p[i]));
7221 if (!map)
7222 goto error;
7224 for (i = 0; i < map2->n; ++i) {
7225 map = isl_map_add_basic_map(map,
7226 isl_basic_map_copy(map2->p[i]));
7227 if (!map)
7228 goto error;
7230 isl_map_free(map1);
7231 isl_map_free(map2);
7232 return map;
7233 error:
7234 isl_map_free(map);
7235 isl_map_free(map1);
7236 isl_map_free(map2);
7237 return NULL;
7240 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7241 * guaranteed to be disjoint by the caller.
7243 * Note that this functions is called from within isl_map_make_disjoint,
7244 * so we have to be careful not to touch the constraints of the inputs
7245 * in any way.
7247 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7248 __isl_take isl_map *map2)
7250 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7253 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7254 * not be disjoint. The parameters are assumed to have been aligned.
7256 * We currently simply call map_union_disjoint, the internal operation
7257 * of which does not really depend on the inputs being disjoint.
7258 * If the result contains more than one basic map, then we clear
7259 * the disjoint flag since the result may contain basic maps from
7260 * both inputs and these are not guaranteed to be disjoint.
7262 * As a special case, if "map1" and "map2" are obviously equal,
7263 * then we simply return "map1".
7265 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7266 __isl_take isl_map *map2)
7268 int equal;
7270 if (!map1 || !map2)
7271 goto error;
7273 equal = isl_map_plain_is_equal(map1, map2);
7274 if (equal < 0)
7275 goto error;
7276 if (equal) {
7277 isl_map_free(map2);
7278 return map1;
7281 map1 = map_union_disjoint(map1, map2);
7282 if (!map1)
7283 return NULL;
7284 if (map1->n > 1)
7285 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7286 return map1;
7287 error:
7288 isl_map_free(map1);
7289 isl_map_free(map2);
7290 return NULL;
7293 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7294 * not be disjoint.
7296 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7297 __isl_take isl_map *map2)
7299 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7302 struct isl_set *isl_set_union_disjoint(
7303 struct isl_set *set1, struct isl_set *set2)
7305 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7306 set_to_map(set2)));
7309 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7311 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7314 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7315 * the results.
7317 * "map" and "set" are assumed to be compatible and non-NULL.
7319 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7320 __isl_take isl_set *set,
7321 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7322 __isl_take isl_basic_set *bset))
7324 unsigned flags = 0;
7325 struct isl_map *result;
7326 int i, j;
7328 if (isl_set_plain_is_universe(set)) {
7329 isl_set_free(set);
7330 return map;
7333 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7334 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7335 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7337 result = isl_map_alloc_space(isl_space_copy(map->dim),
7338 map->n * set->n, flags);
7339 for (i = 0; result && i < map->n; ++i)
7340 for (j = 0; j < set->n; ++j) {
7341 result = isl_map_add_basic_map(result,
7342 fn(isl_basic_map_copy(map->p[i]),
7343 isl_basic_set_copy(set->p[j])));
7344 if (!result)
7345 break;
7348 isl_map_free(map);
7349 isl_set_free(set);
7350 return result;
7353 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7354 __isl_take isl_set *set)
7356 if (!map || !set)
7357 goto error;
7359 if (!isl_map_compatible_range(map, set))
7360 isl_die(set->ctx, isl_error_invalid,
7361 "incompatible spaces", goto error);
7363 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7364 error:
7365 isl_map_free(map);
7366 isl_set_free(set);
7367 return NULL;
7370 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7371 __isl_take isl_set *set)
7373 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7376 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7377 __isl_take isl_set *set)
7379 if (!map || !set)
7380 goto error;
7382 if (!isl_map_compatible_domain(map, set))
7383 isl_die(set->ctx, isl_error_invalid,
7384 "incompatible spaces", goto error);
7386 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7387 error:
7388 isl_map_free(map);
7389 isl_set_free(set);
7390 return NULL;
7393 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7394 __isl_take isl_set *set)
7396 return isl_map_align_params_map_map_and(map, set,
7397 &map_intersect_domain);
7400 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7401 __isl_take isl_map *map2)
7403 if (!map1 || !map2)
7404 goto error;
7405 map1 = isl_map_reverse(map1);
7406 map1 = isl_map_apply_range(map1, map2);
7407 return isl_map_reverse(map1);
7408 error:
7409 isl_map_free(map1);
7410 isl_map_free(map2);
7411 return NULL;
7414 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7415 __isl_take isl_map *map2)
7417 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7420 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7421 __isl_take isl_map *map2)
7423 isl_space *dim_result;
7424 struct isl_map *result;
7425 int i, j;
7427 if (!map1 || !map2)
7428 goto error;
7430 dim_result = isl_space_join(isl_space_copy(map1->dim),
7431 isl_space_copy(map2->dim));
7433 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7434 if (!result)
7435 goto error;
7436 for (i = 0; i < map1->n; ++i)
7437 for (j = 0; j < map2->n; ++j) {
7438 result = isl_map_add_basic_map(result,
7439 isl_basic_map_apply_range(
7440 isl_basic_map_copy(map1->p[i]),
7441 isl_basic_map_copy(map2->p[j])));
7442 if (!result)
7443 goto error;
7445 isl_map_free(map1);
7446 isl_map_free(map2);
7447 if (result && result->n <= 1)
7448 ISL_F_SET(result, ISL_MAP_DISJOINT);
7449 return result;
7450 error:
7451 isl_map_free(map1);
7452 isl_map_free(map2);
7453 return NULL;
7456 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7457 __isl_take isl_map *map2)
7459 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7463 * returns range - domain
7465 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7467 isl_space *target_space;
7468 struct isl_basic_set *bset;
7469 unsigned dim;
7470 unsigned nparam;
7471 int i;
7473 if (!bmap)
7474 goto error;
7475 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7476 bmap->dim, isl_dim_out),
7477 goto error);
7478 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7479 dim = isl_basic_map_n_in(bmap);
7480 nparam = isl_basic_map_n_param(bmap);
7481 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7482 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7483 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7484 for (i = 0; i < dim; ++i) {
7485 int j = isl_basic_map_alloc_equality(bmap);
7486 if (j < 0) {
7487 bmap = isl_basic_map_free(bmap);
7488 break;
7490 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7491 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7492 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7493 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7495 bset = isl_basic_map_domain(bmap);
7496 bset = isl_basic_set_reset_space(bset, target_space);
7497 return bset;
7498 error:
7499 isl_basic_map_free(bmap);
7500 return NULL;
7504 * returns range - domain
7506 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7508 int i;
7509 isl_space *dim;
7510 struct isl_set *result;
7512 if (!map)
7513 return NULL;
7515 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7516 map->dim, isl_dim_out),
7517 goto error);
7518 dim = isl_map_get_space(map);
7519 dim = isl_space_domain(dim);
7520 result = isl_set_alloc_space(dim, map->n, 0);
7521 if (!result)
7522 goto error;
7523 for (i = 0; i < map->n; ++i)
7524 result = isl_set_add_basic_set(result,
7525 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7526 isl_map_free(map);
7527 return result;
7528 error:
7529 isl_map_free(map);
7530 return NULL;
7534 * returns [domain -> range] -> range - domain
7536 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7537 __isl_take isl_basic_map *bmap)
7539 int i, k;
7540 isl_space *dim;
7541 isl_basic_map *domain;
7542 int nparam, n;
7543 unsigned total;
7545 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7546 bmap->dim, isl_dim_out))
7547 isl_die(bmap->ctx, isl_error_invalid,
7548 "domain and range don't match", goto error);
7550 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7551 n = isl_basic_map_dim(bmap, isl_dim_in);
7553 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7554 domain = isl_basic_map_universe(dim);
7556 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7557 bmap = isl_basic_map_apply_range(bmap, domain);
7558 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7560 total = isl_basic_map_total_dim(bmap);
7562 for (i = 0; i < n; ++i) {
7563 k = isl_basic_map_alloc_equality(bmap);
7564 if (k < 0)
7565 goto error;
7566 isl_seq_clr(bmap->eq[k], 1 + total);
7567 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7568 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7569 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7572 bmap = isl_basic_map_gauss(bmap, NULL);
7573 return isl_basic_map_finalize(bmap);
7574 error:
7575 isl_basic_map_free(bmap);
7576 return NULL;
7580 * returns [domain -> range] -> range - domain
7582 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7584 int i;
7585 isl_space *domain_dim;
7587 if (!map)
7588 return NULL;
7590 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7591 map->dim, isl_dim_out))
7592 isl_die(map->ctx, isl_error_invalid,
7593 "domain and range don't match", goto error);
7595 map = isl_map_cow(map);
7596 if (!map)
7597 return NULL;
7599 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7600 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7601 map->dim = isl_space_join(map->dim, domain_dim);
7602 if (!map->dim)
7603 goto error;
7604 for (i = 0; i < map->n; ++i) {
7605 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7606 if (!map->p[i])
7607 goto error;
7609 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7610 return map;
7611 error:
7612 isl_map_free(map);
7613 return NULL;
7616 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7618 struct isl_basic_map *bmap;
7619 unsigned nparam;
7620 unsigned dim;
7621 int i;
7623 if (!dims)
7624 return NULL;
7626 nparam = dims->nparam;
7627 dim = dims->n_out;
7628 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7629 if (!bmap)
7630 goto error;
7632 for (i = 0; i < dim; ++i) {
7633 int j = isl_basic_map_alloc_equality(bmap);
7634 if (j < 0)
7635 goto error;
7636 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7637 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7638 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7640 return isl_basic_map_finalize(bmap);
7641 error:
7642 isl_basic_map_free(bmap);
7643 return NULL;
7646 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7648 if (!dim)
7649 return NULL;
7650 if (dim->n_in != dim->n_out)
7651 isl_die(dim->ctx, isl_error_invalid,
7652 "number of input and output dimensions needs to be "
7653 "the same", goto error);
7654 return basic_map_identity(dim);
7655 error:
7656 isl_space_free(dim);
7657 return NULL;
7660 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7662 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7665 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7667 isl_space *dim = isl_set_get_space(set);
7668 isl_map *id;
7669 id = isl_map_identity(isl_space_map_from_set(dim));
7670 return isl_map_intersect_range(id, set);
7673 /* Construct a basic set with all set dimensions having only non-negative
7674 * values.
7676 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7677 __isl_take isl_space *space)
7679 int i;
7680 unsigned nparam;
7681 unsigned dim;
7682 struct isl_basic_set *bset;
7684 if (!space)
7685 return NULL;
7686 nparam = space->nparam;
7687 dim = space->n_out;
7688 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7689 if (!bset)
7690 return NULL;
7691 for (i = 0; i < dim; ++i) {
7692 int k = isl_basic_set_alloc_inequality(bset);
7693 if (k < 0)
7694 goto error;
7695 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7696 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7698 return bset;
7699 error:
7700 isl_basic_set_free(bset);
7701 return NULL;
7704 /* Construct the half-space x_pos >= 0.
7706 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7707 int pos)
7709 int k;
7710 isl_basic_set *nonneg;
7712 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7713 k = isl_basic_set_alloc_inequality(nonneg);
7714 if (k < 0)
7715 goto error;
7716 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7717 isl_int_set_si(nonneg->ineq[k][pos], 1);
7719 return isl_basic_set_finalize(nonneg);
7720 error:
7721 isl_basic_set_free(nonneg);
7722 return NULL;
7725 /* Construct the half-space x_pos <= -1.
7727 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7729 int k;
7730 isl_basic_set *neg;
7732 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7733 k = isl_basic_set_alloc_inequality(neg);
7734 if (k < 0)
7735 goto error;
7736 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7737 isl_int_set_si(neg->ineq[k][0], -1);
7738 isl_int_set_si(neg->ineq[k][pos], -1);
7740 return isl_basic_set_finalize(neg);
7741 error:
7742 isl_basic_set_free(neg);
7743 return NULL;
7746 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7747 enum isl_dim_type type, unsigned first, unsigned n)
7749 int i;
7750 unsigned offset;
7751 isl_basic_set *nonneg;
7752 isl_basic_set *neg;
7754 if (!set)
7755 return NULL;
7756 if (n == 0)
7757 return set;
7759 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7761 offset = pos(set->dim, type);
7762 for (i = 0; i < n; ++i) {
7763 nonneg = nonneg_halfspace(isl_set_get_space(set),
7764 offset + first + i);
7765 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7767 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7770 return set;
7771 error:
7772 isl_set_free(set);
7773 return NULL;
7776 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7777 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7778 void *user)
7780 isl_set *half;
7782 if (!set)
7783 return -1;
7784 if (isl_set_plain_is_empty(set)) {
7785 isl_set_free(set);
7786 return 0;
7788 if (first == len)
7789 return fn(set, signs, user);
7791 signs[first] = 1;
7792 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7793 1 + first));
7794 half = isl_set_intersect(half, isl_set_copy(set));
7795 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7796 goto error;
7798 signs[first] = -1;
7799 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7800 1 + first));
7801 half = isl_set_intersect(half, set);
7802 return foreach_orthant(half, signs, first + 1, len, fn, user);
7803 error:
7804 isl_set_free(set);
7805 return -1;
7808 /* Call "fn" on the intersections of "set" with each of the orthants
7809 * (except for obviously empty intersections). The orthant is identified
7810 * by the signs array, with each entry having value 1 or -1 according
7811 * to the sign of the corresponding variable.
7813 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7814 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7815 void *user)
7817 unsigned nparam;
7818 unsigned nvar;
7819 int *signs;
7820 int r;
7822 if (!set)
7823 return -1;
7824 if (isl_set_plain_is_empty(set))
7825 return 0;
7827 nparam = isl_set_dim(set, isl_dim_param);
7828 nvar = isl_set_dim(set, isl_dim_set);
7830 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7832 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7833 fn, user);
7835 free(signs);
7837 return r;
7840 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7842 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7845 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7846 __isl_keep isl_basic_map *bmap2)
7848 int is_subset;
7849 struct isl_map *map1;
7850 struct isl_map *map2;
7852 if (!bmap1 || !bmap2)
7853 return isl_bool_error;
7855 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7856 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7858 is_subset = isl_map_is_subset(map1, map2);
7860 isl_map_free(map1);
7861 isl_map_free(map2);
7863 return is_subset;
7866 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7867 __isl_keep isl_basic_set *bset2)
7869 return isl_basic_map_is_subset(bset1, bset2);
7872 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7873 __isl_keep isl_basic_map *bmap2)
7875 isl_bool is_subset;
7877 if (!bmap1 || !bmap2)
7878 return isl_bool_error;
7879 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7880 if (is_subset != isl_bool_true)
7881 return is_subset;
7882 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7883 return is_subset;
7886 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7887 __isl_keep isl_basic_set *bset2)
7889 return isl_basic_map_is_equal(
7890 bset_to_bmap(bset1), bset_to_bmap(bset2));
7893 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7895 int i;
7896 int is_empty;
7898 if (!map)
7899 return isl_bool_error;
7900 for (i = 0; i < map->n; ++i) {
7901 is_empty = isl_basic_map_is_empty(map->p[i]);
7902 if (is_empty < 0)
7903 return isl_bool_error;
7904 if (!is_empty)
7905 return isl_bool_false;
7907 return isl_bool_true;
7910 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7912 return map ? map->n == 0 : isl_bool_error;
7915 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7917 return set ? set->n == 0 : isl_bool_error;
7920 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7922 return isl_map_is_empty(set_to_map(set));
7925 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7927 if (!map1 || !map2)
7928 return -1;
7930 return isl_space_is_equal(map1->dim, map2->dim);
7933 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7935 if (!set1 || !set2)
7936 return -1;
7938 return isl_space_is_equal(set1->dim, set2->dim);
7941 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7943 isl_bool is_subset;
7945 if (!map1 || !map2)
7946 return isl_bool_error;
7947 is_subset = isl_map_is_subset(map1, map2);
7948 if (is_subset != isl_bool_true)
7949 return is_subset;
7950 is_subset = isl_map_is_subset(map2, map1);
7951 return is_subset;
7954 /* Is "map1" equal to "map2"?
7956 * First check if they are obviously equal.
7957 * If not, then perform a more detailed analysis.
7959 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7961 isl_bool equal;
7963 equal = isl_map_plain_is_equal(map1, map2);
7964 if (equal < 0 || equal)
7965 return equal;
7966 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7969 isl_bool isl_basic_map_is_strict_subset(
7970 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7972 isl_bool is_subset;
7974 if (!bmap1 || !bmap2)
7975 return isl_bool_error;
7976 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7977 if (is_subset != isl_bool_true)
7978 return is_subset;
7979 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7980 if (is_subset == isl_bool_error)
7981 return is_subset;
7982 return !is_subset;
7985 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7986 __isl_keep isl_map *map2)
7988 isl_bool is_subset;
7990 if (!map1 || !map2)
7991 return isl_bool_error;
7992 is_subset = isl_map_is_subset(map1, map2);
7993 if (is_subset != isl_bool_true)
7994 return is_subset;
7995 is_subset = isl_map_is_subset(map2, map1);
7996 if (is_subset == isl_bool_error)
7997 return is_subset;
7998 return !is_subset;
8001 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8002 __isl_keep isl_set *set2)
8004 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8007 /* Is "bmap" obviously equal to the universe with the same space?
8009 * That is, does it not have any constraints?
8011 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8013 if (!bmap)
8014 return isl_bool_error;
8015 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8018 /* Is "bset" obviously equal to the universe with the same space?
8020 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8022 return isl_basic_map_plain_is_universe(bset);
8025 /* If "c" does not involve any existentially quantified variables,
8026 * then set *univ to false and abort
8028 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8030 isl_bool *univ = user;
8031 unsigned n;
8033 n = isl_constraint_dim(c, isl_dim_div);
8034 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8035 isl_constraint_free(c);
8036 if (*univ < 0 || !*univ)
8037 return isl_stat_error;
8038 return isl_stat_ok;
8041 /* Is "bmap" equal to the universe with the same space?
8043 * First check if it is obviously equal to the universe.
8044 * If not and if there are any constraints not involving
8045 * existentially quantified variables, then it is certainly
8046 * not equal to the universe.
8047 * Otherwise, check if the universe is a subset of "bmap".
8049 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8051 isl_bool univ;
8052 isl_basic_map *test;
8054 univ = isl_basic_map_plain_is_universe(bmap);
8055 if (univ < 0 || univ)
8056 return univ;
8057 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8058 return isl_bool_false;
8059 univ = isl_bool_true;
8060 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8061 univ)
8062 return isl_bool_error;
8063 if (univ < 0 || !univ)
8064 return univ;
8065 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8066 univ = isl_basic_map_is_subset(test, bmap);
8067 isl_basic_map_free(test);
8068 return univ;
8071 /* Is "bset" equal to the universe with the same space?
8073 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8075 return isl_basic_map_is_universe(bset);
8078 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8080 int i;
8082 if (!map)
8083 return isl_bool_error;
8085 for (i = 0; i < map->n; ++i) {
8086 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8087 if (r < 0 || r)
8088 return r;
8091 return isl_bool_false;
8094 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8096 return isl_map_plain_is_universe(set_to_map(set));
8099 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8101 struct isl_basic_set *bset = NULL;
8102 struct isl_vec *sample = NULL;
8103 isl_bool empty, non_empty;
8105 if (!bmap)
8106 return isl_bool_error;
8108 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8109 return isl_bool_true;
8111 if (isl_basic_map_plain_is_universe(bmap))
8112 return isl_bool_false;
8114 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8115 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8116 copy = isl_basic_map_remove_redundancies(copy);
8117 empty = isl_basic_map_plain_is_empty(copy);
8118 isl_basic_map_free(copy);
8119 return empty;
8122 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8123 if (non_empty < 0)
8124 return isl_bool_error;
8125 if (non_empty)
8126 return isl_bool_false;
8127 isl_vec_free(bmap->sample);
8128 bmap->sample = NULL;
8129 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8130 if (!bset)
8131 return isl_bool_error;
8132 sample = isl_basic_set_sample_vec(bset);
8133 if (!sample)
8134 return isl_bool_error;
8135 empty = sample->size == 0;
8136 isl_vec_free(bmap->sample);
8137 bmap->sample = sample;
8138 if (empty)
8139 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8141 return empty;
8144 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8146 if (!bmap)
8147 return isl_bool_error;
8148 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8151 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8153 if (!bset)
8154 return isl_bool_error;
8155 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8158 /* Is "bmap" known to be non-empty?
8160 * That is, is the cached sample still valid?
8162 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8164 unsigned total;
8166 if (!bmap)
8167 return isl_bool_error;
8168 if (!bmap->sample)
8169 return isl_bool_false;
8170 total = 1 + isl_basic_map_total_dim(bmap);
8171 if (bmap->sample->size != total)
8172 return isl_bool_false;
8173 return isl_basic_map_contains(bmap, bmap->sample);
8176 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8178 return isl_basic_map_is_empty(bset_to_bmap(bset));
8181 struct isl_map *isl_basic_map_union(
8182 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8184 struct isl_map *map;
8185 if (!bmap1 || !bmap2)
8186 goto error;
8188 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8190 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8191 if (!map)
8192 goto error;
8193 map = isl_map_add_basic_map(map, bmap1);
8194 map = isl_map_add_basic_map(map, bmap2);
8195 return map;
8196 error:
8197 isl_basic_map_free(bmap1);
8198 isl_basic_map_free(bmap2);
8199 return NULL;
8202 struct isl_set *isl_basic_set_union(
8203 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8205 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8206 bset_to_bmap(bset2)));
8209 /* Order divs such that any div only depends on previous divs */
8210 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8212 int i;
8213 unsigned off;
8215 if (!bmap)
8216 return NULL;
8218 off = isl_space_dim(bmap->dim, isl_dim_all);
8220 for (i = 0; i < bmap->n_div; ++i) {
8221 int pos;
8222 if (isl_int_is_zero(bmap->div[i][0]))
8223 continue;
8224 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8225 bmap->n_div-i);
8226 if (pos == -1)
8227 continue;
8228 if (pos == 0)
8229 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8230 "integer division depends on itself",
8231 return isl_basic_map_free(bmap));
8232 isl_basic_map_swap_div(bmap, i, i + pos);
8233 --i;
8235 return bmap;
8238 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8240 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8243 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8245 int i;
8247 if (!map)
8248 return 0;
8250 for (i = 0; i < map->n; ++i) {
8251 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8252 if (!map->p[i])
8253 goto error;
8256 return map;
8257 error:
8258 isl_map_free(map);
8259 return NULL;
8262 /* Sort the local variables of "bset".
8264 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8265 __isl_take isl_basic_set *bset)
8267 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8270 /* Apply the expansion computed by isl_merge_divs.
8271 * The expansion itself is given by "exp" while the resulting
8272 * list of divs is given by "div".
8274 * Move the integer divisions of "bmap" into the right position
8275 * according to "exp" and then introduce the additional integer
8276 * divisions, adding div constraints.
8277 * The moving should be done first to avoid moving coefficients
8278 * in the definitions of the extra integer divisions.
8280 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8281 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8283 int i, j;
8284 int n_div;
8286 bmap = isl_basic_map_cow(bmap);
8287 if (!bmap || !div)
8288 goto error;
8290 if (div->n_row < bmap->n_div)
8291 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8292 "not an expansion", goto error);
8294 n_div = bmap->n_div;
8295 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8296 div->n_row - n_div, 0,
8297 2 * (div->n_row - n_div));
8299 for (i = n_div; i < div->n_row; ++i)
8300 if (isl_basic_map_alloc_div(bmap) < 0)
8301 goto error;
8303 for (j = n_div - 1; j >= 0; --j) {
8304 if (exp[j] == j)
8305 break;
8306 isl_basic_map_swap_div(bmap, j, exp[j]);
8308 j = 0;
8309 for (i = 0; i < div->n_row; ++i) {
8310 if (j < n_div && exp[j] == i) {
8311 j++;
8312 } else {
8313 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8314 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8315 continue;
8316 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8317 goto error;
8321 isl_mat_free(div);
8322 return bmap;
8323 error:
8324 isl_basic_map_free(bmap);
8325 isl_mat_free(div);
8326 return NULL;
8329 /* Apply the expansion computed by isl_merge_divs.
8330 * The expansion itself is given by "exp" while the resulting
8331 * list of divs is given by "div".
8333 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8334 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8336 return isl_basic_map_expand_divs(bset, div, exp);
8339 /* Look for a div in dst that corresponds to the div "div" in src.
8340 * The divs before "div" in src and dst are assumed to be the same.
8342 * Returns -1 if no corresponding div was found and the position
8343 * of the corresponding div in dst otherwise.
8345 static int find_div(struct isl_basic_map *dst,
8346 struct isl_basic_map *src, unsigned div)
8348 int i;
8350 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8352 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8353 for (i = div; i < dst->n_div; ++i)
8354 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8355 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8356 dst->n_div - div) == -1)
8357 return i;
8358 return -1;
8361 /* Align the divs of "dst" to those of "src", adding divs from "src"
8362 * if needed. That is, make sure that the first src->n_div divs
8363 * of the result are equal to those of src.
8365 * The result is not finalized as by design it will have redundant
8366 * divs if any divs from "src" were copied.
8368 __isl_give isl_basic_map *isl_basic_map_align_divs(
8369 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8371 int i;
8372 int known, extended;
8373 unsigned total;
8375 if (!dst || !src)
8376 return isl_basic_map_free(dst);
8378 if (src->n_div == 0)
8379 return dst;
8381 known = isl_basic_map_divs_known(src);
8382 if (known < 0)
8383 return isl_basic_map_free(dst);
8384 if (!known)
8385 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8386 "some src divs are unknown",
8387 return isl_basic_map_free(dst));
8389 src = isl_basic_map_order_divs(src);
8391 extended = 0;
8392 total = isl_space_dim(src->dim, isl_dim_all);
8393 for (i = 0; i < src->n_div; ++i) {
8394 int j = find_div(dst, src, i);
8395 if (j < 0) {
8396 if (!extended) {
8397 int extra = src->n_div - i;
8398 dst = isl_basic_map_cow(dst);
8399 if (!dst)
8400 return NULL;
8401 dst = isl_basic_map_extend_space(dst,
8402 isl_space_copy(dst->dim),
8403 extra, 0, 2 * extra);
8404 extended = 1;
8406 j = isl_basic_map_alloc_div(dst);
8407 if (j < 0)
8408 return isl_basic_map_free(dst);
8409 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8410 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8411 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8412 return isl_basic_map_free(dst);
8414 if (j != i)
8415 isl_basic_map_swap_div(dst, i, j);
8417 return dst;
8420 struct isl_basic_set *isl_basic_set_align_divs(
8421 struct isl_basic_set *dst, struct isl_basic_set *src)
8423 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8424 bset_to_bmap(src)));
8427 struct isl_map *isl_map_align_divs(struct isl_map *map)
8429 int i;
8431 if (!map)
8432 return NULL;
8433 if (map->n == 0)
8434 return map;
8435 map = isl_map_compute_divs(map);
8436 map = isl_map_cow(map);
8437 if (!map)
8438 return NULL;
8440 for (i = 1; i < map->n; ++i)
8441 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8442 for (i = 1; i < map->n; ++i) {
8443 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8444 if (!map->p[i])
8445 return isl_map_free(map);
8448 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8449 return map;
8452 struct isl_set *isl_set_align_divs(struct isl_set *set)
8454 return set_from_map(isl_map_align_divs(set_to_map(set)));
8457 /* Align the divs of the basic maps in "map" to those
8458 * of the basic maps in "list", as well as to the other basic maps in "map".
8459 * The elements in "list" are assumed to have known divs.
8461 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8462 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8464 int i, n;
8466 map = isl_map_compute_divs(map);
8467 map = isl_map_cow(map);
8468 if (!map || !list)
8469 return isl_map_free(map);
8470 if (map->n == 0)
8471 return map;
8473 n = isl_basic_map_list_n_basic_map(list);
8474 for (i = 0; i < n; ++i) {
8475 isl_basic_map *bmap;
8477 bmap = isl_basic_map_list_get_basic_map(list, i);
8478 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8479 isl_basic_map_free(bmap);
8481 if (!map->p[0])
8482 return isl_map_free(map);
8484 return isl_map_align_divs(map);
8487 /* Align the divs of each element of "list" to those of "bmap".
8488 * Both "bmap" and the elements of "list" are assumed to have known divs.
8490 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8491 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8493 int i, n;
8495 if (!list || !bmap)
8496 return isl_basic_map_list_free(list);
8498 n = isl_basic_map_list_n_basic_map(list);
8499 for (i = 0; i < n; ++i) {
8500 isl_basic_map *bmap_i;
8502 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8503 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8504 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8507 return list;
8510 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8511 __isl_take isl_map *map)
8513 if (!set || !map)
8514 goto error;
8515 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8516 map = isl_map_intersect_domain(map, set);
8517 set = isl_map_range(map);
8518 return set;
8519 error:
8520 isl_set_free(set);
8521 isl_map_free(map);
8522 return NULL;
8525 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8526 __isl_take isl_map *map)
8528 return isl_map_align_params_map_map_and(set, map, &set_apply);
8531 /* There is no need to cow as removing empty parts doesn't change
8532 * the meaning of the set.
8534 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8536 int i;
8538 if (!map)
8539 return NULL;
8541 for (i = map->n - 1; i >= 0; --i)
8542 remove_if_empty(map, i);
8544 return map;
8547 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8549 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8552 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8554 struct isl_basic_map *bmap;
8555 if (!map || map->n == 0)
8556 return NULL;
8557 bmap = map->p[map->n-1];
8558 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8559 return isl_basic_map_copy(bmap);
8562 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8564 return map_copy_basic_map(map);
8567 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8569 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8572 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8573 __isl_keep isl_basic_map *bmap)
8575 int i;
8577 if (!map || !bmap)
8578 goto error;
8579 for (i = map->n-1; i >= 0; --i) {
8580 if (map->p[i] != bmap)
8581 continue;
8582 map = isl_map_cow(map);
8583 if (!map)
8584 goto error;
8585 isl_basic_map_free(map->p[i]);
8586 if (i != map->n-1) {
8587 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8588 map->p[i] = map->p[map->n-1];
8590 map->n--;
8591 return map;
8593 return map;
8594 error:
8595 isl_map_free(map);
8596 return NULL;
8599 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8600 __isl_keep isl_basic_map *bmap)
8602 return map_drop_basic_map(map, bmap);
8605 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8606 struct isl_basic_set *bset)
8608 return set_from_map(map_drop_basic_map(set_to_map(set),
8609 bset_to_bmap(bset)));
8612 /* Given two basic sets bset1 and bset2, compute the maximal difference
8613 * between the values of dimension pos in bset1 and those in bset2
8614 * for any common value of the parameters and dimensions preceding pos.
8616 static enum isl_lp_result basic_set_maximal_difference_at(
8617 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8618 int pos, isl_int *opt)
8620 isl_basic_map *bmap1;
8621 isl_basic_map *bmap2;
8622 struct isl_ctx *ctx;
8623 struct isl_vec *obj;
8624 unsigned total;
8625 unsigned nparam;
8626 unsigned dim1;
8627 enum isl_lp_result res;
8629 if (!bset1 || !bset2)
8630 return isl_lp_error;
8632 nparam = isl_basic_set_n_param(bset1);
8633 dim1 = isl_basic_set_n_dim(bset1);
8635 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8636 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8637 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8638 isl_dim_out, 0, pos);
8639 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8640 isl_dim_out, 0, pos);
8641 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8642 if (!bmap1)
8643 return isl_lp_error;
8645 total = isl_basic_map_total_dim(bmap1);
8646 ctx = bmap1->ctx;
8647 obj = isl_vec_alloc(ctx, 1 + total);
8648 if (!obj)
8649 goto error;
8650 isl_seq_clr(obj->block.data, 1 + total);
8651 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8652 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8653 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8654 opt, NULL, NULL);
8655 isl_basic_map_free(bmap1);
8656 isl_vec_free(obj);
8657 return res;
8658 error:
8659 isl_basic_map_free(bmap1);
8660 return isl_lp_error;
8663 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8664 * for any common value of the parameters and dimensions preceding pos
8665 * in both basic sets, the values of dimension pos in bset1 are
8666 * smaller or larger than those in bset2.
8668 * Returns
8669 * 1 if bset1 follows bset2
8670 * -1 if bset1 precedes bset2
8671 * 0 if bset1 and bset2 are incomparable
8672 * -2 if some error occurred.
8674 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8675 struct isl_basic_set *bset2, int pos)
8677 isl_int opt;
8678 enum isl_lp_result res;
8679 int cmp;
8681 isl_int_init(opt);
8683 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8685 if (res == isl_lp_empty)
8686 cmp = 0;
8687 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8688 res == isl_lp_unbounded)
8689 cmp = 1;
8690 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8691 cmp = -1;
8692 else
8693 cmp = -2;
8695 isl_int_clear(opt);
8696 return cmp;
8699 /* Given two basic sets bset1 and bset2, check whether
8700 * for any common value of the parameters and dimensions preceding pos
8701 * there is a value of dimension pos in bset1 that is larger
8702 * than a value of the same dimension in bset2.
8704 * Return
8705 * 1 if there exists such a pair
8706 * 0 if there is no such pair, but there is a pair of equal values
8707 * -1 otherwise
8708 * -2 if some error occurred.
8710 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8711 __isl_keep isl_basic_set *bset2, int pos)
8713 isl_int opt;
8714 enum isl_lp_result res;
8715 int cmp;
8717 isl_int_init(opt);
8719 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8721 if (res == isl_lp_empty)
8722 cmp = -1;
8723 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8724 res == isl_lp_unbounded)
8725 cmp = 1;
8726 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8727 cmp = -1;
8728 else if (res == isl_lp_ok)
8729 cmp = 0;
8730 else
8731 cmp = -2;
8733 isl_int_clear(opt);
8734 return cmp;
8737 /* Given two sets set1 and set2, check whether
8738 * for any common value of the parameters and dimensions preceding pos
8739 * there is a value of dimension pos in set1 that is larger
8740 * than a value of the same dimension in set2.
8742 * Return
8743 * 1 if there exists such a pair
8744 * 0 if there is no such pair, but there is a pair of equal values
8745 * -1 otherwise
8746 * -2 if some error occurred.
8748 int isl_set_follows_at(__isl_keep isl_set *set1,
8749 __isl_keep isl_set *set2, int pos)
8751 int i, j;
8752 int follows = -1;
8754 if (!set1 || !set2)
8755 return -2;
8757 for (i = 0; i < set1->n; ++i)
8758 for (j = 0; j < set2->n; ++j) {
8759 int f;
8760 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8761 if (f == 1 || f == -2)
8762 return f;
8763 if (f > follows)
8764 follows = f;
8767 return follows;
8770 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8771 unsigned pos, isl_int *val)
8773 int i;
8774 int d;
8775 unsigned total;
8777 if (!bmap)
8778 return -1;
8779 total = isl_basic_map_total_dim(bmap);
8780 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8781 for (; d+1 > pos; --d)
8782 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8783 break;
8784 if (d != pos)
8785 continue;
8786 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8787 return 0;
8788 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8789 return 0;
8790 if (!isl_int_is_one(bmap->eq[i][1+d]))
8791 return 0;
8792 if (val)
8793 isl_int_neg(*val, bmap->eq[i][0]);
8794 return 1;
8796 return 0;
8799 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8800 unsigned pos, isl_int *val)
8802 int i;
8803 isl_int v;
8804 isl_int tmp;
8805 int fixed;
8807 if (!map)
8808 return -1;
8809 if (map->n == 0)
8810 return 0;
8811 if (map->n == 1)
8812 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8813 isl_int_init(v);
8814 isl_int_init(tmp);
8815 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8816 for (i = 1; fixed == 1 && i < map->n; ++i) {
8817 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8818 if (fixed == 1 && isl_int_ne(tmp, v))
8819 fixed = 0;
8821 if (val)
8822 isl_int_set(*val, v);
8823 isl_int_clear(tmp);
8824 isl_int_clear(v);
8825 return fixed;
8828 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8829 unsigned pos, isl_int *val)
8831 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8832 pos, val);
8835 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8836 isl_int *val)
8838 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
8841 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8842 enum isl_dim_type type, unsigned pos, isl_int *val)
8844 if (pos >= isl_basic_map_dim(bmap, type))
8845 return -1;
8846 return isl_basic_map_plain_has_fixed_var(bmap,
8847 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8850 /* If "bmap" obviously lies on a hyperplane where the given dimension
8851 * has a fixed value, then return that value.
8852 * Otherwise return NaN.
8854 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8855 __isl_keep isl_basic_map *bmap,
8856 enum isl_dim_type type, unsigned pos)
8858 isl_ctx *ctx;
8859 isl_val *v;
8860 int fixed;
8862 if (!bmap)
8863 return NULL;
8864 ctx = isl_basic_map_get_ctx(bmap);
8865 v = isl_val_alloc(ctx);
8866 if (!v)
8867 return NULL;
8868 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8869 if (fixed < 0)
8870 return isl_val_free(v);
8871 if (fixed) {
8872 isl_int_set_si(v->d, 1);
8873 return v;
8875 isl_val_free(v);
8876 return isl_val_nan(ctx);
8879 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8880 enum isl_dim_type type, unsigned pos, isl_int *val)
8882 if (pos >= isl_map_dim(map, type))
8883 return -1;
8884 return isl_map_plain_has_fixed_var(map,
8885 map_offset(map, type) - 1 + pos, val);
8888 /* If "map" obviously lies on a hyperplane where the given dimension
8889 * has a fixed value, then return that value.
8890 * Otherwise return NaN.
8892 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8893 enum isl_dim_type type, unsigned pos)
8895 isl_ctx *ctx;
8896 isl_val *v;
8897 int fixed;
8899 if (!map)
8900 return NULL;
8901 ctx = isl_map_get_ctx(map);
8902 v = isl_val_alloc(ctx);
8903 if (!v)
8904 return NULL;
8905 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8906 if (fixed < 0)
8907 return isl_val_free(v);
8908 if (fixed) {
8909 isl_int_set_si(v->d, 1);
8910 return v;
8912 isl_val_free(v);
8913 return isl_val_nan(ctx);
8916 /* If "set" obviously lies on a hyperplane where the given dimension
8917 * has a fixed value, then return that value.
8918 * Otherwise return NaN.
8920 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8921 enum isl_dim_type type, unsigned pos)
8923 return isl_map_plain_get_val_if_fixed(set, type, pos);
8926 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8927 enum isl_dim_type type, unsigned pos, isl_int *val)
8929 return isl_map_plain_is_fixed(set, type, pos, val);
8932 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8933 * then return this fixed value in *val.
8935 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8936 unsigned dim, isl_int *val)
8938 return isl_basic_set_plain_has_fixed_var(bset,
8939 isl_basic_set_n_param(bset) + dim, 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_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8946 unsigned dim, isl_int *val)
8948 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8951 /* Check if input variable in has fixed value and if so and if val is not NULL,
8952 * then return this fixed value in *val.
8954 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8955 unsigned in, isl_int *val)
8957 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8960 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8961 * and if val is not NULL, then return this lower bound in *val.
8963 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8964 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8966 int i, i_eq = -1, i_ineq = -1;
8967 isl_int *c;
8968 unsigned total;
8969 unsigned nparam;
8971 if (!bset)
8972 return -1;
8973 total = isl_basic_set_total_dim(bset);
8974 nparam = isl_basic_set_n_param(bset);
8975 for (i = 0; i < bset->n_eq; ++i) {
8976 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8977 continue;
8978 if (i_eq != -1)
8979 return 0;
8980 i_eq = i;
8982 for (i = 0; i < bset->n_ineq; ++i) {
8983 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8984 continue;
8985 if (i_eq != -1 || i_ineq != -1)
8986 return 0;
8987 i_ineq = i;
8989 if (i_eq == -1 && i_ineq == -1)
8990 return 0;
8991 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8992 /* The coefficient should always be one due to normalization. */
8993 if (!isl_int_is_one(c[1+nparam+dim]))
8994 return 0;
8995 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8996 return 0;
8997 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8998 total - nparam - dim - 1) != -1)
8999 return 0;
9000 if (val)
9001 isl_int_neg(*val, c[0]);
9002 return 1;
9005 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9006 unsigned dim, isl_int *val)
9008 int i;
9009 isl_int v;
9010 isl_int tmp;
9011 int fixed;
9013 if (!set)
9014 return -1;
9015 if (set->n == 0)
9016 return 0;
9017 if (set->n == 1)
9018 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9019 dim, val);
9020 isl_int_init(v);
9021 isl_int_init(tmp);
9022 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9023 dim, &v);
9024 for (i = 1; fixed == 1 && i < set->n; ++i) {
9025 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9026 dim, &tmp);
9027 if (fixed == 1 && isl_int_ne(tmp, v))
9028 fixed = 0;
9030 if (val)
9031 isl_int_set(*val, v);
9032 isl_int_clear(tmp);
9033 isl_int_clear(v);
9034 return fixed;
9037 /* Return -1 if the constraint "c1" should be sorted before "c2"
9038 * and 1 if it should be sorted after "c2".
9039 * Return 0 if the two constraints are the same (up to the constant term).
9041 * In particular, if a constraint involves later variables than another
9042 * then it is sorted after this other constraint.
9043 * uset_gist depends on constraints without existentially quantified
9044 * variables sorting first.
9046 * For constraints that have the same latest variable, those
9047 * with the same coefficient for this latest variable (first in absolute value
9048 * and then in actual value) are grouped together.
9049 * This is useful for detecting pairs of constraints that can
9050 * be chained in their printed representation.
9052 * Finally, within a group, constraints are sorted according to
9053 * their coefficients (excluding the constant term).
9055 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9057 isl_int **c1 = (isl_int **) p1;
9058 isl_int **c2 = (isl_int **) p2;
9059 int l1, l2;
9060 unsigned size = *(unsigned *) arg;
9061 int cmp;
9063 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9064 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9066 if (l1 != l2)
9067 return l1 - l2;
9069 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9070 if (cmp != 0)
9071 return cmp;
9072 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9073 if (cmp != 0)
9074 return -cmp;
9076 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9079 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9080 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9081 * and 0 if the two constraints are the same (up to the constant term).
9083 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9084 isl_int *c1, isl_int *c2)
9086 unsigned total;
9088 if (!bmap)
9089 return -2;
9090 total = isl_basic_map_total_dim(bmap);
9091 return sort_constraint_cmp(&c1, &c2, &total);
9094 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9095 __isl_take isl_basic_map *bmap)
9097 unsigned total;
9099 if (!bmap)
9100 return NULL;
9101 if (bmap->n_ineq == 0)
9102 return bmap;
9103 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9104 return bmap;
9105 total = isl_basic_map_total_dim(bmap);
9106 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9107 &sort_constraint_cmp, &total) < 0)
9108 return isl_basic_map_free(bmap);
9109 return bmap;
9112 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9113 __isl_take isl_basic_set *bset)
9115 isl_basic_map *bmap = bset_to_bmap(bset);
9116 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9119 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9121 if (!bmap)
9122 return NULL;
9123 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9124 return bmap;
9125 bmap = isl_basic_map_remove_redundancies(bmap);
9126 bmap = isl_basic_map_sort_constraints(bmap);
9127 if (bmap)
9128 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9129 return bmap;
9132 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9134 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9137 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9138 const __isl_keep isl_basic_map *bmap2)
9140 int i, cmp;
9141 unsigned total;
9143 if (!bmap1 || !bmap2)
9144 return -1;
9146 if (bmap1 == bmap2)
9147 return 0;
9148 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9149 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9150 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9151 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9152 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9153 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9154 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9155 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9156 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9157 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9158 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9159 return 0;
9160 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9161 return 1;
9162 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9163 return -1;
9164 if (bmap1->n_eq != bmap2->n_eq)
9165 return bmap1->n_eq - bmap2->n_eq;
9166 if (bmap1->n_ineq != bmap2->n_ineq)
9167 return bmap1->n_ineq - bmap2->n_ineq;
9168 if (bmap1->n_div != bmap2->n_div)
9169 return bmap1->n_div - bmap2->n_div;
9170 total = isl_basic_map_total_dim(bmap1);
9171 for (i = 0; i < bmap1->n_eq; ++i) {
9172 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9173 if (cmp)
9174 return cmp;
9176 for (i = 0; i < bmap1->n_ineq; ++i) {
9177 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9178 if (cmp)
9179 return cmp;
9181 for (i = 0; i < bmap1->n_div; ++i) {
9182 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9183 if (cmp)
9184 return cmp;
9186 return 0;
9189 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9190 const __isl_keep isl_basic_set *bset2)
9192 return isl_basic_map_plain_cmp(bset1, bset2);
9195 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9197 int i, cmp;
9199 if (set1 == set2)
9200 return 0;
9201 if (set1->n != set2->n)
9202 return set1->n - set2->n;
9204 for (i = 0; i < set1->n; ++i) {
9205 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9206 if (cmp)
9207 return cmp;
9210 return 0;
9213 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9214 __isl_keep isl_basic_map *bmap2)
9216 if (!bmap1 || !bmap2)
9217 return isl_bool_error;
9218 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9221 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9222 __isl_keep isl_basic_set *bset2)
9224 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9225 bset_to_bmap(bset2));
9228 static int qsort_bmap_cmp(const void *p1, const void *p2)
9230 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9231 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9233 return isl_basic_map_plain_cmp(bmap1, bmap2);
9236 /* Sort the basic maps of "map" and remove duplicate basic maps.
9238 * While removing basic maps, we make sure that the basic maps remain
9239 * sorted because isl_map_normalize expects the basic maps of the result
9240 * to be sorted.
9242 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9244 int i, j;
9246 map = isl_map_remove_empty_parts(map);
9247 if (!map)
9248 return NULL;
9249 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9250 for (i = map->n - 1; i >= 1; --i) {
9251 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9252 continue;
9253 isl_basic_map_free(map->p[i-1]);
9254 for (j = i; j < map->n; ++j)
9255 map->p[j - 1] = map->p[j];
9256 map->n--;
9259 return map;
9262 /* Remove obvious duplicates among the basic maps of "map".
9264 * Unlike isl_map_normalize, this function does not remove redundant
9265 * constraints and only removes duplicates that have exactly the same
9266 * constraints in the input. It does sort the constraints and
9267 * the basic maps to ease the detection of duplicates.
9269 * If "map" has already been normalized or if the basic maps are
9270 * disjoint, then there can be no duplicates.
9272 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9274 int i;
9275 isl_basic_map *bmap;
9277 if (!map)
9278 return NULL;
9279 if (map->n <= 1)
9280 return map;
9281 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9282 return map;
9283 for (i = 0; i < map->n; ++i) {
9284 bmap = isl_basic_map_copy(map->p[i]);
9285 bmap = isl_basic_map_sort_constraints(bmap);
9286 if (!bmap)
9287 return isl_map_free(map);
9288 isl_basic_map_free(map->p[i]);
9289 map->p[i] = bmap;
9292 map = sort_and_remove_duplicates(map);
9293 return map;
9296 /* We normalize in place, but if anything goes wrong we need
9297 * to return NULL, so we need to make sure we don't change the
9298 * meaning of any possible other copies of map.
9300 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9302 int i;
9303 struct isl_basic_map *bmap;
9305 if (!map)
9306 return NULL;
9307 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9308 return map;
9309 for (i = 0; i < map->n; ++i) {
9310 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9311 if (!bmap)
9312 goto error;
9313 isl_basic_map_free(map->p[i]);
9314 map->p[i] = bmap;
9317 map = sort_and_remove_duplicates(map);
9318 if (map)
9319 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9320 return map;
9321 error:
9322 isl_map_free(map);
9323 return NULL;
9326 struct isl_set *isl_set_normalize(struct isl_set *set)
9328 return set_from_map(isl_map_normalize(set_to_map(set)));
9331 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9332 __isl_keep isl_map *map2)
9334 int i;
9335 isl_bool equal;
9337 if (!map1 || !map2)
9338 return isl_bool_error;
9340 if (map1 == map2)
9341 return isl_bool_true;
9342 if (!isl_space_is_equal(map1->dim, map2->dim))
9343 return isl_bool_false;
9345 map1 = isl_map_copy(map1);
9346 map2 = isl_map_copy(map2);
9347 map1 = isl_map_normalize(map1);
9348 map2 = isl_map_normalize(map2);
9349 if (!map1 || !map2)
9350 goto error;
9351 equal = map1->n == map2->n;
9352 for (i = 0; equal && i < map1->n; ++i) {
9353 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9354 if (equal < 0)
9355 goto error;
9357 isl_map_free(map1);
9358 isl_map_free(map2);
9359 return equal;
9360 error:
9361 isl_map_free(map1);
9362 isl_map_free(map2);
9363 return isl_bool_error;
9366 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9367 __isl_keep isl_set *set2)
9369 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9372 /* Return an interval that ranges from min to max (inclusive)
9374 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9375 isl_int min, isl_int max)
9377 int k;
9378 struct isl_basic_set *bset = NULL;
9380 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9381 if (!bset)
9382 goto error;
9384 k = isl_basic_set_alloc_inequality(bset);
9385 if (k < 0)
9386 goto error;
9387 isl_int_set_si(bset->ineq[k][1], 1);
9388 isl_int_neg(bset->ineq[k][0], min);
9390 k = isl_basic_set_alloc_inequality(bset);
9391 if (k < 0)
9392 goto error;
9393 isl_int_set_si(bset->ineq[k][1], -1);
9394 isl_int_set(bset->ineq[k][0], max);
9396 return bset;
9397 error:
9398 isl_basic_set_free(bset);
9399 return NULL;
9402 /* Return the basic maps in "map" as a list.
9404 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9405 __isl_keep isl_map *map)
9407 int i;
9408 isl_ctx *ctx;
9409 isl_basic_map_list *list;
9411 if (!map)
9412 return NULL;
9413 ctx = isl_map_get_ctx(map);
9414 list = isl_basic_map_list_alloc(ctx, map->n);
9416 for (i = 0; i < map->n; ++i) {
9417 isl_basic_map *bmap;
9419 bmap = isl_basic_map_copy(map->p[i]);
9420 list = isl_basic_map_list_add(list, bmap);
9423 return list;
9426 /* Return the intersection of the elements in the non-empty list "list".
9427 * All elements are assumed to live in the same space.
9429 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9430 __isl_take isl_basic_map_list *list)
9432 int i, n;
9433 isl_basic_map *bmap;
9435 if (!list)
9436 return NULL;
9437 n = isl_basic_map_list_n_basic_map(list);
9438 if (n < 1)
9439 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9440 "expecting non-empty list", goto error);
9442 bmap = isl_basic_map_list_get_basic_map(list, 0);
9443 for (i = 1; i < n; ++i) {
9444 isl_basic_map *bmap_i;
9446 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9447 bmap = isl_basic_map_intersect(bmap, bmap_i);
9450 isl_basic_map_list_free(list);
9451 return bmap;
9452 error:
9453 isl_basic_map_list_free(list);
9454 return NULL;
9457 /* Return the intersection of the elements in the non-empty list "list".
9458 * All elements are assumed to live in the same space.
9460 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9461 __isl_take isl_basic_set_list *list)
9463 return isl_basic_map_list_intersect(list);
9466 /* Return the union of the elements of "list".
9467 * The list is required to have at least one element.
9469 __isl_give isl_set *isl_basic_set_list_union(
9470 __isl_take isl_basic_set_list *list)
9472 int i, n;
9473 isl_space *space;
9474 isl_basic_set *bset;
9475 isl_set *set;
9477 if (!list)
9478 return NULL;
9479 n = isl_basic_set_list_n_basic_set(list);
9480 if (n < 1)
9481 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9482 "expecting non-empty list", goto error);
9484 bset = isl_basic_set_list_get_basic_set(list, 0);
9485 space = isl_basic_set_get_space(bset);
9486 isl_basic_set_free(bset);
9488 set = isl_set_alloc_space(space, n, 0);
9489 for (i = 0; i < n; ++i) {
9490 bset = isl_basic_set_list_get_basic_set(list, i);
9491 set = isl_set_add_basic_set(set, bset);
9494 isl_basic_set_list_free(list);
9495 return set;
9496 error:
9497 isl_basic_set_list_free(list);
9498 return NULL;
9501 /* Return the union of the elements in the non-empty list "list".
9502 * All elements are assumed to live in the same space.
9504 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9506 int i, n;
9507 isl_set *set;
9509 if (!list)
9510 return NULL;
9511 n = isl_set_list_n_set(list);
9512 if (n < 1)
9513 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9514 "expecting non-empty list", goto error);
9516 set = isl_set_list_get_set(list, 0);
9517 for (i = 1; i < n; ++i) {
9518 isl_set *set_i;
9520 set_i = isl_set_list_get_set(list, i);
9521 set = isl_set_union(set, set_i);
9524 isl_set_list_free(list);
9525 return set;
9526 error:
9527 isl_set_list_free(list);
9528 return NULL;
9531 /* Return the Cartesian product of the basic sets in list (in the given order).
9533 __isl_give isl_basic_set *isl_basic_set_list_product(
9534 __isl_take struct isl_basic_set_list *list)
9536 int i;
9537 unsigned dim;
9538 unsigned nparam;
9539 unsigned extra;
9540 unsigned n_eq;
9541 unsigned n_ineq;
9542 struct isl_basic_set *product = NULL;
9544 if (!list)
9545 goto error;
9546 isl_assert(list->ctx, list->n > 0, goto error);
9547 isl_assert(list->ctx, list->p[0], goto error);
9548 nparam = isl_basic_set_n_param(list->p[0]);
9549 dim = isl_basic_set_n_dim(list->p[0]);
9550 extra = list->p[0]->n_div;
9551 n_eq = list->p[0]->n_eq;
9552 n_ineq = list->p[0]->n_ineq;
9553 for (i = 1; i < list->n; ++i) {
9554 isl_assert(list->ctx, list->p[i], goto error);
9555 isl_assert(list->ctx,
9556 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9557 dim += isl_basic_set_n_dim(list->p[i]);
9558 extra += list->p[i]->n_div;
9559 n_eq += list->p[i]->n_eq;
9560 n_ineq += list->p[i]->n_ineq;
9562 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9563 n_eq, n_ineq);
9564 if (!product)
9565 goto error;
9566 dim = 0;
9567 for (i = 0; i < list->n; ++i) {
9568 isl_basic_set_add_constraints(product,
9569 isl_basic_set_copy(list->p[i]), dim);
9570 dim += isl_basic_set_n_dim(list->p[i]);
9572 isl_basic_set_list_free(list);
9573 return product;
9574 error:
9575 isl_basic_set_free(product);
9576 isl_basic_set_list_free(list);
9577 return NULL;
9580 struct isl_basic_map *isl_basic_map_product(
9581 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9583 isl_space *dim_result = NULL;
9584 struct isl_basic_map *bmap;
9585 unsigned in1, in2, out1, out2, nparam, total, pos;
9586 struct isl_dim_map *dim_map1, *dim_map2;
9588 if (!bmap1 || !bmap2)
9589 goto error;
9591 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9592 bmap2->dim, isl_dim_param), goto error);
9593 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9594 isl_space_copy(bmap2->dim));
9596 in1 = isl_basic_map_n_in(bmap1);
9597 in2 = isl_basic_map_n_in(bmap2);
9598 out1 = isl_basic_map_n_out(bmap1);
9599 out2 = isl_basic_map_n_out(bmap2);
9600 nparam = isl_basic_map_n_param(bmap1);
9602 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9603 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9604 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9605 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9606 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9607 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9608 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9609 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9610 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9611 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9612 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9614 bmap = isl_basic_map_alloc_space(dim_result,
9615 bmap1->n_div + bmap2->n_div,
9616 bmap1->n_eq + bmap2->n_eq,
9617 bmap1->n_ineq + bmap2->n_ineq);
9618 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9619 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9620 bmap = isl_basic_map_simplify(bmap);
9621 return isl_basic_map_finalize(bmap);
9622 error:
9623 isl_basic_map_free(bmap1);
9624 isl_basic_map_free(bmap2);
9625 return NULL;
9628 __isl_give isl_basic_map *isl_basic_map_flat_product(
9629 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9631 isl_basic_map *prod;
9633 prod = isl_basic_map_product(bmap1, bmap2);
9634 prod = isl_basic_map_flatten(prod);
9635 return prod;
9638 __isl_give isl_basic_set *isl_basic_set_flat_product(
9639 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9641 return isl_basic_map_flat_range_product(bset1, bset2);
9644 __isl_give isl_basic_map *isl_basic_map_domain_product(
9645 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9647 isl_space *space_result = NULL;
9648 isl_basic_map *bmap;
9649 unsigned in1, in2, out, nparam, total, pos;
9650 struct isl_dim_map *dim_map1, *dim_map2;
9652 if (!bmap1 || !bmap2)
9653 goto error;
9655 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9656 isl_space_copy(bmap2->dim));
9658 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9659 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9660 out = isl_basic_map_dim(bmap1, isl_dim_out);
9661 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9663 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9664 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9665 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9666 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9667 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9668 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9669 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9670 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9671 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9672 isl_dim_map_div(dim_map1, bmap1, pos += out);
9673 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9675 bmap = isl_basic_map_alloc_space(space_result,
9676 bmap1->n_div + bmap2->n_div,
9677 bmap1->n_eq + bmap2->n_eq,
9678 bmap1->n_ineq + bmap2->n_ineq);
9679 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9680 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9681 bmap = isl_basic_map_simplify(bmap);
9682 return isl_basic_map_finalize(bmap);
9683 error:
9684 isl_basic_map_free(bmap1);
9685 isl_basic_map_free(bmap2);
9686 return NULL;
9689 __isl_give isl_basic_map *isl_basic_map_range_product(
9690 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9692 int rational;
9693 isl_space *dim_result = NULL;
9694 isl_basic_map *bmap;
9695 unsigned in, out1, out2, nparam, total, pos;
9696 struct isl_dim_map *dim_map1, *dim_map2;
9698 rational = isl_basic_map_is_rational(bmap1);
9699 if (rational >= 0 && rational)
9700 rational = isl_basic_map_is_rational(bmap2);
9701 if (!bmap1 || !bmap2 || rational < 0)
9702 goto error;
9704 if (!isl_space_match(bmap1->dim, isl_dim_param,
9705 bmap2->dim, isl_dim_param))
9706 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9707 "parameters don't match", goto error);
9709 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9710 isl_space_copy(bmap2->dim));
9712 in = isl_basic_map_dim(bmap1, isl_dim_in);
9713 out1 = isl_basic_map_n_out(bmap1);
9714 out2 = isl_basic_map_n_out(bmap2);
9715 nparam = isl_basic_map_n_param(bmap1);
9717 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9718 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9719 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9720 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9721 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9722 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9723 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9724 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9725 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9726 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9727 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9729 bmap = isl_basic_map_alloc_space(dim_result,
9730 bmap1->n_div + bmap2->n_div,
9731 bmap1->n_eq + bmap2->n_eq,
9732 bmap1->n_ineq + bmap2->n_ineq);
9733 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9734 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9735 if (rational)
9736 bmap = isl_basic_map_set_rational(bmap);
9737 bmap = isl_basic_map_simplify(bmap);
9738 return isl_basic_map_finalize(bmap);
9739 error:
9740 isl_basic_map_free(bmap1);
9741 isl_basic_map_free(bmap2);
9742 return NULL;
9745 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9746 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9748 isl_basic_map *prod;
9750 prod = isl_basic_map_range_product(bmap1, bmap2);
9751 prod = isl_basic_map_flatten_range(prod);
9752 return prod;
9755 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9756 * and collect the results.
9757 * The result live in the space obtained by calling "space_product"
9758 * on the spaces of "map1" and "map2".
9759 * If "remove_duplicates" is set then the result may contain duplicates
9760 * (even if the inputs do not) and so we try and remove the obvious
9761 * duplicates.
9763 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9764 __isl_take isl_map *map2,
9765 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9766 __isl_take isl_space *right),
9767 __isl_give isl_basic_map *(*basic_map_product)(
9768 __isl_take isl_basic_map *left,
9769 __isl_take isl_basic_map *right),
9770 int remove_duplicates)
9772 unsigned flags = 0;
9773 struct isl_map *result;
9774 int i, j;
9776 if (!map1 || !map2)
9777 goto error;
9779 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9780 map2->dim, isl_dim_param), goto error);
9782 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9783 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9784 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9786 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9787 isl_space_copy(map2->dim)),
9788 map1->n * map2->n, flags);
9789 if (!result)
9790 goto error;
9791 for (i = 0; i < map1->n; ++i)
9792 for (j = 0; j < map2->n; ++j) {
9793 struct isl_basic_map *part;
9794 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9795 isl_basic_map_copy(map2->p[j]));
9796 if (isl_basic_map_is_empty(part))
9797 isl_basic_map_free(part);
9798 else
9799 result = isl_map_add_basic_map(result, part);
9800 if (!result)
9801 goto error;
9803 if (remove_duplicates)
9804 result = isl_map_remove_obvious_duplicates(result);
9805 isl_map_free(map1);
9806 isl_map_free(map2);
9807 return result;
9808 error:
9809 isl_map_free(map1);
9810 isl_map_free(map2);
9811 return NULL;
9814 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9816 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9817 __isl_take isl_map *map2)
9819 return map_product(map1, map2, &isl_space_product,
9820 &isl_basic_map_product, 0);
9823 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9824 __isl_take isl_map *map2)
9826 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9829 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9831 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9832 __isl_take isl_map *map2)
9834 isl_map *prod;
9836 prod = isl_map_product(map1, map2);
9837 prod = isl_map_flatten(prod);
9838 return prod;
9841 /* Given two set A and B, construct its Cartesian product A x B.
9843 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9845 return isl_map_range_product(set1, set2);
9848 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9849 __isl_take isl_set *set2)
9851 return isl_map_flat_range_product(set1, set2);
9854 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9856 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9857 __isl_take isl_map *map2)
9859 return map_product(map1, map2, &isl_space_domain_product,
9860 &isl_basic_map_domain_product, 1);
9863 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9865 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9866 __isl_take isl_map *map2)
9868 return map_product(map1, map2, &isl_space_range_product,
9869 &isl_basic_map_range_product, 1);
9872 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9873 __isl_take isl_map *map2)
9875 return isl_map_align_params_map_map_and(map1, map2,
9876 &map_domain_product_aligned);
9879 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9880 __isl_take isl_map *map2)
9882 return isl_map_align_params_map_map_and(map1, map2,
9883 &map_range_product_aligned);
9886 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9888 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9890 isl_space *space;
9891 int total1, keep1, total2, keep2;
9893 if (!map)
9894 return NULL;
9895 if (!isl_space_domain_is_wrapping(map->dim) ||
9896 !isl_space_range_is_wrapping(map->dim))
9897 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9898 "not a product", return isl_map_free(map));
9900 space = isl_map_get_space(map);
9901 total1 = isl_space_dim(space, isl_dim_in);
9902 total2 = isl_space_dim(space, isl_dim_out);
9903 space = isl_space_factor_domain(space);
9904 keep1 = isl_space_dim(space, isl_dim_in);
9905 keep2 = isl_space_dim(space, isl_dim_out);
9906 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9907 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9908 map = isl_map_reset_space(map, space);
9910 return map;
9913 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9915 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9917 isl_space *space;
9918 int total1, keep1, total2, keep2;
9920 if (!map)
9921 return NULL;
9922 if (!isl_space_domain_is_wrapping(map->dim) ||
9923 !isl_space_range_is_wrapping(map->dim))
9924 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9925 "not a product", return isl_map_free(map));
9927 space = isl_map_get_space(map);
9928 total1 = isl_space_dim(space, isl_dim_in);
9929 total2 = isl_space_dim(space, isl_dim_out);
9930 space = isl_space_factor_range(space);
9931 keep1 = isl_space_dim(space, isl_dim_in);
9932 keep2 = isl_space_dim(space, isl_dim_out);
9933 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9934 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9935 map = isl_map_reset_space(map, space);
9937 return map;
9940 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9942 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9944 isl_space *space;
9945 int total, keep;
9947 if (!map)
9948 return NULL;
9949 if (!isl_space_domain_is_wrapping(map->dim))
9950 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9951 "domain is not a product", return isl_map_free(map));
9953 space = isl_map_get_space(map);
9954 total = isl_space_dim(space, isl_dim_in);
9955 space = isl_space_domain_factor_domain(space);
9956 keep = isl_space_dim(space, isl_dim_in);
9957 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9958 map = isl_map_reset_space(map, space);
9960 return map;
9963 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9965 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9967 isl_space *space;
9968 int total, keep;
9970 if (!map)
9971 return NULL;
9972 if (!isl_space_domain_is_wrapping(map->dim))
9973 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9974 "domain is not a product", return isl_map_free(map));
9976 space = isl_map_get_space(map);
9977 total = isl_space_dim(space, isl_dim_in);
9978 space = isl_space_domain_factor_range(space);
9979 keep = isl_space_dim(space, isl_dim_in);
9980 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9981 map = isl_map_reset_space(map, space);
9983 return map;
9986 /* Given a map A -> [B -> C], extract the map A -> B.
9988 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9990 isl_space *space;
9991 int total, keep;
9993 if (!map)
9994 return NULL;
9995 if (!isl_space_range_is_wrapping(map->dim))
9996 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9997 "range is not a product", return isl_map_free(map));
9999 space = isl_map_get_space(map);
10000 total = isl_space_dim(space, isl_dim_out);
10001 space = isl_space_range_factor_domain(space);
10002 keep = isl_space_dim(space, isl_dim_out);
10003 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10004 map = isl_map_reset_space(map, space);
10006 return map;
10009 /* Given a map A -> [B -> C], extract the map A -> C.
10011 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10013 isl_space *space;
10014 int total, keep;
10016 if (!map)
10017 return NULL;
10018 if (!isl_space_range_is_wrapping(map->dim))
10019 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10020 "range is not a product", return isl_map_free(map));
10022 space = isl_map_get_space(map);
10023 total = isl_space_dim(space, isl_dim_out);
10024 space = isl_space_range_factor_range(space);
10025 keep = isl_space_dim(space, isl_dim_out);
10026 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10027 map = isl_map_reset_space(map, space);
10029 return map;
10032 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10034 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10035 __isl_take isl_map *map2)
10037 isl_map *prod;
10039 prod = isl_map_domain_product(map1, map2);
10040 prod = isl_map_flatten_domain(prod);
10041 return prod;
10044 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10046 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10047 __isl_take isl_map *map2)
10049 isl_map *prod;
10051 prod = isl_map_range_product(map1, map2);
10052 prod = isl_map_flatten_range(prod);
10053 return prod;
10056 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10058 int i;
10059 uint32_t hash = isl_hash_init();
10060 unsigned total;
10062 if (!bmap)
10063 return 0;
10064 bmap = isl_basic_map_copy(bmap);
10065 bmap = isl_basic_map_normalize(bmap);
10066 if (!bmap)
10067 return 0;
10068 total = isl_basic_map_total_dim(bmap);
10069 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10070 for (i = 0; i < bmap->n_eq; ++i) {
10071 uint32_t c_hash;
10072 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10073 isl_hash_hash(hash, c_hash);
10075 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10076 for (i = 0; i < bmap->n_ineq; ++i) {
10077 uint32_t c_hash;
10078 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10079 isl_hash_hash(hash, c_hash);
10081 isl_hash_byte(hash, bmap->n_div & 0xFF);
10082 for (i = 0; i < bmap->n_div; ++i) {
10083 uint32_t c_hash;
10084 if (isl_int_is_zero(bmap->div[i][0]))
10085 continue;
10086 isl_hash_byte(hash, i & 0xFF);
10087 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10088 isl_hash_hash(hash, c_hash);
10090 isl_basic_map_free(bmap);
10091 return hash;
10094 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10096 return isl_basic_map_get_hash(bset_to_bmap(bset));
10099 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10101 int i;
10102 uint32_t hash;
10104 if (!map)
10105 return 0;
10106 map = isl_map_copy(map);
10107 map = isl_map_normalize(map);
10108 if (!map)
10109 return 0;
10111 hash = isl_hash_init();
10112 for (i = 0; i < map->n; ++i) {
10113 uint32_t bmap_hash;
10114 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10115 isl_hash_hash(hash, bmap_hash);
10118 isl_map_free(map);
10120 return hash;
10123 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10125 return isl_map_get_hash(set_to_map(set));
10128 /* Check if the value for dimension dim is completely determined
10129 * by the values of the other parameters and variables.
10130 * That is, check if dimension dim is involved in an equality.
10132 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10134 int i;
10135 unsigned nparam;
10137 if (!bset)
10138 return -1;
10139 nparam = isl_basic_set_n_param(bset);
10140 for (i = 0; i < bset->n_eq; ++i)
10141 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10142 return 1;
10143 return 0;
10146 /* Check if the value for dimension dim is completely determined
10147 * by the values of the other parameters and variables.
10148 * That is, check if dimension dim is involved in an equality
10149 * for each of the subsets.
10151 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10153 int i;
10155 if (!set)
10156 return -1;
10157 for (i = 0; i < set->n; ++i) {
10158 int unique;
10159 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10160 if (unique != 1)
10161 return unique;
10163 return 1;
10166 /* Return the number of basic maps in the (current) representation of "map".
10168 int isl_map_n_basic_map(__isl_keep isl_map *map)
10170 return map ? map->n : 0;
10173 int isl_set_n_basic_set(__isl_keep isl_set *set)
10175 return set ? set->n : 0;
10178 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10179 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10181 int i;
10183 if (!map)
10184 return isl_stat_error;
10186 for (i = 0; i < map->n; ++i)
10187 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10188 return isl_stat_error;
10190 return isl_stat_ok;
10193 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10194 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10196 int i;
10198 if (!set)
10199 return isl_stat_error;
10201 for (i = 0; i < set->n; ++i)
10202 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10203 return isl_stat_error;
10205 return isl_stat_ok;
10208 /* Return a list of basic sets, the union of which is equal to "set".
10210 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10211 __isl_keep isl_set *set)
10213 int i;
10214 isl_basic_set_list *list;
10216 if (!set)
10217 return NULL;
10219 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10220 for (i = 0; i < set->n; ++i) {
10221 isl_basic_set *bset;
10223 bset = isl_basic_set_copy(set->p[i]);
10224 list = isl_basic_set_list_add(list, bset);
10227 return list;
10230 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10232 isl_space *dim;
10234 if (!bset)
10235 return NULL;
10237 bset = isl_basic_set_cow(bset);
10238 if (!bset)
10239 return NULL;
10241 dim = isl_basic_set_get_space(bset);
10242 dim = isl_space_lift(dim, bset->n_div);
10243 if (!dim)
10244 goto error;
10245 isl_space_free(bset->dim);
10246 bset->dim = dim;
10247 bset->extra -= bset->n_div;
10248 bset->n_div = 0;
10250 bset = isl_basic_set_finalize(bset);
10252 return bset;
10253 error:
10254 isl_basic_set_free(bset);
10255 return NULL;
10258 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10260 int i;
10261 isl_space *dim;
10262 unsigned n_div;
10264 set = isl_set_align_divs(set);
10266 if (!set)
10267 return NULL;
10269 set = isl_set_cow(set);
10270 if (!set)
10271 return NULL;
10273 n_div = set->p[0]->n_div;
10274 dim = isl_set_get_space(set);
10275 dim = isl_space_lift(dim, n_div);
10276 if (!dim)
10277 goto error;
10278 isl_space_free(set->dim);
10279 set->dim = dim;
10281 for (i = 0; i < set->n; ++i) {
10282 set->p[i] = isl_basic_set_lift(set->p[i]);
10283 if (!set->p[i])
10284 goto error;
10287 return set;
10288 error:
10289 isl_set_free(set);
10290 return NULL;
10293 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10295 isl_space *dim;
10296 struct isl_basic_map *bmap;
10297 unsigned n_set;
10298 unsigned n_div;
10299 unsigned n_param;
10300 unsigned total;
10301 int i, k, l;
10303 set = isl_set_align_divs(set);
10305 if (!set)
10306 return NULL;
10308 dim = isl_set_get_space(set);
10309 if (set->n == 0 || set->p[0]->n_div == 0) {
10310 isl_set_free(set);
10311 return isl_map_identity(isl_space_map_from_set(dim));
10314 n_div = set->p[0]->n_div;
10315 dim = isl_space_map_from_set(dim);
10316 n_param = isl_space_dim(dim, isl_dim_param);
10317 n_set = isl_space_dim(dim, isl_dim_in);
10318 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10319 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10320 for (i = 0; i < n_set; ++i)
10321 bmap = var_equal(bmap, i);
10323 total = n_param + n_set + n_set + n_div;
10324 for (i = 0; i < n_div; ++i) {
10325 k = isl_basic_map_alloc_inequality(bmap);
10326 if (k < 0)
10327 goto error;
10328 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10329 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10330 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10331 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10332 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10333 set->p[0]->div[i][0]);
10335 l = isl_basic_map_alloc_inequality(bmap);
10336 if (l < 0)
10337 goto error;
10338 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10339 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10340 set->p[0]->div[i][0]);
10341 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10344 isl_set_free(set);
10345 bmap = isl_basic_map_simplify(bmap);
10346 bmap = isl_basic_map_finalize(bmap);
10347 return isl_map_from_basic_map(bmap);
10348 error:
10349 isl_set_free(set);
10350 isl_basic_map_free(bmap);
10351 return NULL;
10354 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10356 unsigned dim;
10357 int size = 0;
10359 if (!bset)
10360 return -1;
10362 dim = isl_basic_set_total_dim(bset);
10363 size += bset->n_eq * (1 + dim);
10364 size += bset->n_ineq * (1 + dim);
10365 size += bset->n_div * (2 + dim);
10367 return size;
10370 int isl_set_size(__isl_keep isl_set *set)
10372 int i;
10373 int size = 0;
10375 if (!set)
10376 return -1;
10378 for (i = 0; i < set->n; ++i)
10379 size += isl_basic_set_size(set->p[i]);
10381 return size;
10384 /* Check if there is any lower bound (if lower == 0) and/or upper
10385 * bound (if upper == 0) on the specified dim.
10387 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10388 enum isl_dim_type type, unsigned pos, int lower, int upper)
10390 int i;
10392 if (!bmap)
10393 return isl_bool_error;
10395 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10396 return isl_bool_error);
10398 pos += isl_basic_map_offset(bmap, type);
10400 for (i = 0; i < bmap->n_div; ++i) {
10401 if (isl_int_is_zero(bmap->div[i][0]))
10402 continue;
10403 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10404 return isl_bool_true;
10407 for (i = 0; i < bmap->n_eq; ++i)
10408 if (!isl_int_is_zero(bmap->eq[i][pos]))
10409 return isl_bool_true;
10411 for (i = 0; i < bmap->n_ineq; ++i) {
10412 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10413 if (sgn > 0)
10414 lower = 1;
10415 if (sgn < 0)
10416 upper = 1;
10419 return lower && upper;
10422 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10423 enum isl_dim_type type, unsigned pos)
10425 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10428 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10429 enum isl_dim_type type, unsigned pos)
10431 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10434 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10435 enum isl_dim_type type, unsigned pos)
10437 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10440 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10441 enum isl_dim_type type, unsigned pos)
10443 int i;
10445 if (!map)
10446 return -1;
10448 for (i = 0; i < map->n; ++i) {
10449 int bounded;
10450 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10451 if (bounded < 0 || !bounded)
10452 return bounded;
10455 return 1;
10458 /* Return 1 if the specified dim is involved in both an upper bound
10459 * and a lower bound.
10461 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10462 enum isl_dim_type type, unsigned pos)
10464 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10467 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10469 static isl_bool has_any_bound(__isl_keep isl_map *map,
10470 enum isl_dim_type type, unsigned pos,
10471 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10472 enum isl_dim_type type, unsigned pos))
10474 int i;
10476 if (!map)
10477 return isl_bool_error;
10479 for (i = 0; i < map->n; ++i) {
10480 isl_bool bounded;
10481 bounded = fn(map->p[i], type, pos);
10482 if (bounded < 0 || bounded)
10483 return bounded;
10486 return isl_bool_false;
10489 /* Return 1 if the specified dim is involved in any lower bound.
10491 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10492 enum isl_dim_type type, unsigned pos)
10494 return has_any_bound(set, type, pos,
10495 &isl_basic_map_dim_has_lower_bound);
10498 /* Return 1 if the specified dim is involved in any upper bound.
10500 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10501 enum isl_dim_type type, unsigned pos)
10503 return has_any_bound(set, type, pos,
10504 &isl_basic_map_dim_has_upper_bound);
10507 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10509 static isl_bool has_bound(__isl_keep isl_map *map,
10510 enum isl_dim_type type, unsigned pos,
10511 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10512 enum isl_dim_type type, unsigned pos))
10514 int i;
10516 if (!map)
10517 return isl_bool_error;
10519 for (i = 0; i < map->n; ++i) {
10520 isl_bool bounded;
10521 bounded = fn(map->p[i], type, pos);
10522 if (bounded < 0 || !bounded)
10523 return bounded;
10526 return isl_bool_true;
10529 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10531 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10532 enum isl_dim_type type, unsigned pos)
10534 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10537 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10539 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10540 enum isl_dim_type type, unsigned pos)
10542 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10545 /* For each of the "n" variables starting at "first", determine
10546 * the sign of the variable and put the results in the first "n"
10547 * elements of the array "signs".
10548 * Sign
10549 * 1 means that the variable is non-negative
10550 * -1 means that the variable is non-positive
10551 * 0 means the variable attains both positive and negative values.
10553 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10554 unsigned first, unsigned n, int *signs)
10556 isl_vec *bound = NULL;
10557 struct isl_tab *tab = NULL;
10558 struct isl_tab_undo *snap;
10559 int i;
10561 if (!bset || !signs)
10562 return -1;
10564 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10565 tab = isl_tab_from_basic_set(bset, 0);
10566 if (!bound || !tab)
10567 goto error;
10569 isl_seq_clr(bound->el, bound->size);
10570 isl_int_set_si(bound->el[0], -1);
10572 snap = isl_tab_snap(tab);
10573 for (i = 0; i < n; ++i) {
10574 int empty;
10576 isl_int_set_si(bound->el[1 + first + i], -1);
10577 if (isl_tab_add_ineq(tab, bound->el) < 0)
10578 goto error;
10579 empty = tab->empty;
10580 isl_int_set_si(bound->el[1 + first + i], 0);
10581 if (isl_tab_rollback(tab, snap) < 0)
10582 goto error;
10584 if (empty) {
10585 signs[i] = 1;
10586 continue;
10589 isl_int_set_si(bound->el[1 + first + i], 1);
10590 if (isl_tab_add_ineq(tab, bound->el) < 0)
10591 goto error;
10592 empty = tab->empty;
10593 isl_int_set_si(bound->el[1 + first + i], 0);
10594 if (isl_tab_rollback(tab, snap) < 0)
10595 goto error;
10597 signs[i] = empty ? -1 : 0;
10600 isl_tab_free(tab);
10601 isl_vec_free(bound);
10602 return 0;
10603 error:
10604 isl_tab_free(tab);
10605 isl_vec_free(bound);
10606 return -1;
10609 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10610 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10612 if (!bset || !signs)
10613 return -1;
10614 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10615 return -1);
10617 first += pos(bset->dim, type) - 1;
10618 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10621 /* Is it possible for the integer division "div" to depend (possibly
10622 * indirectly) on any output dimensions?
10624 * If the div is undefined, then we conservatively assume that it
10625 * may depend on them.
10626 * Otherwise, we check if it actually depends on them or on any integer
10627 * divisions that may depend on them.
10629 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10631 int i;
10632 unsigned n_out, o_out;
10633 unsigned n_div, o_div;
10635 if (isl_int_is_zero(bmap->div[div][0]))
10636 return 1;
10638 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10639 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10641 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10642 return 1;
10644 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10645 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10647 for (i = 0; i < n_div; ++i) {
10648 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10649 continue;
10650 if (div_may_involve_output(bmap, i))
10651 return 1;
10654 return 0;
10657 /* Return the first integer division of "bmap" in the range
10658 * [first, first + n[ that may depend on any output dimensions and
10659 * that has a non-zero coefficient in "c" (where the first coefficient
10660 * in "c" corresponds to integer division "first").
10662 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10663 isl_int *c, int first, int n)
10665 int k;
10667 if (!bmap)
10668 return -1;
10670 for (k = first; k < first + n; ++k) {
10671 if (isl_int_is_zero(c[k]))
10672 continue;
10673 if (div_may_involve_output(bmap, k))
10674 return k;
10677 return first + n;
10680 /* Look for a pair of inequality constraints in "bmap" of the form
10682 * -l + i >= 0 or i >= l
10683 * and
10684 * n + l - i >= 0 or i <= l + n
10686 * with n < "m" and i the output dimension at position "pos".
10687 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10688 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10689 * and earlier output dimensions, as well as integer divisions that do
10690 * not involve any of the output dimensions.
10692 * Return the index of the first inequality constraint or bmap->n_ineq
10693 * if no such pair can be found.
10695 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10696 int pos, isl_int m)
10698 int i, j;
10699 isl_ctx *ctx;
10700 unsigned total;
10701 unsigned n_div, o_div;
10702 unsigned n_out, o_out;
10703 int less;
10705 if (!bmap)
10706 return -1;
10708 ctx = isl_basic_map_get_ctx(bmap);
10709 total = isl_basic_map_total_dim(bmap);
10710 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10711 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10712 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10713 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10714 for (i = 0; i < bmap->n_ineq; ++i) {
10715 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10716 continue;
10717 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10718 n_out - (pos + 1)) != -1)
10719 continue;
10720 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10721 0, n_div) < n_div)
10722 continue;
10723 for (j = i + 1; j < bmap->n_ineq; ++j) {
10724 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10725 ctx->one))
10726 continue;
10727 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10728 bmap->ineq[j] + 1, total))
10729 continue;
10730 break;
10732 if (j >= bmap->n_ineq)
10733 continue;
10734 isl_int_add(bmap->ineq[i][0],
10735 bmap->ineq[i][0], bmap->ineq[j][0]);
10736 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10737 isl_int_sub(bmap->ineq[i][0],
10738 bmap->ineq[i][0], bmap->ineq[j][0]);
10739 if (!less)
10740 continue;
10741 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10742 return i;
10743 else
10744 return j;
10747 return bmap->n_ineq;
10750 /* Return the index of the equality of "bmap" that defines
10751 * the output dimension "pos" in terms of earlier dimensions.
10752 * The equality may also involve integer divisions, as long
10753 * as those integer divisions are defined in terms of
10754 * parameters or input dimensions.
10755 * In this case, *div is set to the number of integer divisions and
10756 * *ineq is set to the number of inequality constraints (provided
10757 * div and ineq are not NULL).
10759 * The equality may also involve a single integer division involving
10760 * the output dimensions (typically only output dimension "pos") as
10761 * long as the coefficient of output dimension "pos" is 1 or -1 and
10762 * there is a pair of constraints i >= l and i <= l + n, with i referring
10763 * to output dimension "pos", l an expression involving only earlier
10764 * dimensions and n smaller than the coefficient of the integer division
10765 * in the equality. In this case, the output dimension can be defined
10766 * in terms of a modulo expression that does not involve the integer division.
10767 * *div is then set to this single integer division and
10768 * *ineq is set to the index of constraint i >= l.
10770 * Return bmap->n_eq if there is no such equality.
10771 * Return -1 on error.
10773 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10774 int pos, int *div, int *ineq)
10776 int j, k, l;
10777 unsigned n_out, o_out;
10778 unsigned n_div, o_div;
10780 if (!bmap)
10781 return -1;
10783 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10784 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10785 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10786 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10788 if (ineq)
10789 *ineq = bmap->n_ineq;
10790 if (div)
10791 *div = n_div;
10792 for (j = 0; j < bmap->n_eq; ++j) {
10793 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10794 continue;
10795 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10796 n_out - (pos + 1)) != -1)
10797 continue;
10798 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10799 0, n_div);
10800 if (k >= n_div)
10801 return j;
10802 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10803 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10804 continue;
10805 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10806 k + 1, n_div - (k+1)) < n_div)
10807 continue;
10808 l = find_modulo_constraint_pair(bmap, pos,
10809 bmap->eq[j][o_div + k]);
10810 if (l < 0)
10811 return -1;
10812 if (l >= bmap->n_ineq)
10813 continue;
10814 if (div)
10815 *div = k;
10816 if (ineq)
10817 *ineq = l;
10818 return j;
10821 return bmap->n_eq;
10824 /* Check if the given basic map is obviously single-valued.
10825 * In particular, for each output dimension, check that there is
10826 * an equality that defines the output dimension in terms of
10827 * earlier dimensions.
10829 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10831 int i;
10832 unsigned n_out;
10834 if (!bmap)
10835 return isl_bool_error;
10837 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10839 for (i = 0; i < n_out; ++i) {
10840 int eq;
10842 eq = isl_basic_map_output_defining_equality(bmap, i,
10843 NULL, NULL);
10844 if (eq < 0)
10845 return isl_bool_error;
10846 if (eq >= bmap->n_eq)
10847 return isl_bool_false;
10850 return isl_bool_true;
10853 /* Check if the given basic map is single-valued.
10854 * We simply compute
10856 * M \circ M^-1
10858 * and check if the result is a subset of the identity mapping.
10860 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10862 isl_space *space;
10863 isl_basic_map *test;
10864 isl_basic_map *id;
10865 isl_bool sv;
10867 sv = isl_basic_map_plain_is_single_valued(bmap);
10868 if (sv < 0 || sv)
10869 return sv;
10871 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10872 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10874 space = isl_basic_map_get_space(bmap);
10875 space = isl_space_map_from_set(isl_space_range(space));
10876 id = isl_basic_map_identity(space);
10878 sv = isl_basic_map_is_subset(test, id);
10880 isl_basic_map_free(test);
10881 isl_basic_map_free(id);
10883 return sv;
10886 /* Check if the given map is obviously single-valued.
10888 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10890 if (!map)
10891 return isl_bool_error;
10892 if (map->n == 0)
10893 return isl_bool_true;
10894 if (map->n >= 2)
10895 return isl_bool_false;
10897 return isl_basic_map_plain_is_single_valued(map->p[0]);
10900 /* Check if the given map is single-valued.
10901 * We simply compute
10903 * M \circ M^-1
10905 * and check if the result is a subset of the identity mapping.
10907 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10909 isl_space *dim;
10910 isl_map *test;
10911 isl_map *id;
10912 isl_bool sv;
10914 sv = isl_map_plain_is_single_valued(map);
10915 if (sv < 0 || sv)
10916 return sv;
10918 test = isl_map_reverse(isl_map_copy(map));
10919 test = isl_map_apply_range(test, isl_map_copy(map));
10921 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10922 id = isl_map_identity(dim);
10924 sv = isl_map_is_subset(test, id);
10926 isl_map_free(test);
10927 isl_map_free(id);
10929 return sv;
10932 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10934 isl_bool in;
10936 map = isl_map_copy(map);
10937 map = isl_map_reverse(map);
10938 in = isl_map_is_single_valued(map);
10939 isl_map_free(map);
10941 return in;
10944 /* Check if the given map is obviously injective.
10946 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10948 isl_bool in;
10950 map = isl_map_copy(map);
10951 map = isl_map_reverse(map);
10952 in = isl_map_plain_is_single_valued(map);
10953 isl_map_free(map);
10955 return in;
10958 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10960 isl_bool sv;
10962 sv = isl_map_is_single_valued(map);
10963 if (sv < 0 || !sv)
10964 return sv;
10966 return isl_map_is_injective(map);
10969 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10971 return isl_map_is_single_valued(set_to_map(set));
10974 /* Does "map" only map elements to themselves?
10976 * If the domain and range spaces are different, then "map"
10977 * is considered not to be an identity relation, even if it is empty.
10978 * Otherwise, construct the maximal identity relation and
10979 * check whether "map" is a subset of this relation.
10981 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10983 isl_space *space;
10984 isl_map *id;
10985 isl_bool equal, is_identity;
10987 space = isl_map_get_space(map);
10988 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10989 isl_space_free(space);
10990 if (equal < 0 || !equal)
10991 return equal;
10993 id = isl_map_identity(isl_map_get_space(map));
10994 is_identity = isl_map_is_subset(map, id);
10995 isl_map_free(id);
10997 return is_identity;
11000 int isl_map_is_translation(__isl_keep isl_map *map)
11002 int ok;
11003 isl_set *delta;
11005 delta = isl_map_deltas(isl_map_copy(map));
11006 ok = isl_set_is_singleton(delta);
11007 isl_set_free(delta);
11009 return ok;
11012 static int unique(isl_int *p, unsigned pos, unsigned len)
11014 if (isl_seq_first_non_zero(p, pos) != -1)
11015 return 0;
11016 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11017 return 0;
11018 return 1;
11021 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11023 int i, j;
11024 unsigned nvar;
11025 unsigned ovar;
11027 if (!bset)
11028 return -1;
11030 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11031 return 0;
11033 nvar = isl_basic_set_dim(bset, isl_dim_set);
11034 ovar = isl_space_offset(bset->dim, isl_dim_set);
11035 for (j = 0; j < nvar; ++j) {
11036 int lower = 0, upper = 0;
11037 for (i = 0; i < bset->n_eq; ++i) {
11038 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11039 continue;
11040 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11041 return 0;
11042 break;
11044 if (i < bset->n_eq)
11045 continue;
11046 for (i = 0; i < bset->n_ineq; ++i) {
11047 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11048 continue;
11049 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11050 return 0;
11051 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11052 lower = 1;
11053 else
11054 upper = 1;
11056 if (!lower || !upper)
11057 return 0;
11060 return 1;
11063 int isl_set_is_box(__isl_keep isl_set *set)
11065 if (!set)
11066 return -1;
11067 if (set->n != 1)
11068 return 0;
11070 return isl_basic_set_is_box(set->p[0]);
11073 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11075 if (!bset)
11076 return isl_bool_error;
11078 return isl_space_is_wrapping(bset->dim);
11081 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11083 if (!set)
11084 return isl_bool_error;
11086 return isl_space_is_wrapping(set->dim);
11089 /* Modify the space of "map" through a call to "change".
11090 * If "can_change" is set (not NULL), then first call it to check
11091 * if the modification is allowed, printing the error message "cannot_change"
11092 * if it is not.
11094 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11095 isl_bool (*can_change)(__isl_keep isl_map *map),
11096 const char *cannot_change,
11097 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11099 isl_bool ok;
11100 isl_space *space;
11102 if (!map)
11103 return NULL;
11105 ok = can_change ? can_change(map) : isl_bool_true;
11106 if (ok < 0)
11107 return isl_map_free(map);
11108 if (!ok)
11109 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11110 return isl_map_free(map));
11112 space = change(isl_map_get_space(map));
11113 map = isl_map_reset_space(map, space);
11115 return map;
11118 /* Is the domain of "map" a wrapped relation?
11120 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11122 if (!map)
11123 return isl_bool_error;
11125 return isl_space_domain_is_wrapping(map->dim);
11128 /* Is the range of "map" a wrapped relation?
11130 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11132 if (!map)
11133 return isl_bool_error;
11135 return isl_space_range_is_wrapping(map->dim);
11138 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11140 bmap = isl_basic_map_cow(bmap);
11141 if (!bmap)
11142 return NULL;
11144 bmap->dim = isl_space_wrap(bmap->dim);
11145 if (!bmap->dim)
11146 goto error;
11148 bmap = isl_basic_map_finalize(bmap);
11150 return bset_from_bmap(bmap);
11151 error:
11152 isl_basic_map_free(bmap);
11153 return NULL;
11156 /* Given a map A -> B, return the set (A -> B).
11158 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11160 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11163 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11165 bset = isl_basic_set_cow(bset);
11166 if (!bset)
11167 return NULL;
11169 bset->dim = isl_space_unwrap(bset->dim);
11170 if (!bset->dim)
11171 goto error;
11173 bset = isl_basic_set_finalize(bset);
11175 return bset_to_bmap(bset);
11176 error:
11177 isl_basic_set_free(bset);
11178 return NULL;
11181 /* Given a set (A -> B), return the map A -> B.
11182 * Error out if "set" is not of the form (A -> B).
11184 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11186 return isl_map_change_space(set, &isl_set_is_wrapping,
11187 "not a wrapping set", &isl_space_unwrap);
11190 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11191 enum isl_dim_type type)
11193 if (!bmap)
11194 return NULL;
11196 if (!isl_space_is_named_or_nested(bmap->dim, type))
11197 return bmap;
11199 bmap = isl_basic_map_cow(bmap);
11200 if (!bmap)
11201 return NULL;
11203 bmap->dim = isl_space_reset(bmap->dim, type);
11204 if (!bmap->dim)
11205 goto error;
11207 bmap = isl_basic_map_finalize(bmap);
11209 return bmap;
11210 error:
11211 isl_basic_map_free(bmap);
11212 return NULL;
11215 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11216 enum isl_dim_type type)
11218 int i;
11220 if (!map)
11221 return NULL;
11223 if (!isl_space_is_named_or_nested(map->dim, type))
11224 return map;
11226 map = isl_map_cow(map);
11227 if (!map)
11228 return NULL;
11230 for (i = 0; i < map->n; ++i) {
11231 map->p[i] = isl_basic_map_reset(map->p[i], type);
11232 if (!map->p[i])
11233 goto error;
11235 map->dim = isl_space_reset(map->dim, type);
11236 if (!map->dim)
11237 goto error;
11239 return map;
11240 error:
11241 isl_map_free(map);
11242 return NULL;
11245 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11247 if (!bmap)
11248 return NULL;
11250 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11251 return bmap;
11253 bmap = isl_basic_map_cow(bmap);
11254 if (!bmap)
11255 return NULL;
11257 bmap->dim = isl_space_flatten(bmap->dim);
11258 if (!bmap->dim)
11259 goto error;
11261 bmap = isl_basic_map_finalize(bmap);
11263 return bmap;
11264 error:
11265 isl_basic_map_free(bmap);
11266 return NULL;
11269 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11271 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11274 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11275 __isl_take isl_basic_map *bmap)
11277 if (!bmap)
11278 return NULL;
11280 if (!bmap->dim->nested[0])
11281 return bmap;
11283 bmap = isl_basic_map_cow(bmap);
11284 if (!bmap)
11285 return NULL;
11287 bmap->dim = isl_space_flatten_domain(bmap->dim);
11288 if (!bmap->dim)
11289 goto error;
11291 bmap = isl_basic_map_finalize(bmap);
11293 return bmap;
11294 error:
11295 isl_basic_map_free(bmap);
11296 return NULL;
11299 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11300 __isl_take isl_basic_map *bmap)
11302 if (!bmap)
11303 return NULL;
11305 if (!bmap->dim->nested[1])
11306 return bmap;
11308 bmap = isl_basic_map_cow(bmap);
11309 if (!bmap)
11310 return NULL;
11312 bmap->dim = isl_space_flatten_range(bmap->dim);
11313 if (!bmap->dim)
11314 goto error;
11316 bmap = isl_basic_map_finalize(bmap);
11318 return bmap;
11319 error:
11320 isl_basic_map_free(bmap);
11321 return NULL;
11324 /* Remove any internal structure from the spaces of domain and range of "map".
11326 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11328 if (!map)
11329 return NULL;
11331 if (!map->dim->nested[0] && !map->dim->nested[1])
11332 return map;
11334 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11337 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11339 return set_from_map(isl_map_flatten(set_to_map(set)));
11342 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11344 isl_space *dim, *flat_dim;
11345 isl_map *map;
11347 dim = isl_set_get_space(set);
11348 flat_dim = isl_space_flatten(isl_space_copy(dim));
11349 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11350 map = isl_map_intersect_domain(map, set);
11352 return map;
11355 /* Remove any internal structure from the space of the domain of "map".
11357 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11359 if (!map)
11360 return NULL;
11362 if (!map->dim->nested[0])
11363 return map;
11365 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11368 /* Remove any internal structure from the space of the range of "map".
11370 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11372 if (!map)
11373 return NULL;
11375 if (!map->dim->nested[1])
11376 return map;
11378 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11381 /* Reorder the dimensions of "bmap" according to the given dim_map
11382 * and set the dimension specification to "dim" and
11383 * perform Gaussian elimination on the result.
11385 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11386 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11388 isl_basic_map *res;
11389 unsigned flags;
11391 bmap = isl_basic_map_cow(bmap);
11392 if (!bmap || !dim || !dim_map)
11393 goto error;
11395 flags = bmap->flags;
11396 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11397 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11398 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11399 res = isl_basic_map_alloc_space(dim,
11400 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11401 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11402 if (res)
11403 res->flags = flags;
11404 res = isl_basic_map_gauss(res, NULL);
11405 res = isl_basic_map_finalize(res);
11406 return res;
11407 error:
11408 free(dim_map);
11409 isl_basic_map_free(bmap);
11410 isl_space_free(dim);
11411 return NULL;
11414 /* Reorder the dimensions of "map" according to given reordering.
11416 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11417 __isl_take isl_reordering *r)
11419 int i;
11420 struct isl_dim_map *dim_map;
11422 map = isl_map_cow(map);
11423 dim_map = isl_dim_map_from_reordering(r);
11424 if (!map || !r || !dim_map)
11425 goto error;
11427 for (i = 0; i < map->n; ++i) {
11428 struct isl_dim_map *dim_map_i;
11430 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11432 map->p[i] = isl_basic_map_realign(map->p[i],
11433 isl_space_copy(r->dim), dim_map_i);
11435 if (!map->p[i])
11436 goto error;
11439 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11441 isl_reordering_free(r);
11442 free(dim_map);
11443 return map;
11444 error:
11445 free(dim_map);
11446 isl_map_free(map);
11447 isl_reordering_free(r);
11448 return NULL;
11451 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11452 __isl_take isl_reordering *r)
11454 return set_from_map(isl_map_realign(set_to_map(set), r));
11457 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11458 __isl_take isl_space *model)
11460 isl_ctx *ctx;
11462 if (!map || !model)
11463 goto error;
11465 ctx = isl_space_get_ctx(model);
11466 if (!isl_space_has_named_params(model))
11467 isl_die(ctx, isl_error_invalid,
11468 "model has unnamed parameters", goto error);
11469 if (!isl_space_has_named_params(map->dim))
11470 isl_die(ctx, isl_error_invalid,
11471 "relation has unnamed parameters", goto error);
11472 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11473 isl_reordering *exp;
11475 model = isl_space_drop_dims(model, isl_dim_in,
11476 0, isl_space_dim(model, isl_dim_in));
11477 model = isl_space_drop_dims(model, isl_dim_out,
11478 0, isl_space_dim(model, isl_dim_out));
11479 exp = isl_parameter_alignment_reordering(map->dim, model);
11480 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11481 map = isl_map_realign(map, exp);
11484 isl_space_free(model);
11485 return map;
11486 error:
11487 isl_space_free(model);
11488 isl_map_free(map);
11489 return NULL;
11492 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11493 __isl_take isl_space *model)
11495 return isl_map_align_params(set, model);
11498 /* Align the parameters of "bmap" to those of "model", introducing
11499 * additional parameters if needed.
11501 __isl_give isl_basic_map *isl_basic_map_align_params(
11502 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11504 isl_ctx *ctx;
11506 if (!bmap || !model)
11507 goto error;
11509 ctx = isl_space_get_ctx(model);
11510 if (!isl_space_has_named_params(model))
11511 isl_die(ctx, isl_error_invalid,
11512 "model has unnamed parameters", goto error);
11513 if (!isl_space_has_named_params(bmap->dim))
11514 isl_die(ctx, isl_error_invalid,
11515 "relation has unnamed parameters", goto error);
11516 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11517 isl_reordering *exp;
11518 struct isl_dim_map *dim_map;
11520 model = isl_space_drop_dims(model, isl_dim_in,
11521 0, isl_space_dim(model, isl_dim_in));
11522 model = isl_space_drop_dims(model, isl_dim_out,
11523 0, isl_space_dim(model, isl_dim_out));
11524 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11525 exp = isl_reordering_extend_space(exp,
11526 isl_basic_map_get_space(bmap));
11527 dim_map = isl_dim_map_from_reordering(exp);
11528 bmap = isl_basic_map_realign(bmap,
11529 exp ? isl_space_copy(exp->dim) : NULL,
11530 isl_dim_map_extend(dim_map, bmap));
11531 isl_reordering_free(exp);
11532 free(dim_map);
11535 isl_space_free(model);
11536 return bmap;
11537 error:
11538 isl_space_free(model);
11539 isl_basic_map_free(bmap);
11540 return NULL;
11543 /* Align the parameters of "bset" to those of "model", introducing
11544 * additional parameters if needed.
11546 __isl_give isl_basic_set *isl_basic_set_align_params(
11547 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11549 return isl_basic_map_align_params(bset, model);
11552 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11553 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11554 enum isl_dim_type c2, enum isl_dim_type c3,
11555 enum isl_dim_type c4, enum isl_dim_type c5)
11557 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11558 struct isl_mat *mat;
11559 int i, j, k;
11560 int pos;
11562 if (!bmap)
11563 return NULL;
11564 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11565 isl_basic_map_total_dim(bmap) + 1);
11566 if (!mat)
11567 return NULL;
11568 for (i = 0; i < bmap->n_eq; ++i)
11569 for (j = 0, pos = 0; j < 5; ++j) {
11570 int off = isl_basic_map_offset(bmap, c[j]);
11571 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11572 isl_int_set(mat->row[i][pos],
11573 bmap->eq[i][off + k]);
11574 ++pos;
11578 return mat;
11581 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11582 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11583 enum isl_dim_type c2, enum isl_dim_type c3,
11584 enum isl_dim_type c4, enum isl_dim_type c5)
11586 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11587 struct isl_mat *mat;
11588 int i, j, k;
11589 int pos;
11591 if (!bmap)
11592 return NULL;
11593 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11594 isl_basic_map_total_dim(bmap) + 1);
11595 if (!mat)
11596 return NULL;
11597 for (i = 0; i < bmap->n_ineq; ++i)
11598 for (j = 0, pos = 0; j < 5; ++j) {
11599 int off = isl_basic_map_offset(bmap, c[j]);
11600 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11601 isl_int_set(mat->row[i][pos],
11602 bmap->ineq[i][off + k]);
11603 ++pos;
11607 return mat;
11610 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11611 __isl_take isl_space *dim,
11612 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11613 enum isl_dim_type c2, enum isl_dim_type c3,
11614 enum isl_dim_type c4, enum isl_dim_type c5)
11616 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11617 isl_basic_map *bmap;
11618 unsigned total;
11619 unsigned extra;
11620 int i, j, k, l;
11621 int pos;
11623 if (!dim || !eq || !ineq)
11624 goto error;
11626 if (eq->n_col != ineq->n_col)
11627 isl_die(dim->ctx, isl_error_invalid,
11628 "equalities and inequalities matrices should have "
11629 "same number of columns", goto error);
11631 total = 1 + isl_space_dim(dim, isl_dim_all);
11633 if (eq->n_col < total)
11634 isl_die(dim->ctx, isl_error_invalid,
11635 "number of columns too small", goto error);
11637 extra = eq->n_col - total;
11639 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11640 eq->n_row, ineq->n_row);
11641 if (!bmap)
11642 goto error;
11643 for (i = 0; i < extra; ++i) {
11644 k = isl_basic_map_alloc_div(bmap);
11645 if (k < 0)
11646 goto error;
11647 isl_int_set_si(bmap->div[k][0], 0);
11649 for (i = 0; i < eq->n_row; ++i) {
11650 l = isl_basic_map_alloc_equality(bmap);
11651 if (l < 0)
11652 goto error;
11653 for (j = 0, pos = 0; j < 5; ++j) {
11654 int off = isl_basic_map_offset(bmap, c[j]);
11655 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11656 isl_int_set(bmap->eq[l][off + k],
11657 eq->row[i][pos]);
11658 ++pos;
11662 for (i = 0; i < ineq->n_row; ++i) {
11663 l = isl_basic_map_alloc_inequality(bmap);
11664 if (l < 0)
11665 goto error;
11666 for (j = 0, pos = 0; j < 5; ++j) {
11667 int off = isl_basic_map_offset(bmap, c[j]);
11668 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11669 isl_int_set(bmap->ineq[l][off + k],
11670 ineq->row[i][pos]);
11671 ++pos;
11676 isl_space_free(dim);
11677 isl_mat_free(eq);
11678 isl_mat_free(ineq);
11680 bmap = isl_basic_map_simplify(bmap);
11681 return isl_basic_map_finalize(bmap);
11682 error:
11683 isl_space_free(dim);
11684 isl_mat_free(eq);
11685 isl_mat_free(ineq);
11686 return NULL;
11689 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11690 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11691 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11693 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11694 c1, c2, c3, c4, isl_dim_in);
11697 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11698 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11699 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11701 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11702 c1, c2, c3, c4, isl_dim_in);
11705 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11706 __isl_take isl_space *dim,
11707 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11708 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11710 isl_basic_map *bmap;
11711 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11712 c1, c2, c3, c4, isl_dim_in);
11713 return bset_from_bmap(bmap);
11716 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11718 if (!bmap)
11719 return isl_bool_error;
11721 return isl_space_can_zip(bmap->dim);
11724 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11726 if (!map)
11727 return isl_bool_error;
11729 return isl_space_can_zip(map->dim);
11732 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11733 * (A -> C) -> (B -> D).
11735 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11737 unsigned pos;
11738 unsigned n1;
11739 unsigned n2;
11741 if (!bmap)
11742 return NULL;
11744 if (!isl_basic_map_can_zip(bmap))
11745 isl_die(bmap->ctx, isl_error_invalid,
11746 "basic map cannot be zipped", goto error);
11747 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11748 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11749 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11750 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11751 bmap = isl_basic_map_cow(bmap);
11752 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11753 if (!bmap)
11754 return NULL;
11755 bmap->dim = isl_space_zip(bmap->dim);
11756 if (!bmap->dim)
11757 goto error;
11758 bmap = isl_basic_map_mark_final(bmap);
11759 return bmap;
11760 error:
11761 isl_basic_map_free(bmap);
11762 return NULL;
11765 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11766 * (A -> C) -> (B -> D).
11768 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11770 int i;
11772 if (!map)
11773 return NULL;
11775 if (!isl_map_can_zip(map))
11776 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11777 goto error);
11779 map = isl_map_cow(map);
11780 if (!map)
11781 return NULL;
11783 for (i = 0; i < map->n; ++i) {
11784 map->p[i] = isl_basic_map_zip(map->p[i]);
11785 if (!map->p[i])
11786 goto error;
11789 map->dim = isl_space_zip(map->dim);
11790 if (!map->dim)
11791 goto error;
11793 return map;
11794 error:
11795 isl_map_free(map);
11796 return NULL;
11799 /* Can we apply isl_basic_map_curry to "bmap"?
11800 * That is, does it have a nested relation in its domain?
11802 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11804 if (!bmap)
11805 return isl_bool_error;
11807 return isl_space_can_curry(bmap->dim);
11810 /* Can we apply isl_map_curry to "map"?
11811 * That is, does it have a nested relation in its domain?
11813 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11815 if (!map)
11816 return isl_bool_error;
11818 return isl_space_can_curry(map->dim);
11821 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11822 * A -> (B -> C).
11824 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11827 if (!bmap)
11828 return NULL;
11830 if (!isl_basic_map_can_curry(bmap))
11831 isl_die(bmap->ctx, isl_error_invalid,
11832 "basic map cannot be curried", goto error);
11833 bmap = isl_basic_map_cow(bmap);
11834 if (!bmap)
11835 return NULL;
11836 bmap->dim = isl_space_curry(bmap->dim);
11837 if (!bmap->dim)
11838 goto error;
11839 bmap = isl_basic_map_mark_final(bmap);
11840 return bmap;
11841 error:
11842 isl_basic_map_free(bmap);
11843 return NULL;
11846 /* Given a map (A -> B) -> C, return the corresponding map
11847 * A -> (B -> C).
11849 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11851 return isl_map_change_space(map, &isl_map_can_curry,
11852 "map cannot be curried", &isl_space_curry);
11855 /* Can isl_map_range_curry be applied to "map"?
11856 * That is, does it have a nested relation in its range,
11857 * the domain of which is itself a nested relation?
11859 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11861 if (!map)
11862 return isl_bool_error;
11864 return isl_space_can_range_curry(map->dim);
11867 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11868 * A -> (B -> (C -> D)).
11870 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11872 return isl_map_change_space(map, &isl_map_can_range_curry,
11873 "map range cannot be curried",
11874 &isl_space_range_curry);
11877 /* Can we apply isl_basic_map_uncurry to "bmap"?
11878 * That is, does it have a nested relation in its domain?
11880 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11882 if (!bmap)
11883 return isl_bool_error;
11885 return isl_space_can_uncurry(bmap->dim);
11888 /* Can we apply isl_map_uncurry to "map"?
11889 * That is, does it have a nested relation in its domain?
11891 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11893 if (!map)
11894 return isl_bool_error;
11896 return isl_space_can_uncurry(map->dim);
11899 /* Given a basic map A -> (B -> C), return the corresponding basic map
11900 * (A -> B) -> C.
11902 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11905 if (!bmap)
11906 return NULL;
11908 if (!isl_basic_map_can_uncurry(bmap))
11909 isl_die(bmap->ctx, isl_error_invalid,
11910 "basic map cannot be uncurried",
11911 return isl_basic_map_free(bmap));
11912 bmap = isl_basic_map_cow(bmap);
11913 if (!bmap)
11914 return NULL;
11915 bmap->dim = isl_space_uncurry(bmap->dim);
11916 if (!bmap->dim)
11917 return isl_basic_map_free(bmap);
11918 bmap = isl_basic_map_mark_final(bmap);
11919 return bmap;
11922 /* Given a map A -> (B -> C), return the corresponding map
11923 * (A -> B) -> C.
11925 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11927 return isl_map_change_space(map, &isl_map_can_uncurry,
11928 "map cannot be uncurried", &isl_space_uncurry);
11931 /* Construct a basic map mapping the domain of the affine expression
11932 * to a one-dimensional range prescribed by the affine expression.
11933 * If "rational" is set, then construct a rational basic map.
11935 * A NaN affine expression cannot be converted to a basic map.
11937 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11938 __isl_take isl_aff *aff, int rational)
11940 int k;
11941 int pos;
11942 isl_bool is_nan;
11943 isl_local_space *ls;
11944 isl_basic_map *bmap = NULL;
11946 if (!aff)
11947 return NULL;
11948 is_nan = isl_aff_is_nan(aff);
11949 if (is_nan < 0)
11950 goto error;
11951 if (is_nan)
11952 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11953 "cannot convert NaN", goto error);
11955 ls = isl_aff_get_local_space(aff);
11956 bmap = isl_basic_map_from_local_space(ls);
11957 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11958 k = isl_basic_map_alloc_equality(bmap);
11959 if (k < 0)
11960 goto error;
11962 pos = isl_basic_map_offset(bmap, isl_dim_out);
11963 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11964 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11965 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11966 aff->v->size - (pos + 1));
11968 isl_aff_free(aff);
11969 if (rational)
11970 bmap = isl_basic_map_set_rational(bmap);
11971 bmap = isl_basic_map_finalize(bmap);
11972 return bmap;
11973 error:
11974 isl_aff_free(aff);
11975 isl_basic_map_free(bmap);
11976 return NULL;
11979 /* Construct a basic map mapping the domain of the affine expression
11980 * to a one-dimensional range prescribed by the affine expression.
11982 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11984 return isl_basic_map_from_aff2(aff, 0);
11987 /* Construct a map mapping the domain of the affine expression
11988 * to a one-dimensional range prescribed by the affine expression.
11990 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11992 isl_basic_map *bmap;
11994 bmap = isl_basic_map_from_aff(aff);
11995 return isl_map_from_basic_map(bmap);
11998 /* Construct a basic map mapping the domain the multi-affine expression
11999 * to its range, with each dimension in the range equated to the
12000 * corresponding affine expression.
12001 * If "rational" is set, then construct a rational basic map.
12003 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12004 __isl_take isl_multi_aff *maff, int rational)
12006 int i;
12007 isl_space *space;
12008 isl_basic_map *bmap;
12010 if (!maff)
12011 return NULL;
12013 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12014 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12015 "invalid space", goto error);
12017 space = isl_space_domain(isl_multi_aff_get_space(maff));
12018 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12019 if (rational)
12020 bmap = isl_basic_map_set_rational(bmap);
12022 for (i = 0; i < maff->n; ++i) {
12023 isl_aff *aff;
12024 isl_basic_map *bmap_i;
12026 aff = isl_aff_copy(maff->p[i]);
12027 bmap_i = isl_basic_map_from_aff2(aff, rational);
12029 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12032 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12034 isl_multi_aff_free(maff);
12035 return bmap;
12036 error:
12037 isl_multi_aff_free(maff);
12038 return NULL;
12041 /* Construct a basic map mapping the domain the multi-affine expression
12042 * to its range, with each dimension in the range equated to the
12043 * corresponding affine expression.
12045 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12046 __isl_take isl_multi_aff *ma)
12048 return isl_basic_map_from_multi_aff2(ma, 0);
12051 /* Construct a map mapping the domain the multi-affine expression
12052 * to its range, with each dimension in the range equated to the
12053 * corresponding affine expression.
12055 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12057 isl_basic_map *bmap;
12059 bmap = isl_basic_map_from_multi_aff(maff);
12060 return isl_map_from_basic_map(bmap);
12063 /* Construct a basic map mapping a domain in the given space to
12064 * to an n-dimensional range, with n the number of elements in the list,
12065 * where each coordinate in the range is prescribed by the
12066 * corresponding affine expression.
12067 * The domains of all affine expressions in the list are assumed to match
12068 * domain_dim.
12070 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12071 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12073 int i;
12074 isl_space *dim;
12075 isl_basic_map *bmap;
12077 if (!list)
12078 return NULL;
12080 dim = isl_space_from_domain(domain_dim);
12081 bmap = isl_basic_map_universe(dim);
12083 for (i = 0; i < list->n; ++i) {
12084 isl_aff *aff;
12085 isl_basic_map *bmap_i;
12087 aff = isl_aff_copy(list->p[i]);
12088 bmap_i = isl_basic_map_from_aff(aff);
12090 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12093 isl_aff_list_free(list);
12094 return bmap;
12097 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12098 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12100 return isl_map_equate(set, type1, pos1, type2, pos2);
12103 /* Construct a basic map where the given dimensions are equal to each other.
12105 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12106 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12108 isl_basic_map *bmap = NULL;
12109 int i;
12111 if (!space)
12112 return NULL;
12114 if (pos1 >= isl_space_dim(space, type1))
12115 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12116 "index out of bounds", goto error);
12117 if (pos2 >= isl_space_dim(space, type2))
12118 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12119 "index out of bounds", goto error);
12121 if (type1 == type2 && pos1 == pos2)
12122 return isl_basic_map_universe(space);
12124 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12125 i = isl_basic_map_alloc_equality(bmap);
12126 if (i < 0)
12127 goto error;
12128 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12129 pos1 += isl_basic_map_offset(bmap, type1);
12130 pos2 += isl_basic_map_offset(bmap, type2);
12131 isl_int_set_si(bmap->eq[i][pos1], -1);
12132 isl_int_set_si(bmap->eq[i][pos2], 1);
12133 bmap = isl_basic_map_finalize(bmap);
12134 isl_space_free(space);
12135 return bmap;
12136 error:
12137 isl_space_free(space);
12138 isl_basic_map_free(bmap);
12139 return NULL;
12142 /* Add a constraint imposing that the given two dimensions are equal.
12144 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12145 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12147 isl_basic_map *eq;
12149 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12151 bmap = isl_basic_map_intersect(bmap, eq);
12153 return bmap;
12156 /* Add a constraint imposing that the given two dimensions are equal.
12158 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12159 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12161 isl_basic_map *bmap;
12163 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12165 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12167 return map;
12170 /* Add a constraint imposing that the given two dimensions have opposite values.
12172 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12173 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12175 isl_basic_map *bmap = NULL;
12176 int i;
12178 if (!map)
12179 return NULL;
12181 if (pos1 >= isl_map_dim(map, type1))
12182 isl_die(map->ctx, isl_error_invalid,
12183 "index out of bounds", goto error);
12184 if (pos2 >= isl_map_dim(map, type2))
12185 isl_die(map->ctx, isl_error_invalid,
12186 "index out of bounds", goto error);
12188 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12189 i = isl_basic_map_alloc_equality(bmap);
12190 if (i < 0)
12191 goto error;
12192 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12193 pos1 += isl_basic_map_offset(bmap, type1);
12194 pos2 += isl_basic_map_offset(bmap, type2);
12195 isl_int_set_si(bmap->eq[i][pos1], 1);
12196 isl_int_set_si(bmap->eq[i][pos2], 1);
12197 bmap = isl_basic_map_finalize(bmap);
12199 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12201 return map;
12202 error:
12203 isl_basic_map_free(bmap);
12204 isl_map_free(map);
12205 return NULL;
12208 /* Construct a constraint imposing that the value of the first dimension is
12209 * greater than or equal to that of the second.
12211 static __isl_give isl_constraint *constraint_order_ge(
12212 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12213 enum isl_dim_type type2, int pos2)
12215 isl_constraint *c;
12217 if (!space)
12218 return NULL;
12220 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12222 if (pos1 >= isl_constraint_dim(c, type1))
12223 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12224 "index out of bounds", return isl_constraint_free(c));
12225 if (pos2 >= isl_constraint_dim(c, type2))
12226 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12227 "index out of bounds", return isl_constraint_free(c));
12229 if (type1 == type2 && pos1 == pos2)
12230 return c;
12232 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12233 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12235 return c;
12238 /* Add a constraint imposing that the value of the first dimension is
12239 * greater than or equal to that of the second.
12241 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12242 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12244 isl_constraint *c;
12245 isl_space *space;
12247 if (type1 == type2 && pos1 == pos2)
12248 return bmap;
12249 space = isl_basic_map_get_space(bmap);
12250 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12251 bmap = isl_basic_map_add_constraint(bmap, c);
12253 return bmap;
12256 /* Add a constraint imposing that the value of the first dimension is
12257 * greater than or equal to that of the second.
12259 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12260 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12262 isl_constraint *c;
12263 isl_space *space;
12265 if (type1 == type2 && pos1 == pos2)
12266 return map;
12267 space = isl_map_get_space(map);
12268 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12269 map = isl_map_add_constraint(map, c);
12271 return map;
12274 /* Add a constraint imposing that the value of the first dimension is
12275 * less than or equal to that of the second.
12277 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12278 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12280 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12283 /* Construct a basic map where the value of the first dimension is
12284 * greater than that of the second.
12286 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12287 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12289 isl_basic_map *bmap = NULL;
12290 int i;
12292 if (!space)
12293 return NULL;
12295 if (pos1 >= isl_space_dim(space, type1))
12296 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12297 "index out of bounds", goto error);
12298 if (pos2 >= isl_space_dim(space, type2))
12299 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12300 "index out of bounds", goto error);
12302 if (type1 == type2 && pos1 == pos2)
12303 return isl_basic_map_empty(space);
12305 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12306 i = isl_basic_map_alloc_inequality(bmap);
12307 if (i < 0)
12308 return isl_basic_map_free(bmap);
12309 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12310 pos1 += isl_basic_map_offset(bmap, type1);
12311 pos2 += isl_basic_map_offset(bmap, type2);
12312 isl_int_set_si(bmap->ineq[i][pos1], 1);
12313 isl_int_set_si(bmap->ineq[i][pos2], -1);
12314 isl_int_set_si(bmap->ineq[i][0], -1);
12315 bmap = isl_basic_map_finalize(bmap);
12317 return bmap;
12318 error:
12319 isl_space_free(space);
12320 isl_basic_map_free(bmap);
12321 return NULL;
12324 /* Add a constraint imposing that the value of the first dimension is
12325 * greater than that of the second.
12327 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12328 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12330 isl_basic_map *gt;
12332 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12334 bmap = isl_basic_map_intersect(bmap, gt);
12336 return bmap;
12339 /* Add a constraint imposing that the value of the first dimension is
12340 * greater than that of the second.
12342 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12343 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12345 isl_basic_map *bmap;
12347 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12349 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12351 return map;
12354 /* Add a constraint imposing that the value of the first dimension is
12355 * smaller than that of the second.
12357 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12358 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12360 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12363 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12364 int pos)
12366 isl_aff *div;
12367 isl_local_space *ls;
12369 if (!bmap)
12370 return NULL;
12372 if (!isl_basic_map_divs_known(bmap))
12373 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12374 "some divs are unknown", return NULL);
12376 ls = isl_basic_map_get_local_space(bmap);
12377 div = isl_local_space_get_div(ls, pos);
12378 isl_local_space_free(ls);
12380 return div;
12383 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12384 int pos)
12386 return isl_basic_map_get_div(bset, pos);
12389 /* Plug in "subs" for dimension "type", "pos" of "bset".
12391 * Let i be the dimension to replace and let "subs" be of the form
12393 * f/d
12395 * Any integer division with a non-zero coefficient for i,
12397 * floor((a i + g)/m)
12399 * is replaced by
12401 * floor((a f + d g)/(m d))
12403 * Constraints of the form
12405 * a i + g
12407 * are replaced by
12409 * a f + d g
12411 * We currently require that "subs" is an integral expression.
12412 * Handling rational expressions may require us to add stride constraints
12413 * as we do in isl_basic_set_preimage_multi_aff.
12415 __isl_give isl_basic_set *isl_basic_set_substitute(
12416 __isl_take isl_basic_set *bset,
12417 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12419 int i;
12420 isl_int v;
12421 isl_ctx *ctx;
12423 if (bset && isl_basic_set_plain_is_empty(bset))
12424 return bset;
12426 bset = isl_basic_set_cow(bset);
12427 if (!bset || !subs)
12428 goto error;
12430 ctx = isl_basic_set_get_ctx(bset);
12431 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12432 isl_die(ctx, isl_error_invalid,
12433 "spaces don't match", goto error);
12434 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12435 isl_die(ctx, isl_error_unsupported,
12436 "cannot handle divs yet", goto error);
12437 if (!isl_int_is_one(subs->v->el[0]))
12438 isl_die(ctx, isl_error_invalid,
12439 "can only substitute integer expressions", goto error);
12441 pos += isl_basic_set_offset(bset, type);
12443 isl_int_init(v);
12445 for (i = 0; i < bset->n_eq; ++i) {
12446 if (isl_int_is_zero(bset->eq[i][pos]))
12447 continue;
12448 isl_int_set(v, bset->eq[i][pos]);
12449 isl_int_set_si(bset->eq[i][pos], 0);
12450 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12451 v, subs->v->el + 1, subs->v->size - 1);
12454 for (i = 0; i < bset->n_ineq; ++i) {
12455 if (isl_int_is_zero(bset->ineq[i][pos]))
12456 continue;
12457 isl_int_set(v, bset->ineq[i][pos]);
12458 isl_int_set_si(bset->ineq[i][pos], 0);
12459 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12460 v, subs->v->el + 1, subs->v->size - 1);
12463 for (i = 0; i < bset->n_div; ++i) {
12464 if (isl_int_is_zero(bset->div[i][1 + pos]))
12465 continue;
12466 isl_int_set(v, bset->div[i][1 + pos]);
12467 isl_int_set_si(bset->div[i][1 + pos], 0);
12468 isl_seq_combine(bset->div[i] + 1,
12469 subs->v->el[0], bset->div[i] + 1,
12470 v, subs->v->el + 1, subs->v->size - 1);
12471 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12474 isl_int_clear(v);
12476 bset = isl_basic_set_simplify(bset);
12477 return isl_basic_set_finalize(bset);
12478 error:
12479 isl_basic_set_free(bset);
12480 return NULL;
12483 /* Plug in "subs" for dimension "type", "pos" of "set".
12485 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12486 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12488 int i;
12490 if (set && isl_set_plain_is_empty(set))
12491 return set;
12493 set = isl_set_cow(set);
12494 if (!set || !subs)
12495 goto error;
12497 for (i = set->n - 1; i >= 0; --i) {
12498 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12499 if (remove_if_empty(set, i) < 0)
12500 goto error;
12503 return set;
12504 error:
12505 isl_set_free(set);
12506 return NULL;
12509 /* Check if the range of "ma" is compatible with the domain or range
12510 * (depending on "type") of "bmap".
12511 * Return -1 if anything is wrong.
12513 static int check_basic_map_compatible_range_multi_aff(
12514 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12515 __isl_keep isl_multi_aff *ma)
12517 int m;
12518 isl_space *ma_space;
12520 ma_space = isl_multi_aff_get_space(ma);
12522 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12523 if (m < 0)
12524 goto error;
12525 if (!m)
12526 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12527 "parameters don't match", goto error);
12528 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12529 if (m < 0)
12530 goto error;
12531 if (!m)
12532 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12533 "spaces don't match", goto error);
12535 isl_space_free(ma_space);
12536 return m;
12537 error:
12538 isl_space_free(ma_space);
12539 return -1;
12542 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12543 * coefficients before the transformed range of dimensions,
12544 * the "n_after" coefficients after the transformed range of dimensions
12545 * and the coefficients of the other divs in "bmap".
12547 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12548 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12550 int i;
12551 int n_param;
12552 int n_set;
12553 isl_local_space *ls;
12555 if (n_div == 0)
12556 return 0;
12558 ls = isl_aff_get_domain_local_space(ma->p[0]);
12559 if (!ls)
12560 return -1;
12562 n_param = isl_local_space_dim(ls, isl_dim_param);
12563 n_set = isl_local_space_dim(ls, isl_dim_set);
12564 for (i = 0; i < n_div; ++i) {
12565 int o_bmap = 0, o_ls = 0;
12567 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12568 o_bmap += 1 + 1 + n_param;
12569 o_ls += 1 + 1 + n_param;
12570 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12571 o_bmap += n_before;
12572 isl_seq_cpy(bmap->div[i] + o_bmap,
12573 ls->div->row[i] + o_ls, n_set);
12574 o_bmap += n_set;
12575 o_ls += n_set;
12576 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12577 o_bmap += n_after;
12578 isl_seq_cpy(bmap->div[i] + o_bmap,
12579 ls->div->row[i] + o_ls, n_div);
12580 o_bmap += n_div;
12581 o_ls += n_div;
12582 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12583 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12584 goto error;
12587 isl_local_space_free(ls);
12588 return 0;
12589 error:
12590 isl_local_space_free(ls);
12591 return -1;
12594 /* How many stride constraints does "ma" enforce?
12595 * That is, how many of the affine expressions have a denominator
12596 * different from one?
12598 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12600 int i;
12601 int strides = 0;
12603 for (i = 0; i < ma->n; ++i)
12604 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12605 strides++;
12607 return strides;
12610 /* For each affine expression in ma of the form
12612 * x_i = (f_i y + h_i)/m_i
12614 * with m_i different from one, add a constraint to "bmap"
12615 * of the form
12617 * f_i y + h_i = m_i alpha_i
12619 * with alpha_i an additional existentially quantified variable.
12621 * The input variables of "ma" correspond to a subset of the variables
12622 * of "bmap". There are "n_before" variables in "bmap" before this
12623 * subset and "n_after" variables after this subset.
12624 * The integer divisions of the affine expressions in "ma" are assumed
12625 * to have been aligned. There are "n_div_ma" of them and
12626 * they appear first in "bmap", straight after the "n_after" variables.
12628 static __isl_give isl_basic_map *add_ma_strides(
12629 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12630 int n_before, int n_after, int n_div_ma)
12632 int i, k;
12633 int div;
12634 int total;
12635 int n_param;
12636 int n_in;
12638 total = isl_basic_map_total_dim(bmap);
12639 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12640 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12641 for (i = 0; i < ma->n; ++i) {
12642 int o_bmap = 0, o_ma = 1;
12644 if (isl_int_is_one(ma->p[i]->v->el[0]))
12645 continue;
12646 div = isl_basic_map_alloc_div(bmap);
12647 k = isl_basic_map_alloc_equality(bmap);
12648 if (div < 0 || k < 0)
12649 goto error;
12650 isl_int_set_si(bmap->div[div][0], 0);
12651 isl_seq_cpy(bmap->eq[k] + o_bmap,
12652 ma->p[i]->v->el + o_ma, 1 + n_param);
12653 o_bmap += 1 + n_param;
12654 o_ma += 1 + n_param;
12655 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12656 o_bmap += n_before;
12657 isl_seq_cpy(bmap->eq[k] + o_bmap,
12658 ma->p[i]->v->el + o_ma, n_in);
12659 o_bmap += n_in;
12660 o_ma += n_in;
12661 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12662 o_bmap += n_after;
12663 isl_seq_cpy(bmap->eq[k] + o_bmap,
12664 ma->p[i]->v->el + o_ma, n_div_ma);
12665 o_bmap += n_div_ma;
12666 o_ma += n_div_ma;
12667 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12668 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12669 total++;
12672 return bmap;
12673 error:
12674 isl_basic_map_free(bmap);
12675 return NULL;
12678 /* Replace the domain or range space (depending on "type) of "space" by "set".
12680 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12681 enum isl_dim_type type, __isl_take isl_space *set)
12683 if (type == isl_dim_in) {
12684 space = isl_space_range(space);
12685 space = isl_space_map_from_domain_and_range(set, space);
12686 } else {
12687 space = isl_space_domain(space);
12688 space = isl_space_map_from_domain_and_range(space, set);
12691 return space;
12694 /* Compute the preimage of the domain or range (depending on "type")
12695 * of "bmap" under the function represented by "ma".
12696 * In other words, plug in "ma" in the domain or range of "bmap".
12697 * The result is a basic map that lives in the same space as "bmap"
12698 * except that the domain or range has been replaced by
12699 * the domain space of "ma".
12701 * If bmap is represented by
12703 * A(p) + S u + B x + T v + C(divs) >= 0,
12705 * where u and x are input and output dimensions if type == isl_dim_out
12706 * while x and v are input and output dimensions if type == isl_dim_in,
12707 * and ma is represented by
12709 * x = D(p) + F(y) + G(divs')
12711 * then the result is
12713 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12715 * The divs in the input set are similarly adjusted.
12716 * In particular
12718 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12720 * becomes
12722 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12723 * B_i G(divs') + c_i(divs))/n_i)
12725 * If bmap is not a rational map and if F(y) involves any denominators
12727 * x_i = (f_i y + h_i)/m_i
12729 * then additional constraints are added to ensure that we only
12730 * map back integer points. That is we enforce
12732 * f_i y + h_i = m_i alpha_i
12734 * with alpha_i an additional existentially quantified variable.
12736 * We first copy over the divs from "ma".
12737 * Then we add the modified constraints and divs from "bmap".
12738 * Finally, we add the stride constraints, if needed.
12740 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12741 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12742 __isl_take isl_multi_aff *ma)
12744 int i, k;
12745 isl_space *space;
12746 isl_basic_map *res = NULL;
12747 int n_before, n_after, n_div_bmap, n_div_ma;
12748 isl_int f, c1, c2, g;
12749 int rational, strides;
12751 isl_int_init(f);
12752 isl_int_init(c1);
12753 isl_int_init(c2);
12754 isl_int_init(g);
12756 ma = isl_multi_aff_align_divs(ma);
12757 if (!bmap || !ma)
12758 goto error;
12759 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12760 goto error;
12762 if (type == isl_dim_in) {
12763 n_before = 0;
12764 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12765 } else {
12766 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12767 n_after = 0;
12769 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12770 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12772 space = isl_multi_aff_get_domain_space(ma);
12773 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12774 rational = isl_basic_map_is_rational(bmap);
12775 strides = rational ? 0 : multi_aff_strides(ma);
12776 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12777 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12778 if (rational)
12779 res = isl_basic_map_set_rational(res);
12781 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12782 if (isl_basic_map_alloc_div(res) < 0)
12783 goto error;
12785 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12786 goto error;
12788 for (i = 0; i < bmap->n_eq; ++i) {
12789 k = isl_basic_map_alloc_equality(res);
12790 if (k < 0)
12791 goto error;
12792 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12793 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12796 for (i = 0; i < bmap->n_ineq; ++i) {
12797 k = isl_basic_map_alloc_inequality(res);
12798 if (k < 0)
12799 goto error;
12800 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12801 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12804 for (i = 0; i < bmap->n_div; ++i) {
12805 if (isl_int_is_zero(bmap->div[i][0])) {
12806 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12807 continue;
12809 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12810 n_before, n_after, n_div_ma, n_div_bmap,
12811 f, c1, c2, g, 1);
12814 if (strides)
12815 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12817 isl_int_clear(f);
12818 isl_int_clear(c1);
12819 isl_int_clear(c2);
12820 isl_int_clear(g);
12821 isl_basic_map_free(bmap);
12822 isl_multi_aff_free(ma);
12823 res = isl_basic_set_simplify(res);
12824 return isl_basic_map_finalize(res);
12825 error:
12826 isl_int_clear(f);
12827 isl_int_clear(c1);
12828 isl_int_clear(c2);
12829 isl_int_clear(g);
12830 isl_basic_map_free(bmap);
12831 isl_multi_aff_free(ma);
12832 isl_basic_map_free(res);
12833 return NULL;
12836 /* Compute the preimage of "bset" under the function represented by "ma".
12837 * In other words, plug in "ma" in "bset". The result is a basic set
12838 * that lives in the domain space of "ma".
12840 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12841 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12843 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12846 /* Compute the preimage of the domain of "bmap" under the function
12847 * represented by "ma".
12848 * In other words, plug in "ma" in the domain of "bmap".
12849 * The result is a basic map that lives in the same space as "bmap"
12850 * except that the domain has been replaced by the domain space of "ma".
12852 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12853 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12855 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12858 /* Compute the preimage of the range of "bmap" under the function
12859 * represented by "ma".
12860 * In other words, plug in "ma" in the range of "bmap".
12861 * The result is a basic map that lives in the same space as "bmap"
12862 * except that the range has been replaced by the domain space of "ma".
12864 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12865 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12867 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12870 /* Check if the range of "ma" is compatible with the domain or range
12871 * (depending on "type") of "map".
12872 * Return -1 if anything is wrong.
12874 static int check_map_compatible_range_multi_aff(
12875 __isl_keep isl_map *map, enum isl_dim_type type,
12876 __isl_keep isl_multi_aff *ma)
12878 int m;
12879 isl_space *ma_space;
12881 ma_space = isl_multi_aff_get_space(ma);
12882 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12883 isl_space_free(ma_space);
12884 if (m >= 0 && !m)
12885 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12886 "spaces don't match", return -1);
12887 return m;
12890 /* Compute the preimage of the domain or range (depending on "type")
12891 * of "map" under the function represented by "ma".
12892 * In other words, plug in "ma" in the domain or range of "map".
12893 * The result is a map that lives in the same space as "map"
12894 * except that the domain or range has been replaced by
12895 * the domain space of "ma".
12897 * The parameters are assumed to have been aligned.
12899 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12900 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12902 int i;
12903 isl_space *space;
12905 map = isl_map_cow(map);
12906 ma = isl_multi_aff_align_divs(ma);
12907 if (!map || !ma)
12908 goto error;
12909 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12910 goto error;
12912 for (i = 0; i < map->n; ++i) {
12913 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12914 isl_multi_aff_copy(ma));
12915 if (!map->p[i])
12916 goto error;
12919 space = isl_multi_aff_get_domain_space(ma);
12920 space = isl_space_set(isl_map_get_space(map), type, space);
12922 isl_space_free(map->dim);
12923 map->dim = space;
12924 if (!map->dim)
12925 goto error;
12927 isl_multi_aff_free(ma);
12928 if (map->n > 1)
12929 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12930 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12931 return map;
12932 error:
12933 isl_multi_aff_free(ma);
12934 isl_map_free(map);
12935 return NULL;
12938 /* Compute the preimage of the domain or range (depending on "type")
12939 * of "map" under the function represented by "ma".
12940 * In other words, plug in "ma" in the domain or range of "map".
12941 * The result is a map that lives in the same space as "map"
12942 * except that the domain or range has been replaced by
12943 * the domain space of "ma".
12945 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12946 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12948 if (!map || !ma)
12949 goto error;
12951 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12952 return map_preimage_multi_aff(map, type, ma);
12954 if (!isl_space_has_named_params(map->dim) ||
12955 !isl_space_has_named_params(ma->space))
12956 isl_die(map->ctx, isl_error_invalid,
12957 "unaligned unnamed parameters", goto error);
12958 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12959 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12961 return map_preimage_multi_aff(map, type, ma);
12962 error:
12963 isl_multi_aff_free(ma);
12964 return isl_map_free(map);
12967 /* Compute the preimage of "set" under the function represented by "ma".
12968 * In other words, plug in "ma" in "set". The result is a set
12969 * that lives in the domain space of "ma".
12971 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12972 __isl_take isl_multi_aff *ma)
12974 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12977 /* Compute the preimage of the domain of "map" under the function
12978 * represented by "ma".
12979 * In other words, plug in "ma" in the domain of "map".
12980 * The result is a map that lives in the same space as "map"
12981 * except that the domain has been replaced by the domain space of "ma".
12983 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12984 __isl_take isl_multi_aff *ma)
12986 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12989 /* Compute the preimage of the range of "map" under the function
12990 * represented by "ma".
12991 * In other words, plug in "ma" in the range of "map".
12992 * The result is a map that lives in the same space as "map"
12993 * except that the range has been replaced by the domain space of "ma".
12995 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12996 __isl_take isl_multi_aff *ma)
12998 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13001 /* Compute the preimage of "map" under the function represented by "pma".
13002 * In other words, plug in "pma" in the domain or range of "map".
13003 * The result is a map that lives in the same space as "map",
13004 * except that the space of type "type" has been replaced by
13005 * the domain space of "pma".
13007 * The parameters of "map" and "pma" are assumed to have been aligned.
13009 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13010 __isl_take isl_map *map, enum isl_dim_type type,
13011 __isl_take isl_pw_multi_aff *pma)
13013 int i;
13014 isl_map *res;
13016 if (!pma)
13017 goto error;
13019 if (pma->n == 0) {
13020 isl_pw_multi_aff_free(pma);
13021 res = isl_map_empty(isl_map_get_space(map));
13022 isl_map_free(map);
13023 return res;
13026 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13027 isl_multi_aff_copy(pma->p[0].maff));
13028 if (type == isl_dim_in)
13029 res = isl_map_intersect_domain(res,
13030 isl_map_copy(pma->p[0].set));
13031 else
13032 res = isl_map_intersect_range(res,
13033 isl_map_copy(pma->p[0].set));
13035 for (i = 1; i < pma->n; ++i) {
13036 isl_map *res_i;
13038 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13039 isl_multi_aff_copy(pma->p[i].maff));
13040 if (type == isl_dim_in)
13041 res_i = isl_map_intersect_domain(res_i,
13042 isl_map_copy(pma->p[i].set));
13043 else
13044 res_i = isl_map_intersect_range(res_i,
13045 isl_map_copy(pma->p[i].set));
13046 res = isl_map_union(res, res_i);
13049 isl_pw_multi_aff_free(pma);
13050 isl_map_free(map);
13051 return res;
13052 error:
13053 isl_pw_multi_aff_free(pma);
13054 isl_map_free(map);
13055 return NULL;
13058 /* Compute the preimage of "map" under the function represented by "pma".
13059 * In other words, plug in "pma" in the domain or range of "map".
13060 * The result is a map that lives in the same space as "map",
13061 * except that the space of type "type" has been replaced by
13062 * the domain space of "pma".
13064 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13065 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13067 if (!map || !pma)
13068 goto error;
13070 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13071 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13073 if (!isl_space_has_named_params(map->dim) ||
13074 !isl_space_has_named_params(pma->dim))
13075 isl_die(map->ctx, isl_error_invalid,
13076 "unaligned unnamed parameters", goto error);
13077 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13078 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13080 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13081 error:
13082 isl_pw_multi_aff_free(pma);
13083 return isl_map_free(map);
13086 /* Compute the preimage of "set" under the function represented by "pma".
13087 * In other words, plug in "pma" in "set". The result is a set
13088 * that lives in the domain space of "pma".
13090 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13091 __isl_take isl_pw_multi_aff *pma)
13093 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13096 /* Compute the preimage of the domain of "map" under the function
13097 * represented by "pma".
13098 * In other words, plug in "pma" in the domain of "map".
13099 * The result is a map that lives in the same space as "map",
13100 * except that domain space has been replaced by the domain space of "pma".
13102 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13103 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13105 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13108 /* Compute the preimage of the range of "map" under the function
13109 * represented by "pma".
13110 * In other words, plug in "pma" in the range of "map".
13111 * The result is a map that lives in the same space as "map",
13112 * except that range space has been replaced by the domain space of "pma".
13114 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13115 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13117 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13120 /* Compute the preimage of "map" under the function represented by "mpa".
13121 * In other words, plug in "mpa" in the domain or range of "map".
13122 * The result is a map that lives in the same space as "map",
13123 * except that the space of type "type" has been replaced by
13124 * the domain space of "mpa".
13126 * If the map does not involve any constraints that refer to the
13127 * dimensions of the substituted space, then the only possible
13128 * effect of "mpa" on the map is to map the space to a different space.
13129 * We create a separate isl_multi_aff to effectuate this change
13130 * in order to avoid spurious splitting of the map along the pieces
13131 * of "mpa".
13133 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13134 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13136 int n;
13137 isl_pw_multi_aff *pma;
13139 if (!map || !mpa)
13140 goto error;
13142 n = isl_map_dim(map, type);
13143 if (!isl_map_involves_dims(map, type, 0, n)) {
13144 isl_space *space;
13145 isl_multi_aff *ma;
13147 space = isl_multi_pw_aff_get_space(mpa);
13148 isl_multi_pw_aff_free(mpa);
13149 ma = isl_multi_aff_zero(space);
13150 return isl_map_preimage_multi_aff(map, type, ma);
13153 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13154 return isl_map_preimage_pw_multi_aff(map, type, pma);
13155 error:
13156 isl_map_free(map);
13157 isl_multi_pw_aff_free(mpa);
13158 return NULL;
13161 /* Compute the preimage of "map" under the function represented by "mpa".
13162 * In other words, plug in "mpa" in the domain "map".
13163 * The result is a map that lives in the same space as "map",
13164 * except that domain space has been replaced by the domain space of "mpa".
13166 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13167 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13169 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13172 /* Compute the preimage of "set" by the function represented by "mpa".
13173 * In other words, plug in "mpa" in "set".
13175 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13176 __isl_take isl_multi_pw_aff *mpa)
13178 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13181 /* Is the point "inner" internal to inequality constraint "ineq"
13182 * of "bset"?
13183 * The point is considered to be internal to the inequality constraint,
13184 * if it strictly lies on the positive side of the inequality constraint,
13185 * or if it lies on the constraint and the constraint is lexico-positive.
13187 static isl_bool is_internal(__isl_keep isl_vec *inner,
13188 __isl_keep isl_basic_set *bset, int ineq)
13190 isl_ctx *ctx;
13191 int pos;
13192 unsigned total;
13194 if (!inner || !bset)
13195 return isl_bool_error;
13197 ctx = isl_basic_set_get_ctx(bset);
13198 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13199 &ctx->normalize_gcd);
13200 if (!isl_int_is_zero(ctx->normalize_gcd))
13201 return isl_int_is_nonneg(ctx->normalize_gcd);
13203 total = isl_basic_set_dim(bset, isl_dim_all);
13204 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13205 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13208 /* Tighten the inequality constraints of "bset" that are outward with respect
13209 * to the point "vec".
13210 * That is, tighten the constraints that are not satisfied by "vec".
13212 * "vec" is a point internal to some superset S of "bset" that is used
13213 * to make the subsets of S disjoint, by tightening one half of the constraints
13214 * that separate two subsets. In particular, the constraints of S
13215 * are all satisfied by "vec" and should not be tightened.
13216 * Of the internal constraints, those that have "vec" on the outside
13217 * are tightened. The shared facet is included in the adjacent subset
13218 * with the opposite constraint.
13219 * For constraints that saturate "vec", this criterion cannot be used
13220 * to determine which of the two sides should be tightened.
13221 * Instead, the sign of the first non-zero coefficient is used
13222 * to make this choice. Note that this second criterion is never used
13223 * on the constraints of S since "vec" is interior to "S".
13225 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13226 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13228 int j;
13230 bset = isl_basic_set_cow(bset);
13231 if (!bset)
13232 return NULL;
13233 for (j = 0; j < bset->n_ineq; ++j) {
13234 isl_bool internal;
13236 internal = is_internal(vec, bset, j);
13237 if (internal < 0)
13238 return isl_basic_set_free(bset);
13239 if (internal)
13240 continue;
13241 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13244 return bset;