isl_output.c: print_qpolynomial_c: rename "dim" argument to "space"
[isl.git] / isl_map.c
blob0543d010ce4052b976106847011b30d8373578b4
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl/map.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
35 #include <isl_sort.h>
36 #include "isl_tab.h"
37 #include <isl/vec.h>
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
65 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
91 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
93 return map ? n(map->dim, type) : 0;
96 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
98 return set ? n(set->dim, type) : 0;
101 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
102 enum isl_dim_type type)
104 isl_space *space;
106 if (!bmap)
107 return 0;
109 space = bmap->dim;
110 switch (type) {
111 case isl_dim_cst: return 0;
112 case isl_dim_param: return 1;
113 case isl_dim_in: return 1 + space->nparam;
114 case isl_dim_out: return 1 + space->nparam + space->n_in;
115 case isl_dim_div: return 1 + space->nparam + space->n_in +
116 space->n_out;
117 default: return 0;
121 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
122 enum isl_dim_type type)
124 return isl_basic_map_offset(bset, type);
127 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
129 return pos(map->dim, type);
132 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
133 enum isl_dim_type type)
135 return isl_basic_map_dim(bset, type);
138 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
140 return isl_basic_set_dim(bset, isl_dim_set);
143 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
145 return isl_basic_set_dim(bset, isl_dim_param);
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
150 if (!bset)
151 return 0;
152 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
155 unsigned isl_set_n_dim(__isl_keep isl_set *set)
157 return isl_set_dim(set, isl_dim_set);
160 unsigned isl_set_n_param(__isl_keep isl_set *set)
162 return isl_set_dim(set, isl_dim_param);
165 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
167 return bmap ? bmap->dim->n_in : 0;
170 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
172 return bmap ? bmap->dim->n_out : 0;
175 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
177 return bmap ? bmap->dim->nparam : 0;
180 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
182 return bmap ? bmap->n_div : 0;
185 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
187 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
190 unsigned isl_map_n_in(const struct isl_map *map)
192 return map ? map->dim->n_in : 0;
195 unsigned isl_map_n_out(const struct isl_map *map)
197 return map ? map->dim->n_out : 0;
200 unsigned isl_map_n_param(const struct isl_map *map)
202 return map ? map->dim->nparam : 0;
205 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
207 int m;
208 if (!map || !set)
209 return -1;
210 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
211 if (m < 0 || !m)
212 return m;
213 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
214 set->dim, isl_dim_set);
217 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
218 __isl_keep isl_basic_set *bset)
220 isl_bool m;
221 if (!bmap || !bset)
222 return isl_bool_error;
223 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
224 if (m < 0 || !m)
225 return m;
226 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
227 bset->dim, isl_dim_set);
230 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
232 int m;
233 if (!map || !set)
234 return -1;
235 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
236 if (m < 0 || !m)
237 return m;
238 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
239 set->dim, isl_dim_set);
242 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
243 struct isl_basic_set *bset)
245 int m;
246 if (!bmap || !bset)
247 return -1;
248 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
249 if (m < 0 || !m)
250 return m;
251 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
252 bset->dim, isl_dim_set);
255 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
257 return bmap ? bmap->ctx : NULL;
260 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
262 return bset ? bset->ctx : NULL;
265 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
267 return map ? map->ctx : NULL;
270 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
272 return set ? set->ctx : NULL;
275 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
277 if (!bmap)
278 return NULL;
279 return isl_space_copy(bmap->dim);
282 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
284 if (!bset)
285 return NULL;
286 return isl_space_copy(bset->dim);
289 /* Extract the divs in "bmap" as a matrix.
291 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
293 int i;
294 isl_ctx *ctx;
295 isl_mat *div;
296 unsigned total;
297 unsigned cols;
299 if (!bmap)
300 return NULL;
302 ctx = isl_basic_map_get_ctx(bmap);
303 total = isl_space_dim(bmap->dim, isl_dim_all);
304 cols = 1 + 1 + total + bmap->n_div;
305 div = isl_mat_alloc(ctx, bmap->n_div, cols);
306 if (!div)
307 return NULL;
309 for (i = 0; i < bmap->n_div; ++i)
310 isl_seq_cpy(div->row[i], bmap->div[i], cols);
312 return div;
315 /* Extract the divs in "bset" as a matrix.
317 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
319 return isl_basic_map_get_divs(bset);
322 __isl_give isl_local_space *isl_basic_map_get_local_space(
323 __isl_keep isl_basic_map *bmap)
325 isl_mat *div;
327 if (!bmap)
328 return NULL;
330 div = isl_basic_map_get_divs(bmap);
331 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
334 __isl_give isl_local_space *isl_basic_set_get_local_space(
335 __isl_keep isl_basic_set *bset)
337 return isl_basic_map_get_local_space(bset);
340 /* For each known div d = floor(f/m), add the constraints
342 * f - m d >= 0
343 * -(f-(m-1)) + m d >= 0
345 * Do not finalize the result.
347 static __isl_give isl_basic_map *add_known_div_constraints(
348 __isl_take isl_basic_map *bmap)
350 int i;
351 unsigned n_div;
353 if (!bmap)
354 return NULL;
355 n_div = isl_basic_map_dim(bmap, isl_dim_div);
356 if (n_div == 0)
357 return bmap;
358 bmap = isl_basic_map_cow(bmap);
359 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
360 if (!bmap)
361 return NULL;
362 for (i = 0; i < n_div; ++i) {
363 if (isl_int_is_zero(bmap->div[i][0]))
364 continue;
365 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
366 return isl_basic_map_free(bmap);
369 return bmap;
372 __isl_give isl_basic_map *isl_basic_map_from_local_space(
373 __isl_take isl_local_space *ls)
375 int i;
376 int n_div;
377 isl_basic_map *bmap;
379 if (!ls)
380 return NULL;
382 n_div = isl_local_space_dim(ls, isl_dim_div);
383 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
384 n_div, 0, 2 * n_div);
386 for (i = 0; i < n_div; ++i)
387 if (isl_basic_map_alloc_div(bmap) < 0)
388 goto error;
390 for (i = 0; i < n_div; ++i)
391 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
392 bmap = add_known_div_constraints(bmap);
394 isl_local_space_free(ls);
395 return bmap;
396 error:
397 isl_local_space_free(ls);
398 isl_basic_map_free(bmap);
399 return NULL;
402 __isl_give isl_basic_set *isl_basic_set_from_local_space(
403 __isl_take isl_local_space *ls)
405 return isl_basic_map_from_local_space(ls);
408 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
410 if (!map)
411 return NULL;
412 return isl_space_copy(map->dim);
415 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
417 if (!set)
418 return NULL;
419 return isl_space_copy(set->dim);
422 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
423 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
425 bmap = isl_basic_map_cow(bmap);
426 if (!bmap)
427 return NULL;
428 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
429 if (!bmap->dim)
430 goto error;
431 bmap = isl_basic_map_finalize(bmap);
432 return bmap;
433 error:
434 isl_basic_map_free(bmap);
435 return NULL;
438 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
439 __isl_take isl_basic_set *bset, const char *s)
441 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
444 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
445 enum isl_dim_type type)
447 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
450 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
451 enum isl_dim_type type, const char *s)
453 int i;
455 map = isl_map_cow(map);
456 if (!map)
457 return NULL;
459 map->dim = isl_space_set_tuple_name(map->dim, type, s);
460 if (!map->dim)
461 goto error;
463 for (i = 0; i < map->n; ++i) {
464 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
465 if (!map->p[i])
466 goto error;
469 return map;
470 error:
471 isl_map_free(map);
472 return NULL;
475 /* Replace the identifier of the tuple of type "type" by "id".
477 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
478 __isl_take isl_basic_map *bmap,
479 enum isl_dim_type type, __isl_take isl_id *id)
481 bmap = isl_basic_map_cow(bmap);
482 if (!bmap)
483 goto error;
484 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
485 if (!bmap->dim)
486 return isl_basic_map_free(bmap);
487 bmap = isl_basic_map_finalize(bmap);
488 return bmap;
489 error:
490 isl_id_free(id);
491 return NULL;
494 /* Replace the identifier of the tuple by "id".
496 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
497 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
499 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
502 /* Does the input or output tuple have a name?
504 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
506 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
509 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
510 enum isl_dim_type type)
512 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
515 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
516 const char *s)
518 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
519 isl_dim_set, s));
522 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
523 enum isl_dim_type type, __isl_take isl_id *id)
525 map = isl_map_cow(map);
526 if (!map)
527 goto error;
529 map->dim = isl_space_set_tuple_id(map->dim, type, id);
531 return isl_map_reset_space(map, isl_space_copy(map->dim));
532 error:
533 isl_id_free(id);
534 return NULL;
537 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
538 __isl_take isl_id *id)
540 return isl_map_set_tuple_id(set, isl_dim_set, id);
543 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
544 enum isl_dim_type type)
546 map = isl_map_cow(map);
547 if (!map)
548 return NULL;
550 map->dim = isl_space_reset_tuple_id(map->dim, type);
552 return isl_map_reset_space(map, isl_space_copy(map->dim));
555 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
557 return isl_map_reset_tuple_id(set, isl_dim_set);
560 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
562 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
565 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
566 enum isl_dim_type type)
568 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
571 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
573 return isl_map_has_tuple_id(set, isl_dim_set);
576 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
578 return isl_map_get_tuple_id(set, isl_dim_set);
581 /* Does the set tuple have a name?
583 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
585 if (!set)
586 return isl_bool_error;
587 return isl_space_has_tuple_name(set->dim, isl_dim_set);
591 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
593 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
596 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
598 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
601 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
602 enum isl_dim_type type, unsigned pos)
604 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
607 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
608 enum isl_dim_type type, unsigned pos)
610 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
613 /* Does the given dimension have a name?
615 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
616 enum isl_dim_type type, unsigned pos)
618 if (!map)
619 return isl_bool_error;
620 return isl_space_has_dim_name(map->dim, type, pos);
623 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
624 enum isl_dim_type type, unsigned pos)
626 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
629 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
630 enum isl_dim_type type, unsigned pos)
632 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
635 /* Does the given dimension have a name?
637 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
638 enum isl_dim_type type, unsigned pos)
640 if (!set)
641 return isl_bool_error;
642 return isl_space_has_dim_name(set->dim, type, pos);
645 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
646 __isl_take isl_basic_map *bmap,
647 enum isl_dim_type type, unsigned pos, const char *s)
649 bmap = isl_basic_map_cow(bmap);
650 if (!bmap)
651 return NULL;
652 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
653 if (!bmap->dim)
654 goto error;
655 return isl_basic_map_finalize(bmap);
656 error:
657 isl_basic_map_free(bmap);
658 return NULL;
661 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
662 enum isl_dim_type type, unsigned pos, const char *s)
664 int i;
666 map = isl_map_cow(map);
667 if (!map)
668 return NULL;
670 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
671 if (!map->dim)
672 goto error;
674 for (i = 0; i < map->n; ++i) {
675 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
676 if (!map->p[i])
677 goto error;
680 return map;
681 error:
682 isl_map_free(map);
683 return NULL;
686 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
687 __isl_take isl_basic_set *bset,
688 enum isl_dim_type type, unsigned pos, const char *s)
690 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
691 type, pos, s));
694 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
695 enum isl_dim_type type, unsigned pos, const char *s)
697 return set_from_map(isl_map_set_dim_name(set_to_map(set),
698 type, pos, s));
701 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
702 enum isl_dim_type type, unsigned pos)
704 if (!bmap)
705 return isl_bool_error;
706 return isl_space_has_dim_id(bmap->dim, type, pos);
709 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
710 enum isl_dim_type type, unsigned pos)
712 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
715 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
716 enum isl_dim_type type, unsigned pos)
718 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
721 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
722 enum isl_dim_type type, unsigned pos)
724 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
727 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
728 enum isl_dim_type type, unsigned pos)
730 return isl_map_has_dim_id(set, type, pos);
733 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
734 enum isl_dim_type type, unsigned pos)
736 return isl_map_get_dim_id(set, type, pos);
739 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
740 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
742 map = isl_map_cow(map);
743 if (!map)
744 goto error;
746 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
748 return isl_map_reset_space(map, isl_space_copy(map->dim));
749 error:
750 isl_id_free(id);
751 return NULL;
754 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
755 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
757 return isl_map_set_dim_id(set, type, pos, id);
760 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
761 __isl_keep isl_id *id)
763 if (!map)
764 return -1;
765 return isl_space_find_dim_by_id(map->dim, type, id);
768 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
769 __isl_keep isl_id *id)
771 return isl_map_find_dim_by_id(set, type, id);
774 /* Return the position of the dimension of the given type and name
775 * in "bmap".
776 * Return -1 if no such dimension can be found.
778 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
779 enum isl_dim_type type, const char *name)
781 if (!bmap)
782 return -1;
783 return isl_space_find_dim_by_name(bmap->dim, type, name);
786 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
787 const char *name)
789 if (!map)
790 return -1;
791 return isl_space_find_dim_by_name(map->dim, type, name);
794 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
795 const char *name)
797 return isl_map_find_dim_by_name(set, type, name);
800 /* Reset the user pointer on all identifiers of parameters and tuples
801 * of the space of "map".
803 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
805 isl_space *space;
807 space = isl_map_get_space(map);
808 space = isl_space_reset_user(space);
809 map = isl_map_reset_space(map, space);
811 return map;
814 /* Reset the user pointer on all identifiers of parameters and tuples
815 * of the space of "set".
817 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
819 return isl_map_reset_user(set);
822 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
824 if (!bmap)
825 return -1;
826 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
829 /* Has "map" been marked as a rational map?
830 * In particular, have all basic maps in "map" been marked this way?
831 * An empty map is not considered to be rational.
832 * Maps where only some of the basic maps are marked rational
833 * are not allowed.
835 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
837 int i;
838 isl_bool rational;
840 if (!map)
841 return isl_bool_error;
842 if (map->n == 0)
843 return isl_bool_false;
844 rational = isl_basic_map_is_rational(map->p[0]);
845 if (rational < 0)
846 return rational;
847 for (i = 1; i < map->n; ++i) {
848 isl_bool rational_i;
850 rational_i = isl_basic_map_is_rational(map->p[i]);
851 if (rational_i < 0)
852 return rational_i;
853 if (rational != rational_i)
854 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
855 "mixed rational and integer basic maps "
856 "not supported", return isl_bool_error);
859 return rational;
862 /* Has "set" been marked as a rational set?
863 * In particular, have all basic set in "set" been marked this way?
864 * An empty set is not considered to be rational.
865 * Sets where only some of the basic sets are marked rational
866 * are not allowed.
868 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
870 return isl_map_is_rational(set);
873 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
875 return isl_basic_map_is_rational(bset);
878 /* Does "bmap" contain any rational points?
880 * If "bmap" has an equality for each dimension, equating the dimension
881 * to an integer constant, then it has no rational points, even if it
882 * is marked as rational.
884 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
886 int has_rational = 1;
887 unsigned total;
889 if (!bmap)
890 return -1;
891 if (isl_basic_map_plain_is_empty(bmap))
892 return 0;
893 if (!isl_basic_map_is_rational(bmap))
894 return 0;
895 bmap = isl_basic_map_copy(bmap);
896 bmap = isl_basic_map_implicit_equalities(bmap);
897 if (!bmap)
898 return -1;
899 total = isl_basic_map_total_dim(bmap);
900 if (bmap->n_eq == total) {
901 int i, j;
902 for (i = 0; i < bmap->n_eq; ++i) {
903 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
904 if (j < 0)
905 break;
906 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
907 !isl_int_is_negone(bmap->eq[i][1 + j]))
908 break;
909 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
910 total - j - 1);
911 if (j >= 0)
912 break;
914 if (i == bmap->n_eq)
915 has_rational = 0;
917 isl_basic_map_free(bmap);
919 return has_rational;
922 /* Does "map" contain any rational points?
924 int isl_map_has_rational(__isl_keep isl_map *map)
926 int i;
927 int has_rational;
929 if (!map)
930 return -1;
931 for (i = 0; i < map->n; ++i) {
932 has_rational = isl_basic_map_has_rational(map->p[i]);
933 if (has_rational < 0)
934 return -1;
935 if (has_rational)
936 return 1;
938 return 0;
941 /* Does "set" contain any rational points?
943 int isl_set_has_rational(__isl_keep isl_set *set)
945 return isl_map_has_rational(set);
948 /* Is this basic set a parameter domain?
950 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
952 if (!bset)
953 return -1;
954 return isl_space_is_params(bset->dim);
957 /* Is this set a parameter domain?
959 isl_bool isl_set_is_params(__isl_keep isl_set *set)
961 if (!set)
962 return isl_bool_error;
963 return isl_space_is_params(set->dim);
966 /* Is this map actually a parameter domain?
967 * Users should never call this function. Outside of isl,
968 * a map can never be a parameter domain.
970 int isl_map_is_params(__isl_keep isl_map *map)
972 if (!map)
973 return -1;
974 return isl_space_is_params(map->dim);
977 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
978 struct isl_basic_map *bmap, unsigned extra,
979 unsigned n_eq, unsigned n_ineq)
981 int i;
982 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
984 bmap->ctx = ctx;
985 isl_ctx_ref(ctx);
987 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
988 if (isl_blk_is_error(bmap->block))
989 goto error;
991 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
992 if ((n_ineq + n_eq) && !bmap->ineq)
993 goto error;
995 if (extra == 0) {
996 bmap->block2 = isl_blk_empty();
997 bmap->div = NULL;
998 } else {
999 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1000 if (isl_blk_is_error(bmap->block2))
1001 goto error;
1003 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1004 if (!bmap->div)
1005 goto error;
1008 for (i = 0; i < n_ineq + n_eq; ++i)
1009 bmap->ineq[i] = bmap->block.data + i * row_size;
1011 for (i = 0; i < extra; ++i)
1012 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1014 bmap->ref = 1;
1015 bmap->flags = 0;
1016 bmap->c_size = n_eq + n_ineq;
1017 bmap->eq = bmap->ineq + n_ineq;
1018 bmap->extra = extra;
1019 bmap->n_eq = 0;
1020 bmap->n_ineq = 0;
1021 bmap->n_div = 0;
1022 bmap->sample = NULL;
1024 return bmap;
1025 error:
1026 isl_basic_map_free(bmap);
1027 return NULL;
1030 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1031 unsigned nparam, unsigned dim, unsigned extra,
1032 unsigned n_eq, unsigned n_ineq)
1034 struct isl_basic_map *bmap;
1035 isl_space *space;
1037 space = isl_space_set_alloc(ctx, nparam, dim);
1038 if (!space)
1039 return NULL;
1041 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1042 return bset_from_bmap(bmap);
1045 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1046 unsigned extra, unsigned n_eq, unsigned n_ineq)
1048 struct isl_basic_map *bmap;
1049 if (!dim)
1050 return NULL;
1051 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1052 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1053 return bset_from_bmap(bmap);
1054 error:
1055 isl_space_free(dim);
1056 return NULL;
1059 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1060 unsigned extra, unsigned n_eq, unsigned n_ineq)
1062 struct isl_basic_map *bmap;
1064 if (!dim)
1065 return NULL;
1066 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1067 if (!bmap)
1068 goto error;
1069 bmap->dim = dim;
1071 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1072 error:
1073 isl_space_free(dim);
1074 return NULL;
1077 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1078 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1079 unsigned n_eq, unsigned n_ineq)
1081 struct isl_basic_map *bmap;
1082 isl_space *dim;
1084 dim = isl_space_alloc(ctx, nparam, in, out);
1085 if (!dim)
1086 return NULL;
1088 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1089 return bmap;
1092 static void dup_constraints(
1093 struct isl_basic_map *dst, struct isl_basic_map *src)
1095 int i;
1096 unsigned total = isl_basic_map_total_dim(src);
1098 for (i = 0; i < src->n_eq; ++i) {
1099 int j = isl_basic_map_alloc_equality(dst);
1100 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1103 for (i = 0; i < src->n_ineq; ++i) {
1104 int j = isl_basic_map_alloc_inequality(dst);
1105 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1108 for (i = 0; i < src->n_div; ++i) {
1109 int j = isl_basic_map_alloc_div(dst);
1110 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1112 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1115 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1117 struct isl_basic_map *dup;
1119 if (!bmap)
1120 return NULL;
1121 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1122 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1123 if (!dup)
1124 return NULL;
1125 dup_constraints(dup, bmap);
1126 dup->flags = bmap->flags;
1127 dup->sample = isl_vec_copy(bmap->sample);
1128 return dup;
1131 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1133 struct isl_basic_map *dup;
1135 dup = isl_basic_map_dup(bset_to_bmap(bset));
1136 return bset_from_bmap(dup);
1139 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1141 if (!bset)
1142 return NULL;
1144 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1145 bset->ref++;
1146 return bset;
1148 return isl_basic_set_dup(bset);
1151 struct isl_set *isl_set_copy(struct isl_set *set)
1153 if (!set)
1154 return NULL;
1156 set->ref++;
1157 return set;
1160 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1162 if (!bmap)
1163 return NULL;
1165 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1166 bmap->ref++;
1167 return bmap;
1169 bmap = isl_basic_map_dup(bmap);
1170 if (bmap)
1171 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1172 return bmap;
1175 struct isl_map *isl_map_copy(struct isl_map *map)
1177 if (!map)
1178 return NULL;
1180 map->ref++;
1181 return map;
1184 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1186 if (!bmap)
1187 return NULL;
1189 if (--bmap->ref > 0)
1190 return NULL;
1192 isl_ctx_deref(bmap->ctx);
1193 free(bmap->div);
1194 isl_blk_free(bmap->ctx, bmap->block2);
1195 free(bmap->ineq);
1196 isl_blk_free(bmap->ctx, bmap->block);
1197 isl_vec_free(bmap->sample);
1198 isl_space_free(bmap->dim);
1199 free(bmap);
1201 return NULL;
1204 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1206 return isl_basic_map_free(bset_to_bmap(bset));
1209 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1211 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1214 __isl_give isl_map *isl_map_align_params_map_map_and(
1215 __isl_take isl_map *map1, __isl_take isl_map *map2,
1216 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1217 __isl_take isl_map *map2))
1219 if (!map1 || !map2)
1220 goto error;
1221 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1222 return fn(map1, map2);
1223 if (!isl_space_has_named_params(map1->dim) ||
1224 !isl_space_has_named_params(map2->dim))
1225 isl_die(map1->ctx, isl_error_invalid,
1226 "unaligned unnamed parameters", goto error);
1227 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1228 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1229 return fn(map1, map2);
1230 error:
1231 isl_map_free(map1);
1232 isl_map_free(map2);
1233 return NULL;
1236 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1237 __isl_keep isl_map *map2,
1238 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1240 isl_bool r;
1242 if (!map1 || !map2)
1243 return isl_bool_error;
1244 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1245 return fn(map1, map2);
1246 if (!isl_space_has_named_params(map1->dim) ||
1247 !isl_space_has_named_params(map2->dim))
1248 isl_die(map1->ctx, isl_error_invalid,
1249 "unaligned unnamed parameters", return isl_bool_error);
1250 map1 = isl_map_copy(map1);
1251 map2 = isl_map_copy(map2);
1252 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1253 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1254 r = fn(map1, map2);
1255 isl_map_free(map1);
1256 isl_map_free(map2);
1257 return r;
1260 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1262 struct isl_ctx *ctx;
1263 if (!bmap)
1264 return -1;
1265 ctx = bmap->ctx;
1266 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1267 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1268 return -1);
1269 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1274 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1275 isl_int *t;
1276 int j = isl_basic_map_alloc_inequality(bmap);
1277 if (j < 0)
1278 return -1;
1279 t = bmap->ineq[j];
1280 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1281 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1282 bmap->eq[-1] = t;
1283 bmap->n_eq++;
1284 bmap->n_ineq--;
1285 bmap->eq--;
1286 return 0;
1288 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1289 bmap->extra - bmap->n_div);
1290 return bmap->n_eq++;
1293 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1295 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1298 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1300 if (!bmap)
1301 return -1;
1302 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1303 bmap->n_eq -= n;
1304 return 0;
1307 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1309 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1312 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1314 isl_int *t;
1315 if (!bmap)
1316 return -1;
1317 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1319 if (pos != bmap->n_eq - 1) {
1320 t = bmap->eq[pos];
1321 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1322 bmap->eq[bmap->n_eq - 1] = t;
1324 bmap->n_eq--;
1325 return 0;
1328 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1330 return isl_basic_map_drop_equality(bset_to_bmap(bset), pos);
1333 /* Turn inequality "pos" of "bmap" into an equality.
1335 * In particular, we move the inequality in front of the equalities
1336 * and move the last inequality in the position of the moved inequality.
1337 * Note that isl_tab_make_equalities_explicit depends on this particular
1338 * change in the ordering of the constraints.
1340 void isl_basic_map_inequality_to_equality(
1341 struct isl_basic_map *bmap, unsigned pos)
1343 isl_int *t;
1345 t = bmap->ineq[pos];
1346 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1347 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1348 bmap->eq[-1] = t;
1349 bmap->n_eq++;
1350 bmap->n_ineq--;
1351 bmap->eq--;
1352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1353 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1355 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1358 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1360 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1363 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1365 struct isl_ctx *ctx;
1366 if (!bmap)
1367 return -1;
1368 ctx = bmap->ctx;
1369 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1370 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1371 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1372 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1373 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1374 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1375 1 + isl_basic_map_total_dim(bmap),
1376 bmap->extra - bmap->n_div);
1377 return bmap->n_ineq++;
1380 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1382 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1385 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1387 if (!bmap)
1388 return -1;
1389 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1390 bmap->n_ineq -= n;
1391 return 0;
1394 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1396 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1399 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1401 isl_int *t;
1402 if (!bmap)
1403 return -1;
1404 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1406 if (pos != bmap->n_ineq - 1) {
1407 t = bmap->ineq[pos];
1408 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1409 bmap->ineq[bmap->n_ineq - 1] = t;
1410 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1412 bmap->n_ineq--;
1413 return 0;
1416 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1418 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1421 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1422 isl_int *eq)
1424 int k;
1426 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1427 if (!bmap)
1428 return NULL;
1429 k = isl_basic_map_alloc_equality(bmap);
1430 if (k < 0)
1431 goto error;
1432 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1433 return bmap;
1434 error:
1435 isl_basic_map_free(bmap);
1436 return NULL;
1439 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1440 isl_int *eq)
1442 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1445 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1446 isl_int *ineq)
1448 int k;
1450 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1451 if (!bmap)
1452 return NULL;
1453 k = isl_basic_map_alloc_inequality(bmap);
1454 if (k < 0)
1455 goto error;
1456 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1457 return bmap;
1458 error:
1459 isl_basic_map_free(bmap);
1460 return NULL;
1463 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1464 isl_int *ineq)
1466 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1469 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1471 if (!bmap)
1472 return -1;
1473 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1474 isl_seq_clr(bmap->div[bmap->n_div] +
1475 1 + 1 + isl_basic_map_total_dim(bmap),
1476 bmap->extra - bmap->n_div);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1478 return bmap->n_div++;
1481 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1483 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1486 /* Insert an extra integer division, prescribed by "div", to "bmap"
1487 * at (integer division) position "pos".
1489 * The integer division is first added at the end and then moved
1490 * into the right position.
1492 __isl_give isl_basic_map *isl_basic_map_insert_div(
1493 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1495 int i, k, n_div;
1497 bmap = isl_basic_map_cow(bmap);
1498 if (!bmap || !div)
1499 return isl_basic_map_free(bmap);
1501 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1502 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1503 "unexpected size", return isl_basic_map_free(bmap));
1504 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1505 if (pos < 0 || pos > n_div)
1506 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1507 "invalid position", return isl_basic_map_free(bmap));
1509 bmap = isl_basic_map_extend_space(bmap,
1510 isl_basic_map_get_space(bmap), 1, 0, 2);
1511 k = isl_basic_map_alloc_div(bmap);
1512 if (k < 0)
1513 return isl_basic_map_free(bmap);
1514 isl_seq_cpy(bmap->div[k], div->el, div->size);
1515 isl_int_set_si(bmap->div[k][div->size], 0);
1517 for (i = k; i > pos; --i)
1518 isl_basic_map_swap_div(bmap, i, i - 1);
1520 return bmap;
1523 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1525 if (!bmap)
1526 return -1;
1527 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1528 bmap->n_div -= n;
1529 return 0;
1532 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1534 return isl_basic_map_free_div(bset_to_bmap(bset), n);
1537 /* Copy constraint from src to dst, putting the vars of src at offset
1538 * dim_off in dst and the divs of src at offset div_off in dst.
1539 * If both sets are actually map, then dim_off applies to the input
1540 * variables.
1542 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1543 struct isl_basic_map *src_map, isl_int *src,
1544 unsigned in_off, unsigned out_off, unsigned div_off)
1546 unsigned src_nparam = isl_basic_map_n_param(src_map);
1547 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1548 unsigned src_in = isl_basic_map_n_in(src_map);
1549 unsigned dst_in = isl_basic_map_n_in(dst_map);
1550 unsigned src_out = isl_basic_map_n_out(src_map);
1551 unsigned dst_out = isl_basic_map_n_out(dst_map);
1552 isl_int_set(dst[0], src[0]);
1553 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1554 if (dst_nparam > src_nparam)
1555 isl_seq_clr(dst+1+src_nparam,
1556 dst_nparam - src_nparam);
1557 isl_seq_clr(dst+1+dst_nparam, in_off);
1558 isl_seq_cpy(dst+1+dst_nparam+in_off,
1559 src+1+src_nparam,
1560 isl_min(dst_in-in_off, src_in));
1561 if (dst_in-in_off > src_in)
1562 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1563 dst_in - in_off - src_in);
1564 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1565 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1566 src+1+src_nparam+src_in,
1567 isl_min(dst_out-out_off, src_out));
1568 if (dst_out-out_off > src_out)
1569 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1570 dst_out - out_off - src_out);
1571 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1572 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1573 src+1+src_nparam+src_in+src_out,
1574 isl_min(dst_map->extra-div_off, src_map->n_div));
1575 if (dst_map->n_div-div_off > src_map->n_div)
1576 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1577 div_off+src_map->n_div,
1578 dst_map->n_div - div_off - src_map->n_div);
1581 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1582 struct isl_basic_map *src_map, isl_int *src,
1583 unsigned in_off, unsigned out_off, unsigned div_off)
1585 isl_int_set(dst[0], src[0]);
1586 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1589 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1590 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1592 int i;
1593 unsigned div_off;
1595 if (!bmap1 || !bmap2)
1596 goto error;
1598 div_off = bmap1->n_div;
1600 for (i = 0; i < bmap2->n_eq; ++i) {
1601 int i1 = isl_basic_map_alloc_equality(bmap1);
1602 if (i1 < 0)
1603 goto error;
1604 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1605 i_pos, o_pos, div_off);
1608 for (i = 0; i < bmap2->n_ineq; ++i) {
1609 int i1 = isl_basic_map_alloc_inequality(bmap1);
1610 if (i1 < 0)
1611 goto error;
1612 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1613 i_pos, o_pos, div_off);
1616 for (i = 0; i < bmap2->n_div; ++i) {
1617 int i1 = isl_basic_map_alloc_div(bmap1);
1618 if (i1 < 0)
1619 goto error;
1620 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1621 i_pos, o_pos, div_off);
1624 isl_basic_map_free(bmap2);
1626 return bmap1;
1628 error:
1629 isl_basic_map_free(bmap1);
1630 isl_basic_map_free(bmap2);
1631 return NULL;
1634 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1635 struct isl_basic_set *bset2, unsigned pos)
1637 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1638 bset_to_bmap(bset2), 0, pos));
1641 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1642 __isl_take isl_space *dim, unsigned extra,
1643 unsigned n_eq, unsigned n_ineq)
1645 struct isl_basic_map *ext;
1646 unsigned flags;
1647 int dims_ok;
1649 if (!dim)
1650 goto error;
1652 if (!base)
1653 goto error;
1655 dims_ok = isl_space_is_equal(base->dim, dim) &&
1656 base->extra >= base->n_div + extra;
1658 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1659 room_for_ineq(base, n_ineq)) {
1660 isl_space_free(dim);
1661 return base;
1664 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1665 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1666 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1667 extra += base->extra;
1668 n_eq += base->n_eq;
1669 n_ineq += base->n_ineq;
1671 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1672 dim = NULL;
1673 if (!ext)
1674 goto error;
1676 if (dims_ok)
1677 ext->sample = isl_vec_copy(base->sample);
1678 flags = base->flags;
1679 ext = add_constraints(ext, base, 0, 0);
1680 if (ext) {
1681 ext->flags = flags;
1682 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1685 return ext;
1687 error:
1688 isl_space_free(dim);
1689 isl_basic_map_free(base);
1690 return NULL;
1693 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1694 __isl_take isl_space *dim, unsigned extra,
1695 unsigned n_eq, unsigned n_ineq)
1697 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1698 dim, extra, n_eq, n_ineq));
1701 struct isl_basic_map *isl_basic_map_extend_constraints(
1702 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1704 if (!base)
1705 return NULL;
1706 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1707 0, n_eq, n_ineq);
1710 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1711 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1712 unsigned n_eq, unsigned n_ineq)
1714 struct isl_basic_map *bmap;
1715 isl_space *dim;
1717 if (!base)
1718 return NULL;
1719 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1720 if (!dim)
1721 goto error;
1723 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1724 return bmap;
1725 error:
1726 isl_basic_map_free(base);
1727 return NULL;
1730 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1731 unsigned nparam, unsigned dim, unsigned extra,
1732 unsigned n_eq, unsigned n_ineq)
1734 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1735 nparam, 0, dim, extra, n_eq, n_ineq));
1738 struct isl_basic_set *isl_basic_set_extend_constraints(
1739 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1741 isl_basic_map *bmap = bset_to_bmap(base);
1742 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1743 return bset_from_bmap(bmap);
1746 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1748 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1751 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1753 if (!bmap)
1754 return NULL;
1756 if (bmap->ref > 1) {
1757 bmap->ref--;
1758 bmap = isl_basic_map_dup(bmap);
1760 if (bmap) {
1761 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1762 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1764 return bmap;
1767 /* Clear all cached information in "map", either because it is about
1768 * to be modified or because it is being freed.
1769 * Always return the same pointer that is passed in.
1770 * This is needed for the use in isl_map_free.
1772 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1774 isl_basic_map_free(map->cached_simple_hull[0]);
1775 isl_basic_map_free(map->cached_simple_hull[1]);
1776 map->cached_simple_hull[0] = NULL;
1777 map->cached_simple_hull[1] = NULL;
1778 return map;
1781 struct isl_set *isl_set_cow(struct isl_set *set)
1783 return isl_map_cow(set);
1786 /* Return an isl_map that is equal to "map" and that has only
1787 * a single reference.
1789 * If the original input already has only one reference, then
1790 * simply return it, but clear all cached information, since
1791 * it may be rendered invalid by the operations that will be
1792 * performed on the result.
1794 * Otherwise, create a duplicate (without any cached information).
1796 struct isl_map *isl_map_cow(struct isl_map *map)
1798 if (!map)
1799 return NULL;
1801 if (map->ref == 1)
1802 return clear_caches(map);
1803 map->ref--;
1804 return isl_map_dup(map);
1807 static void swap_vars(struct isl_blk blk, isl_int *a,
1808 unsigned a_len, unsigned b_len)
1810 isl_seq_cpy(blk.data, a+a_len, b_len);
1811 isl_seq_cpy(blk.data+b_len, a, a_len);
1812 isl_seq_cpy(a, blk.data, b_len+a_len);
1815 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1816 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1818 int i;
1819 struct isl_blk blk;
1821 if (!bmap)
1822 goto error;
1824 isl_assert(bmap->ctx,
1825 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1827 if (n1 == 0 || n2 == 0)
1828 return bmap;
1830 bmap = isl_basic_map_cow(bmap);
1831 if (!bmap)
1832 return NULL;
1834 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1835 if (isl_blk_is_error(blk))
1836 goto error;
1838 for (i = 0; i < bmap->n_eq; ++i)
1839 swap_vars(blk,
1840 bmap->eq[i] + pos, n1, n2);
1842 for (i = 0; i < bmap->n_ineq; ++i)
1843 swap_vars(blk,
1844 bmap->ineq[i] + pos, n1, n2);
1846 for (i = 0; i < bmap->n_div; ++i)
1847 swap_vars(blk,
1848 bmap->div[i]+1 + pos, n1, n2);
1850 isl_blk_free(bmap->ctx, blk);
1852 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1853 bmap = isl_basic_map_gauss(bmap, NULL);
1854 return isl_basic_map_finalize(bmap);
1855 error:
1856 isl_basic_map_free(bmap);
1857 return NULL;
1860 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1862 int i = 0;
1863 unsigned total;
1864 if (!bmap)
1865 goto error;
1866 total = isl_basic_map_total_dim(bmap);
1867 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1868 return isl_basic_map_free(bmap);
1869 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1870 if (bmap->n_eq > 0)
1871 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1872 else {
1873 i = isl_basic_map_alloc_equality(bmap);
1874 if (i < 0)
1875 goto error;
1877 isl_int_set_si(bmap->eq[i][0], 1);
1878 isl_seq_clr(bmap->eq[i]+1, total);
1879 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1880 isl_vec_free(bmap->sample);
1881 bmap->sample = NULL;
1882 return isl_basic_map_finalize(bmap);
1883 error:
1884 isl_basic_map_free(bmap);
1885 return NULL;
1888 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1890 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1893 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1894 * of "bmap").
1896 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1898 isl_int *t = bmap->div[a];
1899 bmap->div[a] = bmap->div[b];
1900 bmap->div[b] = t;
1903 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1904 * div definitions accordingly.
1906 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1908 int i;
1909 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1911 swap_div(bmap, a, b);
1913 for (i = 0; i < bmap->n_eq; ++i)
1914 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1916 for (i = 0; i < bmap->n_ineq; ++i)
1917 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1919 for (i = 0; i < bmap->n_div; ++i)
1920 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1921 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1924 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1925 * div definitions accordingly.
1927 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1929 isl_basic_map_swap_div(bset, a, b);
1932 /* Eliminate the specified n dimensions starting at first from the
1933 * constraints, without removing the dimensions from the space.
1934 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1936 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1937 enum isl_dim_type type, unsigned first, unsigned n)
1939 int i;
1941 if (!map)
1942 return NULL;
1943 if (n == 0)
1944 return map;
1946 if (first + n > isl_map_dim(map, type) || first + n < first)
1947 isl_die(map->ctx, isl_error_invalid,
1948 "index out of bounds", goto error);
1950 map = isl_map_cow(map);
1951 if (!map)
1952 return NULL;
1954 for (i = 0; i < map->n; ++i) {
1955 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1956 if (!map->p[i])
1957 goto error;
1959 return map;
1960 error:
1961 isl_map_free(map);
1962 return NULL;
1965 /* Eliminate the specified n dimensions starting at first from the
1966 * constraints, without removing the dimensions from the space.
1967 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1969 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1970 enum isl_dim_type type, unsigned first, unsigned n)
1972 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1975 /* Eliminate the specified n dimensions starting at first from the
1976 * constraints, without removing the dimensions from the space.
1977 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1979 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1980 unsigned first, unsigned n)
1982 return isl_set_eliminate(set, isl_dim_set, first, n);
1985 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1986 __isl_take isl_basic_map *bmap)
1988 if (!bmap)
1989 return NULL;
1990 bmap = isl_basic_map_eliminate_vars(bmap,
1991 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1992 if (!bmap)
1993 return NULL;
1994 bmap->n_div = 0;
1995 return isl_basic_map_finalize(bmap);
1998 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1999 __isl_take isl_basic_set *bset)
2001 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2004 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2006 int i;
2008 if (!map)
2009 return NULL;
2010 if (map->n == 0)
2011 return map;
2013 map = isl_map_cow(map);
2014 if (!map)
2015 return NULL;
2017 for (i = 0; i < map->n; ++i) {
2018 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2019 if (!map->p[i])
2020 goto error;
2022 return map;
2023 error:
2024 isl_map_free(map);
2025 return NULL;
2028 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2030 return isl_map_remove_divs(set);
2033 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2034 enum isl_dim_type type, unsigned first, unsigned n)
2036 if (!bmap)
2037 return NULL;
2038 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2039 goto error);
2040 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2041 return bmap;
2042 bmap = isl_basic_map_eliminate_vars(bmap,
2043 isl_basic_map_offset(bmap, type) - 1 + first, n);
2044 if (!bmap)
2045 return bmap;
2046 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2047 return bmap;
2048 bmap = isl_basic_map_drop(bmap, type, first, n);
2049 return bmap;
2050 error:
2051 isl_basic_map_free(bmap);
2052 return NULL;
2055 /* Return true if the definition of the given div (recursively) involves
2056 * any of the given variables.
2058 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2059 unsigned first, unsigned n)
2061 int i;
2062 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2064 if (isl_int_is_zero(bmap->div[div][0]))
2065 return 0;
2066 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2067 return 1;
2069 for (i = bmap->n_div - 1; i >= 0; --i) {
2070 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2071 continue;
2072 if (div_involves_vars(bmap, i, first, n))
2073 return 1;
2076 return 0;
2079 /* Try and add a lower and/or upper bound on "div" to "bmap"
2080 * based on inequality "i".
2081 * "total" is the total number of variables (excluding the divs).
2082 * "v" is a temporary object that can be used during the calculations.
2083 * If "lb" is set, then a lower bound should be constructed.
2084 * If "ub" is set, then an upper bound should be constructed.
2086 * The calling function has already checked that the inequality does not
2087 * reference "div", but we still need to check that the inequality is
2088 * of the right form. We'll consider the case where we want to construct
2089 * a lower bound. The construction of upper bounds is similar.
2091 * Let "div" be of the form
2093 * q = floor((a + f(x))/d)
2095 * We essentially check if constraint "i" is of the form
2097 * b + f(x) >= 0
2099 * so that we can use it to derive a lower bound on "div".
2100 * However, we allow a slightly more general form
2102 * b + g(x) >= 0
2104 * with the condition that the coefficients of g(x) - f(x) are all
2105 * divisible by d.
2106 * Rewriting this constraint as
2108 * 0 >= -b - g(x)
2110 * adding a + f(x) to both sides and dividing by d, we obtain
2112 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2114 * Taking the floor on both sides, we obtain
2116 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2118 * or
2120 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2122 * In the case of an upper bound, we construct the constraint
2124 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2127 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2128 __isl_take isl_basic_map *bmap, int div, int i,
2129 unsigned total, isl_int v, int lb, int ub)
2131 int j;
2133 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2134 if (lb) {
2135 isl_int_sub(v, bmap->ineq[i][1 + j],
2136 bmap->div[div][1 + 1 + j]);
2137 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2139 if (ub) {
2140 isl_int_add(v, bmap->ineq[i][1 + j],
2141 bmap->div[div][1 + 1 + j]);
2142 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2145 if (!lb && !ub)
2146 return bmap;
2148 bmap = isl_basic_map_cow(bmap);
2149 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2150 if (lb) {
2151 int k = isl_basic_map_alloc_inequality(bmap);
2152 if (k < 0)
2153 goto error;
2154 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2155 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2156 bmap->div[div][1 + j]);
2157 isl_int_cdiv_q(bmap->ineq[k][j],
2158 bmap->ineq[k][j], bmap->div[div][0]);
2160 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2162 if (ub) {
2163 int k = isl_basic_map_alloc_inequality(bmap);
2164 if (k < 0)
2165 goto error;
2166 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2167 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2168 bmap->div[div][1 + j]);
2169 isl_int_fdiv_q(bmap->ineq[k][j],
2170 bmap->ineq[k][j], bmap->div[div][0]);
2172 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2175 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2176 return bmap;
2177 error:
2178 isl_basic_map_free(bmap);
2179 return NULL;
2182 /* This function is called right before "div" is eliminated from "bmap"
2183 * using Fourier-Motzkin.
2184 * Look through the constraints of "bmap" for constraints on the argument
2185 * of the integer division and use them to construct constraints on the
2186 * integer division itself. These constraints can then be combined
2187 * during the Fourier-Motzkin elimination.
2188 * Note that it is only useful to introduce lower bounds on "div"
2189 * if "bmap" already contains upper bounds on "div" as the newly
2190 * introduce lower bounds can then be combined with the pre-existing
2191 * upper bounds. Similarly for upper bounds.
2192 * We therefore first check if "bmap" contains any lower and/or upper bounds
2193 * on "div".
2195 * It is interesting to note that the introduction of these constraints
2196 * can indeed lead to more accurate results, even when compared to
2197 * deriving constraints on the argument of "div" from constraints on "div".
2198 * Consider, for example, the set
2200 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2202 * The second constraint can be rewritten as
2204 * 2 * [(-i-2j+3)/4] + k >= 0
2206 * from which we can derive
2208 * -i - 2j + 3 >= -2k
2210 * or
2212 * i + 2j <= 3 + 2k
2214 * Combined with the first constraint, we obtain
2216 * -3 <= 3 + 2k or k >= -3
2218 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2219 * the first constraint, we obtain
2221 * [(i + 2j)/4] >= [-3/4] = -1
2223 * Combining this constraint with the second constraint, we obtain
2225 * k >= -2
2227 static __isl_give isl_basic_map *insert_bounds_on_div(
2228 __isl_take isl_basic_map *bmap, int div)
2230 int i;
2231 int check_lb, check_ub;
2232 isl_int v;
2233 unsigned total;
2235 if (!bmap)
2236 return NULL;
2238 if (isl_int_is_zero(bmap->div[div][0]))
2239 return bmap;
2241 total = isl_space_dim(bmap->dim, isl_dim_all);
2243 check_lb = 0;
2244 check_ub = 0;
2245 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2246 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2247 if (s > 0)
2248 check_ub = 1;
2249 if (s < 0)
2250 check_lb = 1;
2253 if (!check_lb && !check_ub)
2254 return bmap;
2256 isl_int_init(v);
2258 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2259 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2260 continue;
2262 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2263 check_lb, check_ub);
2266 isl_int_clear(v);
2268 return bmap;
2271 /* Remove all divs (recursively) involving any of the given dimensions
2272 * in their definitions.
2274 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2275 __isl_take isl_basic_map *bmap,
2276 enum isl_dim_type type, unsigned first, unsigned n)
2278 int i;
2280 if (!bmap)
2281 return NULL;
2282 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2283 goto error);
2284 first += isl_basic_map_offset(bmap, type);
2286 for (i = bmap->n_div - 1; i >= 0; --i) {
2287 if (!div_involves_vars(bmap, i, first, n))
2288 continue;
2289 bmap = insert_bounds_on_div(bmap, i);
2290 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2291 if (!bmap)
2292 return NULL;
2293 i = bmap->n_div;
2296 return bmap;
2297 error:
2298 isl_basic_map_free(bmap);
2299 return NULL;
2302 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2303 __isl_take isl_basic_set *bset,
2304 enum isl_dim_type type, unsigned first, unsigned n)
2306 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2309 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2310 enum isl_dim_type type, unsigned first, unsigned n)
2312 int i;
2314 if (!map)
2315 return NULL;
2316 if (map->n == 0)
2317 return map;
2319 map = isl_map_cow(map);
2320 if (!map)
2321 return NULL;
2323 for (i = 0; i < map->n; ++i) {
2324 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2325 type, first, n);
2326 if (!map->p[i])
2327 goto error;
2329 return map;
2330 error:
2331 isl_map_free(map);
2332 return NULL;
2335 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2336 enum isl_dim_type type, unsigned first, unsigned n)
2338 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2339 type, first, n));
2342 /* Does the description of "bmap" depend on the specified dimensions?
2343 * We also check whether the dimensions appear in any of the div definitions.
2344 * In principle there is no need for this check. If the dimensions appear
2345 * in a div definition, they also appear in the defining constraints of that
2346 * div.
2348 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2349 enum isl_dim_type type, unsigned first, unsigned n)
2351 int i;
2353 if (!bmap)
2354 return isl_bool_error;
2356 if (first + n > isl_basic_map_dim(bmap, type))
2357 isl_die(bmap->ctx, isl_error_invalid,
2358 "index out of bounds", return isl_bool_error);
2360 first += isl_basic_map_offset(bmap, type);
2361 for (i = 0; i < bmap->n_eq; ++i)
2362 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2363 return isl_bool_true;
2364 for (i = 0; i < bmap->n_ineq; ++i)
2365 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2366 return isl_bool_true;
2367 for (i = 0; i < bmap->n_div; ++i) {
2368 if (isl_int_is_zero(bmap->div[i][0]))
2369 continue;
2370 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2371 return isl_bool_true;
2374 return isl_bool_false;
2377 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2378 enum isl_dim_type type, unsigned first, unsigned n)
2380 int i;
2382 if (!map)
2383 return isl_bool_error;
2385 if (first + n > isl_map_dim(map, type))
2386 isl_die(map->ctx, isl_error_invalid,
2387 "index out of bounds", return isl_bool_error);
2389 for (i = 0; i < map->n; ++i) {
2390 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2391 type, first, n);
2392 if (involves < 0 || involves)
2393 return involves;
2396 return isl_bool_false;
2399 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2400 enum isl_dim_type type, unsigned first, unsigned n)
2402 return isl_basic_map_involves_dims(bset, type, first, n);
2405 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2406 enum isl_dim_type type, unsigned first, unsigned n)
2408 return isl_map_involves_dims(set, type, first, n);
2411 /* Does local variable "div" of "bmap" have a complete explicit representation?
2412 * Having a complete explicit representation requires not only
2413 * an explicit representation, but also that all local variables
2414 * that appear in this explicit representation in turn have
2415 * a complete explicit representation.
2417 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2419 int i;
2420 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2421 isl_bool marked;
2423 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2424 if (marked < 0 || marked)
2425 return isl_bool_not(marked);
2427 for (i = bmap->n_div - 1; i >= 0; --i) {
2428 isl_bool known;
2430 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2431 continue;
2432 known = isl_basic_map_div_is_known(bmap, i);
2433 if (known < 0 || !known)
2434 return known;
2437 return isl_bool_true;
2440 /* Does local variable "div" of "bset" have a complete explicit representation?
2442 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2444 return isl_basic_map_div_is_known(bset, div);
2447 /* Remove all divs that are unknown or defined in terms of unknown divs.
2449 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2450 __isl_take isl_basic_map *bmap)
2452 int i;
2454 if (!bmap)
2455 return NULL;
2457 for (i = bmap->n_div - 1; i >= 0; --i) {
2458 if (isl_basic_map_div_is_known(bmap, i))
2459 continue;
2460 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2461 if (!bmap)
2462 return NULL;
2463 i = bmap->n_div;
2466 return bmap;
2469 /* Remove all divs that are unknown or defined in terms of unknown divs.
2471 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2472 __isl_take isl_basic_set *bset)
2474 return isl_basic_map_remove_unknown_divs(bset);
2477 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2479 int i;
2481 if (!map)
2482 return NULL;
2483 if (map->n == 0)
2484 return map;
2486 map = isl_map_cow(map);
2487 if (!map)
2488 return NULL;
2490 for (i = 0; i < map->n; ++i) {
2491 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2492 if (!map->p[i])
2493 goto error;
2495 return map;
2496 error:
2497 isl_map_free(map);
2498 return NULL;
2501 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2503 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2506 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2507 __isl_take isl_basic_set *bset,
2508 enum isl_dim_type type, unsigned first, unsigned n)
2510 isl_basic_map *bmap = bset_to_bmap(bset);
2511 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2512 return bset_from_bmap(bmap);
2515 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2516 enum isl_dim_type type, unsigned first, unsigned n)
2518 int i;
2520 if (n == 0)
2521 return map;
2523 map = isl_map_cow(map);
2524 if (!map)
2525 return NULL;
2526 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2528 for (i = 0; i < map->n; ++i) {
2529 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2530 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2531 if (!map->p[i])
2532 goto error;
2534 map = isl_map_drop(map, type, first, n);
2535 return map;
2536 error:
2537 isl_map_free(map);
2538 return NULL;
2541 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2542 enum isl_dim_type type, unsigned first, unsigned n)
2544 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2545 type, first, n));
2548 /* Project out n inputs starting at first using Fourier-Motzkin */
2549 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2550 unsigned first, unsigned n)
2552 return isl_map_remove_dims(map, isl_dim_in, first, n);
2555 static void dump_term(struct isl_basic_map *bmap,
2556 isl_int c, int pos, FILE *out)
2558 const char *name;
2559 unsigned in = isl_basic_map_n_in(bmap);
2560 unsigned dim = in + isl_basic_map_n_out(bmap);
2561 unsigned nparam = isl_basic_map_n_param(bmap);
2562 if (!pos)
2563 isl_int_print(out, c, 0);
2564 else {
2565 if (!isl_int_is_one(c))
2566 isl_int_print(out, c, 0);
2567 if (pos < 1 + nparam) {
2568 name = isl_space_get_dim_name(bmap->dim,
2569 isl_dim_param, pos - 1);
2570 if (name)
2571 fprintf(out, "%s", name);
2572 else
2573 fprintf(out, "p%d", pos - 1);
2574 } else if (pos < 1 + nparam + in)
2575 fprintf(out, "i%d", pos - 1 - nparam);
2576 else if (pos < 1 + nparam + dim)
2577 fprintf(out, "o%d", pos - 1 - nparam - in);
2578 else
2579 fprintf(out, "e%d", pos - 1 - nparam - dim);
2583 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2584 int sign, FILE *out)
2586 int i;
2587 int first;
2588 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2589 isl_int v;
2591 isl_int_init(v);
2592 for (i = 0, first = 1; i < len; ++i) {
2593 if (isl_int_sgn(c[i]) * sign <= 0)
2594 continue;
2595 if (!first)
2596 fprintf(out, " + ");
2597 first = 0;
2598 isl_int_abs(v, c[i]);
2599 dump_term(bmap, v, i, out);
2601 isl_int_clear(v);
2602 if (first)
2603 fprintf(out, "0");
2606 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2607 const char *op, FILE *out, int indent)
2609 int i;
2611 fprintf(out, "%*s", indent, "");
2613 dump_constraint_sign(bmap, c, 1, out);
2614 fprintf(out, " %s ", op);
2615 dump_constraint_sign(bmap, c, -1, out);
2617 fprintf(out, "\n");
2619 for (i = bmap->n_div; i < bmap->extra; ++i) {
2620 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2621 continue;
2622 fprintf(out, "%*s", indent, "");
2623 fprintf(out, "ERROR: unused div coefficient not zero\n");
2624 abort();
2628 static void dump_constraints(struct isl_basic_map *bmap,
2629 isl_int **c, unsigned n,
2630 const char *op, FILE *out, int indent)
2632 int i;
2634 for (i = 0; i < n; ++i)
2635 dump_constraint(bmap, c[i], op, out, indent);
2638 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2640 int j;
2641 int first = 1;
2642 unsigned total = isl_basic_map_total_dim(bmap);
2644 for (j = 0; j < 1 + total; ++j) {
2645 if (isl_int_is_zero(exp[j]))
2646 continue;
2647 if (!first && isl_int_is_pos(exp[j]))
2648 fprintf(out, "+");
2649 dump_term(bmap, exp[j], j, out);
2650 first = 0;
2654 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2656 int i;
2658 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2659 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2661 for (i = 0; i < bmap->n_div; ++i) {
2662 fprintf(out, "%*s", indent, "");
2663 fprintf(out, "e%d = [(", i);
2664 dump_affine(bmap, bmap->div[i]+1, out);
2665 fprintf(out, ")/");
2666 isl_int_print(out, bmap->div[i][0], 0);
2667 fprintf(out, "]\n");
2671 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2672 FILE *out, int indent)
2674 if (!bset) {
2675 fprintf(out, "null basic set\n");
2676 return;
2679 fprintf(out, "%*s", indent, "");
2680 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2681 bset->ref, bset->dim->nparam, bset->dim->n_out,
2682 bset->extra, bset->flags);
2683 dump(bset_to_bmap(bset), out, indent);
2686 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2687 FILE *out, int indent)
2689 if (!bmap) {
2690 fprintf(out, "null basic map\n");
2691 return;
2694 fprintf(out, "%*s", indent, "");
2695 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2696 "flags: %x, n_name: %d\n",
2697 bmap->ref,
2698 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2699 bmap->extra, bmap->flags, bmap->dim->n_id);
2700 dump(bmap, out, indent);
2703 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2705 unsigned total;
2706 if (!bmap)
2707 return -1;
2708 total = isl_basic_map_total_dim(bmap);
2709 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2710 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2711 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2712 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2713 return 0;
2716 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2717 unsigned flags)
2719 if (!space)
2720 return NULL;
2721 if (isl_space_dim(space, isl_dim_in) != 0)
2722 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2723 "set cannot have input dimensions", goto error);
2724 return isl_map_alloc_space(space, n, flags);
2725 error:
2726 isl_space_free(space);
2727 return NULL;
2730 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2731 unsigned nparam, unsigned dim, int n, unsigned flags)
2733 struct isl_set *set;
2734 isl_space *dims;
2736 dims = isl_space_alloc(ctx, nparam, 0, dim);
2737 if (!dims)
2738 return NULL;
2740 set = isl_set_alloc_space(dims, n, flags);
2741 return set;
2744 /* Make sure "map" has room for at least "n" more basic maps.
2746 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2748 int i;
2749 struct isl_map *grown = NULL;
2751 if (!map)
2752 return NULL;
2753 isl_assert(map->ctx, n >= 0, goto error);
2754 if (map->n + n <= map->size)
2755 return map;
2756 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2757 if (!grown)
2758 goto error;
2759 for (i = 0; i < map->n; ++i) {
2760 grown->p[i] = isl_basic_map_copy(map->p[i]);
2761 if (!grown->p[i])
2762 goto error;
2763 grown->n++;
2765 isl_map_free(map);
2766 return grown;
2767 error:
2768 isl_map_free(grown);
2769 isl_map_free(map);
2770 return NULL;
2773 /* Make sure "set" has room for at least "n" more basic sets.
2775 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2777 return set_from_map(isl_map_grow(set_to_map(set), n));
2780 struct isl_set *isl_set_dup(struct isl_set *set)
2782 int i;
2783 struct isl_set *dup;
2785 if (!set)
2786 return NULL;
2788 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2789 if (!dup)
2790 return NULL;
2791 for (i = 0; i < set->n; ++i)
2792 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2793 return dup;
2796 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2798 return isl_map_from_basic_map(bset);
2801 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2803 struct isl_map *map;
2805 if (!bmap)
2806 return NULL;
2808 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2809 return isl_map_add_basic_map(map, bmap);
2812 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2813 __isl_take isl_basic_set *bset)
2815 return set_from_map(isl_map_add_basic_map(set_to_map(set),
2816 bset_to_bmap(bset)));
2819 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2821 return isl_map_free(set);
2824 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2826 int i;
2828 if (!set) {
2829 fprintf(out, "null set\n");
2830 return;
2833 fprintf(out, "%*s", indent, "");
2834 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2835 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2836 set->flags);
2837 for (i = 0; i < set->n; ++i) {
2838 fprintf(out, "%*s", indent, "");
2839 fprintf(out, "basic set %d:\n", i);
2840 isl_basic_set_print_internal(set->p[i], out, indent+4);
2844 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2846 int i;
2848 if (!map) {
2849 fprintf(out, "null map\n");
2850 return;
2853 fprintf(out, "%*s", indent, "");
2854 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2855 "flags: %x, n_name: %d\n",
2856 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2857 map->dim->n_out, map->flags, map->dim->n_id);
2858 for (i = 0; i < map->n; ++i) {
2859 fprintf(out, "%*s", indent, "");
2860 fprintf(out, "basic map %d:\n", i);
2861 isl_basic_map_print_internal(map->p[i], out, indent+4);
2865 struct isl_basic_map *isl_basic_map_intersect_domain(
2866 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2868 struct isl_basic_map *bmap_domain;
2870 if (!bmap || !bset)
2871 goto error;
2873 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2874 bset->dim, isl_dim_param), goto error);
2876 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2877 isl_assert(bset->ctx,
2878 isl_basic_map_compatible_domain(bmap, bset), goto error);
2880 bmap = isl_basic_map_cow(bmap);
2881 if (!bmap)
2882 goto error;
2883 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2884 bset->n_div, bset->n_eq, bset->n_ineq);
2885 bmap_domain = isl_basic_map_from_domain(bset);
2886 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2888 bmap = isl_basic_map_simplify(bmap);
2889 return isl_basic_map_finalize(bmap);
2890 error:
2891 isl_basic_map_free(bmap);
2892 isl_basic_set_free(bset);
2893 return NULL;
2896 struct isl_basic_map *isl_basic_map_intersect_range(
2897 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2899 struct isl_basic_map *bmap_range;
2901 if (!bmap || !bset)
2902 goto error;
2904 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2905 bset->dim, isl_dim_param), goto error);
2907 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2908 isl_assert(bset->ctx,
2909 isl_basic_map_compatible_range(bmap, bset), goto error);
2911 if (isl_basic_set_plain_is_universe(bset)) {
2912 isl_basic_set_free(bset);
2913 return bmap;
2916 bmap = isl_basic_map_cow(bmap);
2917 if (!bmap)
2918 goto error;
2919 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2920 bset->n_div, bset->n_eq, bset->n_ineq);
2921 bmap_range = bset_to_bmap(bset);
2922 bmap = add_constraints(bmap, bmap_range, 0, 0);
2924 bmap = isl_basic_map_simplify(bmap);
2925 return isl_basic_map_finalize(bmap);
2926 error:
2927 isl_basic_map_free(bmap);
2928 isl_basic_set_free(bset);
2929 return NULL;
2932 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2933 __isl_keep isl_vec *vec)
2935 int i;
2936 unsigned total;
2937 isl_int s;
2939 if (!bmap || !vec)
2940 return isl_bool_error;
2942 total = 1 + isl_basic_map_total_dim(bmap);
2943 if (total != vec->size)
2944 return isl_bool_false;
2946 isl_int_init(s);
2948 for (i = 0; i < bmap->n_eq; ++i) {
2949 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2950 if (!isl_int_is_zero(s)) {
2951 isl_int_clear(s);
2952 return isl_bool_false;
2956 for (i = 0; i < bmap->n_ineq; ++i) {
2957 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2958 if (isl_int_is_neg(s)) {
2959 isl_int_clear(s);
2960 return isl_bool_false;
2964 isl_int_clear(s);
2966 return isl_bool_true;
2969 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2970 __isl_keep isl_vec *vec)
2972 return isl_basic_map_contains(bset_to_bmap(bset), vec);
2975 struct isl_basic_map *isl_basic_map_intersect(
2976 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2978 struct isl_vec *sample = NULL;
2980 if (!bmap1 || !bmap2)
2981 goto error;
2983 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2984 bmap2->dim, isl_dim_param), goto error);
2985 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2986 isl_space_dim(bmap1->dim, isl_dim_param) &&
2987 isl_space_dim(bmap2->dim, isl_dim_all) !=
2988 isl_space_dim(bmap2->dim, isl_dim_param))
2989 return isl_basic_map_intersect(bmap2, bmap1);
2991 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2992 isl_space_dim(bmap2->dim, isl_dim_param))
2993 isl_assert(bmap1->ctx,
2994 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2996 if (isl_basic_map_plain_is_empty(bmap1)) {
2997 isl_basic_map_free(bmap2);
2998 return bmap1;
3000 if (isl_basic_map_plain_is_empty(bmap2)) {
3001 isl_basic_map_free(bmap1);
3002 return bmap2;
3005 if (bmap1->sample &&
3006 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3007 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3008 sample = isl_vec_copy(bmap1->sample);
3009 else if (bmap2->sample &&
3010 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3011 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3012 sample = isl_vec_copy(bmap2->sample);
3014 bmap1 = isl_basic_map_cow(bmap1);
3015 if (!bmap1)
3016 goto error;
3017 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3018 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3019 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3021 if (!bmap1)
3022 isl_vec_free(sample);
3023 else if (sample) {
3024 isl_vec_free(bmap1->sample);
3025 bmap1->sample = sample;
3028 bmap1 = isl_basic_map_simplify(bmap1);
3029 return isl_basic_map_finalize(bmap1);
3030 error:
3031 if (sample)
3032 isl_vec_free(sample);
3033 isl_basic_map_free(bmap1);
3034 isl_basic_map_free(bmap2);
3035 return NULL;
3038 struct isl_basic_set *isl_basic_set_intersect(
3039 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3041 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3042 bset_to_bmap(bset2)));
3045 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3046 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3048 return isl_basic_set_intersect(bset1, bset2);
3051 /* Special case of isl_map_intersect, where both map1 and map2
3052 * are convex, without any divs and such that either map1 or map2
3053 * contains a single constraint. This constraint is then simply
3054 * added to the other map.
3056 static __isl_give isl_map *map_intersect_add_constraint(
3057 __isl_take isl_map *map1, __isl_take isl_map *map2)
3059 isl_assert(map1->ctx, map1->n == 1, goto error);
3060 isl_assert(map2->ctx, map1->n == 1, goto error);
3061 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3062 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3064 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3065 return isl_map_intersect(map2, map1);
3067 isl_assert(map2->ctx,
3068 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3070 map1 = isl_map_cow(map1);
3071 if (!map1)
3072 goto error;
3073 if (isl_map_plain_is_empty(map1)) {
3074 isl_map_free(map2);
3075 return map1;
3077 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3078 if (map2->p[0]->n_eq == 1)
3079 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3080 else
3081 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3082 map2->p[0]->ineq[0]);
3084 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3085 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3086 if (!map1->p[0])
3087 goto error;
3089 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3090 isl_basic_map_free(map1->p[0]);
3091 map1->n = 0;
3094 isl_map_free(map2);
3096 return map1;
3097 error:
3098 isl_map_free(map1);
3099 isl_map_free(map2);
3100 return NULL;
3103 /* map2 may be either a parameter domain or a map living in the same
3104 * space as map1.
3106 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3107 __isl_take isl_map *map2)
3109 unsigned flags = 0;
3110 isl_map *result;
3111 int i, j;
3113 if (!map1 || !map2)
3114 goto error;
3116 if ((isl_map_plain_is_empty(map1) ||
3117 isl_map_plain_is_universe(map2)) &&
3118 isl_space_is_equal(map1->dim, map2->dim)) {
3119 isl_map_free(map2);
3120 return map1;
3122 if ((isl_map_plain_is_empty(map2) ||
3123 isl_map_plain_is_universe(map1)) &&
3124 isl_space_is_equal(map1->dim, map2->dim)) {
3125 isl_map_free(map1);
3126 return map2;
3129 if (map1->n == 1 && map2->n == 1 &&
3130 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3131 isl_space_is_equal(map1->dim, map2->dim) &&
3132 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3133 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3134 return map_intersect_add_constraint(map1, map2);
3136 if (isl_space_dim(map2->dim, isl_dim_all) !=
3137 isl_space_dim(map2->dim, isl_dim_param))
3138 isl_assert(map1->ctx,
3139 isl_space_is_equal(map1->dim, map2->dim), goto error);
3141 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3142 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3143 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3145 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3146 map1->n * map2->n, flags);
3147 if (!result)
3148 goto error;
3149 for (i = 0; i < map1->n; ++i)
3150 for (j = 0; j < map2->n; ++j) {
3151 struct isl_basic_map *part;
3152 part = isl_basic_map_intersect(
3153 isl_basic_map_copy(map1->p[i]),
3154 isl_basic_map_copy(map2->p[j]));
3155 if (isl_basic_map_is_empty(part) < 0)
3156 part = isl_basic_map_free(part);
3157 result = isl_map_add_basic_map(result, part);
3158 if (!result)
3159 goto error;
3161 isl_map_free(map1);
3162 isl_map_free(map2);
3163 return result;
3164 error:
3165 isl_map_free(map1);
3166 isl_map_free(map2);
3167 return NULL;
3170 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3171 __isl_take isl_map *map2)
3173 if (!map1 || !map2)
3174 goto error;
3175 if (!isl_space_is_equal(map1->dim, map2->dim))
3176 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3177 "spaces don't match", goto error);
3178 return map_intersect_internal(map1, map2);
3179 error:
3180 isl_map_free(map1);
3181 isl_map_free(map2);
3182 return NULL;
3185 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3186 __isl_take isl_map *map2)
3188 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3191 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3193 return set_from_map(isl_map_intersect(set_to_map(set1),
3194 set_to_map(set2)));
3197 /* map_intersect_internal accepts intersections
3198 * with parameter domains, so we can just call that function.
3200 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3201 __isl_take isl_set *params)
3203 return map_intersect_internal(map, params);
3206 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3207 __isl_take isl_map *map2)
3209 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3212 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3213 __isl_take isl_set *params)
3215 return isl_map_intersect_params(set, params);
3218 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3220 isl_space *space;
3221 unsigned pos, n1, n2;
3223 if (!bmap)
3224 return NULL;
3225 bmap = isl_basic_map_cow(bmap);
3226 if (!bmap)
3227 return NULL;
3228 space = isl_space_reverse(isl_space_copy(bmap->dim));
3229 pos = isl_basic_map_offset(bmap, isl_dim_in);
3230 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3231 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3232 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3233 return isl_basic_map_reset_space(bmap, space);
3236 static __isl_give isl_basic_map *basic_map_space_reset(
3237 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3239 isl_space *space;
3241 if (!bmap)
3242 return NULL;
3243 if (!isl_space_is_named_or_nested(bmap->dim, type))
3244 return bmap;
3246 space = isl_basic_map_get_space(bmap);
3247 space = isl_space_reset(space, type);
3248 bmap = isl_basic_map_reset_space(bmap, space);
3249 return bmap;
3252 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3253 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3254 unsigned pos, unsigned n)
3256 isl_space *res_dim;
3257 struct isl_basic_map *res;
3258 struct isl_dim_map *dim_map;
3259 unsigned total, off;
3260 enum isl_dim_type t;
3262 if (n == 0)
3263 return basic_map_space_reset(bmap, type);
3265 if (!bmap)
3266 return NULL;
3268 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3270 total = isl_basic_map_total_dim(bmap) + n;
3271 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3272 off = 0;
3273 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3274 if (t != type) {
3275 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3276 } else {
3277 unsigned size = isl_basic_map_dim(bmap, t);
3278 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3279 0, pos, off);
3280 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3281 pos, size - pos, off + pos + n);
3283 off += isl_space_dim(res_dim, t);
3285 isl_dim_map_div(dim_map, bmap, off);
3287 res = isl_basic_map_alloc_space(res_dim,
3288 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3289 if (isl_basic_map_is_rational(bmap))
3290 res = isl_basic_map_set_rational(res);
3291 if (isl_basic_map_plain_is_empty(bmap)) {
3292 isl_basic_map_free(bmap);
3293 free(dim_map);
3294 return isl_basic_map_set_to_empty(res);
3296 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3297 return isl_basic_map_finalize(res);
3300 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3301 __isl_take isl_basic_set *bset,
3302 enum isl_dim_type type, unsigned pos, unsigned n)
3304 return isl_basic_map_insert_dims(bset, type, pos, n);
3307 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3308 enum isl_dim_type type, unsigned n)
3310 if (!bmap)
3311 return NULL;
3312 return isl_basic_map_insert_dims(bmap, type,
3313 isl_basic_map_dim(bmap, type), n);
3316 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3317 enum isl_dim_type type, unsigned n)
3319 if (!bset)
3320 return NULL;
3321 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3322 return isl_basic_map_add_dims(bset, type, n);
3323 error:
3324 isl_basic_set_free(bset);
3325 return NULL;
3328 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3329 enum isl_dim_type type)
3331 isl_space *space;
3333 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3334 return map;
3336 space = isl_map_get_space(map);
3337 space = isl_space_reset(space, type);
3338 map = isl_map_reset_space(map, space);
3339 return map;
3342 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3343 enum isl_dim_type type, unsigned pos, unsigned n)
3345 int i;
3347 if (n == 0)
3348 return map_space_reset(map, type);
3350 map = isl_map_cow(map);
3351 if (!map)
3352 return NULL;
3354 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3355 if (!map->dim)
3356 goto error;
3358 for (i = 0; i < map->n; ++i) {
3359 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3360 if (!map->p[i])
3361 goto error;
3364 return map;
3365 error:
3366 isl_map_free(map);
3367 return NULL;
3370 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3371 enum isl_dim_type type, unsigned pos, unsigned n)
3373 return isl_map_insert_dims(set, type, pos, n);
3376 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3377 enum isl_dim_type type, unsigned n)
3379 if (!map)
3380 return NULL;
3381 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3384 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3385 enum isl_dim_type type, unsigned n)
3387 if (!set)
3388 return NULL;
3389 isl_assert(set->ctx, type != isl_dim_in, goto error);
3390 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3391 error:
3392 isl_set_free(set);
3393 return NULL;
3396 __isl_give isl_basic_map *isl_basic_map_move_dims(
3397 __isl_take isl_basic_map *bmap,
3398 enum isl_dim_type dst_type, unsigned dst_pos,
3399 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3401 struct isl_dim_map *dim_map;
3402 struct isl_basic_map *res;
3403 enum isl_dim_type t;
3404 unsigned total, off;
3406 if (!bmap)
3407 return NULL;
3408 if (n == 0)
3409 return bmap;
3411 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3412 goto error);
3414 if (dst_type == src_type && dst_pos == src_pos)
3415 return bmap;
3417 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3419 if (pos(bmap->dim, dst_type) + dst_pos ==
3420 pos(bmap->dim, src_type) + src_pos +
3421 ((src_type < dst_type) ? n : 0)) {
3422 bmap = isl_basic_map_cow(bmap);
3423 if (!bmap)
3424 return NULL;
3426 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3427 src_type, src_pos, n);
3428 if (!bmap->dim)
3429 goto error;
3431 bmap = isl_basic_map_finalize(bmap);
3433 return bmap;
3436 total = isl_basic_map_total_dim(bmap);
3437 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3439 off = 0;
3440 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3441 unsigned size = isl_space_dim(bmap->dim, t);
3442 if (t == dst_type) {
3443 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3444 0, dst_pos, off);
3445 off += dst_pos;
3446 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3447 src_pos, n, off);
3448 off += n;
3449 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3450 dst_pos, size - dst_pos, off);
3451 off += size - dst_pos;
3452 } else if (t == src_type) {
3453 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3454 0, src_pos, off);
3455 off += src_pos;
3456 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3457 src_pos + n, size - src_pos - n, off);
3458 off += size - src_pos - n;
3459 } else {
3460 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3461 off += size;
3464 isl_dim_map_div(dim_map, bmap, off);
3466 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3467 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3468 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3469 if (!bmap)
3470 goto error;
3472 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3473 src_type, src_pos, n);
3474 if (!bmap->dim)
3475 goto error;
3477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3478 bmap = isl_basic_map_gauss(bmap, NULL);
3479 bmap = isl_basic_map_finalize(bmap);
3481 return bmap;
3482 error:
3483 isl_basic_map_free(bmap);
3484 return NULL;
3487 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3488 enum isl_dim_type dst_type, unsigned dst_pos,
3489 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3491 isl_basic_map *bmap = bset_to_bmap(bset);
3492 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3493 src_type, src_pos, n);
3494 return bset_from_bmap(bmap);
3497 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3498 enum isl_dim_type dst_type, unsigned dst_pos,
3499 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3501 if (!set)
3502 return NULL;
3503 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3504 return set_from_map(isl_map_move_dims(set_to_map(set),
3505 dst_type, dst_pos, src_type, src_pos, n));
3506 error:
3507 isl_set_free(set);
3508 return NULL;
3511 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3512 enum isl_dim_type dst_type, unsigned dst_pos,
3513 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3515 int i;
3517 if (!map)
3518 return NULL;
3519 if (n == 0)
3520 return map;
3522 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3523 goto error);
3525 if (dst_type == src_type && dst_pos == src_pos)
3526 return map;
3528 isl_assert(map->ctx, dst_type != src_type, goto error);
3530 map = isl_map_cow(map);
3531 if (!map)
3532 return NULL;
3534 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3535 if (!map->dim)
3536 goto error;
3538 for (i = 0; i < map->n; ++i) {
3539 map->p[i] = isl_basic_map_move_dims(map->p[i],
3540 dst_type, dst_pos,
3541 src_type, src_pos, n);
3542 if (!map->p[i])
3543 goto error;
3546 return map;
3547 error:
3548 isl_map_free(map);
3549 return NULL;
3552 /* Move the specified dimensions to the last columns right before
3553 * the divs. Don't change the dimension specification of bmap.
3554 * That's the responsibility of the caller.
3556 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3557 enum isl_dim_type type, unsigned first, unsigned n)
3559 struct isl_dim_map *dim_map;
3560 struct isl_basic_map *res;
3561 enum isl_dim_type t;
3562 unsigned total, off;
3564 if (!bmap)
3565 return NULL;
3566 if (pos(bmap->dim, type) + first + n ==
3567 1 + isl_space_dim(bmap->dim, isl_dim_all))
3568 return bmap;
3570 total = isl_basic_map_total_dim(bmap);
3571 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3573 off = 0;
3574 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3575 unsigned size = isl_space_dim(bmap->dim, t);
3576 if (t == type) {
3577 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3578 0, first, off);
3579 off += first;
3580 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3581 first, n, total - bmap->n_div - n);
3582 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3583 first + n, size - (first + n), off);
3584 off += size - (first + n);
3585 } else {
3586 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3587 off += size;
3590 isl_dim_map_div(dim_map, bmap, off + n);
3592 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3593 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3594 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3595 return res;
3598 /* Insert "n" rows in the divs of "bmap".
3600 * The number of columns is not changed, which means that the last
3601 * dimensions of "bmap" are being reintepreted as the new divs.
3602 * The space of "bmap" is not adjusted, however, which means
3603 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3604 * from the space of "bmap" is the responsibility of the caller.
3606 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3607 int n)
3609 int i;
3610 size_t row_size;
3611 isl_int **new_div;
3612 isl_int *old;
3614 bmap = isl_basic_map_cow(bmap);
3615 if (!bmap)
3616 return NULL;
3618 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3619 old = bmap->block2.data;
3620 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3621 (bmap->extra + n) * (1 + row_size));
3622 if (!bmap->block2.data)
3623 return isl_basic_map_free(bmap);
3624 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3625 if (!new_div)
3626 return isl_basic_map_free(bmap);
3627 for (i = 0; i < n; ++i) {
3628 new_div[i] = bmap->block2.data +
3629 (bmap->extra + i) * (1 + row_size);
3630 isl_seq_clr(new_div[i], 1 + row_size);
3632 for (i = 0; i < bmap->extra; ++i)
3633 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3634 free(bmap->div);
3635 bmap->div = new_div;
3636 bmap->n_div += n;
3637 bmap->extra += n;
3639 return bmap;
3642 /* Drop constraints from "bmap" that only involve the variables
3643 * of "type" in the range [first, first + n] that are not related
3644 * to any of the variables outside that interval.
3645 * These constraints cannot influence the values for the variables
3646 * outside the interval, except in case they cause "bmap" to be empty.
3647 * Only drop the constraints if "bmap" is known to be non-empty.
3649 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3650 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3651 unsigned first, unsigned n)
3653 int i;
3654 int *groups;
3655 unsigned dim, n_div;
3656 isl_bool non_empty;
3658 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3659 if (non_empty < 0)
3660 return isl_basic_map_free(bmap);
3661 if (!non_empty)
3662 return bmap;
3664 dim = isl_basic_map_dim(bmap, isl_dim_all);
3665 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3666 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3667 if (!groups)
3668 return isl_basic_map_free(bmap);
3669 first += isl_basic_map_offset(bmap, type) - 1;
3670 for (i = 0; i < first; ++i)
3671 groups[i] = -1;
3672 for (i = first + n; i < dim - n_div; ++i)
3673 groups[i] = -1;
3675 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3677 return bmap;
3680 /* Turn the n dimensions of type type, starting at first
3681 * into existentially quantified variables.
3683 * If a subset of the projected out variables are unrelated
3684 * to any of the variables that remain, then the constraints
3685 * involving this subset are simply dropped first.
3687 __isl_give isl_basic_map *isl_basic_map_project_out(
3688 __isl_take isl_basic_map *bmap,
3689 enum isl_dim_type type, unsigned first, unsigned n)
3691 if (n == 0)
3692 return basic_map_space_reset(bmap, type);
3693 if (type == isl_dim_div)
3694 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3695 "cannot project out existentially quantified variables",
3696 return isl_basic_map_free(bmap));
3698 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3699 if (!bmap)
3700 return NULL;
3702 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3703 return isl_basic_map_remove_dims(bmap, type, first, n);
3705 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3706 goto error);
3708 bmap = move_last(bmap, type, first, n);
3709 bmap = isl_basic_map_cow(bmap);
3710 bmap = insert_div_rows(bmap, n);
3711 if (!bmap)
3712 return NULL;
3714 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3715 if (!bmap->dim)
3716 goto error;
3717 bmap = isl_basic_map_simplify(bmap);
3718 bmap = isl_basic_map_drop_redundant_divs(bmap);
3719 return isl_basic_map_finalize(bmap);
3720 error:
3721 isl_basic_map_free(bmap);
3722 return NULL;
3725 /* Turn the n dimensions of type type, starting at first
3726 * into existentially quantified variables.
3728 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3729 enum isl_dim_type type, unsigned first, unsigned n)
3731 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3732 type, first, n));
3735 /* Turn the n dimensions of type type, starting at first
3736 * into existentially quantified variables.
3738 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3739 enum isl_dim_type type, unsigned first, unsigned n)
3741 int i;
3743 if (!map)
3744 return NULL;
3746 if (n == 0)
3747 return map_space_reset(map, type);
3749 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3751 map = isl_map_cow(map);
3752 if (!map)
3753 return NULL;
3755 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3756 if (!map->dim)
3757 goto error;
3759 for (i = 0; i < map->n; ++i) {
3760 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3761 if (!map->p[i])
3762 goto error;
3765 return map;
3766 error:
3767 isl_map_free(map);
3768 return NULL;
3771 /* Turn the n dimensions of type type, starting at first
3772 * into existentially quantified variables.
3774 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3775 enum isl_dim_type type, unsigned first, unsigned n)
3777 return set_from_map(isl_map_project_out(set_to_map(set),
3778 type, first, n));
3781 /* Return a map that projects the elements in "set" onto their
3782 * "n" set dimensions starting at "first".
3783 * "type" should be equal to isl_dim_set.
3785 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3786 enum isl_dim_type type, unsigned first, unsigned n)
3788 int i;
3789 int dim;
3790 isl_map *map;
3792 if (!set)
3793 return NULL;
3794 if (type != isl_dim_set)
3795 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3796 "only set dimensions can be projected out", goto error);
3797 dim = isl_set_dim(set, isl_dim_set);
3798 if (first + n > dim || first + n < first)
3799 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3800 "index out of bounds", goto error);
3802 map = isl_map_from_domain(set);
3803 map = isl_map_add_dims(map, isl_dim_out, n);
3804 for (i = 0; i < n; ++i)
3805 map = isl_map_equate(map, isl_dim_in, first + i,
3806 isl_dim_out, i);
3807 return map;
3808 error:
3809 isl_set_free(set);
3810 return NULL;
3813 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3815 int i, j;
3817 for (i = 0; i < n; ++i) {
3818 j = isl_basic_map_alloc_div(bmap);
3819 if (j < 0)
3820 goto error;
3821 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3823 return bmap;
3824 error:
3825 isl_basic_map_free(bmap);
3826 return NULL;
3829 struct isl_basic_map *isl_basic_map_apply_range(
3830 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3832 isl_space *dim_result = NULL;
3833 struct isl_basic_map *bmap;
3834 unsigned n_in, n_out, n, nparam, total, pos;
3835 struct isl_dim_map *dim_map1, *dim_map2;
3837 if (!bmap1 || !bmap2)
3838 goto error;
3839 if (!isl_space_match(bmap1->dim, isl_dim_param,
3840 bmap2->dim, isl_dim_param))
3841 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3842 "parameters don't match", goto error);
3843 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3844 bmap2->dim, isl_dim_in))
3845 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3846 "spaces don't match", goto error);
3848 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3849 isl_space_copy(bmap2->dim));
3851 n_in = isl_basic_map_n_in(bmap1);
3852 n_out = isl_basic_map_n_out(bmap2);
3853 n = isl_basic_map_n_out(bmap1);
3854 nparam = isl_basic_map_n_param(bmap1);
3856 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3857 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3858 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3859 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3860 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3861 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3862 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3863 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3864 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3865 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3866 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3868 bmap = isl_basic_map_alloc_space(dim_result,
3869 bmap1->n_div + bmap2->n_div + n,
3870 bmap1->n_eq + bmap2->n_eq,
3871 bmap1->n_ineq + bmap2->n_ineq);
3872 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3873 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3874 bmap = add_divs(bmap, n);
3875 bmap = isl_basic_map_simplify(bmap);
3876 bmap = isl_basic_map_drop_redundant_divs(bmap);
3877 return isl_basic_map_finalize(bmap);
3878 error:
3879 isl_basic_map_free(bmap1);
3880 isl_basic_map_free(bmap2);
3881 return NULL;
3884 struct isl_basic_set *isl_basic_set_apply(
3885 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3887 if (!bset || !bmap)
3888 goto error;
3890 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3891 goto error);
3893 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
3894 bmap));
3895 error:
3896 isl_basic_set_free(bset);
3897 isl_basic_map_free(bmap);
3898 return NULL;
3901 struct isl_basic_map *isl_basic_map_apply_domain(
3902 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3904 if (!bmap1 || !bmap2)
3905 goto error;
3907 if (!isl_space_match(bmap1->dim, isl_dim_param,
3908 bmap2->dim, isl_dim_param))
3909 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3910 "parameters don't match", goto error);
3911 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
3912 bmap2->dim, isl_dim_in))
3913 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3914 "spaces don't match", goto error);
3916 bmap1 = isl_basic_map_reverse(bmap1);
3917 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3918 return isl_basic_map_reverse(bmap1);
3919 error:
3920 isl_basic_map_free(bmap1);
3921 isl_basic_map_free(bmap2);
3922 return NULL;
3925 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3926 * A \cap B -> f(A) + f(B)
3928 struct isl_basic_map *isl_basic_map_sum(
3929 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3931 unsigned n_in, n_out, nparam, total, pos;
3932 struct isl_basic_map *bmap = NULL;
3933 struct isl_dim_map *dim_map1, *dim_map2;
3934 int i;
3936 if (!bmap1 || !bmap2)
3937 goto error;
3939 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3940 goto error);
3942 nparam = isl_basic_map_n_param(bmap1);
3943 n_in = isl_basic_map_n_in(bmap1);
3944 n_out = isl_basic_map_n_out(bmap1);
3946 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3947 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3948 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3949 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3950 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3951 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3952 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3953 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3954 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3955 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3956 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3958 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3959 bmap1->n_div + bmap2->n_div + 2 * n_out,
3960 bmap1->n_eq + bmap2->n_eq + n_out,
3961 bmap1->n_ineq + bmap2->n_ineq);
3962 for (i = 0; i < n_out; ++i) {
3963 int j = isl_basic_map_alloc_equality(bmap);
3964 if (j < 0)
3965 goto error;
3966 isl_seq_clr(bmap->eq[j], 1+total);
3967 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3968 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3969 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3971 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3972 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3973 bmap = add_divs(bmap, 2 * n_out);
3975 bmap = isl_basic_map_simplify(bmap);
3976 return isl_basic_map_finalize(bmap);
3977 error:
3978 isl_basic_map_free(bmap);
3979 isl_basic_map_free(bmap1);
3980 isl_basic_map_free(bmap2);
3981 return NULL;
3984 /* Given two maps A -> f(A) and B -> g(B), construct a map
3985 * A \cap B -> f(A) + f(B)
3987 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3989 struct isl_map *result;
3990 int i, j;
3992 if (!map1 || !map2)
3993 goto error;
3995 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3997 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3998 map1->n * map2->n, 0);
3999 if (!result)
4000 goto error;
4001 for (i = 0; i < map1->n; ++i)
4002 for (j = 0; j < map2->n; ++j) {
4003 struct isl_basic_map *part;
4004 part = isl_basic_map_sum(
4005 isl_basic_map_copy(map1->p[i]),
4006 isl_basic_map_copy(map2->p[j]));
4007 if (isl_basic_map_is_empty(part))
4008 isl_basic_map_free(part);
4009 else
4010 result = isl_map_add_basic_map(result, part);
4011 if (!result)
4012 goto error;
4014 isl_map_free(map1);
4015 isl_map_free(map2);
4016 return result;
4017 error:
4018 isl_map_free(map1);
4019 isl_map_free(map2);
4020 return NULL;
4023 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4024 __isl_take isl_set *set2)
4026 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4029 /* Given a basic map A -> f(A), construct A -> -f(A).
4031 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4033 int i, j;
4034 unsigned off, n;
4036 bmap = isl_basic_map_cow(bmap);
4037 if (!bmap)
4038 return NULL;
4040 n = isl_basic_map_dim(bmap, isl_dim_out);
4041 off = isl_basic_map_offset(bmap, isl_dim_out);
4042 for (i = 0; i < bmap->n_eq; ++i)
4043 for (j = 0; j < n; ++j)
4044 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4045 for (i = 0; i < bmap->n_ineq; ++i)
4046 for (j = 0; j < n; ++j)
4047 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4048 for (i = 0; i < bmap->n_div; ++i)
4049 for (j = 0; j < n; ++j)
4050 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4051 bmap = isl_basic_map_gauss(bmap, NULL);
4052 return isl_basic_map_finalize(bmap);
4055 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4057 return isl_basic_map_neg(bset);
4060 /* Given a map A -> f(A), construct A -> -f(A).
4062 struct isl_map *isl_map_neg(struct isl_map *map)
4064 int i;
4066 map = isl_map_cow(map);
4067 if (!map)
4068 return NULL;
4070 for (i = 0; i < map->n; ++i) {
4071 map->p[i] = isl_basic_map_neg(map->p[i]);
4072 if (!map->p[i])
4073 goto error;
4076 return map;
4077 error:
4078 isl_map_free(map);
4079 return NULL;
4082 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4084 return set_from_map(isl_map_neg(set_to_map(set)));
4087 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4088 * A -> floor(f(A)/d).
4090 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4091 isl_int d)
4093 unsigned n_in, n_out, nparam, total, pos;
4094 struct isl_basic_map *result = NULL;
4095 struct isl_dim_map *dim_map;
4096 int i;
4098 if (!bmap)
4099 return NULL;
4101 nparam = isl_basic_map_n_param(bmap);
4102 n_in = isl_basic_map_n_in(bmap);
4103 n_out = isl_basic_map_n_out(bmap);
4105 total = nparam + n_in + n_out + bmap->n_div + n_out;
4106 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4107 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4108 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4109 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4110 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4112 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4113 bmap->n_div + n_out,
4114 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4115 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4116 result = add_divs(result, n_out);
4117 for (i = 0; i < n_out; ++i) {
4118 int j;
4119 j = isl_basic_map_alloc_inequality(result);
4120 if (j < 0)
4121 goto error;
4122 isl_seq_clr(result->ineq[j], 1+total);
4123 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4124 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4125 j = isl_basic_map_alloc_inequality(result);
4126 if (j < 0)
4127 goto error;
4128 isl_seq_clr(result->ineq[j], 1+total);
4129 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4130 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4131 isl_int_sub_ui(result->ineq[j][0], d, 1);
4134 result = isl_basic_map_simplify(result);
4135 return isl_basic_map_finalize(result);
4136 error:
4137 isl_basic_map_free(result);
4138 return NULL;
4141 /* Given a map A -> f(A) and an integer d, construct a map
4142 * A -> floor(f(A)/d).
4144 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4146 int i;
4148 map = isl_map_cow(map);
4149 if (!map)
4150 return NULL;
4152 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4153 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4154 for (i = 0; i < map->n; ++i) {
4155 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4156 if (!map->p[i])
4157 goto error;
4160 return map;
4161 error:
4162 isl_map_free(map);
4163 return NULL;
4166 /* Given a map A -> f(A) and an integer d, construct a map
4167 * A -> floor(f(A)/d).
4169 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4170 __isl_take isl_val *d)
4172 if (!map || !d)
4173 goto error;
4174 if (!isl_val_is_int(d))
4175 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4176 "expecting integer denominator", goto error);
4177 map = isl_map_floordiv(map, d->n);
4178 isl_val_free(d);
4179 return map;
4180 error:
4181 isl_map_free(map);
4182 isl_val_free(d);
4183 return NULL;
4186 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4188 int i;
4189 unsigned nparam;
4190 unsigned n_in;
4192 i = isl_basic_map_alloc_equality(bmap);
4193 if (i < 0)
4194 goto error;
4195 nparam = isl_basic_map_n_param(bmap);
4196 n_in = isl_basic_map_n_in(bmap);
4197 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4198 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4199 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4200 return isl_basic_map_finalize(bmap);
4201 error:
4202 isl_basic_map_free(bmap);
4203 return NULL;
4206 /* Add a constraint to "bmap" expressing i_pos < o_pos
4208 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4210 int i;
4211 unsigned nparam;
4212 unsigned n_in;
4214 i = isl_basic_map_alloc_inequality(bmap);
4215 if (i < 0)
4216 goto error;
4217 nparam = isl_basic_map_n_param(bmap);
4218 n_in = isl_basic_map_n_in(bmap);
4219 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4220 isl_int_set_si(bmap->ineq[i][0], -1);
4221 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4222 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4223 return isl_basic_map_finalize(bmap);
4224 error:
4225 isl_basic_map_free(bmap);
4226 return NULL;
4229 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4231 static __isl_give isl_basic_map *var_less_or_equal(
4232 __isl_take isl_basic_map *bmap, unsigned pos)
4234 int i;
4235 unsigned nparam;
4236 unsigned n_in;
4238 i = isl_basic_map_alloc_inequality(bmap);
4239 if (i < 0)
4240 goto error;
4241 nparam = isl_basic_map_n_param(bmap);
4242 n_in = isl_basic_map_n_in(bmap);
4243 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4244 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4245 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4246 return isl_basic_map_finalize(bmap);
4247 error:
4248 isl_basic_map_free(bmap);
4249 return NULL;
4252 /* Add a constraint to "bmap" expressing i_pos > o_pos
4254 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4256 int i;
4257 unsigned nparam;
4258 unsigned n_in;
4260 i = isl_basic_map_alloc_inequality(bmap);
4261 if (i < 0)
4262 goto error;
4263 nparam = isl_basic_map_n_param(bmap);
4264 n_in = isl_basic_map_n_in(bmap);
4265 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4266 isl_int_set_si(bmap->ineq[i][0], -1);
4267 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4268 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4269 return isl_basic_map_finalize(bmap);
4270 error:
4271 isl_basic_map_free(bmap);
4272 return NULL;
4275 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4277 static __isl_give isl_basic_map *var_more_or_equal(
4278 __isl_take isl_basic_map *bmap, unsigned pos)
4280 int i;
4281 unsigned nparam;
4282 unsigned n_in;
4284 i = isl_basic_map_alloc_inequality(bmap);
4285 if (i < 0)
4286 goto error;
4287 nparam = isl_basic_map_n_param(bmap);
4288 n_in = isl_basic_map_n_in(bmap);
4289 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4290 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4291 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4292 return isl_basic_map_finalize(bmap);
4293 error:
4294 isl_basic_map_free(bmap);
4295 return NULL;
4298 __isl_give isl_basic_map *isl_basic_map_equal(
4299 __isl_take isl_space *dim, unsigned n_equal)
4301 int i;
4302 struct isl_basic_map *bmap;
4303 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4304 if (!bmap)
4305 return NULL;
4306 for (i = 0; i < n_equal && bmap; ++i)
4307 bmap = var_equal(bmap, i);
4308 return isl_basic_map_finalize(bmap);
4311 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4313 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4314 unsigned pos)
4316 int i;
4317 struct isl_basic_map *bmap;
4318 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4319 if (!bmap)
4320 return NULL;
4321 for (i = 0; i < pos && bmap; ++i)
4322 bmap = var_equal(bmap, i);
4323 if (bmap)
4324 bmap = var_less(bmap, pos);
4325 return isl_basic_map_finalize(bmap);
4328 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4330 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4331 __isl_take isl_space *dim, unsigned pos)
4333 int i;
4334 isl_basic_map *bmap;
4336 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4337 for (i = 0; i < pos; ++i)
4338 bmap = var_equal(bmap, i);
4339 bmap = var_less_or_equal(bmap, pos);
4340 return isl_basic_map_finalize(bmap);
4343 /* Return a relation on "dim" expressing i_pos > o_pos
4345 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4346 unsigned pos)
4348 int i;
4349 struct isl_basic_map *bmap;
4350 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4351 if (!bmap)
4352 return NULL;
4353 for (i = 0; i < pos && bmap; ++i)
4354 bmap = var_equal(bmap, i);
4355 if (bmap)
4356 bmap = var_more(bmap, pos);
4357 return isl_basic_map_finalize(bmap);
4360 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4362 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4363 __isl_take isl_space *dim, unsigned pos)
4365 int i;
4366 isl_basic_map *bmap;
4368 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4369 for (i = 0; i < pos; ++i)
4370 bmap = var_equal(bmap, i);
4371 bmap = var_more_or_equal(bmap, pos);
4372 return isl_basic_map_finalize(bmap);
4375 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4376 unsigned n, int equal)
4378 struct isl_map *map;
4379 int i;
4381 if (n == 0 && equal)
4382 return isl_map_universe(dims);
4384 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4386 for (i = 0; i + 1 < n; ++i)
4387 map = isl_map_add_basic_map(map,
4388 isl_basic_map_less_at(isl_space_copy(dims), i));
4389 if (n > 0) {
4390 if (equal)
4391 map = isl_map_add_basic_map(map,
4392 isl_basic_map_less_or_equal_at(dims, n - 1));
4393 else
4394 map = isl_map_add_basic_map(map,
4395 isl_basic_map_less_at(dims, n - 1));
4396 } else
4397 isl_space_free(dims);
4399 return map;
4402 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4404 if (!dims)
4405 return NULL;
4406 return map_lex_lte_first(dims, dims->n_out, equal);
4409 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4411 return map_lex_lte_first(dim, n, 0);
4414 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4416 return map_lex_lte_first(dim, n, 1);
4419 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4421 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4424 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4426 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4429 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4430 unsigned n, int equal)
4432 struct isl_map *map;
4433 int i;
4435 if (n == 0 && equal)
4436 return isl_map_universe(dims);
4438 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4440 for (i = 0; i + 1 < n; ++i)
4441 map = isl_map_add_basic_map(map,
4442 isl_basic_map_more_at(isl_space_copy(dims), i));
4443 if (n > 0) {
4444 if (equal)
4445 map = isl_map_add_basic_map(map,
4446 isl_basic_map_more_or_equal_at(dims, n - 1));
4447 else
4448 map = isl_map_add_basic_map(map,
4449 isl_basic_map_more_at(dims, n - 1));
4450 } else
4451 isl_space_free(dims);
4453 return map;
4456 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4458 if (!dims)
4459 return NULL;
4460 return map_lex_gte_first(dims, dims->n_out, equal);
4463 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4465 return map_lex_gte_first(dim, n, 0);
4468 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4470 return map_lex_gte_first(dim, n, 1);
4473 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4475 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4478 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4480 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4483 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4484 __isl_take isl_set *set2)
4486 isl_map *map;
4487 map = isl_map_lex_le(isl_set_get_space(set1));
4488 map = isl_map_intersect_domain(map, set1);
4489 map = isl_map_intersect_range(map, set2);
4490 return map;
4493 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4494 __isl_take isl_set *set2)
4496 isl_map *map;
4497 map = isl_map_lex_lt(isl_set_get_space(set1));
4498 map = isl_map_intersect_domain(map, set1);
4499 map = isl_map_intersect_range(map, set2);
4500 return map;
4503 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4504 __isl_take isl_set *set2)
4506 isl_map *map;
4507 map = isl_map_lex_ge(isl_set_get_space(set1));
4508 map = isl_map_intersect_domain(map, set1);
4509 map = isl_map_intersect_range(map, set2);
4510 return map;
4513 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4514 __isl_take isl_set *set2)
4516 isl_map *map;
4517 map = isl_map_lex_gt(isl_set_get_space(set1));
4518 map = isl_map_intersect_domain(map, set1);
4519 map = isl_map_intersect_range(map, set2);
4520 return map;
4523 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4524 __isl_take isl_map *map2)
4526 isl_map *map;
4527 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4528 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4529 map = isl_map_apply_range(map, isl_map_reverse(map2));
4530 return map;
4533 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4534 __isl_take isl_map *map2)
4536 isl_map *map;
4537 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4538 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4539 map = isl_map_apply_range(map, isl_map_reverse(map2));
4540 return map;
4543 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4544 __isl_take isl_map *map2)
4546 isl_map *map;
4547 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4548 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4549 map = isl_map_apply_range(map, isl_map_reverse(map2));
4550 return map;
4553 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4554 __isl_take isl_map *map2)
4556 isl_map *map;
4557 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4558 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4559 map = isl_map_apply_range(map, isl_map_reverse(map2));
4560 return map;
4563 static __isl_give isl_basic_map *basic_map_from_basic_set(
4564 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4566 struct isl_basic_map *bmap;
4568 bset = isl_basic_set_cow(bset);
4569 if (!bset || !dim)
4570 goto error;
4572 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4573 goto error);
4574 isl_space_free(bset->dim);
4575 bmap = bset_to_bmap(bset);
4576 bmap->dim = dim;
4577 return isl_basic_map_finalize(bmap);
4578 error:
4579 isl_basic_set_free(bset);
4580 isl_space_free(dim);
4581 return NULL;
4584 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4585 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4587 return basic_map_from_basic_set(bset, space);
4590 /* For a div d = floor(f/m), add the constraint
4592 * f - m d >= 0
4594 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4595 unsigned pos, isl_int *div)
4597 int i;
4598 unsigned total = isl_basic_map_total_dim(bmap);
4600 i = isl_basic_map_alloc_inequality(bmap);
4601 if (i < 0)
4602 return -1;
4603 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4604 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4606 return 0;
4609 /* For a div d = floor(f/m), add the constraint
4611 * -(f-(m-1)) + m d >= 0
4613 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4614 unsigned pos, isl_int *div)
4616 int i;
4617 unsigned total = isl_basic_map_total_dim(bmap);
4619 i = isl_basic_map_alloc_inequality(bmap);
4620 if (i < 0)
4621 return -1;
4622 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4623 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4624 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4625 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4627 return 0;
4630 /* For a div d = floor(f/m), add the constraints
4632 * f - m d >= 0
4633 * -(f-(m-1)) + m d >= 0
4635 * Note that the second constraint is the negation of
4637 * f - m d >= m
4639 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4640 unsigned pos, isl_int *div)
4642 if (add_upper_div_constraint(bmap, pos, div) < 0)
4643 return -1;
4644 if (add_lower_div_constraint(bmap, pos, div) < 0)
4645 return -1;
4646 return 0;
4649 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4650 unsigned pos, isl_int *div)
4652 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4653 pos, div);
4656 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4658 unsigned total = isl_basic_map_total_dim(bmap);
4659 unsigned div_pos = total - bmap->n_div + div;
4661 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4662 bmap->div[div]);
4665 /* For each known div d = floor(f/m), add the constraints
4667 * f - m d >= 0
4668 * -(f-(m-1)) + m d >= 0
4670 * Remove duplicate constraints in case of some these div constraints
4671 * already appear in "bmap".
4673 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4674 __isl_take isl_basic_map *bmap)
4676 unsigned n_div;
4678 if (!bmap)
4679 return NULL;
4680 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4681 if (n_div == 0)
4682 return bmap;
4684 bmap = add_known_div_constraints(bmap);
4685 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4686 bmap = isl_basic_map_finalize(bmap);
4687 return bmap;
4690 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4692 * In particular, if this div is of the form d = floor(f/m),
4693 * then add the constraint
4695 * f - m d >= 0
4697 * if sign < 0 or the constraint
4699 * -(f-(m-1)) + m d >= 0
4701 * if sign > 0.
4703 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4704 unsigned div, int sign)
4706 unsigned total;
4707 unsigned div_pos;
4709 if (!bmap)
4710 return -1;
4712 total = isl_basic_map_total_dim(bmap);
4713 div_pos = total - bmap->n_div + div;
4715 if (sign < 0)
4716 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4717 else
4718 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4721 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4723 return isl_basic_map_add_div_constraints(bset, div);
4726 struct isl_basic_set *isl_basic_map_underlying_set(
4727 struct isl_basic_map *bmap)
4729 if (!bmap)
4730 goto error;
4731 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4732 bmap->n_div == 0 &&
4733 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4734 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4735 return bset_from_bmap(bmap);
4736 bmap = isl_basic_map_cow(bmap);
4737 if (!bmap)
4738 goto error;
4739 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4740 if (!bmap->dim)
4741 goto error;
4742 bmap->extra -= bmap->n_div;
4743 bmap->n_div = 0;
4744 bmap = isl_basic_map_finalize(bmap);
4745 return bset_from_bmap(bmap);
4746 error:
4747 isl_basic_map_free(bmap);
4748 return NULL;
4751 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4752 __isl_take isl_basic_set *bset)
4754 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4757 /* Replace each element in "list" by the result of applying
4758 * isl_basic_map_underlying_set to the element.
4760 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4761 __isl_take isl_basic_map_list *list)
4763 int i, n;
4765 if (!list)
4766 return NULL;
4768 n = isl_basic_map_list_n_basic_map(list);
4769 for (i = 0; i < n; ++i) {
4770 isl_basic_map *bmap;
4771 isl_basic_set *bset;
4773 bmap = isl_basic_map_list_get_basic_map(list, i);
4774 bset = isl_basic_set_underlying_set(bmap);
4775 list = isl_basic_set_list_set_basic_set(list, i, bset);
4778 return list;
4781 struct isl_basic_map *isl_basic_map_overlying_set(
4782 struct isl_basic_set *bset, struct isl_basic_map *like)
4784 struct isl_basic_map *bmap;
4785 struct isl_ctx *ctx;
4786 unsigned total;
4787 int i;
4789 if (!bset || !like)
4790 goto error;
4791 ctx = bset->ctx;
4792 isl_assert(ctx, bset->n_div == 0, goto error);
4793 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4794 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4795 goto error);
4796 if (like->n_div == 0) {
4797 isl_space *space = isl_basic_map_get_space(like);
4798 isl_basic_map_free(like);
4799 return isl_basic_map_reset_space(bset, space);
4801 bset = isl_basic_set_cow(bset);
4802 if (!bset)
4803 goto error;
4804 total = bset->dim->n_out + bset->extra;
4805 bmap = bset_to_bmap(bset);
4806 isl_space_free(bmap->dim);
4807 bmap->dim = isl_space_copy(like->dim);
4808 if (!bmap->dim)
4809 goto error;
4810 bmap->n_div = like->n_div;
4811 bmap->extra += like->n_div;
4812 if (bmap->extra) {
4813 unsigned ltotal;
4814 isl_int **div;
4815 ltotal = total - bmap->extra + like->extra;
4816 if (ltotal > total)
4817 ltotal = total;
4818 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4819 bmap->extra * (1 + 1 + total));
4820 if (isl_blk_is_error(bmap->block2))
4821 goto error;
4822 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4823 if (!div)
4824 goto error;
4825 bmap->div = div;
4826 for (i = 0; i < bmap->extra; ++i)
4827 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4828 for (i = 0; i < like->n_div; ++i) {
4829 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4830 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4832 bmap = isl_basic_map_add_known_div_constraints(bmap);
4834 isl_basic_map_free(like);
4835 bmap = isl_basic_map_simplify(bmap);
4836 bmap = isl_basic_map_finalize(bmap);
4837 return bmap;
4838 error:
4839 isl_basic_map_free(like);
4840 isl_basic_set_free(bset);
4841 return NULL;
4844 struct isl_basic_set *isl_basic_set_from_underlying_set(
4845 struct isl_basic_set *bset, struct isl_basic_set *like)
4847 return bset_from_bmap(isl_basic_map_overlying_set(bset,
4848 bset_to_bmap(like)));
4851 struct isl_set *isl_set_from_underlying_set(
4852 struct isl_set *set, struct isl_basic_set *like)
4854 int i;
4856 if (!set || !like)
4857 goto error;
4858 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4859 goto error);
4860 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4861 isl_basic_set_free(like);
4862 return set;
4864 set = isl_set_cow(set);
4865 if (!set)
4866 goto error;
4867 for (i = 0; i < set->n; ++i) {
4868 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4869 isl_basic_set_copy(like));
4870 if (!set->p[i])
4871 goto error;
4873 isl_space_free(set->dim);
4874 set->dim = isl_space_copy(like->dim);
4875 if (!set->dim)
4876 goto error;
4877 isl_basic_set_free(like);
4878 return set;
4879 error:
4880 isl_basic_set_free(like);
4881 isl_set_free(set);
4882 return NULL;
4885 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4887 int i;
4889 map = isl_map_cow(map);
4890 if (!map)
4891 return NULL;
4892 map->dim = isl_space_cow(map->dim);
4893 if (!map->dim)
4894 goto error;
4896 for (i = 1; i < map->n; ++i)
4897 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4898 goto error);
4899 for (i = 0; i < map->n; ++i) {
4900 map->p[i] = bset_to_bmap(
4901 isl_basic_map_underlying_set(map->p[i]));
4902 if (!map->p[i])
4903 goto error;
4905 if (map->n == 0)
4906 map->dim = isl_space_underlying(map->dim, 0);
4907 else {
4908 isl_space_free(map->dim);
4909 map->dim = isl_space_copy(map->p[0]->dim);
4911 if (!map->dim)
4912 goto error;
4913 return set_from_map(map);
4914 error:
4915 isl_map_free(map);
4916 return NULL;
4919 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4921 return set_from_map(isl_map_underlying_set(set_to_map(set)));
4924 /* Replace the space of "bmap" by "space".
4926 * If the space of "bmap" is identical to "space" (including the identifiers
4927 * of the input and output dimensions), then simply return the original input.
4929 __isl_give isl_basic_map *isl_basic_map_reset_space(
4930 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4932 isl_bool equal;
4934 if (!bmap)
4935 goto error;
4936 equal = isl_space_is_equal(bmap->dim, space);
4937 if (equal >= 0 && equal)
4938 equal = isl_space_match(bmap->dim, isl_dim_in,
4939 space, isl_dim_in);
4940 if (equal >= 0 && equal)
4941 equal = isl_space_match(bmap->dim, isl_dim_out,
4942 space, isl_dim_out);
4943 if (equal < 0)
4944 goto error;
4945 if (equal) {
4946 isl_space_free(space);
4947 return bmap;
4949 bmap = isl_basic_map_cow(bmap);
4950 if (!bmap || !space)
4951 goto error;
4953 isl_space_free(bmap->dim);
4954 bmap->dim = space;
4956 bmap = isl_basic_map_finalize(bmap);
4958 return bmap;
4959 error:
4960 isl_basic_map_free(bmap);
4961 isl_space_free(space);
4962 return NULL;
4965 __isl_give isl_basic_set *isl_basic_set_reset_space(
4966 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4968 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
4969 dim));
4972 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4973 __isl_take isl_space *dim)
4975 int i;
4977 map = isl_map_cow(map);
4978 if (!map || !dim)
4979 goto error;
4981 for (i = 0; i < map->n; ++i) {
4982 map->p[i] = isl_basic_map_reset_space(map->p[i],
4983 isl_space_copy(dim));
4984 if (!map->p[i])
4985 goto error;
4987 isl_space_free(map->dim);
4988 map->dim = dim;
4990 return map;
4991 error:
4992 isl_map_free(map);
4993 isl_space_free(dim);
4994 return NULL;
4997 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4998 __isl_take isl_space *dim)
5000 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5003 /* Compute the parameter domain of the given basic set.
5005 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5007 isl_space *space;
5008 unsigned n;
5010 if (isl_basic_set_is_params(bset))
5011 return bset;
5013 n = isl_basic_set_dim(bset, isl_dim_set);
5014 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5015 space = isl_basic_set_get_space(bset);
5016 space = isl_space_params(space);
5017 bset = isl_basic_set_reset_space(bset, space);
5018 return bset;
5021 /* Construct a zero-dimensional basic set with the given parameter domain.
5023 __isl_give isl_basic_set *isl_basic_set_from_params(
5024 __isl_take isl_basic_set *bset)
5026 isl_space *space;
5027 space = isl_basic_set_get_space(bset);
5028 space = isl_space_set_from_params(space);
5029 bset = isl_basic_set_reset_space(bset, space);
5030 return bset;
5033 /* Compute the parameter domain of the given set.
5035 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5037 isl_space *space;
5038 unsigned n;
5040 if (isl_set_is_params(set))
5041 return set;
5043 n = isl_set_dim(set, isl_dim_set);
5044 set = isl_set_project_out(set, isl_dim_set, 0, n);
5045 space = isl_set_get_space(set);
5046 space = isl_space_params(space);
5047 set = isl_set_reset_space(set, space);
5048 return set;
5051 /* Construct a zero-dimensional set with the given parameter domain.
5053 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5055 isl_space *space;
5056 space = isl_set_get_space(set);
5057 space = isl_space_set_from_params(space);
5058 set = isl_set_reset_space(set, space);
5059 return set;
5062 /* Compute the parameter domain of the given map.
5064 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5066 isl_space *space;
5067 unsigned n;
5069 n = isl_map_dim(map, isl_dim_in);
5070 map = isl_map_project_out(map, isl_dim_in, 0, n);
5071 n = isl_map_dim(map, isl_dim_out);
5072 map = isl_map_project_out(map, isl_dim_out, 0, n);
5073 space = isl_map_get_space(map);
5074 space = isl_space_params(space);
5075 map = isl_map_reset_space(map, space);
5076 return map;
5079 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5081 isl_space *space;
5082 unsigned n_out;
5084 if (!bmap)
5085 return NULL;
5086 space = isl_space_domain(isl_basic_map_get_space(bmap));
5088 n_out = isl_basic_map_n_out(bmap);
5089 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5091 return isl_basic_map_reset_space(bmap, space);
5094 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5096 if (!bmap)
5097 return -1;
5098 return isl_space_may_be_set(bmap->dim);
5101 /* Is this basic map actually a set?
5102 * Users should never call this function. Outside of isl,
5103 * the type should indicate whether something is a set or a map.
5105 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5107 if (!bmap)
5108 return -1;
5109 return isl_space_is_set(bmap->dim);
5112 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5114 if (!bmap)
5115 return NULL;
5116 if (isl_basic_map_is_set(bmap))
5117 return bmap;
5118 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5121 __isl_give isl_basic_map *isl_basic_map_domain_map(
5122 __isl_take isl_basic_map *bmap)
5124 int i, k;
5125 isl_space *dim;
5126 isl_basic_map *domain;
5127 int nparam, n_in, n_out;
5128 unsigned total;
5130 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5131 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5132 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5134 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5135 domain = isl_basic_map_universe(dim);
5137 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5138 bmap = isl_basic_map_apply_range(bmap, domain);
5139 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5141 total = isl_basic_map_total_dim(bmap);
5143 for (i = 0; i < n_in; ++i) {
5144 k = isl_basic_map_alloc_equality(bmap);
5145 if (k < 0)
5146 goto error;
5147 isl_seq_clr(bmap->eq[k], 1 + total);
5148 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5149 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5152 bmap = isl_basic_map_gauss(bmap, NULL);
5153 return isl_basic_map_finalize(bmap);
5154 error:
5155 isl_basic_map_free(bmap);
5156 return NULL;
5159 __isl_give isl_basic_map *isl_basic_map_range_map(
5160 __isl_take isl_basic_map *bmap)
5162 int i, k;
5163 isl_space *dim;
5164 isl_basic_map *range;
5165 int nparam, n_in, n_out;
5166 unsigned total;
5168 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5169 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5170 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5172 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5173 range = isl_basic_map_universe(dim);
5175 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5176 bmap = isl_basic_map_apply_range(bmap, range);
5177 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5179 total = isl_basic_map_total_dim(bmap);
5181 for (i = 0; i < n_out; ++i) {
5182 k = isl_basic_map_alloc_equality(bmap);
5183 if (k < 0)
5184 goto error;
5185 isl_seq_clr(bmap->eq[k], 1 + total);
5186 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5187 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5190 bmap = isl_basic_map_gauss(bmap, NULL);
5191 return isl_basic_map_finalize(bmap);
5192 error:
5193 isl_basic_map_free(bmap);
5194 return NULL;
5197 int isl_map_may_be_set(__isl_keep isl_map *map)
5199 if (!map)
5200 return -1;
5201 return isl_space_may_be_set(map->dim);
5204 /* Is this map actually a set?
5205 * Users should never call this function. Outside of isl,
5206 * the type should indicate whether something is a set or a map.
5208 int isl_map_is_set(__isl_keep isl_map *map)
5210 if (!map)
5211 return -1;
5212 return isl_space_is_set(map->dim);
5215 struct isl_set *isl_map_range(struct isl_map *map)
5217 int i;
5218 struct isl_set *set;
5220 if (!map)
5221 goto error;
5222 if (isl_map_is_set(map))
5223 return set_from_map(map);
5225 map = isl_map_cow(map);
5226 if (!map)
5227 goto error;
5229 set = set_from_map(map);
5230 set->dim = isl_space_range(set->dim);
5231 if (!set->dim)
5232 goto error;
5233 for (i = 0; i < map->n; ++i) {
5234 set->p[i] = isl_basic_map_range(map->p[i]);
5235 if (!set->p[i])
5236 goto error;
5238 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5239 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5240 return set;
5241 error:
5242 isl_map_free(map);
5243 return NULL;
5246 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5248 int i;
5250 map = isl_map_cow(map);
5251 if (!map)
5252 return NULL;
5254 map->dim = isl_space_domain_map(map->dim);
5255 if (!map->dim)
5256 goto error;
5257 for (i = 0; i < map->n; ++i) {
5258 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5259 if (!map->p[i])
5260 goto error;
5262 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5263 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5264 return map;
5265 error:
5266 isl_map_free(map);
5267 return NULL;
5270 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5272 int i;
5273 isl_space *range_dim;
5275 map = isl_map_cow(map);
5276 if (!map)
5277 return NULL;
5279 range_dim = isl_space_range(isl_map_get_space(map));
5280 range_dim = isl_space_from_range(range_dim);
5281 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5282 map->dim = isl_space_join(map->dim, range_dim);
5283 if (!map->dim)
5284 goto error;
5285 for (i = 0; i < map->n; ++i) {
5286 map->p[i] = isl_basic_map_range_map(map->p[i]);
5287 if (!map->p[i])
5288 goto error;
5290 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5291 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5292 return map;
5293 error:
5294 isl_map_free(map);
5295 return NULL;
5298 /* Given a wrapped map of the form A[B -> C],
5299 * return the map A[B -> C] -> B.
5301 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5303 isl_id *id;
5304 isl_map *map;
5306 if (!set)
5307 return NULL;
5308 if (!isl_set_has_tuple_id(set))
5309 return isl_map_domain_map(isl_set_unwrap(set));
5311 id = isl_set_get_tuple_id(set);
5312 map = isl_map_domain_map(isl_set_unwrap(set));
5313 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5315 return map;
5318 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5319 __isl_take isl_space *dim)
5321 int i;
5322 struct isl_map *map = NULL;
5324 set = isl_set_cow(set);
5325 if (!set || !dim)
5326 goto error;
5327 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5328 goto error);
5329 map = set_to_map(set);
5330 for (i = 0; i < set->n; ++i) {
5331 map->p[i] = basic_map_from_basic_set(
5332 set->p[i], isl_space_copy(dim));
5333 if (!map->p[i])
5334 goto error;
5336 isl_space_free(map->dim);
5337 map->dim = dim;
5338 return map;
5339 error:
5340 isl_space_free(dim);
5341 isl_set_free(set);
5342 return NULL;
5345 __isl_give isl_basic_map *isl_basic_map_from_domain(
5346 __isl_take isl_basic_set *bset)
5348 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5351 __isl_give isl_basic_map *isl_basic_map_from_range(
5352 __isl_take isl_basic_set *bset)
5354 isl_space *space;
5355 space = isl_basic_set_get_space(bset);
5356 space = isl_space_from_range(space);
5357 bset = isl_basic_set_reset_space(bset, space);
5358 return bset_to_bmap(bset);
5361 /* Create a relation with the given set as range.
5362 * The domain of the created relation is a zero-dimensional
5363 * flat anonymous space.
5365 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5367 isl_space *space;
5368 space = isl_set_get_space(set);
5369 space = isl_space_from_range(space);
5370 set = isl_set_reset_space(set, space);
5371 return set_to_map(set);
5374 /* Create a relation with the given set as domain.
5375 * The range of the created relation is a zero-dimensional
5376 * flat anonymous space.
5378 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5380 return isl_map_reverse(isl_map_from_range(set));
5383 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5384 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5386 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5389 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5390 __isl_take isl_set *range)
5392 return isl_map_apply_range(isl_map_reverse(domain), range);
5395 /* Return a newly allocated isl_map with given space and flags and
5396 * room for "n" basic maps.
5397 * Make sure that all cached information is cleared.
5399 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5400 unsigned flags)
5402 struct isl_map *map;
5404 if (!space)
5405 return NULL;
5406 if (n < 0)
5407 isl_die(space->ctx, isl_error_internal,
5408 "negative number of basic maps", goto error);
5409 map = isl_calloc(space->ctx, struct isl_map,
5410 sizeof(struct isl_map) +
5411 (n - 1) * sizeof(struct isl_basic_map *));
5412 if (!map)
5413 goto error;
5415 map->ctx = space->ctx;
5416 isl_ctx_ref(map->ctx);
5417 map->ref = 1;
5418 map->size = n;
5419 map->n = 0;
5420 map->dim = space;
5421 map->flags = flags;
5422 return map;
5423 error:
5424 isl_space_free(space);
5425 return NULL;
5428 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5429 unsigned nparam, unsigned in, unsigned out, int n,
5430 unsigned flags)
5432 struct isl_map *map;
5433 isl_space *dims;
5435 dims = isl_space_alloc(ctx, nparam, in, out);
5436 if (!dims)
5437 return NULL;
5439 map = isl_map_alloc_space(dims, n, flags);
5440 return map;
5443 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5445 struct isl_basic_map *bmap;
5446 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5447 bmap = isl_basic_map_set_to_empty(bmap);
5448 return bmap;
5451 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5453 struct isl_basic_set *bset;
5454 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5455 bset = isl_basic_set_set_to_empty(bset);
5456 return bset;
5459 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5461 struct isl_basic_map *bmap;
5462 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5463 bmap = isl_basic_map_finalize(bmap);
5464 return bmap;
5467 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5469 struct isl_basic_set *bset;
5470 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5471 bset = isl_basic_set_finalize(bset);
5472 return bset;
5475 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5477 int i;
5478 unsigned total = isl_space_dim(dim, isl_dim_all);
5479 isl_basic_map *bmap;
5481 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5482 for (i = 0; i < total; ++i) {
5483 int k = isl_basic_map_alloc_inequality(bmap);
5484 if (k < 0)
5485 goto error;
5486 isl_seq_clr(bmap->ineq[k], 1 + total);
5487 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5489 return bmap;
5490 error:
5491 isl_basic_map_free(bmap);
5492 return NULL;
5495 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5497 return isl_basic_map_nat_universe(dim);
5500 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5502 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5505 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5507 return isl_map_nat_universe(dim);
5510 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5512 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5515 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5517 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5520 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5522 struct isl_map *map;
5523 if (!dim)
5524 return NULL;
5525 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5526 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5527 return map;
5530 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5532 struct isl_set *set;
5533 if (!dim)
5534 return NULL;
5535 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5536 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5537 return set;
5540 struct isl_map *isl_map_dup(struct isl_map *map)
5542 int i;
5543 struct isl_map *dup;
5545 if (!map)
5546 return NULL;
5547 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5548 for (i = 0; i < map->n; ++i)
5549 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5550 return dup;
5553 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5554 __isl_take isl_basic_map *bmap)
5556 if (!bmap || !map)
5557 goto error;
5558 if (isl_basic_map_plain_is_empty(bmap)) {
5559 isl_basic_map_free(bmap);
5560 return map;
5562 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5563 isl_assert(map->ctx, map->n < map->size, goto error);
5564 map->p[map->n] = bmap;
5565 map->n++;
5566 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5567 return map;
5568 error:
5569 if (map)
5570 isl_map_free(map);
5571 if (bmap)
5572 isl_basic_map_free(bmap);
5573 return NULL;
5576 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5578 int i;
5580 if (!map)
5581 return NULL;
5583 if (--map->ref > 0)
5584 return NULL;
5586 clear_caches(map);
5587 isl_ctx_deref(map->ctx);
5588 for (i = 0; i < map->n; ++i)
5589 isl_basic_map_free(map->p[i]);
5590 isl_space_free(map->dim);
5591 free(map);
5593 return NULL;
5596 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5597 struct isl_basic_map *bmap, unsigned pos, int value)
5599 int j;
5601 bmap = isl_basic_map_cow(bmap);
5602 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5603 j = isl_basic_map_alloc_equality(bmap);
5604 if (j < 0)
5605 goto error;
5606 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5607 isl_int_set_si(bmap->eq[j][pos], -1);
5608 isl_int_set_si(bmap->eq[j][0], value);
5609 bmap = isl_basic_map_simplify(bmap);
5610 return isl_basic_map_finalize(bmap);
5611 error:
5612 isl_basic_map_free(bmap);
5613 return NULL;
5616 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5617 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5619 int j;
5621 bmap = isl_basic_map_cow(bmap);
5622 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5623 j = isl_basic_map_alloc_equality(bmap);
5624 if (j < 0)
5625 goto error;
5626 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5627 isl_int_set_si(bmap->eq[j][pos], -1);
5628 isl_int_set(bmap->eq[j][0], value);
5629 bmap = isl_basic_map_simplify(bmap);
5630 return isl_basic_map_finalize(bmap);
5631 error:
5632 isl_basic_map_free(bmap);
5633 return NULL;
5636 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5637 enum isl_dim_type type, unsigned pos, int value)
5639 if (!bmap)
5640 return NULL;
5641 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5642 return isl_basic_map_fix_pos_si(bmap,
5643 isl_basic_map_offset(bmap, type) + pos, value);
5644 error:
5645 isl_basic_map_free(bmap);
5646 return NULL;
5649 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5650 enum isl_dim_type type, unsigned pos, isl_int value)
5652 if (!bmap)
5653 return NULL;
5654 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5655 return isl_basic_map_fix_pos(bmap,
5656 isl_basic_map_offset(bmap, type) + pos, value);
5657 error:
5658 isl_basic_map_free(bmap);
5659 return NULL;
5662 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5663 * to be equal to "v".
5665 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5666 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5668 if (!bmap || !v)
5669 goto error;
5670 if (!isl_val_is_int(v))
5671 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5672 "expecting integer value", goto error);
5673 if (pos >= isl_basic_map_dim(bmap, type))
5674 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5675 "index out of bounds", goto error);
5676 pos += isl_basic_map_offset(bmap, type);
5677 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5678 isl_val_free(v);
5679 return bmap;
5680 error:
5681 isl_basic_map_free(bmap);
5682 isl_val_free(v);
5683 return NULL;
5686 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5687 * to be equal to "v".
5689 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5690 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5692 return isl_basic_map_fix_val(bset, type, pos, v);
5695 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5696 enum isl_dim_type type, unsigned pos, int value)
5698 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5699 type, pos, value));
5702 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5703 enum isl_dim_type type, unsigned pos, isl_int value)
5705 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5706 type, pos, value));
5709 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5710 unsigned input, int value)
5712 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5715 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5716 unsigned dim, int value)
5718 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5719 isl_dim_set, dim, value));
5722 static int remove_if_empty(__isl_keep isl_map *map, int i)
5724 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5726 if (empty < 0)
5727 return -1;
5728 if (!empty)
5729 return 0;
5731 isl_basic_map_free(map->p[i]);
5732 if (i != map->n - 1) {
5733 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5734 map->p[i] = map->p[map->n - 1];
5736 map->n--;
5738 return 0;
5741 /* Perform "fn" on each basic map of "map", where we may not be holding
5742 * the only reference to "map".
5743 * In particular, "fn" should be a semantics preserving operation
5744 * that we want to apply to all copies of "map". We therefore need
5745 * to be careful not to modify "map" in a way that breaks "map"
5746 * in case anything goes wrong.
5748 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5749 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5751 struct isl_basic_map *bmap;
5752 int i;
5754 if (!map)
5755 return NULL;
5757 for (i = map->n - 1; i >= 0; --i) {
5758 bmap = isl_basic_map_copy(map->p[i]);
5759 bmap = fn(bmap);
5760 if (!bmap)
5761 goto error;
5762 isl_basic_map_free(map->p[i]);
5763 map->p[i] = bmap;
5764 if (remove_if_empty(map, i) < 0)
5765 goto error;
5768 return map;
5769 error:
5770 isl_map_free(map);
5771 return NULL;
5774 struct isl_map *isl_map_fix_si(struct isl_map *map,
5775 enum isl_dim_type type, unsigned pos, int value)
5777 int i;
5779 map = isl_map_cow(map);
5780 if (!map)
5781 return NULL;
5783 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5784 for (i = map->n - 1; i >= 0; --i) {
5785 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5786 if (remove_if_empty(map, i) < 0)
5787 goto error;
5789 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5790 return map;
5791 error:
5792 isl_map_free(map);
5793 return NULL;
5796 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5797 enum isl_dim_type type, unsigned pos, int value)
5799 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5802 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5803 enum isl_dim_type type, unsigned pos, isl_int value)
5805 int i;
5807 map = isl_map_cow(map);
5808 if (!map)
5809 return NULL;
5811 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5812 for (i = 0; i < map->n; ++i) {
5813 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5814 if (!map->p[i])
5815 goto error;
5817 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5818 return map;
5819 error:
5820 isl_map_free(map);
5821 return NULL;
5824 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5825 enum isl_dim_type type, unsigned pos, isl_int value)
5827 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5830 /* Fix the value of the variable at position "pos" of type "type" of "map"
5831 * to be equal to "v".
5833 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5834 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5836 int i;
5838 map = isl_map_cow(map);
5839 if (!map || !v)
5840 goto error;
5842 if (!isl_val_is_int(v))
5843 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5844 "expecting integer value", goto error);
5845 if (pos >= isl_map_dim(map, type))
5846 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5847 "index out of bounds", goto error);
5848 for (i = map->n - 1; i >= 0; --i) {
5849 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5850 isl_val_copy(v));
5851 if (remove_if_empty(map, i) < 0)
5852 goto error;
5854 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5855 isl_val_free(v);
5856 return map;
5857 error:
5858 isl_map_free(map);
5859 isl_val_free(v);
5860 return NULL;
5863 /* Fix the value of the variable at position "pos" of type "type" of "set"
5864 * to be equal to "v".
5866 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5867 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5869 return isl_map_fix_val(set, type, pos, v);
5872 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5873 unsigned input, int value)
5875 return isl_map_fix_si(map, isl_dim_in, input, value);
5878 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5880 return set_from_map(isl_map_fix_si(set_to_map(set),
5881 isl_dim_set, dim, value));
5884 static __isl_give isl_basic_map *basic_map_bound_si(
5885 __isl_take isl_basic_map *bmap,
5886 enum isl_dim_type type, unsigned pos, int value, int upper)
5888 int j;
5890 if (!bmap)
5891 return NULL;
5892 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5893 pos += isl_basic_map_offset(bmap, type);
5894 bmap = isl_basic_map_cow(bmap);
5895 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5896 j = isl_basic_map_alloc_inequality(bmap);
5897 if (j < 0)
5898 goto error;
5899 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5900 if (upper) {
5901 isl_int_set_si(bmap->ineq[j][pos], -1);
5902 isl_int_set_si(bmap->ineq[j][0], value);
5903 } else {
5904 isl_int_set_si(bmap->ineq[j][pos], 1);
5905 isl_int_set_si(bmap->ineq[j][0], -value);
5907 bmap = isl_basic_map_simplify(bmap);
5908 return isl_basic_map_finalize(bmap);
5909 error:
5910 isl_basic_map_free(bmap);
5911 return NULL;
5914 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5915 __isl_take isl_basic_map *bmap,
5916 enum isl_dim_type type, unsigned pos, int value)
5918 return basic_map_bound_si(bmap, type, pos, value, 0);
5921 /* Constrain the values of the given dimension to be no greater than "value".
5923 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5924 __isl_take isl_basic_map *bmap,
5925 enum isl_dim_type type, unsigned pos, int value)
5927 return basic_map_bound_si(bmap, type, pos, value, 1);
5930 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5931 unsigned dim, isl_int value)
5933 int j;
5935 bset = isl_basic_set_cow(bset);
5936 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5937 j = isl_basic_set_alloc_inequality(bset);
5938 if (j < 0)
5939 goto error;
5940 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5941 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5942 isl_int_neg(bset->ineq[j][0], value);
5943 bset = isl_basic_set_simplify(bset);
5944 return isl_basic_set_finalize(bset);
5945 error:
5946 isl_basic_set_free(bset);
5947 return NULL;
5950 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5951 enum isl_dim_type type, unsigned pos, int value, int upper)
5953 int i;
5955 map = isl_map_cow(map);
5956 if (!map)
5957 return NULL;
5959 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5960 for (i = 0; i < map->n; ++i) {
5961 map->p[i] = basic_map_bound_si(map->p[i],
5962 type, pos, value, upper);
5963 if (!map->p[i])
5964 goto error;
5966 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5967 return map;
5968 error:
5969 isl_map_free(map);
5970 return NULL;
5973 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5974 enum isl_dim_type type, unsigned pos, int value)
5976 return map_bound_si(map, type, pos, value, 0);
5979 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5980 enum isl_dim_type type, unsigned pos, int value)
5982 return map_bound_si(map, type, pos, value, 1);
5985 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5986 enum isl_dim_type type, unsigned pos, int value)
5988 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
5989 type, pos, value));
5992 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5993 enum isl_dim_type type, unsigned pos, int value)
5995 return isl_map_upper_bound_si(set, type, pos, value);
5998 /* Bound the given variable of "bmap" from below (or above is "upper"
5999 * is set) to "value".
6001 static __isl_give isl_basic_map *basic_map_bound(
6002 __isl_take isl_basic_map *bmap,
6003 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6005 int j;
6007 if (!bmap)
6008 return NULL;
6009 if (pos >= isl_basic_map_dim(bmap, type))
6010 isl_die(bmap->ctx, isl_error_invalid,
6011 "index out of bounds", goto error);
6012 pos += isl_basic_map_offset(bmap, type);
6013 bmap = isl_basic_map_cow(bmap);
6014 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6015 j = isl_basic_map_alloc_inequality(bmap);
6016 if (j < 0)
6017 goto error;
6018 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6019 if (upper) {
6020 isl_int_set_si(bmap->ineq[j][pos], -1);
6021 isl_int_set(bmap->ineq[j][0], value);
6022 } else {
6023 isl_int_set_si(bmap->ineq[j][pos], 1);
6024 isl_int_neg(bmap->ineq[j][0], value);
6026 bmap = isl_basic_map_simplify(bmap);
6027 return isl_basic_map_finalize(bmap);
6028 error:
6029 isl_basic_map_free(bmap);
6030 return NULL;
6033 /* Bound the given variable of "map" from below (or above is "upper"
6034 * is set) to "value".
6036 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6037 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6039 int i;
6041 map = isl_map_cow(map);
6042 if (!map)
6043 return NULL;
6045 if (pos >= isl_map_dim(map, type))
6046 isl_die(map->ctx, isl_error_invalid,
6047 "index out of bounds", goto error);
6048 for (i = map->n - 1; i >= 0; --i) {
6049 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6050 if (remove_if_empty(map, i) < 0)
6051 goto error;
6053 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6054 return map;
6055 error:
6056 isl_map_free(map);
6057 return NULL;
6060 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6061 enum isl_dim_type type, unsigned pos, isl_int value)
6063 return map_bound(map, type, pos, value, 0);
6066 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6067 enum isl_dim_type type, unsigned pos, isl_int value)
6069 return map_bound(map, type, pos, value, 1);
6072 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6073 enum isl_dim_type type, unsigned pos, isl_int value)
6075 return isl_map_lower_bound(set, type, pos, value);
6078 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6079 enum isl_dim_type type, unsigned pos, isl_int value)
6081 return isl_map_upper_bound(set, type, pos, value);
6084 /* Force the values of the variable at position "pos" of type "type" of "set"
6085 * to be no smaller than "value".
6087 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6088 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6090 if (!value)
6091 goto error;
6092 if (!isl_val_is_int(value))
6093 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6094 "expecting integer value", goto error);
6095 set = isl_set_lower_bound(set, type, pos, value->n);
6096 isl_val_free(value);
6097 return set;
6098 error:
6099 isl_val_free(value);
6100 isl_set_free(set);
6101 return NULL;
6104 /* Force the values of the variable at position "pos" of type "type" of "set"
6105 * to be no greater than "value".
6107 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6108 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6110 if (!value)
6111 goto error;
6112 if (!isl_val_is_int(value))
6113 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6114 "expecting integer value", goto error);
6115 set = isl_set_upper_bound(set, type, pos, value->n);
6116 isl_val_free(value);
6117 return set;
6118 error:
6119 isl_val_free(value);
6120 isl_set_free(set);
6121 return NULL;
6124 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6125 isl_int value)
6127 int i;
6129 set = isl_set_cow(set);
6130 if (!set)
6131 return NULL;
6133 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6134 for (i = 0; i < set->n; ++i) {
6135 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6136 if (!set->p[i])
6137 goto error;
6139 return set;
6140 error:
6141 isl_set_free(set);
6142 return NULL;
6145 struct isl_map *isl_map_reverse(struct isl_map *map)
6147 int i;
6149 map = isl_map_cow(map);
6150 if (!map)
6151 return NULL;
6153 map->dim = isl_space_reverse(map->dim);
6154 if (!map->dim)
6155 goto error;
6156 for (i = 0; i < map->n; ++i) {
6157 map->p[i] = isl_basic_map_reverse(map->p[i]);
6158 if (!map->p[i])
6159 goto error;
6161 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6162 return map;
6163 error:
6164 isl_map_free(map);
6165 return NULL;
6168 #undef TYPE
6169 #define TYPE isl_pw_multi_aff
6170 #undef SUFFIX
6171 #define SUFFIX _pw_multi_aff
6172 #undef EMPTY
6173 #define EMPTY isl_pw_multi_aff_empty
6174 #undef ADD
6175 #define ADD isl_pw_multi_aff_union_add
6176 #include "isl_map_lexopt_templ.c"
6178 /* Given a map "map", compute the lexicographically minimal
6179 * (or maximal) image element for each domain element in dom,
6180 * in the form of an isl_pw_multi_aff.
6181 * If "empty" is not NULL, then set *empty to those elements in dom that
6182 * do not have an image element.
6183 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6184 * should be computed over the domain of "map". "empty" is also NULL
6185 * in this case.
6187 * We first compute the lexicographically minimal or maximal element
6188 * in the first basic map. This results in a partial solution "res"
6189 * and a subset "todo" of dom that still need to be handled.
6190 * We then consider each of the remaining maps in "map" and successively
6191 * update both "res" and "todo".
6192 * If "empty" is NULL, then the todo sets are not needed and therefore
6193 * also not computed.
6195 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6196 __isl_take isl_map *map, __isl_take isl_set *dom,
6197 __isl_give isl_set **empty, unsigned flags)
6199 int i;
6200 int full;
6201 isl_pw_multi_aff *res;
6202 isl_set *todo;
6204 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6205 if (!map || (!full && !dom))
6206 goto error;
6208 if (isl_map_plain_is_empty(map)) {
6209 if (empty)
6210 *empty = dom;
6211 else
6212 isl_set_free(dom);
6213 return isl_pw_multi_aff_from_map(map);
6216 res = basic_map_partial_lexopt_pw_multi_aff(
6217 isl_basic_map_copy(map->p[0]),
6218 isl_set_copy(dom), empty, flags);
6220 if (empty)
6221 todo = *empty;
6222 for (i = 1; i < map->n; ++i) {
6223 isl_pw_multi_aff *res_i;
6225 res_i = basic_map_partial_lexopt_pw_multi_aff(
6226 isl_basic_map_copy(map->p[i]),
6227 isl_set_copy(dom), empty, flags);
6229 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6230 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6231 else
6232 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6234 if (empty)
6235 todo = isl_set_intersect(todo, *empty);
6238 isl_set_free(dom);
6239 isl_map_free(map);
6241 if (empty)
6242 *empty = todo;
6244 return res;
6245 error:
6246 if (empty)
6247 *empty = NULL;
6248 isl_set_free(dom);
6249 isl_map_free(map);
6250 return NULL;
6253 #undef TYPE
6254 #define TYPE isl_map
6255 #undef SUFFIX
6256 #define SUFFIX
6257 #undef EMPTY
6258 #define EMPTY isl_map_empty
6259 #undef ADD
6260 #define ADD isl_map_union_disjoint
6261 #include "isl_map_lexopt_templ.c"
6263 /* Given a map "map", compute the lexicographically minimal
6264 * (or maximal) image element for each domain element in "dom",
6265 * in the form of an isl_map.
6266 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6267 * do not have an image element.
6268 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6269 * should be computed over the domain of "map". "empty" is also NULL
6270 * in this case.
6272 * If the input consists of more than one disjunct, then first
6273 * compute the desired result in the form of an isl_pw_multi_aff and
6274 * then convert that into an isl_map.
6276 * This function used to have an explicit implementation in terms
6277 * of isl_maps, but it would continually intersect the domains of
6278 * partial results with the complement of the domain of the next
6279 * partial solution, potentially leading to an explosion in the number
6280 * of disjuncts if there are several disjuncts in the input.
6281 * An even earlier implementation of this function would look for
6282 * better results in the domain of the partial result and for extra
6283 * results in the complement of this domain, which would lead to
6284 * even more splintering.
6286 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6287 __isl_take isl_map *map, __isl_take isl_set *dom,
6288 __isl_give isl_set **empty, unsigned flags)
6290 int full;
6291 struct isl_map *res;
6292 isl_pw_multi_aff *pma;
6294 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6295 if (!map || (!full && !dom))
6296 goto error;
6298 if (isl_map_plain_is_empty(map)) {
6299 if (empty)
6300 *empty = dom;
6301 else
6302 isl_set_free(dom);
6303 return map;
6306 if (map->n == 1) {
6307 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6308 dom, empty, flags);
6309 isl_map_free(map);
6310 return res;
6313 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6314 flags);
6315 return isl_map_from_pw_multi_aff(pma);
6316 error:
6317 if (empty)
6318 *empty = NULL;
6319 isl_set_free(dom);
6320 isl_map_free(map);
6321 return NULL;
6324 __isl_give isl_map *isl_map_partial_lexmax(
6325 __isl_take isl_map *map, __isl_take isl_set *dom,
6326 __isl_give isl_set **empty)
6328 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6331 __isl_give isl_map *isl_map_partial_lexmin(
6332 __isl_take isl_map *map, __isl_take isl_set *dom,
6333 __isl_give isl_set **empty)
6335 return isl_map_partial_lexopt(map, dom, empty, 0);
6338 __isl_give isl_set *isl_set_partial_lexmin(
6339 __isl_take isl_set *set, __isl_take isl_set *dom,
6340 __isl_give isl_set **empty)
6342 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6343 dom, empty));
6346 __isl_give isl_set *isl_set_partial_lexmax(
6347 __isl_take isl_set *set, __isl_take isl_set *dom,
6348 __isl_give isl_set **empty)
6350 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6351 dom, empty));
6354 /* Compute the lexicographic minimum (or maximum if "flags" includes
6355 * ISL_OPT_MAX) of "bset" over its parametric domain.
6357 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6358 unsigned flags)
6360 return isl_basic_map_lexopt(bset, flags);
6363 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6365 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6368 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6370 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6373 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6375 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6378 /* Compute the lexicographic minimum of "bset" over its parametric domain
6379 * for the purpose of quantifier elimination.
6380 * That is, find an explicit representation for all the existentially
6381 * quantified variables in "bset" by computing their lexicographic
6382 * minimum.
6384 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6385 __isl_take isl_basic_set *bset)
6387 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6390 /* Extract the first and only affine expression from list
6391 * and then add it to *pwaff with the given dom.
6392 * This domain is known to be disjoint from other domains
6393 * because of the way isl_basic_map_foreach_lexmax works.
6395 static int update_dim_opt(__isl_take isl_basic_set *dom,
6396 __isl_take isl_aff_list *list, void *user)
6398 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6399 isl_aff *aff;
6400 isl_pw_aff **pwaff = user;
6401 isl_pw_aff *pwaff_i;
6403 if (!list)
6404 goto error;
6405 if (isl_aff_list_n_aff(list) != 1)
6406 isl_die(ctx, isl_error_internal,
6407 "expecting single element list", goto error);
6409 aff = isl_aff_list_get_aff(list, 0);
6410 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6412 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6414 isl_aff_list_free(list);
6416 return 0;
6417 error:
6418 isl_basic_set_free(dom);
6419 isl_aff_list_free(list);
6420 return -1;
6423 /* Given a basic map with one output dimension, compute the minimum or
6424 * maximum of that dimension as an isl_pw_aff.
6426 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6427 * call update_dim_opt on each leaf of the result.
6429 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6430 int max)
6432 isl_space *dim = isl_basic_map_get_space(bmap);
6433 isl_pw_aff *pwaff;
6434 int r;
6436 dim = isl_space_from_domain(isl_space_domain(dim));
6437 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6438 pwaff = isl_pw_aff_empty(dim);
6440 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6441 if (r < 0)
6442 return isl_pw_aff_free(pwaff);
6444 return pwaff;
6447 /* Compute the minimum or maximum of the given output dimension
6448 * as a function of the parameters and the input dimensions,
6449 * but independently of the other output dimensions.
6451 * We first project out the other output dimension and then compute
6452 * the "lexicographic" maximum in each basic map, combining the results
6453 * using isl_pw_aff_union_max.
6455 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6456 int max)
6458 int i;
6459 isl_pw_aff *pwaff;
6460 unsigned n_out;
6462 n_out = isl_map_dim(map, isl_dim_out);
6463 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6464 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6465 if (!map)
6466 return NULL;
6468 if (map->n == 0) {
6469 isl_space *dim = isl_map_get_space(map);
6470 isl_map_free(map);
6471 return isl_pw_aff_empty(dim);
6474 pwaff = basic_map_dim_opt(map->p[0], max);
6475 for (i = 1; i < map->n; ++i) {
6476 isl_pw_aff *pwaff_i;
6478 pwaff_i = basic_map_dim_opt(map->p[i], max);
6479 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6482 isl_map_free(map);
6484 return pwaff;
6487 /* Compute the minimum of the given output dimension as a function of the
6488 * parameters and input dimensions, but independently of
6489 * the other output dimensions.
6491 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6493 return map_dim_opt(map, pos, 0);
6496 /* Compute the maximum of the given output dimension as a function of the
6497 * parameters and input dimensions, but independently of
6498 * the other output dimensions.
6500 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6502 return map_dim_opt(map, pos, 1);
6505 /* Compute the minimum or maximum of the given set dimension
6506 * as a function of the parameters,
6507 * but independently of the other set dimensions.
6509 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6510 int max)
6512 return map_dim_opt(set, pos, max);
6515 /* Compute the maximum of the given set dimension as a function of the
6516 * parameters, but independently of the other set dimensions.
6518 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6520 return set_dim_opt(set, pos, 1);
6523 /* Compute the minimum of the given set dimension as a function of the
6524 * parameters, but independently of the other set dimensions.
6526 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6528 return set_dim_opt(set, pos, 0);
6531 /* Apply a preimage specified by "mat" on the parameters of "bset".
6532 * bset is assumed to have only parameters and divs.
6534 static struct isl_basic_set *basic_set_parameter_preimage(
6535 struct isl_basic_set *bset, struct isl_mat *mat)
6537 unsigned nparam;
6539 if (!bset || !mat)
6540 goto error;
6542 bset->dim = isl_space_cow(bset->dim);
6543 if (!bset->dim)
6544 goto error;
6546 nparam = isl_basic_set_dim(bset, isl_dim_param);
6548 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6550 bset->dim->nparam = 0;
6551 bset->dim->n_out = nparam;
6552 bset = isl_basic_set_preimage(bset, mat);
6553 if (bset) {
6554 bset->dim->nparam = bset->dim->n_out;
6555 bset->dim->n_out = 0;
6557 return bset;
6558 error:
6559 isl_mat_free(mat);
6560 isl_basic_set_free(bset);
6561 return NULL;
6564 /* Apply a preimage specified by "mat" on the parameters of "set".
6565 * set is assumed to have only parameters and divs.
6567 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6568 __isl_take isl_mat *mat)
6570 isl_space *space;
6571 unsigned nparam;
6573 if (!set || !mat)
6574 goto error;
6576 nparam = isl_set_dim(set, isl_dim_param);
6578 if (mat->n_row != 1 + nparam)
6579 isl_die(isl_set_get_ctx(set), isl_error_internal,
6580 "unexpected number of rows", goto error);
6582 space = isl_set_get_space(set);
6583 space = isl_space_move_dims(space, isl_dim_set, 0,
6584 isl_dim_param, 0, nparam);
6585 set = isl_set_reset_space(set, space);
6586 set = isl_set_preimage(set, mat);
6587 nparam = isl_set_dim(set, isl_dim_out);
6588 space = isl_set_get_space(set);
6589 space = isl_space_move_dims(space, isl_dim_param, 0,
6590 isl_dim_out, 0, nparam);
6591 set = isl_set_reset_space(set, space);
6592 return set;
6593 error:
6594 isl_mat_free(mat);
6595 isl_set_free(set);
6596 return NULL;
6599 /* Intersect the basic set "bset" with the affine space specified by the
6600 * equalities in "eq".
6602 static struct isl_basic_set *basic_set_append_equalities(
6603 struct isl_basic_set *bset, struct isl_mat *eq)
6605 int i, k;
6606 unsigned len;
6608 if (!bset || !eq)
6609 goto error;
6611 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6612 eq->n_row, 0);
6613 if (!bset)
6614 goto error;
6616 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6617 for (i = 0; i < eq->n_row; ++i) {
6618 k = isl_basic_set_alloc_equality(bset);
6619 if (k < 0)
6620 goto error;
6621 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6622 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6624 isl_mat_free(eq);
6626 bset = isl_basic_set_gauss(bset, NULL);
6627 bset = isl_basic_set_finalize(bset);
6629 return bset;
6630 error:
6631 isl_mat_free(eq);
6632 isl_basic_set_free(bset);
6633 return NULL;
6636 /* Intersect the set "set" with the affine space specified by the
6637 * equalities in "eq".
6639 static struct isl_set *set_append_equalities(struct isl_set *set,
6640 struct isl_mat *eq)
6642 int i;
6644 if (!set || !eq)
6645 goto error;
6647 for (i = 0; i < set->n; ++i) {
6648 set->p[i] = basic_set_append_equalities(set->p[i],
6649 isl_mat_copy(eq));
6650 if (!set->p[i])
6651 goto error;
6653 isl_mat_free(eq);
6654 return set;
6655 error:
6656 isl_mat_free(eq);
6657 isl_set_free(set);
6658 return NULL;
6661 /* Given a basic set "bset" that only involves parameters and existentially
6662 * quantified variables, return the index of the first equality
6663 * that only involves parameters. If there is no such equality then
6664 * return bset->n_eq.
6666 * This function assumes that isl_basic_set_gauss has been called on "bset".
6668 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6670 int i, j;
6671 unsigned nparam, n_div;
6673 if (!bset)
6674 return -1;
6676 nparam = isl_basic_set_dim(bset, isl_dim_param);
6677 n_div = isl_basic_set_dim(bset, isl_dim_div);
6679 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6680 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6681 ++i;
6684 return i;
6687 /* Compute an explicit representation for the existentially quantified
6688 * variables in "bset" by computing the "minimal value" of the set
6689 * variables. Since there are no set variables, the computation of
6690 * the minimal value essentially computes an explicit representation
6691 * of the non-empty part(s) of "bset".
6693 * The input only involves parameters and existentially quantified variables.
6694 * All equalities among parameters have been removed.
6696 * Since the existentially quantified variables in the result are in general
6697 * going to be different from those in the input, we first replace
6698 * them by the minimal number of variables based on their equalities.
6699 * This should simplify the parametric integer programming.
6701 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6703 isl_morph *morph1, *morph2;
6704 isl_set *set;
6705 unsigned n;
6707 if (!bset)
6708 return NULL;
6709 if (bset->n_eq == 0)
6710 return isl_basic_set_lexmin_compute_divs(bset);
6712 morph1 = isl_basic_set_parameter_compression(bset);
6713 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6714 bset = isl_basic_set_lift(bset);
6715 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6716 bset = isl_morph_basic_set(morph2, bset);
6717 n = isl_basic_set_dim(bset, isl_dim_set);
6718 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6720 set = isl_basic_set_lexmin_compute_divs(bset);
6722 set = isl_morph_set(isl_morph_inverse(morph1), set);
6724 return set;
6727 /* Project the given basic set onto its parameter domain, possibly introducing
6728 * new, explicit, existential variables in the constraints.
6729 * The input has parameters and (possibly implicit) existential variables.
6730 * The output has the same parameters, but only
6731 * explicit existentially quantified variables.
6733 * The actual projection is performed by pip, but pip doesn't seem
6734 * to like equalities very much, so we first remove the equalities
6735 * among the parameters by performing a variable compression on
6736 * the parameters. Afterward, an inverse transformation is performed
6737 * and the equalities among the parameters are inserted back in.
6739 * The variable compression on the parameters may uncover additional
6740 * equalities that were only implicit before. We therefore check
6741 * if there are any new parameter equalities in the result and
6742 * if so recurse. The removal of parameter equalities is required
6743 * for the parameter compression performed by base_compute_divs.
6745 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6747 int i;
6748 struct isl_mat *eq;
6749 struct isl_mat *T, *T2;
6750 struct isl_set *set;
6751 unsigned nparam;
6753 bset = isl_basic_set_cow(bset);
6754 if (!bset)
6755 return NULL;
6757 if (bset->n_eq == 0)
6758 return base_compute_divs(bset);
6760 bset = isl_basic_set_gauss(bset, NULL);
6761 if (!bset)
6762 return NULL;
6763 if (isl_basic_set_plain_is_empty(bset))
6764 return isl_set_from_basic_set(bset);
6766 i = first_parameter_equality(bset);
6767 if (i == bset->n_eq)
6768 return base_compute_divs(bset);
6770 nparam = isl_basic_set_dim(bset, isl_dim_param);
6771 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6772 0, 1 + nparam);
6773 eq = isl_mat_cow(eq);
6774 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6775 if (T && T->n_col == 0) {
6776 isl_mat_free(T);
6777 isl_mat_free(T2);
6778 isl_mat_free(eq);
6779 bset = isl_basic_set_set_to_empty(bset);
6780 return isl_set_from_basic_set(bset);
6782 bset = basic_set_parameter_preimage(bset, T);
6784 i = first_parameter_equality(bset);
6785 if (!bset)
6786 set = NULL;
6787 else if (i == bset->n_eq)
6788 set = base_compute_divs(bset);
6789 else
6790 set = parameter_compute_divs(bset);
6791 set = set_parameter_preimage(set, T2);
6792 set = set_append_equalities(set, eq);
6793 return set;
6796 /* Insert the divs from "ls" before those of "bmap".
6798 * The number of columns is not changed, which means that the last
6799 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6800 * The caller is responsible for removing the same number of dimensions
6801 * from the space of "bmap".
6803 static __isl_give isl_basic_map *insert_divs_from_local_space(
6804 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6806 int i;
6807 int n_div;
6808 int old_n_div;
6810 n_div = isl_local_space_dim(ls, isl_dim_div);
6811 if (n_div == 0)
6812 return bmap;
6814 old_n_div = bmap->n_div;
6815 bmap = insert_div_rows(bmap, n_div);
6816 if (!bmap)
6817 return NULL;
6819 for (i = 0; i < n_div; ++i) {
6820 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6821 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6824 return bmap;
6827 /* Replace the space of "bmap" by the space and divs of "ls".
6829 * If "ls" has any divs, then we simplify the result since we may
6830 * have discovered some additional equalities that could simplify
6831 * the div expressions.
6833 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6834 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6836 int n_div;
6838 bmap = isl_basic_map_cow(bmap);
6839 if (!bmap || !ls)
6840 goto error;
6842 n_div = isl_local_space_dim(ls, isl_dim_div);
6843 bmap = insert_divs_from_local_space(bmap, ls);
6844 if (!bmap)
6845 goto error;
6847 isl_space_free(bmap->dim);
6848 bmap->dim = isl_local_space_get_space(ls);
6849 if (!bmap->dim)
6850 goto error;
6852 isl_local_space_free(ls);
6853 if (n_div > 0)
6854 bmap = isl_basic_map_simplify(bmap);
6855 bmap = isl_basic_map_finalize(bmap);
6856 return bmap;
6857 error:
6858 isl_basic_map_free(bmap);
6859 isl_local_space_free(ls);
6860 return NULL;
6863 /* Replace the space of "map" by the space and divs of "ls".
6865 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6866 __isl_take isl_local_space *ls)
6868 int i;
6870 map = isl_map_cow(map);
6871 if (!map || !ls)
6872 goto error;
6874 for (i = 0; i < map->n; ++i) {
6875 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6876 isl_local_space_copy(ls));
6877 if (!map->p[i])
6878 goto error;
6880 isl_space_free(map->dim);
6881 map->dim = isl_local_space_get_space(ls);
6882 if (!map->dim)
6883 goto error;
6885 isl_local_space_free(ls);
6886 return map;
6887 error:
6888 isl_local_space_free(ls);
6889 isl_map_free(map);
6890 return NULL;
6893 /* Compute an explicit representation for the existentially
6894 * quantified variables for which do not know any explicit representation yet.
6896 * We first sort the existentially quantified variables so that the
6897 * existentially quantified variables for which we already have an explicit
6898 * representation are placed before those for which we do not.
6899 * The input dimensions, the output dimensions and the existentially
6900 * quantified variables for which we already have an explicit
6901 * representation are then turned into parameters.
6902 * compute_divs returns a map with the same parameters and
6903 * no input or output dimensions and the dimension specification
6904 * is reset to that of the input, including the existentially quantified
6905 * variables for which we already had an explicit representation.
6907 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6909 struct isl_basic_set *bset;
6910 struct isl_set *set;
6911 struct isl_map *map;
6912 isl_space *dim;
6913 isl_local_space *ls;
6914 unsigned nparam;
6915 unsigned n_in;
6916 unsigned n_out;
6917 int n_known;
6918 int i;
6920 bmap = isl_basic_map_sort_divs(bmap);
6921 bmap = isl_basic_map_cow(bmap);
6922 if (!bmap)
6923 return NULL;
6925 n_known = isl_basic_map_first_unknown_div(bmap);
6926 if (n_known < 0)
6927 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6929 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6930 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6931 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6932 dim = isl_space_set_alloc(bmap->ctx,
6933 nparam + n_in + n_out + n_known, 0);
6934 if (!dim)
6935 goto error;
6937 ls = isl_basic_map_get_local_space(bmap);
6938 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6939 n_known, bmap->n_div - n_known);
6940 if (n_known > 0) {
6941 for (i = n_known; i < bmap->n_div; ++i)
6942 swap_div(bmap, i - n_known, i);
6943 bmap->n_div -= n_known;
6944 bmap->extra -= n_known;
6946 bmap = isl_basic_map_reset_space(bmap, dim);
6947 bset = bset_from_bmap(bmap);
6949 set = parameter_compute_divs(bset);
6950 map = set_to_map(set);
6951 map = replace_space_by_local_space(map, ls);
6953 return map;
6954 error:
6955 isl_basic_map_free(bmap);
6956 return NULL;
6959 /* Remove the explicit representation of local variable "div",
6960 * if there is any.
6962 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6963 __isl_take isl_basic_map *bmap, int div)
6965 isl_bool unknown;
6967 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6968 if (unknown < 0)
6969 return isl_basic_map_free(bmap);
6970 if (unknown)
6971 return bmap;
6973 bmap = isl_basic_map_cow(bmap);
6974 if (!bmap)
6975 return NULL;
6976 isl_int_set_si(bmap->div[div][0], 0);
6977 return bmap;
6980 /* Is local variable "div" of "bmap" marked as not having an explicit
6981 * representation?
6982 * Note that even if "div" is not marked in this way and therefore
6983 * has an explicit representation, this representation may still
6984 * depend (indirectly) on other local variables that do not
6985 * have an explicit representation.
6987 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6988 int div)
6990 if (!bmap)
6991 return isl_bool_error;
6992 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6993 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6994 "position out of bounds", return isl_bool_error);
6995 return isl_int_is_zero(bmap->div[div][0]);
6998 /* Return the position of the first local variable that does not
6999 * have an explicit representation.
7000 * Return the total number of local variables if they all have
7001 * an explicit representation.
7002 * Return -1 on error.
7004 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7006 int i;
7008 if (!bmap)
7009 return -1;
7011 for (i = 0; i < bmap->n_div; ++i) {
7012 if (!isl_basic_map_div_is_known(bmap, i))
7013 return i;
7015 return bmap->n_div;
7018 /* Return the position of the first local variable that does not
7019 * have an explicit representation.
7020 * Return the total number of local variables if they all have
7021 * an explicit representation.
7022 * Return -1 on error.
7024 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7026 return isl_basic_map_first_unknown_div(bset);
7029 /* Does "bmap" have an explicit representation for all local variables?
7031 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7033 int first, n;
7035 n = isl_basic_map_dim(bmap, isl_dim_div);
7036 first = isl_basic_map_first_unknown_div(bmap);
7037 if (first < 0)
7038 return isl_bool_error;
7039 return first == n;
7042 /* Do all basic maps in "map" have an explicit representation
7043 * for all local variables?
7045 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7047 int i;
7049 if (!map)
7050 return isl_bool_error;
7052 for (i = 0; i < map->n; ++i) {
7053 int known = isl_basic_map_divs_known(map->p[i]);
7054 if (known <= 0)
7055 return known;
7058 return isl_bool_true;
7061 /* If bmap contains any unknown divs, then compute explicit
7062 * expressions for them. However, this computation may be
7063 * quite expensive, so first try to remove divs that aren't
7064 * strictly needed.
7066 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7068 int known;
7069 struct isl_map *map;
7071 known = isl_basic_map_divs_known(bmap);
7072 if (known < 0)
7073 goto error;
7074 if (known)
7075 return isl_map_from_basic_map(bmap);
7077 bmap = isl_basic_map_drop_redundant_divs(bmap);
7079 known = isl_basic_map_divs_known(bmap);
7080 if (known < 0)
7081 goto error;
7082 if (known)
7083 return isl_map_from_basic_map(bmap);
7085 map = compute_divs(bmap);
7086 return map;
7087 error:
7088 isl_basic_map_free(bmap);
7089 return NULL;
7092 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7094 int i;
7095 int known;
7096 struct isl_map *res;
7098 if (!map)
7099 return NULL;
7100 if (map->n == 0)
7101 return map;
7103 known = isl_map_divs_known(map);
7104 if (known < 0) {
7105 isl_map_free(map);
7106 return NULL;
7108 if (known)
7109 return map;
7111 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7112 for (i = 1 ; i < map->n; ++i) {
7113 struct isl_map *r2;
7114 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7115 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7116 res = isl_map_union_disjoint(res, r2);
7117 else
7118 res = isl_map_union(res, r2);
7120 isl_map_free(map);
7122 return res;
7125 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7127 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7130 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7132 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7135 struct isl_set *isl_map_domain(struct isl_map *map)
7137 int i;
7138 struct isl_set *set;
7140 if (!map)
7141 goto error;
7143 map = isl_map_cow(map);
7144 if (!map)
7145 return NULL;
7147 set = set_from_map(map);
7148 set->dim = isl_space_domain(set->dim);
7149 if (!set->dim)
7150 goto error;
7151 for (i = 0; i < map->n; ++i) {
7152 set->p[i] = isl_basic_map_domain(map->p[i]);
7153 if (!set->p[i])
7154 goto error;
7156 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7157 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7158 return set;
7159 error:
7160 isl_map_free(map);
7161 return NULL;
7164 /* Return the union of "map1" and "map2", where we assume for now that
7165 * "map1" and "map2" are disjoint. Note that the basic maps inside
7166 * "map1" or "map2" may not be disjoint from each other.
7167 * Also note that this function is also called from isl_map_union,
7168 * which takes care of handling the situation where "map1" and "map2"
7169 * may not be disjoint.
7171 * If one of the inputs is empty, we can simply return the other input.
7172 * Similarly, if one of the inputs is universal, then it is equal to the union.
7174 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7175 __isl_take isl_map *map2)
7177 int i;
7178 unsigned flags = 0;
7179 struct isl_map *map = NULL;
7180 int is_universe;
7182 if (!map1 || !map2)
7183 goto error;
7185 if (!isl_space_is_equal(map1->dim, map2->dim))
7186 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7187 "spaces don't match", goto error);
7189 if (map1->n == 0) {
7190 isl_map_free(map1);
7191 return map2;
7193 if (map2->n == 0) {
7194 isl_map_free(map2);
7195 return map1;
7198 is_universe = isl_map_plain_is_universe(map1);
7199 if (is_universe < 0)
7200 goto error;
7201 if (is_universe) {
7202 isl_map_free(map2);
7203 return map1;
7206 is_universe = isl_map_plain_is_universe(map2);
7207 if (is_universe < 0)
7208 goto error;
7209 if (is_universe) {
7210 isl_map_free(map1);
7211 return map2;
7214 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7215 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7216 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7218 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7219 map1->n + map2->n, flags);
7220 if (!map)
7221 goto error;
7222 for (i = 0; i < map1->n; ++i) {
7223 map = isl_map_add_basic_map(map,
7224 isl_basic_map_copy(map1->p[i]));
7225 if (!map)
7226 goto error;
7228 for (i = 0; i < map2->n; ++i) {
7229 map = isl_map_add_basic_map(map,
7230 isl_basic_map_copy(map2->p[i]));
7231 if (!map)
7232 goto error;
7234 isl_map_free(map1);
7235 isl_map_free(map2);
7236 return map;
7237 error:
7238 isl_map_free(map);
7239 isl_map_free(map1);
7240 isl_map_free(map2);
7241 return NULL;
7244 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7245 * guaranteed to be disjoint by the caller.
7247 * Note that this functions is called from within isl_map_make_disjoint,
7248 * so we have to be careful not to touch the constraints of the inputs
7249 * in any way.
7251 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7252 __isl_take isl_map *map2)
7254 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7257 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7258 * not be disjoint. The parameters are assumed to have been aligned.
7260 * We currently simply call map_union_disjoint, the internal operation
7261 * of which does not really depend on the inputs being disjoint.
7262 * If the result contains more than one basic map, then we clear
7263 * the disjoint flag since the result may contain basic maps from
7264 * both inputs and these are not guaranteed to be disjoint.
7266 * As a special case, if "map1" and "map2" are obviously equal,
7267 * then we simply return "map1".
7269 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7270 __isl_take isl_map *map2)
7272 int equal;
7274 if (!map1 || !map2)
7275 goto error;
7277 equal = isl_map_plain_is_equal(map1, map2);
7278 if (equal < 0)
7279 goto error;
7280 if (equal) {
7281 isl_map_free(map2);
7282 return map1;
7285 map1 = map_union_disjoint(map1, map2);
7286 if (!map1)
7287 return NULL;
7288 if (map1->n > 1)
7289 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7290 return map1;
7291 error:
7292 isl_map_free(map1);
7293 isl_map_free(map2);
7294 return NULL;
7297 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7298 * not be disjoint.
7300 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7301 __isl_take isl_map *map2)
7303 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7306 struct isl_set *isl_set_union_disjoint(
7307 struct isl_set *set1, struct isl_set *set2)
7309 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7310 set_to_map(set2)));
7313 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7315 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7318 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7319 * the results.
7321 * "map" and "set" are assumed to be compatible and non-NULL.
7323 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7324 __isl_take isl_set *set,
7325 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7326 __isl_take isl_basic_set *bset))
7328 unsigned flags = 0;
7329 struct isl_map *result;
7330 int i, j;
7332 if (isl_set_plain_is_universe(set)) {
7333 isl_set_free(set);
7334 return map;
7337 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7338 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7339 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7341 result = isl_map_alloc_space(isl_space_copy(map->dim),
7342 map->n * set->n, flags);
7343 for (i = 0; result && i < map->n; ++i)
7344 for (j = 0; j < set->n; ++j) {
7345 result = isl_map_add_basic_map(result,
7346 fn(isl_basic_map_copy(map->p[i]),
7347 isl_basic_set_copy(set->p[j])));
7348 if (!result)
7349 break;
7352 isl_map_free(map);
7353 isl_set_free(set);
7354 return result;
7357 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7358 __isl_take isl_set *set)
7360 if (!map || !set)
7361 goto error;
7363 if (!isl_map_compatible_range(map, set))
7364 isl_die(set->ctx, isl_error_invalid,
7365 "incompatible spaces", goto error);
7367 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7368 error:
7369 isl_map_free(map);
7370 isl_set_free(set);
7371 return NULL;
7374 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7375 __isl_take isl_set *set)
7377 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7380 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7381 __isl_take isl_set *set)
7383 if (!map || !set)
7384 goto error;
7386 if (!isl_map_compatible_domain(map, set))
7387 isl_die(set->ctx, isl_error_invalid,
7388 "incompatible spaces", goto error);
7390 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7391 error:
7392 isl_map_free(map);
7393 isl_set_free(set);
7394 return NULL;
7397 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7398 __isl_take isl_set *set)
7400 return isl_map_align_params_map_map_and(map, set,
7401 &map_intersect_domain);
7404 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7405 __isl_take isl_map *map2)
7407 if (!map1 || !map2)
7408 goto error;
7409 map1 = isl_map_reverse(map1);
7410 map1 = isl_map_apply_range(map1, map2);
7411 return isl_map_reverse(map1);
7412 error:
7413 isl_map_free(map1);
7414 isl_map_free(map2);
7415 return NULL;
7418 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7419 __isl_take isl_map *map2)
7421 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7424 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7425 __isl_take isl_map *map2)
7427 isl_space *dim_result;
7428 struct isl_map *result;
7429 int i, j;
7431 if (!map1 || !map2)
7432 goto error;
7434 dim_result = isl_space_join(isl_space_copy(map1->dim),
7435 isl_space_copy(map2->dim));
7437 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7438 if (!result)
7439 goto error;
7440 for (i = 0; i < map1->n; ++i)
7441 for (j = 0; j < map2->n; ++j) {
7442 result = isl_map_add_basic_map(result,
7443 isl_basic_map_apply_range(
7444 isl_basic_map_copy(map1->p[i]),
7445 isl_basic_map_copy(map2->p[j])));
7446 if (!result)
7447 goto error;
7449 isl_map_free(map1);
7450 isl_map_free(map2);
7451 if (result && result->n <= 1)
7452 ISL_F_SET(result, ISL_MAP_DISJOINT);
7453 return result;
7454 error:
7455 isl_map_free(map1);
7456 isl_map_free(map2);
7457 return NULL;
7460 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7461 __isl_take isl_map *map2)
7463 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7467 * returns range - domain
7469 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7471 isl_space *target_space;
7472 struct isl_basic_set *bset;
7473 unsigned dim;
7474 unsigned nparam;
7475 int i;
7477 if (!bmap)
7478 goto error;
7479 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7480 bmap->dim, isl_dim_out),
7481 goto error);
7482 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7483 dim = isl_basic_map_n_in(bmap);
7484 nparam = isl_basic_map_n_param(bmap);
7485 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7486 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7487 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7488 for (i = 0; i < dim; ++i) {
7489 int j = isl_basic_map_alloc_equality(bmap);
7490 if (j < 0) {
7491 bmap = isl_basic_map_free(bmap);
7492 break;
7494 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7495 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7496 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7497 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7499 bset = isl_basic_map_domain(bmap);
7500 bset = isl_basic_set_reset_space(bset, target_space);
7501 return bset;
7502 error:
7503 isl_basic_map_free(bmap);
7504 return NULL;
7508 * returns range - domain
7510 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7512 int i;
7513 isl_space *dim;
7514 struct isl_set *result;
7516 if (!map)
7517 return NULL;
7519 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7520 map->dim, isl_dim_out),
7521 goto error);
7522 dim = isl_map_get_space(map);
7523 dim = isl_space_domain(dim);
7524 result = isl_set_alloc_space(dim, map->n, 0);
7525 if (!result)
7526 goto error;
7527 for (i = 0; i < map->n; ++i)
7528 result = isl_set_add_basic_set(result,
7529 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7530 isl_map_free(map);
7531 return result;
7532 error:
7533 isl_map_free(map);
7534 return NULL;
7538 * returns [domain -> range] -> range - domain
7540 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7541 __isl_take isl_basic_map *bmap)
7543 int i, k;
7544 isl_space *dim;
7545 isl_basic_map *domain;
7546 int nparam, n;
7547 unsigned total;
7549 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7550 bmap->dim, isl_dim_out))
7551 isl_die(bmap->ctx, isl_error_invalid,
7552 "domain and range don't match", goto error);
7554 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7555 n = isl_basic_map_dim(bmap, isl_dim_in);
7557 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7558 domain = isl_basic_map_universe(dim);
7560 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7561 bmap = isl_basic_map_apply_range(bmap, domain);
7562 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7564 total = isl_basic_map_total_dim(bmap);
7566 for (i = 0; i < n; ++i) {
7567 k = isl_basic_map_alloc_equality(bmap);
7568 if (k < 0)
7569 goto error;
7570 isl_seq_clr(bmap->eq[k], 1 + total);
7571 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7572 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7573 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7576 bmap = isl_basic_map_gauss(bmap, NULL);
7577 return isl_basic_map_finalize(bmap);
7578 error:
7579 isl_basic_map_free(bmap);
7580 return NULL;
7584 * returns [domain -> range] -> range - domain
7586 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7588 int i;
7589 isl_space *domain_dim;
7591 if (!map)
7592 return NULL;
7594 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7595 map->dim, isl_dim_out))
7596 isl_die(map->ctx, isl_error_invalid,
7597 "domain and range don't match", goto error);
7599 map = isl_map_cow(map);
7600 if (!map)
7601 return NULL;
7603 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7604 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7605 map->dim = isl_space_join(map->dim, domain_dim);
7606 if (!map->dim)
7607 goto error;
7608 for (i = 0; i < map->n; ++i) {
7609 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7610 if (!map->p[i])
7611 goto error;
7613 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7614 return map;
7615 error:
7616 isl_map_free(map);
7617 return NULL;
7620 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7622 struct isl_basic_map *bmap;
7623 unsigned nparam;
7624 unsigned dim;
7625 int i;
7627 if (!dims)
7628 return NULL;
7630 nparam = dims->nparam;
7631 dim = dims->n_out;
7632 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7633 if (!bmap)
7634 goto error;
7636 for (i = 0; i < dim; ++i) {
7637 int j = isl_basic_map_alloc_equality(bmap);
7638 if (j < 0)
7639 goto error;
7640 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7641 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7642 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7644 return isl_basic_map_finalize(bmap);
7645 error:
7646 isl_basic_map_free(bmap);
7647 return NULL;
7650 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7652 if (!dim)
7653 return NULL;
7654 if (dim->n_in != dim->n_out)
7655 isl_die(dim->ctx, isl_error_invalid,
7656 "number of input and output dimensions needs to be "
7657 "the same", goto error);
7658 return basic_map_identity(dim);
7659 error:
7660 isl_space_free(dim);
7661 return NULL;
7664 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7666 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7669 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7671 isl_space *dim = isl_set_get_space(set);
7672 isl_map *id;
7673 id = isl_map_identity(isl_space_map_from_set(dim));
7674 return isl_map_intersect_range(id, set);
7677 /* Construct a basic set with all set dimensions having only non-negative
7678 * values.
7680 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7681 __isl_take isl_space *space)
7683 int i;
7684 unsigned nparam;
7685 unsigned dim;
7686 struct isl_basic_set *bset;
7688 if (!space)
7689 return NULL;
7690 nparam = space->nparam;
7691 dim = space->n_out;
7692 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7693 if (!bset)
7694 return NULL;
7695 for (i = 0; i < dim; ++i) {
7696 int k = isl_basic_set_alloc_inequality(bset);
7697 if (k < 0)
7698 goto error;
7699 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7700 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7702 return bset;
7703 error:
7704 isl_basic_set_free(bset);
7705 return NULL;
7708 /* Construct the half-space x_pos >= 0.
7710 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7711 int pos)
7713 int k;
7714 isl_basic_set *nonneg;
7716 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7717 k = isl_basic_set_alloc_inequality(nonneg);
7718 if (k < 0)
7719 goto error;
7720 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7721 isl_int_set_si(nonneg->ineq[k][pos], 1);
7723 return isl_basic_set_finalize(nonneg);
7724 error:
7725 isl_basic_set_free(nonneg);
7726 return NULL;
7729 /* Construct the half-space x_pos <= -1.
7731 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7733 int k;
7734 isl_basic_set *neg;
7736 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7737 k = isl_basic_set_alloc_inequality(neg);
7738 if (k < 0)
7739 goto error;
7740 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7741 isl_int_set_si(neg->ineq[k][0], -1);
7742 isl_int_set_si(neg->ineq[k][pos], -1);
7744 return isl_basic_set_finalize(neg);
7745 error:
7746 isl_basic_set_free(neg);
7747 return NULL;
7750 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7751 enum isl_dim_type type, unsigned first, unsigned n)
7753 int i;
7754 unsigned offset;
7755 isl_basic_set *nonneg;
7756 isl_basic_set *neg;
7758 if (!set)
7759 return NULL;
7760 if (n == 0)
7761 return set;
7763 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7765 offset = pos(set->dim, type);
7766 for (i = 0; i < n; ++i) {
7767 nonneg = nonneg_halfspace(isl_set_get_space(set),
7768 offset + first + i);
7769 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7771 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7774 return set;
7775 error:
7776 isl_set_free(set);
7777 return NULL;
7780 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7781 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7782 void *user)
7784 isl_set *half;
7786 if (!set)
7787 return -1;
7788 if (isl_set_plain_is_empty(set)) {
7789 isl_set_free(set);
7790 return 0;
7792 if (first == len)
7793 return fn(set, signs, user);
7795 signs[first] = 1;
7796 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7797 1 + first));
7798 half = isl_set_intersect(half, isl_set_copy(set));
7799 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7800 goto error;
7802 signs[first] = -1;
7803 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7804 1 + first));
7805 half = isl_set_intersect(half, set);
7806 return foreach_orthant(half, signs, first + 1, len, fn, user);
7807 error:
7808 isl_set_free(set);
7809 return -1;
7812 /* Call "fn" on the intersections of "set" with each of the orthants
7813 * (except for obviously empty intersections). The orthant is identified
7814 * by the signs array, with each entry having value 1 or -1 according
7815 * to the sign of the corresponding variable.
7817 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7818 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7819 void *user)
7821 unsigned nparam;
7822 unsigned nvar;
7823 int *signs;
7824 int r;
7826 if (!set)
7827 return -1;
7828 if (isl_set_plain_is_empty(set))
7829 return 0;
7831 nparam = isl_set_dim(set, isl_dim_param);
7832 nvar = isl_set_dim(set, isl_dim_set);
7834 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7836 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7837 fn, user);
7839 free(signs);
7841 return r;
7844 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7846 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7849 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7850 __isl_keep isl_basic_map *bmap2)
7852 int is_subset;
7853 struct isl_map *map1;
7854 struct isl_map *map2;
7856 if (!bmap1 || !bmap2)
7857 return isl_bool_error;
7859 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7860 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7862 is_subset = isl_map_is_subset(map1, map2);
7864 isl_map_free(map1);
7865 isl_map_free(map2);
7867 return is_subset;
7870 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7871 __isl_keep isl_basic_set *bset2)
7873 return isl_basic_map_is_subset(bset1, bset2);
7876 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7877 __isl_keep isl_basic_map *bmap2)
7879 isl_bool is_subset;
7881 if (!bmap1 || !bmap2)
7882 return isl_bool_error;
7883 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7884 if (is_subset != isl_bool_true)
7885 return is_subset;
7886 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7887 return is_subset;
7890 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7891 __isl_keep isl_basic_set *bset2)
7893 return isl_basic_map_is_equal(
7894 bset_to_bmap(bset1), bset_to_bmap(bset2));
7897 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7899 int i;
7900 int is_empty;
7902 if (!map)
7903 return isl_bool_error;
7904 for (i = 0; i < map->n; ++i) {
7905 is_empty = isl_basic_map_is_empty(map->p[i]);
7906 if (is_empty < 0)
7907 return isl_bool_error;
7908 if (!is_empty)
7909 return isl_bool_false;
7911 return isl_bool_true;
7914 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7916 return map ? map->n == 0 : isl_bool_error;
7919 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7921 return set ? set->n == 0 : isl_bool_error;
7924 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7926 return isl_map_is_empty(set_to_map(set));
7929 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7931 if (!map1 || !map2)
7932 return -1;
7934 return isl_space_is_equal(map1->dim, map2->dim);
7937 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7939 if (!set1 || !set2)
7940 return -1;
7942 return isl_space_is_equal(set1->dim, set2->dim);
7945 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7947 isl_bool is_subset;
7949 if (!map1 || !map2)
7950 return isl_bool_error;
7951 is_subset = isl_map_is_subset(map1, map2);
7952 if (is_subset != isl_bool_true)
7953 return is_subset;
7954 is_subset = isl_map_is_subset(map2, map1);
7955 return is_subset;
7958 /* Is "map1" equal to "map2"?
7960 * First check if they are obviously equal.
7961 * If not, then perform a more detailed analysis.
7963 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7965 isl_bool equal;
7967 equal = isl_map_plain_is_equal(map1, map2);
7968 if (equal < 0 || equal)
7969 return equal;
7970 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7973 isl_bool isl_basic_map_is_strict_subset(
7974 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7976 isl_bool is_subset;
7978 if (!bmap1 || !bmap2)
7979 return isl_bool_error;
7980 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7981 if (is_subset != isl_bool_true)
7982 return is_subset;
7983 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7984 if (is_subset == isl_bool_error)
7985 return is_subset;
7986 return !is_subset;
7989 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7990 __isl_keep isl_map *map2)
7992 isl_bool is_subset;
7994 if (!map1 || !map2)
7995 return isl_bool_error;
7996 is_subset = isl_map_is_subset(map1, map2);
7997 if (is_subset != isl_bool_true)
7998 return is_subset;
7999 is_subset = isl_map_is_subset(map2, map1);
8000 if (is_subset == isl_bool_error)
8001 return is_subset;
8002 return !is_subset;
8005 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8006 __isl_keep isl_set *set2)
8008 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8011 /* Is "bmap" obviously equal to the universe with the same space?
8013 * That is, does it not have any constraints?
8015 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8017 if (!bmap)
8018 return isl_bool_error;
8019 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8022 /* Is "bset" obviously equal to the universe with the same space?
8024 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8026 return isl_basic_map_plain_is_universe(bset);
8029 /* If "c" does not involve any existentially quantified variables,
8030 * then set *univ to false and abort
8032 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8034 isl_bool *univ = user;
8035 unsigned n;
8037 n = isl_constraint_dim(c, isl_dim_div);
8038 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8039 isl_constraint_free(c);
8040 if (*univ < 0 || !*univ)
8041 return isl_stat_error;
8042 return isl_stat_ok;
8045 /* Is "bmap" equal to the universe with the same space?
8047 * First check if it is obviously equal to the universe.
8048 * If not and if there are any constraints not involving
8049 * existentially quantified variables, then it is certainly
8050 * not equal to the universe.
8051 * Otherwise, check if the universe is a subset of "bmap".
8053 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8055 isl_bool univ;
8056 isl_basic_map *test;
8058 univ = isl_basic_map_plain_is_universe(bmap);
8059 if (univ < 0 || univ)
8060 return univ;
8061 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8062 return isl_bool_false;
8063 univ = isl_bool_true;
8064 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8065 univ)
8066 return isl_bool_error;
8067 if (univ < 0 || !univ)
8068 return univ;
8069 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8070 univ = isl_basic_map_is_subset(test, bmap);
8071 isl_basic_map_free(test);
8072 return univ;
8075 /* Is "bset" equal to the universe with the same space?
8077 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8079 return isl_basic_map_is_universe(bset);
8082 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8084 int i;
8086 if (!map)
8087 return isl_bool_error;
8089 for (i = 0; i < map->n; ++i) {
8090 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8091 if (r < 0 || r)
8092 return r;
8095 return isl_bool_false;
8098 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8100 return isl_map_plain_is_universe(set_to_map(set));
8103 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8105 struct isl_basic_set *bset = NULL;
8106 struct isl_vec *sample = NULL;
8107 isl_bool empty, non_empty;
8109 if (!bmap)
8110 return isl_bool_error;
8112 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8113 return isl_bool_true;
8115 if (isl_basic_map_plain_is_universe(bmap))
8116 return isl_bool_false;
8118 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8119 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8120 copy = isl_basic_map_remove_redundancies(copy);
8121 empty = isl_basic_map_plain_is_empty(copy);
8122 isl_basic_map_free(copy);
8123 return empty;
8126 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8127 if (non_empty < 0)
8128 return isl_bool_error;
8129 if (non_empty)
8130 return isl_bool_false;
8131 isl_vec_free(bmap->sample);
8132 bmap->sample = NULL;
8133 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8134 if (!bset)
8135 return isl_bool_error;
8136 sample = isl_basic_set_sample_vec(bset);
8137 if (!sample)
8138 return isl_bool_error;
8139 empty = sample->size == 0;
8140 isl_vec_free(bmap->sample);
8141 bmap->sample = sample;
8142 if (empty)
8143 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8145 return empty;
8148 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8150 if (!bmap)
8151 return isl_bool_error;
8152 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8155 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8157 if (!bset)
8158 return isl_bool_error;
8159 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8162 /* Is "bmap" known to be non-empty?
8164 * That is, is the cached sample still valid?
8166 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8168 unsigned total;
8170 if (!bmap)
8171 return isl_bool_error;
8172 if (!bmap->sample)
8173 return isl_bool_false;
8174 total = 1 + isl_basic_map_total_dim(bmap);
8175 if (bmap->sample->size != total)
8176 return isl_bool_false;
8177 return isl_basic_map_contains(bmap, bmap->sample);
8180 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8182 return isl_basic_map_is_empty(bset_to_bmap(bset));
8185 struct isl_map *isl_basic_map_union(
8186 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8188 struct isl_map *map;
8189 if (!bmap1 || !bmap2)
8190 goto error;
8192 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8194 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8195 if (!map)
8196 goto error;
8197 map = isl_map_add_basic_map(map, bmap1);
8198 map = isl_map_add_basic_map(map, bmap2);
8199 return map;
8200 error:
8201 isl_basic_map_free(bmap1);
8202 isl_basic_map_free(bmap2);
8203 return NULL;
8206 struct isl_set *isl_basic_set_union(
8207 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8209 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8210 bset_to_bmap(bset2)));
8213 /* Order divs such that any div only depends on previous divs */
8214 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8216 int i;
8217 unsigned off;
8219 if (!bmap)
8220 return NULL;
8222 off = isl_space_dim(bmap->dim, isl_dim_all);
8224 for (i = 0; i < bmap->n_div; ++i) {
8225 int pos;
8226 if (isl_int_is_zero(bmap->div[i][0]))
8227 continue;
8228 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8229 bmap->n_div-i);
8230 if (pos == -1)
8231 continue;
8232 if (pos == 0)
8233 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8234 "integer division depends on itself",
8235 return isl_basic_map_free(bmap));
8236 isl_basic_map_swap_div(bmap, i, i + pos);
8237 --i;
8239 return bmap;
8242 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8244 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8247 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8249 int i;
8251 if (!map)
8252 return 0;
8254 for (i = 0; i < map->n; ++i) {
8255 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8256 if (!map->p[i])
8257 goto error;
8260 return map;
8261 error:
8262 isl_map_free(map);
8263 return NULL;
8266 /* Sort the local variables of "bset".
8268 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8269 __isl_take isl_basic_set *bset)
8271 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8274 /* Apply the expansion computed by isl_merge_divs.
8275 * The expansion itself is given by "exp" while the resulting
8276 * list of divs is given by "div".
8278 * Move the integer divisions of "bmap" into the right position
8279 * according to "exp" and then introduce the additional integer
8280 * divisions, adding div constraints.
8281 * The moving should be done first to avoid moving coefficients
8282 * in the definitions of the extra integer divisions.
8284 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8285 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8287 int i, j;
8288 int n_div;
8290 bmap = isl_basic_map_cow(bmap);
8291 if (!bmap || !div)
8292 goto error;
8294 if (div->n_row < bmap->n_div)
8295 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8296 "not an expansion", goto error);
8298 n_div = bmap->n_div;
8299 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8300 div->n_row - n_div, 0,
8301 2 * (div->n_row - n_div));
8303 for (i = n_div; i < div->n_row; ++i)
8304 if (isl_basic_map_alloc_div(bmap) < 0)
8305 goto error;
8307 for (j = n_div - 1; j >= 0; --j) {
8308 if (exp[j] == j)
8309 break;
8310 isl_basic_map_swap_div(bmap, j, exp[j]);
8312 j = 0;
8313 for (i = 0; i < div->n_row; ++i) {
8314 if (j < n_div && exp[j] == i) {
8315 j++;
8316 } else {
8317 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8318 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8319 continue;
8320 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8321 goto error;
8325 isl_mat_free(div);
8326 return bmap;
8327 error:
8328 isl_basic_map_free(bmap);
8329 isl_mat_free(div);
8330 return NULL;
8333 /* Apply the expansion computed by isl_merge_divs.
8334 * The expansion itself is given by "exp" while the resulting
8335 * list of divs is given by "div".
8337 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8338 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8340 return isl_basic_map_expand_divs(bset, div, exp);
8343 /* Look for a div in dst that corresponds to the div "div" in src.
8344 * The divs before "div" in src and dst are assumed to be the same.
8346 * Returns -1 if no corresponding div was found and the position
8347 * of the corresponding div in dst otherwise.
8349 static int find_div(struct isl_basic_map *dst,
8350 struct isl_basic_map *src, unsigned div)
8352 int i;
8354 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8356 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8357 for (i = div; i < dst->n_div; ++i)
8358 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8359 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8360 dst->n_div - div) == -1)
8361 return i;
8362 return -1;
8365 /* Align the divs of "dst" to those of "src", adding divs from "src"
8366 * if needed. That is, make sure that the first src->n_div divs
8367 * of the result are equal to those of src.
8369 * The result is not finalized as by design it will have redundant
8370 * divs if any divs from "src" were copied.
8372 __isl_give isl_basic_map *isl_basic_map_align_divs(
8373 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8375 int i;
8376 int known, extended;
8377 unsigned total;
8379 if (!dst || !src)
8380 return isl_basic_map_free(dst);
8382 if (src->n_div == 0)
8383 return dst;
8385 known = isl_basic_map_divs_known(src);
8386 if (known < 0)
8387 return isl_basic_map_free(dst);
8388 if (!known)
8389 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8390 "some src divs are unknown",
8391 return isl_basic_map_free(dst));
8393 src = isl_basic_map_order_divs(src);
8395 extended = 0;
8396 total = isl_space_dim(src->dim, isl_dim_all);
8397 for (i = 0; i < src->n_div; ++i) {
8398 int j = find_div(dst, src, i);
8399 if (j < 0) {
8400 if (!extended) {
8401 int extra = src->n_div - i;
8402 dst = isl_basic_map_cow(dst);
8403 if (!dst)
8404 return NULL;
8405 dst = isl_basic_map_extend_space(dst,
8406 isl_space_copy(dst->dim),
8407 extra, 0, 2 * extra);
8408 extended = 1;
8410 j = isl_basic_map_alloc_div(dst);
8411 if (j < 0)
8412 return isl_basic_map_free(dst);
8413 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8414 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8415 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8416 return isl_basic_map_free(dst);
8418 if (j != i)
8419 isl_basic_map_swap_div(dst, i, j);
8421 return dst;
8424 struct isl_basic_set *isl_basic_set_align_divs(
8425 struct isl_basic_set *dst, struct isl_basic_set *src)
8427 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8428 bset_to_bmap(src)));
8431 struct isl_map *isl_map_align_divs(struct isl_map *map)
8433 int i;
8435 if (!map)
8436 return NULL;
8437 if (map->n == 0)
8438 return map;
8439 map = isl_map_compute_divs(map);
8440 map = isl_map_cow(map);
8441 if (!map)
8442 return NULL;
8444 for (i = 1; i < map->n; ++i)
8445 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8446 for (i = 1; i < map->n; ++i) {
8447 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8448 if (!map->p[i])
8449 return isl_map_free(map);
8452 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8453 return map;
8456 struct isl_set *isl_set_align_divs(struct isl_set *set)
8458 return set_from_map(isl_map_align_divs(set_to_map(set)));
8461 /* Align the divs of the basic maps in "map" to those
8462 * of the basic maps in "list", as well as to the other basic maps in "map".
8463 * The elements in "list" are assumed to have known divs.
8465 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8466 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8468 int i, n;
8470 map = isl_map_compute_divs(map);
8471 map = isl_map_cow(map);
8472 if (!map || !list)
8473 return isl_map_free(map);
8474 if (map->n == 0)
8475 return map;
8477 n = isl_basic_map_list_n_basic_map(list);
8478 for (i = 0; i < n; ++i) {
8479 isl_basic_map *bmap;
8481 bmap = isl_basic_map_list_get_basic_map(list, i);
8482 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8483 isl_basic_map_free(bmap);
8485 if (!map->p[0])
8486 return isl_map_free(map);
8488 return isl_map_align_divs(map);
8491 /* Align the divs of each element of "list" to those of "bmap".
8492 * Both "bmap" and the elements of "list" are assumed to have known divs.
8494 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8495 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8497 int i, n;
8499 if (!list || !bmap)
8500 return isl_basic_map_list_free(list);
8502 n = isl_basic_map_list_n_basic_map(list);
8503 for (i = 0; i < n; ++i) {
8504 isl_basic_map *bmap_i;
8506 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8507 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8508 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8511 return list;
8514 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8515 __isl_take isl_map *map)
8517 if (!set || !map)
8518 goto error;
8519 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8520 map = isl_map_intersect_domain(map, set);
8521 set = isl_map_range(map);
8522 return set;
8523 error:
8524 isl_set_free(set);
8525 isl_map_free(map);
8526 return NULL;
8529 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8530 __isl_take isl_map *map)
8532 return isl_map_align_params_map_map_and(set, map, &set_apply);
8535 /* There is no need to cow as removing empty parts doesn't change
8536 * the meaning of the set.
8538 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8540 int i;
8542 if (!map)
8543 return NULL;
8545 for (i = map->n - 1; i >= 0; --i)
8546 remove_if_empty(map, i);
8548 return map;
8551 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8553 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8556 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8558 struct isl_basic_map *bmap;
8559 if (!map || map->n == 0)
8560 return NULL;
8561 bmap = map->p[map->n-1];
8562 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8563 return isl_basic_map_copy(bmap);
8566 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8568 return map_copy_basic_map(map);
8571 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8573 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8576 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8577 __isl_keep isl_basic_map *bmap)
8579 int i;
8581 if (!map || !bmap)
8582 goto error;
8583 for (i = map->n-1; i >= 0; --i) {
8584 if (map->p[i] != bmap)
8585 continue;
8586 map = isl_map_cow(map);
8587 if (!map)
8588 goto error;
8589 isl_basic_map_free(map->p[i]);
8590 if (i != map->n-1) {
8591 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8592 map->p[i] = map->p[map->n-1];
8594 map->n--;
8595 return map;
8597 return map;
8598 error:
8599 isl_map_free(map);
8600 return NULL;
8603 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8604 __isl_keep isl_basic_map *bmap)
8606 return map_drop_basic_map(map, bmap);
8609 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8610 struct isl_basic_set *bset)
8612 return set_from_map(map_drop_basic_map(set_to_map(set),
8613 bset_to_bmap(bset)));
8616 /* Given two basic sets bset1 and bset2, compute the maximal difference
8617 * between the values of dimension pos in bset1 and those in bset2
8618 * for any common value of the parameters and dimensions preceding pos.
8620 static enum isl_lp_result basic_set_maximal_difference_at(
8621 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8622 int pos, isl_int *opt)
8624 isl_basic_map *bmap1;
8625 isl_basic_map *bmap2;
8626 struct isl_ctx *ctx;
8627 struct isl_vec *obj;
8628 unsigned total;
8629 unsigned nparam;
8630 unsigned dim1;
8631 enum isl_lp_result res;
8633 if (!bset1 || !bset2)
8634 return isl_lp_error;
8636 nparam = isl_basic_set_n_param(bset1);
8637 dim1 = isl_basic_set_n_dim(bset1);
8639 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8640 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8641 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8642 isl_dim_out, 0, pos);
8643 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8644 isl_dim_out, 0, pos);
8645 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8646 if (!bmap1)
8647 return isl_lp_error;
8649 total = isl_basic_map_total_dim(bmap1);
8650 ctx = bmap1->ctx;
8651 obj = isl_vec_alloc(ctx, 1 + total);
8652 if (!obj)
8653 goto error;
8654 isl_seq_clr(obj->block.data, 1 + total);
8655 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8656 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8657 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8658 opt, NULL, NULL);
8659 isl_basic_map_free(bmap1);
8660 isl_vec_free(obj);
8661 return res;
8662 error:
8663 isl_basic_map_free(bmap1);
8664 return isl_lp_error;
8667 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8668 * for any common value of the parameters and dimensions preceding pos
8669 * in both basic sets, the values of dimension pos in bset1 are
8670 * smaller or larger than those in bset2.
8672 * Returns
8673 * 1 if bset1 follows bset2
8674 * -1 if bset1 precedes bset2
8675 * 0 if bset1 and bset2 are incomparable
8676 * -2 if some error occurred.
8678 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8679 struct isl_basic_set *bset2, int pos)
8681 isl_int opt;
8682 enum isl_lp_result res;
8683 int cmp;
8685 isl_int_init(opt);
8687 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8689 if (res == isl_lp_empty)
8690 cmp = 0;
8691 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8692 res == isl_lp_unbounded)
8693 cmp = 1;
8694 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8695 cmp = -1;
8696 else
8697 cmp = -2;
8699 isl_int_clear(opt);
8700 return cmp;
8703 /* Given two basic sets bset1 and bset2, check whether
8704 * for any common value of the parameters and dimensions preceding pos
8705 * there is a value of dimension pos in bset1 that is larger
8706 * than a value of the same dimension in bset2.
8708 * Return
8709 * 1 if there exists such a pair
8710 * 0 if there is no such pair, but there is a pair of equal values
8711 * -1 otherwise
8712 * -2 if some error occurred.
8714 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8715 __isl_keep isl_basic_set *bset2, int pos)
8717 isl_int opt;
8718 enum isl_lp_result res;
8719 int cmp;
8721 isl_int_init(opt);
8723 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8725 if (res == isl_lp_empty)
8726 cmp = -1;
8727 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8728 res == isl_lp_unbounded)
8729 cmp = 1;
8730 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8731 cmp = -1;
8732 else if (res == isl_lp_ok)
8733 cmp = 0;
8734 else
8735 cmp = -2;
8737 isl_int_clear(opt);
8738 return cmp;
8741 /* Given two sets set1 and set2, check whether
8742 * for any common value of the parameters and dimensions preceding pos
8743 * there is a value of dimension pos in set1 that is larger
8744 * than a value of the same dimension in set2.
8746 * Return
8747 * 1 if there exists such a pair
8748 * 0 if there is no such pair, but there is a pair of equal values
8749 * -1 otherwise
8750 * -2 if some error occurred.
8752 int isl_set_follows_at(__isl_keep isl_set *set1,
8753 __isl_keep isl_set *set2, int pos)
8755 int i, j;
8756 int follows = -1;
8758 if (!set1 || !set2)
8759 return -2;
8761 for (i = 0; i < set1->n; ++i)
8762 for (j = 0; j < set2->n; ++j) {
8763 int f;
8764 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8765 if (f == 1 || f == -2)
8766 return f;
8767 if (f > follows)
8768 follows = f;
8771 return follows;
8774 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8775 unsigned pos, isl_int *val)
8777 int i;
8778 int d;
8779 unsigned total;
8781 if (!bmap)
8782 return -1;
8783 total = isl_basic_map_total_dim(bmap);
8784 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8785 for (; d+1 > pos; --d)
8786 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8787 break;
8788 if (d != pos)
8789 continue;
8790 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8791 return 0;
8792 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8793 return 0;
8794 if (!isl_int_is_one(bmap->eq[i][1+d]))
8795 return 0;
8796 if (val)
8797 isl_int_neg(*val, bmap->eq[i][0]);
8798 return 1;
8800 return 0;
8803 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8804 unsigned pos, isl_int *val)
8806 int i;
8807 isl_int v;
8808 isl_int tmp;
8809 int fixed;
8811 if (!map)
8812 return -1;
8813 if (map->n == 0)
8814 return 0;
8815 if (map->n == 1)
8816 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8817 isl_int_init(v);
8818 isl_int_init(tmp);
8819 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8820 for (i = 1; fixed == 1 && i < map->n; ++i) {
8821 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8822 if (fixed == 1 && isl_int_ne(tmp, v))
8823 fixed = 0;
8825 if (val)
8826 isl_int_set(*val, v);
8827 isl_int_clear(tmp);
8828 isl_int_clear(v);
8829 return fixed;
8832 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8833 unsigned pos, isl_int *val)
8835 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8836 pos, val);
8839 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8840 isl_int *val)
8842 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
8845 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8846 enum isl_dim_type type, unsigned pos, isl_int *val)
8848 if (pos >= isl_basic_map_dim(bmap, type))
8849 return -1;
8850 return isl_basic_map_plain_has_fixed_var(bmap,
8851 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8854 /* If "bmap" obviously lies on a hyperplane where the given dimension
8855 * has a fixed value, then return that value.
8856 * Otherwise return NaN.
8858 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8859 __isl_keep isl_basic_map *bmap,
8860 enum isl_dim_type type, unsigned pos)
8862 isl_ctx *ctx;
8863 isl_val *v;
8864 int fixed;
8866 if (!bmap)
8867 return NULL;
8868 ctx = isl_basic_map_get_ctx(bmap);
8869 v = isl_val_alloc(ctx);
8870 if (!v)
8871 return NULL;
8872 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8873 if (fixed < 0)
8874 return isl_val_free(v);
8875 if (fixed) {
8876 isl_int_set_si(v->d, 1);
8877 return v;
8879 isl_val_free(v);
8880 return isl_val_nan(ctx);
8883 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8884 enum isl_dim_type type, unsigned pos, isl_int *val)
8886 if (pos >= isl_map_dim(map, type))
8887 return -1;
8888 return isl_map_plain_has_fixed_var(map,
8889 map_offset(map, type) - 1 + pos, val);
8892 /* If "map" obviously lies on a hyperplane where the given dimension
8893 * has a fixed value, then return that value.
8894 * Otherwise return NaN.
8896 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8897 enum isl_dim_type type, unsigned pos)
8899 isl_ctx *ctx;
8900 isl_val *v;
8901 int fixed;
8903 if (!map)
8904 return NULL;
8905 ctx = isl_map_get_ctx(map);
8906 v = isl_val_alloc(ctx);
8907 if (!v)
8908 return NULL;
8909 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8910 if (fixed < 0)
8911 return isl_val_free(v);
8912 if (fixed) {
8913 isl_int_set_si(v->d, 1);
8914 return v;
8916 isl_val_free(v);
8917 return isl_val_nan(ctx);
8920 /* If "set" obviously lies on a hyperplane where the given dimension
8921 * has a fixed value, then return that value.
8922 * Otherwise return NaN.
8924 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8925 enum isl_dim_type type, unsigned pos)
8927 return isl_map_plain_get_val_if_fixed(set, type, pos);
8930 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8931 enum isl_dim_type type, unsigned pos, isl_int *val)
8933 return isl_map_plain_is_fixed(set, type, pos, val);
8936 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8937 * then return this fixed value in *val.
8939 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8940 unsigned dim, isl_int *val)
8942 return isl_basic_set_plain_has_fixed_var(bset,
8943 isl_basic_set_n_param(bset) + dim, val);
8946 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8947 * then return this fixed value in *val.
8949 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8950 unsigned dim, isl_int *val)
8952 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8955 /* Check if input variable in has fixed value and if so and if val is not NULL,
8956 * then return this fixed value in *val.
8958 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8959 unsigned in, isl_int *val)
8961 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8964 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8965 * and if val is not NULL, then return this lower bound in *val.
8967 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8968 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8970 int i, i_eq = -1, i_ineq = -1;
8971 isl_int *c;
8972 unsigned total;
8973 unsigned nparam;
8975 if (!bset)
8976 return -1;
8977 total = isl_basic_set_total_dim(bset);
8978 nparam = isl_basic_set_n_param(bset);
8979 for (i = 0; i < bset->n_eq; ++i) {
8980 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8981 continue;
8982 if (i_eq != -1)
8983 return 0;
8984 i_eq = i;
8986 for (i = 0; i < bset->n_ineq; ++i) {
8987 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8988 continue;
8989 if (i_eq != -1 || i_ineq != -1)
8990 return 0;
8991 i_ineq = i;
8993 if (i_eq == -1 && i_ineq == -1)
8994 return 0;
8995 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8996 /* The coefficient should always be one due to normalization. */
8997 if (!isl_int_is_one(c[1+nparam+dim]))
8998 return 0;
8999 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
9000 return 0;
9001 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
9002 total - nparam - dim - 1) != -1)
9003 return 0;
9004 if (val)
9005 isl_int_neg(*val, c[0]);
9006 return 1;
9009 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9010 unsigned dim, isl_int *val)
9012 int i;
9013 isl_int v;
9014 isl_int tmp;
9015 int fixed;
9017 if (!set)
9018 return -1;
9019 if (set->n == 0)
9020 return 0;
9021 if (set->n == 1)
9022 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9023 dim, val);
9024 isl_int_init(v);
9025 isl_int_init(tmp);
9026 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9027 dim, &v);
9028 for (i = 1; fixed == 1 && i < set->n; ++i) {
9029 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9030 dim, &tmp);
9031 if (fixed == 1 && isl_int_ne(tmp, v))
9032 fixed = 0;
9034 if (val)
9035 isl_int_set(*val, v);
9036 isl_int_clear(tmp);
9037 isl_int_clear(v);
9038 return fixed;
9041 /* Return -1 if the constraint "c1" should be sorted before "c2"
9042 * and 1 if it should be sorted after "c2".
9043 * Return 0 if the two constraints are the same (up to the constant term).
9045 * In particular, if a constraint involves later variables than another
9046 * then it is sorted after this other constraint.
9047 * uset_gist depends on constraints without existentially quantified
9048 * variables sorting first.
9050 * For constraints that have the same latest variable, those
9051 * with the same coefficient for this latest variable (first in absolute value
9052 * and then in actual value) are grouped together.
9053 * This is useful for detecting pairs of constraints that can
9054 * be chained in their printed representation.
9056 * Finally, within a group, constraints are sorted according to
9057 * their coefficients (excluding the constant term).
9059 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9061 isl_int **c1 = (isl_int **) p1;
9062 isl_int **c2 = (isl_int **) p2;
9063 int l1, l2;
9064 unsigned size = *(unsigned *) arg;
9065 int cmp;
9067 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9068 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9070 if (l1 != l2)
9071 return l1 - l2;
9073 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9074 if (cmp != 0)
9075 return cmp;
9076 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9077 if (cmp != 0)
9078 return -cmp;
9080 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9083 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9084 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9085 * and 0 if the two constraints are the same (up to the constant term).
9087 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9088 isl_int *c1, isl_int *c2)
9090 unsigned total;
9092 if (!bmap)
9093 return -2;
9094 total = isl_basic_map_total_dim(bmap);
9095 return sort_constraint_cmp(&c1, &c2, &total);
9098 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9099 __isl_take isl_basic_map *bmap)
9101 unsigned total;
9103 if (!bmap)
9104 return NULL;
9105 if (bmap->n_ineq == 0)
9106 return bmap;
9107 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9108 return bmap;
9109 total = isl_basic_map_total_dim(bmap);
9110 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9111 &sort_constraint_cmp, &total) < 0)
9112 return isl_basic_map_free(bmap);
9113 return bmap;
9116 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9117 __isl_take isl_basic_set *bset)
9119 isl_basic_map *bmap = bset_to_bmap(bset);
9120 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9123 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9125 if (!bmap)
9126 return NULL;
9127 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9128 return bmap;
9129 bmap = isl_basic_map_remove_redundancies(bmap);
9130 bmap = isl_basic_map_sort_constraints(bmap);
9131 if (bmap)
9132 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9133 return bmap;
9136 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9138 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9141 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9142 const __isl_keep isl_basic_map *bmap2)
9144 int i, cmp;
9145 unsigned total;
9147 if (!bmap1 || !bmap2)
9148 return -1;
9150 if (bmap1 == bmap2)
9151 return 0;
9152 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9153 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9154 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9155 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9156 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9157 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9158 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9159 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9160 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9161 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9162 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9163 return 0;
9164 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9165 return 1;
9166 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9167 return -1;
9168 if (bmap1->n_eq != bmap2->n_eq)
9169 return bmap1->n_eq - bmap2->n_eq;
9170 if (bmap1->n_ineq != bmap2->n_ineq)
9171 return bmap1->n_ineq - bmap2->n_ineq;
9172 if (bmap1->n_div != bmap2->n_div)
9173 return bmap1->n_div - bmap2->n_div;
9174 total = isl_basic_map_total_dim(bmap1);
9175 for (i = 0; i < bmap1->n_eq; ++i) {
9176 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9177 if (cmp)
9178 return cmp;
9180 for (i = 0; i < bmap1->n_ineq; ++i) {
9181 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9182 if (cmp)
9183 return cmp;
9185 for (i = 0; i < bmap1->n_div; ++i) {
9186 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9187 if (cmp)
9188 return cmp;
9190 return 0;
9193 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9194 const __isl_keep isl_basic_set *bset2)
9196 return isl_basic_map_plain_cmp(bset1, bset2);
9199 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9201 int i, cmp;
9203 if (set1 == set2)
9204 return 0;
9205 if (set1->n != set2->n)
9206 return set1->n - set2->n;
9208 for (i = 0; i < set1->n; ++i) {
9209 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9210 if (cmp)
9211 return cmp;
9214 return 0;
9217 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9218 __isl_keep isl_basic_map *bmap2)
9220 if (!bmap1 || !bmap2)
9221 return isl_bool_error;
9222 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9225 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9226 __isl_keep isl_basic_set *bset2)
9228 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9229 bset_to_bmap(bset2));
9232 static int qsort_bmap_cmp(const void *p1, const void *p2)
9234 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9235 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9237 return isl_basic_map_plain_cmp(bmap1, bmap2);
9240 /* Sort the basic maps of "map" and remove duplicate basic maps.
9242 * While removing basic maps, we make sure that the basic maps remain
9243 * sorted because isl_map_normalize expects the basic maps of the result
9244 * to be sorted.
9246 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9248 int i, j;
9250 map = isl_map_remove_empty_parts(map);
9251 if (!map)
9252 return NULL;
9253 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9254 for (i = map->n - 1; i >= 1; --i) {
9255 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9256 continue;
9257 isl_basic_map_free(map->p[i-1]);
9258 for (j = i; j < map->n; ++j)
9259 map->p[j - 1] = map->p[j];
9260 map->n--;
9263 return map;
9266 /* Remove obvious duplicates among the basic maps of "map".
9268 * Unlike isl_map_normalize, this function does not remove redundant
9269 * constraints and only removes duplicates that have exactly the same
9270 * constraints in the input. It does sort the constraints and
9271 * the basic maps to ease the detection of duplicates.
9273 * If "map" has already been normalized or if the basic maps are
9274 * disjoint, then there can be no duplicates.
9276 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9278 int i;
9279 isl_basic_map *bmap;
9281 if (!map)
9282 return NULL;
9283 if (map->n <= 1)
9284 return map;
9285 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9286 return map;
9287 for (i = 0; i < map->n; ++i) {
9288 bmap = isl_basic_map_copy(map->p[i]);
9289 bmap = isl_basic_map_sort_constraints(bmap);
9290 if (!bmap)
9291 return isl_map_free(map);
9292 isl_basic_map_free(map->p[i]);
9293 map->p[i] = bmap;
9296 map = sort_and_remove_duplicates(map);
9297 return map;
9300 /* We normalize in place, but if anything goes wrong we need
9301 * to return NULL, so we need to make sure we don't change the
9302 * meaning of any possible other copies of map.
9304 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9306 int i;
9307 struct isl_basic_map *bmap;
9309 if (!map)
9310 return NULL;
9311 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9312 return map;
9313 for (i = 0; i < map->n; ++i) {
9314 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9315 if (!bmap)
9316 goto error;
9317 isl_basic_map_free(map->p[i]);
9318 map->p[i] = bmap;
9321 map = sort_and_remove_duplicates(map);
9322 if (map)
9323 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9324 return map;
9325 error:
9326 isl_map_free(map);
9327 return NULL;
9330 struct isl_set *isl_set_normalize(struct isl_set *set)
9332 return set_from_map(isl_map_normalize(set_to_map(set)));
9335 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9336 __isl_keep isl_map *map2)
9338 int i;
9339 isl_bool equal;
9341 if (!map1 || !map2)
9342 return isl_bool_error;
9344 if (map1 == map2)
9345 return isl_bool_true;
9346 if (!isl_space_is_equal(map1->dim, map2->dim))
9347 return isl_bool_false;
9349 map1 = isl_map_copy(map1);
9350 map2 = isl_map_copy(map2);
9351 map1 = isl_map_normalize(map1);
9352 map2 = isl_map_normalize(map2);
9353 if (!map1 || !map2)
9354 goto error;
9355 equal = map1->n == map2->n;
9356 for (i = 0; equal && i < map1->n; ++i) {
9357 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9358 if (equal < 0)
9359 goto error;
9361 isl_map_free(map1);
9362 isl_map_free(map2);
9363 return equal;
9364 error:
9365 isl_map_free(map1);
9366 isl_map_free(map2);
9367 return isl_bool_error;
9370 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9371 __isl_keep isl_set *set2)
9373 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9376 /* Return an interval that ranges from min to max (inclusive)
9378 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9379 isl_int min, isl_int max)
9381 int k;
9382 struct isl_basic_set *bset = NULL;
9384 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9385 if (!bset)
9386 goto error;
9388 k = isl_basic_set_alloc_inequality(bset);
9389 if (k < 0)
9390 goto error;
9391 isl_int_set_si(bset->ineq[k][1], 1);
9392 isl_int_neg(bset->ineq[k][0], min);
9394 k = isl_basic_set_alloc_inequality(bset);
9395 if (k < 0)
9396 goto error;
9397 isl_int_set_si(bset->ineq[k][1], -1);
9398 isl_int_set(bset->ineq[k][0], max);
9400 return bset;
9401 error:
9402 isl_basic_set_free(bset);
9403 return NULL;
9406 /* Return the basic maps in "map" as a list.
9408 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9409 __isl_keep isl_map *map)
9411 int i;
9412 isl_ctx *ctx;
9413 isl_basic_map_list *list;
9415 if (!map)
9416 return NULL;
9417 ctx = isl_map_get_ctx(map);
9418 list = isl_basic_map_list_alloc(ctx, map->n);
9420 for (i = 0; i < map->n; ++i) {
9421 isl_basic_map *bmap;
9423 bmap = isl_basic_map_copy(map->p[i]);
9424 list = isl_basic_map_list_add(list, bmap);
9427 return list;
9430 /* Return the intersection of the elements in the non-empty list "list".
9431 * All elements are assumed to live in the same space.
9433 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9434 __isl_take isl_basic_map_list *list)
9436 int i, n;
9437 isl_basic_map *bmap;
9439 if (!list)
9440 return NULL;
9441 n = isl_basic_map_list_n_basic_map(list);
9442 if (n < 1)
9443 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9444 "expecting non-empty list", goto error);
9446 bmap = isl_basic_map_list_get_basic_map(list, 0);
9447 for (i = 1; i < n; ++i) {
9448 isl_basic_map *bmap_i;
9450 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9451 bmap = isl_basic_map_intersect(bmap, bmap_i);
9454 isl_basic_map_list_free(list);
9455 return bmap;
9456 error:
9457 isl_basic_map_list_free(list);
9458 return NULL;
9461 /* Return the intersection of the elements in the non-empty list "list".
9462 * All elements are assumed to live in the same space.
9464 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9465 __isl_take isl_basic_set_list *list)
9467 return isl_basic_map_list_intersect(list);
9470 /* Return the union of the elements of "list".
9471 * The list is required to have at least one element.
9473 __isl_give isl_set *isl_basic_set_list_union(
9474 __isl_take isl_basic_set_list *list)
9476 int i, n;
9477 isl_space *space;
9478 isl_basic_set *bset;
9479 isl_set *set;
9481 if (!list)
9482 return NULL;
9483 n = isl_basic_set_list_n_basic_set(list);
9484 if (n < 1)
9485 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9486 "expecting non-empty list", goto error);
9488 bset = isl_basic_set_list_get_basic_set(list, 0);
9489 space = isl_basic_set_get_space(bset);
9490 isl_basic_set_free(bset);
9492 set = isl_set_alloc_space(space, n, 0);
9493 for (i = 0; i < n; ++i) {
9494 bset = isl_basic_set_list_get_basic_set(list, i);
9495 set = isl_set_add_basic_set(set, bset);
9498 isl_basic_set_list_free(list);
9499 return set;
9500 error:
9501 isl_basic_set_list_free(list);
9502 return NULL;
9505 /* Return the union of the elements in the non-empty list "list".
9506 * All elements are assumed to live in the same space.
9508 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9510 int i, n;
9511 isl_set *set;
9513 if (!list)
9514 return NULL;
9515 n = isl_set_list_n_set(list);
9516 if (n < 1)
9517 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9518 "expecting non-empty list", goto error);
9520 set = isl_set_list_get_set(list, 0);
9521 for (i = 1; i < n; ++i) {
9522 isl_set *set_i;
9524 set_i = isl_set_list_get_set(list, i);
9525 set = isl_set_union(set, set_i);
9528 isl_set_list_free(list);
9529 return set;
9530 error:
9531 isl_set_list_free(list);
9532 return NULL;
9535 /* Return the Cartesian product of the basic sets in list (in the given order).
9537 __isl_give isl_basic_set *isl_basic_set_list_product(
9538 __isl_take struct isl_basic_set_list *list)
9540 int i;
9541 unsigned dim;
9542 unsigned nparam;
9543 unsigned extra;
9544 unsigned n_eq;
9545 unsigned n_ineq;
9546 struct isl_basic_set *product = NULL;
9548 if (!list)
9549 goto error;
9550 isl_assert(list->ctx, list->n > 0, goto error);
9551 isl_assert(list->ctx, list->p[0], goto error);
9552 nparam = isl_basic_set_n_param(list->p[0]);
9553 dim = isl_basic_set_n_dim(list->p[0]);
9554 extra = list->p[0]->n_div;
9555 n_eq = list->p[0]->n_eq;
9556 n_ineq = list->p[0]->n_ineq;
9557 for (i = 1; i < list->n; ++i) {
9558 isl_assert(list->ctx, list->p[i], goto error);
9559 isl_assert(list->ctx,
9560 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9561 dim += isl_basic_set_n_dim(list->p[i]);
9562 extra += list->p[i]->n_div;
9563 n_eq += list->p[i]->n_eq;
9564 n_ineq += list->p[i]->n_ineq;
9566 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9567 n_eq, n_ineq);
9568 if (!product)
9569 goto error;
9570 dim = 0;
9571 for (i = 0; i < list->n; ++i) {
9572 isl_basic_set_add_constraints(product,
9573 isl_basic_set_copy(list->p[i]), dim);
9574 dim += isl_basic_set_n_dim(list->p[i]);
9576 isl_basic_set_list_free(list);
9577 return product;
9578 error:
9579 isl_basic_set_free(product);
9580 isl_basic_set_list_free(list);
9581 return NULL;
9584 struct isl_basic_map *isl_basic_map_product(
9585 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9587 isl_space *dim_result = NULL;
9588 struct isl_basic_map *bmap;
9589 unsigned in1, in2, out1, out2, nparam, total, pos;
9590 struct isl_dim_map *dim_map1, *dim_map2;
9592 if (!bmap1 || !bmap2)
9593 goto error;
9595 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9596 bmap2->dim, isl_dim_param), goto error);
9597 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9598 isl_space_copy(bmap2->dim));
9600 in1 = isl_basic_map_n_in(bmap1);
9601 in2 = isl_basic_map_n_in(bmap2);
9602 out1 = isl_basic_map_n_out(bmap1);
9603 out2 = isl_basic_map_n_out(bmap2);
9604 nparam = isl_basic_map_n_param(bmap1);
9606 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9607 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9608 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9609 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9610 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9611 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9612 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9613 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9614 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9615 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9616 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9618 bmap = isl_basic_map_alloc_space(dim_result,
9619 bmap1->n_div + bmap2->n_div,
9620 bmap1->n_eq + bmap2->n_eq,
9621 bmap1->n_ineq + bmap2->n_ineq);
9622 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9623 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9624 bmap = isl_basic_map_simplify(bmap);
9625 return isl_basic_map_finalize(bmap);
9626 error:
9627 isl_basic_map_free(bmap1);
9628 isl_basic_map_free(bmap2);
9629 return NULL;
9632 __isl_give isl_basic_map *isl_basic_map_flat_product(
9633 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9635 isl_basic_map *prod;
9637 prod = isl_basic_map_product(bmap1, bmap2);
9638 prod = isl_basic_map_flatten(prod);
9639 return prod;
9642 __isl_give isl_basic_set *isl_basic_set_flat_product(
9643 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9645 return isl_basic_map_flat_range_product(bset1, bset2);
9648 __isl_give isl_basic_map *isl_basic_map_domain_product(
9649 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9651 isl_space *space_result = NULL;
9652 isl_basic_map *bmap;
9653 unsigned in1, in2, out, nparam, total, pos;
9654 struct isl_dim_map *dim_map1, *dim_map2;
9656 if (!bmap1 || !bmap2)
9657 goto error;
9659 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9660 isl_space_copy(bmap2->dim));
9662 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9663 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9664 out = isl_basic_map_dim(bmap1, isl_dim_out);
9665 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9667 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9668 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9669 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9670 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9671 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9672 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9673 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9674 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9675 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9676 isl_dim_map_div(dim_map1, bmap1, pos += out);
9677 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9679 bmap = isl_basic_map_alloc_space(space_result,
9680 bmap1->n_div + bmap2->n_div,
9681 bmap1->n_eq + bmap2->n_eq,
9682 bmap1->n_ineq + bmap2->n_ineq);
9683 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9684 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9685 bmap = isl_basic_map_simplify(bmap);
9686 return isl_basic_map_finalize(bmap);
9687 error:
9688 isl_basic_map_free(bmap1);
9689 isl_basic_map_free(bmap2);
9690 return NULL;
9693 __isl_give isl_basic_map *isl_basic_map_range_product(
9694 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9696 int rational;
9697 isl_space *dim_result = NULL;
9698 isl_basic_map *bmap;
9699 unsigned in, out1, out2, nparam, total, pos;
9700 struct isl_dim_map *dim_map1, *dim_map2;
9702 rational = isl_basic_map_is_rational(bmap1);
9703 if (rational >= 0 && rational)
9704 rational = isl_basic_map_is_rational(bmap2);
9705 if (!bmap1 || !bmap2 || rational < 0)
9706 goto error;
9708 if (!isl_space_match(bmap1->dim, isl_dim_param,
9709 bmap2->dim, isl_dim_param))
9710 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9711 "parameters don't match", goto error);
9713 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9714 isl_space_copy(bmap2->dim));
9716 in = isl_basic_map_dim(bmap1, isl_dim_in);
9717 out1 = isl_basic_map_n_out(bmap1);
9718 out2 = isl_basic_map_n_out(bmap2);
9719 nparam = isl_basic_map_n_param(bmap1);
9721 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9722 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9723 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9724 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9725 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9726 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9727 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9728 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9729 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9730 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9731 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9733 bmap = isl_basic_map_alloc_space(dim_result,
9734 bmap1->n_div + bmap2->n_div,
9735 bmap1->n_eq + bmap2->n_eq,
9736 bmap1->n_ineq + bmap2->n_ineq);
9737 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9738 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9739 if (rational)
9740 bmap = isl_basic_map_set_rational(bmap);
9741 bmap = isl_basic_map_simplify(bmap);
9742 return isl_basic_map_finalize(bmap);
9743 error:
9744 isl_basic_map_free(bmap1);
9745 isl_basic_map_free(bmap2);
9746 return NULL;
9749 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9750 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9752 isl_basic_map *prod;
9754 prod = isl_basic_map_range_product(bmap1, bmap2);
9755 prod = isl_basic_map_flatten_range(prod);
9756 return prod;
9759 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9760 * and collect the results.
9761 * The result live in the space obtained by calling "space_product"
9762 * on the spaces of "map1" and "map2".
9763 * If "remove_duplicates" is set then the result may contain duplicates
9764 * (even if the inputs do not) and so we try and remove the obvious
9765 * duplicates.
9767 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9768 __isl_take isl_map *map2,
9769 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9770 __isl_take isl_space *right),
9771 __isl_give isl_basic_map *(*basic_map_product)(
9772 __isl_take isl_basic_map *left,
9773 __isl_take isl_basic_map *right),
9774 int remove_duplicates)
9776 unsigned flags = 0;
9777 struct isl_map *result;
9778 int i, j;
9780 if (!map1 || !map2)
9781 goto error;
9783 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9784 map2->dim, isl_dim_param), goto error);
9786 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9787 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9788 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9790 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9791 isl_space_copy(map2->dim)),
9792 map1->n * map2->n, flags);
9793 if (!result)
9794 goto error;
9795 for (i = 0; i < map1->n; ++i)
9796 for (j = 0; j < map2->n; ++j) {
9797 struct isl_basic_map *part;
9798 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9799 isl_basic_map_copy(map2->p[j]));
9800 if (isl_basic_map_is_empty(part))
9801 isl_basic_map_free(part);
9802 else
9803 result = isl_map_add_basic_map(result, part);
9804 if (!result)
9805 goto error;
9807 if (remove_duplicates)
9808 result = isl_map_remove_obvious_duplicates(result);
9809 isl_map_free(map1);
9810 isl_map_free(map2);
9811 return result;
9812 error:
9813 isl_map_free(map1);
9814 isl_map_free(map2);
9815 return NULL;
9818 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9820 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9821 __isl_take isl_map *map2)
9823 return map_product(map1, map2, &isl_space_product,
9824 &isl_basic_map_product, 0);
9827 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9828 __isl_take isl_map *map2)
9830 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9833 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9835 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9836 __isl_take isl_map *map2)
9838 isl_map *prod;
9840 prod = isl_map_product(map1, map2);
9841 prod = isl_map_flatten(prod);
9842 return prod;
9845 /* Given two set A and B, construct its Cartesian product A x B.
9847 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9849 return isl_map_range_product(set1, set2);
9852 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9853 __isl_take isl_set *set2)
9855 return isl_map_flat_range_product(set1, set2);
9858 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9860 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9861 __isl_take isl_map *map2)
9863 return map_product(map1, map2, &isl_space_domain_product,
9864 &isl_basic_map_domain_product, 1);
9867 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9869 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9870 __isl_take isl_map *map2)
9872 return map_product(map1, map2, &isl_space_range_product,
9873 &isl_basic_map_range_product, 1);
9876 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9877 __isl_take isl_map *map2)
9879 return isl_map_align_params_map_map_and(map1, map2,
9880 &map_domain_product_aligned);
9883 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9884 __isl_take isl_map *map2)
9886 return isl_map_align_params_map_map_and(map1, map2,
9887 &map_range_product_aligned);
9890 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9892 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9894 isl_space *space;
9895 int total1, keep1, total2, keep2;
9897 if (!map)
9898 return NULL;
9899 if (!isl_space_domain_is_wrapping(map->dim) ||
9900 !isl_space_range_is_wrapping(map->dim))
9901 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9902 "not a product", return isl_map_free(map));
9904 space = isl_map_get_space(map);
9905 total1 = isl_space_dim(space, isl_dim_in);
9906 total2 = isl_space_dim(space, isl_dim_out);
9907 space = isl_space_factor_domain(space);
9908 keep1 = isl_space_dim(space, isl_dim_in);
9909 keep2 = isl_space_dim(space, isl_dim_out);
9910 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9911 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9912 map = isl_map_reset_space(map, space);
9914 return map;
9917 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9919 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9921 isl_space *space;
9922 int total1, keep1, total2, keep2;
9924 if (!map)
9925 return NULL;
9926 if (!isl_space_domain_is_wrapping(map->dim) ||
9927 !isl_space_range_is_wrapping(map->dim))
9928 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9929 "not a product", return isl_map_free(map));
9931 space = isl_map_get_space(map);
9932 total1 = isl_space_dim(space, isl_dim_in);
9933 total2 = isl_space_dim(space, isl_dim_out);
9934 space = isl_space_factor_range(space);
9935 keep1 = isl_space_dim(space, isl_dim_in);
9936 keep2 = isl_space_dim(space, isl_dim_out);
9937 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9938 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9939 map = isl_map_reset_space(map, space);
9941 return map;
9944 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9946 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9948 isl_space *space;
9949 int total, keep;
9951 if (!map)
9952 return NULL;
9953 if (!isl_space_domain_is_wrapping(map->dim))
9954 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9955 "domain is not a product", return isl_map_free(map));
9957 space = isl_map_get_space(map);
9958 total = isl_space_dim(space, isl_dim_in);
9959 space = isl_space_domain_factor_domain(space);
9960 keep = isl_space_dim(space, isl_dim_in);
9961 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9962 map = isl_map_reset_space(map, space);
9964 return map;
9967 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9969 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9971 isl_space *space;
9972 int total, keep;
9974 if (!map)
9975 return NULL;
9976 if (!isl_space_domain_is_wrapping(map->dim))
9977 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9978 "domain is not a product", return isl_map_free(map));
9980 space = isl_map_get_space(map);
9981 total = isl_space_dim(space, isl_dim_in);
9982 space = isl_space_domain_factor_range(space);
9983 keep = isl_space_dim(space, isl_dim_in);
9984 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9985 map = isl_map_reset_space(map, space);
9987 return map;
9990 /* Given a map A -> [B -> C], extract the map A -> B.
9992 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9994 isl_space *space;
9995 int total, keep;
9997 if (!map)
9998 return NULL;
9999 if (!isl_space_range_is_wrapping(map->dim))
10000 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10001 "range is not a product", return isl_map_free(map));
10003 space = isl_map_get_space(map);
10004 total = isl_space_dim(space, isl_dim_out);
10005 space = isl_space_range_factor_domain(space);
10006 keep = isl_space_dim(space, isl_dim_out);
10007 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10008 map = isl_map_reset_space(map, space);
10010 return map;
10013 /* Given a map A -> [B -> C], extract the map A -> C.
10015 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10017 isl_space *space;
10018 int total, keep;
10020 if (!map)
10021 return NULL;
10022 if (!isl_space_range_is_wrapping(map->dim))
10023 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10024 "range is not a product", return isl_map_free(map));
10026 space = isl_map_get_space(map);
10027 total = isl_space_dim(space, isl_dim_out);
10028 space = isl_space_range_factor_range(space);
10029 keep = isl_space_dim(space, isl_dim_out);
10030 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10031 map = isl_map_reset_space(map, space);
10033 return map;
10036 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10038 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10039 __isl_take isl_map *map2)
10041 isl_map *prod;
10043 prod = isl_map_domain_product(map1, map2);
10044 prod = isl_map_flatten_domain(prod);
10045 return prod;
10048 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10050 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10051 __isl_take isl_map *map2)
10053 isl_map *prod;
10055 prod = isl_map_range_product(map1, map2);
10056 prod = isl_map_flatten_range(prod);
10057 return prod;
10060 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10062 int i;
10063 uint32_t hash = isl_hash_init();
10064 unsigned total;
10066 if (!bmap)
10067 return 0;
10068 bmap = isl_basic_map_copy(bmap);
10069 bmap = isl_basic_map_normalize(bmap);
10070 if (!bmap)
10071 return 0;
10072 total = isl_basic_map_total_dim(bmap);
10073 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10074 for (i = 0; i < bmap->n_eq; ++i) {
10075 uint32_t c_hash;
10076 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10077 isl_hash_hash(hash, c_hash);
10079 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10080 for (i = 0; i < bmap->n_ineq; ++i) {
10081 uint32_t c_hash;
10082 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10083 isl_hash_hash(hash, c_hash);
10085 isl_hash_byte(hash, bmap->n_div & 0xFF);
10086 for (i = 0; i < bmap->n_div; ++i) {
10087 uint32_t c_hash;
10088 if (isl_int_is_zero(bmap->div[i][0]))
10089 continue;
10090 isl_hash_byte(hash, i & 0xFF);
10091 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10092 isl_hash_hash(hash, c_hash);
10094 isl_basic_map_free(bmap);
10095 return hash;
10098 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10100 return isl_basic_map_get_hash(bset_to_bmap(bset));
10103 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10105 int i;
10106 uint32_t hash;
10108 if (!map)
10109 return 0;
10110 map = isl_map_copy(map);
10111 map = isl_map_normalize(map);
10112 if (!map)
10113 return 0;
10115 hash = isl_hash_init();
10116 for (i = 0; i < map->n; ++i) {
10117 uint32_t bmap_hash;
10118 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10119 isl_hash_hash(hash, bmap_hash);
10122 isl_map_free(map);
10124 return hash;
10127 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10129 return isl_map_get_hash(set_to_map(set));
10132 /* Check if the value for dimension dim is completely determined
10133 * by the values of the other parameters and variables.
10134 * That is, check if dimension dim is involved in an equality.
10136 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10138 int i;
10139 unsigned nparam;
10141 if (!bset)
10142 return -1;
10143 nparam = isl_basic_set_n_param(bset);
10144 for (i = 0; i < bset->n_eq; ++i)
10145 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10146 return 1;
10147 return 0;
10150 /* Check if the value for dimension dim is completely determined
10151 * by the values of the other parameters and variables.
10152 * That is, check if dimension dim is involved in an equality
10153 * for each of the subsets.
10155 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10157 int i;
10159 if (!set)
10160 return -1;
10161 for (i = 0; i < set->n; ++i) {
10162 int unique;
10163 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10164 if (unique != 1)
10165 return unique;
10167 return 1;
10170 /* Return the number of basic maps in the (current) representation of "map".
10172 int isl_map_n_basic_map(__isl_keep isl_map *map)
10174 return map ? map->n : 0;
10177 int isl_set_n_basic_set(__isl_keep isl_set *set)
10179 return set ? set->n : 0;
10182 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10183 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10185 int i;
10187 if (!map)
10188 return isl_stat_error;
10190 for (i = 0; i < map->n; ++i)
10191 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10192 return isl_stat_error;
10194 return isl_stat_ok;
10197 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10198 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10200 int i;
10202 if (!set)
10203 return isl_stat_error;
10205 for (i = 0; i < set->n; ++i)
10206 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10207 return isl_stat_error;
10209 return isl_stat_ok;
10212 /* Return a list of basic sets, the union of which is equal to "set".
10214 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10215 __isl_keep isl_set *set)
10217 int i;
10218 isl_basic_set_list *list;
10220 if (!set)
10221 return NULL;
10223 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10224 for (i = 0; i < set->n; ++i) {
10225 isl_basic_set *bset;
10227 bset = isl_basic_set_copy(set->p[i]);
10228 list = isl_basic_set_list_add(list, bset);
10231 return list;
10234 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10236 isl_space *dim;
10238 if (!bset)
10239 return NULL;
10241 bset = isl_basic_set_cow(bset);
10242 if (!bset)
10243 return NULL;
10245 dim = isl_basic_set_get_space(bset);
10246 dim = isl_space_lift(dim, bset->n_div);
10247 if (!dim)
10248 goto error;
10249 isl_space_free(bset->dim);
10250 bset->dim = dim;
10251 bset->extra -= bset->n_div;
10252 bset->n_div = 0;
10254 bset = isl_basic_set_finalize(bset);
10256 return bset;
10257 error:
10258 isl_basic_set_free(bset);
10259 return NULL;
10262 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10264 int i;
10265 isl_space *dim;
10266 unsigned n_div;
10268 set = isl_set_align_divs(set);
10270 if (!set)
10271 return NULL;
10273 set = isl_set_cow(set);
10274 if (!set)
10275 return NULL;
10277 n_div = set->p[0]->n_div;
10278 dim = isl_set_get_space(set);
10279 dim = isl_space_lift(dim, n_div);
10280 if (!dim)
10281 goto error;
10282 isl_space_free(set->dim);
10283 set->dim = dim;
10285 for (i = 0; i < set->n; ++i) {
10286 set->p[i] = isl_basic_set_lift(set->p[i]);
10287 if (!set->p[i])
10288 goto error;
10291 return set;
10292 error:
10293 isl_set_free(set);
10294 return NULL;
10297 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10299 isl_space *dim;
10300 struct isl_basic_map *bmap;
10301 unsigned n_set;
10302 unsigned n_div;
10303 unsigned n_param;
10304 unsigned total;
10305 int i, k, l;
10307 set = isl_set_align_divs(set);
10309 if (!set)
10310 return NULL;
10312 dim = isl_set_get_space(set);
10313 if (set->n == 0 || set->p[0]->n_div == 0) {
10314 isl_set_free(set);
10315 return isl_map_identity(isl_space_map_from_set(dim));
10318 n_div = set->p[0]->n_div;
10319 dim = isl_space_map_from_set(dim);
10320 n_param = isl_space_dim(dim, isl_dim_param);
10321 n_set = isl_space_dim(dim, isl_dim_in);
10322 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10323 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10324 for (i = 0; i < n_set; ++i)
10325 bmap = var_equal(bmap, i);
10327 total = n_param + n_set + n_set + n_div;
10328 for (i = 0; i < n_div; ++i) {
10329 k = isl_basic_map_alloc_inequality(bmap);
10330 if (k < 0)
10331 goto error;
10332 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10333 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10334 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10335 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10336 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10337 set->p[0]->div[i][0]);
10339 l = isl_basic_map_alloc_inequality(bmap);
10340 if (l < 0)
10341 goto error;
10342 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10343 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10344 set->p[0]->div[i][0]);
10345 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10348 isl_set_free(set);
10349 bmap = isl_basic_map_simplify(bmap);
10350 bmap = isl_basic_map_finalize(bmap);
10351 return isl_map_from_basic_map(bmap);
10352 error:
10353 isl_set_free(set);
10354 isl_basic_map_free(bmap);
10355 return NULL;
10358 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10360 unsigned dim;
10361 int size = 0;
10363 if (!bset)
10364 return -1;
10366 dim = isl_basic_set_total_dim(bset);
10367 size += bset->n_eq * (1 + dim);
10368 size += bset->n_ineq * (1 + dim);
10369 size += bset->n_div * (2 + dim);
10371 return size;
10374 int isl_set_size(__isl_keep isl_set *set)
10376 int i;
10377 int size = 0;
10379 if (!set)
10380 return -1;
10382 for (i = 0; i < set->n; ++i)
10383 size += isl_basic_set_size(set->p[i]);
10385 return size;
10388 /* Check if there is any lower bound (if lower == 0) and/or upper
10389 * bound (if upper == 0) on the specified dim.
10391 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10392 enum isl_dim_type type, unsigned pos, int lower, int upper)
10394 int i;
10396 if (!bmap)
10397 return isl_bool_error;
10399 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10400 return isl_bool_error);
10402 pos += isl_basic_map_offset(bmap, type);
10404 for (i = 0; i < bmap->n_div; ++i) {
10405 if (isl_int_is_zero(bmap->div[i][0]))
10406 continue;
10407 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10408 return isl_bool_true;
10411 for (i = 0; i < bmap->n_eq; ++i)
10412 if (!isl_int_is_zero(bmap->eq[i][pos]))
10413 return isl_bool_true;
10415 for (i = 0; i < bmap->n_ineq; ++i) {
10416 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10417 if (sgn > 0)
10418 lower = 1;
10419 if (sgn < 0)
10420 upper = 1;
10423 return lower && upper;
10426 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10427 enum isl_dim_type type, unsigned pos)
10429 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10432 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10433 enum isl_dim_type type, unsigned pos)
10435 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10438 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10439 enum isl_dim_type type, unsigned pos)
10441 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10444 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10445 enum isl_dim_type type, unsigned pos)
10447 int i;
10449 if (!map)
10450 return -1;
10452 for (i = 0; i < map->n; ++i) {
10453 int bounded;
10454 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10455 if (bounded < 0 || !bounded)
10456 return bounded;
10459 return 1;
10462 /* Return 1 if the specified dim is involved in both an upper bound
10463 * and a lower bound.
10465 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10466 enum isl_dim_type type, unsigned pos)
10468 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10471 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10473 static isl_bool has_any_bound(__isl_keep isl_map *map,
10474 enum isl_dim_type type, unsigned pos,
10475 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10476 enum isl_dim_type type, unsigned pos))
10478 int i;
10480 if (!map)
10481 return isl_bool_error;
10483 for (i = 0; i < map->n; ++i) {
10484 isl_bool bounded;
10485 bounded = fn(map->p[i], type, pos);
10486 if (bounded < 0 || bounded)
10487 return bounded;
10490 return isl_bool_false;
10493 /* Return 1 if the specified dim is involved in any lower bound.
10495 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10496 enum isl_dim_type type, unsigned pos)
10498 return has_any_bound(set, type, pos,
10499 &isl_basic_map_dim_has_lower_bound);
10502 /* Return 1 if the specified dim is involved in any upper bound.
10504 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10505 enum isl_dim_type type, unsigned pos)
10507 return has_any_bound(set, type, pos,
10508 &isl_basic_map_dim_has_upper_bound);
10511 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10513 static isl_bool has_bound(__isl_keep isl_map *map,
10514 enum isl_dim_type type, unsigned pos,
10515 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10516 enum isl_dim_type type, unsigned pos))
10518 int i;
10520 if (!map)
10521 return isl_bool_error;
10523 for (i = 0; i < map->n; ++i) {
10524 isl_bool bounded;
10525 bounded = fn(map->p[i], type, pos);
10526 if (bounded < 0 || !bounded)
10527 return bounded;
10530 return isl_bool_true;
10533 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10535 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10536 enum isl_dim_type type, unsigned pos)
10538 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10541 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10543 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10544 enum isl_dim_type type, unsigned pos)
10546 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10549 /* For each of the "n" variables starting at "first", determine
10550 * the sign of the variable and put the results in the first "n"
10551 * elements of the array "signs".
10552 * Sign
10553 * 1 means that the variable is non-negative
10554 * -1 means that the variable is non-positive
10555 * 0 means the variable attains both positive and negative values.
10557 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10558 unsigned first, unsigned n, int *signs)
10560 isl_vec *bound = NULL;
10561 struct isl_tab *tab = NULL;
10562 struct isl_tab_undo *snap;
10563 int i;
10565 if (!bset || !signs)
10566 return -1;
10568 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10569 tab = isl_tab_from_basic_set(bset, 0);
10570 if (!bound || !tab)
10571 goto error;
10573 isl_seq_clr(bound->el, bound->size);
10574 isl_int_set_si(bound->el[0], -1);
10576 snap = isl_tab_snap(tab);
10577 for (i = 0; i < n; ++i) {
10578 int empty;
10580 isl_int_set_si(bound->el[1 + first + i], -1);
10581 if (isl_tab_add_ineq(tab, bound->el) < 0)
10582 goto error;
10583 empty = tab->empty;
10584 isl_int_set_si(bound->el[1 + first + i], 0);
10585 if (isl_tab_rollback(tab, snap) < 0)
10586 goto error;
10588 if (empty) {
10589 signs[i] = 1;
10590 continue;
10593 isl_int_set_si(bound->el[1 + first + i], 1);
10594 if (isl_tab_add_ineq(tab, bound->el) < 0)
10595 goto error;
10596 empty = tab->empty;
10597 isl_int_set_si(bound->el[1 + first + i], 0);
10598 if (isl_tab_rollback(tab, snap) < 0)
10599 goto error;
10601 signs[i] = empty ? -1 : 0;
10604 isl_tab_free(tab);
10605 isl_vec_free(bound);
10606 return 0;
10607 error:
10608 isl_tab_free(tab);
10609 isl_vec_free(bound);
10610 return -1;
10613 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10614 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10616 if (!bset || !signs)
10617 return -1;
10618 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10619 return -1);
10621 first += pos(bset->dim, type) - 1;
10622 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10625 /* Is it possible for the integer division "div" to depend (possibly
10626 * indirectly) on any output dimensions?
10628 * If the div is undefined, then we conservatively assume that it
10629 * may depend on them.
10630 * Otherwise, we check if it actually depends on them or on any integer
10631 * divisions that may depend on them.
10633 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10635 int i;
10636 unsigned n_out, o_out;
10637 unsigned n_div, o_div;
10639 if (isl_int_is_zero(bmap->div[div][0]))
10640 return 1;
10642 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10643 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10645 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10646 return 1;
10648 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10649 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10651 for (i = 0; i < n_div; ++i) {
10652 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10653 continue;
10654 if (div_may_involve_output(bmap, i))
10655 return 1;
10658 return 0;
10661 /* Return the first integer division of "bmap" in the range
10662 * [first, first + n[ that may depend on any output dimensions and
10663 * that has a non-zero coefficient in "c" (where the first coefficient
10664 * in "c" corresponds to integer division "first").
10666 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10667 isl_int *c, int first, int n)
10669 int k;
10671 if (!bmap)
10672 return -1;
10674 for (k = first; k < first + n; ++k) {
10675 if (isl_int_is_zero(c[k]))
10676 continue;
10677 if (div_may_involve_output(bmap, k))
10678 return k;
10681 return first + n;
10684 /* Look for a pair of inequality constraints in "bmap" of the form
10686 * -l + i >= 0 or i >= l
10687 * and
10688 * n + l - i >= 0 or i <= l + n
10690 * with n < "m" and i the output dimension at position "pos".
10691 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10692 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10693 * and earlier output dimensions, as well as integer divisions that do
10694 * not involve any of the output dimensions.
10696 * Return the index of the first inequality constraint or bmap->n_ineq
10697 * if no such pair can be found.
10699 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10700 int pos, isl_int m)
10702 int i, j;
10703 isl_ctx *ctx;
10704 unsigned total;
10705 unsigned n_div, o_div;
10706 unsigned n_out, o_out;
10707 int less;
10709 if (!bmap)
10710 return -1;
10712 ctx = isl_basic_map_get_ctx(bmap);
10713 total = isl_basic_map_total_dim(bmap);
10714 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10715 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10716 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10717 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10718 for (i = 0; i < bmap->n_ineq; ++i) {
10719 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10720 continue;
10721 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10722 n_out - (pos + 1)) != -1)
10723 continue;
10724 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10725 0, n_div) < n_div)
10726 continue;
10727 for (j = i + 1; j < bmap->n_ineq; ++j) {
10728 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10729 ctx->one))
10730 continue;
10731 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10732 bmap->ineq[j] + 1, total))
10733 continue;
10734 break;
10736 if (j >= bmap->n_ineq)
10737 continue;
10738 isl_int_add(bmap->ineq[i][0],
10739 bmap->ineq[i][0], bmap->ineq[j][0]);
10740 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10741 isl_int_sub(bmap->ineq[i][0],
10742 bmap->ineq[i][0], bmap->ineq[j][0]);
10743 if (!less)
10744 continue;
10745 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10746 return i;
10747 else
10748 return j;
10751 return bmap->n_ineq;
10754 /* Return the index of the equality of "bmap" that defines
10755 * the output dimension "pos" in terms of earlier dimensions.
10756 * The equality may also involve integer divisions, as long
10757 * as those integer divisions are defined in terms of
10758 * parameters or input dimensions.
10759 * In this case, *div is set to the number of integer divisions and
10760 * *ineq is set to the number of inequality constraints (provided
10761 * div and ineq are not NULL).
10763 * The equality may also involve a single integer division involving
10764 * the output dimensions (typically only output dimension "pos") as
10765 * long as the coefficient of output dimension "pos" is 1 or -1 and
10766 * there is a pair of constraints i >= l and i <= l + n, with i referring
10767 * to output dimension "pos", l an expression involving only earlier
10768 * dimensions and n smaller than the coefficient of the integer division
10769 * in the equality. In this case, the output dimension can be defined
10770 * in terms of a modulo expression that does not involve the integer division.
10771 * *div is then set to this single integer division and
10772 * *ineq is set to the index of constraint i >= l.
10774 * Return bmap->n_eq if there is no such equality.
10775 * Return -1 on error.
10777 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10778 int pos, int *div, int *ineq)
10780 int j, k, l;
10781 unsigned n_out, o_out;
10782 unsigned n_div, o_div;
10784 if (!bmap)
10785 return -1;
10787 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10788 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10789 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10790 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10792 if (ineq)
10793 *ineq = bmap->n_ineq;
10794 if (div)
10795 *div = n_div;
10796 for (j = 0; j < bmap->n_eq; ++j) {
10797 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10798 continue;
10799 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10800 n_out - (pos + 1)) != -1)
10801 continue;
10802 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10803 0, n_div);
10804 if (k >= n_div)
10805 return j;
10806 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10807 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10808 continue;
10809 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10810 k + 1, n_div - (k+1)) < n_div)
10811 continue;
10812 l = find_modulo_constraint_pair(bmap, pos,
10813 bmap->eq[j][o_div + k]);
10814 if (l < 0)
10815 return -1;
10816 if (l >= bmap->n_ineq)
10817 continue;
10818 if (div)
10819 *div = k;
10820 if (ineq)
10821 *ineq = l;
10822 return j;
10825 return bmap->n_eq;
10828 /* Check if the given basic map is obviously single-valued.
10829 * In particular, for each output dimension, check that there is
10830 * an equality that defines the output dimension in terms of
10831 * earlier dimensions.
10833 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10835 int i;
10836 unsigned n_out;
10838 if (!bmap)
10839 return isl_bool_error;
10841 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10843 for (i = 0; i < n_out; ++i) {
10844 int eq;
10846 eq = isl_basic_map_output_defining_equality(bmap, i,
10847 NULL, NULL);
10848 if (eq < 0)
10849 return isl_bool_error;
10850 if (eq >= bmap->n_eq)
10851 return isl_bool_false;
10854 return isl_bool_true;
10857 /* Check if the given basic map is single-valued.
10858 * We simply compute
10860 * M \circ M^-1
10862 * and check if the result is a subset of the identity mapping.
10864 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10866 isl_space *space;
10867 isl_basic_map *test;
10868 isl_basic_map *id;
10869 isl_bool sv;
10871 sv = isl_basic_map_plain_is_single_valued(bmap);
10872 if (sv < 0 || sv)
10873 return sv;
10875 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10876 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10878 space = isl_basic_map_get_space(bmap);
10879 space = isl_space_map_from_set(isl_space_range(space));
10880 id = isl_basic_map_identity(space);
10882 sv = isl_basic_map_is_subset(test, id);
10884 isl_basic_map_free(test);
10885 isl_basic_map_free(id);
10887 return sv;
10890 /* Check if the given map is obviously single-valued.
10892 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10894 if (!map)
10895 return isl_bool_error;
10896 if (map->n == 0)
10897 return isl_bool_true;
10898 if (map->n >= 2)
10899 return isl_bool_false;
10901 return isl_basic_map_plain_is_single_valued(map->p[0]);
10904 /* Check if the given map is single-valued.
10905 * We simply compute
10907 * M \circ M^-1
10909 * and check if the result is a subset of the identity mapping.
10911 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10913 isl_space *dim;
10914 isl_map *test;
10915 isl_map *id;
10916 isl_bool sv;
10918 sv = isl_map_plain_is_single_valued(map);
10919 if (sv < 0 || sv)
10920 return sv;
10922 test = isl_map_reverse(isl_map_copy(map));
10923 test = isl_map_apply_range(test, isl_map_copy(map));
10925 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10926 id = isl_map_identity(dim);
10928 sv = isl_map_is_subset(test, id);
10930 isl_map_free(test);
10931 isl_map_free(id);
10933 return sv;
10936 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10938 isl_bool in;
10940 map = isl_map_copy(map);
10941 map = isl_map_reverse(map);
10942 in = isl_map_is_single_valued(map);
10943 isl_map_free(map);
10945 return in;
10948 /* Check if the given map is obviously injective.
10950 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10952 isl_bool in;
10954 map = isl_map_copy(map);
10955 map = isl_map_reverse(map);
10956 in = isl_map_plain_is_single_valued(map);
10957 isl_map_free(map);
10959 return in;
10962 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10964 isl_bool sv;
10966 sv = isl_map_is_single_valued(map);
10967 if (sv < 0 || !sv)
10968 return sv;
10970 return isl_map_is_injective(map);
10973 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10975 return isl_map_is_single_valued(set_to_map(set));
10978 /* Does "map" only map elements to themselves?
10980 * If the domain and range spaces are different, then "map"
10981 * is considered not to be an identity relation, even if it is empty.
10982 * Otherwise, construct the maximal identity relation and
10983 * check whether "map" is a subset of this relation.
10985 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10987 isl_space *space;
10988 isl_map *id;
10989 isl_bool equal, is_identity;
10991 space = isl_map_get_space(map);
10992 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10993 isl_space_free(space);
10994 if (equal < 0 || !equal)
10995 return equal;
10997 id = isl_map_identity(isl_map_get_space(map));
10998 is_identity = isl_map_is_subset(map, id);
10999 isl_map_free(id);
11001 return is_identity;
11004 int isl_map_is_translation(__isl_keep isl_map *map)
11006 int ok;
11007 isl_set *delta;
11009 delta = isl_map_deltas(isl_map_copy(map));
11010 ok = isl_set_is_singleton(delta);
11011 isl_set_free(delta);
11013 return ok;
11016 static int unique(isl_int *p, unsigned pos, unsigned len)
11018 if (isl_seq_first_non_zero(p, pos) != -1)
11019 return 0;
11020 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11021 return 0;
11022 return 1;
11025 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11027 int i, j;
11028 unsigned nvar;
11029 unsigned ovar;
11031 if (!bset)
11032 return -1;
11034 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11035 return 0;
11037 nvar = isl_basic_set_dim(bset, isl_dim_set);
11038 ovar = isl_space_offset(bset->dim, isl_dim_set);
11039 for (j = 0; j < nvar; ++j) {
11040 int lower = 0, upper = 0;
11041 for (i = 0; i < bset->n_eq; ++i) {
11042 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11043 continue;
11044 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11045 return 0;
11046 break;
11048 if (i < bset->n_eq)
11049 continue;
11050 for (i = 0; i < bset->n_ineq; ++i) {
11051 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11052 continue;
11053 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11054 return 0;
11055 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11056 lower = 1;
11057 else
11058 upper = 1;
11060 if (!lower || !upper)
11061 return 0;
11064 return 1;
11067 int isl_set_is_box(__isl_keep isl_set *set)
11069 if (!set)
11070 return -1;
11071 if (set->n != 1)
11072 return 0;
11074 return isl_basic_set_is_box(set->p[0]);
11077 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11079 if (!bset)
11080 return isl_bool_error;
11082 return isl_space_is_wrapping(bset->dim);
11085 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11087 if (!set)
11088 return isl_bool_error;
11090 return isl_space_is_wrapping(set->dim);
11093 /* Modify the space of "map" through a call to "change".
11094 * If "can_change" is set (not NULL), then first call it to check
11095 * if the modification is allowed, printing the error message "cannot_change"
11096 * if it is not.
11098 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11099 isl_bool (*can_change)(__isl_keep isl_map *map),
11100 const char *cannot_change,
11101 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11103 isl_bool ok;
11104 isl_space *space;
11106 if (!map)
11107 return NULL;
11109 ok = can_change ? can_change(map) : isl_bool_true;
11110 if (ok < 0)
11111 return isl_map_free(map);
11112 if (!ok)
11113 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11114 return isl_map_free(map));
11116 space = change(isl_map_get_space(map));
11117 map = isl_map_reset_space(map, space);
11119 return map;
11122 /* Is the domain of "map" a wrapped relation?
11124 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11126 if (!map)
11127 return isl_bool_error;
11129 return isl_space_domain_is_wrapping(map->dim);
11132 /* Is the range of "map" a wrapped relation?
11134 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11136 if (!map)
11137 return isl_bool_error;
11139 return isl_space_range_is_wrapping(map->dim);
11142 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11144 bmap = isl_basic_map_cow(bmap);
11145 if (!bmap)
11146 return NULL;
11148 bmap->dim = isl_space_wrap(bmap->dim);
11149 if (!bmap->dim)
11150 goto error;
11152 bmap = isl_basic_map_finalize(bmap);
11154 return bset_from_bmap(bmap);
11155 error:
11156 isl_basic_map_free(bmap);
11157 return NULL;
11160 /* Given a map A -> B, return the set (A -> B).
11162 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11164 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11167 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11169 bset = isl_basic_set_cow(bset);
11170 if (!bset)
11171 return NULL;
11173 bset->dim = isl_space_unwrap(bset->dim);
11174 if (!bset->dim)
11175 goto error;
11177 bset = isl_basic_set_finalize(bset);
11179 return bset_to_bmap(bset);
11180 error:
11181 isl_basic_set_free(bset);
11182 return NULL;
11185 /* Given a set (A -> B), return the map A -> B.
11186 * Error out if "set" is not of the form (A -> B).
11188 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11190 return isl_map_change_space(set, &isl_set_is_wrapping,
11191 "not a wrapping set", &isl_space_unwrap);
11194 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11195 enum isl_dim_type type)
11197 if (!bmap)
11198 return NULL;
11200 if (!isl_space_is_named_or_nested(bmap->dim, type))
11201 return bmap;
11203 bmap = isl_basic_map_cow(bmap);
11204 if (!bmap)
11205 return NULL;
11207 bmap->dim = isl_space_reset(bmap->dim, type);
11208 if (!bmap->dim)
11209 goto error;
11211 bmap = isl_basic_map_finalize(bmap);
11213 return bmap;
11214 error:
11215 isl_basic_map_free(bmap);
11216 return NULL;
11219 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11220 enum isl_dim_type type)
11222 int i;
11224 if (!map)
11225 return NULL;
11227 if (!isl_space_is_named_or_nested(map->dim, type))
11228 return map;
11230 map = isl_map_cow(map);
11231 if (!map)
11232 return NULL;
11234 for (i = 0; i < map->n; ++i) {
11235 map->p[i] = isl_basic_map_reset(map->p[i], type);
11236 if (!map->p[i])
11237 goto error;
11239 map->dim = isl_space_reset(map->dim, type);
11240 if (!map->dim)
11241 goto error;
11243 return map;
11244 error:
11245 isl_map_free(map);
11246 return NULL;
11249 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11251 if (!bmap)
11252 return NULL;
11254 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11255 return bmap;
11257 bmap = isl_basic_map_cow(bmap);
11258 if (!bmap)
11259 return NULL;
11261 bmap->dim = isl_space_flatten(bmap->dim);
11262 if (!bmap->dim)
11263 goto error;
11265 bmap = isl_basic_map_finalize(bmap);
11267 return bmap;
11268 error:
11269 isl_basic_map_free(bmap);
11270 return NULL;
11273 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11275 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11278 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11279 __isl_take isl_basic_map *bmap)
11281 if (!bmap)
11282 return NULL;
11284 if (!bmap->dim->nested[0])
11285 return bmap;
11287 bmap = isl_basic_map_cow(bmap);
11288 if (!bmap)
11289 return NULL;
11291 bmap->dim = isl_space_flatten_domain(bmap->dim);
11292 if (!bmap->dim)
11293 goto error;
11295 bmap = isl_basic_map_finalize(bmap);
11297 return bmap;
11298 error:
11299 isl_basic_map_free(bmap);
11300 return NULL;
11303 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11304 __isl_take isl_basic_map *bmap)
11306 if (!bmap)
11307 return NULL;
11309 if (!bmap->dim->nested[1])
11310 return bmap;
11312 bmap = isl_basic_map_cow(bmap);
11313 if (!bmap)
11314 return NULL;
11316 bmap->dim = isl_space_flatten_range(bmap->dim);
11317 if (!bmap->dim)
11318 goto error;
11320 bmap = isl_basic_map_finalize(bmap);
11322 return bmap;
11323 error:
11324 isl_basic_map_free(bmap);
11325 return NULL;
11328 /* Remove any internal structure from the spaces of domain and range of "map".
11330 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11332 if (!map)
11333 return NULL;
11335 if (!map->dim->nested[0] && !map->dim->nested[1])
11336 return map;
11338 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11341 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11343 return set_from_map(isl_map_flatten(set_to_map(set)));
11346 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11348 isl_space *dim, *flat_dim;
11349 isl_map *map;
11351 dim = isl_set_get_space(set);
11352 flat_dim = isl_space_flatten(isl_space_copy(dim));
11353 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11354 map = isl_map_intersect_domain(map, set);
11356 return map;
11359 /* Remove any internal structure from the space of the domain of "map".
11361 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11363 if (!map)
11364 return NULL;
11366 if (!map->dim->nested[0])
11367 return map;
11369 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11372 /* Remove any internal structure from the space of the range of "map".
11374 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11376 if (!map)
11377 return NULL;
11379 if (!map->dim->nested[1])
11380 return map;
11382 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11385 /* Reorder the dimensions of "bmap" according to the given dim_map
11386 * and set the dimension specification to "dim" and
11387 * perform Gaussian elimination on the result.
11389 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11390 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11392 isl_basic_map *res;
11393 unsigned flags;
11395 bmap = isl_basic_map_cow(bmap);
11396 if (!bmap || !dim || !dim_map)
11397 goto error;
11399 flags = bmap->flags;
11400 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11401 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11402 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11403 res = isl_basic_map_alloc_space(dim,
11404 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11405 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11406 if (res)
11407 res->flags = flags;
11408 res = isl_basic_map_gauss(res, NULL);
11409 res = isl_basic_map_finalize(res);
11410 return res;
11411 error:
11412 free(dim_map);
11413 isl_basic_map_free(bmap);
11414 isl_space_free(dim);
11415 return NULL;
11418 /* Reorder the dimensions of "map" according to given reordering.
11420 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11421 __isl_take isl_reordering *r)
11423 int i;
11424 struct isl_dim_map *dim_map;
11426 map = isl_map_cow(map);
11427 dim_map = isl_dim_map_from_reordering(r);
11428 if (!map || !r || !dim_map)
11429 goto error;
11431 for (i = 0; i < map->n; ++i) {
11432 struct isl_dim_map *dim_map_i;
11434 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11436 map->p[i] = isl_basic_map_realign(map->p[i],
11437 isl_space_copy(r->dim), dim_map_i);
11439 if (!map->p[i])
11440 goto error;
11443 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11445 isl_reordering_free(r);
11446 free(dim_map);
11447 return map;
11448 error:
11449 free(dim_map);
11450 isl_map_free(map);
11451 isl_reordering_free(r);
11452 return NULL;
11455 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11456 __isl_take isl_reordering *r)
11458 return set_from_map(isl_map_realign(set_to_map(set), r));
11461 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11462 __isl_take isl_space *model)
11464 isl_ctx *ctx;
11466 if (!map || !model)
11467 goto error;
11469 ctx = isl_space_get_ctx(model);
11470 if (!isl_space_has_named_params(model))
11471 isl_die(ctx, isl_error_invalid,
11472 "model has unnamed parameters", goto error);
11473 if (!isl_space_has_named_params(map->dim))
11474 isl_die(ctx, isl_error_invalid,
11475 "relation has unnamed parameters", goto error);
11476 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11477 isl_reordering *exp;
11479 model = isl_space_drop_dims(model, isl_dim_in,
11480 0, isl_space_dim(model, isl_dim_in));
11481 model = isl_space_drop_dims(model, isl_dim_out,
11482 0, isl_space_dim(model, isl_dim_out));
11483 exp = isl_parameter_alignment_reordering(map->dim, model);
11484 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11485 map = isl_map_realign(map, exp);
11488 isl_space_free(model);
11489 return map;
11490 error:
11491 isl_space_free(model);
11492 isl_map_free(map);
11493 return NULL;
11496 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11497 __isl_take isl_space *model)
11499 return isl_map_align_params(set, model);
11502 /* Align the parameters of "bmap" to those of "model", introducing
11503 * additional parameters if needed.
11505 __isl_give isl_basic_map *isl_basic_map_align_params(
11506 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11508 isl_ctx *ctx;
11510 if (!bmap || !model)
11511 goto error;
11513 ctx = isl_space_get_ctx(model);
11514 if (!isl_space_has_named_params(model))
11515 isl_die(ctx, isl_error_invalid,
11516 "model has unnamed parameters", goto error);
11517 if (!isl_space_has_named_params(bmap->dim))
11518 isl_die(ctx, isl_error_invalid,
11519 "relation has unnamed parameters", goto error);
11520 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11521 isl_reordering *exp;
11522 struct isl_dim_map *dim_map;
11524 model = isl_space_drop_dims(model, isl_dim_in,
11525 0, isl_space_dim(model, isl_dim_in));
11526 model = isl_space_drop_dims(model, isl_dim_out,
11527 0, isl_space_dim(model, isl_dim_out));
11528 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11529 exp = isl_reordering_extend_space(exp,
11530 isl_basic_map_get_space(bmap));
11531 dim_map = isl_dim_map_from_reordering(exp);
11532 bmap = isl_basic_map_realign(bmap,
11533 exp ? isl_space_copy(exp->dim) : NULL,
11534 isl_dim_map_extend(dim_map, bmap));
11535 isl_reordering_free(exp);
11536 free(dim_map);
11539 isl_space_free(model);
11540 return bmap;
11541 error:
11542 isl_space_free(model);
11543 isl_basic_map_free(bmap);
11544 return NULL;
11547 /* Align the parameters of "bset" to those of "model", introducing
11548 * additional parameters if needed.
11550 __isl_give isl_basic_set *isl_basic_set_align_params(
11551 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11553 return isl_basic_map_align_params(bset, model);
11556 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11557 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11558 enum isl_dim_type c2, enum isl_dim_type c3,
11559 enum isl_dim_type c4, enum isl_dim_type c5)
11561 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11562 struct isl_mat *mat;
11563 int i, j, k;
11564 int pos;
11566 if (!bmap)
11567 return NULL;
11568 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11569 isl_basic_map_total_dim(bmap) + 1);
11570 if (!mat)
11571 return NULL;
11572 for (i = 0; i < bmap->n_eq; ++i)
11573 for (j = 0, pos = 0; j < 5; ++j) {
11574 int off = isl_basic_map_offset(bmap, c[j]);
11575 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11576 isl_int_set(mat->row[i][pos],
11577 bmap->eq[i][off + k]);
11578 ++pos;
11582 return mat;
11585 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11586 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11587 enum isl_dim_type c2, enum isl_dim_type c3,
11588 enum isl_dim_type c4, enum isl_dim_type c5)
11590 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11591 struct isl_mat *mat;
11592 int i, j, k;
11593 int pos;
11595 if (!bmap)
11596 return NULL;
11597 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11598 isl_basic_map_total_dim(bmap) + 1);
11599 if (!mat)
11600 return NULL;
11601 for (i = 0; i < bmap->n_ineq; ++i)
11602 for (j = 0, pos = 0; j < 5; ++j) {
11603 int off = isl_basic_map_offset(bmap, c[j]);
11604 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11605 isl_int_set(mat->row[i][pos],
11606 bmap->ineq[i][off + k]);
11607 ++pos;
11611 return mat;
11614 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11615 __isl_take isl_space *dim,
11616 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11617 enum isl_dim_type c2, enum isl_dim_type c3,
11618 enum isl_dim_type c4, enum isl_dim_type c5)
11620 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11621 isl_basic_map *bmap;
11622 unsigned total;
11623 unsigned extra;
11624 int i, j, k, l;
11625 int pos;
11627 if (!dim || !eq || !ineq)
11628 goto error;
11630 if (eq->n_col != ineq->n_col)
11631 isl_die(dim->ctx, isl_error_invalid,
11632 "equalities and inequalities matrices should have "
11633 "same number of columns", goto error);
11635 total = 1 + isl_space_dim(dim, isl_dim_all);
11637 if (eq->n_col < total)
11638 isl_die(dim->ctx, isl_error_invalid,
11639 "number of columns too small", goto error);
11641 extra = eq->n_col - total;
11643 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11644 eq->n_row, ineq->n_row);
11645 if (!bmap)
11646 goto error;
11647 for (i = 0; i < extra; ++i) {
11648 k = isl_basic_map_alloc_div(bmap);
11649 if (k < 0)
11650 goto error;
11651 isl_int_set_si(bmap->div[k][0], 0);
11653 for (i = 0; i < eq->n_row; ++i) {
11654 l = isl_basic_map_alloc_equality(bmap);
11655 if (l < 0)
11656 goto error;
11657 for (j = 0, pos = 0; j < 5; ++j) {
11658 int off = isl_basic_map_offset(bmap, c[j]);
11659 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11660 isl_int_set(bmap->eq[l][off + k],
11661 eq->row[i][pos]);
11662 ++pos;
11666 for (i = 0; i < ineq->n_row; ++i) {
11667 l = isl_basic_map_alloc_inequality(bmap);
11668 if (l < 0)
11669 goto error;
11670 for (j = 0, pos = 0; j < 5; ++j) {
11671 int off = isl_basic_map_offset(bmap, c[j]);
11672 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11673 isl_int_set(bmap->ineq[l][off + k],
11674 ineq->row[i][pos]);
11675 ++pos;
11680 isl_space_free(dim);
11681 isl_mat_free(eq);
11682 isl_mat_free(ineq);
11684 bmap = isl_basic_map_simplify(bmap);
11685 return isl_basic_map_finalize(bmap);
11686 error:
11687 isl_space_free(dim);
11688 isl_mat_free(eq);
11689 isl_mat_free(ineq);
11690 return NULL;
11693 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11694 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11695 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11697 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11698 c1, c2, c3, c4, isl_dim_in);
11701 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11702 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11703 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11705 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11706 c1, c2, c3, c4, isl_dim_in);
11709 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11710 __isl_take isl_space *dim,
11711 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11712 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11714 isl_basic_map *bmap;
11715 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11716 c1, c2, c3, c4, isl_dim_in);
11717 return bset_from_bmap(bmap);
11720 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11722 if (!bmap)
11723 return isl_bool_error;
11725 return isl_space_can_zip(bmap->dim);
11728 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11730 if (!map)
11731 return isl_bool_error;
11733 return isl_space_can_zip(map->dim);
11736 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11737 * (A -> C) -> (B -> D).
11739 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11741 unsigned pos;
11742 unsigned n1;
11743 unsigned n2;
11745 if (!bmap)
11746 return NULL;
11748 if (!isl_basic_map_can_zip(bmap))
11749 isl_die(bmap->ctx, isl_error_invalid,
11750 "basic map cannot be zipped", goto error);
11751 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11752 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11753 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11754 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11755 bmap = isl_basic_map_cow(bmap);
11756 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11757 if (!bmap)
11758 return NULL;
11759 bmap->dim = isl_space_zip(bmap->dim);
11760 if (!bmap->dim)
11761 goto error;
11762 bmap = isl_basic_map_mark_final(bmap);
11763 return bmap;
11764 error:
11765 isl_basic_map_free(bmap);
11766 return NULL;
11769 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11770 * (A -> C) -> (B -> D).
11772 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11774 int i;
11776 if (!map)
11777 return NULL;
11779 if (!isl_map_can_zip(map))
11780 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11781 goto error);
11783 map = isl_map_cow(map);
11784 if (!map)
11785 return NULL;
11787 for (i = 0; i < map->n; ++i) {
11788 map->p[i] = isl_basic_map_zip(map->p[i]);
11789 if (!map->p[i])
11790 goto error;
11793 map->dim = isl_space_zip(map->dim);
11794 if (!map->dim)
11795 goto error;
11797 return map;
11798 error:
11799 isl_map_free(map);
11800 return NULL;
11803 /* Can we apply isl_basic_map_curry to "bmap"?
11804 * That is, does it have a nested relation in its domain?
11806 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11808 if (!bmap)
11809 return isl_bool_error;
11811 return isl_space_can_curry(bmap->dim);
11814 /* Can we apply isl_map_curry to "map"?
11815 * That is, does it have a nested relation in its domain?
11817 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11819 if (!map)
11820 return isl_bool_error;
11822 return isl_space_can_curry(map->dim);
11825 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11826 * A -> (B -> C).
11828 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11831 if (!bmap)
11832 return NULL;
11834 if (!isl_basic_map_can_curry(bmap))
11835 isl_die(bmap->ctx, isl_error_invalid,
11836 "basic map cannot be curried", goto error);
11837 bmap = isl_basic_map_cow(bmap);
11838 if (!bmap)
11839 return NULL;
11840 bmap->dim = isl_space_curry(bmap->dim);
11841 if (!bmap->dim)
11842 goto error;
11843 bmap = isl_basic_map_mark_final(bmap);
11844 return bmap;
11845 error:
11846 isl_basic_map_free(bmap);
11847 return NULL;
11850 /* Given a map (A -> B) -> C, return the corresponding map
11851 * A -> (B -> C).
11853 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11855 return isl_map_change_space(map, &isl_map_can_curry,
11856 "map cannot be curried", &isl_space_curry);
11859 /* Can isl_map_range_curry be applied to "map"?
11860 * That is, does it have a nested relation in its range,
11861 * the domain of which is itself a nested relation?
11863 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11865 if (!map)
11866 return isl_bool_error;
11868 return isl_space_can_range_curry(map->dim);
11871 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11872 * A -> (B -> (C -> D)).
11874 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11876 return isl_map_change_space(map, &isl_map_can_range_curry,
11877 "map range cannot be curried",
11878 &isl_space_range_curry);
11881 /* Can we apply isl_basic_map_uncurry to "bmap"?
11882 * That is, does it have a nested relation in its domain?
11884 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11886 if (!bmap)
11887 return isl_bool_error;
11889 return isl_space_can_uncurry(bmap->dim);
11892 /* Can we apply isl_map_uncurry to "map"?
11893 * That is, does it have a nested relation in its domain?
11895 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11897 if (!map)
11898 return isl_bool_error;
11900 return isl_space_can_uncurry(map->dim);
11903 /* Given a basic map A -> (B -> C), return the corresponding basic map
11904 * (A -> B) -> C.
11906 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11909 if (!bmap)
11910 return NULL;
11912 if (!isl_basic_map_can_uncurry(bmap))
11913 isl_die(bmap->ctx, isl_error_invalid,
11914 "basic map cannot be uncurried",
11915 return isl_basic_map_free(bmap));
11916 bmap = isl_basic_map_cow(bmap);
11917 if (!bmap)
11918 return NULL;
11919 bmap->dim = isl_space_uncurry(bmap->dim);
11920 if (!bmap->dim)
11921 return isl_basic_map_free(bmap);
11922 bmap = isl_basic_map_mark_final(bmap);
11923 return bmap;
11926 /* Given a map A -> (B -> C), return the corresponding map
11927 * (A -> B) -> C.
11929 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11931 return isl_map_change_space(map, &isl_map_can_uncurry,
11932 "map cannot be uncurried", &isl_space_uncurry);
11935 /* Construct a basic map mapping the domain of the affine expression
11936 * to a one-dimensional range prescribed by the affine expression.
11937 * If "rational" is set, then construct a rational basic map.
11939 * A NaN affine expression cannot be converted to a basic map.
11941 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11942 __isl_take isl_aff *aff, int rational)
11944 int k;
11945 int pos;
11946 isl_bool is_nan;
11947 isl_local_space *ls;
11948 isl_basic_map *bmap = NULL;
11950 if (!aff)
11951 return NULL;
11952 is_nan = isl_aff_is_nan(aff);
11953 if (is_nan < 0)
11954 goto error;
11955 if (is_nan)
11956 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11957 "cannot convert NaN", goto error);
11959 ls = isl_aff_get_local_space(aff);
11960 bmap = isl_basic_map_from_local_space(ls);
11961 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11962 k = isl_basic_map_alloc_equality(bmap);
11963 if (k < 0)
11964 goto error;
11966 pos = isl_basic_map_offset(bmap, isl_dim_out);
11967 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11968 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11969 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11970 aff->v->size - (pos + 1));
11972 isl_aff_free(aff);
11973 if (rational)
11974 bmap = isl_basic_map_set_rational(bmap);
11975 bmap = isl_basic_map_finalize(bmap);
11976 return bmap;
11977 error:
11978 isl_aff_free(aff);
11979 isl_basic_map_free(bmap);
11980 return NULL;
11983 /* Construct a basic map mapping the domain of the affine expression
11984 * to a one-dimensional range prescribed by the affine expression.
11986 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11988 return isl_basic_map_from_aff2(aff, 0);
11991 /* Construct a map mapping the domain of the affine expression
11992 * to a one-dimensional range prescribed by the affine expression.
11994 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11996 isl_basic_map *bmap;
11998 bmap = isl_basic_map_from_aff(aff);
11999 return isl_map_from_basic_map(bmap);
12002 /* Construct a basic map mapping the domain the multi-affine expression
12003 * to its range, with each dimension in the range equated to the
12004 * corresponding affine expression.
12005 * If "rational" is set, then construct a rational basic map.
12007 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12008 __isl_take isl_multi_aff *maff, int rational)
12010 int i;
12011 isl_space *space;
12012 isl_basic_map *bmap;
12014 if (!maff)
12015 return NULL;
12017 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12018 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12019 "invalid space", goto error);
12021 space = isl_space_domain(isl_multi_aff_get_space(maff));
12022 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12023 if (rational)
12024 bmap = isl_basic_map_set_rational(bmap);
12026 for (i = 0; i < maff->n; ++i) {
12027 isl_aff *aff;
12028 isl_basic_map *bmap_i;
12030 aff = isl_aff_copy(maff->p[i]);
12031 bmap_i = isl_basic_map_from_aff2(aff, rational);
12033 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12036 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12038 isl_multi_aff_free(maff);
12039 return bmap;
12040 error:
12041 isl_multi_aff_free(maff);
12042 return NULL;
12045 /* Construct a basic map mapping the domain the multi-affine expression
12046 * to its range, with each dimension in the range equated to the
12047 * corresponding affine expression.
12049 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12050 __isl_take isl_multi_aff *ma)
12052 return isl_basic_map_from_multi_aff2(ma, 0);
12055 /* Construct a map mapping the domain the multi-affine expression
12056 * to its range, with each dimension in the range equated to the
12057 * corresponding affine expression.
12059 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12061 isl_basic_map *bmap;
12063 bmap = isl_basic_map_from_multi_aff(maff);
12064 return isl_map_from_basic_map(bmap);
12067 /* Construct a basic map mapping a domain in the given space to
12068 * to an n-dimensional range, with n the number of elements in the list,
12069 * where each coordinate in the range is prescribed by the
12070 * corresponding affine expression.
12071 * The domains of all affine expressions in the list are assumed to match
12072 * domain_dim.
12074 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12075 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12077 int i;
12078 isl_space *dim;
12079 isl_basic_map *bmap;
12081 if (!list)
12082 return NULL;
12084 dim = isl_space_from_domain(domain_dim);
12085 bmap = isl_basic_map_universe(dim);
12087 for (i = 0; i < list->n; ++i) {
12088 isl_aff *aff;
12089 isl_basic_map *bmap_i;
12091 aff = isl_aff_copy(list->p[i]);
12092 bmap_i = isl_basic_map_from_aff(aff);
12094 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12097 isl_aff_list_free(list);
12098 return bmap;
12101 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12102 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12104 return isl_map_equate(set, type1, pos1, type2, pos2);
12107 /* Construct a basic map where the given dimensions are equal to each other.
12109 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12110 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12112 isl_basic_map *bmap = NULL;
12113 int i;
12115 if (!space)
12116 return NULL;
12118 if (pos1 >= isl_space_dim(space, type1))
12119 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12120 "index out of bounds", goto error);
12121 if (pos2 >= isl_space_dim(space, type2))
12122 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12123 "index out of bounds", goto error);
12125 if (type1 == type2 && pos1 == pos2)
12126 return isl_basic_map_universe(space);
12128 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12129 i = isl_basic_map_alloc_equality(bmap);
12130 if (i < 0)
12131 goto error;
12132 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12133 pos1 += isl_basic_map_offset(bmap, type1);
12134 pos2 += isl_basic_map_offset(bmap, type2);
12135 isl_int_set_si(bmap->eq[i][pos1], -1);
12136 isl_int_set_si(bmap->eq[i][pos2], 1);
12137 bmap = isl_basic_map_finalize(bmap);
12138 isl_space_free(space);
12139 return bmap;
12140 error:
12141 isl_space_free(space);
12142 isl_basic_map_free(bmap);
12143 return NULL;
12146 /* Add a constraint imposing that the given two dimensions are equal.
12148 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12149 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12151 isl_basic_map *eq;
12153 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12155 bmap = isl_basic_map_intersect(bmap, eq);
12157 return bmap;
12160 /* Add a constraint imposing that the given two dimensions are equal.
12162 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12163 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12165 isl_basic_map *bmap;
12167 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12169 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12171 return map;
12174 /* Add a constraint imposing that the given two dimensions have opposite values.
12176 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12177 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12179 isl_basic_map *bmap = NULL;
12180 int i;
12182 if (!map)
12183 return NULL;
12185 if (pos1 >= isl_map_dim(map, type1))
12186 isl_die(map->ctx, isl_error_invalid,
12187 "index out of bounds", goto error);
12188 if (pos2 >= isl_map_dim(map, type2))
12189 isl_die(map->ctx, isl_error_invalid,
12190 "index out of bounds", goto error);
12192 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12193 i = isl_basic_map_alloc_equality(bmap);
12194 if (i < 0)
12195 goto error;
12196 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12197 pos1 += isl_basic_map_offset(bmap, type1);
12198 pos2 += isl_basic_map_offset(bmap, type2);
12199 isl_int_set_si(bmap->eq[i][pos1], 1);
12200 isl_int_set_si(bmap->eq[i][pos2], 1);
12201 bmap = isl_basic_map_finalize(bmap);
12203 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12205 return map;
12206 error:
12207 isl_basic_map_free(bmap);
12208 isl_map_free(map);
12209 return NULL;
12212 /* Construct a constraint imposing that the value of the first dimension is
12213 * greater than or equal to that of the second.
12215 static __isl_give isl_constraint *constraint_order_ge(
12216 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12217 enum isl_dim_type type2, int pos2)
12219 isl_constraint *c;
12221 if (!space)
12222 return NULL;
12224 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12226 if (pos1 >= isl_constraint_dim(c, type1))
12227 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12228 "index out of bounds", return isl_constraint_free(c));
12229 if (pos2 >= isl_constraint_dim(c, type2))
12230 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12231 "index out of bounds", return isl_constraint_free(c));
12233 if (type1 == type2 && pos1 == pos2)
12234 return c;
12236 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12237 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12239 return c;
12242 /* Add a constraint imposing that the value of the first dimension is
12243 * greater than or equal to that of the second.
12245 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12246 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12248 isl_constraint *c;
12249 isl_space *space;
12251 if (type1 == type2 && pos1 == pos2)
12252 return bmap;
12253 space = isl_basic_map_get_space(bmap);
12254 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12255 bmap = isl_basic_map_add_constraint(bmap, c);
12257 return bmap;
12260 /* Add a constraint imposing that the value of the first dimension is
12261 * greater than or equal to that of the second.
12263 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12264 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12266 isl_constraint *c;
12267 isl_space *space;
12269 if (type1 == type2 && pos1 == pos2)
12270 return map;
12271 space = isl_map_get_space(map);
12272 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12273 map = isl_map_add_constraint(map, c);
12275 return map;
12278 /* Add a constraint imposing that the value of the first dimension is
12279 * less than or equal to that of the second.
12281 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12282 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12284 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12287 /* Construct a basic map where the value of the first dimension is
12288 * greater than that of the second.
12290 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12291 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12293 isl_basic_map *bmap = NULL;
12294 int i;
12296 if (!space)
12297 return NULL;
12299 if (pos1 >= isl_space_dim(space, type1))
12300 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12301 "index out of bounds", goto error);
12302 if (pos2 >= isl_space_dim(space, type2))
12303 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12304 "index out of bounds", goto error);
12306 if (type1 == type2 && pos1 == pos2)
12307 return isl_basic_map_empty(space);
12309 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12310 i = isl_basic_map_alloc_inequality(bmap);
12311 if (i < 0)
12312 return isl_basic_map_free(bmap);
12313 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12314 pos1 += isl_basic_map_offset(bmap, type1);
12315 pos2 += isl_basic_map_offset(bmap, type2);
12316 isl_int_set_si(bmap->ineq[i][pos1], 1);
12317 isl_int_set_si(bmap->ineq[i][pos2], -1);
12318 isl_int_set_si(bmap->ineq[i][0], -1);
12319 bmap = isl_basic_map_finalize(bmap);
12321 return bmap;
12322 error:
12323 isl_space_free(space);
12324 isl_basic_map_free(bmap);
12325 return NULL;
12328 /* Add a constraint imposing that the value of the first dimension is
12329 * greater than that of the second.
12331 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12332 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12334 isl_basic_map *gt;
12336 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12338 bmap = isl_basic_map_intersect(bmap, gt);
12340 return bmap;
12343 /* Add a constraint imposing that the value of the first dimension is
12344 * greater than that of the second.
12346 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12347 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12349 isl_basic_map *bmap;
12351 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12353 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12355 return map;
12358 /* Add a constraint imposing that the value of the first dimension is
12359 * smaller than that of the second.
12361 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12362 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12364 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12367 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12368 int pos)
12370 isl_aff *div;
12371 isl_local_space *ls;
12373 if (!bmap)
12374 return NULL;
12376 if (!isl_basic_map_divs_known(bmap))
12377 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12378 "some divs are unknown", return NULL);
12380 ls = isl_basic_map_get_local_space(bmap);
12381 div = isl_local_space_get_div(ls, pos);
12382 isl_local_space_free(ls);
12384 return div;
12387 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12388 int pos)
12390 return isl_basic_map_get_div(bset, pos);
12393 /* Plug in "subs" for dimension "type", "pos" of "bset".
12395 * Let i be the dimension to replace and let "subs" be of the form
12397 * f/d
12399 * Any integer division with a non-zero coefficient for i,
12401 * floor((a i + g)/m)
12403 * is replaced by
12405 * floor((a f + d g)/(m d))
12407 * Constraints of the form
12409 * a i + g
12411 * are replaced by
12413 * a f + d g
12415 * We currently require that "subs" is an integral expression.
12416 * Handling rational expressions may require us to add stride constraints
12417 * as we do in isl_basic_set_preimage_multi_aff.
12419 __isl_give isl_basic_set *isl_basic_set_substitute(
12420 __isl_take isl_basic_set *bset,
12421 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12423 int i;
12424 isl_int v;
12425 isl_ctx *ctx;
12427 if (bset && isl_basic_set_plain_is_empty(bset))
12428 return bset;
12430 bset = isl_basic_set_cow(bset);
12431 if (!bset || !subs)
12432 goto error;
12434 ctx = isl_basic_set_get_ctx(bset);
12435 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12436 isl_die(ctx, isl_error_invalid,
12437 "spaces don't match", goto error);
12438 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12439 isl_die(ctx, isl_error_unsupported,
12440 "cannot handle divs yet", goto error);
12441 if (!isl_int_is_one(subs->v->el[0]))
12442 isl_die(ctx, isl_error_invalid,
12443 "can only substitute integer expressions", goto error);
12445 pos += isl_basic_set_offset(bset, type);
12447 isl_int_init(v);
12449 for (i = 0; i < bset->n_eq; ++i) {
12450 if (isl_int_is_zero(bset->eq[i][pos]))
12451 continue;
12452 isl_int_set(v, bset->eq[i][pos]);
12453 isl_int_set_si(bset->eq[i][pos], 0);
12454 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12455 v, subs->v->el + 1, subs->v->size - 1);
12458 for (i = 0; i < bset->n_ineq; ++i) {
12459 if (isl_int_is_zero(bset->ineq[i][pos]))
12460 continue;
12461 isl_int_set(v, bset->ineq[i][pos]);
12462 isl_int_set_si(bset->ineq[i][pos], 0);
12463 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12464 v, subs->v->el + 1, subs->v->size - 1);
12467 for (i = 0; i < bset->n_div; ++i) {
12468 if (isl_int_is_zero(bset->div[i][1 + pos]))
12469 continue;
12470 isl_int_set(v, bset->div[i][1 + pos]);
12471 isl_int_set_si(bset->div[i][1 + pos], 0);
12472 isl_seq_combine(bset->div[i] + 1,
12473 subs->v->el[0], bset->div[i] + 1,
12474 v, subs->v->el + 1, subs->v->size - 1);
12475 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12478 isl_int_clear(v);
12480 bset = isl_basic_set_simplify(bset);
12481 return isl_basic_set_finalize(bset);
12482 error:
12483 isl_basic_set_free(bset);
12484 return NULL;
12487 /* Plug in "subs" for dimension "type", "pos" of "set".
12489 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12490 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12492 int i;
12494 if (set && isl_set_plain_is_empty(set))
12495 return set;
12497 set = isl_set_cow(set);
12498 if (!set || !subs)
12499 goto error;
12501 for (i = set->n - 1; i >= 0; --i) {
12502 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12503 if (remove_if_empty(set, i) < 0)
12504 goto error;
12507 return set;
12508 error:
12509 isl_set_free(set);
12510 return NULL;
12513 /* Check if the range of "ma" is compatible with the domain or range
12514 * (depending on "type") of "bmap".
12515 * Return -1 if anything is wrong.
12517 static int check_basic_map_compatible_range_multi_aff(
12518 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12519 __isl_keep isl_multi_aff *ma)
12521 int m;
12522 isl_space *ma_space;
12524 ma_space = isl_multi_aff_get_space(ma);
12526 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12527 if (m < 0)
12528 goto error;
12529 if (!m)
12530 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12531 "parameters don't match", goto error);
12532 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12533 if (m < 0)
12534 goto error;
12535 if (!m)
12536 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12537 "spaces don't match", goto error);
12539 isl_space_free(ma_space);
12540 return m;
12541 error:
12542 isl_space_free(ma_space);
12543 return -1;
12546 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12547 * coefficients before the transformed range of dimensions,
12548 * the "n_after" coefficients after the transformed range of dimensions
12549 * and the coefficients of the other divs in "bmap".
12551 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12552 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12554 int i;
12555 int n_param;
12556 int n_set;
12557 isl_local_space *ls;
12559 if (n_div == 0)
12560 return 0;
12562 ls = isl_aff_get_domain_local_space(ma->p[0]);
12563 if (!ls)
12564 return -1;
12566 n_param = isl_local_space_dim(ls, isl_dim_param);
12567 n_set = isl_local_space_dim(ls, isl_dim_set);
12568 for (i = 0; i < n_div; ++i) {
12569 int o_bmap = 0, o_ls = 0;
12571 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12572 o_bmap += 1 + 1 + n_param;
12573 o_ls += 1 + 1 + n_param;
12574 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12575 o_bmap += n_before;
12576 isl_seq_cpy(bmap->div[i] + o_bmap,
12577 ls->div->row[i] + o_ls, n_set);
12578 o_bmap += n_set;
12579 o_ls += n_set;
12580 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12581 o_bmap += n_after;
12582 isl_seq_cpy(bmap->div[i] + o_bmap,
12583 ls->div->row[i] + o_ls, n_div);
12584 o_bmap += n_div;
12585 o_ls += n_div;
12586 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12587 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12588 goto error;
12591 isl_local_space_free(ls);
12592 return 0;
12593 error:
12594 isl_local_space_free(ls);
12595 return -1;
12598 /* How many stride constraints does "ma" enforce?
12599 * That is, how many of the affine expressions have a denominator
12600 * different from one?
12602 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12604 int i;
12605 int strides = 0;
12607 for (i = 0; i < ma->n; ++i)
12608 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12609 strides++;
12611 return strides;
12614 /* For each affine expression in ma of the form
12616 * x_i = (f_i y + h_i)/m_i
12618 * with m_i different from one, add a constraint to "bmap"
12619 * of the form
12621 * f_i y + h_i = m_i alpha_i
12623 * with alpha_i an additional existentially quantified variable.
12625 * The input variables of "ma" correspond to a subset of the variables
12626 * of "bmap". There are "n_before" variables in "bmap" before this
12627 * subset and "n_after" variables after this subset.
12628 * The integer divisions of the affine expressions in "ma" are assumed
12629 * to have been aligned. There are "n_div_ma" of them and
12630 * they appear first in "bmap", straight after the "n_after" variables.
12632 static __isl_give isl_basic_map *add_ma_strides(
12633 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12634 int n_before, int n_after, int n_div_ma)
12636 int i, k;
12637 int div;
12638 int total;
12639 int n_param;
12640 int n_in;
12642 total = isl_basic_map_total_dim(bmap);
12643 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12644 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12645 for (i = 0; i < ma->n; ++i) {
12646 int o_bmap = 0, o_ma = 1;
12648 if (isl_int_is_one(ma->p[i]->v->el[0]))
12649 continue;
12650 div = isl_basic_map_alloc_div(bmap);
12651 k = isl_basic_map_alloc_equality(bmap);
12652 if (div < 0 || k < 0)
12653 goto error;
12654 isl_int_set_si(bmap->div[div][0], 0);
12655 isl_seq_cpy(bmap->eq[k] + o_bmap,
12656 ma->p[i]->v->el + o_ma, 1 + n_param);
12657 o_bmap += 1 + n_param;
12658 o_ma += 1 + n_param;
12659 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12660 o_bmap += n_before;
12661 isl_seq_cpy(bmap->eq[k] + o_bmap,
12662 ma->p[i]->v->el + o_ma, n_in);
12663 o_bmap += n_in;
12664 o_ma += n_in;
12665 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12666 o_bmap += n_after;
12667 isl_seq_cpy(bmap->eq[k] + o_bmap,
12668 ma->p[i]->v->el + o_ma, n_div_ma);
12669 o_bmap += n_div_ma;
12670 o_ma += n_div_ma;
12671 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12672 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12673 total++;
12676 return bmap;
12677 error:
12678 isl_basic_map_free(bmap);
12679 return NULL;
12682 /* Replace the domain or range space (depending on "type) of "space" by "set".
12684 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12685 enum isl_dim_type type, __isl_take isl_space *set)
12687 if (type == isl_dim_in) {
12688 space = isl_space_range(space);
12689 space = isl_space_map_from_domain_and_range(set, space);
12690 } else {
12691 space = isl_space_domain(space);
12692 space = isl_space_map_from_domain_and_range(space, set);
12695 return space;
12698 /* Compute the preimage of the domain or range (depending on "type")
12699 * of "bmap" under the function represented by "ma".
12700 * In other words, plug in "ma" in the domain or range of "bmap".
12701 * The result is a basic map that lives in the same space as "bmap"
12702 * except that the domain or range has been replaced by
12703 * the domain space of "ma".
12705 * If bmap is represented by
12707 * A(p) + S u + B x + T v + C(divs) >= 0,
12709 * where u and x are input and output dimensions if type == isl_dim_out
12710 * while x and v are input and output dimensions if type == isl_dim_in,
12711 * and ma is represented by
12713 * x = D(p) + F(y) + G(divs')
12715 * then the result is
12717 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12719 * The divs in the input set are similarly adjusted.
12720 * In particular
12722 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12724 * becomes
12726 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12727 * B_i G(divs') + c_i(divs))/n_i)
12729 * If bmap is not a rational map and if F(y) involves any denominators
12731 * x_i = (f_i y + h_i)/m_i
12733 * then additional constraints are added to ensure that we only
12734 * map back integer points. That is we enforce
12736 * f_i y + h_i = m_i alpha_i
12738 * with alpha_i an additional existentially quantified variable.
12740 * We first copy over the divs from "ma".
12741 * Then we add the modified constraints and divs from "bmap".
12742 * Finally, we add the stride constraints, if needed.
12744 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12745 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12746 __isl_take isl_multi_aff *ma)
12748 int i, k;
12749 isl_space *space;
12750 isl_basic_map *res = NULL;
12751 int n_before, n_after, n_div_bmap, n_div_ma;
12752 isl_int f, c1, c2, g;
12753 int rational, strides;
12755 isl_int_init(f);
12756 isl_int_init(c1);
12757 isl_int_init(c2);
12758 isl_int_init(g);
12760 ma = isl_multi_aff_align_divs(ma);
12761 if (!bmap || !ma)
12762 goto error;
12763 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12764 goto error;
12766 if (type == isl_dim_in) {
12767 n_before = 0;
12768 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12769 } else {
12770 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12771 n_after = 0;
12773 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12774 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12776 space = isl_multi_aff_get_domain_space(ma);
12777 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12778 rational = isl_basic_map_is_rational(bmap);
12779 strides = rational ? 0 : multi_aff_strides(ma);
12780 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12781 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12782 if (rational)
12783 res = isl_basic_map_set_rational(res);
12785 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12786 if (isl_basic_map_alloc_div(res) < 0)
12787 goto error;
12789 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12790 goto error;
12792 for (i = 0; i < bmap->n_eq; ++i) {
12793 k = isl_basic_map_alloc_equality(res);
12794 if (k < 0)
12795 goto error;
12796 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12797 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12800 for (i = 0; i < bmap->n_ineq; ++i) {
12801 k = isl_basic_map_alloc_inequality(res);
12802 if (k < 0)
12803 goto error;
12804 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12805 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12808 for (i = 0; i < bmap->n_div; ++i) {
12809 if (isl_int_is_zero(bmap->div[i][0])) {
12810 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12811 continue;
12813 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12814 n_before, n_after, n_div_ma, n_div_bmap,
12815 f, c1, c2, g, 1);
12818 if (strides)
12819 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12821 isl_int_clear(f);
12822 isl_int_clear(c1);
12823 isl_int_clear(c2);
12824 isl_int_clear(g);
12825 isl_basic_map_free(bmap);
12826 isl_multi_aff_free(ma);
12827 res = isl_basic_set_simplify(res);
12828 return isl_basic_map_finalize(res);
12829 error:
12830 isl_int_clear(f);
12831 isl_int_clear(c1);
12832 isl_int_clear(c2);
12833 isl_int_clear(g);
12834 isl_basic_map_free(bmap);
12835 isl_multi_aff_free(ma);
12836 isl_basic_map_free(res);
12837 return NULL;
12840 /* Compute the preimage of "bset" under the function represented by "ma".
12841 * In other words, plug in "ma" in "bset". The result is a basic set
12842 * that lives in the domain space of "ma".
12844 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12845 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12847 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12850 /* Compute the preimage of the domain of "bmap" under the function
12851 * represented by "ma".
12852 * In other words, plug in "ma" in the domain of "bmap".
12853 * The result is a basic map that lives in the same space as "bmap"
12854 * except that the domain has been replaced by the domain space of "ma".
12856 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12857 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12859 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12862 /* Compute the preimage of the range of "bmap" under the function
12863 * represented by "ma".
12864 * In other words, plug in "ma" in the range of "bmap".
12865 * The result is a basic map that lives in the same space as "bmap"
12866 * except that the range has been replaced by the domain space of "ma".
12868 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12869 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12871 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12874 /* Check if the range of "ma" is compatible with the domain or range
12875 * (depending on "type") of "map".
12876 * Return -1 if anything is wrong.
12878 static int check_map_compatible_range_multi_aff(
12879 __isl_keep isl_map *map, enum isl_dim_type type,
12880 __isl_keep isl_multi_aff *ma)
12882 int m;
12883 isl_space *ma_space;
12885 ma_space = isl_multi_aff_get_space(ma);
12886 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12887 isl_space_free(ma_space);
12888 if (m >= 0 && !m)
12889 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12890 "spaces don't match", return -1);
12891 return m;
12894 /* Compute the preimage of the domain or range (depending on "type")
12895 * of "map" under the function represented by "ma".
12896 * In other words, plug in "ma" in the domain or range of "map".
12897 * The result is a map that lives in the same space as "map"
12898 * except that the domain or range has been replaced by
12899 * the domain space of "ma".
12901 * The parameters are assumed to have been aligned.
12903 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12904 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12906 int i;
12907 isl_space *space;
12909 map = isl_map_cow(map);
12910 ma = isl_multi_aff_align_divs(ma);
12911 if (!map || !ma)
12912 goto error;
12913 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12914 goto error;
12916 for (i = 0; i < map->n; ++i) {
12917 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12918 isl_multi_aff_copy(ma));
12919 if (!map->p[i])
12920 goto error;
12923 space = isl_multi_aff_get_domain_space(ma);
12924 space = isl_space_set(isl_map_get_space(map), type, space);
12926 isl_space_free(map->dim);
12927 map->dim = space;
12928 if (!map->dim)
12929 goto error;
12931 isl_multi_aff_free(ma);
12932 if (map->n > 1)
12933 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12934 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12935 return map;
12936 error:
12937 isl_multi_aff_free(ma);
12938 isl_map_free(map);
12939 return NULL;
12942 /* Compute the preimage of the domain or range (depending on "type")
12943 * of "map" under the function represented by "ma".
12944 * In other words, plug in "ma" in the domain or range of "map".
12945 * The result is a map that lives in the same space as "map"
12946 * except that the domain or range has been replaced by
12947 * the domain space of "ma".
12949 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12950 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12952 if (!map || !ma)
12953 goto error;
12955 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12956 return map_preimage_multi_aff(map, type, ma);
12958 if (!isl_space_has_named_params(map->dim) ||
12959 !isl_space_has_named_params(ma->space))
12960 isl_die(map->ctx, isl_error_invalid,
12961 "unaligned unnamed parameters", goto error);
12962 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12963 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12965 return map_preimage_multi_aff(map, type, ma);
12966 error:
12967 isl_multi_aff_free(ma);
12968 return isl_map_free(map);
12971 /* Compute the preimage of "set" under the function represented by "ma".
12972 * In other words, plug in "ma" in "set". The result is a set
12973 * that lives in the domain space of "ma".
12975 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12976 __isl_take isl_multi_aff *ma)
12978 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12981 /* Compute the preimage of the domain of "map" under the function
12982 * represented by "ma".
12983 * In other words, plug in "ma" in the domain of "map".
12984 * The result is a map that lives in the same space as "map"
12985 * except that the domain has been replaced by the domain space of "ma".
12987 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12988 __isl_take isl_multi_aff *ma)
12990 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12993 /* Compute the preimage of the range of "map" under the function
12994 * represented by "ma".
12995 * In other words, plug in "ma" in the range of "map".
12996 * The result is a map that lives in the same space as "map"
12997 * except that the range has been replaced by the domain space of "ma".
12999 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13000 __isl_take isl_multi_aff *ma)
13002 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13005 /* Compute the preimage of "map" under the function represented by "pma".
13006 * In other words, plug in "pma" in the domain or range of "map".
13007 * The result is a map that lives in the same space as "map",
13008 * except that the space of type "type" has been replaced by
13009 * the domain space of "pma".
13011 * The parameters of "map" and "pma" are assumed to have been aligned.
13013 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13014 __isl_take isl_map *map, enum isl_dim_type type,
13015 __isl_take isl_pw_multi_aff *pma)
13017 int i;
13018 isl_map *res;
13020 if (!pma)
13021 goto error;
13023 if (pma->n == 0) {
13024 isl_pw_multi_aff_free(pma);
13025 res = isl_map_empty(isl_map_get_space(map));
13026 isl_map_free(map);
13027 return res;
13030 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13031 isl_multi_aff_copy(pma->p[0].maff));
13032 if (type == isl_dim_in)
13033 res = isl_map_intersect_domain(res,
13034 isl_map_copy(pma->p[0].set));
13035 else
13036 res = isl_map_intersect_range(res,
13037 isl_map_copy(pma->p[0].set));
13039 for (i = 1; i < pma->n; ++i) {
13040 isl_map *res_i;
13042 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13043 isl_multi_aff_copy(pma->p[i].maff));
13044 if (type == isl_dim_in)
13045 res_i = isl_map_intersect_domain(res_i,
13046 isl_map_copy(pma->p[i].set));
13047 else
13048 res_i = isl_map_intersect_range(res_i,
13049 isl_map_copy(pma->p[i].set));
13050 res = isl_map_union(res, res_i);
13053 isl_pw_multi_aff_free(pma);
13054 isl_map_free(map);
13055 return res;
13056 error:
13057 isl_pw_multi_aff_free(pma);
13058 isl_map_free(map);
13059 return NULL;
13062 /* Compute the preimage of "map" under the function represented by "pma".
13063 * In other words, plug in "pma" in the domain or range of "map".
13064 * The result is a map that lives in the same space as "map",
13065 * except that the space of type "type" has been replaced by
13066 * the domain space of "pma".
13068 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13069 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13071 if (!map || !pma)
13072 goto error;
13074 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13075 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13077 if (!isl_space_has_named_params(map->dim) ||
13078 !isl_space_has_named_params(pma->dim))
13079 isl_die(map->ctx, isl_error_invalid,
13080 "unaligned unnamed parameters", goto error);
13081 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13082 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13084 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13085 error:
13086 isl_pw_multi_aff_free(pma);
13087 return isl_map_free(map);
13090 /* Compute the preimage of "set" under the function represented by "pma".
13091 * In other words, plug in "pma" in "set". The result is a set
13092 * that lives in the domain space of "pma".
13094 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13095 __isl_take isl_pw_multi_aff *pma)
13097 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13100 /* Compute the preimage of the domain of "map" under the function
13101 * represented by "pma".
13102 * In other words, plug in "pma" in the domain of "map".
13103 * The result is a map that lives in the same space as "map",
13104 * except that domain space has been replaced by the domain space of "pma".
13106 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13107 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13109 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13112 /* Compute the preimage of the range of "map" under the function
13113 * represented by "pma".
13114 * In other words, plug in "pma" in the range of "map".
13115 * The result is a map that lives in the same space as "map",
13116 * except that range space has been replaced by the domain space of "pma".
13118 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13119 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13121 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13124 /* Compute the preimage of "map" under the function represented by "mpa".
13125 * In other words, plug in "mpa" in the domain or range of "map".
13126 * The result is a map that lives in the same space as "map",
13127 * except that the space of type "type" has been replaced by
13128 * the domain space of "mpa".
13130 * If the map does not involve any constraints that refer to the
13131 * dimensions of the substituted space, then the only possible
13132 * effect of "mpa" on the map is to map the space to a different space.
13133 * We create a separate isl_multi_aff to effectuate this change
13134 * in order to avoid spurious splitting of the map along the pieces
13135 * of "mpa".
13137 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13138 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13140 int n;
13141 isl_pw_multi_aff *pma;
13143 if (!map || !mpa)
13144 goto error;
13146 n = isl_map_dim(map, type);
13147 if (!isl_map_involves_dims(map, type, 0, n)) {
13148 isl_space *space;
13149 isl_multi_aff *ma;
13151 space = isl_multi_pw_aff_get_space(mpa);
13152 isl_multi_pw_aff_free(mpa);
13153 ma = isl_multi_aff_zero(space);
13154 return isl_map_preimage_multi_aff(map, type, ma);
13157 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13158 return isl_map_preimage_pw_multi_aff(map, type, pma);
13159 error:
13160 isl_map_free(map);
13161 isl_multi_pw_aff_free(mpa);
13162 return NULL;
13165 /* Compute the preimage of "map" under the function represented by "mpa".
13166 * In other words, plug in "mpa" in the domain "map".
13167 * The result is a map that lives in the same space as "map",
13168 * except that domain space has been replaced by the domain space of "mpa".
13170 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13171 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13173 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13176 /* Compute the preimage of "set" by the function represented by "mpa".
13177 * In other words, plug in "mpa" in "set".
13179 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13180 __isl_take isl_multi_pw_aff *mpa)
13182 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13185 /* Is the point "inner" internal to inequality constraint "ineq"
13186 * of "bset"?
13187 * The point is considered to be internal to the inequality constraint,
13188 * if it strictly lies on the positive side of the inequality constraint,
13189 * or if it lies on the constraint and the constraint is lexico-positive.
13191 static isl_bool is_internal(__isl_keep isl_vec *inner,
13192 __isl_keep isl_basic_set *bset, int ineq)
13194 isl_ctx *ctx;
13195 int pos;
13196 unsigned total;
13198 if (!inner || !bset)
13199 return isl_bool_error;
13201 ctx = isl_basic_set_get_ctx(bset);
13202 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13203 &ctx->normalize_gcd);
13204 if (!isl_int_is_zero(ctx->normalize_gcd))
13205 return isl_int_is_nonneg(ctx->normalize_gcd);
13207 total = isl_basic_set_dim(bset, isl_dim_all);
13208 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13209 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13212 /* Tighten the inequality constraints of "bset" that are outward with respect
13213 * to the point "vec".
13214 * That is, tighten the constraints that are not satisfied by "vec".
13216 * "vec" is a point internal to some superset S of "bset" that is used
13217 * to make the subsets of S disjoint, by tightening one half of the constraints
13218 * that separate two subsets. In particular, the constraints of S
13219 * are all satisfied by "vec" and should not be tightened.
13220 * Of the internal constraints, those that have "vec" on the outside
13221 * are tightened. The shared facet is included in the adjacent subset
13222 * with the opposite constraint.
13223 * For constraints that saturate "vec", this criterion cannot be used
13224 * to determine which of the two sides should be tightened.
13225 * Instead, the sign of the first non-zero coefficient is used
13226 * to make this choice. Note that this second criterion is never used
13227 * on the constraints of S since "vec" is interior to "S".
13229 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13230 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13232 int j;
13234 bset = isl_basic_set_cow(bset);
13235 if (!bset)
13236 return NULL;
13237 for (j = 0; j < bset->n_ineq; ++j) {
13238 isl_bool internal;
13240 internal = is_internal(vec, bset, j);
13241 if (internal < 0)
13242 return isl_basic_set_free(bset);
13243 if (internal)
13244 continue;
13245 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13248 return bset;