isl_basic_set_plain_dim_is_fixed: return isl_bool
[isl.git] / isl_map.c
blobc73285699b0e59f923b4d3f502ff2bb8d9bd19dc
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 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
243 __isl_keep isl_basic_set *bset)
245 isl_bool m;
246 if (!bmap || !bset)
247 return isl_bool_error;
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 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
952 if (!bset)
953 return isl_bool_error;
954 return isl_space_is_params(bset->dim);
957 /* Is this set a parameter domain?
959 isl_bool isl_set_is_params(__isl_keep isl_set *set)
961 if (!set)
962 return isl_bool_error;
963 return isl_space_is_params(set->dim);
966 /* Is this map actually a parameter domain?
967 * Users should never call this function. Outside of isl,
968 * a map can never be a parameter domain.
970 int isl_map_is_params(__isl_keep isl_map *map)
972 if (!map)
973 return -1;
974 return isl_space_is_params(map->dim);
977 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
978 struct isl_basic_map *bmap, unsigned extra,
979 unsigned n_eq, unsigned n_ineq)
981 int i;
982 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
984 bmap->ctx = ctx;
985 isl_ctx_ref(ctx);
987 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
988 if (isl_blk_is_error(bmap->block))
989 goto error;
991 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
992 if ((n_ineq + n_eq) && !bmap->ineq)
993 goto error;
995 if (extra == 0) {
996 bmap->block2 = isl_blk_empty();
997 bmap->div = NULL;
998 } else {
999 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1000 if (isl_blk_is_error(bmap->block2))
1001 goto error;
1003 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1004 if (!bmap->div)
1005 goto error;
1008 for (i = 0; i < n_ineq + n_eq; ++i)
1009 bmap->ineq[i] = bmap->block.data + i * row_size;
1011 for (i = 0; i < extra; ++i)
1012 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1014 bmap->ref = 1;
1015 bmap->flags = 0;
1016 bmap->c_size = n_eq + n_ineq;
1017 bmap->eq = bmap->ineq + n_ineq;
1018 bmap->extra = extra;
1019 bmap->n_eq = 0;
1020 bmap->n_ineq = 0;
1021 bmap->n_div = 0;
1022 bmap->sample = NULL;
1024 return bmap;
1025 error:
1026 isl_basic_map_free(bmap);
1027 return NULL;
1030 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1031 unsigned nparam, unsigned dim, unsigned extra,
1032 unsigned n_eq, unsigned n_ineq)
1034 struct isl_basic_map *bmap;
1035 isl_space *space;
1037 space = isl_space_set_alloc(ctx, nparam, dim);
1038 if (!space)
1039 return NULL;
1041 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1042 return bset_from_bmap(bmap);
1045 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1046 unsigned extra, unsigned n_eq, unsigned n_ineq)
1048 struct isl_basic_map *bmap;
1049 if (!dim)
1050 return NULL;
1051 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1052 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1053 return bset_from_bmap(bmap);
1054 error:
1055 isl_space_free(dim);
1056 return NULL;
1059 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1060 unsigned extra, unsigned n_eq, unsigned n_ineq)
1062 struct isl_basic_map *bmap;
1064 if (!dim)
1065 return NULL;
1066 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1067 if (!bmap)
1068 goto error;
1069 bmap->dim = dim;
1071 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1072 error:
1073 isl_space_free(dim);
1074 return NULL;
1077 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1078 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1079 unsigned n_eq, unsigned n_ineq)
1081 struct isl_basic_map *bmap;
1082 isl_space *dim;
1084 dim = isl_space_alloc(ctx, nparam, in, out);
1085 if (!dim)
1086 return NULL;
1088 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1089 return bmap;
1092 static void dup_constraints(
1093 struct isl_basic_map *dst, struct isl_basic_map *src)
1095 int i;
1096 unsigned total = isl_basic_map_total_dim(src);
1098 for (i = 0; i < src->n_eq; ++i) {
1099 int j = isl_basic_map_alloc_equality(dst);
1100 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1103 for (i = 0; i < src->n_ineq; ++i) {
1104 int j = isl_basic_map_alloc_inequality(dst);
1105 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1108 for (i = 0; i < src->n_div; ++i) {
1109 int j = isl_basic_map_alloc_div(dst);
1110 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1112 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1115 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1117 struct isl_basic_map *dup;
1119 if (!bmap)
1120 return NULL;
1121 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1122 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1123 if (!dup)
1124 return NULL;
1125 dup_constraints(dup, bmap);
1126 dup->flags = bmap->flags;
1127 dup->sample = isl_vec_copy(bmap->sample);
1128 return dup;
1131 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1133 struct isl_basic_map *dup;
1135 dup = isl_basic_map_dup(bset_to_bmap(bset));
1136 return bset_from_bmap(dup);
1139 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1141 if (!bset)
1142 return NULL;
1144 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1145 bset->ref++;
1146 return bset;
1148 return isl_basic_set_dup(bset);
1151 struct isl_set *isl_set_copy(struct isl_set *set)
1153 if (!set)
1154 return NULL;
1156 set->ref++;
1157 return set;
1160 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1162 if (!bmap)
1163 return NULL;
1165 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1166 bmap->ref++;
1167 return bmap;
1169 bmap = isl_basic_map_dup(bmap);
1170 if (bmap)
1171 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1172 return bmap;
1175 struct isl_map *isl_map_copy(struct isl_map *map)
1177 if (!map)
1178 return NULL;
1180 map->ref++;
1181 return map;
1184 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1186 if (!bmap)
1187 return NULL;
1189 if (--bmap->ref > 0)
1190 return NULL;
1192 isl_ctx_deref(bmap->ctx);
1193 free(bmap->div);
1194 isl_blk_free(bmap->ctx, bmap->block2);
1195 free(bmap->ineq);
1196 isl_blk_free(bmap->ctx, bmap->block);
1197 isl_vec_free(bmap->sample);
1198 isl_space_free(bmap->dim);
1199 free(bmap);
1201 return NULL;
1204 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1206 return isl_basic_map_free(bset_to_bmap(bset));
1209 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1211 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1214 __isl_give isl_map *isl_map_align_params_map_map_and(
1215 __isl_take isl_map *map1, __isl_take isl_map *map2,
1216 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1217 __isl_take isl_map *map2))
1219 if (!map1 || !map2)
1220 goto error;
1221 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1222 return fn(map1, map2);
1223 if (!isl_space_has_named_params(map1->dim) ||
1224 !isl_space_has_named_params(map2->dim))
1225 isl_die(map1->ctx, isl_error_invalid,
1226 "unaligned unnamed parameters", goto error);
1227 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1228 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1229 return fn(map1, map2);
1230 error:
1231 isl_map_free(map1);
1232 isl_map_free(map2);
1233 return NULL;
1236 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1237 __isl_keep isl_map *map2,
1238 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1240 isl_bool r;
1242 if (!map1 || !map2)
1243 return isl_bool_error;
1244 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1245 return fn(map1, map2);
1246 if (!isl_space_has_named_params(map1->dim) ||
1247 !isl_space_has_named_params(map2->dim))
1248 isl_die(map1->ctx, isl_error_invalid,
1249 "unaligned unnamed parameters", return isl_bool_error);
1250 map1 = isl_map_copy(map1);
1251 map2 = isl_map_copy(map2);
1252 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1253 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1254 r = fn(map1, map2);
1255 isl_map_free(map1);
1256 isl_map_free(map2);
1257 return r;
1260 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1262 struct isl_ctx *ctx;
1263 if (!bmap)
1264 return -1;
1265 ctx = bmap->ctx;
1266 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1267 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1268 return -1);
1269 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1274 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1275 isl_int *t;
1276 int j = isl_basic_map_alloc_inequality(bmap);
1277 if (j < 0)
1278 return -1;
1279 t = bmap->ineq[j];
1280 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1281 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1282 bmap->eq[-1] = t;
1283 bmap->n_eq++;
1284 bmap->n_ineq--;
1285 bmap->eq--;
1286 return 0;
1288 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1289 bmap->extra - bmap->n_div);
1290 return bmap->n_eq++;
1293 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1295 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1298 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1300 if (!bmap)
1301 return -1;
1302 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1303 bmap->n_eq -= n;
1304 return 0;
1307 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1309 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1312 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1314 isl_int *t;
1315 if (!bmap)
1316 return -1;
1317 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1319 if (pos != bmap->n_eq - 1) {
1320 t = bmap->eq[pos];
1321 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1322 bmap->eq[bmap->n_eq - 1] = t;
1324 bmap->n_eq--;
1325 return 0;
1328 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1330 return isl_basic_map_drop_equality(bset_to_bmap(bset), pos);
1333 /* Turn inequality "pos" of "bmap" into an equality.
1335 * In particular, we move the inequality in front of the equalities
1336 * and move the last inequality in the position of the moved inequality.
1337 * Note that isl_tab_make_equalities_explicit depends on this particular
1338 * change in the ordering of the constraints.
1340 void isl_basic_map_inequality_to_equality(
1341 struct isl_basic_map *bmap, unsigned pos)
1343 isl_int *t;
1345 t = bmap->ineq[pos];
1346 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1347 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1348 bmap->eq[-1] = t;
1349 bmap->n_eq++;
1350 bmap->n_ineq--;
1351 bmap->eq--;
1352 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1353 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1354 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1355 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1358 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1360 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1363 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1365 struct isl_ctx *ctx;
1366 if (!bmap)
1367 return -1;
1368 ctx = bmap->ctx;
1369 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1370 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1371 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1372 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1373 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1374 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1375 1 + isl_basic_map_total_dim(bmap),
1376 bmap->extra - bmap->n_div);
1377 return bmap->n_ineq++;
1380 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1382 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1385 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1387 if (!bmap)
1388 return -1;
1389 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1390 bmap->n_ineq -= n;
1391 return 0;
1394 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1396 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1399 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1401 isl_int *t;
1402 if (!bmap)
1403 return -1;
1404 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1406 if (pos != bmap->n_ineq - 1) {
1407 t = bmap->ineq[pos];
1408 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1409 bmap->ineq[bmap->n_ineq - 1] = t;
1410 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1412 bmap->n_ineq--;
1413 return 0;
1416 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1418 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1421 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1422 isl_int *eq)
1424 int k;
1426 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1427 if (!bmap)
1428 return NULL;
1429 k = isl_basic_map_alloc_equality(bmap);
1430 if (k < 0)
1431 goto error;
1432 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1433 return bmap;
1434 error:
1435 isl_basic_map_free(bmap);
1436 return NULL;
1439 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1440 isl_int *eq)
1442 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1445 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1446 isl_int *ineq)
1448 int k;
1450 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1451 if (!bmap)
1452 return NULL;
1453 k = isl_basic_map_alloc_inequality(bmap);
1454 if (k < 0)
1455 goto error;
1456 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1457 return bmap;
1458 error:
1459 isl_basic_map_free(bmap);
1460 return NULL;
1463 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1464 isl_int *ineq)
1466 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1469 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1471 if (!bmap)
1472 return -1;
1473 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1474 isl_seq_clr(bmap->div[bmap->n_div] +
1475 1 + 1 + isl_basic_map_total_dim(bmap),
1476 bmap->extra - bmap->n_div);
1477 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1478 return bmap->n_div++;
1481 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1483 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1486 /* Check that there are "n" dimensions of type "type" starting at "first"
1487 * in "bmap".
1489 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1490 enum isl_dim_type type, unsigned first, unsigned n)
1492 unsigned dim;
1494 if (!bmap)
1495 return isl_stat_error;
1496 dim = isl_basic_map_dim(bmap, type);
1497 if (first + n > dim || first + n < first)
1498 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1499 "position or range out of bounds",
1500 return isl_stat_error);
1501 return isl_stat_ok;
1504 /* Insert an extra integer division, prescribed by "div", to "bmap"
1505 * at (integer division) position "pos".
1507 * The integer division is first added at the end and then moved
1508 * into the right position.
1510 __isl_give isl_basic_map *isl_basic_map_insert_div(
1511 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1513 int i, k;
1515 bmap = isl_basic_map_cow(bmap);
1516 if (!bmap || !div)
1517 return isl_basic_map_free(bmap);
1519 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1520 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1521 "unexpected size", return isl_basic_map_free(bmap));
1522 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1523 return isl_basic_map_free(bmap);
1525 bmap = isl_basic_map_extend_space(bmap,
1526 isl_basic_map_get_space(bmap), 1, 0, 2);
1527 k = isl_basic_map_alloc_div(bmap);
1528 if (k < 0)
1529 return isl_basic_map_free(bmap);
1530 isl_seq_cpy(bmap->div[k], div->el, div->size);
1531 isl_int_set_si(bmap->div[k][div->size], 0);
1533 for (i = k; i > pos; --i)
1534 isl_basic_map_swap_div(bmap, i, i - 1);
1536 return bmap;
1539 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1541 if (!bmap)
1542 return -1;
1543 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1544 bmap->n_div -= n;
1545 return 0;
1548 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1550 return isl_basic_map_free_div(bset_to_bmap(bset), n);
1553 /* Copy constraint from src to dst, putting the vars of src at offset
1554 * dim_off in dst and the divs of src at offset div_off in dst.
1555 * If both sets are actually map, then dim_off applies to the input
1556 * variables.
1558 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1559 struct isl_basic_map *src_map, isl_int *src,
1560 unsigned in_off, unsigned out_off, unsigned div_off)
1562 unsigned src_nparam = isl_basic_map_n_param(src_map);
1563 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1564 unsigned src_in = isl_basic_map_n_in(src_map);
1565 unsigned dst_in = isl_basic_map_n_in(dst_map);
1566 unsigned src_out = isl_basic_map_n_out(src_map);
1567 unsigned dst_out = isl_basic_map_n_out(dst_map);
1568 isl_int_set(dst[0], src[0]);
1569 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1570 if (dst_nparam > src_nparam)
1571 isl_seq_clr(dst+1+src_nparam,
1572 dst_nparam - src_nparam);
1573 isl_seq_clr(dst+1+dst_nparam, in_off);
1574 isl_seq_cpy(dst+1+dst_nparam+in_off,
1575 src+1+src_nparam,
1576 isl_min(dst_in-in_off, src_in));
1577 if (dst_in-in_off > src_in)
1578 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1579 dst_in - in_off - src_in);
1580 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1581 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1582 src+1+src_nparam+src_in,
1583 isl_min(dst_out-out_off, src_out));
1584 if (dst_out-out_off > src_out)
1585 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1586 dst_out - out_off - src_out);
1587 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1588 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1589 src+1+src_nparam+src_in+src_out,
1590 isl_min(dst_map->extra-div_off, src_map->n_div));
1591 if (dst_map->n_div-div_off > src_map->n_div)
1592 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1593 div_off+src_map->n_div,
1594 dst_map->n_div - div_off - src_map->n_div);
1597 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1598 struct isl_basic_map *src_map, isl_int *src,
1599 unsigned in_off, unsigned out_off, unsigned div_off)
1601 isl_int_set(dst[0], src[0]);
1602 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1605 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1606 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1608 int i;
1609 unsigned div_off;
1611 if (!bmap1 || !bmap2)
1612 goto error;
1614 div_off = bmap1->n_div;
1616 for (i = 0; i < bmap2->n_eq; ++i) {
1617 int i1 = isl_basic_map_alloc_equality(bmap1);
1618 if (i1 < 0)
1619 goto error;
1620 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1621 i_pos, o_pos, div_off);
1624 for (i = 0; i < bmap2->n_ineq; ++i) {
1625 int i1 = isl_basic_map_alloc_inequality(bmap1);
1626 if (i1 < 0)
1627 goto error;
1628 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1629 i_pos, o_pos, div_off);
1632 for (i = 0; i < bmap2->n_div; ++i) {
1633 int i1 = isl_basic_map_alloc_div(bmap1);
1634 if (i1 < 0)
1635 goto error;
1636 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1637 i_pos, o_pos, div_off);
1640 isl_basic_map_free(bmap2);
1642 return bmap1;
1644 error:
1645 isl_basic_map_free(bmap1);
1646 isl_basic_map_free(bmap2);
1647 return NULL;
1650 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1651 struct isl_basic_set *bset2, unsigned pos)
1653 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1654 bset_to_bmap(bset2), 0, pos));
1657 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1658 __isl_take isl_space *dim, unsigned extra,
1659 unsigned n_eq, unsigned n_ineq)
1661 struct isl_basic_map *ext;
1662 unsigned flags;
1663 int dims_ok;
1665 if (!dim)
1666 goto error;
1668 if (!base)
1669 goto error;
1671 dims_ok = isl_space_is_equal(base->dim, dim) &&
1672 base->extra >= base->n_div + extra;
1674 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1675 room_for_ineq(base, n_ineq)) {
1676 isl_space_free(dim);
1677 return base;
1680 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1681 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1682 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1683 extra += base->extra;
1684 n_eq += base->n_eq;
1685 n_ineq += base->n_ineq;
1687 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1688 dim = NULL;
1689 if (!ext)
1690 goto error;
1692 if (dims_ok)
1693 ext->sample = isl_vec_copy(base->sample);
1694 flags = base->flags;
1695 ext = add_constraints(ext, base, 0, 0);
1696 if (ext) {
1697 ext->flags = flags;
1698 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1701 return ext;
1703 error:
1704 isl_space_free(dim);
1705 isl_basic_map_free(base);
1706 return NULL;
1709 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1710 __isl_take isl_space *dim, unsigned extra,
1711 unsigned n_eq, unsigned n_ineq)
1713 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1714 dim, extra, n_eq, n_ineq));
1717 struct isl_basic_map *isl_basic_map_extend_constraints(
1718 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1720 if (!base)
1721 return NULL;
1722 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1723 0, n_eq, n_ineq);
1726 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1727 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1728 unsigned n_eq, unsigned n_ineq)
1730 struct isl_basic_map *bmap;
1731 isl_space *dim;
1733 if (!base)
1734 return NULL;
1735 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1736 if (!dim)
1737 goto error;
1739 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1740 return bmap;
1741 error:
1742 isl_basic_map_free(base);
1743 return NULL;
1746 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1747 unsigned nparam, unsigned dim, unsigned extra,
1748 unsigned n_eq, unsigned n_ineq)
1750 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1751 nparam, 0, dim, extra, n_eq, n_ineq));
1754 struct isl_basic_set *isl_basic_set_extend_constraints(
1755 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1757 isl_basic_map *bmap = bset_to_bmap(base);
1758 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1759 return bset_from_bmap(bmap);
1762 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1764 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1767 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1769 if (!bmap)
1770 return NULL;
1772 if (bmap->ref > 1) {
1773 bmap->ref--;
1774 bmap = isl_basic_map_dup(bmap);
1776 if (bmap) {
1777 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1778 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1780 return bmap;
1783 /* Clear all cached information in "map", either because it is about
1784 * to be modified or because it is being freed.
1785 * Always return the same pointer that is passed in.
1786 * This is needed for the use in isl_map_free.
1788 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1790 isl_basic_map_free(map->cached_simple_hull[0]);
1791 isl_basic_map_free(map->cached_simple_hull[1]);
1792 map->cached_simple_hull[0] = NULL;
1793 map->cached_simple_hull[1] = NULL;
1794 return map;
1797 struct isl_set *isl_set_cow(struct isl_set *set)
1799 return isl_map_cow(set);
1802 /* Return an isl_map that is equal to "map" and that has only
1803 * a single reference.
1805 * If the original input already has only one reference, then
1806 * simply return it, but clear all cached information, since
1807 * it may be rendered invalid by the operations that will be
1808 * performed on the result.
1810 * Otherwise, create a duplicate (without any cached information).
1812 struct isl_map *isl_map_cow(struct isl_map *map)
1814 if (!map)
1815 return NULL;
1817 if (map->ref == 1)
1818 return clear_caches(map);
1819 map->ref--;
1820 return isl_map_dup(map);
1823 static void swap_vars(struct isl_blk blk, isl_int *a,
1824 unsigned a_len, unsigned b_len)
1826 isl_seq_cpy(blk.data, a+a_len, b_len);
1827 isl_seq_cpy(blk.data+b_len, a, a_len);
1828 isl_seq_cpy(a, blk.data, b_len+a_len);
1831 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1832 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1834 int i;
1835 struct isl_blk blk;
1837 if (!bmap)
1838 goto error;
1840 isl_assert(bmap->ctx,
1841 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1843 if (n1 == 0 || n2 == 0)
1844 return bmap;
1846 bmap = isl_basic_map_cow(bmap);
1847 if (!bmap)
1848 return NULL;
1850 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1851 if (isl_blk_is_error(blk))
1852 goto error;
1854 for (i = 0; i < bmap->n_eq; ++i)
1855 swap_vars(blk,
1856 bmap->eq[i] + pos, n1, n2);
1858 for (i = 0; i < bmap->n_ineq; ++i)
1859 swap_vars(blk,
1860 bmap->ineq[i] + pos, n1, n2);
1862 for (i = 0; i < bmap->n_div; ++i)
1863 swap_vars(blk,
1864 bmap->div[i]+1 + pos, n1, n2);
1866 isl_blk_free(bmap->ctx, blk);
1868 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1869 bmap = isl_basic_map_gauss(bmap, NULL);
1870 return isl_basic_map_finalize(bmap);
1871 error:
1872 isl_basic_map_free(bmap);
1873 return NULL;
1876 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1878 int i = 0;
1879 unsigned total;
1880 if (!bmap)
1881 goto error;
1882 total = isl_basic_map_total_dim(bmap);
1883 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1884 return isl_basic_map_free(bmap);
1885 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1886 if (bmap->n_eq > 0)
1887 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1888 else {
1889 i = isl_basic_map_alloc_equality(bmap);
1890 if (i < 0)
1891 goto error;
1893 isl_int_set_si(bmap->eq[i][0], 1);
1894 isl_seq_clr(bmap->eq[i]+1, total);
1895 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1896 isl_vec_free(bmap->sample);
1897 bmap->sample = NULL;
1898 return isl_basic_map_finalize(bmap);
1899 error:
1900 isl_basic_map_free(bmap);
1901 return NULL;
1904 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1906 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1909 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1910 * of "bmap").
1912 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1914 isl_int *t = bmap->div[a];
1915 bmap->div[a] = bmap->div[b];
1916 bmap->div[b] = t;
1919 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1920 * div definitions accordingly.
1922 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1924 int i;
1925 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1927 swap_div(bmap, a, b);
1929 for (i = 0; i < bmap->n_eq; ++i)
1930 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1932 for (i = 0; i < bmap->n_ineq; ++i)
1933 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1935 for (i = 0; i < bmap->n_div; ++i)
1936 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1937 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1940 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1941 * div definitions accordingly.
1943 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1945 isl_basic_map_swap_div(bset, a, b);
1948 /* Eliminate the specified n dimensions starting at first from the
1949 * constraints, without removing the dimensions from the space.
1950 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1952 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1953 enum isl_dim_type type, unsigned first, unsigned n)
1955 int i;
1957 if (!map)
1958 return NULL;
1959 if (n == 0)
1960 return map;
1962 if (first + n > isl_map_dim(map, type) || first + n < first)
1963 isl_die(map->ctx, isl_error_invalid,
1964 "index out of bounds", goto error);
1966 map = isl_map_cow(map);
1967 if (!map)
1968 return NULL;
1970 for (i = 0; i < map->n; ++i) {
1971 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1972 if (!map->p[i])
1973 goto error;
1975 return map;
1976 error:
1977 isl_map_free(map);
1978 return NULL;
1981 /* Eliminate the specified n dimensions starting at first from the
1982 * constraints, without removing the dimensions from the space.
1983 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1985 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1986 enum isl_dim_type type, unsigned first, unsigned n)
1988 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
1991 /* Eliminate the specified n dimensions starting at first from the
1992 * constraints, without removing the dimensions from the space.
1993 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1995 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1996 unsigned first, unsigned n)
1998 return isl_set_eliminate(set, isl_dim_set, first, n);
2001 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2002 __isl_take isl_basic_map *bmap)
2004 if (!bmap)
2005 return NULL;
2006 bmap = isl_basic_map_eliminate_vars(bmap,
2007 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2008 if (!bmap)
2009 return NULL;
2010 bmap->n_div = 0;
2011 return isl_basic_map_finalize(bmap);
2014 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2015 __isl_take isl_basic_set *bset)
2017 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2020 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2022 int i;
2024 if (!map)
2025 return NULL;
2026 if (map->n == 0)
2027 return map;
2029 map = isl_map_cow(map);
2030 if (!map)
2031 return NULL;
2033 for (i = 0; i < map->n; ++i) {
2034 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2035 if (!map->p[i])
2036 goto error;
2038 return map;
2039 error:
2040 isl_map_free(map);
2041 return NULL;
2044 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2046 return isl_map_remove_divs(set);
2049 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2050 enum isl_dim_type type, unsigned first, unsigned n)
2052 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2053 return isl_basic_map_free(bmap);
2054 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2055 return bmap;
2056 bmap = isl_basic_map_eliminate_vars(bmap,
2057 isl_basic_map_offset(bmap, type) - 1 + first, n);
2058 if (!bmap)
2059 return bmap;
2060 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2061 return bmap;
2062 bmap = isl_basic_map_drop(bmap, type, first, n);
2063 return bmap;
2066 /* Return true if the definition of the given div (recursively) involves
2067 * any of the given variables.
2069 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2070 unsigned first, unsigned n)
2072 int i;
2073 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2075 if (isl_int_is_zero(bmap->div[div][0]))
2076 return isl_bool_false;
2077 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2078 return isl_bool_true;
2080 for (i = bmap->n_div - 1; i >= 0; --i) {
2081 isl_bool involves;
2083 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2084 continue;
2085 involves = div_involves_vars(bmap, i, first, n);
2086 if (involves < 0 || involves)
2087 return involves;
2090 return isl_bool_false;
2093 /* Try and add a lower and/or upper bound on "div" to "bmap"
2094 * based on inequality "i".
2095 * "total" is the total number of variables (excluding the divs).
2096 * "v" is a temporary object that can be used during the calculations.
2097 * If "lb" is set, then a lower bound should be constructed.
2098 * If "ub" is set, then an upper bound should be constructed.
2100 * The calling function has already checked that the inequality does not
2101 * reference "div", but we still need to check that the inequality is
2102 * of the right form. We'll consider the case where we want to construct
2103 * a lower bound. The construction of upper bounds is similar.
2105 * Let "div" be of the form
2107 * q = floor((a + f(x))/d)
2109 * We essentially check if constraint "i" is of the form
2111 * b + f(x) >= 0
2113 * so that we can use it to derive a lower bound on "div".
2114 * However, we allow a slightly more general form
2116 * b + g(x) >= 0
2118 * with the condition that the coefficients of g(x) - f(x) are all
2119 * divisible by d.
2120 * Rewriting this constraint as
2122 * 0 >= -b - g(x)
2124 * adding a + f(x) to both sides and dividing by d, we obtain
2126 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2128 * Taking the floor on both sides, we obtain
2130 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2132 * or
2134 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2136 * In the case of an upper bound, we construct the constraint
2138 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2141 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2142 __isl_take isl_basic_map *bmap, int div, int i,
2143 unsigned total, isl_int v, int lb, int ub)
2145 int j;
2147 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2148 if (lb) {
2149 isl_int_sub(v, bmap->ineq[i][1 + j],
2150 bmap->div[div][1 + 1 + j]);
2151 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2153 if (ub) {
2154 isl_int_add(v, bmap->ineq[i][1 + j],
2155 bmap->div[div][1 + 1 + j]);
2156 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2159 if (!lb && !ub)
2160 return bmap;
2162 bmap = isl_basic_map_cow(bmap);
2163 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2164 if (lb) {
2165 int k = isl_basic_map_alloc_inequality(bmap);
2166 if (k < 0)
2167 goto error;
2168 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2169 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2170 bmap->div[div][1 + j]);
2171 isl_int_cdiv_q(bmap->ineq[k][j],
2172 bmap->ineq[k][j], bmap->div[div][0]);
2174 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2176 if (ub) {
2177 int k = isl_basic_map_alloc_inequality(bmap);
2178 if (k < 0)
2179 goto error;
2180 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2181 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2182 bmap->div[div][1 + j]);
2183 isl_int_fdiv_q(bmap->ineq[k][j],
2184 bmap->ineq[k][j], bmap->div[div][0]);
2186 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2189 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2190 return bmap;
2191 error:
2192 isl_basic_map_free(bmap);
2193 return NULL;
2196 /* This function is called right before "div" is eliminated from "bmap"
2197 * using Fourier-Motzkin.
2198 * Look through the constraints of "bmap" for constraints on the argument
2199 * of the integer division and use them to construct constraints on the
2200 * integer division itself. These constraints can then be combined
2201 * during the Fourier-Motzkin elimination.
2202 * Note that it is only useful to introduce lower bounds on "div"
2203 * if "bmap" already contains upper bounds on "div" as the newly
2204 * introduce lower bounds can then be combined with the pre-existing
2205 * upper bounds. Similarly for upper bounds.
2206 * We therefore first check if "bmap" contains any lower and/or upper bounds
2207 * on "div".
2209 * It is interesting to note that the introduction of these constraints
2210 * can indeed lead to more accurate results, even when compared to
2211 * deriving constraints on the argument of "div" from constraints on "div".
2212 * Consider, for example, the set
2214 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2216 * The second constraint can be rewritten as
2218 * 2 * [(-i-2j+3)/4] + k >= 0
2220 * from which we can derive
2222 * -i - 2j + 3 >= -2k
2224 * or
2226 * i + 2j <= 3 + 2k
2228 * Combined with the first constraint, we obtain
2230 * -3 <= 3 + 2k or k >= -3
2232 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2233 * the first constraint, we obtain
2235 * [(i + 2j)/4] >= [-3/4] = -1
2237 * Combining this constraint with the second constraint, we obtain
2239 * k >= -2
2241 static __isl_give isl_basic_map *insert_bounds_on_div(
2242 __isl_take isl_basic_map *bmap, int div)
2244 int i;
2245 int check_lb, check_ub;
2246 isl_int v;
2247 unsigned total;
2249 if (!bmap)
2250 return NULL;
2252 if (isl_int_is_zero(bmap->div[div][0]))
2253 return bmap;
2255 total = isl_space_dim(bmap->dim, isl_dim_all);
2257 check_lb = 0;
2258 check_ub = 0;
2259 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2260 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2261 if (s > 0)
2262 check_ub = 1;
2263 if (s < 0)
2264 check_lb = 1;
2267 if (!check_lb && !check_ub)
2268 return bmap;
2270 isl_int_init(v);
2272 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2273 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2274 continue;
2276 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2277 check_lb, check_ub);
2280 isl_int_clear(v);
2282 return bmap;
2285 /* Remove all divs (recursively) involving any of the given dimensions
2286 * in their definitions.
2288 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2289 __isl_take isl_basic_map *bmap,
2290 enum isl_dim_type type, unsigned first, unsigned n)
2292 int i;
2294 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2295 return isl_basic_map_free(bmap);
2296 first += isl_basic_map_offset(bmap, type);
2298 for (i = bmap->n_div - 1; i >= 0; --i) {
2299 isl_bool involves;
2301 involves = div_involves_vars(bmap, i, first, n);
2302 if (involves < 0)
2303 return isl_basic_map_free(bmap);
2304 if (!involves)
2305 continue;
2306 bmap = insert_bounds_on_div(bmap, i);
2307 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2308 if (!bmap)
2309 return NULL;
2310 i = bmap->n_div;
2313 return bmap;
2316 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2317 __isl_take isl_basic_set *bset,
2318 enum isl_dim_type type, unsigned first, unsigned n)
2320 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2323 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2324 enum isl_dim_type type, unsigned first, unsigned n)
2326 int i;
2328 if (!map)
2329 return NULL;
2330 if (map->n == 0)
2331 return map;
2333 map = isl_map_cow(map);
2334 if (!map)
2335 return NULL;
2337 for (i = 0; i < map->n; ++i) {
2338 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2339 type, first, n);
2340 if (!map->p[i])
2341 goto error;
2343 return map;
2344 error:
2345 isl_map_free(map);
2346 return NULL;
2349 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2350 enum isl_dim_type type, unsigned first, unsigned n)
2352 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2353 type, first, n));
2356 /* Does the description of "bmap" depend on the specified dimensions?
2357 * We also check whether the dimensions appear in any of the div definitions.
2358 * In principle there is no need for this check. If the dimensions appear
2359 * in a div definition, they also appear in the defining constraints of that
2360 * div.
2362 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2363 enum isl_dim_type type, unsigned first, unsigned n)
2365 int i;
2367 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2368 return isl_bool_error;
2370 first += isl_basic_map_offset(bmap, type);
2371 for (i = 0; i < bmap->n_eq; ++i)
2372 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2373 return isl_bool_true;
2374 for (i = 0; i < bmap->n_ineq; ++i)
2375 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2376 return isl_bool_true;
2377 for (i = 0; i < bmap->n_div; ++i) {
2378 if (isl_int_is_zero(bmap->div[i][0]))
2379 continue;
2380 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2381 return isl_bool_true;
2384 return isl_bool_false;
2387 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2388 enum isl_dim_type type, unsigned first, unsigned n)
2390 int i;
2392 if (!map)
2393 return isl_bool_error;
2395 if (first + n > isl_map_dim(map, type))
2396 isl_die(map->ctx, isl_error_invalid,
2397 "index out of bounds", return isl_bool_error);
2399 for (i = 0; i < map->n; ++i) {
2400 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2401 type, first, n);
2402 if (involves < 0 || involves)
2403 return involves;
2406 return isl_bool_false;
2409 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2410 enum isl_dim_type type, unsigned first, unsigned n)
2412 return isl_basic_map_involves_dims(bset, type, first, n);
2415 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2416 enum isl_dim_type type, unsigned first, unsigned n)
2418 return isl_map_involves_dims(set, type, first, n);
2421 /* Drop all constraints in bmap that involve any of the dimensions
2422 * first to first+n-1.
2424 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2425 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2427 int i;
2429 if (n == 0)
2430 return bmap;
2432 bmap = isl_basic_map_cow(bmap);
2434 if (!bmap)
2435 return NULL;
2437 for (i = bmap->n_eq - 1; i >= 0; --i) {
2438 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2439 continue;
2440 isl_basic_map_drop_equality(bmap, i);
2443 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2444 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2445 continue;
2446 isl_basic_map_drop_inequality(bmap, i);
2449 bmap = isl_basic_map_add_known_div_constraints(bmap);
2450 return bmap;
2453 /* Drop all constraints in bset that involve any of the dimensions
2454 * first to first+n-1.
2456 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2457 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2459 return isl_basic_map_drop_constraints_involving(bset, first, n);
2462 /* Drop all constraints in bmap that do not involve any of the dimensions
2463 * first to first + n - 1 of the given type.
2465 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2466 __isl_take isl_basic_map *bmap,
2467 enum isl_dim_type type, unsigned first, unsigned n)
2469 int i;
2471 if (n == 0) {
2472 isl_space *space = isl_basic_map_get_space(bmap);
2473 isl_basic_map_free(bmap);
2474 return isl_basic_map_universe(space);
2476 bmap = isl_basic_map_cow(bmap);
2477 if (!bmap)
2478 return NULL;
2480 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2481 return isl_basic_map_free(bmap);
2483 first += isl_basic_map_offset(bmap, type) - 1;
2485 for (i = bmap->n_eq - 1; i >= 0; --i) {
2486 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2487 continue;
2488 isl_basic_map_drop_equality(bmap, i);
2491 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2492 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2493 continue;
2494 isl_basic_map_drop_inequality(bmap, i);
2497 bmap = isl_basic_map_add_known_div_constraints(bmap);
2498 return bmap;
2501 /* Drop all constraints in bset that do not involve any of the dimensions
2502 * first to first + n - 1 of the given type.
2504 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2505 __isl_take isl_basic_set *bset,
2506 enum isl_dim_type type, unsigned first, unsigned n)
2508 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2509 type, first, n);
2512 /* Drop all constraints in bmap that involve any of the dimensions
2513 * first to first + n - 1 of the given type.
2515 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2516 __isl_take isl_basic_map *bmap,
2517 enum isl_dim_type type, unsigned first, unsigned n)
2519 if (!bmap)
2520 return NULL;
2521 if (n == 0)
2522 return bmap;
2524 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2525 return isl_basic_map_free(bmap);
2527 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2528 first += isl_basic_map_offset(bmap, type) - 1;
2529 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2532 /* Drop all constraints in bset that involve any of the dimensions
2533 * first to first + n - 1 of the given type.
2535 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2536 __isl_take isl_basic_set *bset,
2537 enum isl_dim_type type, unsigned first, unsigned n)
2539 return isl_basic_map_drop_constraints_involving_dims(bset,
2540 type, first, n);
2543 /* Drop constraints from "map" by applying "drop" to each basic map.
2545 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2546 enum isl_dim_type type, unsigned first, unsigned n,
2547 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2548 enum isl_dim_type type, unsigned first, unsigned n))
2550 int i;
2551 unsigned dim;
2553 if (!map)
2554 return NULL;
2556 dim = isl_map_dim(map, type);
2557 if (first + n > dim || first + n < first)
2558 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2559 "index out of bounds", return isl_map_free(map));
2561 map = isl_map_cow(map);
2562 if (!map)
2563 return NULL;
2565 for (i = 0; i < map->n; ++i) {
2566 map->p[i] = drop(map->p[i], type, first, n);
2567 if (!map->p[i])
2568 return isl_map_free(map);
2571 if (map->n > 1)
2572 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2574 return map;
2577 /* Drop all constraints in map that involve any of the dimensions
2578 * first to first + n - 1 of the given type.
2580 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2581 __isl_take isl_map *map,
2582 enum isl_dim_type type, unsigned first, unsigned n)
2584 if (n == 0)
2585 return map;
2586 return drop_constraints(map, type, first, n,
2587 &isl_basic_map_drop_constraints_involving_dims);
2590 /* Drop all constraints in "map" that do not involve any of the dimensions
2591 * first to first + n - 1 of the given type.
2593 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2594 __isl_take isl_map *map,
2595 enum isl_dim_type type, unsigned first, unsigned n)
2597 if (n == 0) {
2598 isl_space *space = isl_map_get_space(map);
2599 isl_map_free(map);
2600 return isl_map_universe(space);
2602 return drop_constraints(map, type, first, n,
2603 &isl_basic_map_drop_constraints_not_involving_dims);
2606 /* Drop all constraints in set that involve any of the dimensions
2607 * first to first + n - 1 of the given type.
2609 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2610 __isl_take isl_set *set,
2611 enum isl_dim_type type, unsigned first, unsigned n)
2613 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2616 /* Drop all constraints in "set" that do not involve any of the dimensions
2617 * first to first + n - 1 of the given type.
2619 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2620 __isl_take isl_set *set,
2621 enum isl_dim_type type, unsigned first, unsigned n)
2623 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2626 /* Does local variable "div" of "bmap" have a complete explicit representation?
2627 * Having a complete explicit representation requires not only
2628 * an explicit representation, but also that all local variables
2629 * that appear in this explicit representation in turn have
2630 * a complete explicit representation.
2632 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2634 int i;
2635 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2636 isl_bool marked;
2638 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2639 if (marked < 0 || marked)
2640 return isl_bool_not(marked);
2642 for (i = bmap->n_div - 1; i >= 0; --i) {
2643 isl_bool known;
2645 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2646 continue;
2647 known = isl_basic_map_div_is_known(bmap, i);
2648 if (known < 0 || !known)
2649 return known;
2652 return isl_bool_true;
2655 /* Does local variable "div" of "bset" have a complete explicit representation?
2657 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2659 return isl_basic_map_div_is_known(bset, div);
2662 /* Remove all divs that are unknown or defined in terms of unknown divs.
2664 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2665 __isl_take isl_basic_map *bmap)
2667 int i;
2669 if (!bmap)
2670 return NULL;
2672 for (i = bmap->n_div - 1; i >= 0; --i) {
2673 if (isl_basic_map_div_is_known(bmap, i))
2674 continue;
2675 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2676 if (!bmap)
2677 return NULL;
2678 i = bmap->n_div;
2681 return bmap;
2684 /* Remove all divs that are unknown or defined in terms of unknown divs.
2686 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2687 __isl_take isl_basic_set *bset)
2689 return isl_basic_map_remove_unknown_divs(bset);
2692 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2694 int i;
2696 if (!map)
2697 return NULL;
2698 if (map->n == 0)
2699 return map;
2701 map = isl_map_cow(map);
2702 if (!map)
2703 return NULL;
2705 for (i = 0; i < map->n; ++i) {
2706 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2707 if (!map->p[i])
2708 goto error;
2710 return map;
2711 error:
2712 isl_map_free(map);
2713 return NULL;
2716 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2718 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2721 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2722 __isl_take isl_basic_set *bset,
2723 enum isl_dim_type type, unsigned first, unsigned n)
2725 isl_basic_map *bmap = bset_to_bmap(bset);
2726 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2727 return bset_from_bmap(bmap);
2730 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2731 enum isl_dim_type type, unsigned first, unsigned n)
2733 int i;
2735 if (n == 0)
2736 return map;
2738 map = isl_map_cow(map);
2739 if (!map)
2740 return NULL;
2741 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2743 for (i = 0; i < map->n; ++i) {
2744 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2745 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2746 if (!map->p[i])
2747 goto error;
2749 map = isl_map_drop(map, type, first, n);
2750 return map;
2751 error:
2752 isl_map_free(map);
2753 return NULL;
2756 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2757 enum isl_dim_type type, unsigned first, unsigned n)
2759 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2760 type, first, n));
2763 /* Project out n inputs starting at first using Fourier-Motzkin */
2764 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2765 unsigned first, unsigned n)
2767 return isl_map_remove_dims(map, isl_dim_in, first, n);
2770 static void dump_term(struct isl_basic_map *bmap,
2771 isl_int c, int pos, FILE *out)
2773 const char *name;
2774 unsigned in = isl_basic_map_n_in(bmap);
2775 unsigned dim = in + isl_basic_map_n_out(bmap);
2776 unsigned nparam = isl_basic_map_n_param(bmap);
2777 if (!pos)
2778 isl_int_print(out, c, 0);
2779 else {
2780 if (!isl_int_is_one(c))
2781 isl_int_print(out, c, 0);
2782 if (pos < 1 + nparam) {
2783 name = isl_space_get_dim_name(bmap->dim,
2784 isl_dim_param, pos - 1);
2785 if (name)
2786 fprintf(out, "%s", name);
2787 else
2788 fprintf(out, "p%d", pos - 1);
2789 } else if (pos < 1 + nparam + in)
2790 fprintf(out, "i%d", pos - 1 - nparam);
2791 else if (pos < 1 + nparam + dim)
2792 fprintf(out, "o%d", pos - 1 - nparam - in);
2793 else
2794 fprintf(out, "e%d", pos - 1 - nparam - dim);
2798 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2799 int sign, FILE *out)
2801 int i;
2802 int first;
2803 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2804 isl_int v;
2806 isl_int_init(v);
2807 for (i = 0, first = 1; i < len; ++i) {
2808 if (isl_int_sgn(c[i]) * sign <= 0)
2809 continue;
2810 if (!first)
2811 fprintf(out, " + ");
2812 first = 0;
2813 isl_int_abs(v, c[i]);
2814 dump_term(bmap, v, i, out);
2816 isl_int_clear(v);
2817 if (first)
2818 fprintf(out, "0");
2821 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2822 const char *op, FILE *out, int indent)
2824 int i;
2826 fprintf(out, "%*s", indent, "");
2828 dump_constraint_sign(bmap, c, 1, out);
2829 fprintf(out, " %s ", op);
2830 dump_constraint_sign(bmap, c, -1, out);
2832 fprintf(out, "\n");
2834 for (i = bmap->n_div; i < bmap->extra; ++i) {
2835 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2836 continue;
2837 fprintf(out, "%*s", indent, "");
2838 fprintf(out, "ERROR: unused div coefficient not zero\n");
2839 abort();
2843 static void dump_constraints(struct isl_basic_map *bmap,
2844 isl_int **c, unsigned n,
2845 const char *op, FILE *out, int indent)
2847 int i;
2849 for (i = 0; i < n; ++i)
2850 dump_constraint(bmap, c[i], op, out, indent);
2853 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2855 int j;
2856 int first = 1;
2857 unsigned total = isl_basic_map_total_dim(bmap);
2859 for (j = 0; j < 1 + total; ++j) {
2860 if (isl_int_is_zero(exp[j]))
2861 continue;
2862 if (!first && isl_int_is_pos(exp[j]))
2863 fprintf(out, "+");
2864 dump_term(bmap, exp[j], j, out);
2865 first = 0;
2869 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2871 int i;
2873 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2874 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2876 for (i = 0; i < bmap->n_div; ++i) {
2877 fprintf(out, "%*s", indent, "");
2878 fprintf(out, "e%d = [(", i);
2879 dump_affine(bmap, bmap->div[i]+1, out);
2880 fprintf(out, ")/");
2881 isl_int_print(out, bmap->div[i][0], 0);
2882 fprintf(out, "]\n");
2886 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2887 FILE *out, int indent)
2889 if (!bset) {
2890 fprintf(out, "null basic set\n");
2891 return;
2894 fprintf(out, "%*s", indent, "");
2895 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2896 bset->ref, bset->dim->nparam, bset->dim->n_out,
2897 bset->extra, bset->flags);
2898 dump(bset_to_bmap(bset), out, indent);
2901 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2902 FILE *out, int indent)
2904 if (!bmap) {
2905 fprintf(out, "null basic map\n");
2906 return;
2909 fprintf(out, "%*s", indent, "");
2910 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2911 "flags: %x, n_name: %d\n",
2912 bmap->ref,
2913 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2914 bmap->extra, bmap->flags, bmap->dim->n_id);
2915 dump(bmap, out, indent);
2918 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2920 unsigned total;
2921 if (!bmap)
2922 return -1;
2923 total = isl_basic_map_total_dim(bmap);
2924 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2925 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2926 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2927 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2928 return 0;
2931 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2932 unsigned flags)
2934 if (!space)
2935 return NULL;
2936 if (isl_space_dim(space, isl_dim_in) != 0)
2937 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2938 "set cannot have input dimensions", goto error);
2939 return isl_map_alloc_space(space, n, flags);
2940 error:
2941 isl_space_free(space);
2942 return NULL;
2945 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2946 unsigned nparam, unsigned dim, int n, unsigned flags)
2948 struct isl_set *set;
2949 isl_space *dims;
2951 dims = isl_space_alloc(ctx, nparam, 0, dim);
2952 if (!dims)
2953 return NULL;
2955 set = isl_set_alloc_space(dims, n, flags);
2956 return set;
2959 /* Make sure "map" has room for at least "n" more basic maps.
2961 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2963 int i;
2964 struct isl_map *grown = NULL;
2966 if (!map)
2967 return NULL;
2968 isl_assert(map->ctx, n >= 0, goto error);
2969 if (map->n + n <= map->size)
2970 return map;
2971 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2972 if (!grown)
2973 goto error;
2974 for (i = 0; i < map->n; ++i) {
2975 grown->p[i] = isl_basic_map_copy(map->p[i]);
2976 if (!grown->p[i])
2977 goto error;
2978 grown->n++;
2980 isl_map_free(map);
2981 return grown;
2982 error:
2983 isl_map_free(grown);
2984 isl_map_free(map);
2985 return NULL;
2988 /* Make sure "set" has room for at least "n" more basic sets.
2990 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2992 return set_from_map(isl_map_grow(set_to_map(set), n));
2995 struct isl_set *isl_set_dup(struct isl_set *set)
2997 int i;
2998 struct isl_set *dup;
3000 if (!set)
3001 return NULL;
3003 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
3004 if (!dup)
3005 return NULL;
3006 for (i = 0; i < set->n; ++i)
3007 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
3008 return dup;
3011 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
3013 return isl_map_from_basic_map(bset);
3016 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
3018 struct isl_map *map;
3020 if (!bmap)
3021 return NULL;
3023 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3024 return isl_map_add_basic_map(map, bmap);
3027 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3028 __isl_take isl_basic_set *bset)
3030 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3031 bset_to_bmap(bset)));
3034 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3036 return isl_map_free(set);
3039 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3041 int i;
3043 if (!set) {
3044 fprintf(out, "null set\n");
3045 return;
3048 fprintf(out, "%*s", indent, "");
3049 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3050 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3051 set->flags);
3052 for (i = 0; i < set->n; ++i) {
3053 fprintf(out, "%*s", indent, "");
3054 fprintf(out, "basic set %d:\n", i);
3055 isl_basic_set_print_internal(set->p[i], out, indent+4);
3059 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3061 int i;
3063 if (!map) {
3064 fprintf(out, "null map\n");
3065 return;
3068 fprintf(out, "%*s", indent, "");
3069 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3070 "flags: %x, n_name: %d\n",
3071 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3072 map->dim->n_out, map->flags, map->dim->n_id);
3073 for (i = 0; i < map->n; ++i) {
3074 fprintf(out, "%*s", indent, "");
3075 fprintf(out, "basic map %d:\n", i);
3076 isl_basic_map_print_internal(map->p[i], out, indent+4);
3080 struct isl_basic_map *isl_basic_map_intersect_domain(
3081 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3083 struct isl_basic_map *bmap_domain;
3085 if (!bmap || !bset)
3086 goto error;
3088 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3089 bset->dim, isl_dim_param), goto error);
3091 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3092 isl_assert(bset->ctx,
3093 isl_basic_map_compatible_domain(bmap, bset), goto error);
3095 bmap = isl_basic_map_cow(bmap);
3096 if (!bmap)
3097 goto error;
3098 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3099 bset->n_div, bset->n_eq, bset->n_ineq);
3100 bmap_domain = isl_basic_map_from_domain(bset);
3101 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3103 bmap = isl_basic_map_simplify(bmap);
3104 return isl_basic_map_finalize(bmap);
3105 error:
3106 isl_basic_map_free(bmap);
3107 isl_basic_set_free(bset);
3108 return NULL;
3111 /* Check that the space of "bset" is the same as that of the range of "bmap".
3113 static isl_stat isl_basic_map_check_compatible_range(
3114 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3116 isl_bool ok;
3118 ok = isl_basic_map_compatible_range(bmap, bset);
3119 if (ok < 0)
3120 return isl_stat_error;
3121 if (!ok)
3122 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3123 "incompatible spaces", return isl_stat_error);
3125 return isl_stat_ok;
3128 struct isl_basic_map *isl_basic_map_intersect_range(
3129 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3131 struct isl_basic_map *bmap_range;
3133 if (!bmap || !bset)
3134 goto error;
3136 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3137 bset->dim, isl_dim_param), goto error);
3139 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3140 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3141 goto error;
3143 if (isl_basic_set_plain_is_universe(bset)) {
3144 isl_basic_set_free(bset);
3145 return bmap;
3148 bmap = isl_basic_map_cow(bmap);
3149 if (!bmap)
3150 goto error;
3151 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3152 bset->n_div, bset->n_eq, bset->n_ineq);
3153 bmap_range = bset_to_bmap(bset);
3154 bmap = add_constraints(bmap, bmap_range, 0, 0);
3156 bmap = isl_basic_map_simplify(bmap);
3157 return isl_basic_map_finalize(bmap);
3158 error:
3159 isl_basic_map_free(bmap);
3160 isl_basic_set_free(bset);
3161 return NULL;
3164 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3165 __isl_keep isl_vec *vec)
3167 int i;
3168 unsigned total;
3169 isl_int s;
3171 if (!bmap || !vec)
3172 return isl_bool_error;
3174 total = 1 + isl_basic_map_total_dim(bmap);
3175 if (total != vec->size)
3176 return isl_bool_false;
3178 isl_int_init(s);
3180 for (i = 0; i < bmap->n_eq; ++i) {
3181 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3182 if (!isl_int_is_zero(s)) {
3183 isl_int_clear(s);
3184 return isl_bool_false;
3188 for (i = 0; i < bmap->n_ineq; ++i) {
3189 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3190 if (isl_int_is_neg(s)) {
3191 isl_int_clear(s);
3192 return isl_bool_false;
3196 isl_int_clear(s);
3198 return isl_bool_true;
3201 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3202 __isl_keep isl_vec *vec)
3204 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3207 struct isl_basic_map *isl_basic_map_intersect(
3208 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3210 struct isl_vec *sample = NULL;
3212 if (!bmap1 || !bmap2)
3213 goto error;
3215 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
3216 bmap2->dim, isl_dim_param), goto error);
3217 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3218 isl_space_dim(bmap1->dim, isl_dim_param) &&
3219 isl_space_dim(bmap2->dim, isl_dim_all) !=
3220 isl_space_dim(bmap2->dim, isl_dim_param))
3221 return isl_basic_map_intersect(bmap2, bmap1);
3223 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3224 isl_space_dim(bmap2->dim, isl_dim_param))
3225 isl_assert(bmap1->ctx,
3226 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3228 if (isl_basic_map_plain_is_empty(bmap1)) {
3229 isl_basic_map_free(bmap2);
3230 return bmap1;
3232 if (isl_basic_map_plain_is_empty(bmap2)) {
3233 isl_basic_map_free(bmap1);
3234 return bmap2;
3237 if (bmap1->sample &&
3238 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3239 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3240 sample = isl_vec_copy(bmap1->sample);
3241 else if (bmap2->sample &&
3242 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3243 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3244 sample = isl_vec_copy(bmap2->sample);
3246 bmap1 = isl_basic_map_cow(bmap1);
3247 if (!bmap1)
3248 goto error;
3249 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3250 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3251 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3253 if (!bmap1)
3254 isl_vec_free(sample);
3255 else if (sample) {
3256 isl_vec_free(bmap1->sample);
3257 bmap1->sample = sample;
3260 bmap1 = isl_basic_map_simplify(bmap1);
3261 return isl_basic_map_finalize(bmap1);
3262 error:
3263 if (sample)
3264 isl_vec_free(sample);
3265 isl_basic_map_free(bmap1);
3266 isl_basic_map_free(bmap2);
3267 return NULL;
3270 struct isl_basic_set *isl_basic_set_intersect(
3271 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3273 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3274 bset_to_bmap(bset2)));
3277 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3278 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3280 return isl_basic_set_intersect(bset1, bset2);
3283 /* Special case of isl_map_intersect, where both map1 and map2
3284 * are convex, without any divs and such that either map1 or map2
3285 * contains a single constraint. This constraint is then simply
3286 * added to the other map.
3288 static __isl_give isl_map *map_intersect_add_constraint(
3289 __isl_take isl_map *map1, __isl_take isl_map *map2)
3291 isl_assert(map1->ctx, map1->n == 1, goto error);
3292 isl_assert(map2->ctx, map1->n == 1, goto error);
3293 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3294 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3296 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3297 return isl_map_intersect(map2, map1);
3299 isl_assert(map2->ctx,
3300 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3302 map1 = isl_map_cow(map1);
3303 if (!map1)
3304 goto error;
3305 if (isl_map_plain_is_empty(map1)) {
3306 isl_map_free(map2);
3307 return map1;
3309 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3310 if (map2->p[0]->n_eq == 1)
3311 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3312 else
3313 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3314 map2->p[0]->ineq[0]);
3316 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3317 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3318 if (!map1->p[0])
3319 goto error;
3321 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3322 isl_basic_map_free(map1->p[0]);
3323 map1->n = 0;
3326 isl_map_free(map2);
3328 return map1;
3329 error:
3330 isl_map_free(map1);
3331 isl_map_free(map2);
3332 return NULL;
3335 /* map2 may be either a parameter domain or a map living in the same
3336 * space as map1.
3338 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3339 __isl_take isl_map *map2)
3341 unsigned flags = 0;
3342 isl_map *result;
3343 int i, j;
3345 if (!map1 || !map2)
3346 goto error;
3348 if ((isl_map_plain_is_empty(map1) ||
3349 isl_map_plain_is_universe(map2)) &&
3350 isl_space_is_equal(map1->dim, map2->dim)) {
3351 isl_map_free(map2);
3352 return map1;
3354 if ((isl_map_plain_is_empty(map2) ||
3355 isl_map_plain_is_universe(map1)) &&
3356 isl_space_is_equal(map1->dim, map2->dim)) {
3357 isl_map_free(map1);
3358 return map2;
3361 if (map1->n == 1 && map2->n == 1 &&
3362 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3363 isl_space_is_equal(map1->dim, map2->dim) &&
3364 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3365 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3366 return map_intersect_add_constraint(map1, map2);
3368 if (isl_space_dim(map2->dim, isl_dim_all) !=
3369 isl_space_dim(map2->dim, isl_dim_param))
3370 isl_assert(map1->ctx,
3371 isl_space_is_equal(map1->dim, map2->dim), goto error);
3373 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3374 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3375 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3377 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3378 map1->n * map2->n, flags);
3379 if (!result)
3380 goto error;
3381 for (i = 0; i < map1->n; ++i)
3382 for (j = 0; j < map2->n; ++j) {
3383 struct isl_basic_map *part;
3384 part = isl_basic_map_intersect(
3385 isl_basic_map_copy(map1->p[i]),
3386 isl_basic_map_copy(map2->p[j]));
3387 if (isl_basic_map_is_empty(part) < 0)
3388 part = isl_basic_map_free(part);
3389 result = isl_map_add_basic_map(result, part);
3390 if (!result)
3391 goto error;
3393 isl_map_free(map1);
3394 isl_map_free(map2);
3395 return result;
3396 error:
3397 isl_map_free(map1);
3398 isl_map_free(map2);
3399 return NULL;
3402 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3403 __isl_take isl_map *map2)
3405 if (!map1 || !map2)
3406 goto error;
3407 if (!isl_space_is_equal(map1->dim, map2->dim))
3408 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3409 "spaces don't match", goto error);
3410 return map_intersect_internal(map1, map2);
3411 error:
3412 isl_map_free(map1);
3413 isl_map_free(map2);
3414 return NULL;
3417 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3418 __isl_take isl_map *map2)
3420 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3423 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3425 return set_from_map(isl_map_intersect(set_to_map(set1),
3426 set_to_map(set2)));
3429 /* map_intersect_internal accepts intersections
3430 * with parameter domains, so we can just call that function.
3432 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3433 __isl_take isl_set *params)
3435 return map_intersect_internal(map, params);
3438 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3439 __isl_take isl_map *map2)
3441 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3444 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3445 __isl_take isl_set *params)
3447 return isl_map_intersect_params(set, params);
3450 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3452 isl_space *space;
3453 unsigned pos, n1, n2;
3455 if (!bmap)
3456 return NULL;
3457 bmap = isl_basic_map_cow(bmap);
3458 if (!bmap)
3459 return NULL;
3460 space = isl_space_reverse(isl_space_copy(bmap->dim));
3461 pos = isl_basic_map_offset(bmap, isl_dim_in);
3462 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3463 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3464 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3465 return isl_basic_map_reset_space(bmap, space);
3468 static __isl_give isl_basic_map *basic_map_space_reset(
3469 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3471 isl_space *space;
3473 if (!bmap)
3474 return NULL;
3475 if (!isl_space_is_named_or_nested(bmap->dim, type))
3476 return bmap;
3478 space = isl_basic_map_get_space(bmap);
3479 space = isl_space_reset(space, type);
3480 bmap = isl_basic_map_reset_space(bmap, space);
3481 return bmap;
3484 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3485 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3486 unsigned pos, unsigned n)
3488 isl_space *res_dim;
3489 struct isl_basic_map *res;
3490 struct isl_dim_map *dim_map;
3491 unsigned total, off;
3492 enum isl_dim_type t;
3494 if (n == 0)
3495 return basic_map_space_reset(bmap, type);
3497 if (!bmap)
3498 return NULL;
3500 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3502 total = isl_basic_map_total_dim(bmap) + n;
3503 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3504 off = 0;
3505 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3506 if (t != type) {
3507 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3508 } else {
3509 unsigned size = isl_basic_map_dim(bmap, t);
3510 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3511 0, pos, off);
3512 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3513 pos, size - pos, off + pos + n);
3515 off += isl_space_dim(res_dim, t);
3517 isl_dim_map_div(dim_map, bmap, off);
3519 res = isl_basic_map_alloc_space(res_dim,
3520 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3521 if (isl_basic_map_is_rational(bmap))
3522 res = isl_basic_map_set_rational(res);
3523 if (isl_basic_map_plain_is_empty(bmap)) {
3524 isl_basic_map_free(bmap);
3525 free(dim_map);
3526 return isl_basic_map_set_to_empty(res);
3528 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3529 return isl_basic_map_finalize(res);
3532 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3533 __isl_take isl_basic_set *bset,
3534 enum isl_dim_type type, unsigned pos, unsigned n)
3536 return isl_basic_map_insert_dims(bset, type, pos, n);
3539 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3540 enum isl_dim_type type, unsigned n)
3542 if (!bmap)
3543 return NULL;
3544 return isl_basic_map_insert_dims(bmap, type,
3545 isl_basic_map_dim(bmap, type), n);
3548 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3549 enum isl_dim_type type, unsigned n)
3551 if (!bset)
3552 return NULL;
3553 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3554 return isl_basic_map_add_dims(bset, type, n);
3555 error:
3556 isl_basic_set_free(bset);
3557 return NULL;
3560 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3561 enum isl_dim_type type)
3563 isl_space *space;
3565 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3566 return map;
3568 space = isl_map_get_space(map);
3569 space = isl_space_reset(space, type);
3570 map = isl_map_reset_space(map, space);
3571 return map;
3574 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3575 enum isl_dim_type type, unsigned pos, unsigned n)
3577 int i;
3579 if (n == 0)
3580 return map_space_reset(map, type);
3582 map = isl_map_cow(map);
3583 if (!map)
3584 return NULL;
3586 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3587 if (!map->dim)
3588 goto error;
3590 for (i = 0; i < map->n; ++i) {
3591 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3592 if (!map->p[i])
3593 goto error;
3596 return map;
3597 error:
3598 isl_map_free(map);
3599 return NULL;
3602 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3603 enum isl_dim_type type, unsigned pos, unsigned n)
3605 return isl_map_insert_dims(set, type, pos, n);
3608 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3609 enum isl_dim_type type, unsigned n)
3611 if (!map)
3612 return NULL;
3613 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3616 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3617 enum isl_dim_type type, unsigned n)
3619 if (!set)
3620 return NULL;
3621 isl_assert(set->ctx, type != isl_dim_in, goto error);
3622 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3623 error:
3624 isl_set_free(set);
3625 return NULL;
3628 __isl_give isl_basic_map *isl_basic_map_move_dims(
3629 __isl_take isl_basic_map *bmap,
3630 enum isl_dim_type dst_type, unsigned dst_pos,
3631 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3633 struct isl_dim_map *dim_map;
3634 struct isl_basic_map *res;
3635 enum isl_dim_type t;
3636 unsigned total, off;
3638 if (!bmap)
3639 return NULL;
3640 if (n == 0)
3641 return bmap;
3643 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3644 return isl_basic_map_free(bmap);
3646 if (dst_type == src_type && dst_pos == src_pos)
3647 return bmap;
3649 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3651 if (pos(bmap->dim, dst_type) + dst_pos ==
3652 pos(bmap->dim, src_type) + src_pos +
3653 ((src_type < dst_type) ? n : 0)) {
3654 bmap = isl_basic_map_cow(bmap);
3655 if (!bmap)
3656 return NULL;
3658 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3659 src_type, src_pos, n);
3660 if (!bmap->dim)
3661 goto error;
3663 bmap = isl_basic_map_finalize(bmap);
3665 return bmap;
3668 total = isl_basic_map_total_dim(bmap);
3669 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3671 off = 0;
3672 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3673 unsigned size = isl_space_dim(bmap->dim, t);
3674 if (t == dst_type) {
3675 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3676 0, dst_pos, off);
3677 off += dst_pos;
3678 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3679 src_pos, n, off);
3680 off += n;
3681 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3682 dst_pos, size - dst_pos, off);
3683 off += size - dst_pos;
3684 } else if (t == src_type) {
3685 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3686 0, src_pos, off);
3687 off += src_pos;
3688 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3689 src_pos + n, size - src_pos - n, off);
3690 off += size - src_pos - n;
3691 } else {
3692 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3693 off += size;
3696 isl_dim_map_div(dim_map, bmap, off);
3698 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3699 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3700 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3701 if (!bmap)
3702 goto error;
3704 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3705 src_type, src_pos, n);
3706 if (!bmap->dim)
3707 goto error;
3709 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3710 bmap = isl_basic_map_gauss(bmap, NULL);
3711 bmap = isl_basic_map_finalize(bmap);
3713 return bmap;
3714 error:
3715 isl_basic_map_free(bmap);
3716 return NULL;
3719 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3720 enum isl_dim_type dst_type, unsigned dst_pos,
3721 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3723 isl_basic_map *bmap = bset_to_bmap(bset);
3724 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3725 src_type, src_pos, n);
3726 return bset_from_bmap(bmap);
3729 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3730 enum isl_dim_type dst_type, unsigned dst_pos,
3731 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3733 if (!set)
3734 return NULL;
3735 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3736 return set_from_map(isl_map_move_dims(set_to_map(set),
3737 dst_type, dst_pos, src_type, src_pos, n));
3738 error:
3739 isl_set_free(set);
3740 return NULL;
3743 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3744 enum isl_dim_type dst_type, unsigned dst_pos,
3745 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3747 int i;
3749 if (!map)
3750 return NULL;
3751 if (n == 0)
3752 return map;
3754 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3755 goto error);
3757 if (dst_type == src_type && dst_pos == src_pos)
3758 return map;
3760 isl_assert(map->ctx, dst_type != src_type, goto error);
3762 map = isl_map_cow(map);
3763 if (!map)
3764 return NULL;
3766 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3767 if (!map->dim)
3768 goto error;
3770 for (i = 0; i < map->n; ++i) {
3771 map->p[i] = isl_basic_map_move_dims(map->p[i],
3772 dst_type, dst_pos,
3773 src_type, src_pos, n);
3774 if (!map->p[i])
3775 goto error;
3778 return map;
3779 error:
3780 isl_map_free(map);
3781 return NULL;
3784 /* Move the specified dimensions to the last columns right before
3785 * the divs. Don't change the dimension specification of bmap.
3786 * That's the responsibility of the caller.
3788 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3789 enum isl_dim_type type, unsigned first, unsigned n)
3791 struct isl_dim_map *dim_map;
3792 struct isl_basic_map *res;
3793 enum isl_dim_type t;
3794 unsigned total, off;
3796 if (!bmap)
3797 return NULL;
3798 if (pos(bmap->dim, type) + first + n ==
3799 1 + isl_space_dim(bmap->dim, isl_dim_all))
3800 return bmap;
3802 total = isl_basic_map_total_dim(bmap);
3803 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3805 off = 0;
3806 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3807 unsigned size = isl_space_dim(bmap->dim, t);
3808 if (t == type) {
3809 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3810 0, first, off);
3811 off += first;
3812 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3813 first, n, total - bmap->n_div - n);
3814 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3815 first + n, size - (first + n), off);
3816 off += size - (first + n);
3817 } else {
3818 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3819 off += size;
3822 isl_dim_map_div(dim_map, bmap, off + n);
3824 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3825 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3826 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3827 return res;
3830 /* Insert "n" rows in the divs of "bmap".
3832 * The number of columns is not changed, which means that the last
3833 * dimensions of "bmap" are being reintepreted as the new divs.
3834 * The space of "bmap" is not adjusted, however, which means
3835 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3836 * from the space of "bmap" is the responsibility of the caller.
3838 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3839 int n)
3841 int i;
3842 size_t row_size;
3843 isl_int **new_div;
3844 isl_int *old;
3846 bmap = isl_basic_map_cow(bmap);
3847 if (!bmap)
3848 return NULL;
3850 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3851 old = bmap->block2.data;
3852 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3853 (bmap->extra + n) * (1 + row_size));
3854 if (!bmap->block2.data)
3855 return isl_basic_map_free(bmap);
3856 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3857 if (!new_div)
3858 return isl_basic_map_free(bmap);
3859 for (i = 0; i < n; ++i) {
3860 new_div[i] = bmap->block2.data +
3861 (bmap->extra + i) * (1 + row_size);
3862 isl_seq_clr(new_div[i], 1 + row_size);
3864 for (i = 0; i < bmap->extra; ++i)
3865 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3866 free(bmap->div);
3867 bmap->div = new_div;
3868 bmap->n_div += n;
3869 bmap->extra += n;
3871 return bmap;
3874 /* Drop constraints from "bmap" that only involve the variables
3875 * of "type" in the range [first, first + n] that are not related
3876 * to any of the variables outside that interval.
3877 * These constraints cannot influence the values for the variables
3878 * outside the interval, except in case they cause "bmap" to be empty.
3879 * Only drop the constraints if "bmap" is known to be non-empty.
3881 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3882 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3883 unsigned first, unsigned n)
3885 int i;
3886 int *groups;
3887 unsigned dim, n_div;
3888 isl_bool non_empty;
3890 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3891 if (non_empty < 0)
3892 return isl_basic_map_free(bmap);
3893 if (!non_empty)
3894 return bmap;
3896 dim = isl_basic_map_dim(bmap, isl_dim_all);
3897 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3898 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3899 if (!groups)
3900 return isl_basic_map_free(bmap);
3901 first += isl_basic_map_offset(bmap, type) - 1;
3902 for (i = 0; i < first; ++i)
3903 groups[i] = -1;
3904 for (i = first + n; i < dim - n_div; ++i)
3905 groups[i] = -1;
3907 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3909 return bmap;
3912 /* Turn the n dimensions of type type, starting at first
3913 * into existentially quantified variables.
3915 * If a subset of the projected out variables are unrelated
3916 * to any of the variables that remain, then the constraints
3917 * involving this subset are simply dropped first.
3919 __isl_give isl_basic_map *isl_basic_map_project_out(
3920 __isl_take isl_basic_map *bmap,
3921 enum isl_dim_type type, unsigned first, unsigned n)
3923 if (n == 0)
3924 return basic_map_space_reset(bmap, type);
3925 if (type == isl_dim_div)
3926 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3927 "cannot project out existentially quantified variables",
3928 return isl_basic_map_free(bmap));
3930 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3931 if (!bmap)
3932 return NULL;
3934 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3935 return isl_basic_map_remove_dims(bmap, type, first, n);
3937 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3938 return isl_basic_map_free(bmap);
3940 bmap = move_last(bmap, type, first, n);
3941 bmap = isl_basic_map_cow(bmap);
3942 bmap = insert_div_rows(bmap, n);
3943 if (!bmap)
3944 return NULL;
3946 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3947 if (!bmap->dim)
3948 goto error;
3949 bmap = isl_basic_map_simplify(bmap);
3950 bmap = isl_basic_map_drop_redundant_divs(bmap);
3951 return isl_basic_map_finalize(bmap);
3952 error:
3953 isl_basic_map_free(bmap);
3954 return NULL;
3957 /* Turn the n dimensions of type type, starting at first
3958 * into existentially quantified variables.
3960 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3961 enum isl_dim_type type, unsigned first, unsigned n)
3963 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3964 type, first, n));
3967 /* Turn the n dimensions of type type, starting at first
3968 * into existentially quantified variables.
3970 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3971 enum isl_dim_type type, unsigned first, unsigned n)
3973 int i;
3975 if (!map)
3976 return NULL;
3978 if (n == 0)
3979 return map_space_reset(map, type);
3981 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3983 map = isl_map_cow(map);
3984 if (!map)
3985 return NULL;
3987 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3988 if (!map->dim)
3989 goto error;
3991 for (i = 0; i < map->n; ++i) {
3992 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3993 if (!map->p[i])
3994 goto error;
3997 return map;
3998 error:
3999 isl_map_free(map);
4000 return NULL;
4003 /* Turn the n dimensions of type type, starting at first
4004 * into existentially quantified variables.
4006 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4007 enum isl_dim_type type, unsigned first, unsigned n)
4009 return set_from_map(isl_map_project_out(set_to_map(set),
4010 type, first, n));
4013 /* Return a map that projects the elements in "set" onto their
4014 * "n" set dimensions starting at "first".
4015 * "type" should be equal to isl_dim_set.
4017 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4018 enum isl_dim_type type, unsigned first, unsigned n)
4020 int i;
4021 int dim;
4022 isl_map *map;
4024 if (!set)
4025 return NULL;
4026 if (type != isl_dim_set)
4027 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4028 "only set dimensions can be projected out", goto error);
4029 dim = isl_set_dim(set, isl_dim_set);
4030 if (first + n > dim || first + n < first)
4031 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4032 "index out of bounds", goto error);
4034 map = isl_map_from_domain(set);
4035 map = isl_map_add_dims(map, isl_dim_out, n);
4036 for (i = 0; i < n; ++i)
4037 map = isl_map_equate(map, isl_dim_in, first + i,
4038 isl_dim_out, i);
4039 return map;
4040 error:
4041 isl_set_free(set);
4042 return NULL;
4045 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4047 int i, j;
4049 for (i = 0; i < n; ++i) {
4050 j = isl_basic_map_alloc_div(bmap);
4051 if (j < 0)
4052 goto error;
4053 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4055 return bmap;
4056 error:
4057 isl_basic_map_free(bmap);
4058 return NULL;
4061 struct isl_basic_map *isl_basic_map_apply_range(
4062 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4064 isl_space *dim_result = NULL;
4065 struct isl_basic_map *bmap;
4066 unsigned n_in, n_out, n, nparam, total, pos;
4067 struct isl_dim_map *dim_map1, *dim_map2;
4069 if (!bmap1 || !bmap2)
4070 goto error;
4071 if (!isl_space_match(bmap1->dim, isl_dim_param,
4072 bmap2->dim, isl_dim_param))
4073 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4074 "parameters don't match", goto error);
4075 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4076 bmap2->dim, isl_dim_in))
4077 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4078 "spaces don't match", goto error);
4080 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4081 isl_space_copy(bmap2->dim));
4083 n_in = isl_basic_map_n_in(bmap1);
4084 n_out = isl_basic_map_n_out(bmap2);
4085 n = isl_basic_map_n_out(bmap1);
4086 nparam = isl_basic_map_n_param(bmap1);
4088 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4089 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4090 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4091 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4092 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4093 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4094 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4095 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4096 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4097 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4098 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4100 bmap = isl_basic_map_alloc_space(dim_result,
4101 bmap1->n_div + bmap2->n_div + n,
4102 bmap1->n_eq + bmap2->n_eq,
4103 bmap1->n_ineq + bmap2->n_ineq);
4104 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4105 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4106 bmap = add_divs(bmap, n);
4107 bmap = isl_basic_map_simplify(bmap);
4108 bmap = isl_basic_map_drop_redundant_divs(bmap);
4109 return isl_basic_map_finalize(bmap);
4110 error:
4111 isl_basic_map_free(bmap1);
4112 isl_basic_map_free(bmap2);
4113 return NULL;
4116 struct isl_basic_set *isl_basic_set_apply(
4117 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4119 if (!bset || !bmap)
4120 goto error;
4122 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4123 goto error);
4125 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4126 bmap));
4127 error:
4128 isl_basic_set_free(bset);
4129 isl_basic_map_free(bmap);
4130 return NULL;
4133 struct isl_basic_map *isl_basic_map_apply_domain(
4134 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4136 if (!bmap1 || !bmap2)
4137 goto error;
4139 isl_assert(bmap1->ctx,
4140 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
4141 isl_assert(bmap1->ctx,
4142 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
4143 goto error);
4145 bmap1 = isl_basic_map_reverse(bmap1);
4146 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4147 return isl_basic_map_reverse(bmap1);
4148 error:
4149 isl_basic_map_free(bmap1);
4150 isl_basic_map_free(bmap2);
4151 return NULL;
4154 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4155 * A \cap B -> f(A) + f(B)
4157 struct isl_basic_map *isl_basic_map_sum(
4158 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4160 unsigned n_in, n_out, nparam, total, pos;
4161 struct isl_basic_map *bmap = NULL;
4162 struct isl_dim_map *dim_map1, *dim_map2;
4163 int i;
4165 if (!bmap1 || !bmap2)
4166 goto error;
4168 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4169 goto error);
4171 nparam = isl_basic_map_n_param(bmap1);
4172 n_in = isl_basic_map_n_in(bmap1);
4173 n_out = isl_basic_map_n_out(bmap1);
4175 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4176 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4177 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4178 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4179 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4180 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4181 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4182 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4183 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4184 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4185 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4187 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4188 bmap1->n_div + bmap2->n_div + 2 * n_out,
4189 bmap1->n_eq + bmap2->n_eq + n_out,
4190 bmap1->n_ineq + bmap2->n_ineq);
4191 for (i = 0; i < n_out; ++i) {
4192 int j = isl_basic_map_alloc_equality(bmap);
4193 if (j < 0)
4194 goto error;
4195 isl_seq_clr(bmap->eq[j], 1+total);
4196 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4197 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4198 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4200 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4201 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4202 bmap = add_divs(bmap, 2 * n_out);
4204 bmap = isl_basic_map_simplify(bmap);
4205 return isl_basic_map_finalize(bmap);
4206 error:
4207 isl_basic_map_free(bmap);
4208 isl_basic_map_free(bmap1);
4209 isl_basic_map_free(bmap2);
4210 return NULL;
4213 /* Given two maps A -> f(A) and B -> g(B), construct a map
4214 * A \cap B -> f(A) + f(B)
4216 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4218 struct isl_map *result;
4219 int i, j;
4221 if (!map1 || !map2)
4222 goto error;
4224 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4226 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4227 map1->n * map2->n, 0);
4228 if (!result)
4229 goto error;
4230 for (i = 0; i < map1->n; ++i)
4231 for (j = 0; j < map2->n; ++j) {
4232 struct isl_basic_map *part;
4233 part = isl_basic_map_sum(
4234 isl_basic_map_copy(map1->p[i]),
4235 isl_basic_map_copy(map2->p[j]));
4236 if (isl_basic_map_is_empty(part))
4237 isl_basic_map_free(part);
4238 else
4239 result = isl_map_add_basic_map(result, part);
4240 if (!result)
4241 goto error;
4243 isl_map_free(map1);
4244 isl_map_free(map2);
4245 return result;
4246 error:
4247 isl_map_free(map1);
4248 isl_map_free(map2);
4249 return NULL;
4252 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4253 __isl_take isl_set *set2)
4255 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4258 /* Given a basic map A -> f(A), construct A -> -f(A).
4260 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4262 int i, j;
4263 unsigned off, n;
4265 bmap = isl_basic_map_cow(bmap);
4266 if (!bmap)
4267 return NULL;
4269 n = isl_basic_map_dim(bmap, isl_dim_out);
4270 off = isl_basic_map_offset(bmap, isl_dim_out);
4271 for (i = 0; i < bmap->n_eq; ++i)
4272 for (j = 0; j < n; ++j)
4273 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4274 for (i = 0; i < bmap->n_ineq; ++i)
4275 for (j = 0; j < n; ++j)
4276 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4277 for (i = 0; i < bmap->n_div; ++i)
4278 for (j = 0; j < n; ++j)
4279 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4280 bmap = isl_basic_map_gauss(bmap, NULL);
4281 return isl_basic_map_finalize(bmap);
4284 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4286 return isl_basic_map_neg(bset);
4289 /* Given a map A -> f(A), construct A -> -f(A).
4291 struct isl_map *isl_map_neg(struct isl_map *map)
4293 int i;
4295 map = isl_map_cow(map);
4296 if (!map)
4297 return NULL;
4299 for (i = 0; i < map->n; ++i) {
4300 map->p[i] = isl_basic_map_neg(map->p[i]);
4301 if (!map->p[i])
4302 goto error;
4305 return map;
4306 error:
4307 isl_map_free(map);
4308 return NULL;
4311 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4313 return set_from_map(isl_map_neg(set_to_map(set)));
4316 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4317 * A -> floor(f(A)/d).
4319 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4320 isl_int d)
4322 unsigned n_in, n_out, nparam, total, pos;
4323 struct isl_basic_map *result = NULL;
4324 struct isl_dim_map *dim_map;
4325 int i;
4327 if (!bmap)
4328 return NULL;
4330 nparam = isl_basic_map_n_param(bmap);
4331 n_in = isl_basic_map_n_in(bmap);
4332 n_out = isl_basic_map_n_out(bmap);
4334 total = nparam + n_in + n_out + bmap->n_div + n_out;
4335 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4336 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4337 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4338 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4339 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4341 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4342 bmap->n_div + n_out,
4343 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4344 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4345 result = add_divs(result, n_out);
4346 for (i = 0; i < n_out; ++i) {
4347 int j;
4348 j = isl_basic_map_alloc_inequality(result);
4349 if (j < 0)
4350 goto error;
4351 isl_seq_clr(result->ineq[j], 1+total);
4352 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4353 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4354 j = isl_basic_map_alloc_inequality(result);
4355 if (j < 0)
4356 goto error;
4357 isl_seq_clr(result->ineq[j], 1+total);
4358 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4359 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4360 isl_int_sub_ui(result->ineq[j][0], d, 1);
4363 result = isl_basic_map_simplify(result);
4364 return isl_basic_map_finalize(result);
4365 error:
4366 isl_basic_map_free(result);
4367 return NULL;
4370 /* Given a map A -> f(A) and an integer d, construct a map
4371 * A -> floor(f(A)/d).
4373 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4375 int i;
4377 map = isl_map_cow(map);
4378 if (!map)
4379 return NULL;
4381 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4382 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4383 for (i = 0; i < map->n; ++i) {
4384 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4385 if (!map->p[i])
4386 goto error;
4389 return map;
4390 error:
4391 isl_map_free(map);
4392 return NULL;
4395 /* Given a map A -> f(A) and an integer d, construct a map
4396 * A -> floor(f(A)/d).
4398 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4399 __isl_take isl_val *d)
4401 if (!map || !d)
4402 goto error;
4403 if (!isl_val_is_int(d))
4404 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4405 "expecting integer denominator", goto error);
4406 map = isl_map_floordiv(map, d->n);
4407 isl_val_free(d);
4408 return map;
4409 error:
4410 isl_map_free(map);
4411 isl_val_free(d);
4412 return NULL;
4415 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4417 int i;
4418 unsigned nparam;
4419 unsigned n_in;
4421 i = isl_basic_map_alloc_equality(bmap);
4422 if (i < 0)
4423 goto error;
4424 nparam = isl_basic_map_n_param(bmap);
4425 n_in = isl_basic_map_n_in(bmap);
4426 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4427 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4428 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4429 return isl_basic_map_finalize(bmap);
4430 error:
4431 isl_basic_map_free(bmap);
4432 return NULL;
4435 /* Add a constraint to "bmap" expressing i_pos < o_pos
4437 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4439 int i;
4440 unsigned nparam;
4441 unsigned n_in;
4443 i = isl_basic_map_alloc_inequality(bmap);
4444 if (i < 0)
4445 goto error;
4446 nparam = isl_basic_map_n_param(bmap);
4447 n_in = isl_basic_map_n_in(bmap);
4448 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4449 isl_int_set_si(bmap->ineq[i][0], -1);
4450 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4451 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4452 return isl_basic_map_finalize(bmap);
4453 error:
4454 isl_basic_map_free(bmap);
4455 return NULL;
4458 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4460 static __isl_give isl_basic_map *var_less_or_equal(
4461 __isl_take isl_basic_map *bmap, unsigned pos)
4463 int i;
4464 unsigned nparam;
4465 unsigned n_in;
4467 i = isl_basic_map_alloc_inequality(bmap);
4468 if (i < 0)
4469 goto error;
4470 nparam = isl_basic_map_n_param(bmap);
4471 n_in = isl_basic_map_n_in(bmap);
4472 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4473 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4474 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4475 return isl_basic_map_finalize(bmap);
4476 error:
4477 isl_basic_map_free(bmap);
4478 return NULL;
4481 /* Add a constraint to "bmap" expressing i_pos > o_pos
4483 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4485 int i;
4486 unsigned nparam;
4487 unsigned n_in;
4489 i = isl_basic_map_alloc_inequality(bmap);
4490 if (i < 0)
4491 goto error;
4492 nparam = isl_basic_map_n_param(bmap);
4493 n_in = isl_basic_map_n_in(bmap);
4494 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4495 isl_int_set_si(bmap->ineq[i][0], -1);
4496 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4497 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4498 return isl_basic_map_finalize(bmap);
4499 error:
4500 isl_basic_map_free(bmap);
4501 return NULL;
4504 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4506 static __isl_give isl_basic_map *var_more_or_equal(
4507 __isl_take isl_basic_map *bmap, unsigned pos)
4509 int i;
4510 unsigned nparam;
4511 unsigned n_in;
4513 i = isl_basic_map_alloc_inequality(bmap);
4514 if (i < 0)
4515 goto error;
4516 nparam = isl_basic_map_n_param(bmap);
4517 n_in = isl_basic_map_n_in(bmap);
4518 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4519 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4520 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4521 return isl_basic_map_finalize(bmap);
4522 error:
4523 isl_basic_map_free(bmap);
4524 return NULL;
4527 __isl_give isl_basic_map *isl_basic_map_equal(
4528 __isl_take isl_space *dim, unsigned n_equal)
4530 int i;
4531 struct isl_basic_map *bmap;
4532 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4533 if (!bmap)
4534 return NULL;
4535 for (i = 0; i < n_equal && bmap; ++i)
4536 bmap = var_equal(bmap, i);
4537 return isl_basic_map_finalize(bmap);
4540 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4542 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4543 unsigned pos)
4545 int i;
4546 struct isl_basic_map *bmap;
4547 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4548 if (!bmap)
4549 return NULL;
4550 for (i = 0; i < pos && bmap; ++i)
4551 bmap = var_equal(bmap, i);
4552 if (bmap)
4553 bmap = var_less(bmap, pos);
4554 return isl_basic_map_finalize(bmap);
4557 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4559 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4560 __isl_take isl_space *dim, unsigned pos)
4562 int i;
4563 isl_basic_map *bmap;
4565 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4566 for (i = 0; i < pos; ++i)
4567 bmap = var_equal(bmap, i);
4568 bmap = var_less_or_equal(bmap, pos);
4569 return isl_basic_map_finalize(bmap);
4572 /* Return a relation on "dim" expressing i_pos > o_pos
4574 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4575 unsigned pos)
4577 int i;
4578 struct isl_basic_map *bmap;
4579 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4580 if (!bmap)
4581 return NULL;
4582 for (i = 0; i < pos && bmap; ++i)
4583 bmap = var_equal(bmap, i);
4584 if (bmap)
4585 bmap = var_more(bmap, pos);
4586 return isl_basic_map_finalize(bmap);
4589 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4591 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4592 __isl_take isl_space *dim, unsigned pos)
4594 int i;
4595 isl_basic_map *bmap;
4597 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4598 for (i = 0; i < pos; ++i)
4599 bmap = var_equal(bmap, i);
4600 bmap = var_more_or_equal(bmap, pos);
4601 return isl_basic_map_finalize(bmap);
4604 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4605 unsigned n, int equal)
4607 struct isl_map *map;
4608 int i;
4610 if (n == 0 && equal)
4611 return isl_map_universe(dims);
4613 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4615 for (i = 0; i + 1 < n; ++i)
4616 map = isl_map_add_basic_map(map,
4617 isl_basic_map_less_at(isl_space_copy(dims), i));
4618 if (n > 0) {
4619 if (equal)
4620 map = isl_map_add_basic_map(map,
4621 isl_basic_map_less_or_equal_at(dims, n - 1));
4622 else
4623 map = isl_map_add_basic_map(map,
4624 isl_basic_map_less_at(dims, n - 1));
4625 } else
4626 isl_space_free(dims);
4628 return map;
4631 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4633 if (!dims)
4634 return NULL;
4635 return map_lex_lte_first(dims, dims->n_out, equal);
4638 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4640 return map_lex_lte_first(dim, n, 0);
4643 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4645 return map_lex_lte_first(dim, n, 1);
4648 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4650 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4653 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4655 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4658 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4659 unsigned n, int equal)
4661 struct isl_map *map;
4662 int i;
4664 if (n == 0 && equal)
4665 return isl_map_universe(dims);
4667 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4669 for (i = 0; i + 1 < n; ++i)
4670 map = isl_map_add_basic_map(map,
4671 isl_basic_map_more_at(isl_space_copy(dims), i));
4672 if (n > 0) {
4673 if (equal)
4674 map = isl_map_add_basic_map(map,
4675 isl_basic_map_more_or_equal_at(dims, n - 1));
4676 else
4677 map = isl_map_add_basic_map(map,
4678 isl_basic_map_more_at(dims, n - 1));
4679 } else
4680 isl_space_free(dims);
4682 return map;
4685 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4687 if (!dims)
4688 return NULL;
4689 return map_lex_gte_first(dims, dims->n_out, equal);
4692 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4694 return map_lex_gte_first(dim, n, 0);
4697 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4699 return map_lex_gte_first(dim, n, 1);
4702 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4704 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4707 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4709 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4712 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4713 __isl_take isl_set *set2)
4715 isl_map *map;
4716 map = isl_map_lex_le(isl_set_get_space(set1));
4717 map = isl_map_intersect_domain(map, set1);
4718 map = isl_map_intersect_range(map, set2);
4719 return map;
4722 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4723 __isl_take isl_set *set2)
4725 isl_map *map;
4726 map = isl_map_lex_lt(isl_set_get_space(set1));
4727 map = isl_map_intersect_domain(map, set1);
4728 map = isl_map_intersect_range(map, set2);
4729 return map;
4732 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4733 __isl_take isl_set *set2)
4735 isl_map *map;
4736 map = isl_map_lex_ge(isl_set_get_space(set1));
4737 map = isl_map_intersect_domain(map, set1);
4738 map = isl_map_intersect_range(map, set2);
4739 return map;
4742 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4743 __isl_take isl_set *set2)
4745 isl_map *map;
4746 map = isl_map_lex_gt(isl_set_get_space(set1));
4747 map = isl_map_intersect_domain(map, set1);
4748 map = isl_map_intersect_range(map, set2);
4749 return map;
4752 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4753 __isl_take isl_map *map2)
4755 isl_map *map;
4756 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4757 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4758 map = isl_map_apply_range(map, isl_map_reverse(map2));
4759 return map;
4762 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4763 __isl_take isl_map *map2)
4765 isl_map *map;
4766 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4767 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4768 map = isl_map_apply_range(map, isl_map_reverse(map2));
4769 return map;
4772 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4773 __isl_take isl_map *map2)
4775 isl_map *map;
4776 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4777 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4778 map = isl_map_apply_range(map, isl_map_reverse(map2));
4779 return map;
4782 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4783 __isl_take isl_map *map2)
4785 isl_map *map;
4786 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4787 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4788 map = isl_map_apply_range(map, isl_map_reverse(map2));
4789 return map;
4792 static __isl_give isl_basic_map *basic_map_from_basic_set(
4793 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4795 struct isl_basic_map *bmap;
4797 bset = isl_basic_set_cow(bset);
4798 if (!bset || !dim)
4799 goto error;
4801 isl_assert(bset->ctx, isl_space_compatible_internal(bset->dim, dim),
4802 goto error);
4803 isl_space_free(bset->dim);
4804 bmap = bset_to_bmap(bset);
4805 bmap->dim = dim;
4806 return isl_basic_map_finalize(bmap);
4807 error:
4808 isl_basic_set_free(bset);
4809 isl_space_free(dim);
4810 return NULL;
4813 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4814 __isl_take isl_basic_set *bset, __isl_take isl_space *space)
4816 return basic_map_from_basic_set(bset, space);
4819 /* For a div d = floor(f/m), add the constraint
4821 * f - m d >= 0
4823 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4824 unsigned pos, isl_int *div)
4826 int i;
4827 unsigned total = isl_basic_map_total_dim(bmap);
4829 i = isl_basic_map_alloc_inequality(bmap);
4830 if (i < 0)
4831 return -1;
4832 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4833 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4835 return 0;
4838 /* For a div d = floor(f/m), add the constraint
4840 * -(f-(m-1)) + m d >= 0
4842 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4843 unsigned pos, isl_int *div)
4845 int i;
4846 unsigned total = isl_basic_map_total_dim(bmap);
4848 i = isl_basic_map_alloc_inequality(bmap);
4849 if (i < 0)
4850 return -1;
4851 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4852 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4853 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4854 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4856 return 0;
4859 /* For a div d = floor(f/m), add the constraints
4861 * f - m d >= 0
4862 * -(f-(m-1)) + m d >= 0
4864 * Note that the second constraint is the negation of
4866 * f - m d >= m
4868 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4869 unsigned pos, isl_int *div)
4871 if (add_upper_div_constraint(bmap, pos, div) < 0)
4872 return -1;
4873 if (add_lower_div_constraint(bmap, pos, div) < 0)
4874 return -1;
4875 return 0;
4878 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4879 unsigned pos, isl_int *div)
4881 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4882 pos, div);
4885 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4887 unsigned total = isl_basic_map_total_dim(bmap);
4888 unsigned div_pos = total - bmap->n_div + div;
4890 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4891 bmap->div[div]);
4894 /* For each known div d = floor(f/m), add the constraints
4896 * f - m d >= 0
4897 * -(f-(m-1)) + m d >= 0
4899 * Remove duplicate constraints in case of some these div constraints
4900 * already appear in "bmap".
4902 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4903 __isl_take isl_basic_map *bmap)
4905 unsigned n_div;
4907 if (!bmap)
4908 return NULL;
4909 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4910 if (n_div == 0)
4911 return bmap;
4913 bmap = add_known_div_constraints(bmap);
4914 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4915 bmap = isl_basic_map_finalize(bmap);
4916 return bmap;
4919 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4921 * In particular, if this div is of the form d = floor(f/m),
4922 * then add the constraint
4924 * f - m d >= 0
4926 * if sign < 0 or the constraint
4928 * -(f-(m-1)) + m d >= 0
4930 * if sign > 0.
4932 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4933 unsigned div, int sign)
4935 unsigned total;
4936 unsigned div_pos;
4938 if (!bmap)
4939 return -1;
4941 total = isl_basic_map_total_dim(bmap);
4942 div_pos = total - bmap->n_div + div;
4944 if (sign < 0)
4945 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4946 else
4947 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4950 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4952 return isl_basic_map_add_div_constraints(bset, div);
4955 struct isl_basic_set *isl_basic_map_underlying_set(
4956 struct isl_basic_map *bmap)
4958 if (!bmap)
4959 goto error;
4960 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4961 bmap->n_div == 0 &&
4962 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4963 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4964 return bset_from_bmap(bmap);
4965 bmap = isl_basic_map_cow(bmap);
4966 if (!bmap)
4967 goto error;
4968 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4969 if (!bmap->dim)
4970 goto error;
4971 bmap->extra -= bmap->n_div;
4972 bmap->n_div = 0;
4973 bmap = isl_basic_map_finalize(bmap);
4974 return bset_from_bmap(bmap);
4975 error:
4976 isl_basic_map_free(bmap);
4977 return NULL;
4980 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4981 __isl_take isl_basic_set *bset)
4983 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4986 /* Replace each element in "list" by the result of applying
4987 * isl_basic_map_underlying_set to the element.
4989 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4990 __isl_take isl_basic_map_list *list)
4992 int i, n;
4994 if (!list)
4995 return NULL;
4997 n = isl_basic_map_list_n_basic_map(list);
4998 for (i = 0; i < n; ++i) {
4999 isl_basic_map *bmap;
5000 isl_basic_set *bset;
5002 bmap = isl_basic_map_list_get_basic_map(list, i);
5003 bset = isl_basic_set_underlying_set(bmap);
5004 list = isl_basic_set_list_set_basic_set(list, i, bset);
5007 return list;
5010 struct isl_basic_map *isl_basic_map_overlying_set(
5011 struct isl_basic_set *bset, struct isl_basic_map *like)
5013 struct isl_basic_map *bmap;
5014 struct isl_ctx *ctx;
5015 unsigned total;
5016 int i;
5018 if (!bset || !like)
5019 goto error;
5020 ctx = bset->ctx;
5021 isl_assert(ctx, bset->n_div == 0, goto error);
5022 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5023 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5024 goto error);
5025 if (like->n_div == 0) {
5026 isl_space *space = isl_basic_map_get_space(like);
5027 isl_basic_map_free(like);
5028 return isl_basic_map_reset_space(bset, space);
5030 bset = isl_basic_set_cow(bset);
5031 if (!bset)
5032 goto error;
5033 total = bset->dim->n_out + bset->extra;
5034 bmap = bset_to_bmap(bset);
5035 isl_space_free(bmap->dim);
5036 bmap->dim = isl_space_copy(like->dim);
5037 if (!bmap->dim)
5038 goto error;
5039 bmap->n_div = like->n_div;
5040 bmap->extra += like->n_div;
5041 if (bmap->extra) {
5042 unsigned ltotal;
5043 isl_int **div;
5044 ltotal = total - bmap->extra + like->extra;
5045 if (ltotal > total)
5046 ltotal = total;
5047 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5048 bmap->extra * (1 + 1 + total));
5049 if (isl_blk_is_error(bmap->block2))
5050 goto error;
5051 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5052 if (!div)
5053 goto error;
5054 bmap->div = div;
5055 for (i = 0; i < bmap->extra; ++i)
5056 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5057 for (i = 0; i < like->n_div; ++i) {
5058 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5059 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5061 bmap = isl_basic_map_add_known_div_constraints(bmap);
5063 isl_basic_map_free(like);
5064 bmap = isl_basic_map_simplify(bmap);
5065 bmap = isl_basic_map_finalize(bmap);
5066 return bmap;
5067 error:
5068 isl_basic_map_free(like);
5069 isl_basic_set_free(bset);
5070 return NULL;
5073 struct isl_basic_set *isl_basic_set_from_underlying_set(
5074 struct isl_basic_set *bset, struct isl_basic_set *like)
5076 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5077 bset_to_bmap(like)));
5080 struct isl_set *isl_set_from_underlying_set(
5081 struct isl_set *set, struct isl_basic_set *like)
5083 int i;
5085 if (!set || !like)
5086 goto error;
5087 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
5088 goto error);
5089 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
5090 isl_basic_set_free(like);
5091 return set;
5093 set = isl_set_cow(set);
5094 if (!set)
5095 goto error;
5096 for (i = 0; i < set->n; ++i) {
5097 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
5098 isl_basic_set_copy(like));
5099 if (!set->p[i])
5100 goto error;
5102 isl_space_free(set->dim);
5103 set->dim = isl_space_copy(like->dim);
5104 if (!set->dim)
5105 goto error;
5106 isl_basic_set_free(like);
5107 return set;
5108 error:
5109 isl_basic_set_free(like);
5110 isl_set_free(set);
5111 return NULL;
5114 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5116 int i;
5118 map = isl_map_cow(map);
5119 if (!map)
5120 return NULL;
5121 map->dim = isl_space_cow(map->dim);
5122 if (!map->dim)
5123 goto error;
5125 for (i = 1; i < map->n; ++i)
5126 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5127 goto error);
5128 for (i = 0; i < map->n; ++i) {
5129 map->p[i] = bset_to_bmap(
5130 isl_basic_map_underlying_set(map->p[i]));
5131 if (!map->p[i])
5132 goto error;
5134 if (map->n == 0)
5135 map->dim = isl_space_underlying(map->dim, 0);
5136 else {
5137 isl_space_free(map->dim);
5138 map->dim = isl_space_copy(map->p[0]->dim);
5140 if (!map->dim)
5141 goto error;
5142 return set_from_map(map);
5143 error:
5144 isl_map_free(map);
5145 return NULL;
5148 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
5150 return set_from_map(isl_map_underlying_set(set_to_map(set)));
5153 /* Replace the space of "bmap" by "space".
5155 * If the space of "bmap" is identical to "space" (including the identifiers
5156 * of the input and output dimensions), then simply return the original input.
5158 __isl_give isl_basic_map *isl_basic_map_reset_space(
5159 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5161 isl_bool equal;
5163 if (!bmap)
5164 goto error;
5165 equal = isl_space_is_equal(bmap->dim, space);
5166 if (equal >= 0 && equal)
5167 equal = isl_space_match(bmap->dim, isl_dim_in,
5168 space, isl_dim_in);
5169 if (equal >= 0 && equal)
5170 equal = isl_space_match(bmap->dim, isl_dim_out,
5171 space, isl_dim_out);
5172 if (equal < 0)
5173 goto error;
5174 if (equal) {
5175 isl_space_free(space);
5176 return bmap;
5178 bmap = isl_basic_map_cow(bmap);
5179 if (!bmap || !space)
5180 goto error;
5182 isl_space_free(bmap->dim);
5183 bmap->dim = space;
5185 bmap = isl_basic_map_finalize(bmap);
5187 return bmap;
5188 error:
5189 isl_basic_map_free(bmap);
5190 isl_space_free(space);
5191 return NULL;
5194 __isl_give isl_basic_set *isl_basic_set_reset_space(
5195 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5197 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5198 dim));
5201 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5202 __isl_take isl_space *dim)
5204 int i;
5206 map = isl_map_cow(map);
5207 if (!map || !dim)
5208 goto error;
5210 for (i = 0; i < map->n; ++i) {
5211 map->p[i] = isl_basic_map_reset_space(map->p[i],
5212 isl_space_copy(dim));
5213 if (!map->p[i])
5214 goto error;
5216 isl_space_free(map->dim);
5217 map->dim = dim;
5219 return map;
5220 error:
5221 isl_map_free(map);
5222 isl_space_free(dim);
5223 return NULL;
5226 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5227 __isl_take isl_space *dim)
5229 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5232 /* Compute the parameter domain of the given basic set.
5234 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5236 isl_bool is_params;
5237 isl_space *space;
5238 unsigned n;
5240 is_params = isl_basic_set_is_params(bset);
5241 if (is_params < 0)
5242 return isl_basic_set_free(bset);
5243 if (is_params)
5244 return bset;
5246 n = isl_basic_set_dim(bset, isl_dim_set);
5247 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5248 space = isl_basic_set_get_space(bset);
5249 space = isl_space_params(space);
5250 bset = isl_basic_set_reset_space(bset, space);
5251 return bset;
5254 /* Construct a zero-dimensional basic set with the given parameter domain.
5256 __isl_give isl_basic_set *isl_basic_set_from_params(
5257 __isl_take isl_basic_set *bset)
5259 isl_space *space;
5260 space = isl_basic_set_get_space(bset);
5261 space = isl_space_set_from_params(space);
5262 bset = isl_basic_set_reset_space(bset, space);
5263 return bset;
5266 /* Compute the parameter domain of the given set.
5268 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5270 isl_space *space;
5271 unsigned n;
5273 if (isl_set_is_params(set))
5274 return set;
5276 n = isl_set_dim(set, isl_dim_set);
5277 set = isl_set_project_out(set, isl_dim_set, 0, n);
5278 space = isl_set_get_space(set);
5279 space = isl_space_params(space);
5280 set = isl_set_reset_space(set, space);
5281 return set;
5284 /* Construct a zero-dimensional set with the given parameter domain.
5286 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5288 isl_space *space;
5289 space = isl_set_get_space(set);
5290 space = isl_space_set_from_params(space);
5291 set = isl_set_reset_space(set, space);
5292 return set;
5295 /* Compute the parameter domain of the given map.
5297 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5299 isl_space *space;
5300 unsigned n;
5302 n = isl_map_dim(map, isl_dim_in);
5303 map = isl_map_project_out(map, isl_dim_in, 0, n);
5304 n = isl_map_dim(map, isl_dim_out);
5305 map = isl_map_project_out(map, isl_dim_out, 0, n);
5306 space = isl_map_get_space(map);
5307 space = isl_space_params(space);
5308 map = isl_map_reset_space(map, space);
5309 return map;
5312 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5314 isl_space *space;
5315 unsigned n_out;
5317 if (!bmap)
5318 return NULL;
5319 space = isl_space_domain(isl_basic_map_get_space(bmap));
5321 n_out = isl_basic_map_n_out(bmap);
5322 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5324 return isl_basic_map_reset_space(bmap, space);
5327 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5329 if (!bmap)
5330 return isl_bool_error;
5331 return isl_space_may_be_set(bmap->dim);
5334 /* Is this basic map actually a set?
5335 * Users should never call this function. Outside of isl,
5336 * the type should indicate whether something is a set or a map.
5338 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5340 if (!bmap)
5341 return isl_bool_error;
5342 return isl_space_is_set(bmap->dim);
5345 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5347 isl_bool is_set;
5349 is_set = isl_basic_map_is_set(bmap);
5350 if (is_set < 0)
5351 goto error;
5352 if (is_set)
5353 return bmap;
5354 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5355 error:
5356 isl_basic_map_free(bmap);
5357 return NULL;
5360 __isl_give isl_basic_map *isl_basic_map_domain_map(
5361 __isl_take isl_basic_map *bmap)
5363 int i;
5364 isl_space *dim;
5365 isl_basic_map *domain;
5366 int nparam, n_in, n_out;
5368 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5369 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5370 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5372 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5373 domain = isl_basic_map_universe(dim);
5375 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5376 bmap = isl_basic_map_apply_range(bmap, domain);
5377 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5379 for (i = 0; i < n_in; ++i)
5380 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5381 isl_dim_out, i);
5383 bmap = isl_basic_map_gauss(bmap, NULL);
5384 return isl_basic_map_finalize(bmap);
5387 __isl_give isl_basic_map *isl_basic_map_range_map(
5388 __isl_take isl_basic_map *bmap)
5390 int i;
5391 isl_space *dim;
5392 isl_basic_map *range;
5393 int nparam, n_in, n_out;
5395 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5396 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5397 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5399 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5400 range = isl_basic_map_universe(dim);
5402 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5403 bmap = isl_basic_map_apply_range(bmap, range);
5404 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5406 for (i = 0; i < n_out; ++i)
5407 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5408 isl_dim_out, i);
5410 bmap = isl_basic_map_gauss(bmap, NULL);
5411 return isl_basic_map_finalize(bmap);
5414 int isl_map_may_be_set(__isl_keep isl_map *map)
5416 if (!map)
5417 return -1;
5418 return isl_space_may_be_set(map->dim);
5421 /* Is this map actually a set?
5422 * Users should never call this function. Outside of isl,
5423 * the type should indicate whether something is a set or a map.
5425 int isl_map_is_set(__isl_keep isl_map *map)
5427 if (!map)
5428 return -1;
5429 return isl_space_is_set(map->dim);
5432 struct isl_set *isl_map_range(struct isl_map *map)
5434 int i;
5435 struct isl_set *set;
5437 if (!map)
5438 goto error;
5439 if (isl_map_is_set(map))
5440 return set_from_map(map);
5442 map = isl_map_cow(map);
5443 if (!map)
5444 goto error;
5446 set = set_from_map(map);
5447 set->dim = isl_space_range(set->dim);
5448 if (!set->dim)
5449 goto error;
5450 for (i = 0; i < map->n; ++i) {
5451 set->p[i] = isl_basic_map_range(map->p[i]);
5452 if (!set->p[i])
5453 goto error;
5455 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5456 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5457 return set;
5458 error:
5459 isl_map_free(map);
5460 return NULL;
5463 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5465 int i;
5467 map = isl_map_cow(map);
5468 if (!map)
5469 return NULL;
5471 map->dim = isl_space_domain_map(map->dim);
5472 if (!map->dim)
5473 goto error;
5474 for (i = 0; i < map->n; ++i) {
5475 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5476 if (!map->p[i])
5477 goto error;
5479 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5480 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5481 return map;
5482 error:
5483 isl_map_free(map);
5484 return NULL;
5487 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5489 int i;
5490 isl_space *range_dim;
5492 map = isl_map_cow(map);
5493 if (!map)
5494 return NULL;
5496 range_dim = isl_space_range(isl_map_get_space(map));
5497 range_dim = isl_space_from_range(range_dim);
5498 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5499 map->dim = isl_space_join(map->dim, range_dim);
5500 if (!map->dim)
5501 goto error;
5502 for (i = 0; i < map->n; ++i) {
5503 map->p[i] = isl_basic_map_range_map(map->p[i]);
5504 if (!map->p[i])
5505 goto error;
5507 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5508 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5509 return map;
5510 error:
5511 isl_map_free(map);
5512 return NULL;
5515 /* Given a wrapped map of the form A[B -> C],
5516 * return the map A[B -> C] -> B.
5518 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5520 isl_id *id;
5521 isl_map *map;
5523 if (!set)
5524 return NULL;
5525 if (!isl_set_has_tuple_id(set))
5526 return isl_map_domain_map(isl_set_unwrap(set));
5528 id = isl_set_get_tuple_id(set);
5529 map = isl_map_domain_map(isl_set_unwrap(set));
5530 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5532 return map;
5535 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5536 __isl_take isl_space *dim)
5538 int i;
5539 struct isl_map *map = NULL;
5541 set = isl_set_cow(set);
5542 if (!set || !dim)
5543 goto error;
5544 isl_assert(set->ctx, isl_space_compatible_internal(set->dim, dim),
5545 goto error);
5546 map = set_to_map(set);
5547 for (i = 0; i < set->n; ++i) {
5548 map->p[i] = basic_map_from_basic_set(
5549 set->p[i], isl_space_copy(dim));
5550 if (!map->p[i])
5551 goto error;
5553 isl_space_free(map->dim);
5554 map->dim = dim;
5555 return map;
5556 error:
5557 isl_space_free(dim);
5558 isl_set_free(set);
5559 return NULL;
5562 __isl_give isl_basic_map *isl_basic_map_from_domain(
5563 __isl_take isl_basic_set *bset)
5565 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5568 __isl_give isl_basic_map *isl_basic_map_from_range(
5569 __isl_take isl_basic_set *bset)
5571 isl_space *space;
5572 space = isl_basic_set_get_space(bset);
5573 space = isl_space_from_range(space);
5574 bset = isl_basic_set_reset_space(bset, space);
5575 return bset_to_bmap(bset);
5578 /* Create a relation with the given set as range.
5579 * The domain of the created relation is a zero-dimensional
5580 * flat anonymous space.
5582 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5584 isl_space *space;
5585 space = isl_set_get_space(set);
5586 space = isl_space_from_range(space);
5587 set = isl_set_reset_space(set, space);
5588 return set_to_map(set);
5591 /* Create a relation with the given set as domain.
5592 * The range of the created relation is a zero-dimensional
5593 * flat anonymous space.
5595 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5597 return isl_map_reverse(isl_map_from_range(set));
5600 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5601 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5603 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5606 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5607 __isl_take isl_set *range)
5609 return isl_map_apply_range(isl_map_reverse(domain), range);
5612 /* Return a newly allocated isl_map with given space and flags and
5613 * room for "n" basic maps.
5614 * Make sure that all cached information is cleared.
5616 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5617 unsigned flags)
5619 struct isl_map *map;
5621 if (!space)
5622 return NULL;
5623 if (n < 0)
5624 isl_die(space->ctx, isl_error_internal,
5625 "negative number of basic maps", goto error);
5626 map = isl_calloc(space->ctx, struct isl_map,
5627 sizeof(struct isl_map) +
5628 (n - 1) * sizeof(struct isl_basic_map *));
5629 if (!map)
5630 goto error;
5632 map->ctx = space->ctx;
5633 isl_ctx_ref(map->ctx);
5634 map->ref = 1;
5635 map->size = n;
5636 map->n = 0;
5637 map->dim = space;
5638 map->flags = flags;
5639 return map;
5640 error:
5641 isl_space_free(space);
5642 return NULL;
5645 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5646 unsigned nparam, unsigned in, unsigned out, int n,
5647 unsigned flags)
5649 struct isl_map *map;
5650 isl_space *dims;
5652 dims = isl_space_alloc(ctx, nparam, in, out);
5653 if (!dims)
5654 return NULL;
5656 map = isl_map_alloc_space(dims, n, flags);
5657 return map;
5660 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5662 struct isl_basic_map *bmap;
5663 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5664 bmap = isl_basic_map_set_to_empty(bmap);
5665 return bmap;
5668 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5670 struct isl_basic_set *bset;
5671 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5672 bset = isl_basic_set_set_to_empty(bset);
5673 return bset;
5676 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5678 struct isl_basic_map *bmap;
5679 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5680 bmap = isl_basic_map_finalize(bmap);
5681 return bmap;
5684 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5686 struct isl_basic_set *bset;
5687 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5688 bset = isl_basic_set_finalize(bset);
5689 return bset;
5692 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5694 int i;
5695 unsigned total = isl_space_dim(dim, isl_dim_all);
5696 isl_basic_map *bmap;
5698 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5699 for (i = 0; i < total; ++i) {
5700 int k = isl_basic_map_alloc_inequality(bmap);
5701 if (k < 0)
5702 goto error;
5703 isl_seq_clr(bmap->ineq[k], 1 + total);
5704 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5706 return bmap;
5707 error:
5708 isl_basic_map_free(bmap);
5709 return NULL;
5712 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5714 return isl_basic_map_nat_universe(dim);
5717 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5719 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5722 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5724 return isl_map_nat_universe(dim);
5727 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5729 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5732 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5734 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5737 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5739 struct isl_map *map;
5740 if (!dim)
5741 return NULL;
5742 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5743 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5744 return map;
5747 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5749 struct isl_set *set;
5750 if (!dim)
5751 return NULL;
5752 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5753 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5754 return set;
5757 struct isl_map *isl_map_dup(struct isl_map *map)
5759 int i;
5760 struct isl_map *dup;
5762 if (!map)
5763 return NULL;
5764 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5765 for (i = 0; i < map->n; ++i)
5766 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5767 return dup;
5770 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5771 __isl_take isl_basic_map *bmap)
5773 if (!bmap || !map)
5774 goto error;
5775 if (isl_basic_map_plain_is_empty(bmap)) {
5776 isl_basic_map_free(bmap);
5777 return map;
5779 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5780 isl_assert(map->ctx, map->n < map->size, goto error);
5781 map->p[map->n] = bmap;
5782 map->n++;
5783 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5784 return map;
5785 error:
5786 if (map)
5787 isl_map_free(map);
5788 if (bmap)
5789 isl_basic_map_free(bmap);
5790 return NULL;
5793 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5795 int i;
5797 if (!map)
5798 return NULL;
5800 if (--map->ref > 0)
5801 return NULL;
5803 clear_caches(map);
5804 isl_ctx_deref(map->ctx);
5805 for (i = 0; i < map->n; ++i)
5806 isl_basic_map_free(map->p[i]);
5807 isl_space_free(map->dim);
5808 free(map);
5810 return NULL;
5813 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5814 struct isl_basic_map *bmap, unsigned pos, int value)
5816 int j;
5818 bmap = isl_basic_map_cow(bmap);
5819 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5820 j = isl_basic_map_alloc_equality(bmap);
5821 if (j < 0)
5822 goto error;
5823 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5824 isl_int_set_si(bmap->eq[j][pos], -1);
5825 isl_int_set_si(bmap->eq[j][0], value);
5826 bmap = isl_basic_map_simplify(bmap);
5827 return isl_basic_map_finalize(bmap);
5828 error:
5829 isl_basic_map_free(bmap);
5830 return NULL;
5833 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5834 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5836 int j;
5838 bmap = isl_basic_map_cow(bmap);
5839 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5840 j = isl_basic_map_alloc_equality(bmap);
5841 if (j < 0)
5842 goto error;
5843 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5844 isl_int_set_si(bmap->eq[j][pos], -1);
5845 isl_int_set(bmap->eq[j][0], value);
5846 bmap = isl_basic_map_simplify(bmap);
5847 return isl_basic_map_finalize(bmap);
5848 error:
5849 isl_basic_map_free(bmap);
5850 return NULL;
5853 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5854 enum isl_dim_type type, unsigned pos, int value)
5856 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5857 return isl_basic_map_free(bmap);
5858 return isl_basic_map_fix_pos_si(bmap,
5859 isl_basic_map_offset(bmap, type) + pos, value);
5862 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5863 enum isl_dim_type type, unsigned pos, isl_int value)
5865 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5866 return isl_basic_map_free(bmap);
5867 return isl_basic_map_fix_pos(bmap,
5868 isl_basic_map_offset(bmap, type) + pos, value);
5871 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5872 * to be equal to "v".
5874 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5875 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5877 if (!bmap || !v)
5878 goto error;
5879 if (!isl_val_is_int(v))
5880 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5881 "expecting integer value", goto error);
5882 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5883 goto error;
5884 pos += isl_basic_map_offset(bmap, type);
5885 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5886 isl_val_free(v);
5887 return bmap;
5888 error:
5889 isl_basic_map_free(bmap);
5890 isl_val_free(v);
5891 return NULL;
5894 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5895 * to be equal to "v".
5897 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5898 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5900 return isl_basic_map_fix_val(bset, type, pos, v);
5903 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5904 enum isl_dim_type type, unsigned pos, int value)
5906 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5907 type, pos, value));
5910 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5911 enum isl_dim_type type, unsigned pos, isl_int value)
5913 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5914 type, pos, value));
5917 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5918 unsigned input, int value)
5920 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5923 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5924 unsigned dim, int value)
5926 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5927 isl_dim_set, dim, value));
5930 static int remove_if_empty(__isl_keep isl_map *map, int i)
5932 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5934 if (empty < 0)
5935 return -1;
5936 if (!empty)
5937 return 0;
5939 isl_basic_map_free(map->p[i]);
5940 if (i != map->n - 1) {
5941 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5942 map->p[i] = map->p[map->n - 1];
5944 map->n--;
5946 return 0;
5949 /* Perform "fn" on each basic map of "map", where we may not be holding
5950 * the only reference to "map".
5951 * In particular, "fn" should be a semantics preserving operation
5952 * that we want to apply to all copies of "map". We therefore need
5953 * to be careful not to modify "map" in a way that breaks "map"
5954 * in case anything goes wrong.
5956 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5957 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5959 struct isl_basic_map *bmap;
5960 int i;
5962 if (!map)
5963 return NULL;
5965 for (i = map->n - 1; i >= 0; --i) {
5966 bmap = isl_basic_map_copy(map->p[i]);
5967 bmap = fn(bmap);
5968 if (!bmap)
5969 goto error;
5970 isl_basic_map_free(map->p[i]);
5971 map->p[i] = bmap;
5972 if (remove_if_empty(map, i) < 0)
5973 goto error;
5976 return map;
5977 error:
5978 isl_map_free(map);
5979 return NULL;
5982 struct isl_map *isl_map_fix_si(struct isl_map *map,
5983 enum isl_dim_type type, unsigned pos, int value)
5985 int i;
5987 map = isl_map_cow(map);
5988 if (!map)
5989 return NULL;
5991 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5992 for (i = map->n - 1; i >= 0; --i) {
5993 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5994 if (remove_if_empty(map, i) < 0)
5995 goto error;
5997 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5998 return map;
5999 error:
6000 isl_map_free(map);
6001 return NULL;
6004 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6005 enum isl_dim_type type, unsigned pos, int value)
6007 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6010 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6011 enum isl_dim_type type, unsigned pos, isl_int value)
6013 int i;
6015 map = isl_map_cow(map);
6016 if (!map)
6017 return NULL;
6019 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6020 for (i = 0; i < map->n; ++i) {
6021 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6022 if (!map->p[i])
6023 goto error;
6025 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6026 return map;
6027 error:
6028 isl_map_free(map);
6029 return NULL;
6032 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6033 enum isl_dim_type type, unsigned pos, isl_int value)
6035 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6038 /* Fix the value of the variable at position "pos" of type "type" of "map"
6039 * to be equal to "v".
6041 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6042 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6044 int i;
6046 map = isl_map_cow(map);
6047 if (!map || !v)
6048 goto error;
6050 if (!isl_val_is_int(v))
6051 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6052 "expecting integer value", goto error);
6053 if (pos >= isl_map_dim(map, type))
6054 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6055 "index out of bounds", goto error);
6056 for (i = map->n - 1; i >= 0; --i) {
6057 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6058 isl_val_copy(v));
6059 if (remove_if_empty(map, i) < 0)
6060 goto error;
6062 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6063 isl_val_free(v);
6064 return map;
6065 error:
6066 isl_map_free(map);
6067 isl_val_free(v);
6068 return NULL;
6071 /* Fix the value of the variable at position "pos" of type "type" of "set"
6072 * to be equal to "v".
6074 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6075 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6077 return isl_map_fix_val(set, type, pos, v);
6080 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6081 unsigned input, int value)
6083 return isl_map_fix_si(map, isl_dim_in, input, value);
6086 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6088 return set_from_map(isl_map_fix_si(set_to_map(set),
6089 isl_dim_set, dim, value));
6092 static __isl_give isl_basic_map *basic_map_bound_si(
6093 __isl_take isl_basic_map *bmap,
6094 enum isl_dim_type type, unsigned pos, int value, int upper)
6096 int j;
6098 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6099 return isl_basic_map_free(bmap);
6100 pos += isl_basic_map_offset(bmap, type);
6101 bmap = isl_basic_map_cow(bmap);
6102 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6103 j = isl_basic_map_alloc_inequality(bmap);
6104 if (j < 0)
6105 goto error;
6106 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6107 if (upper) {
6108 isl_int_set_si(bmap->ineq[j][pos], -1);
6109 isl_int_set_si(bmap->ineq[j][0], value);
6110 } else {
6111 isl_int_set_si(bmap->ineq[j][pos], 1);
6112 isl_int_set_si(bmap->ineq[j][0], -value);
6114 bmap = isl_basic_map_simplify(bmap);
6115 return isl_basic_map_finalize(bmap);
6116 error:
6117 isl_basic_map_free(bmap);
6118 return NULL;
6121 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6122 __isl_take isl_basic_map *bmap,
6123 enum isl_dim_type type, unsigned pos, int value)
6125 return basic_map_bound_si(bmap, type, pos, value, 0);
6128 /* Constrain the values of the given dimension to be no greater than "value".
6130 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6131 __isl_take isl_basic_map *bmap,
6132 enum isl_dim_type type, unsigned pos, int value)
6134 return basic_map_bound_si(bmap, type, pos, value, 1);
6137 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
6138 unsigned dim, isl_int value)
6140 int j;
6142 bset = isl_basic_set_cow(bset);
6143 bset = isl_basic_set_extend_constraints(bset, 0, 1);
6144 j = isl_basic_set_alloc_inequality(bset);
6145 if (j < 0)
6146 goto error;
6147 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
6148 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
6149 isl_int_neg(bset->ineq[j][0], value);
6150 bset = isl_basic_set_simplify(bset);
6151 return isl_basic_set_finalize(bset);
6152 error:
6153 isl_basic_set_free(bset);
6154 return NULL;
6157 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6158 enum isl_dim_type type, unsigned pos, int value, int upper)
6160 int i;
6162 map = isl_map_cow(map);
6163 if (!map)
6164 return NULL;
6166 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6167 for (i = 0; i < map->n; ++i) {
6168 map->p[i] = basic_map_bound_si(map->p[i],
6169 type, pos, value, upper);
6170 if (!map->p[i])
6171 goto error;
6173 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6174 return map;
6175 error:
6176 isl_map_free(map);
6177 return NULL;
6180 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6181 enum isl_dim_type type, unsigned pos, int value)
6183 return map_bound_si(map, type, pos, value, 0);
6186 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6187 enum isl_dim_type type, unsigned pos, int value)
6189 return map_bound_si(map, type, pos, value, 1);
6192 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6193 enum isl_dim_type type, unsigned pos, int value)
6195 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6196 type, pos, value));
6199 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6200 enum isl_dim_type type, unsigned pos, int value)
6202 return isl_map_upper_bound_si(set, type, pos, value);
6205 /* Bound the given variable of "bmap" from below (or above is "upper"
6206 * is set) to "value".
6208 static __isl_give isl_basic_map *basic_map_bound(
6209 __isl_take isl_basic_map *bmap,
6210 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6212 int j;
6214 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6215 return isl_basic_map_free(bmap);
6216 pos += isl_basic_map_offset(bmap, type);
6217 bmap = isl_basic_map_cow(bmap);
6218 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6219 j = isl_basic_map_alloc_inequality(bmap);
6220 if (j < 0)
6221 goto error;
6222 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6223 if (upper) {
6224 isl_int_set_si(bmap->ineq[j][pos], -1);
6225 isl_int_set(bmap->ineq[j][0], value);
6226 } else {
6227 isl_int_set_si(bmap->ineq[j][pos], 1);
6228 isl_int_neg(bmap->ineq[j][0], value);
6230 bmap = isl_basic_map_simplify(bmap);
6231 return isl_basic_map_finalize(bmap);
6232 error:
6233 isl_basic_map_free(bmap);
6234 return NULL;
6237 /* Bound the given variable of "map" from below (or above is "upper"
6238 * is set) to "value".
6240 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6241 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6243 int i;
6245 map = isl_map_cow(map);
6246 if (!map)
6247 return NULL;
6249 if (pos >= isl_map_dim(map, type))
6250 isl_die(map->ctx, isl_error_invalid,
6251 "index out of bounds", goto error);
6252 for (i = map->n - 1; i >= 0; --i) {
6253 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6254 if (remove_if_empty(map, i) < 0)
6255 goto error;
6257 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6258 return map;
6259 error:
6260 isl_map_free(map);
6261 return NULL;
6264 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6265 enum isl_dim_type type, unsigned pos, isl_int value)
6267 return map_bound(map, type, pos, value, 0);
6270 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6271 enum isl_dim_type type, unsigned pos, isl_int value)
6273 return map_bound(map, type, pos, value, 1);
6276 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6277 enum isl_dim_type type, unsigned pos, isl_int value)
6279 return isl_map_lower_bound(set, type, pos, value);
6282 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6283 enum isl_dim_type type, unsigned pos, isl_int value)
6285 return isl_map_upper_bound(set, type, pos, value);
6288 /* Force the values of the variable at position "pos" of type "type" of "set"
6289 * to be no smaller than "value".
6291 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6292 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6294 if (!value)
6295 goto error;
6296 if (!isl_val_is_int(value))
6297 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6298 "expecting integer value", goto error);
6299 set = isl_set_lower_bound(set, type, pos, value->n);
6300 isl_val_free(value);
6301 return set;
6302 error:
6303 isl_val_free(value);
6304 isl_set_free(set);
6305 return NULL;
6308 /* Force the values of the variable at position "pos" of type "type" of "set"
6309 * to be no greater than "value".
6311 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6312 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6314 if (!value)
6315 goto error;
6316 if (!isl_val_is_int(value))
6317 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6318 "expecting integer value", goto error);
6319 set = isl_set_upper_bound(set, type, pos, value->n);
6320 isl_val_free(value);
6321 return set;
6322 error:
6323 isl_val_free(value);
6324 isl_set_free(set);
6325 return NULL;
6328 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6329 isl_int value)
6331 int i;
6333 set = isl_set_cow(set);
6334 if (!set)
6335 return NULL;
6337 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6338 for (i = 0; i < set->n; ++i) {
6339 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6340 if (!set->p[i])
6341 goto error;
6343 return set;
6344 error:
6345 isl_set_free(set);
6346 return NULL;
6349 struct isl_map *isl_map_reverse(struct isl_map *map)
6351 int i;
6353 map = isl_map_cow(map);
6354 if (!map)
6355 return NULL;
6357 map->dim = isl_space_reverse(map->dim);
6358 if (!map->dim)
6359 goto error;
6360 for (i = 0; i < map->n; ++i) {
6361 map->p[i] = isl_basic_map_reverse(map->p[i]);
6362 if (!map->p[i])
6363 goto error;
6365 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6366 return map;
6367 error:
6368 isl_map_free(map);
6369 return NULL;
6372 #undef TYPE
6373 #define TYPE isl_pw_multi_aff
6374 #undef SUFFIX
6375 #define SUFFIX _pw_multi_aff
6376 #undef EMPTY
6377 #define EMPTY isl_pw_multi_aff_empty
6378 #undef ADD
6379 #define ADD isl_pw_multi_aff_union_add
6380 #include "isl_map_lexopt_templ.c"
6382 /* Given a map "map", compute the lexicographically minimal
6383 * (or maximal) image element for each domain element in dom,
6384 * in the form of an isl_pw_multi_aff.
6385 * If "empty" is not NULL, then set *empty to those elements in dom that
6386 * do not have an image element.
6387 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6388 * should be computed over the domain of "map". "empty" is also NULL
6389 * in this case.
6391 * We first compute the lexicographically minimal or maximal element
6392 * in the first basic map. This results in a partial solution "res"
6393 * and a subset "todo" of dom that still need to be handled.
6394 * We then consider each of the remaining maps in "map" and successively
6395 * update both "res" and "todo".
6396 * If "empty" is NULL, then the todo sets are not needed and therefore
6397 * also not computed.
6399 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6400 __isl_take isl_map *map, __isl_take isl_set *dom,
6401 __isl_give isl_set **empty, unsigned flags)
6403 int i;
6404 int full;
6405 isl_pw_multi_aff *res;
6406 isl_set *todo;
6408 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6409 if (!map || (!full && !dom))
6410 goto error;
6412 if (isl_map_plain_is_empty(map)) {
6413 if (empty)
6414 *empty = dom;
6415 else
6416 isl_set_free(dom);
6417 return isl_pw_multi_aff_from_map(map);
6420 res = basic_map_partial_lexopt_pw_multi_aff(
6421 isl_basic_map_copy(map->p[0]),
6422 isl_set_copy(dom), empty, flags);
6424 if (empty)
6425 todo = *empty;
6426 for (i = 1; i < map->n; ++i) {
6427 isl_pw_multi_aff *res_i;
6429 res_i = basic_map_partial_lexopt_pw_multi_aff(
6430 isl_basic_map_copy(map->p[i]),
6431 isl_set_copy(dom), empty, flags);
6433 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6434 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6435 else
6436 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6438 if (empty)
6439 todo = isl_set_intersect(todo, *empty);
6442 isl_set_free(dom);
6443 isl_map_free(map);
6445 if (empty)
6446 *empty = todo;
6448 return res;
6449 error:
6450 if (empty)
6451 *empty = NULL;
6452 isl_set_free(dom);
6453 isl_map_free(map);
6454 return NULL;
6457 #undef TYPE
6458 #define TYPE isl_map
6459 #undef SUFFIX
6460 #define SUFFIX
6461 #undef EMPTY
6462 #define EMPTY isl_map_empty
6463 #undef ADD
6464 #define ADD isl_map_union_disjoint
6465 #include "isl_map_lexopt_templ.c"
6467 /* Given a map "map", compute the lexicographically minimal
6468 * (or maximal) image element for each domain element in "dom",
6469 * in the form of an isl_map.
6470 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6471 * do not have an image element.
6472 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6473 * should be computed over the domain of "map". "empty" is also NULL
6474 * in this case.
6476 * If the input consists of more than one disjunct, then first
6477 * compute the desired result in the form of an isl_pw_multi_aff and
6478 * then convert that into an isl_map.
6480 * This function used to have an explicit implementation in terms
6481 * of isl_maps, but it would continually intersect the domains of
6482 * partial results with the complement of the domain of the next
6483 * partial solution, potentially leading to an explosion in the number
6484 * of disjuncts if there are several disjuncts in the input.
6485 * An even earlier implementation of this function would look for
6486 * better results in the domain of the partial result and for extra
6487 * results in the complement of this domain, which would lead to
6488 * even more splintering.
6490 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6491 __isl_take isl_map *map, __isl_take isl_set *dom,
6492 __isl_give isl_set **empty, unsigned flags)
6494 int full;
6495 struct isl_map *res;
6496 isl_pw_multi_aff *pma;
6498 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6499 if (!map || (!full && !dom))
6500 goto error;
6502 if (isl_map_plain_is_empty(map)) {
6503 if (empty)
6504 *empty = dom;
6505 else
6506 isl_set_free(dom);
6507 return map;
6510 if (map->n == 1) {
6511 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6512 dom, empty, flags);
6513 isl_map_free(map);
6514 return res;
6517 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6518 flags);
6519 return isl_map_from_pw_multi_aff(pma);
6520 error:
6521 if (empty)
6522 *empty = NULL;
6523 isl_set_free(dom);
6524 isl_map_free(map);
6525 return NULL;
6528 __isl_give isl_map *isl_map_partial_lexmax(
6529 __isl_take isl_map *map, __isl_take isl_set *dom,
6530 __isl_give isl_set **empty)
6532 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6535 __isl_give isl_map *isl_map_partial_lexmin(
6536 __isl_take isl_map *map, __isl_take isl_set *dom,
6537 __isl_give isl_set **empty)
6539 return isl_map_partial_lexopt(map, dom, empty, 0);
6542 __isl_give isl_set *isl_set_partial_lexmin(
6543 __isl_take isl_set *set, __isl_take isl_set *dom,
6544 __isl_give isl_set **empty)
6546 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6547 dom, empty));
6550 __isl_give isl_set *isl_set_partial_lexmax(
6551 __isl_take isl_set *set, __isl_take isl_set *dom,
6552 __isl_give isl_set **empty)
6554 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6555 dom, empty));
6558 /* Compute the lexicographic minimum (or maximum if "flags" includes
6559 * ISL_OPT_MAX) of "bset" over its parametric domain.
6561 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6562 unsigned flags)
6564 return isl_basic_map_lexopt(bset, flags);
6567 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6569 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6572 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6574 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6577 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6579 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6582 /* Compute the lexicographic minimum of "bset" over its parametric domain
6583 * for the purpose of quantifier elimination.
6584 * That is, find an explicit representation for all the existentially
6585 * quantified variables in "bset" by computing their lexicographic
6586 * minimum.
6588 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6589 __isl_take isl_basic_set *bset)
6591 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6594 /* Extract the first and only affine expression from list
6595 * and then add it to *pwaff with the given dom.
6596 * This domain is known to be disjoint from other domains
6597 * because of the way isl_basic_map_foreach_lexmax works.
6599 static int update_dim_opt(__isl_take isl_basic_set *dom,
6600 __isl_take isl_aff_list *list, void *user)
6602 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6603 isl_aff *aff;
6604 isl_pw_aff **pwaff = user;
6605 isl_pw_aff *pwaff_i;
6607 if (!list)
6608 goto error;
6609 if (isl_aff_list_n_aff(list) != 1)
6610 isl_die(ctx, isl_error_internal,
6611 "expecting single element list", goto error);
6613 aff = isl_aff_list_get_aff(list, 0);
6614 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6616 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6618 isl_aff_list_free(list);
6620 return 0;
6621 error:
6622 isl_basic_set_free(dom);
6623 isl_aff_list_free(list);
6624 return -1;
6627 /* Given a basic map with one output dimension, compute the minimum or
6628 * maximum of that dimension as an isl_pw_aff.
6630 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6631 * call update_dim_opt on each leaf of the result.
6633 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6634 int max)
6636 isl_space *dim = isl_basic_map_get_space(bmap);
6637 isl_pw_aff *pwaff;
6638 int r;
6640 dim = isl_space_from_domain(isl_space_domain(dim));
6641 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6642 pwaff = isl_pw_aff_empty(dim);
6644 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6645 if (r < 0)
6646 return isl_pw_aff_free(pwaff);
6648 return pwaff;
6651 /* Compute the minimum or maximum of the given output dimension
6652 * as a function of the parameters and the input dimensions,
6653 * but independently of the other output dimensions.
6655 * We first project out the other output dimension and then compute
6656 * the "lexicographic" maximum in each basic map, combining the results
6657 * using isl_pw_aff_union_max.
6659 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6660 int max)
6662 int i;
6663 isl_pw_aff *pwaff;
6664 unsigned n_out;
6666 n_out = isl_map_dim(map, isl_dim_out);
6667 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6668 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6669 if (!map)
6670 return NULL;
6672 if (map->n == 0) {
6673 isl_space *dim = isl_map_get_space(map);
6674 isl_map_free(map);
6675 return isl_pw_aff_empty(dim);
6678 pwaff = basic_map_dim_opt(map->p[0], max);
6679 for (i = 1; i < map->n; ++i) {
6680 isl_pw_aff *pwaff_i;
6682 pwaff_i = basic_map_dim_opt(map->p[i], max);
6683 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6686 isl_map_free(map);
6688 return pwaff;
6691 /* Compute the minimum of the given output dimension as a function of the
6692 * parameters and input dimensions, but independently of
6693 * the other output dimensions.
6695 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6697 return map_dim_opt(map, pos, 0);
6700 /* Compute the maximum of the given output dimension as a function of the
6701 * parameters and input dimensions, but independently of
6702 * the other output dimensions.
6704 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6706 return map_dim_opt(map, pos, 1);
6709 /* Compute the minimum or maximum of the given set dimension
6710 * as a function of the parameters,
6711 * but independently of the other set dimensions.
6713 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6714 int max)
6716 return map_dim_opt(set, pos, max);
6719 /* Compute the maximum of the given set dimension as a function of the
6720 * parameters, but independently of the other set dimensions.
6722 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6724 return set_dim_opt(set, pos, 1);
6727 /* Compute the minimum of the given set dimension as a function of the
6728 * parameters, but independently of the other set dimensions.
6730 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6732 return set_dim_opt(set, pos, 0);
6735 /* Apply a preimage specified by "mat" on the parameters of "bset".
6736 * bset is assumed to have only parameters and divs.
6738 static struct isl_basic_set *basic_set_parameter_preimage(
6739 struct isl_basic_set *bset, struct isl_mat *mat)
6741 unsigned nparam;
6743 if (!bset || !mat)
6744 goto error;
6746 bset->dim = isl_space_cow(bset->dim);
6747 if (!bset->dim)
6748 goto error;
6750 nparam = isl_basic_set_dim(bset, isl_dim_param);
6752 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6754 bset->dim->nparam = 0;
6755 bset->dim->n_out = nparam;
6756 bset = isl_basic_set_preimage(bset, mat);
6757 if (bset) {
6758 bset->dim->nparam = bset->dim->n_out;
6759 bset->dim->n_out = 0;
6761 return bset;
6762 error:
6763 isl_mat_free(mat);
6764 isl_basic_set_free(bset);
6765 return NULL;
6768 /* Apply a preimage specified by "mat" on the parameters of "set".
6769 * set is assumed to have only parameters and divs.
6771 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6772 __isl_take isl_mat *mat)
6774 isl_space *space;
6775 unsigned nparam;
6777 if (!set || !mat)
6778 goto error;
6780 nparam = isl_set_dim(set, isl_dim_param);
6782 if (mat->n_row != 1 + nparam)
6783 isl_die(isl_set_get_ctx(set), isl_error_internal,
6784 "unexpected number of rows", goto error);
6786 space = isl_set_get_space(set);
6787 space = isl_space_move_dims(space, isl_dim_set, 0,
6788 isl_dim_param, 0, nparam);
6789 set = isl_set_reset_space(set, space);
6790 set = isl_set_preimage(set, mat);
6791 nparam = isl_set_dim(set, isl_dim_out);
6792 space = isl_set_get_space(set);
6793 space = isl_space_move_dims(space, isl_dim_param, 0,
6794 isl_dim_out, 0, nparam);
6795 set = isl_set_reset_space(set, space);
6796 return set;
6797 error:
6798 isl_mat_free(mat);
6799 isl_set_free(set);
6800 return NULL;
6803 /* Intersect the basic set "bset" with the affine space specified by the
6804 * equalities in "eq".
6806 static struct isl_basic_set *basic_set_append_equalities(
6807 struct isl_basic_set *bset, struct isl_mat *eq)
6809 int i, k;
6810 unsigned len;
6812 if (!bset || !eq)
6813 goto error;
6815 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6816 eq->n_row, 0);
6817 if (!bset)
6818 goto error;
6820 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6821 for (i = 0; i < eq->n_row; ++i) {
6822 k = isl_basic_set_alloc_equality(bset);
6823 if (k < 0)
6824 goto error;
6825 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6826 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6828 isl_mat_free(eq);
6830 bset = isl_basic_set_gauss(bset, NULL);
6831 bset = isl_basic_set_finalize(bset);
6833 return bset;
6834 error:
6835 isl_mat_free(eq);
6836 isl_basic_set_free(bset);
6837 return NULL;
6840 /* Intersect the set "set" with the affine space specified by the
6841 * equalities in "eq".
6843 static struct isl_set *set_append_equalities(struct isl_set *set,
6844 struct isl_mat *eq)
6846 int i;
6848 if (!set || !eq)
6849 goto error;
6851 for (i = 0; i < set->n; ++i) {
6852 set->p[i] = basic_set_append_equalities(set->p[i],
6853 isl_mat_copy(eq));
6854 if (!set->p[i])
6855 goto error;
6857 isl_mat_free(eq);
6858 return set;
6859 error:
6860 isl_mat_free(eq);
6861 isl_set_free(set);
6862 return NULL;
6865 /* Given a basic set "bset" that only involves parameters and existentially
6866 * quantified variables, return the index of the first equality
6867 * that only involves parameters. If there is no such equality then
6868 * return bset->n_eq.
6870 * This function assumes that isl_basic_set_gauss has been called on "bset".
6872 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6874 int i, j;
6875 unsigned nparam, n_div;
6877 if (!bset)
6878 return -1;
6880 nparam = isl_basic_set_dim(bset, isl_dim_param);
6881 n_div = isl_basic_set_dim(bset, isl_dim_div);
6883 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6884 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6885 ++i;
6888 return i;
6891 /* Compute an explicit representation for the existentially quantified
6892 * variables in "bset" by computing the "minimal value" of the set
6893 * variables. Since there are no set variables, the computation of
6894 * the minimal value essentially computes an explicit representation
6895 * of the non-empty part(s) of "bset".
6897 * The input only involves parameters and existentially quantified variables.
6898 * All equalities among parameters have been removed.
6900 * Since the existentially quantified variables in the result are in general
6901 * going to be different from those in the input, we first replace
6902 * them by the minimal number of variables based on their equalities.
6903 * This should simplify the parametric integer programming.
6905 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6907 isl_morph *morph1, *morph2;
6908 isl_set *set;
6909 unsigned n;
6911 if (!bset)
6912 return NULL;
6913 if (bset->n_eq == 0)
6914 return isl_basic_set_lexmin_compute_divs(bset);
6916 morph1 = isl_basic_set_parameter_compression(bset);
6917 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6918 bset = isl_basic_set_lift(bset);
6919 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6920 bset = isl_morph_basic_set(morph2, bset);
6921 n = isl_basic_set_dim(bset, isl_dim_set);
6922 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6924 set = isl_basic_set_lexmin_compute_divs(bset);
6926 set = isl_morph_set(isl_morph_inverse(morph1), set);
6928 return set;
6931 /* Project the given basic set onto its parameter domain, possibly introducing
6932 * new, explicit, existential variables in the constraints.
6933 * The input has parameters and (possibly implicit) existential variables.
6934 * The output has the same parameters, but only
6935 * explicit existentially quantified variables.
6937 * The actual projection is performed by pip, but pip doesn't seem
6938 * to like equalities very much, so we first remove the equalities
6939 * among the parameters by performing a variable compression on
6940 * the parameters. Afterward, an inverse transformation is performed
6941 * and the equalities among the parameters are inserted back in.
6943 * The variable compression on the parameters may uncover additional
6944 * equalities that were only implicit before. We therefore check
6945 * if there are any new parameter equalities in the result and
6946 * if so recurse. The removal of parameter equalities is required
6947 * for the parameter compression performed by base_compute_divs.
6949 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6951 int i;
6952 struct isl_mat *eq;
6953 struct isl_mat *T, *T2;
6954 struct isl_set *set;
6955 unsigned nparam;
6957 bset = isl_basic_set_cow(bset);
6958 if (!bset)
6959 return NULL;
6961 if (bset->n_eq == 0)
6962 return base_compute_divs(bset);
6964 bset = isl_basic_set_gauss(bset, NULL);
6965 if (!bset)
6966 return NULL;
6967 if (isl_basic_set_plain_is_empty(bset))
6968 return isl_set_from_basic_set(bset);
6970 i = first_parameter_equality(bset);
6971 if (i == bset->n_eq)
6972 return base_compute_divs(bset);
6974 nparam = isl_basic_set_dim(bset, isl_dim_param);
6975 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6976 0, 1 + nparam);
6977 eq = isl_mat_cow(eq);
6978 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6979 if (T && T->n_col == 0) {
6980 isl_mat_free(T);
6981 isl_mat_free(T2);
6982 isl_mat_free(eq);
6983 bset = isl_basic_set_set_to_empty(bset);
6984 return isl_set_from_basic_set(bset);
6986 bset = basic_set_parameter_preimage(bset, T);
6988 i = first_parameter_equality(bset);
6989 if (!bset)
6990 set = NULL;
6991 else if (i == bset->n_eq)
6992 set = base_compute_divs(bset);
6993 else
6994 set = parameter_compute_divs(bset);
6995 set = set_parameter_preimage(set, T2);
6996 set = set_append_equalities(set, eq);
6997 return set;
7000 /* Insert the divs from "ls" before those of "bmap".
7002 * The number of columns is not changed, which means that the last
7003 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7004 * The caller is responsible for removing the same number of dimensions
7005 * from the space of "bmap".
7007 static __isl_give isl_basic_map *insert_divs_from_local_space(
7008 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7010 int i;
7011 int n_div;
7012 int old_n_div;
7014 n_div = isl_local_space_dim(ls, isl_dim_div);
7015 if (n_div == 0)
7016 return bmap;
7018 old_n_div = bmap->n_div;
7019 bmap = insert_div_rows(bmap, n_div);
7020 if (!bmap)
7021 return NULL;
7023 for (i = 0; i < n_div; ++i) {
7024 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7025 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7028 return bmap;
7031 /* Replace the space of "bmap" by the space and divs of "ls".
7033 * If "ls" has any divs, then we simplify the result since we may
7034 * have discovered some additional equalities that could simplify
7035 * the div expressions.
7037 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7038 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7040 int n_div;
7042 bmap = isl_basic_map_cow(bmap);
7043 if (!bmap || !ls)
7044 goto error;
7046 n_div = isl_local_space_dim(ls, isl_dim_div);
7047 bmap = insert_divs_from_local_space(bmap, ls);
7048 if (!bmap)
7049 goto error;
7051 isl_space_free(bmap->dim);
7052 bmap->dim = isl_local_space_get_space(ls);
7053 if (!bmap->dim)
7054 goto error;
7056 isl_local_space_free(ls);
7057 if (n_div > 0)
7058 bmap = isl_basic_map_simplify(bmap);
7059 bmap = isl_basic_map_finalize(bmap);
7060 return bmap;
7061 error:
7062 isl_basic_map_free(bmap);
7063 isl_local_space_free(ls);
7064 return NULL;
7067 /* Replace the space of "map" by the space and divs of "ls".
7069 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7070 __isl_take isl_local_space *ls)
7072 int i;
7074 map = isl_map_cow(map);
7075 if (!map || !ls)
7076 goto error;
7078 for (i = 0; i < map->n; ++i) {
7079 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7080 isl_local_space_copy(ls));
7081 if (!map->p[i])
7082 goto error;
7084 isl_space_free(map->dim);
7085 map->dim = isl_local_space_get_space(ls);
7086 if (!map->dim)
7087 goto error;
7089 isl_local_space_free(ls);
7090 return map;
7091 error:
7092 isl_local_space_free(ls);
7093 isl_map_free(map);
7094 return NULL;
7097 /* Compute an explicit representation for the existentially
7098 * quantified variables for which do not know any explicit representation yet.
7100 * We first sort the existentially quantified variables so that the
7101 * existentially quantified variables for which we already have an explicit
7102 * representation are placed before those for which we do not.
7103 * The input dimensions, the output dimensions and the existentially
7104 * quantified variables for which we already have an explicit
7105 * representation are then turned into parameters.
7106 * compute_divs returns a map with the same parameters and
7107 * no input or output dimensions and the dimension specification
7108 * is reset to that of the input, including the existentially quantified
7109 * variables for which we already had an explicit representation.
7111 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
7113 struct isl_basic_set *bset;
7114 struct isl_set *set;
7115 struct isl_map *map;
7116 isl_space *dim;
7117 isl_local_space *ls;
7118 unsigned nparam;
7119 unsigned n_in;
7120 unsigned n_out;
7121 int n_known;
7122 int i;
7124 bmap = isl_basic_map_sort_divs(bmap);
7125 bmap = isl_basic_map_cow(bmap);
7126 if (!bmap)
7127 return NULL;
7129 n_known = isl_basic_map_first_unknown_div(bmap);
7130 if (n_known < 0)
7131 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7133 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7134 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7135 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7136 dim = isl_space_set_alloc(bmap->ctx,
7137 nparam + n_in + n_out + n_known, 0);
7138 if (!dim)
7139 goto error;
7141 ls = isl_basic_map_get_local_space(bmap);
7142 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7143 n_known, bmap->n_div - n_known);
7144 if (n_known > 0) {
7145 for (i = n_known; i < bmap->n_div; ++i)
7146 swap_div(bmap, i - n_known, i);
7147 bmap->n_div -= n_known;
7148 bmap->extra -= n_known;
7150 bmap = isl_basic_map_reset_space(bmap, dim);
7151 bset = bset_from_bmap(bmap);
7153 set = parameter_compute_divs(bset);
7154 map = set_to_map(set);
7155 map = replace_space_by_local_space(map, ls);
7157 return map;
7158 error:
7159 isl_basic_map_free(bmap);
7160 return NULL;
7163 /* Remove the explicit representation of local variable "div",
7164 * if there is any.
7166 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7167 __isl_take isl_basic_map *bmap, int div)
7169 isl_bool unknown;
7171 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7172 if (unknown < 0)
7173 return isl_basic_map_free(bmap);
7174 if (unknown)
7175 return bmap;
7177 bmap = isl_basic_map_cow(bmap);
7178 if (!bmap)
7179 return NULL;
7180 isl_int_set_si(bmap->div[div][0], 0);
7181 return bmap;
7184 /* Is local variable "div" of "bmap" marked as not having an explicit
7185 * representation?
7186 * Note that even if "div" is not marked in this way and therefore
7187 * has an explicit representation, this representation may still
7188 * depend (indirectly) on other local variables that do not
7189 * have an explicit representation.
7191 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7192 int div)
7194 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7195 return isl_bool_error;
7196 return isl_int_is_zero(bmap->div[div][0]);
7199 /* Return the position of the first local variable that does not
7200 * have an explicit representation.
7201 * Return the total number of local variables if they all have
7202 * an explicit representation.
7203 * Return -1 on error.
7205 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7207 int i;
7209 if (!bmap)
7210 return -1;
7212 for (i = 0; i < bmap->n_div; ++i) {
7213 if (!isl_basic_map_div_is_known(bmap, i))
7214 return i;
7216 return bmap->n_div;
7219 /* Return the position of the first local variable that does not
7220 * have an explicit representation.
7221 * Return the total number of local variables if they all have
7222 * an explicit representation.
7223 * Return -1 on error.
7225 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7227 return isl_basic_map_first_unknown_div(bset);
7230 /* Does "bmap" have an explicit representation for all local variables?
7232 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7234 int first, n;
7236 n = isl_basic_map_dim(bmap, isl_dim_div);
7237 first = isl_basic_map_first_unknown_div(bmap);
7238 if (first < 0)
7239 return isl_bool_error;
7240 return first == n;
7243 /* Do all basic maps in "map" have an explicit representation
7244 * for all local variables?
7246 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7248 int i;
7250 if (!map)
7251 return isl_bool_error;
7253 for (i = 0; i < map->n; ++i) {
7254 int known = isl_basic_map_divs_known(map->p[i]);
7255 if (known <= 0)
7256 return known;
7259 return isl_bool_true;
7262 /* If bmap contains any unknown divs, then compute explicit
7263 * expressions for them. However, this computation may be
7264 * quite expensive, so first try to remove divs that aren't
7265 * strictly needed.
7267 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7269 int known;
7270 struct isl_map *map;
7272 known = isl_basic_map_divs_known(bmap);
7273 if (known < 0)
7274 goto error;
7275 if (known)
7276 return isl_map_from_basic_map(bmap);
7278 bmap = isl_basic_map_drop_redundant_divs(bmap);
7280 known = isl_basic_map_divs_known(bmap);
7281 if (known < 0)
7282 goto error;
7283 if (known)
7284 return isl_map_from_basic_map(bmap);
7286 map = compute_divs(bmap);
7287 return map;
7288 error:
7289 isl_basic_map_free(bmap);
7290 return NULL;
7293 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7295 int i;
7296 int known;
7297 struct isl_map *res;
7299 if (!map)
7300 return NULL;
7301 if (map->n == 0)
7302 return map;
7304 known = isl_map_divs_known(map);
7305 if (known < 0) {
7306 isl_map_free(map);
7307 return NULL;
7309 if (known)
7310 return map;
7312 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7313 for (i = 1 ; i < map->n; ++i) {
7314 struct isl_map *r2;
7315 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7316 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7317 res = isl_map_union_disjoint(res, r2);
7318 else
7319 res = isl_map_union(res, r2);
7321 isl_map_free(map);
7323 return res;
7326 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7328 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7331 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7333 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7336 struct isl_set *isl_map_domain(struct isl_map *map)
7338 int i;
7339 struct isl_set *set;
7341 if (!map)
7342 goto error;
7344 map = isl_map_cow(map);
7345 if (!map)
7346 return NULL;
7348 set = set_from_map(map);
7349 set->dim = isl_space_domain(set->dim);
7350 if (!set->dim)
7351 goto error;
7352 for (i = 0; i < map->n; ++i) {
7353 set->p[i] = isl_basic_map_domain(map->p[i]);
7354 if (!set->p[i])
7355 goto error;
7357 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7358 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7359 return set;
7360 error:
7361 isl_map_free(map);
7362 return NULL;
7365 /* Return the union of "map1" and "map2", where we assume for now that
7366 * "map1" and "map2" are disjoint. Note that the basic maps inside
7367 * "map1" or "map2" may not be disjoint from each other.
7368 * Also note that this function is also called from isl_map_union,
7369 * which takes care of handling the situation where "map1" and "map2"
7370 * may not be disjoint.
7372 * If one of the inputs is empty, we can simply return the other input.
7373 * Similarly, if one of the inputs is universal, then it is equal to the union.
7375 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7376 __isl_take isl_map *map2)
7378 int i;
7379 unsigned flags = 0;
7380 struct isl_map *map = NULL;
7381 int is_universe;
7383 if (!map1 || !map2)
7384 goto error;
7386 if (!isl_space_is_equal(map1->dim, map2->dim))
7387 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7388 "spaces don't match", goto error);
7390 if (map1->n == 0) {
7391 isl_map_free(map1);
7392 return map2;
7394 if (map2->n == 0) {
7395 isl_map_free(map2);
7396 return map1;
7399 is_universe = isl_map_plain_is_universe(map1);
7400 if (is_universe < 0)
7401 goto error;
7402 if (is_universe) {
7403 isl_map_free(map2);
7404 return map1;
7407 is_universe = isl_map_plain_is_universe(map2);
7408 if (is_universe < 0)
7409 goto error;
7410 if (is_universe) {
7411 isl_map_free(map1);
7412 return map2;
7415 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7416 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7417 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7419 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7420 map1->n + map2->n, flags);
7421 if (!map)
7422 goto error;
7423 for (i = 0; i < map1->n; ++i) {
7424 map = isl_map_add_basic_map(map,
7425 isl_basic_map_copy(map1->p[i]));
7426 if (!map)
7427 goto error;
7429 for (i = 0; i < map2->n; ++i) {
7430 map = isl_map_add_basic_map(map,
7431 isl_basic_map_copy(map2->p[i]));
7432 if (!map)
7433 goto error;
7435 isl_map_free(map1);
7436 isl_map_free(map2);
7437 return map;
7438 error:
7439 isl_map_free(map);
7440 isl_map_free(map1);
7441 isl_map_free(map2);
7442 return NULL;
7445 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7446 * guaranteed to be disjoint by the caller.
7448 * Note that this functions is called from within isl_map_make_disjoint,
7449 * so we have to be careful not to touch the constraints of the inputs
7450 * in any way.
7452 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7453 __isl_take isl_map *map2)
7455 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7458 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7459 * not be disjoint. The parameters are assumed to have been aligned.
7461 * We currently simply call map_union_disjoint, the internal operation
7462 * of which does not really depend on the inputs being disjoint.
7463 * If the result contains more than one basic map, then we clear
7464 * the disjoint flag since the result may contain basic maps from
7465 * both inputs and these are not guaranteed to be disjoint.
7467 * As a special case, if "map1" and "map2" are obviously equal,
7468 * then we simply return "map1".
7470 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7471 __isl_take isl_map *map2)
7473 int equal;
7475 if (!map1 || !map2)
7476 goto error;
7478 equal = isl_map_plain_is_equal(map1, map2);
7479 if (equal < 0)
7480 goto error;
7481 if (equal) {
7482 isl_map_free(map2);
7483 return map1;
7486 map1 = map_union_disjoint(map1, map2);
7487 if (!map1)
7488 return NULL;
7489 if (map1->n > 1)
7490 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7491 return map1;
7492 error:
7493 isl_map_free(map1);
7494 isl_map_free(map2);
7495 return NULL;
7498 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7499 * not be disjoint.
7501 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7502 __isl_take isl_map *map2)
7504 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7507 struct isl_set *isl_set_union_disjoint(
7508 struct isl_set *set1, struct isl_set *set2)
7510 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7511 set_to_map(set2)));
7514 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7516 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7519 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7520 * the results.
7522 * "map" and "set" are assumed to be compatible and non-NULL.
7524 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7525 __isl_take isl_set *set,
7526 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7527 __isl_take isl_basic_set *bset))
7529 unsigned flags = 0;
7530 struct isl_map *result;
7531 int i, j;
7533 if (isl_set_plain_is_universe(set)) {
7534 isl_set_free(set);
7535 return map;
7538 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7539 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7540 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7542 result = isl_map_alloc_space(isl_space_copy(map->dim),
7543 map->n * set->n, flags);
7544 for (i = 0; result && i < map->n; ++i)
7545 for (j = 0; j < set->n; ++j) {
7546 result = isl_map_add_basic_map(result,
7547 fn(isl_basic_map_copy(map->p[i]),
7548 isl_basic_set_copy(set->p[j])));
7549 if (!result)
7550 break;
7553 isl_map_free(map);
7554 isl_set_free(set);
7555 return result;
7558 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7559 __isl_take isl_set *set)
7561 if (!map || !set)
7562 goto error;
7564 if (!isl_map_compatible_range(map, set))
7565 isl_die(set->ctx, isl_error_invalid,
7566 "incompatible spaces", goto error);
7568 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7569 error:
7570 isl_map_free(map);
7571 isl_set_free(set);
7572 return NULL;
7575 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7576 __isl_take isl_set *set)
7578 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7581 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7582 __isl_take isl_set *set)
7584 if (!map || !set)
7585 goto error;
7587 if (!isl_map_compatible_domain(map, set))
7588 isl_die(set->ctx, isl_error_invalid,
7589 "incompatible spaces", goto error);
7591 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7592 error:
7593 isl_map_free(map);
7594 isl_set_free(set);
7595 return NULL;
7598 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7599 __isl_take isl_set *set)
7601 return isl_map_align_params_map_map_and(map, set,
7602 &map_intersect_domain);
7605 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7606 __isl_take isl_map *map2)
7608 if (!map1 || !map2)
7609 goto error;
7610 map1 = isl_map_reverse(map1);
7611 map1 = isl_map_apply_range(map1, map2);
7612 return isl_map_reverse(map1);
7613 error:
7614 isl_map_free(map1);
7615 isl_map_free(map2);
7616 return NULL;
7619 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7620 __isl_take isl_map *map2)
7622 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7625 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7626 __isl_take isl_map *map2)
7628 isl_space *dim_result;
7629 struct isl_map *result;
7630 int i, j;
7632 if (!map1 || !map2)
7633 goto error;
7635 dim_result = isl_space_join(isl_space_copy(map1->dim),
7636 isl_space_copy(map2->dim));
7638 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7639 if (!result)
7640 goto error;
7641 for (i = 0; i < map1->n; ++i)
7642 for (j = 0; j < map2->n; ++j) {
7643 result = isl_map_add_basic_map(result,
7644 isl_basic_map_apply_range(
7645 isl_basic_map_copy(map1->p[i]),
7646 isl_basic_map_copy(map2->p[j])));
7647 if (!result)
7648 goto error;
7650 isl_map_free(map1);
7651 isl_map_free(map2);
7652 if (result && result->n <= 1)
7653 ISL_F_SET(result, ISL_MAP_DISJOINT);
7654 return result;
7655 error:
7656 isl_map_free(map1);
7657 isl_map_free(map2);
7658 return NULL;
7661 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7662 __isl_take isl_map *map2)
7664 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7668 * returns range - domain
7670 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7672 isl_space *target_space;
7673 struct isl_basic_set *bset;
7674 unsigned dim;
7675 unsigned nparam;
7676 int i;
7678 if (!bmap)
7679 goto error;
7680 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7681 bmap->dim, isl_dim_out),
7682 goto error);
7683 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7684 dim = isl_basic_map_n_in(bmap);
7685 nparam = isl_basic_map_n_param(bmap);
7686 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7687 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7688 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7689 for (i = 0; i < dim; ++i) {
7690 int j = isl_basic_map_alloc_equality(bmap);
7691 if (j < 0) {
7692 bmap = isl_basic_map_free(bmap);
7693 break;
7695 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7696 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7697 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7698 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7700 bset = isl_basic_map_domain(bmap);
7701 bset = isl_basic_set_reset_space(bset, target_space);
7702 return bset;
7703 error:
7704 isl_basic_map_free(bmap);
7705 return NULL;
7709 * returns range - domain
7711 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7713 int i;
7714 isl_space *dim;
7715 struct isl_set *result;
7717 if (!map)
7718 return NULL;
7720 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7721 map->dim, isl_dim_out),
7722 goto error);
7723 dim = isl_map_get_space(map);
7724 dim = isl_space_domain(dim);
7725 result = isl_set_alloc_space(dim, map->n, 0);
7726 if (!result)
7727 goto error;
7728 for (i = 0; i < map->n; ++i)
7729 result = isl_set_add_basic_set(result,
7730 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7731 isl_map_free(map);
7732 return result;
7733 error:
7734 isl_map_free(map);
7735 return NULL;
7739 * returns [domain -> range] -> range - domain
7741 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7742 __isl_take isl_basic_map *bmap)
7744 int i, k;
7745 isl_space *dim;
7746 isl_basic_map *domain;
7747 int nparam, n;
7748 unsigned total;
7750 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7751 bmap->dim, isl_dim_out))
7752 isl_die(bmap->ctx, isl_error_invalid,
7753 "domain and range don't match", goto error);
7755 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7756 n = isl_basic_map_dim(bmap, isl_dim_in);
7758 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7759 domain = isl_basic_map_universe(dim);
7761 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7762 bmap = isl_basic_map_apply_range(bmap, domain);
7763 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7765 total = isl_basic_map_total_dim(bmap);
7767 for (i = 0; i < n; ++i) {
7768 k = isl_basic_map_alloc_equality(bmap);
7769 if (k < 0)
7770 goto error;
7771 isl_seq_clr(bmap->eq[k], 1 + total);
7772 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7773 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7774 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7777 bmap = isl_basic_map_gauss(bmap, NULL);
7778 return isl_basic_map_finalize(bmap);
7779 error:
7780 isl_basic_map_free(bmap);
7781 return NULL;
7785 * returns [domain -> range] -> range - domain
7787 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7789 int i;
7790 isl_space *domain_dim;
7792 if (!map)
7793 return NULL;
7795 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7796 map->dim, isl_dim_out))
7797 isl_die(map->ctx, isl_error_invalid,
7798 "domain and range don't match", goto error);
7800 map = isl_map_cow(map);
7801 if (!map)
7802 return NULL;
7804 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7805 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7806 map->dim = isl_space_join(map->dim, domain_dim);
7807 if (!map->dim)
7808 goto error;
7809 for (i = 0; i < map->n; ++i) {
7810 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7811 if (!map->p[i])
7812 goto error;
7814 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7815 return map;
7816 error:
7817 isl_map_free(map);
7818 return NULL;
7821 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7823 struct isl_basic_map *bmap;
7824 unsigned nparam;
7825 unsigned dim;
7826 int i;
7828 if (!dims)
7829 return NULL;
7831 nparam = dims->nparam;
7832 dim = dims->n_out;
7833 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7834 if (!bmap)
7835 goto error;
7837 for (i = 0; i < dim; ++i) {
7838 int j = isl_basic_map_alloc_equality(bmap);
7839 if (j < 0)
7840 goto error;
7841 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7842 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7843 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7845 return isl_basic_map_finalize(bmap);
7846 error:
7847 isl_basic_map_free(bmap);
7848 return NULL;
7851 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7853 if (!dim)
7854 return NULL;
7855 if (dim->n_in != dim->n_out)
7856 isl_die(dim->ctx, isl_error_invalid,
7857 "number of input and output dimensions needs to be "
7858 "the same", goto error);
7859 return basic_map_identity(dim);
7860 error:
7861 isl_space_free(dim);
7862 return NULL;
7865 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7867 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7870 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7872 isl_space *dim = isl_set_get_space(set);
7873 isl_map *id;
7874 id = isl_map_identity(isl_space_map_from_set(dim));
7875 return isl_map_intersect_range(id, set);
7878 /* Construct a basic set with all set dimensions having only non-negative
7879 * values.
7881 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7882 __isl_take isl_space *space)
7884 int i;
7885 unsigned nparam;
7886 unsigned dim;
7887 struct isl_basic_set *bset;
7889 if (!space)
7890 return NULL;
7891 nparam = space->nparam;
7892 dim = space->n_out;
7893 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7894 if (!bset)
7895 return NULL;
7896 for (i = 0; i < dim; ++i) {
7897 int k = isl_basic_set_alloc_inequality(bset);
7898 if (k < 0)
7899 goto error;
7900 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7901 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7903 return bset;
7904 error:
7905 isl_basic_set_free(bset);
7906 return NULL;
7909 /* Construct the half-space x_pos >= 0.
7911 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7912 int pos)
7914 int k;
7915 isl_basic_set *nonneg;
7917 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7918 k = isl_basic_set_alloc_inequality(nonneg);
7919 if (k < 0)
7920 goto error;
7921 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7922 isl_int_set_si(nonneg->ineq[k][pos], 1);
7924 return isl_basic_set_finalize(nonneg);
7925 error:
7926 isl_basic_set_free(nonneg);
7927 return NULL;
7930 /* Construct the half-space x_pos <= -1.
7932 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7934 int k;
7935 isl_basic_set *neg;
7937 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7938 k = isl_basic_set_alloc_inequality(neg);
7939 if (k < 0)
7940 goto error;
7941 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7942 isl_int_set_si(neg->ineq[k][0], -1);
7943 isl_int_set_si(neg->ineq[k][pos], -1);
7945 return isl_basic_set_finalize(neg);
7946 error:
7947 isl_basic_set_free(neg);
7948 return NULL;
7951 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7952 enum isl_dim_type type, unsigned first, unsigned n)
7954 int i;
7955 unsigned offset;
7956 isl_basic_set *nonneg;
7957 isl_basic_set *neg;
7959 if (!set)
7960 return NULL;
7961 if (n == 0)
7962 return set;
7964 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7966 offset = pos(set->dim, type);
7967 for (i = 0; i < n; ++i) {
7968 nonneg = nonneg_halfspace(isl_set_get_space(set),
7969 offset + first + i);
7970 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7972 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7975 return set;
7976 error:
7977 isl_set_free(set);
7978 return NULL;
7981 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7982 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7983 void *user)
7985 isl_set *half;
7987 if (!set)
7988 return -1;
7989 if (isl_set_plain_is_empty(set)) {
7990 isl_set_free(set);
7991 return 0;
7993 if (first == len)
7994 return fn(set, signs, user);
7996 signs[first] = 1;
7997 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7998 1 + first));
7999 half = isl_set_intersect(half, isl_set_copy(set));
8000 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8001 goto error;
8003 signs[first] = -1;
8004 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8005 1 + first));
8006 half = isl_set_intersect(half, set);
8007 return foreach_orthant(half, signs, first + 1, len, fn, user);
8008 error:
8009 isl_set_free(set);
8010 return -1;
8013 /* Call "fn" on the intersections of "set" with each of the orthants
8014 * (except for obviously empty intersections). The orthant is identified
8015 * by the signs array, with each entry having value 1 or -1 according
8016 * to the sign of the corresponding variable.
8018 int isl_set_foreach_orthant(__isl_keep isl_set *set,
8019 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8020 void *user)
8022 unsigned nparam;
8023 unsigned nvar;
8024 int *signs;
8025 int r;
8027 if (!set)
8028 return -1;
8029 if (isl_set_plain_is_empty(set))
8030 return 0;
8032 nparam = isl_set_dim(set, isl_dim_param);
8033 nvar = isl_set_dim(set, isl_dim_set);
8035 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8037 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8038 fn, user);
8040 free(signs);
8042 return r;
8045 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8047 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8050 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8051 __isl_keep isl_basic_map *bmap2)
8053 int is_subset;
8054 struct isl_map *map1;
8055 struct isl_map *map2;
8057 if (!bmap1 || !bmap2)
8058 return isl_bool_error;
8060 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8061 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8063 is_subset = isl_map_is_subset(map1, map2);
8065 isl_map_free(map1);
8066 isl_map_free(map2);
8068 return is_subset;
8071 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8072 __isl_keep isl_basic_set *bset2)
8074 return isl_basic_map_is_subset(bset1, bset2);
8077 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8078 __isl_keep isl_basic_map *bmap2)
8080 isl_bool is_subset;
8082 if (!bmap1 || !bmap2)
8083 return isl_bool_error;
8084 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8085 if (is_subset != isl_bool_true)
8086 return is_subset;
8087 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8088 return is_subset;
8091 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8092 __isl_keep isl_basic_set *bset2)
8094 return isl_basic_map_is_equal(
8095 bset_to_bmap(bset1), bset_to_bmap(bset2));
8098 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8100 int i;
8101 int is_empty;
8103 if (!map)
8104 return isl_bool_error;
8105 for (i = 0; i < map->n; ++i) {
8106 is_empty = isl_basic_map_is_empty(map->p[i]);
8107 if (is_empty < 0)
8108 return isl_bool_error;
8109 if (!is_empty)
8110 return isl_bool_false;
8112 return isl_bool_true;
8115 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8117 return map ? map->n == 0 : isl_bool_error;
8120 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8122 return set ? set->n == 0 : isl_bool_error;
8125 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8127 return isl_map_is_empty(set_to_map(set));
8130 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8132 if (!map1 || !map2)
8133 return -1;
8135 return isl_space_is_equal(map1->dim, map2->dim);
8138 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8140 if (!set1 || !set2)
8141 return -1;
8143 return isl_space_is_equal(set1->dim, set2->dim);
8146 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8148 isl_bool is_subset;
8150 if (!map1 || !map2)
8151 return isl_bool_error;
8152 is_subset = isl_map_is_subset(map1, map2);
8153 if (is_subset != isl_bool_true)
8154 return is_subset;
8155 is_subset = isl_map_is_subset(map2, map1);
8156 return is_subset;
8159 /* Is "map1" equal to "map2"?
8161 * First check if they are obviously equal.
8162 * If not, then perform a more detailed analysis.
8164 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8166 isl_bool equal;
8168 equal = isl_map_plain_is_equal(map1, map2);
8169 if (equal < 0 || equal)
8170 return equal;
8171 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8174 isl_bool isl_basic_map_is_strict_subset(
8175 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8177 isl_bool is_subset;
8179 if (!bmap1 || !bmap2)
8180 return isl_bool_error;
8181 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8182 if (is_subset != isl_bool_true)
8183 return is_subset;
8184 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8185 if (is_subset == isl_bool_error)
8186 return is_subset;
8187 return !is_subset;
8190 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8191 __isl_keep isl_map *map2)
8193 isl_bool is_subset;
8195 if (!map1 || !map2)
8196 return isl_bool_error;
8197 is_subset = isl_map_is_subset(map1, map2);
8198 if (is_subset != isl_bool_true)
8199 return is_subset;
8200 is_subset = isl_map_is_subset(map2, map1);
8201 if (is_subset == isl_bool_error)
8202 return is_subset;
8203 return !is_subset;
8206 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8207 __isl_keep isl_set *set2)
8209 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8212 /* Is "bmap" obviously equal to the universe with the same space?
8214 * That is, does it not have any constraints?
8216 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8218 if (!bmap)
8219 return isl_bool_error;
8220 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8223 /* Is "bset" obviously equal to the universe with the same space?
8225 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8227 return isl_basic_map_plain_is_universe(bset);
8230 /* If "c" does not involve any existentially quantified variables,
8231 * then set *univ to false and abort
8233 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8235 isl_bool *univ = user;
8236 unsigned n;
8238 n = isl_constraint_dim(c, isl_dim_div);
8239 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8240 isl_constraint_free(c);
8241 if (*univ < 0 || !*univ)
8242 return isl_stat_error;
8243 return isl_stat_ok;
8246 /* Is "bmap" equal to the universe with the same space?
8248 * First check if it is obviously equal to the universe.
8249 * If not and if there are any constraints not involving
8250 * existentially quantified variables, then it is certainly
8251 * not equal to the universe.
8252 * Otherwise, check if the universe is a subset of "bmap".
8254 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8256 isl_bool univ;
8257 isl_basic_map *test;
8259 univ = isl_basic_map_plain_is_universe(bmap);
8260 if (univ < 0 || univ)
8261 return univ;
8262 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8263 return isl_bool_false;
8264 univ = isl_bool_true;
8265 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8266 univ)
8267 return isl_bool_error;
8268 if (univ < 0 || !univ)
8269 return univ;
8270 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8271 univ = isl_basic_map_is_subset(test, bmap);
8272 isl_basic_map_free(test);
8273 return univ;
8276 /* Is "bset" equal to the universe with the same space?
8278 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8280 return isl_basic_map_is_universe(bset);
8283 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8285 int i;
8287 if (!map)
8288 return isl_bool_error;
8290 for (i = 0; i < map->n; ++i) {
8291 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8292 if (r < 0 || r)
8293 return r;
8296 return isl_bool_false;
8299 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8301 return isl_map_plain_is_universe(set_to_map(set));
8304 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8306 struct isl_basic_set *bset = NULL;
8307 struct isl_vec *sample = NULL;
8308 isl_bool empty, non_empty;
8310 if (!bmap)
8311 return isl_bool_error;
8313 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8314 return isl_bool_true;
8316 if (isl_basic_map_plain_is_universe(bmap))
8317 return isl_bool_false;
8319 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8320 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8321 copy = isl_basic_map_remove_redundancies(copy);
8322 empty = isl_basic_map_plain_is_empty(copy);
8323 isl_basic_map_free(copy);
8324 return empty;
8327 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8328 if (non_empty < 0)
8329 return isl_bool_error;
8330 if (non_empty)
8331 return isl_bool_false;
8332 isl_vec_free(bmap->sample);
8333 bmap->sample = NULL;
8334 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8335 if (!bset)
8336 return isl_bool_error;
8337 sample = isl_basic_set_sample_vec(bset);
8338 if (!sample)
8339 return isl_bool_error;
8340 empty = sample->size == 0;
8341 isl_vec_free(bmap->sample);
8342 bmap->sample = sample;
8343 if (empty)
8344 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8346 return empty;
8349 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8351 if (!bmap)
8352 return isl_bool_error;
8353 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8356 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8358 if (!bset)
8359 return isl_bool_error;
8360 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8363 /* Is "bmap" known to be non-empty?
8365 * That is, is the cached sample still valid?
8367 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8369 unsigned total;
8371 if (!bmap)
8372 return isl_bool_error;
8373 if (!bmap->sample)
8374 return isl_bool_false;
8375 total = 1 + isl_basic_map_total_dim(bmap);
8376 if (bmap->sample->size != total)
8377 return isl_bool_false;
8378 return isl_basic_map_contains(bmap, bmap->sample);
8381 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8383 return isl_basic_map_is_empty(bset_to_bmap(bset));
8386 struct isl_map *isl_basic_map_union(
8387 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8389 struct isl_map *map;
8390 if (!bmap1 || !bmap2)
8391 goto error;
8393 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8395 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8396 if (!map)
8397 goto error;
8398 map = isl_map_add_basic_map(map, bmap1);
8399 map = isl_map_add_basic_map(map, bmap2);
8400 return map;
8401 error:
8402 isl_basic_map_free(bmap1);
8403 isl_basic_map_free(bmap2);
8404 return NULL;
8407 struct isl_set *isl_basic_set_union(
8408 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8410 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8411 bset_to_bmap(bset2)));
8414 /* Order divs such that any div only depends on previous divs */
8415 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8417 int i;
8418 unsigned off;
8420 if (!bmap)
8421 return NULL;
8423 off = isl_space_dim(bmap->dim, isl_dim_all);
8425 for (i = 0; i < bmap->n_div; ++i) {
8426 int pos;
8427 if (isl_int_is_zero(bmap->div[i][0]))
8428 continue;
8429 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8430 bmap->n_div-i);
8431 if (pos == -1)
8432 continue;
8433 if (pos == 0)
8434 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8435 "integer division depends on itself",
8436 return isl_basic_map_free(bmap));
8437 isl_basic_map_swap_div(bmap, i, i + pos);
8438 --i;
8440 return bmap;
8443 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8445 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8448 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8450 int i;
8452 if (!map)
8453 return 0;
8455 for (i = 0; i < map->n; ++i) {
8456 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8457 if (!map->p[i])
8458 goto error;
8461 return map;
8462 error:
8463 isl_map_free(map);
8464 return NULL;
8467 /* Sort the local variables of "bset".
8469 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8470 __isl_take isl_basic_set *bset)
8472 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8475 /* Apply the expansion computed by isl_merge_divs.
8476 * The expansion itself is given by "exp" while the resulting
8477 * list of divs is given by "div".
8479 * Move the integer divisions of "bmap" into the right position
8480 * according to "exp" and then introduce the additional integer
8481 * divisions, adding div constraints.
8482 * The moving should be done first to avoid moving coefficients
8483 * in the definitions of the extra integer divisions.
8485 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8486 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8488 int i, j;
8489 int n_div;
8491 bmap = isl_basic_map_cow(bmap);
8492 if (!bmap || !div)
8493 goto error;
8495 if (div->n_row < bmap->n_div)
8496 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8497 "not an expansion", goto error);
8499 n_div = bmap->n_div;
8500 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8501 div->n_row - n_div, 0,
8502 2 * (div->n_row - n_div));
8504 for (i = n_div; i < div->n_row; ++i)
8505 if (isl_basic_map_alloc_div(bmap) < 0)
8506 goto error;
8508 for (j = n_div - 1; j >= 0; --j) {
8509 if (exp[j] == j)
8510 break;
8511 isl_basic_map_swap_div(bmap, j, exp[j]);
8513 j = 0;
8514 for (i = 0; i < div->n_row; ++i) {
8515 if (j < n_div && exp[j] == i) {
8516 j++;
8517 } else {
8518 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8519 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8520 continue;
8521 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8522 goto error;
8526 isl_mat_free(div);
8527 return bmap;
8528 error:
8529 isl_basic_map_free(bmap);
8530 isl_mat_free(div);
8531 return NULL;
8534 /* Apply the expansion computed by isl_merge_divs.
8535 * The expansion itself is given by "exp" while the resulting
8536 * list of divs is given by "div".
8538 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8539 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8541 return isl_basic_map_expand_divs(bset, div, exp);
8544 /* Look for a div in dst that corresponds to the div "div" in src.
8545 * The divs before "div" in src and dst are assumed to be the same.
8547 * Returns -1 if no corresponding div was found and the position
8548 * of the corresponding div in dst otherwise.
8550 static int find_div(struct isl_basic_map *dst,
8551 struct isl_basic_map *src, unsigned div)
8553 int i;
8555 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8557 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8558 for (i = div; i < dst->n_div; ++i)
8559 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8560 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8561 dst->n_div - div) == -1)
8562 return i;
8563 return -1;
8566 /* Align the divs of "dst" to those of "src", adding divs from "src"
8567 * if needed. That is, make sure that the first src->n_div divs
8568 * of the result are equal to those of src.
8570 * The result is not finalized as by design it will have redundant
8571 * divs if any divs from "src" were copied.
8573 __isl_give isl_basic_map *isl_basic_map_align_divs(
8574 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8576 int i;
8577 int known, extended;
8578 unsigned total;
8580 if (!dst || !src)
8581 return isl_basic_map_free(dst);
8583 if (src->n_div == 0)
8584 return dst;
8586 known = isl_basic_map_divs_known(src);
8587 if (known < 0)
8588 return isl_basic_map_free(dst);
8589 if (!known)
8590 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8591 "some src divs are unknown",
8592 return isl_basic_map_free(dst));
8594 src = isl_basic_map_order_divs(src);
8596 extended = 0;
8597 total = isl_space_dim(src->dim, isl_dim_all);
8598 for (i = 0; i < src->n_div; ++i) {
8599 int j = find_div(dst, src, i);
8600 if (j < 0) {
8601 if (!extended) {
8602 int extra = src->n_div - i;
8603 dst = isl_basic_map_cow(dst);
8604 if (!dst)
8605 return NULL;
8606 dst = isl_basic_map_extend_space(dst,
8607 isl_space_copy(dst->dim),
8608 extra, 0, 2 * extra);
8609 extended = 1;
8611 j = isl_basic_map_alloc_div(dst);
8612 if (j < 0)
8613 return isl_basic_map_free(dst);
8614 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8615 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8616 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8617 return isl_basic_map_free(dst);
8619 if (j != i)
8620 isl_basic_map_swap_div(dst, i, j);
8622 return dst;
8625 struct isl_basic_set *isl_basic_set_align_divs(
8626 struct isl_basic_set *dst, struct isl_basic_set *src)
8628 return bset_from_bmap(isl_basic_map_align_divs(bset_to_bmap(dst),
8629 bset_to_bmap(src)));
8632 struct isl_map *isl_map_align_divs(struct isl_map *map)
8634 int i;
8636 if (!map)
8637 return NULL;
8638 if (map->n == 0)
8639 return map;
8640 map = isl_map_compute_divs(map);
8641 map = isl_map_cow(map);
8642 if (!map)
8643 return NULL;
8645 for (i = 1; i < map->n; ++i)
8646 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8647 for (i = 1; i < map->n; ++i) {
8648 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8649 if (!map->p[i])
8650 return isl_map_free(map);
8653 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8654 return map;
8657 struct isl_set *isl_set_align_divs(struct isl_set *set)
8659 return set_from_map(isl_map_align_divs(set_to_map(set)));
8662 /* Align the divs of the basic maps in "map" to those
8663 * of the basic maps in "list", as well as to the other basic maps in "map".
8664 * The elements in "list" are assumed to have known divs.
8666 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8667 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8669 int i, n;
8671 map = isl_map_compute_divs(map);
8672 map = isl_map_cow(map);
8673 if (!map || !list)
8674 return isl_map_free(map);
8675 if (map->n == 0)
8676 return map;
8678 n = isl_basic_map_list_n_basic_map(list);
8679 for (i = 0; i < n; ++i) {
8680 isl_basic_map *bmap;
8682 bmap = isl_basic_map_list_get_basic_map(list, i);
8683 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8684 isl_basic_map_free(bmap);
8686 if (!map->p[0])
8687 return isl_map_free(map);
8689 return isl_map_align_divs(map);
8692 /* Align the divs of each element of "list" to those of "bmap".
8693 * Both "bmap" and the elements of "list" are assumed to have known divs.
8695 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8696 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8698 int i, n;
8700 if (!list || !bmap)
8701 return isl_basic_map_list_free(list);
8703 n = isl_basic_map_list_n_basic_map(list);
8704 for (i = 0; i < n; ++i) {
8705 isl_basic_map *bmap_i;
8707 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8708 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8709 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8712 return list;
8715 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8716 __isl_take isl_map *map)
8718 if (!set || !map)
8719 goto error;
8720 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8721 map = isl_map_intersect_domain(map, set);
8722 set = isl_map_range(map);
8723 return set;
8724 error:
8725 isl_set_free(set);
8726 isl_map_free(map);
8727 return NULL;
8730 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8731 __isl_take isl_map *map)
8733 return isl_map_align_params_map_map_and(set, map, &set_apply);
8736 /* There is no need to cow as removing empty parts doesn't change
8737 * the meaning of the set.
8739 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8741 int i;
8743 if (!map)
8744 return NULL;
8746 for (i = map->n - 1; i >= 0; --i)
8747 remove_if_empty(map, i);
8749 return map;
8752 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8754 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8757 static __isl_give isl_basic_map *map_copy_basic_map(__isl_keep isl_map *map)
8759 struct isl_basic_map *bmap;
8760 if (!map || map->n == 0)
8761 return NULL;
8762 bmap = map->p[map->n-1];
8763 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8764 return isl_basic_map_copy(bmap);
8767 __isl_give isl_basic_map *isl_map_copy_basic_map(__isl_keep isl_map *map)
8769 return map_copy_basic_map(map);
8772 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8774 return bset_from_bmap(map_copy_basic_map(set_to_map(set)));
8777 static __isl_give isl_map *map_drop_basic_map(__isl_take isl_map *map,
8778 __isl_keep isl_basic_map *bmap)
8780 int i;
8782 if (!map || !bmap)
8783 goto error;
8784 for (i = map->n-1; i >= 0; --i) {
8785 if (map->p[i] != bmap)
8786 continue;
8787 map = isl_map_cow(map);
8788 if (!map)
8789 goto error;
8790 isl_basic_map_free(map->p[i]);
8791 if (i != map->n-1) {
8792 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8793 map->p[i] = map->p[map->n-1];
8795 map->n--;
8796 return map;
8798 return map;
8799 error:
8800 isl_map_free(map);
8801 return NULL;
8804 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8805 __isl_keep isl_basic_map *bmap)
8807 return map_drop_basic_map(map, bmap);
8810 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8811 struct isl_basic_set *bset)
8813 return set_from_map(map_drop_basic_map(set_to_map(set),
8814 bset_to_bmap(bset)));
8817 /* Given two basic sets bset1 and bset2, compute the maximal difference
8818 * between the values of dimension pos in bset1 and those in bset2
8819 * for any common value of the parameters and dimensions preceding pos.
8821 static enum isl_lp_result basic_set_maximal_difference_at(
8822 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8823 int pos, isl_int *opt)
8825 isl_basic_map *bmap1;
8826 isl_basic_map *bmap2;
8827 struct isl_ctx *ctx;
8828 struct isl_vec *obj;
8829 unsigned total;
8830 unsigned nparam;
8831 unsigned dim1;
8832 enum isl_lp_result res;
8834 if (!bset1 || !bset2)
8835 return isl_lp_error;
8837 nparam = isl_basic_set_n_param(bset1);
8838 dim1 = isl_basic_set_n_dim(bset1);
8840 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8841 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8842 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8843 isl_dim_out, 0, pos);
8844 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8845 isl_dim_out, 0, pos);
8846 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8847 if (!bmap1)
8848 return isl_lp_error;
8850 total = isl_basic_map_total_dim(bmap1);
8851 ctx = bmap1->ctx;
8852 obj = isl_vec_alloc(ctx, 1 + total);
8853 if (!obj)
8854 goto error;
8855 isl_seq_clr(obj->block.data, 1 + total);
8856 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8857 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8858 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8859 opt, NULL, NULL);
8860 isl_basic_map_free(bmap1);
8861 isl_vec_free(obj);
8862 return res;
8863 error:
8864 isl_basic_map_free(bmap1);
8865 return isl_lp_error;
8868 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8869 * for any common value of the parameters and dimensions preceding pos
8870 * in both basic sets, the values of dimension pos in bset1 are
8871 * smaller or larger than those in bset2.
8873 * Returns
8874 * 1 if bset1 follows bset2
8875 * -1 if bset1 precedes bset2
8876 * 0 if bset1 and bset2 are incomparable
8877 * -2 if some error occurred.
8879 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8880 struct isl_basic_set *bset2, int pos)
8882 isl_int opt;
8883 enum isl_lp_result res;
8884 int cmp;
8886 isl_int_init(opt);
8888 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8890 if (res == isl_lp_empty)
8891 cmp = 0;
8892 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8893 res == isl_lp_unbounded)
8894 cmp = 1;
8895 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8896 cmp = -1;
8897 else
8898 cmp = -2;
8900 isl_int_clear(opt);
8901 return cmp;
8904 /* Given two basic sets bset1 and bset2, check whether
8905 * for any common value of the parameters and dimensions preceding pos
8906 * there is a value of dimension pos in bset1 that is larger
8907 * than a value of the same dimension in bset2.
8909 * Return
8910 * 1 if there exists such a pair
8911 * 0 if there is no such pair, but there is a pair of equal values
8912 * -1 otherwise
8913 * -2 if some error occurred.
8915 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8916 __isl_keep isl_basic_set *bset2, int pos)
8918 isl_int opt;
8919 enum isl_lp_result res;
8920 int cmp;
8922 isl_int_init(opt);
8924 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8926 if (res == isl_lp_empty)
8927 cmp = -1;
8928 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8929 res == isl_lp_unbounded)
8930 cmp = 1;
8931 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8932 cmp = -1;
8933 else if (res == isl_lp_ok)
8934 cmp = 0;
8935 else
8936 cmp = -2;
8938 isl_int_clear(opt);
8939 return cmp;
8942 /* Given two sets set1 and set2, check whether
8943 * for any common value of the parameters and dimensions preceding pos
8944 * there is a value of dimension pos in set1 that is larger
8945 * than a value of the same dimension in set2.
8947 * Return
8948 * 1 if there exists such a pair
8949 * 0 if there is no such pair, but there is a pair of equal values
8950 * -1 otherwise
8951 * -2 if some error occurred.
8953 int isl_set_follows_at(__isl_keep isl_set *set1,
8954 __isl_keep isl_set *set2, int pos)
8956 int i, j;
8957 int follows = -1;
8959 if (!set1 || !set2)
8960 return -2;
8962 for (i = 0; i < set1->n; ++i)
8963 for (j = 0; j < set2->n; ++j) {
8964 int f;
8965 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8966 if (f == 1 || f == -2)
8967 return f;
8968 if (f > follows)
8969 follows = f;
8972 return follows;
8975 static isl_bool isl_basic_map_plain_has_fixed_var(
8976 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8978 int i;
8979 int d;
8980 unsigned total;
8982 if (!bmap)
8983 return isl_bool_error;
8984 total = isl_basic_map_total_dim(bmap);
8985 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8986 for (; d+1 > pos; --d)
8987 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8988 break;
8989 if (d != pos)
8990 continue;
8991 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8992 return isl_bool_false;
8993 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8994 return isl_bool_false;
8995 if (!isl_int_is_one(bmap->eq[i][1+d]))
8996 return isl_bool_false;
8997 if (val)
8998 isl_int_neg(*val, bmap->eq[i][0]);
8999 return isl_bool_true;
9001 return isl_bool_false;
9004 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9005 unsigned pos, isl_int *val)
9007 int i;
9008 isl_int v;
9009 isl_int tmp;
9010 isl_bool fixed;
9012 if (!map)
9013 return -1;
9014 if (map->n == 0)
9015 return 0;
9016 if (map->n == 1)
9017 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9018 isl_int_init(v);
9019 isl_int_init(tmp);
9020 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9021 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9022 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9023 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9024 fixed = isl_bool_false;
9026 if (val)
9027 isl_int_set(*val, v);
9028 isl_int_clear(tmp);
9029 isl_int_clear(v);
9030 return fixed;
9033 static isl_bool isl_basic_set_plain_has_fixed_var(
9034 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9036 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9037 pos, val);
9040 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
9041 isl_int *val)
9043 return isl_map_plain_has_fixed_var(set_to_map(set), pos, val);
9046 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9047 enum isl_dim_type type, unsigned pos, isl_int *val)
9049 if (pos >= isl_basic_map_dim(bmap, type))
9050 return -1;
9051 return isl_basic_map_plain_has_fixed_var(bmap,
9052 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9055 /* If "bmap" obviously lies on a hyperplane where the given dimension
9056 * has a fixed value, then return that value.
9057 * Otherwise return NaN.
9059 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9060 __isl_keep isl_basic_map *bmap,
9061 enum isl_dim_type type, unsigned pos)
9063 isl_ctx *ctx;
9064 isl_val *v;
9065 int fixed;
9067 if (!bmap)
9068 return NULL;
9069 ctx = isl_basic_map_get_ctx(bmap);
9070 v = isl_val_alloc(ctx);
9071 if (!v)
9072 return NULL;
9073 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9074 if (fixed < 0)
9075 return isl_val_free(v);
9076 if (fixed) {
9077 isl_int_set_si(v->d, 1);
9078 return v;
9080 isl_val_free(v);
9081 return isl_val_nan(ctx);
9084 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
9085 enum isl_dim_type type, unsigned pos, isl_int *val)
9087 if (pos >= isl_map_dim(map, type))
9088 return -1;
9089 return isl_map_plain_has_fixed_var(map,
9090 map_offset(map, type) - 1 + pos, val);
9093 /* If "map" obviously lies on a hyperplane where the given dimension
9094 * has a fixed value, then return that value.
9095 * Otherwise return NaN.
9097 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9098 enum isl_dim_type type, unsigned pos)
9100 isl_ctx *ctx;
9101 isl_val *v;
9102 int fixed;
9104 if (!map)
9105 return NULL;
9106 ctx = isl_map_get_ctx(map);
9107 v = isl_val_alloc(ctx);
9108 if (!v)
9109 return NULL;
9110 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9111 if (fixed < 0)
9112 return isl_val_free(v);
9113 if (fixed) {
9114 isl_int_set_si(v->d, 1);
9115 return v;
9117 isl_val_free(v);
9118 return isl_val_nan(ctx);
9121 /* If "set" obviously lies on a hyperplane where the given dimension
9122 * has a fixed value, then return that value.
9123 * Otherwise return NaN.
9125 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9126 enum isl_dim_type type, unsigned pos)
9128 return isl_map_plain_get_val_if_fixed(set, type, pos);
9131 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
9132 enum isl_dim_type type, unsigned pos, isl_int *val)
9134 return isl_map_plain_is_fixed(set, type, pos, val);
9137 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9138 * then return this fixed value in *val.
9140 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9141 unsigned dim, isl_int *val)
9143 return isl_basic_set_plain_has_fixed_var(bset,
9144 isl_basic_set_n_param(bset) + dim, val);
9147 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9148 * then return this fixed value in *val.
9150 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
9151 unsigned dim, isl_int *val)
9153 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
9156 /* Check if input variable in has fixed value and if so and if val is not NULL,
9157 * then return this fixed value in *val.
9159 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
9160 unsigned in, isl_int *val)
9162 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
9165 /* Check if dimension dim has an (obvious) fixed lower bound and if so
9166 * and if val is not NULL, then return this lower bound in *val.
9168 int isl_basic_set_plain_dim_has_fixed_lower_bound(
9169 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
9171 int i, i_eq = -1, i_ineq = -1;
9172 isl_int *c;
9173 unsigned total;
9174 unsigned nparam;
9176 if (!bset)
9177 return -1;
9178 total = isl_basic_set_total_dim(bset);
9179 nparam = isl_basic_set_n_param(bset);
9180 for (i = 0; i < bset->n_eq; ++i) {
9181 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
9182 continue;
9183 if (i_eq != -1)
9184 return 0;
9185 i_eq = i;
9187 for (i = 0; i < bset->n_ineq; ++i) {
9188 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
9189 continue;
9190 if (i_eq != -1 || i_ineq != -1)
9191 return 0;
9192 i_ineq = i;
9194 if (i_eq == -1 && i_ineq == -1)
9195 return 0;
9196 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
9197 /* The coefficient should always be one due to normalization. */
9198 if (!isl_int_is_one(c[1+nparam+dim]))
9199 return 0;
9200 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
9201 return 0;
9202 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
9203 total - nparam - dim - 1) != -1)
9204 return 0;
9205 if (val)
9206 isl_int_neg(*val, c[0]);
9207 return 1;
9210 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
9211 unsigned dim, isl_int *val)
9213 int i;
9214 isl_int v;
9215 isl_int tmp;
9216 int fixed;
9218 if (!set)
9219 return -1;
9220 if (set->n == 0)
9221 return 0;
9222 if (set->n == 1)
9223 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9224 dim, val);
9225 isl_int_init(v);
9226 isl_int_init(tmp);
9227 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
9228 dim, &v);
9229 for (i = 1; fixed == 1 && i < set->n; ++i) {
9230 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9231 dim, &tmp);
9232 if (fixed == 1 && isl_int_ne(tmp, v))
9233 fixed = 0;
9235 if (val)
9236 isl_int_set(*val, v);
9237 isl_int_clear(tmp);
9238 isl_int_clear(v);
9239 return fixed;
9242 /* Return -1 if the constraint "c1" should be sorted before "c2"
9243 * and 1 if it should be sorted after "c2".
9244 * Return 0 if the two constraints are the same (up to the constant term).
9246 * In particular, if a constraint involves later variables than another
9247 * then it is sorted after this other constraint.
9248 * uset_gist depends on constraints without existentially quantified
9249 * variables sorting first.
9251 * For constraints that have the same latest variable, those
9252 * with the same coefficient for this latest variable (first in absolute value
9253 * and then in actual value) are grouped together.
9254 * This is useful for detecting pairs of constraints that can
9255 * be chained in their printed representation.
9257 * Finally, within a group, constraints are sorted according to
9258 * their coefficients (excluding the constant term).
9260 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9262 isl_int **c1 = (isl_int **) p1;
9263 isl_int **c2 = (isl_int **) p2;
9264 int l1, l2;
9265 unsigned size = *(unsigned *) arg;
9266 int cmp;
9268 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9269 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9271 if (l1 != l2)
9272 return l1 - l2;
9274 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9275 if (cmp != 0)
9276 return cmp;
9277 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9278 if (cmp != 0)
9279 return -cmp;
9281 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9284 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9285 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9286 * and 0 if the two constraints are the same (up to the constant term).
9288 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9289 isl_int *c1, isl_int *c2)
9291 unsigned total;
9293 if (!bmap)
9294 return -2;
9295 total = isl_basic_map_total_dim(bmap);
9296 return sort_constraint_cmp(&c1, &c2, &total);
9299 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9300 __isl_take isl_basic_map *bmap)
9302 unsigned total;
9304 if (!bmap)
9305 return NULL;
9306 if (bmap->n_ineq == 0)
9307 return bmap;
9308 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9309 return bmap;
9310 total = isl_basic_map_total_dim(bmap);
9311 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9312 &sort_constraint_cmp, &total) < 0)
9313 return isl_basic_map_free(bmap);
9314 return bmap;
9317 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9318 __isl_take isl_basic_set *bset)
9320 isl_basic_map *bmap = bset_to_bmap(bset);
9321 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9324 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9326 if (!bmap)
9327 return NULL;
9328 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9329 return bmap;
9330 bmap = isl_basic_map_remove_redundancies(bmap);
9331 bmap = isl_basic_map_sort_constraints(bmap);
9332 if (bmap)
9333 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9334 return bmap;
9337 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9339 return bset_from_bmap(isl_basic_map_normalize(bset_to_bmap(bset)));
9342 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9343 const __isl_keep isl_basic_map *bmap2)
9345 int i, cmp;
9346 unsigned total;
9348 if (!bmap1 || !bmap2)
9349 return -1;
9351 if (bmap1 == bmap2)
9352 return 0;
9353 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9354 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9355 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9356 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9357 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9358 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9359 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9360 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9361 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9362 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9363 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9364 return 0;
9365 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9366 return 1;
9367 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9368 return -1;
9369 if (bmap1->n_eq != bmap2->n_eq)
9370 return bmap1->n_eq - bmap2->n_eq;
9371 if (bmap1->n_ineq != bmap2->n_ineq)
9372 return bmap1->n_ineq - bmap2->n_ineq;
9373 if (bmap1->n_div != bmap2->n_div)
9374 return bmap1->n_div - bmap2->n_div;
9375 total = isl_basic_map_total_dim(bmap1);
9376 for (i = 0; i < bmap1->n_eq; ++i) {
9377 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9378 if (cmp)
9379 return cmp;
9381 for (i = 0; i < bmap1->n_ineq; ++i) {
9382 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9383 if (cmp)
9384 return cmp;
9386 for (i = 0; i < bmap1->n_div; ++i) {
9387 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9388 if (cmp)
9389 return cmp;
9391 return 0;
9394 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9395 const __isl_keep isl_basic_set *bset2)
9397 return isl_basic_map_plain_cmp(bset1, bset2);
9400 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9402 int i, cmp;
9404 if (set1 == set2)
9405 return 0;
9406 if (set1->n != set2->n)
9407 return set1->n - set2->n;
9409 for (i = 0; i < set1->n; ++i) {
9410 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9411 if (cmp)
9412 return cmp;
9415 return 0;
9418 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9419 __isl_keep isl_basic_map *bmap2)
9421 if (!bmap1 || !bmap2)
9422 return isl_bool_error;
9423 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9426 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9427 __isl_keep isl_basic_set *bset2)
9429 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9430 bset_to_bmap(bset2));
9433 static int qsort_bmap_cmp(const void *p1, const void *p2)
9435 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9436 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9438 return isl_basic_map_plain_cmp(bmap1, bmap2);
9441 /* Sort the basic maps of "map" and remove duplicate basic maps.
9443 * While removing basic maps, we make sure that the basic maps remain
9444 * sorted because isl_map_normalize expects the basic maps of the result
9445 * to be sorted.
9447 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9449 int i, j;
9451 map = isl_map_remove_empty_parts(map);
9452 if (!map)
9453 return NULL;
9454 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9455 for (i = map->n - 1; i >= 1; --i) {
9456 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9457 continue;
9458 isl_basic_map_free(map->p[i-1]);
9459 for (j = i; j < map->n; ++j)
9460 map->p[j - 1] = map->p[j];
9461 map->n--;
9464 return map;
9467 /* Remove obvious duplicates among the basic maps of "map".
9469 * Unlike isl_map_normalize, this function does not remove redundant
9470 * constraints and only removes duplicates that have exactly the same
9471 * constraints in the input. It does sort the constraints and
9472 * the basic maps to ease the detection of duplicates.
9474 * If "map" has already been normalized or if the basic maps are
9475 * disjoint, then there can be no duplicates.
9477 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9479 int i;
9480 isl_basic_map *bmap;
9482 if (!map)
9483 return NULL;
9484 if (map->n <= 1)
9485 return map;
9486 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9487 return map;
9488 for (i = 0; i < map->n; ++i) {
9489 bmap = isl_basic_map_copy(map->p[i]);
9490 bmap = isl_basic_map_sort_constraints(bmap);
9491 if (!bmap)
9492 return isl_map_free(map);
9493 isl_basic_map_free(map->p[i]);
9494 map->p[i] = bmap;
9497 map = sort_and_remove_duplicates(map);
9498 return map;
9501 /* We normalize in place, but if anything goes wrong we need
9502 * to return NULL, so we need to make sure we don't change the
9503 * meaning of any possible other copies of map.
9505 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9507 int i;
9508 struct isl_basic_map *bmap;
9510 if (!map)
9511 return NULL;
9512 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9513 return map;
9514 for (i = 0; i < map->n; ++i) {
9515 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9516 if (!bmap)
9517 goto error;
9518 isl_basic_map_free(map->p[i]);
9519 map->p[i] = bmap;
9522 map = sort_and_remove_duplicates(map);
9523 if (map)
9524 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9525 return map;
9526 error:
9527 isl_map_free(map);
9528 return NULL;
9531 struct isl_set *isl_set_normalize(struct isl_set *set)
9533 return set_from_map(isl_map_normalize(set_to_map(set)));
9536 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9537 __isl_keep isl_map *map2)
9539 int i;
9540 isl_bool equal;
9542 if (!map1 || !map2)
9543 return isl_bool_error;
9545 if (map1 == map2)
9546 return isl_bool_true;
9547 if (!isl_space_is_equal(map1->dim, map2->dim))
9548 return isl_bool_false;
9550 map1 = isl_map_copy(map1);
9551 map2 = isl_map_copy(map2);
9552 map1 = isl_map_normalize(map1);
9553 map2 = isl_map_normalize(map2);
9554 if (!map1 || !map2)
9555 goto error;
9556 equal = map1->n == map2->n;
9557 for (i = 0; equal && i < map1->n; ++i) {
9558 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9559 if (equal < 0)
9560 goto error;
9562 isl_map_free(map1);
9563 isl_map_free(map2);
9564 return equal;
9565 error:
9566 isl_map_free(map1);
9567 isl_map_free(map2);
9568 return isl_bool_error;
9571 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9572 __isl_keep isl_set *set2)
9574 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9577 /* Return an interval that ranges from min to max (inclusive)
9579 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9580 isl_int min, isl_int max)
9582 int k;
9583 struct isl_basic_set *bset = NULL;
9585 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9586 if (!bset)
9587 goto error;
9589 k = isl_basic_set_alloc_inequality(bset);
9590 if (k < 0)
9591 goto error;
9592 isl_int_set_si(bset->ineq[k][1], 1);
9593 isl_int_neg(bset->ineq[k][0], min);
9595 k = isl_basic_set_alloc_inequality(bset);
9596 if (k < 0)
9597 goto error;
9598 isl_int_set_si(bset->ineq[k][1], -1);
9599 isl_int_set(bset->ineq[k][0], max);
9601 return bset;
9602 error:
9603 isl_basic_set_free(bset);
9604 return NULL;
9607 /* Return the basic maps in "map" as a list.
9609 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9610 __isl_keep isl_map *map)
9612 int i;
9613 isl_ctx *ctx;
9614 isl_basic_map_list *list;
9616 if (!map)
9617 return NULL;
9618 ctx = isl_map_get_ctx(map);
9619 list = isl_basic_map_list_alloc(ctx, map->n);
9621 for (i = 0; i < map->n; ++i) {
9622 isl_basic_map *bmap;
9624 bmap = isl_basic_map_copy(map->p[i]);
9625 list = isl_basic_map_list_add(list, bmap);
9628 return list;
9631 /* Return the intersection of the elements in the non-empty list "list".
9632 * All elements are assumed to live in the same space.
9634 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9635 __isl_take isl_basic_map_list *list)
9637 int i, n;
9638 isl_basic_map *bmap;
9640 if (!list)
9641 return NULL;
9642 n = isl_basic_map_list_n_basic_map(list);
9643 if (n < 1)
9644 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9645 "expecting non-empty list", goto error);
9647 bmap = isl_basic_map_list_get_basic_map(list, 0);
9648 for (i = 1; i < n; ++i) {
9649 isl_basic_map *bmap_i;
9651 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9652 bmap = isl_basic_map_intersect(bmap, bmap_i);
9655 isl_basic_map_list_free(list);
9656 return bmap;
9657 error:
9658 isl_basic_map_list_free(list);
9659 return NULL;
9662 /* Return the intersection of the elements in the non-empty list "list".
9663 * All elements are assumed to live in the same space.
9665 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9666 __isl_take isl_basic_set_list *list)
9668 return isl_basic_map_list_intersect(list);
9671 /* Return the union of the elements of "list".
9672 * The list is required to have at least one element.
9674 __isl_give isl_set *isl_basic_set_list_union(
9675 __isl_take isl_basic_set_list *list)
9677 int i, n;
9678 isl_space *space;
9679 isl_basic_set *bset;
9680 isl_set *set;
9682 if (!list)
9683 return NULL;
9684 n = isl_basic_set_list_n_basic_set(list);
9685 if (n < 1)
9686 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9687 "expecting non-empty list", goto error);
9689 bset = isl_basic_set_list_get_basic_set(list, 0);
9690 space = isl_basic_set_get_space(bset);
9691 isl_basic_set_free(bset);
9693 set = isl_set_alloc_space(space, n, 0);
9694 for (i = 0; i < n; ++i) {
9695 bset = isl_basic_set_list_get_basic_set(list, i);
9696 set = isl_set_add_basic_set(set, bset);
9699 isl_basic_set_list_free(list);
9700 return set;
9701 error:
9702 isl_basic_set_list_free(list);
9703 return NULL;
9706 /* Return the union of the elements in the non-empty list "list".
9707 * All elements are assumed to live in the same space.
9709 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9711 int i, n;
9712 isl_set *set;
9714 if (!list)
9715 return NULL;
9716 n = isl_set_list_n_set(list);
9717 if (n < 1)
9718 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9719 "expecting non-empty list", goto error);
9721 set = isl_set_list_get_set(list, 0);
9722 for (i = 1; i < n; ++i) {
9723 isl_set *set_i;
9725 set_i = isl_set_list_get_set(list, i);
9726 set = isl_set_union(set, set_i);
9729 isl_set_list_free(list);
9730 return set;
9731 error:
9732 isl_set_list_free(list);
9733 return NULL;
9736 /* Return the Cartesian product of the basic sets in list (in the given order).
9738 __isl_give isl_basic_set *isl_basic_set_list_product(
9739 __isl_take struct isl_basic_set_list *list)
9741 int i;
9742 unsigned dim;
9743 unsigned nparam;
9744 unsigned extra;
9745 unsigned n_eq;
9746 unsigned n_ineq;
9747 struct isl_basic_set *product = NULL;
9749 if (!list)
9750 goto error;
9751 isl_assert(list->ctx, list->n > 0, goto error);
9752 isl_assert(list->ctx, list->p[0], goto error);
9753 nparam = isl_basic_set_n_param(list->p[0]);
9754 dim = isl_basic_set_n_dim(list->p[0]);
9755 extra = list->p[0]->n_div;
9756 n_eq = list->p[0]->n_eq;
9757 n_ineq = list->p[0]->n_ineq;
9758 for (i = 1; i < list->n; ++i) {
9759 isl_assert(list->ctx, list->p[i], goto error);
9760 isl_assert(list->ctx,
9761 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9762 dim += isl_basic_set_n_dim(list->p[i]);
9763 extra += list->p[i]->n_div;
9764 n_eq += list->p[i]->n_eq;
9765 n_ineq += list->p[i]->n_ineq;
9767 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9768 n_eq, n_ineq);
9769 if (!product)
9770 goto error;
9771 dim = 0;
9772 for (i = 0; i < list->n; ++i) {
9773 isl_basic_set_add_constraints(product,
9774 isl_basic_set_copy(list->p[i]), dim);
9775 dim += isl_basic_set_n_dim(list->p[i]);
9777 isl_basic_set_list_free(list);
9778 return product;
9779 error:
9780 isl_basic_set_free(product);
9781 isl_basic_set_list_free(list);
9782 return NULL;
9785 struct isl_basic_map *isl_basic_map_product(
9786 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9788 isl_space *dim_result = NULL;
9789 struct isl_basic_map *bmap;
9790 unsigned in1, in2, out1, out2, nparam, total, pos;
9791 struct isl_dim_map *dim_map1, *dim_map2;
9793 if (!bmap1 || !bmap2)
9794 goto error;
9796 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9797 bmap2->dim, isl_dim_param), goto error);
9798 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9799 isl_space_copy(bmap2->dim));
9801 in1 = isl_basic_map_n_in(bmap1);
9802 in2 = isl_basic_map_n_in(bmap2);
9803 out1 = isl_basic_map_n_out(bmap1);
9804 out2 = isl_basic_map_n_out(bmap2);
9805 nparam = isl_basic_map_n_param(bmap1);
9807 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9808 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9809 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9810 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9811 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9812 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9813 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9814 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9815 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9816 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9817 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9819 bmap = isl_basic_map_alloc_space(dim_result,
9820 bmap1->n_div + bmap2->n_div,
9821 bmap1->n_eq + bmap2->n_eq,
9822 bmap1->n_ineq + bmap2->n_ineq);
9823 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9824 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9825 bmap = isl_basic_map_simplify(bmap);
9826 return isl_basic_map_finalize(bmap);
9827 error:
9828 isl_basic_map_free(bmap1);
9829 isl_basic_map_free(bmap2);
9830 return NULL;
9833 __isl_give isl_basic_map *isl_basic_map_flat_product(
9834 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9836 isl_basic_map *prod;
9838 prod = isl_basic_map_product(bmap1, bmap2);
9839 prod = isl_basic_map_flatten(prod);
9840 return prod;
9843 __isl_give isl_basic_set *isl_basic_set_flat_product(
9844 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9846 return isl_basic_map_flat_range_product(bset1, bset2);
9849 __isl_give isl_basic_map *isl_basic_map_domain_product(
9850 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9852 isl_space *space_result = NULL;
9853 isl_basic_map *bmap;
9854 unsigned in1, in2, out, nparam, total, pos;
9855 struct isl_dim_map *dim_map1, *dim_map2;
9857 if (!bmap1 || !bmap2)
9858 goto error;
9860 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9861 isl_space_copy(bmap2->dim));
9863 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9864 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9865 out = isl_basic_map_dim(bmap1, isl_dim_out);
9866 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9868 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9869 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9870 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9871 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9872 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9873 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9874 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9875 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9876 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9877 isl_dim_map_div(dim_map1, bmap1, pos += out);
9878 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9880 bmap = isl_basic_map_alloc_space(space_result,
9881 bmap1->n_div + bmap2->n_div,
9882 bmap1->n_eq + bmap2->n_eq,
9883 bmap1->n_ineq + bmap2->n_ineq);
9884 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9885 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9886 bmap = isl_basic_map_simplify(bmap);
9887 return isl_basic_map_finalize(bmap);
9888 error:
9889 isl_basic_map_free(bmap1);
9890 isl_basic_map_free(bmap2);
9891 return NULL;
9894 __isl_give isl_basic_map *isl_basic_map_range_product(
9895 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9897 int rational;
9898 isl_space *dim_result = NULL;
9899 isl_basic_map *bmap;
9900 unsigned in, out1, out2, nparam, total, pos;
9901 struct isl_dim_map *dim_map1, *dim_map2;
9903 rational = isl_basic_map_is_rational(bmap1);
9904 if (rational >= 0 && rational)
9905 rational = isl_basic_map_is_rational(bmap2);
9906 if (!bmap1 || !bmap2 || rational < 0)
9907 goto error;
9909 if (!isl_space_match(bmap1->dim, isl_dim_param,
9910 bmap2->dim, isl_dim_param))
9911 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9912 "parameters don't match", goto error);
9914 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9915 isl_space_copy(bmap2->dim));
9917 in = isl_basic_map_dim(bmap1, isl_dim_in);
9918 out1 = isl_basic_map_n_out(bmap1);
9919 out2 = isl_basic_map_n_out(bmap2);
9920 nparam = isl_basic_map_n_param(bmap1);
9922 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9923 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9924 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9925 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9926 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9927 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9928 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9929 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9930 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9931 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9932 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9934 bmap = isl_basic_map_alloc_space(dim_result,
9935 bmap1->n_div + bmap2->n_div,
9936 bmap1->n_eq + bmap2->n_eq,
9937 bmap1->n_ineq + bmap2->n_ineq);
9938 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9939 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9940 if (rational)
9941 bmap = isl_basic_map_set_rational(bmap);
9942 bmap = isl_basic_map_simplify(bmap);
9943 return isl_basic_map_finalize(bmap);
9944 error:
9945 isl_basic_map_free(bmap1);
9946 isl_basic_map_free(bmap2);
9947 return NULL;
9950 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9951 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9953 isl_basic_map *prod;
9955 prod = isl_basic_map_range_product(bmap1, bmap2);
9956 prod = isl_basic_map_flatten_range(prod);
9957 return prod;
9960 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9961 * and collect the results.
9962 * The result live in the space obtained by calling "space_product"
9963 * on the spaces of "map1" and "map2".
9964 * If "remove_duplicates" is set then the result may contain duplicates
9965 * (even if the inputs do not) and so we try and remove the obvious
9966 * duplicates.
9968 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9969 __isl_take isl_map *map2,
9970 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9971 __isl_take isl_space *right),
9972 __isl_give isl_basic_map *(*basic_map_product)(
9973 __isl_take isl_basic_map *left,
9974 __isl_take isl_basic_map *right),
9975 int remove_duplicates)
9977 unsigned flags = 0;
9978 struct isl_map *result;
9979 int i, j;
9981 if (!map1 || !map2)
9982 goto error;
9984 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9985 map2->dim, isl_dim_param), goto error);
9987 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9988 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9989 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9991 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9992 isl_space_copy(map2->dim)),
9993 map1->n * map2->n, flags);
9994 if (!result)
9995 goto error;
9996 for (i = 0; i < map1->n; ++i)
9997 for (j = 0; j < map2->n; ++j) {
9998 struct isl_basic_map *part;
9999 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10000 isl_basic_map_copy(map2->p[j]));
10001 if (isl_basic_map_is_empty(part))
10002 isl_basic_map_free(part);
10003 else
10004 result = isl_map_add_basic_map(result, part);
10005 if (!result)
10006 goto error;
10008 if (remove_duplicates)
10009 result = isl_map_remove_obvious_duplicates(result);
10010 isl_map_free(map1);
10011 isl_map_free(map2);
10012 return result;
10013 error:
10014 isl_map_free(map1);
10015 isl_map_free(map2);
10016 return NULL;
10019 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10021 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10022 __isl_take isl_map *map2)
10024 return map_product(map1, map2, &isl_space_product,
10025 &isl_basic_map_product, 0);
10028 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10029 __isl_take isl_map *map2)
10031 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10034 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10036 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10037 __isl_take isl_map *map2)
10039 isl_map *prod;
10041 prod = isl_map_product(map1, map2);
10042 prod = isl_map_flatten(prod);
10043 return prod;
10046 /* Given two set A and B, construct its Cartesian product A x B.
10048 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10050 return isl_map_range_product(set1, set2);
10053 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10054 __isl_take isl_set *set2)
10056 return isl_map_flat_range_product(set1, set2);
10059 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10061 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10062 __isl_take isl_map *map2)
10064 return map_product(map1, map2, &isl_space_domain_product,
10065 &isl_basic_map_domain_product, 1);
10068 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10070 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10071 __isl_take isl_map *map2)
10073 return map_product(map1, map2, &isl_space_range_product,
10074 &isl_basic_map_range_product, 1);
10077 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10078 __isl_take isl_map *map2)
10080 return isl_map_align_params_map_map_and(map1, map2,
10081 &map_domain_product_aligned);
10084 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10085 __isl_take isl_map *map2)
10087 return isl_map_align_params_map_map_and(map1, map2,
10088 &map_range_product_aligned);
10091 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10093 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10095 isl_space *space;
10096 int total1, keep1, total2, keep2;
10098 if (!map)
10099 return NULL;
10100 if (!isl_space_domain_is_wrapping(map->dim) ||
10101 !isl_space_range_is_wrapping(map->dim))
10102 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10103 "not a product", return isl_map_free(map));
10105 space = isl_map_get_space(map);
10106 total1 = isl_space_dim(space, isl_dim_in);
10107 total2 = isl_space_dim(space, isl_dim_out);
10108 space = isl_space_factor_domain(space);
10109 keep1 = isl_space_dim(space, isl_dim_in);
10110 keep2 = isl_space_dim(space, isl_dim_out);
10111 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10112 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10113 map = isl_map_reset_space(map, space);
10115 return map;
10118 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10120 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10122 isl_space *space;
10123 int total1, keep1, total2, keep2;
10125 if (!map)
10126 return NULL;
10127 if (!isl_space_domain_is_wrapping(map->dim) ||
10128 !isl_space_range_is_wrapping(map->dim))
10129 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10130 "not a product", return isl_map_free(map));
10132 space = isl_map_get_space(map);
10133 total1 = isl_space_dim(space, isl_dim_in);
10134 total2 = isl_space_dim(space, isl_dim_out);
10135 space = isl_space_factor_range(space);
10136 keep1 = isl_space_dim(space, isl_dim_in);
10137 keep2 = isl_space_dim(space, isl_dim_out);
10138 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10139 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10140 map = isl_map_reset_space(map, space);
10142 return map;
10145 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10147 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10149 isl_space *space;
10150 int total, keep;
10152 if (!map)
10153 return NULL;
10154 if (!isl_space_domain_is_wrapping(map->dim))
10155 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10156 "domain is not a product", return isl_map_free(map));
10158 space = isl_map_get_space(map);
10159 total = isl_space_dim(space, isl_dim_in);
10160 space = isl_space_domain_factor_domain(space);
10161 keep = isl_space_dim(space, isl_dim_in);
10162 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10163 map = isl_map_reset_space(map, space);
10165 return map;
10168 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10170 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10172 isl_space *space;
10173 int total, keep;
10175 if (!map)
10176 return NULL;
10177 if (!isl_space_domain_is_wrapping(map->dim))
10178 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10179 "domain is not a product", return isl_map_free(map));
10181 space = isl_map_get_space(map);
10182 total = isl_space_dim(space, isl_dim_in);
10183 space = isl_space_domain_factor_range(space);
10184 keep = isl_space_dim(space, isl_dim_in);
10185 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10186 map = isl_map_reset_space(map, space);
10188 return map;
10191 /* Given a map A -> [B -> C], extract the map A -> B.
10193 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10195 isl_space *space;
10196 int total, keep;
10198 if (!map)
10199 return NULL;
10200 if (!isl_space_range_is_wrapping(map->dim))
10201 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10202 "range is not a product", return isl_map_free(map));
10204 space = isl_map_get_space(map);
10205 total = isl_space_dim(space, isl_dim_out);
10206 space = isl_space_range_factor_domain(space);
10207 keep = isl_space_dim(space, isl_dim_out);
10208 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10209 map = isl_map_reset_space(map, space);
10211 return map;
10214 /* Given a map A -> [B -> C], extract the map A -> C.
10216 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10218 isl_space *space;
10219 int total, keep;
10221 if (!map)
10222 return NULL;
10223 if (!isl_space_range_is_wrapping(map->dim))
10224 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10225 "range is not a product", return isl_map_free(map));
10227 space = isl_map_get_space(map);
10228 total = isl_space_dim(space, isl_dim_out);
10229 space = isl_space_range_factor_range(space);
10230 keep = isl_space_dim(space, isl_dim_out);
10231 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10232 map = isl_map_reset_space(map, space);
10234 return map;
10237 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10239 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10240 __isl_take isl_map *map2)
10242 isl_map *prod;
10244 prod = isl_map_domain_product(map1, map2);
10245 prod = isl_map_flatten_domain(prod);
10246 return prod;
10249 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10251 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10252 __isl_take isl_map *map2)
10254 isl_map *prod;
10256 prod = isl_map_range_product(map1, map2);
10257 prod = isl_map_flatten_range(prod);
10258 return prod;
10261 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10263 int i;
10264 uint32_t hash = isl_hash_init();
10265 unsigned total;
10267 if (!bmap)
10268 return 0;
10269 bmap = isl_basic_map_copy(bmap);
10270 bmap = isl_basic_map_normalize(bmap);
10271 if (!bmap)
10272 return 0;
10273 total = isl_basic_map_total_dim(bmap);
10274 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10275 for (i = 0; i < bmap->n_eq; ++i) {
10276 uint32_t c_hash;
10277 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10278 isl_hash_hash(hash, c_hash);
10280 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10281 for (i = 0; i < bmap->n_ineq; ++i) {
10282 uint32_t c_hash;
10283 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10284 isl_hash_hash(hash, c_hash);
10286 isl_hash_byte(hash, bmap->n_div & 0xFF);
10287 for (i = 0; i < bmap->n_div; ++i) {
10288 uint32_t c_hash;
10289 if (isl_int_is_zero(bmap->div[i][0]))
10290 continue;
10291 isl_hash_byte(hash, i & 0xFF);
10292 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10293 isl_hash_hash(hash, c_hash);
10295 isl_basic_map_free(bmap);
10296 return hash;
10299 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10301 return isl_basic_map_get_hash(bset_to_bmap(bset));
10304 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10306 int i;
10307 uint32_t hash;
10309 if (!map)
10310 return 0;
10311 map = isl_map_copy(map);
10312 map = isl_map_normalize(map);
10313 if (!map)
10314 return 0;
10316 hash = isl_hash_init();
10317 for (i = 0; i < map->n; ++i) {
10318 uint32_t bmap_hash;
10319 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10320 isl_hash_hash(hash, bmap_hash);
10323 isl_map_free(map);
10325 return hash;
10328 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10330 return isl_map_get_hash(set_to_map(set));
10333 /* Check if the value for dimension dim is completely determined
10334 * by the values of the other parameters and variables.
10335 * That is, check if dimension dim is involved in an equality.
10337 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10339 int i;
10340 unsigned nparam;
10342 if (!bset)
10343 return -1;
10344 nparam = isl_basic_set_n_param(bset);
10345 for (i = 0; i < bset->n_eq; ++i)
10346 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10347 return 1;
10348 return 0;
10351 /* Check if the value for dimension dim is completely determined
10352 * by the values of the other parameters and variables.
10353 * That is, check if dimension dim is involved in an equality
10354 * for each of the subsets.
10356 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10358 int i;
10360 if (!set)
10361 return -1;
10362 for (i = 0; i < set->n; ++i) {
10363 int unique;
10364 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10365 if (unique != 1)
10366 return unique;
10368 return 1;
10371 /* Return the number of basic maps in the (current) representation of "map".
10373 int isl_map_n_basic_map(__isl_keep isl_map *map)
10375 return map ? map->n : 0;
10378 int isl_set_n_basic_set(__isl_keep isl_set *set)
10380 return set ? set->n : 0;
10383 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10384 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10386 int i;
10388 if (!map)
10389 return isl_stat_error;
10391 for (i = 0; i < map->n; ++i)
10392 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10393 return isl_stat_error;
10395 return isl_stat_ok;
10398 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10399 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10401 int i;
10403 if (!set)
10404 return isl_stat_error;
10406 for (i = 0; i < set->n; ++i)
10407 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10408 return isl_stat_error;
10410 return isl_stat_ok;
10413 /* Return a list of basic sets, the union of which is equal to "set".
10415 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10416 __isl_keep isl_set *set)
10418 int i;
10419 isl_basic_set_list *list;
10421 if (!set)
10422 return NULL;
10424 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10425 for (i = 0; i < set->n; ++i) {
10426 isl_basic_set *bset;
10428 bset = isl_basic_set_copy(set->p[i]);
10429 list = isl_basic_set_list_add(list, bset);
10432 return list;
10435 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10437 isl_space *dim;
10439 if (!bset)
10440 return NULL;
10442 bset = isl_basic_set_cow(bset);
10443 if (!bset)
10444 return NULL;
10446 dim = isl_basic_set_get_space(bset);
10447 dim = isl_space_lift(dim, bset->n_div);
10448 if (!dim)
10449 goto error;
10450 isl_space_free(bset->dim);
10451 bset->dim = dim;
10452 bset->extra -= bset->n_div;
10453 bset->n_div = 0;
10455 bset = isl_basic_set_finalize(bset);
10457 return bset;
10458 error:
10459 isl_basic_set_free(bset);
10460 return NULL;
10463 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10465 int i;
10466 isl_space *dim;
10467 unsigned n_div;
10469 set = isl_set_align_divs(set);
10471 if (!set)
10472 return NULL;
10474 set = isl_set_cow(set);
10475 if (!set)
10476 return NULL;
10478 n_div = set->p[0]->n_div;
10479 dim = isl_set_get_space(set);
10480 dim = isl_space_lift(dim, n_div);
10481 if (!dim)
10482 goto error;
10483 isl_space_free(set->dim);
10484 set->dim = dim;
10486 for (i = 0; i < set->n; ++i) {
10487 set->p[i] = isl_basic_set_lift(set->p[i]);
10488 if (!set->p[i])
10489 goto error;
10492 return set;
10493 error:
10494 isl_set_free(set);
10495 return NULL;
10498 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10500 isl_space *dim;
10501 struct isl_basic_map *bmap;
10502 unsigned n_set;
10503 unsigned n_div;
10504 unsigned n_param;
10505 unsigned total;
10506 int i, k, l;
10508 set = isl_set_align_divs(set);
10510 if (!set)
10511 return NULL;
10513 dim = isl_set_get_space(set);
10514 if (set->n == 0 || set->p[0]->n_div == 0) {
10515 isl_set_free(set);
10516 return isl_map_identity(isl_space_map_from_set(dim));
10519 n_div = set->p[0]->n_div;
10520 dim = isl_space_map_from_set(dim);
10521 n_param = isl_space_dim(dim, isl_dim_param);
10522 n_set = isl_space_dim(dim, isl_dim_in);
10523 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10524 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10525 for (i = 0; i < n_set; ++i)
10526 bmap = var_equal(bmap, i);
10528 total = n_param + n_set + n_set + n_div;
10529 for (i = 0; i < n_div; ++i) {
10530 k = isl_basic_map_alloc_inequality(bmap);
10531 if (k < 0)
10532 goto error;
10533 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10534 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10535 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10536 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10537 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10538 set->p[0]->div[i][0]);
10540 l = isl_basic_map_alloc_inequality(bmap);
10541 if (l < 0)
10542 goto error;
10543 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10544 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10545 set->p[0]->div[i][0]);
10546 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10549 isl_set_free(set);
10550 bmap = isl_basic_map_simplify(bmap);
10551 bmap = isl_basic_map_finalize(bmap);
10552 return isl_map_from_basic_map(bmap);
10553 error:
10554 isl_set_free(set);
10555 isl_basic_map_free(bmap);
10556 return NULL;
10559 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10561 unsigned dim;
10562 int size = 0;
10564 if (!bset)
10565 return -1;
10567 dim = isl_basic_set_total_dim(bset);
10568 size += bset->n_eq * (1 + dim);
10569 size += bset->n_ineq * (1 + dim);
10570 size += bset->n_div * (2 + dim);
10572 return size;
10575 int isl_set_size(__isl_keep isl_set *set)
10577 int i;
10578 int size = 0;
10580 if (!set)
10581 return -1;
10583 for (i = 0; i < set->n; ++i)
10584 size += isl_basic_set_size(set->p[i]);
10586 return size;
10589 /* Check if there is any lower bound (if lower == 0) and/or upper
10590 * bound (if upper == 0) on the specified dim.
10592 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10593 enum isl_dim_type type, unsigned pos, int lower, int upper)
10595 int i;
10597 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10598 return isl_bool_error;
10600 pos += isl_basic_map_offset(bmap, type);
10602 for (i = 0; i < bmap->n_div; ++i) {
10603 if (isl_int_is_zero(bmap->div[i][0]))
10604 continue;
10605 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10606 return isl_bool_true;
10609 for (i = 0; i < bmap->n_eq; ++i)
10610 if (!isl_int_is_zero(bmap->eq[i][pos]))
10611 return isl_bool_true;
10613 for (i = 0; i < bmap->n_ineq; ++i) {
10614 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10615 if (sgn > 0)
10616 lower = 1;
10617 if (sgn < 0)
10618 upper = 1;
10621 return lower && upper;
10624 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10625 enum isl_dim_type type, unsigned pos)
10627 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10630 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10631 enum isl_dim_type type, unsigned pos)
10633 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10636 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10637 enum isl_dim_type type, unsigned pos)
10639 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10642 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10643 enum isl_dim_type type, unsigned pos)
10645 int i;
10647 if (!map)
10648 return -1;
10650 for (i = 0; i < map->n; ++i) {
10651 int bounded;
10652 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10653 if (bounded < 0 || !bounded)
10654 return bounded;
10657 return 1;
10660 /* Return 1 if the specified dim is involved in both an upper bound
10661 * and a lower bound.
10663 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10664 enum isl_dim_type type, unsigned pos)
10666 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10669 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10671 static isl_bool has_any_bound(__isl_keep isl_map *map,
10672 enum isl_dim_type type, unsigned pos,
10673 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10674 enum isl_dim_type type, unsigned pos))
10676 int i;
10678 if (!map)
10679 return isl_bool_error;
10681 for (i = 0; i < map->n; ++i) {
10682 isl_bool bounded;
10683 bounded = fn(map->p[i], type, pos);
10684 if (bounded < 0 || bounded)
10685 return bounded;
10688 return isl_bool_false;
10691 /* Return 1 if the specified dim is involved in any lower bound.
10693 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10694 enum isl_dim_type type, unsigned pos)
10696 return has_any_bound(set, type, pos,
10697 &isl_basic_map_dim_has_lower_bound);
10700 /* Return 1 if the specified dim is involved in any upper bound.
10702 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10703 enum isl_dim_type type, unsigned pos)
10705 return has_any_bound(set, type, pos,
10706 &isl_basic_map_dim_has_upper_bound);
10709 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10711 static isl_bool has_bound(__isl_keep isl_map *map,
10712 enum isl_dim_type type, unsigned pos,
10713 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10714 enum isl_dim_type type, unsigned pos))
10716 int i;
10718 if (!map)
10719 return isl_bool_error;
10721 for (i = 0; i < map->n; ++i) {
10722 isl_bool bounded;
10723 bounded = fn(map->p[i], type, pos);
10724 if (bounded < 0 || !bounded)
10725 return bounded;
10728 return isl_bool_true;
10731 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10733 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10734 enum isl_dim_type type, unsigned pos)
10736 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10739 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10741 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10742 enum isl_dim_type type, unsigned pos)
10744 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10747 /* For each of the "n" variables starting at "first", determine
10748 * the sign of the variable and put the results in the first "n"
10749 * elements of the array "signs".
10750 * Sign
10751 * 1 means that the variable is non-negative
10752 * -1 means that the variable is non-positive
10753 * 0 means the variable attains both positive and negative values.
10755 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10756 unsigned first, unsigned n, int *signs)
10758 isl_vec *bound = NULL;
10759 struct isl_tab *tab = NULL;
10760 struct isl_tab_undo *snap;
10761 int i;
10763 if (!bset || !signs)
10764 return -1;
10766 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10767 tab = isl_tab_from_basic_set(bset, 0);
10768 if (!bound || !tab)
10769 goto error;
10771 isl_seq_clr(bound->el, bound->size);
10772 isl_int_set_si(bound->el[0], -1);
10774 snap = isl_tab_snap(tab);
10775 for (i = 0; i < n; ++i) {
10776 int empty;
10778 isl_int_set_si(bound->el[1 + first + i], -1);
10779 if (isl_tab_add_ineq(tab, bound->el) < 0)
10780 goto error;
10781 empty = tab->empty;
10782 isl_int_set_si(bound->el[1 + first + i], 0);
10783 if (isl_tab_rollback(tab, snap) < 0)
10784 goto error;
10786 if (empty) {
10787 signs[i] = 1;
10788 continue;
10791 isl_int_set_si(bound->el[1 + first + i], 1);
10792 if (isl_tab_add_ineq(tab, bound->el) < 0)
10793 goto error;
10794 empty = tab->empty;
10795 isl_int_set_si(bound->el[1 + first + i], 0);
10796 if (isl_tab_rollback(tab, snap) < 0)
10797 goto error;
10799 signs[i] = empty ? -1 : 0;
10802 isl_tab_free(tab);
10803 isl_vec_free(bound);
10804 return 0;
10805 error:
10806 isl_tab_free(tab);
10807 isl_vec_free(bound);
10808 return -1;
10811 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10812 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10814 if (!bset || !signs)
10815 return -1;
10816 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10817 return -1);
10819 first += pos(bset->dim, type) - 1;
10820 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10823 /* Is it possible for the integer division "div" to depend (possibly
10824 * indirectly) on any output dimensions?
10826 * If the div is undefined, then we conservatively assume that it
10827 * may depend on them.
10828 * Otherwise, we check if it actually depends on them or on any integer
10829 * divisions that may depend on them.
10831 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10833 int i;
10834 unsigned n_out, o_out;
10835 unsigned n_div, o_div;
10837 if (isl_int_is_zero(bmap->div[div][0]))
10838 return isl_bool_true;
10840 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10841 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10843 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10844 return isl_bool_true;
10846 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10847 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10849 for (i = 0; i < n_div; ++i) {
10850 isl_bool may_involve;
10852 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10853 continue;
10854 may_involve = div_may_involve_output(bmap, i);
10855 if (may_involve < 0 || may_involve)
10856 return may_involve;
10859 return isl_bool_false;
10862 /* Return the first integer division of "bmap" in the range
10863 * [first, first + n[ that may depend on any output dimensions and
10864 * that has a non-zero coefficient in "c" (where the first coefficient
10865 * in "c" corresponds to integer division "first").
10867 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10868 isl_int *c, int first, int n)
10870 int k;
10872 if (!bmap)
10873 return -1;
10875 for (k = first; k < first + n; ++k) {
10876 isl_bool may_involve;
10878 if (isl_int_is_zero(c[k]))
10879 continue;
10880 may_involve = div_may_involve_output(bmap, k);
10881 if (may_involve < 0)
10882 return -1;
10883 if (may_involve)
10884 return k;
10887 return first + n;
10890 /* Look for a pair of inequality constraints in "bmap" of the form
10892 * -l + i >= 0 or i >= l
10893 * and
10894 * n + l - i >= 0 or i <= l + n
10896 * with n < "m" and i the output dimension at position "pos".
10897 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10898 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10899 * and earlier output dimensions, as well as integer divisions that do
10900 * not involve any of the output dimensions.
10902 * Return the index of the first inequality constraint or bmap->n_ineq
10903 * if no such pair can be found.
10905 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10906 int pos, isl_int m)
10908 int i, j;
10909 isl_ctx *ctx;
10910 unsigned total;
10911 unsigned n_div, o_div;
10912 unsigned n_out, o_out;
10913 int less;
10915 if (!bmap)
10916 return -1;
10918 ctx = isl_basic_map_get_ctx(bmap);
10919 total = isl_basic_map_total_dim(bmap);
10920 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10921 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10922 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10923 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10924 for (i = 0; i < bmap->n_ineq; ++i) {
10925 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10926 continue;
10927 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10928 n_out - (pos + 1)) != -1)
10929 continue;
10930 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10931 0, n_div) < n_div)
10932 continue;
10933 for (j = i + 1; j < bmap->n_ineq; ++j) {
10934 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10935 ctx->one))
10936 continue;
10937 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10938 bmap->ineq[j] + 1, total))
10939 continue;
10940 break;
10942 if (j >= bmap->n_ineq)
10943 continue;
10944 isl_int_add(bmap->ineq[i][0],
10945 bmap->ineq[i][0], bmap->ineq[j][0]);
10946 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10947 isl_int_sub(bmap->ineq[i][0],
10948 bmap->ineq[i][0], bmap->ineq[j][0]);
10949 if (!less)
10950 continue;
10951 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10952 return i;
10953 else
10954 return j;
10957 return bmap->n_ineq;
10960 /* Return the index of the equality of "bmap" that defines
10961 * the output dimension "pos" in terms of earlier dimensions.
10962 * The equality may also involve integer divisions, as long
10963 * as those integer divisions are defined in terms of
10964 * parameters or input dimensions.
10965 * In this case, *div is set to the number of integer divisions and
10966 * *ineq is set to the number of inequality constraints (provided
10967 * div and ineq are not NULL).
10969 * The equality may also involve a single integer division involving
10970 * the output dimensions (typically only output dimension "pos") as
10971 * long as the coefficient of output dimension "pos" is 1 or -1 and
10972 * there is a pair of constraints i >= l and i <= l + n, with i referring
10973 * to output dimension "pos", l an expression involving only earlier
10974 * dimensions and n smaller than the coefficient of the integer division
10975 * in the equality. In this case, the output dimension can be defined
10976 * in terms of a modulo expression that does not involve the integer division.
10977 * *div is then set to this single integer division and
10978 * *ineq is set to the index of constraint i >= l.
10980 * Return bmap->n_eq if there is no such equality.
10981 * Return -1 on error.
10983 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10984 int pos, int *div, int *ineq)
10986 int j, k, l;
10987 unsigned n_out, o_out;
10988 unsigned n_div, o_div;
10990 if (!bmap)
10991 return -1;
10993 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10994 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10995 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10996 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10998 if (ineq)
10999 *ineq = bmap->n_ineq;
11000 if (div)
11001 *div = n_div;
11002 for (j = 0; j < bmap->n_eq; ++j) {
11003 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11004 continue;
11005 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11006 n_out - (pos + 1)) != -1)
11007 continue;
11008 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11009 0, n_div);
11010 if (k >= n_div)
11011 return j;
11012 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11013 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11014 continue;
11015 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11016 k + 1, n_div - (k+1)) < n_div)
11017 continue;
11018 l = find_modulo_constraint_pair(bmap, pos,
11019 bmap->eq[j][o_div + k]);
11020 if (l < 0)
11021 return -1;
11022 if (l >= bmap->n_ineq)
11023 continue;
11024 if (div)
11025 *div = k;
11026 if (ineq)
11027 *ineq = l;
11028 return j;
11031 return bmap->n_eq;
11034 /* Check if the given basic map is obviously single-valued.
11035 * In particular, for each output dimension, check that there is
11036 * an equality that defines the output dimension in terms of
11037 * earlier dimensions.
11039 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11041 int i;
11042 unsigned n_out;
11044 if (!bmap)
11045 return isl_bool_error;
11047 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11049 for (i = 0; i < n_out; ++i) {
11050 int eq;
11052 eq = isl_basic_map_output_defining_equality(bmap, i,
11053 NULL, NULL);
11054 if (eq < 0)
11055 return isl_bool_error;
11056 if (eq >= bmap->n_eq)
11057 return isl_bool_false;
11060 return isl_bool_true;
11063 /* Check if the given basic map is single-valued.
11064 * We simply compute
11066 * M \circ M^-1
11068 * and check if the result is a subset of the identity mapping.
11070 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11072 isl_space *space;
11073 isl_basic_map *test;
11074 isl_basic_map *id;
11075 isl_bool sv;
11077 sv = isl_basic_map_plain_is_single_valued(bmap);
11078 if (sv < 0 || sv)
11079 return sv;
11081 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11082 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11084 space = isl_basic_map_get_space(bmap);
11085 space = isl_space_map_from_set(isl_space_range(space));
11086 id = isl_basic_map_identity(space);
11088 sv = isl_basic_map_is_subset(test, id);
11090 isl_basic_map_free(test);
11091 isl_basic_map_free(id);
11093 return sv;
11096 /* Check if the given map is obviously single-valued.
11098 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11100 if (!map)
11101 return isl_bool_error;
11102 if (map->n == 0)
11103 return isl_bool_true;
11104 if (map->n >= 2)
11105 return isl_bool_false;
11107 return isl_basic_map_plain_is_single_valued(map->p[0]);
11110 /* Check if the given map is single-valued.
11111 * We simply compute
11113 * M \circ M^-1
11115 * and check if the result is a subset of the identity mapping.
11117 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11119 isl_space *dim;
11120 isl_map *test;
11121 isl_map *id;
11122 isl_bool sv;
11124 sv = isl_map_plain_is_single_valued(map);
11125 if (sv < 0 || sv)
11126 return sv;
11128 test = isl_map_reverse(isl_map_copy(map));
11129 test = isl_map_apply_range(test, isl_map_copy(map));
11131 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11132 id = isl_map_identity(dim);
11134 sv = isl_map_is_subset(test, id);
11136 isl_map_free(test);
11137 isl_map_free(id);
11139 return sv;
11142 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11144 isl_bool in;
11146 map = isl_map_copy(map);
11147 map = isl_map_reverse(map);
11148 in = isl_map_is_single_valued(map);
11149 isl_map_free(map);
11151 return in;
11154 /* Check if the given map is obviously injective.
11156 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11158 isl_bool in;
11160 map = isl_map_copy(map);
11161 map = isl_map_reverse(map);
11162 in = isl_map_plain_is_single_valued(map);
11163 isl_map_free(map);
11165 return in;
11168 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11170 isl_bool sv;
11172 sv = isl_map_is_single_valued(map);
11173 if (sv < 0 || !sv)
11174 return sv;
11176 return isl_map_is_injective(map);
11179 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11181 return isl_map_is_single_valued(set_to_map(set));
11184 /* Does "map" only map elements to themselves?
11186 * If the domain and range spaces are different, then "map"
11187 * is considered not to be an identity relation, even if it is empty.
11188 * Otherwise, construct the maximal identity relation and
11189 * check whether "map" is a subset of this relation.
11191 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11193 isl_space *space;
11194 isl_map *id;
11195 isl_bool equal, is_identity;
11197 space = isl_map_get_space(map);
11198 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11199 isl_space_free(space);
11200 if (equal < 0 || !equal)
11201 return equal;
11203 id = isl_map_identity(isl_map_get_space(map));
11204 is_identity = isl_map_is_subset(map, id);
11205 isl_map_free(id);
11207 return is_identity;
11210 int isl_map_is_translation(__isl_keep isl_map *map)
11212 int ok;
11213 isl_set *delta;
11215 delta = isl_map_deltas(isl_map_copy(map));
11216 ok = isl_set_is_singleton(delta);
11217 isl_set_free(delta);
11219 return ok;
11222 static int unique(isl_int *p, unsigned pos, unsigned len)
11224 if (isl_seq_first_non_zero(p, pos) != -1)
11225 return 0;
11226 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11227 return 0;
11228 return 1;
11231 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11233 int i, j;
11234 unsigned nvar;
11235 unsigned ovar;
11237 if (!bset)
11238 return -1;
11240 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11241 return 0;
11243 nvar = isl_basic_set_dim(bset, isl_dim_set);
11244 ovar = isl_space_offset(bset->dim, isl_dim_set);
11245 for (j = 0; j < nvar; ++j) {
11246 int lower = 0, upper = 0;
11247 for (i = 0; i < bset->n_eq; ++i) {
11248 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11249 continue;
11250 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11251 return 0;
11252 break;
11254 if (i < bset->n_eq)
11255 continue;
11256 for (i = 0; i < bset->n_ineq; ++i) {
11257 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11258 continue;
11259 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11260 return 0;
11261 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11262 lower = 1;
11263 else
11264 upper = 1;
11266 if (!lower || !upper)
11267 return 0;
11270 return 1;
11273 int isl_set_is_box(__isl_keep isl_set *set)
11275 if (!set)
11276 return -1;
11277 if (set->n != 1)
11278 return 0;
11280 return isl_basic_set_is_box(set->p[0]);
11283 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11285 if (!bset)
11286 return isl_bool_error;
11288 return isl_space_is_wrapping(bset->dim);
11291 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11293 if (!set)
11294 return isl_bool_error;
11296 return isl_space_is_wrapping(set->dim);
11299 /* Modify the space of "map" through a call to "change".
11300 * If "can_change" is set (not NULL), then first call it to check
11301 * if the modification is allowed, printing the error message "cannot_change"
11302 * if it is not.
11304 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11305 isl_bool (*can_change)(__isl_keep isl_map *map),
11306 const char *cannot_change,
11307 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11309 isl_bool ok;
11310 isl_space *space;
11312 if (!map)
11313 return NULL;
11315 ok = can_change ? can_change(map) : isl_bool_true;
11316 if (ok < 0)
11317 return isl_map_free(map);
11318 if (!ok)
11319 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11320 return isl_map_free(map));
11322 space = change(isl_map_get_space(map));
11323 map = isl_map_reset_space(map, space);
11325 return map;
11328 /* Is the domain of "map" a wrapped relation?
11330 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11332 if (!map)
11333 return isl_bool_error;
11335 return isl_space_domain_is_wrapping(map->dim);
11338 /* Is the range of "map" a wrapped relation?
11340 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11342 if (!map)
11343 return isl_bool_error;
11345 return isl_space_range_is_wrapping(map->dim);
11348 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11350 bmap = isl_basic_map_cow(bmap);
11351 if (!bmap)
11352 return NULL;
11354 bmap->dim = isl_space_wrap(bmap->dim);
11355 if (!bmap->dim)
11356 goto error;
11358 bmap = isl_basic_map_finalize(bmap);
11360 return bset_from_bmap(bmap);
11361 error:
11362 isl_basic_map_free(bmap);
11363 return NULL;
11366 /* Given a map A -> B, return the set (A -> B).
11368 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11370 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11373 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11375 bset = isl_basic_set_cow(bset);
11376 if (!bset)
11377 return NULL;
11379 bset->dim = isl_space_unwrap(bset->dim);
11380 if (!bset->dim)
11381 goto error;
11383 bset = isl_basic_set_finalize(bset);
11385 return bset_to_bmap(bset);
11386 error:
11387 isl_basic_set_free(bset);
11388 return NULL;
11391 /* Given a set (A -> B), return the map A -> B.
11392 * Error out if "set" is not of the form (A -> B).
11394 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11396 return isl_map_change_space(set, &isl_set_is_wrapping,
11397 "not a wrapping set", &isl_space_unwrap);
11400 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11401 enum isl_dim_type type)
11403 if (!bmap)
11404 return NULL;
11406 if (!isl_space_is_named_or_nested(bmap->dim, type))
11407 return bmap;
11409 bmap = isl_basic_map_cow(bmap);
11410 if (!bmap)
11411 return NULL;
11413 bmap->dim = isl_space_reset(bmap->dim, type);
11414 if (!bmap->dim)
11415 goto error;
11417 bmap = isl_basic_map_finalize(bmap);
11419 return bmap;
11420 error:
11421 isl_basic_map_free(bmap);
11422 return NULL;
11425 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11426 enum isl_dim_type type)
11428 int i;
11430 if (!map)
11431 return NULL;
11433 if (!isl_space_is_named_or_nested(map->dim, type))
11434 return map;
11436 map = isl_map_cow(map);
11437 if (!map)
11438 return NULL;
11440 for (i = 0; i < map->n; ++i) {
11441 map->p[i] = isl_basic_map_reset(map->p[i], type);
11442 if (!map->p[i])
11443 goto error;
11445 map->dim = isl_space_reset(map->dim, type);
11446 if (!map->dim)
11447 goto error;
11449 return map;
11450 error:
11451 isl_map_free(map);
11452 return NULL;
11455 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11457 if (!bmap)
11458 return NULL;
11460 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11461 return bmap;
11463 bmap = isl_basic_map_cow(bmap);
11464 if (!bmap)
11465 return NULL;
11467 bmap->dim = isl_space_flatten(bmap->dim);
11468 if (!bmap->dim)
11469 goto error;
11471 bmap = isl_basic_map_finalize(bmap);
11473 return bmap;
11474 error:
11475 isl_basic_map_free(bmap);
11476 return NULL;
11479 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11481 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11484 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11485 __isl_take isl_basic_map *bmap)
11487 if (!bmap)
11488 return NULL;
11490 if (!bmap->dim->nested[0])
11491 return bmap;
11493 bmap = isl_basic_map_cow(bmap);
11494 if (!bmap)
11495 return NULL;
11497 bmap->dim = isl_space_flatten_domain(bmap->dim);
11498 if (!bmap->dim)
11499 goto error;
11501 bmap = isl_basic_map_finalize(bmap);
11503 return bmap;
11504 error:
11505 isl_basic_map_free(bmap);
11506 return NULL;
11509 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11510 __isl_take isl_basic_map *bmap)
11512 if (!bmap)
11513 return NULL;
11515 if (!bmap->dim->nested[1])
11516 return bmap;
11518 bmap = isl_basic_map_cow(bmap);
11519 if (!bmap)
11520 return NULL;
11522 bmap->dim = isl_space_flatten_range(bmap->dim);
11523 if (!bmap->dim)
11524 goto error;
11526 bmap = isl_basic_map_finalize(bmap);
11528 return bmap;
11529 error:
11530 isl_basic_map_free(bmap);
11531 return NULL;
11534 /* Remove any internal structure from the spaces of domain and range of "map".
11536 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11538 if (!map)
11539 return NULL;
11541 if (!map->dim->nested[0] && !map->dim->nested[1])
11542 return map;
11544 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11547 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11549 return set_from_map(isl_map_flatten(set_to_map(set)));
11552 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11554 isl_space *dim, *flat_dim;
11555 isl_map *map;
11557 dim = isl_set_get_space(set);
11558 flat_dim = isl_space_flatten(isl_space_copy(dim));
11559 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11560 map = isl_map_intersect_domain(map, set);
11562 return map;
11565 /* Remove any internal structure from the space of the domain of "map".
11567 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11569 if (!map)
11570 return NULL;
11572 if (!map->dim->nested[0])
11573 return map;
11575 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11578 /* Remove any internal structure from the space of the range of "map".
11580 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11582 if (!map)
11583 return NULL;
11585 if (!map->dim->nested[1])
11586 return map;
11588 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11591 /* Reorder the dimensions of "bmap" according to the given dim_map
11592 * and set the dimension specification to "dim" and
11593 * perform Gaussian elimination on the result.
11595 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11596 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11598 isl_basic_map *res;
11599 unsigned flags;
11601 bmap = isl_basic_map_cow(bmap);
11602 if (!bmap || !dim || !dim_map)
11603 goto error;
11605 flags = bmap->flags;
11606 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11607 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11608 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11609 res = isl_basic_map_alloc_space(dim,
11610 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11611 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11612 if (res)
11613 res->flags = flags;
11614 res = isl_basic_map_gauss(res, NULL);
11615 res = isl_basic_map_finalize(res);
11616 return res;
11617 error:
11618 free(dim_map);
11619 isl_basic_map_free(bmap);
11620 isl_space_free(dim);
11621 return NULL;
11624 /* Reorder the dimensions of "map" according to given reordering.
11626 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11627 __isl_take isl_reordering *r)
11629 int i;
11630 struct isl_dim_map *dim_map;
11632 map = isl_map_cow(map);
11633 dim_map = isl_dim_map_from_reordering(r);
11634 if (!map || !r || !dim_map)
11635 goto error;
11637 for (i = 0; i < map->n; ++i) {
11638 struct isl_dim_map *dim_map_i;
11640 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11642 map->p[i] = isl_basic_map_realign(map->p[i],
11643 isl_space_copy(r->dim), dim_map_i);
11645 if (!map->p[i])
11646 goto error;
11649 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11651 isl_reordering_free(r);
11652 free(dim_map);
11653 return map;
11654 error:
11655 free(dim_map);
11656 isl_map_free(map);
11657 isl_reordering_free(r);
11658 return NULL;
11661 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11662 __isl_take isl_reordering *r)
11664 return set_from_map(isl_map_realign(set_to_map(set), r));
11667 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11668 __isl_take isl_space *model)
11670 isl_ctx *ctx;
11672 if (!map || !model)
11673 goto error;
11675 ctx = isl_space_get_ctx(model);
11676 if (!isl_space_has_named_params(model))
11677 isl_die(ctx, isl_error_invalid,
11678 "model has unnamed parameters", goto error);
11679 if (!isl_space_has_named_params(map->dim))
11680 isl_die(ctx, isl_error_invalid,
11681 "relation has unnamed parameters", goto error);
11682 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11683 isl_reordering *exp;
11685 model = isl_space_drop_dims(model, isl_dim_in,
11686 0, isl_space_dim(model, isl_dim_in));
11687 model = isl_space_drop_dims(model, isl_dim_out,
11688 0, isl_space_dim(model, isl_dim_out));
11689 exp = isl_parameter_alignment_reordering(map->dim, model);
11690 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11691 map = isl_map_realign(map, exp);
11694 isl_space_free(model);
11695 return map;
11696 error:
11697 isl_space_free(model);
11698 isl_map_free(map);
11699 return NULL;
11702 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11703 __isl_take isl_space *model)
11705 return isl_map_align_params(set, model);
11708 /* Align the parameters of "bmap" to those of "model", introducing
11709 * additional parameters if needed.
11711 __isl_give isl_basic_map *isl_basic_map_align_params(
11712 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11714 isl_ctx *ctx;
11716 if (!bmap || !model)
11717 goto error;
11719 ctx = isl_space_get_ctx(model);
11720 if (!isl_space_has_named_params(model))
11721 isl_die(ctx, isl_error_invalid,
11722 "model has unnamed parameters", goto error);
11723 if (!isl_space_has_named_params(bmap->dim))
11724 isl_die(ctx, isl_error_invalid,
11725 "relation has unnamed parameters", goto error);
11726 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11727 isl_reordering *exp;
11728 struct isl_dim_map *dim_map;
11730 model = isl_space_drop_dims(model, isl_dim_in,
11731 0, isl_space_dim(model, isl_dim_in));
11732 model = isl_space_drop_dims(model, isl_dim_out,
11733 0, isl_space_dim(model, isl_dim_out));
11734 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11735 exp = isl_reordering_extend_space(exp,
11736 isl_basic_map_get_space(bmap));
11737 dim_map = isl_dim_map_from_reordering(exp);
11738 bmap = isl_basic_map_realign(bmap,
11739 exp ? isl_space_copy(exp->dim) : NULL,
11740 isl_dim_map_extend(dim_map, bmap));
11741 isl_reordering_free(exp);
11742 free(dim_map);
11745 isl_space_free(model);
11746 return bmap;
11747 error:
11748 isl_space_free(model);
11749 isl_basic_map_free(bmap);
11750 return NULL;
11753 /* Align the parameters of "bset" to those of "model", introducing
11754 * additional parameters if needed.
11756 __isl_give isl_basic_set *isl_basic_set_align_params(
11757 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11759 return isl_basic_map_align_params(bset, model);
11762 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11763 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11764 enum isl_dim_type c2, enum isl_dim_type c3,
11765 enum isl_dim_type c4, enum isl_dim_type c5)
11767 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11768 struct isl_mat *mat;
11769 int i, j, k;
11770 int pos;
11772 if (!bmap)
11773 return NULL;
11774 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11775 isl_basic_map_total_dim(bmap) + 1);
11776 if (!mat)
11777 return NULL;
11778 for (i = 0; i < bmap->n_eq; ++i)
11779 for (j = 0, pos = 0; j < 5; ++j) {
11780 int off = isl_basic_map_offset(bmap, c[j]);
11781 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11782 isl_int_set(mat->row[i][pos],
11783 bmap->eq[i][off + k]);
11784 ++pos;
11788 return mat;
11791 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11792 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11793 enum isl_dim_type c2, enum isl_dim_type c3,
11794 enum isl_dim_type c4, enum isl_dim_type c5)
11796 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11797 struct isl_mat *mat;
11798 int i, j, k;
11799 int pos;
11801 if (!bmap)
11802 return NULL;
11803 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11804 isl_basic_map_total_dim(bmap) + 1);
11805 if (!mat)
11806 return NULL;
11807 for (i = 0; i < bmap->n_ineq; ++i)
11808 for (j = 0, pos = 0; j < 5; ++j) {
11809 int off = isl_basic_map_offset(bmap, c[j]);
11810 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11811 isl_int_set(mat->row[i][pos],
11812 bmap->ineq[i][off + k]);
11813 ++pos;
11817 return mat;
11820 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11821 __isl_take isl_space *dim,
11822 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11823 enum isl_dim_type c2, enum isl_dim_type c3,
11824 enum isl_dim_type c4, enum isl_dim_type c5)
11826 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11827 isl_basic_map *bmap;
11828 unsigned total;
11829 unsigned extra;
11830 int i, j, k, l;
11831 int pos;
11833 if (!dim || !eq || !ineq)
11834 goto error;
11836 if (eq->n_col != ineq->n_col)
11837 isl_die(dim->ctx, isl_error_invalid,
11838 "equalities and inequalities matrices should have "
11839 "same number of columns", goto error);
11841 total = 1 + isl_space_dim(dim, isl_dim_all);
11843 if (eq->n_col < total)
11844 isl_die(dim->ctx, isl_error_invalid,
11845 "number of columns too small", goto error);
11847 extra = eq->n_col - total;
11849 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11850 eq->n_row, ineq->n_row);
11851 if (!bmap)
11852 goto error;
11853 for (i = 0; i < extra; ++i) {
11854 k = isl_basic_map_alloc_div(bmap);
11855 if (k < 0)
11856 goto error;
11857 isl_int_set_si(bmap->div[k][0], 0);
11859 for (i = 0; i < eq->n_row; ++i) {
11860 l = isl_basic_map_alloc_equality(bmap);
11861 if (l < 0)
11862 goto error;
11863 for (j = 0, pos = 0; j < 5; ++j) {
11864 int off = isl_basic_map_offset(bmap, c[j]);
11865 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11866 isl_int_set(bmap->eq[l][off + k],
11867 eq->row[i][pos]);
11868 ++pos;
11872 for (i = 0; i < ineq->n_row; ++i) {
11873 l = isl_basic_map_alloc_inequality(bmap);
11874 if (l < 0)
11875 goto error;
11876 for (j = 0, pos = 0; j < 5; ++j) {
11877 int off = isl_basic_map_offset(bmap, c[j]);
11878 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11879 isl_int_set(bmap->ineq[l][off + k],
11880 ineq->row[i][pos]);
11881 ++pos;
11886 isl_space_free(dim);
11887 isl_mat_free(eq);
11888 isl_mat_free(ineq);
11890 bmap = isl_basic_map_simplify(bmap);
11891 return isl_basic_map_finalize(bmap);
11892 error:
11893 isl_space_free(dim);
11894 isl_mat_free(eq);
11895 isl_mat_free(ineq);
11896 return NULL;
11899 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11900 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11901 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11903 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11904 c1, c2, c3, c4, isl_dim_in);
11907 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11908 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11909 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11911 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11912 c1, c2, c3, c4, isl_dim_in);
11915 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11916 __isl_take isl_space *dim,
11917 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11918 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11920 isl_basic_map *bmap;
11921 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11922 c1, c2, c3, c4, isl_dim_in);
11923 return bset_from_bmap(bmap);
11926 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11928 if (!bmap)
11929 return isl_bool_error;
11931 return isl_space_can_zip(bmap->dim);
11934 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11936 if (!map)
11937 return isl_bool_error;
11939 return isl_space_can_zip(map->dim);
11942 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11943 * (A -> C) -> (B -> D).
11945 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11947 unsigned pos;
11948 unsigned n1;
11949 unsigned n2;
11951 if (!bmap)
11952 return NULL;
11954 if (!isl_basic_map_can_zip(bmap))
11955 isl_die(bmap->ctx, isl_error_invalid,
11956 "basic map cannot be zipped", goto error);
11957 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11958 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11959 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11960 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11961 bmap = isl_basic_map_cow(bmap);
11962 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11963 if (!bmap)
11964 return NULL;
11965 bmap->dim = isl_space_zip(bmap->dim);
11966 if (!bmap->dim)
11967 goto error;
11968 bmap = isl_basic_map_mark_final(bmap);
11969 return bmap;
11970 error:
11971 isl_basic_map_free(bmap);
11972 return NULL;
11975 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11976 * (A -> C) -> (B -> D).
11978 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11980 int i;
11982 if (!map)
11983 return NULL;
11985 if (!isl_map_can_zip(map))
11986 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11987 goto error);
11989 map = isl_map_cow(map);
11990 if (!map)
11991 return NULL;
11993 for (i = 0; i < map->n; ++i) {
11994 map->p[i] = isl_basic_map_zip(map->p[i]);
11995 if (!map->p[i])
11996 goto error;
11999 map->dim = isl_space_zip(map->dim);
12000 if (!map->dim)
12001 goto error;
12003 return map;
12004 error:
12005 isl_map_free(map);
12006 return NULL;
12009 /* Can we apply isl_basic_map_curry to "bmap"?
12010 * That is, does it have a nested relation in its domain?
12012 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12014 if (!bmap)
12015 return isl_bool_error;
12017 return isl_space_can_curry(bmap->dim);
12020 /* Can we apply isl_map_curry to "map"?
12021 * That is, does it have a nested relation in its domain?
12023 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12025 if (!map)
12026 return isl_bool_error;
12028 return isl_space_can_curry(map->dim);
12031 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12032 * A -> (B -> C).
12034 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12037 if (!bmap)
12038 return NULL;
12040 if (!isl_basic_map_can_curry(bmap))
12041 isl_die(bmap->ctx, isl_error_invalid,
12042 "basic map cannot be curried", goto error);
12043 bmap = isl_basic_map_cow(bmap);
12044 if (!bmap)
12045 return NULL;
12046 bmap->dim = isl_space_curry(bmap->dim);
12047 if (!bmap->dim)
12048 goto error;
12049 bmap = isl_basic_map_mark_final(bmap);
12050 return bmap;
12051 error:
12052 isl_basic_map_free(bmap);
12053 return NULL;
12056 /* Given a map (A -> B) -> C, return the corresponding map
12057 * A -> (B -> C).
12059 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12061 return isl_map_change_space(map, &isl_map_can_curry,
12062 "map cannot be curried", &isl_space_curry);
12065 /* Can isl_map_range_curry be applied to "map"?
12066 * That is, does it have a nested relation in its range,
12067 * the domain of which is itself a nested relation?
12069 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12071 if (!map)
12072 return isl_bool_error;
12074 return isl_space_can_range_curry(map->dim);
12077 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12078 * A -> (B -> (C -> D)).
12080 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12082 return isl_map_change_space(map, &isl_map_can_range_curry,
12083 "map range cannot be curried",
12084 &isl_space_range_curry);
12087 /* Can we apply isl_basic_map_uncurry to "bmap"?
12088 * That is, does it have a nested relation in its domain?
12090 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12092 if (!bmap)
12093 return isl_bool_error;
12095 return isl_space_can_uncurry(bmap->dim);
12098 /* Can we apply isl_map_uncurry to "map"?
12099 * That is, does it have a nested relation in its domain?
12101 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12103 if (!map)
12104 return isl_bool_error;
12106 return isl_space_can_uncurry(map->dim);
12109 /* Given a basic map A -> (B -> C), return the corresponding basic map
12110 * (A -> B) -> C.
12112 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12115 if (!bmap)
12116 return NULL;
12118 if (!isl_basic_map_can_uncurry(bmap))
12119 isl_die(bmap->ctx, isl_error_invalid,
12120 "basic map cannot be uncurried",
12121 return isl_basic_map_free(bmap));
12122 bmap = isl_basic_map_cow(bmap);
12123 if (!bmap)
12124 return NULL;
12125 bmap->dim = isl_space_uncurry(bmap->dim);
12126 if (!bmap->dim)
12127 return isl_basic_map_free(bmap);
12128 bmap = isl_basic_map_mark_final(bmap);
12129 return bmap;
12132 /* Given a map A -> (B -> C), return the corresponding map
12133 * (A -> B) -> C.
12135 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12137 return isl_map_change_space(map, &isl_map_can_uncurry,
12138 "map cannot be uncurried", &isl_space_uncurry);
12141 /* Construct a basic map mapping the domain of the affine expression
12142 * to a one-dimensional range prescribed by the affine expression.
12143 * If "rational" is set, then construct a rational basic map.
12145 * A NaN affine expression cannot be converted to a basic map.
12147 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
12148 __isl_take isl_aff *aff, int rational)
12150 int k;
12151 int pos;
12152 isl_bool is_nan;
12153 isl_local_space *ls;
12154 isl_basic_map *bmap = NULL;
12156 if (!aff)
12157 return NULL;
12158 is_nan = isl_aff_is_nan(aff);
12159 if (is_nan < 0)
12160 goto error;
12161 if (is_nan)
12162 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
12163 "cannot convert NaN", goto error);
12165 ls = isl_aff_get_local_space(aff);
12166 bmap = isl_basic_map_from_local_space(ls);
12167 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
12168 k = isl_basic_map_alloc_equality(bmap);
12169 if (k < 0)
12170 goto error;
12172 pos = isl_basic_map_offset(bmap, isl_dim_out);
12173 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
12174 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
12175 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
12176 aff->v->size - (pos + 1));
12178 isl_aff_free(aff);
12179 if (rational)
12180 bmap = isl_basic_map_set_rational(bmap);
12181 bmap = isl_basic_map_finalize(bmap);
12182 return bmap;
12183 error:
12184 isl_aff_free(aff);
12185 isl_basic_map_free(bmap);
12186 return NULL;
12189 /* Construct a basic map mapping the domain of the affine expression
12190 * to a one-dimensional range prescribed by the affine expression.
12192 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
12194 return isl_basic_map_from_aff2(aff, 0);
12197 /* Construct a map mapping the domain of the affine expression
12198 * to a one-dimensional range prescribed by the affine expression.
12200 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
12202 isl_basic_map *bmap;
12204 bmap = isl_basic_map_from_aff(aff);
12205 return isl_map_from_basic_map(bmap);
12208 /* Construct a basic map mapping the domain the multi-affine expression
12209 * to its range, with each dimension in the range equated to the
12210 * corresponding affine expression.
12211 * If "rational" is set, then construct a rational basic map.
12213 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
12214 __isl_take isl_multi_aff *maff, int rational)
12216 int i;
12217 isl_space *space;
12218 isl_basic_map *bmap;
12220 if (!maff)
12221 return NULL;
12223 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
12224 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
12225 "invalid space", goto error);
12227 space = isl_space_domain(isl_multi_aff_get_space(maff));
12228 bmap = isl_basic_map_universe(isl_space_from_domain(space));
12229 if (rational)
12230 bmap = isl_basic_map_set_rational(bmap);
12232 for (i = 0; i < maff->n; ++i) {
12233 isl_aff *aff;
12234 isl_basic_map *bmap_i;
12236 aff = isl_aff_copy(maff->p[i]);
12237 bmap_i = isl_basic_map_from_aff2(aff, rational);
12239 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12242 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
12244 isl_multi_aff_free(maff);
12245 return bmap;
12246 error:
12247 isl_multi_aff_free(maff);
12248 return NULL;
12251 /* Construct a basic map mapping the domain the multi-affine expression
12252 * to its range, with each dimension in the range equated to the
12253 * corresponding affine expression.
12255 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
12256 __isl_take isl_multi_aff *ma)
12258 return isl_basic_map_from_multi_aff2(ma, 0);
12261 /* Construct a map mapping the domain the multi-affine expression
12262 * to its range, with each dimension in the range equated to the
12263 * corresponding affine expression.
12265 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
12267 isl_basic_map *bmap;
12269 bmap = isl_basic_map_from_multi_aff(maff);
12270 return isl_map_from_basic_map(bmap);
12273 /* Construct a basic map mapping a domain in the given space to
12274 * to an n-dimensional range, with n the number of elements in the list,
12275 * where each coordinate in the range is prescribed by the
12276 * corresponding affine expression.
12277 * The domains of all affine expressions in the list are assumed to match
12278 * domain_dim.
12280 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
12281 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
12283 int i;
12284 isl_space *dim;
12285 isl_basic_map *bmap;
12287 if (!list)
12288 return NULL;
12290 dim = isl_space_from_domain(domain_dim);
12291 bmap = isl_basic_map_universe(dim);
12293 for (i = 0; i < list->n; ++i) {
12294 isl_aff *aff;
12295 isl_basic_map *bmap_i;
12297 aff = isl_aff_copy(list->p[i]);
12298 bmap_i = isl_basic_map_from_aff(aff);
12300 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12303 isl_aff_list_free(list);
12304 return bmap;
12307 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12308 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12310 return isl_map_equate(set, type1, pos1, type2, pos2);
12313 /* Construct a basic map where the given dimensions are equal to each other.
12315 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12316 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12318 isl_basic_map *bmap = NULL;
12319 int i;
12321 if (!space)
12322 return NULL;
12324 if (pos1 >= isl_space_dim(space, type1))
12325 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12326 "index out of bounds", goto error);
12327 if (pos2 >= isl_space_dim(space, type2))
12328 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12329 "index out of bounds", goto error);
12331 if (type1 == type2 && pos1 == pos2)
12332 return isl_basic_map_universe(space);
12334 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12335 i = isl_basic_map_alloc_equality(bmap);
12336 if (i < 0)
12337 goto error;
12338 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12339 pos1 += isl_basic_map_offset(bmap, type1);
12340 pos2 += isl_basic_map_offset(bmap, type2);
12341 isl_int_set_si(bmap->eq[i][pos1], -1);
12342 isl_int_set_si(bmap->eq[i][pos2], 1);
12343 bmap = isl_basic_map_finalize(bmap);
12344 isl_space_free(space);
12345 return bmap;
12346 error:
12347 isl_space_free(space);
12348 isl_basic_map_free(bmap);
12349 return NULL;
12352 /* Add a constraint imposing that the given two dimensions are equal.
12354 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12355 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12357 isl_basic_map *eq;
12359 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12361 bmap = isl_basic_map_intersect(bmap, eq);
12363 return bmap;
12366 /* Add a constraint imposing that the given two dimensions are equal.
12368 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12369 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12371 isl_basic_map *bmap;
12373 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12375 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12377 return map;
12380 /* Add a constraint imposing that the given two dimensions have opposite values.
12382 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12383 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12385 isl_basic_map *bmap = NULL;
12386 int i;
12388 if (!map)
12389 return NULL;
12391 if (pos1 >= isl_map_dim(map, type1))
12392 isl_die(map->ctx, isl_error_invalid,
12393 "index out of bounds", goto error);
12394 if (pos2 >= isl_map_dim(map, type2))
12395 isl_die(map->ctx, isl_error_invalid,
12396 "index out of bounds", goto error);
12398 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12399 i = isl_basic_map_alloc_equality(bmap);
12400 if (i < 0)
12401 goto error;
12402 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12403 pos1 += isl_basic_map_offset(bmap, type1);
12404 pos2 += isl_basic_map_offset(bmap, type2);
12405 isl_int_set_si(bmap->eq[i][pos1], 1);
12406 isl_int_set_si(bmap->eq[i][pos2], 1);
12407 bmap = isl_basic_map_finalize(bmap);
12409 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12411 return map;
12412 error:
12413 isl_basic_map_free(bmap);
12414 isl_map_free(map);
12415 return NULL;
12418 /* Construct a constraint imposing that the value of the first dimension is
12419 * greater than or equal to that of the second.
12421 static __isl_give isl_constraint *constraint_order_ge(
12422 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12423 enum isl_dim_type type2, int pos2)
12425 isl_constraint *c;
12427 if (!space)
12428 return NULL;
12430 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12432 if (pos1 >= isl_constraint_dim(c, type1))
12433 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12434 "index out of bounds", return isl_constraint_free(c));
12435 if (pos2 >= isl_constraint_dim(c, type2))
12436 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12437 "index out of bounds", return isl_constraint_free(c));
12439 if (type1 == type2 && pos1 == pos2)
12440 return c;
12442 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12443 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12445 return c;
12448 /* Add a constraint imposing that the value of the first dimension is
12449 * greater than or equal to that of the second.
12451 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12452 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12454 isl_constraint *c;
12455 isl_space *space;
12457 if (type1 == type2 && pos1 == pos2)
12458 return bmap;
12459 space = isl_basic_map_get_space(bmap);
12460 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12461 bmap = isl_basic_map_add_constraint(bmap, c);
12463 return bmap;
12466 /* Add a constraint imposing that the value of the first dimension is
12467 * greater than or equal to that of the second.
12469 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12470 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12472 isl_constraint *c;
12473 isl_space *space;
12475 if (type1 == type2 && pos1 == pos2)
12476 return map;
12477 space = isl_map_get_space(map);
12478 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12479 map = isl_map_add_constraint(map, c);
12481 return map;
12484 /* Add a constraint imposing that the value of the first dimension is
12485 * less than or equal to that of the second.
12487 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12488 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12490 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12493 /* Construct a basic map where the value of the first dimension is
12494 * greater than that of the second.
12496 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12497 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12499 isl_basic_map *bmap = NULL;
12500 int i;
12502 if (!space)
12503 return NULL;
12505 if (pos1 >= isl_space_dim(space, type1))
12506 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12507 "index out of bounds", goto error);
12508 if (pos2 >= isl_space_dim(space, type2))
12509 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12510 "index out of bounds", goto error);
12512 if (type1 == type2 && pos1 == pos2)
12513 return isl_basic_map_empty(space);
12515 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12516 i = isl_basic_map_alloc_inequality(bmap);
12517 if (i < 0)
12518 return isl_basic_map_free(bmap);
12519 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12520 pos1 += isl_basic_map_offset(bmap, type1);
12521 pos2 += isl_basic_map_offset(bmap, type2);
12522 isl_int_set_si(bmap->ineq[i][pos1], 1);
12523 isl_int_set_si(bmap->ineq[i][pos2], -1);
12524 isl_int_set_si(bmap->ineq[i][0], -1);
12525 bmap = isl_basic_map_finalize(bmap);
12527 return bmap;
12528 error:
12529 isl_space_free(space);
12530 isl_basic_map_free(bmap);
12531 return NULL;
12534 /* Add a constraint imposing that the value of the first dimension is
12535 * greater than that of the second.
12537 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12538 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12540 isl_basic_map *gt;
12542 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12544 bmap = isl_basic_map_intersect(bmap, gt);
12546 return bmap;
12549 /* Add a constraint imposing that the value of the first dimension is
12550 * greater than that of the second.
12552 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12553 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12555 isl_basic_map *bmap;
12557 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12559 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12561 return map;
12564 /* Add a constraint imposing that the value of the first dimension is
12565 * smaller than that of the second.
12567 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12568 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12570 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12573 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12574 int pos)
12576 isl_aff *div;
12577 isl_local_space *ls;
12579 if (!bmap)
12580 return NULL;
12582 if (!isl_basic_map_divs_known(bmap))
12583 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12584 "some divs are unknown", return NULL);
12586 ls = isl_basic_map_get_local_space(bmap);
12587 div = isl_local_space_get_div(ls, pos);
12588 isl_local_space_free(ls);
12590 return div;
12593 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12594 int pos)
12596 return isl_basic_map_get_div(bset, pos);
12599 /* Plug in "subs" for dimension "type", "pos" of "bset".
12601 * Let i be the dimension to replace and let "subs" be of the form
12603 * f/d
12605 * Any integer division with a non-zero coefficient for i,
12607 * floor((a i + g)/m)
12609 * is replaced by
12611 * floor((a f + d g)/(m d))
12613 * Constraints of the form
12615 * a i + g
12617 * are replaced by
12619 * a f + d g
12621 * We currently require that "subs" is an integral expression.
12622 * Handling rational expressions may require us to add stride constraints
12623 * as we do in isl_basic_set_preimage_multi_aff.
12625 __isl_give isl_basic_set *isl_basic_set_substitute(
12626 __isl_take isl_basic_set *bset,
12627 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12629 int i;
12630 isl_int v;
12631 isl_ctx *ctx;
12633 if (bset && isl_basic_set_plain_is_empty(bset))
12634 return bset;
12636 bset = isl_basic_set_cow(bset);
12637 if (!bset || !subs)
12638 goto error;
12640 ctx = isl_basic_set_get_ctx(bset);
12641 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12642 isl_die(ctx, isl_error_invalid,
12643 "spaces don't match", goto error);
12644 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12645 isl_die(ctx, isl_error_unsupported,
12646 "cannot handle divs yet", goto error);
12647 if (!isl_int_is_one(subs->v->el[0]))
12648 isl_die(ctx, isl_error_invalid,
12649 "can only substitute integer expressions", goto error);
12651 pos += isl_basic_set_offset(bset, type);
12653 isl_int_init(v);
12655 for (i = 0; i < bset->n_eq; ++i) {
12656 if (isl_int_is_zero(bset->eq[i][pos]))
12657 continue;
12658 isl_int_set(v, bset->eq[i][pos]);
12659 isl_int_set_si(bset->eq[i][pos], 0);
12660 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12661 v, subs->v->el + 1, subs->v->size - 1);
12664 for (i = 0; i < bset->n_ineq; ++i) {
12665 if (isl_int_is_zero(bset->ineq[i][pos]))
12666 continue;
12667 isl_int_set(v, bset->ineq[i][pos]);
12668 isl_int_set_si(bset->ineq[i][pos], 0);
12669 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12670 v, subs->v->el + 1, subs->v->size - 1);
12673 for (i = 0; i < bset->n_div; ++i) {
12674 if (isl_int_is_zero(bset->div[i][1 + pos]))
12675 continue;
12676 isl_int_set(v, bset->div[i][1 + pos]);
12677 isl_int_set_si(bset->div[i][1 + pos], 0);
12678 isl_seq_combine(bset->div[i] + 1,
12679 subs->v->el[0], bset->div[i] + 1,
12680 v, subs->v->el + 1, subs->v->size - 1);
12681 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12684 isl_int_clear(v);
12686 bset = isl_basic_set_simplify(bset);
12687 return isl_basic_set_finalize(bset);
12688 error:
12689 isl_basic_set_free(bset);
12690 return NULL;
12693 /* Plug in "subs" for dimension "type", "pos" of "set".
12695 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12696 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12698 int i;
12700 if (set && isl_set_plain_is_empty(set))
12701 return set;
12703 set = isl_set_cow(set);
12704 if (!set || !subs)
12705 goto error;
12707 for (i = set->n - 1; i >= 0; --i) {
12708 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12709 if (remove_if_empty(set, i) < 0)
12710 goto error;
12713 return set;
12714 error:
12715 isl_set_free(set);
12716 return NULL;
12719 /* Check if the range of "ma" is compatible with the domain or range
12720 * (depending on "type") of "bmap".
12721 * Return -1 if anything is wrong.
12723 static int check_basic_map_compatible_range_multi_aff(
12724 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12725 __isl_keep isl_multi_aff *ma)
12727 int m;
12728 isl_space *ma_space;
12730 ma_space = isl_multi_aff_get_space(ma);
12732 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12733 if (m < 0)
12734 goto error;
12735 if (!m)
12736 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12737 "parameters don't match", goto error);
12738 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12739 if (m < 0)
12740 goto error;
12741 if (!m)
12742 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12743 "spaces don't match", goto error);
12745 isl_space_free(ma_space);
12746 return m;
12747 error:
12748 isl_space_free(ma_space);
12749 return -1;
12752 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12753 * coefficients before the transformed range of dimensions,
12754 * the "n_after" coefficients after the transformed range of dimensions
12755 * and the coefficients of the other divs in "bmap".
12757 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12758 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12760 int i;
12761 int n_param;
12762 int n_set;
12763 isl_local_space *ls;
12765 if (n_div == 0)
12766 return 0;
12768 ls = isl_aff_get_domain_local_space(ma->p[0]);
12769 if (!ls)
12770 return -1;
12772 n_param = isl_local_space_dim(ls, isl_dim_param);
12773 n_set = isl_local_space_dim(ls, isl_dim_set);
12774 for (i = 0; i < n_div; ++i) {
12775 int o_bmap = 0, o_ls = 0;
12777 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12778 o_bmap += 1 + 1 + n_param;
12779 o_ls += 1 + 1 + n_param;
12780 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12781 o_bmap += n_before;
12782 isl_seq_cpy(bmap->div[i] + o_bmap,
12783 ls->div->row[i] + o_ls, n_set);
12784 o_bmap += n_set;
12785 o_ls += n_set;
12786 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12787 o_bmap += n_after;
12788 isl_seq_cpy(bmap->div[i] + o_bmap,
12789 ls->div->row[i] + o_ls, n_div);
12790 o_bmap += n_div;
12791 o_ls += n_div;
12792 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12793 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12794 goto error;
12797 isl_local_space_free(ls);
12798 return 0;
12799 error:
12800 isl_local_space_free(ls);
12801 return -1;
12804 /* How many stride constraints does "ma" enforce?
12805 * That is, how many of the affine expressions have a denominator
12806 * different from one?
12808 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12810 int i;
12811 int strides = 0;
12813 for (i = 0; i < ma->n; ++i)
12814 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12815 strides++;
12817 return strides;
12820 /* For each affine expression in ma of the form
12822 * x_i = (f_i y + h_i)/m_i
12824 * with m_i different from one, add a constraint to "bmap"
12825 * of the form
12827 * f_i y + h_i = m_i alpha_i
12829 * with alpha_i an additional existentially quantified variable.
12831 * The input variables of "ma" correspond to a subset of the variables
12832 * of "bmap". There are "n_before" variables in "bmap" before this
12833 * subset and "n_after" variables after this subset.
12834 * The integer divisions of the affine expressions in "ma" are assumed
12835 * to have been aligned. There are "n_div_ma" of them and
12836 * they appear first in "bmap", straight after the "n_after" variables.
12838 static __isl_give isl_basic_map *add_ma_strides(
12839 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12840 int n_before, int n_after, int n_div_ma)
12842 int i, k;
12843 int div;
12844 int total;
12845 int n_param;
12846 int n_in;
12848 total = isl_basic_map_total_dim(bmap);
12849 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12850 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12851 for (i = 0; i < ma->n; ++i) {
12852 int o_bmap = 0, o_ma = 1;
12854 if (isl_int_is_one(ma->p[i]->v->el[0]))
12855 continue;
12856 div = isl_basic_map_alloc_div(bmap);
12857 k = isl_basic_map_alloc_equality(bmap);
12858 if (div < 0 || k < 0)
12859 goto error;
12860 isl_int_set_si(bmap->div[div][0], 0);
12861 isl_seq_cpy(bmap->eq[k] + o_bmap,
12862 ma->p[i]->v->el + o_ma, 1 + n_param);
12863 o_bmap += 1 + n_param;
12864 o_ma += 1 + n_param;
12865 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12866 o_bmap += n_before;
12867 isl_seq_cpy(bmap->eq[k] + o_bmap,
12868 ma->p[i]->v->el + o_ma, n_in);
12869 o_bmap += n_in;
12870 o_ma += n_in;
12871 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12872 o_bmap += n_after;
12873 isl_seq_cpy(bmap->eq[k] + o_bmap,
12874 ma->p[i]->v->el + o_ma, n_div_ma);
12875 o_bmap += n_div_ma;
12876 o_ma += n_div_ma;
12877 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12878 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12879 total++;
12882 return bmap;
12883 error:
12884 isl_basic_map_free(bmap);
12885 return NULL;
12888 /* Replace the domain or range space (depending on "type) of "space" by "set".
12890 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12891 enum isl_dim_type type, __isl_take isl_space *set)
12893 if (type == isl_dim_in) {
12894 space = isl_space_range(space);
12895 space = isl_space_map_from_domain_and_range(set, space);
12896 } else {
12897 space = isl_space_domain(space);
12898 space = isl_space_map_from_domain_and_range(space, set);
12901 return space;
12904 /* Compute the preimage of the domain or range (depending on "type")
12905 * of "bmap" under the function represented by "ma".
12906 * In other words, plug in "ma" in the domain or range of "bmap".
12907 * The result is a basic map that lives in the same space as "bmap"
12908 * except that the domain or range has been replaced by
12909 * the domain space of "ma".
12911 * If bmap is represented by
12913 * A(p) + S u + B x + T v + C(divs) >= 0,
12915 * where u and x are input and output dimensions if type == isl_dim_out
12916 * while x and v are input and output dimensions if type == isl_dim_in,
12917 * and ma is represented by
12919 * x = D(p) + F(y) + G(divs')
12921 * then the result is
12923 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12925 * The divs in the input set are similarly adjusted.
12926 * In particular
12928 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12930 * becomes
12932 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12933 * B_i G(divs') + c_i(divs))/n_i)
12935 * If bmap is not a rational map and if F(y) involves any denominators
12937 * x_i = (f_i y + h_i)/m_i
12939 * then additional constraints are added to ensure that we only
12940 * map back integer points. That is we enforce
12942 * f_i y + h_i = m_i alpha_i
12944 * with alpha_i an additional existentially quantified variable.
12946 * We first copy over the divs from "ma".
12947 * Then we add the modified constraints and divs from "bmap".
12948 * Finally, we add the stride constraints, if needed.
12950 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12951 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12952 __isl_take isl_multi_aff *ma)
12954 int i, k;
12955 isl_space *space;
12956 isl_basic_map *res = NULL;
12957 int n_before, n_after, n_div_bmap, n_div_ma;
12958 isl_int f, c1, c2, g;
12959 int rational, strides;
12961 isl_int_init(f);
12962 isl_int_init(c1);
12963 isl_int_init(c2);
12964 isl_int_init(g);
12966 ma = isl_multi_aff_align_divs(ma);
12967 if (!bmap || !ma)
12968 goto error;
12969 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12970 goto error;
12972 if (type == isl_dim_in) {
12973 n_before = 0;
12974 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12975 } else {
12976 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12977 n_after = 0;
12979 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12980 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12982 space = isl_multi_aff_get_domain_space(ma);
12983 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12984 rational = isl_basic_map_is_rational(bmap);
12985 strides = rational ? 0 : multi_aff_strides(ma);
12986 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12987 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12988 if (rational)
12989 res = isl_basic_map_set_rational(res);
12991 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12992 if (isl_basic_map_alloc_div(res) < 0)
12993 goto error;
12995 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12996 goto error;
12998 for (i = 0; i < bmap->n_eq; ++i) {
12999 k = isl_basic_map_alloc_equality(res);
13000 if (k < 0)
13001 goto error;
13002 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13003 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13006 for (i = 0; i < bmap->n_ineq; ++i) {
13007 k = isl_basic_map_alloc_inequality(res);
13008 if (k < 0)
13009 goto error;
13010 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13011 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13014 for (i = 0; i < bmap->n_div; ++i) {
13015 if (isl_int_is_zero(bmap->div[i][0])) {
13016 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13017 continue;
13019 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13020 n_before, n_after, n_div_ma, n_div_bmap,
13021 f, c1, c2, g, 1);
13024 if (strides)
13025 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13027 isl_int_clear(f);
13028 isl_int_clear(c1);
13029 isl_int_clear(c2);
13030 isl_int_clear(g);
13031 isl_basic_map_free(bmap);
13032 isl_multi_aff_free(ma);
13033 res = isl_basic_set_simplify(res);
13034 return isl_basic_map_finalize(res);
13035 error:
13036 isl_int_clear(f);
13037 isl_int_clear(c1);
13038 isl_int_clear(c2);
13039 isl_int_clear(g);
13040 isl_basic_map_free(bmap);
13041 isl_multi_aff_free(ma);
13042 isl_basic_map_free(res);
13043 return NULL;
13046 /* Compute the preimage of "bset" under the function represented by "ma".
13047 * In other words, plug in "ma" in "bset". The result is a basic set
13048 * that lives in the domain space of "ma".
13050 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13051 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13053 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13056 /* Compute the preimage of the domain of "bmap" under the function
13057 * represented by "ma".
13058 * In other words, plug in "ma" in the domain of "bmap".
13059 * The result is a basic map that lives in the same space as "bmap"
13060 * except that the domain has been replaced by the domain space of "ma".
13062 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13063 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13065 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13068 /* Compute the preimage of the range of "bmap" under the function
13069 * represented by "ma".
13070 * In other words, plug in "ma" in the range of "bmap".
13071 * The result is a basic map that lives in the same space as "bmap"
13072 * except that the range has been replaced by the domain space of "ma".
13074 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13075 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13077 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13080 /* Check if the range of "ma" is compatible with the domain or range
13081 * (depending on "type") of "map".
13082 * Return -1 if anything is wrong.
13084 static int check_map_compatible_range_multi_aff(
13085 __isl_keep isl_map *map, enum isl_dim_type type,
13086 __isl_keep isl_multi_aff *ma)
13088 int m;
13089 isl_space *ma_space;
13091 ma_space = isl_multi_aff_get_space(ma);
13092 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13093 isl_space_free(ma_space);
13094 if (m >= 0 && !m)
13095 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13096 "spaces don't match", return -1);
13097 return m;
13100 /* Compute the preimage of the domain or range (depending on "type")
13101 * of "map" under the function represented by "ma".
13102 * In other words, plug in "ma" in the domain or range of "map".
13103 * The result is a map that lives in the same space as "map"
13104 * except that the domain or range has been replaced by
13105 * the domain space of "ma".
13107 * The parameters are assumed to have been aligned.
13109 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13110 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13112 int i;
13113 isl_space *space;
13115 map = isl_map_cow(map);
13116 ma = isl_multi_aff_align_divs(ma);
13117 if (!map || !ma)
13118 goto error;
13119 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13120 goto error;
13122 for (i = 0; i < map->n; ++i) {
13123 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13124 isl_multi_aff_copy(ma));
13125 if (!map->p[i])
13126 goto error;
13129 space = isl_multi_aff_get_domain_space(ma);
13130 space = isl_space_set(isl_map_get_space(map), type, space);
13132 isl_space_free(map->dim);
13133 map->dim = space;
13134 if (!map->dim)
13135 goto error;
13137 isl_multi_aff_free(ma);
13138 if (map->n > 1)
13139 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13140 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13141 return map;
13142 error:
13143 isl_multi_aff_free(ma);
13144 isl_map_free(map);
13145 return NULL;
13148 /* Compute the preimage of the domain or range (depending on "type")
13149 * of "map" under the function represented by "ma".
13150 * In other words, plug in "ma" in the domain or range of "map".
13151 * The result is a map that lives in the same space as "map"
13152 * except that the domain or range has been replaced by
13153 * the domain space of "ma".
13155 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13156 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13158 if (!map || !ma)
13159 goto error;
13161 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
13162 return map_preimage_multi_aff(map, type, ma);
13164 if (!isl_space_has_named_params(map->dim) ||
13165 !isl_space_has_named_params(ma->space))
13166 isl_die(map->ctx, isl_error_invalid,
13167 "unaligned unnamed parameters", goto error);
13168 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13169 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13171 return map_preimage_multi_aff(map, type, ma);
13172 error:
13173 isl_multi_aff_free(ma);
13174 return isl_map_free(map);
13177 /* Compute the preimage of "set" under the function represented by "ma".
13178 * In other words, plug in "ma" in "set". The result is a set
13179 * that lives in the domain space of "ma".
13181 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13182 __isl_take isl_multi_aff *ma)
13184 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13187 /* Compute the preimage of the domain of "map" under the function
13188 * represented by "ma".
13189 * In other words, plug in "ma" in the domain of "map".
13190 * The result is a map that lives in the same space as "map"
13191 * except that the domain has been replaced by the domain space of "ma".
13193 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13194 __isl_take isl_multi_aff *ma)
13196 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13199 /* Compute the preimage of the range of "map" under the function
13200 * represented by "ma".
13201 * In other words, plug in "ma" in the range of "map".
13202 * The result is a map that lives in the same space as "map"
13203 * except that the range has been replaced by the domain space of "ma".
13205 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13206 __isl_take isl_multi_aff *ma)
13208 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13211 /* Compute the preimage of "map" under the function represented by "pma".
13212 * In other words, plug in "pma" in the domain or range of "map".
13213 * The result is a map that lives in the same space as "map",
13214 * except that the space of type "type" has been replaced by
13215 * the domain space of "pma".
13217 * The parameters of "map" and "pma" are assumed to have been aligned.
13219 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13220 __isl_take isl_map *map, enum isl_dim_type type,
13221 __isl_take isl_pw_multi_aff *pma)
13223 int i;
13224 isl_map *res;
13226 if (!pma)
13227 goto error;
13229 if (pma->n == 0) {
13230 isl_pw_multi_aff_free(pma);
13231 res = isl_map_empty(isl_map_get_space(map));
13232 isl_map_free(map);
13233 return res;
13236 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13237 isl_multi_aff_copy(pma->p[0].maff));
13238 if (type == isl_dim_in)
13239 res = isl_map_intersect_domain(res,
13240 isl_map_copy(pma->p[0].set));
13241 else
13242 res = isl_map_intersect_range(res,
13243 isl_map_copy(pma->p[0].set));
13245 for (i = 1; i < pma->n; ++i) {
13246 isl_map *res_i;
13248 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13249 isl_multi_aff_copy(pma->p[i].maff));
13250 if (type == isl_dim_in)
13251 res_i = isl_map_intersect_domain(res_i,
13252 isl_map_copy(pma->p[i].set));
13253 else
13254 res_i = isl_map_intersect_range(res_i,
13255 isl_map_copy(pma->p[i].set));
13256 res = isl_map_union(res, res_i);
13259 isl_pw_multi_aff_free(pma);
13260 isl_map_free(map);
13261 return res;
13262 error:
13263 isl_pw_multi_aff_free(pma);
13264 isl_map_free(map);
13265 return NULL;
13268 /* Compute the preimage of "map" under the function represented by "pma".
13269 * In other words, plug in "pma" in the domain or range of "map".
13270 * The result is a map that lives in the same space as "map",
13271 * except that the space of type "type" has been replaced by
13272 * the domain space of "pma".
13274 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13275 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13277 if (!map || !pma)
13278 goto error;
13280 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
13281 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13283 if (!isl_space_has_named_params(map->dim) ||
13284 !isl_space_has_named_params(pma->dim))
13285 isl_die(map->ctx, isl_error_invalid,
13286 "unaligned unnamed parameters", goto error);
13287 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13288 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13290 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13291 error:
13292 isl_pw_multi_aff_free(pma);
13293 return isl_map_free(map);
13296 /* Compute the preimage of "set" under the function represented by "pma".
13297 * In other words, plug in "pma" in "set". The result is a set
13298 * that lives in the domain space of "pma".
13300 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13301 __isl_take isl_pw_multi_aff *pma)
13303 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13306 /* Compute the preimage of the domain of "map" under the function
13307 * represented by "pma".
13308 * In other words, plug in "pma" in the domain of "map".
13309 * The result is a map that lives in the same space as "map",
13310 * except that domain space has been replaced by the domain space of "pma".
13312 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13313 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13315 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13318 /* Compute the preimage of the range of "map" under the function
13319 * represented by "pma".
13320 * In other words, plug in "pma" in the range of "map".
13321 * The result is a map that lives in the same space as "map",
13322 * except that range space has been replaced by the domain space of "pma".
13324 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13325 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13327 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13330 /* Compute the preimage of "map" under the function represented by "mpa".
13331 * In other words, plug in "mpa" in the domain or range of "map".
13332 * The result is a map that lives in the same space as "map",
13333 * except that the space of type "type" has been replaced by
13334 * the domain space of "mpa".
13336 * If the map does not involve any constraints that refer to the
13337 * dimensions of the substituted space, then the only possible
13338 * effect of "mpa" on the map is to map the space to a different space.
13339 * We create a separate isl_multi_aff to effectuate this change
13340 * in order to avoid spurious splitting of the map along the pieces
13341 * of "mpa".
13343 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13344 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13346 int n;
13347 isl_pw_multi_aff *pma;
13349 if (!map || !mpa)
13350 goto error;
13352 n = isl_map_dim(map, type);
13353 if (!isl_map_involves_dims(map, type, 0, n)) {
13354 isl_space *space;
13355 isl_multi_aff *ma;
13357 space = isl_multi_pw_aff_get_space(mpa);
13358 isl_multi_pw_aff_free(mpa);
13359 ma = isl_multi_aff_zero(space);
13360 return isl_map_preimage_multi_aff(map, type, ma);
13363 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13364 return isl_map_preimage_pw_multi_aff(map, type, pma);
13365 error:
13366 isl_map_free(map);
13367 isl_multi_pw_aff_free(mpa);
13368 return NULL;
13371 /* Compute the preimage of "map" under the function represented by "mpa".
13372 * In other words, plug in "mpa" in the domain "map".
13373 * The result is a map that lives in the same space as "map",
13374 * except that domain space has been replaced by the domain space of "mpa".
13376 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13377 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13379 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13382 /* Compute the preimage of "set" by the function represented by "mpa".
13383 * In other words, plug in "mpa" in "set".
13385 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13386 __isl_take isl_multi_pw_aff *mpa)
13388 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13391 /* Are the "n" "coefficients" starting at "first" of the integer division
13392 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13393 * to each other?
13394 * The "coefficient" at position 0 is the denominator.
13395 * The "coefficient" at position 1 is the constant term.
13397 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13398 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13399 unsigned first, unsigned n)
13401 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13402 return isl_bool_error;
13403 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13404 return isl_bool_error;
13405 return isl_seq_eq(bmap1->div[pos1] + first,
13406 bmap2->div[pos2] + first, n);
13409 /* Are the integer division expressions at position "pos1" in "bmap1" and
13410 * "pos2" in "bmap2" equal to each other, except that the constant terms
13411 * are different?
13413 isl_bool isl_basic_map_equal_div_expr_except_constant(
13414 __isl_keep isl_basic_map *bmap1, int pos1,
13415 __isl_keep isl_basic_map *bmap2, int pos2)
13417 isl_bool equal;
13418 unsigned total;
13420 if (!bmap1 || !bmap2)
13421 return isl_bool_error;
13422 total = isl_basic_map_total_dim(bmap1);
13423 if (total != isl_basic_map_total_dim(bmap2))
13424 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13425 "incomparable div expressions", return isl_bool_error);
13426 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13427 0, 1);
13428 if (equal < 0 || !equal)
13429 return equal;
13430 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13431 1, 1);
13432 if (equal < 0 || equal)
13433 return isl_bool_not(equal);
13434 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13435 2, total);
13438 /* Replace the numerator of the constant term of the integer division
13439 * expression at position "div" in "bmap" by "value".
13440 * The caller guarantees that this does not change the meaning
13441 * of the input.
13443 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13444 __isl_take isl_basic_map *bmap, int div, int value)
13446 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13447 return isl_basic_map_free(bmap);
13449 isl_int_set_si(bmap->div[div][1], value);
13451 return bmap;
13454 /* Is the point "inner" internal to inequality constraint "ineq"
13455 * of "bset"?
13456 * The point is considered to be internal to the inequality constraint,
13457 * if it strictly lies on the positive side of the inequality constraint,
13458 * or if it lies on the constraint and the constraint is lexico-positive.
13460 static isl_bool is_internal(__isl_keep isl_vec *inner,
13461 __isl_keep isl_basic_set *bset, int ineq)
13463 isl_ctx *ctx;
13464 int pos;
13465 unsigned total;
13467 if (!inner || !bset)
13468 return isl_bool_error;
13470 ctx = isl_basic_set_get_ctx(bset);
13471 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13472 &ctx->normalize_gcd);
13473 if (!isl_int_is_zero(ctx->normalize_gcd))
13474 return isl_int_is_nonneg(ctx->normalize_gcd);
13476 total = isl_basic_set_dim(bset, isl_dim_all);
13477 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13478 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13481 /* Tighten the inequality constraints of "bset" that are outward with respect
13482 * to the point "vec".
13483 * That is, tighten the constraints that are not satisfied by "vec".
13485 * "vec" is a point internal to some superset S of "bset" that is used
13486 * to make the subsets of S disjoint, by tightening one half of the constraints
13487 * that separate two subsets. In particular, the constraints of S
13488 * are all satisfied by "vec" and should not be tightened.
13489 * Of the internal constraints, those that have "vec" on the outside
13490 * are tightened. The shared facet is included in the adjacent subset
13491 * with the opposite constraint.
13492 * For constraints that saturate "vec", this criterion cannot be used
13493 * to determine which of the two sides should be tightened.
13494 * Instead, the sign of the first non-zero coefficient is used
13495 * to make this choice. Note that this second criterion is never used
13496 * on the constraints of S since "vec" is interior to "S".
13498 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13499 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13501 int j;
13503 bset = isl_basic_set_cow(bset);
13504 if (!bset)
13505 return NULL;
13506 for (j = 0; j < bset->n_ineq; ++j) {
13507 isl_bool internal;
13509 internal = is_internal(vec, bset, j);
13510 if (internal < 0)
13511 return isl_basic_set_free(bset);
13512 if (internal)
13513 continue;
13514 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13517 return bset;