isl_basic_map_add_{eq,ineq}: only modify single copy of basic map
[isl.git] / isl_map.c
blob5274dee228be170c7060cda0a575557e942b396e
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/id.h>
27 #include <isl/constraint.h>
28 #include "isl_space_private.h"
29 #include "isl_equalities.h"
30 #include <isl_lp_private.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl/map.h>
34 #include <isl_reordering.h>
35 #include "isl_sample.h"
36 #include <isl_sort.h>
37 #include "isl_tab.h"
38 #include <isl/vec.h>
39 #include <isl_mat_private.h>
40 #include <isl_vec_private.h>
41 #include <isl_dim_map.h>
42 #include <isl_local_space_private.h>
43 #include <isl_aff_private.h>
44 #include <isl_options_private.h>
45 #include <isl_morph.h>
46 #include <isl_val_private.h>
48 #include <bset_to_bmap.c>
49 #include <bset_from_bmap.c>
50 #include <set_to_map.c>
51 #include <set_from_map.c>
53 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
55 switch (type) {
56 case isl_dim_param: return dim->nparam;
57 case isl_dim_in: return dim->n_in;
58 case isl_dim_out: return dim->n_out;
59 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
60 default: return 0;
64 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
66 switch (type) {
67 case isl_dim_param: return 1;
68 case isl_dim_in: return 1 + dim->nparam;
69 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
70 default: return 0;
74 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
75 enum isl_dim_type type)
77 if (!bmap)
78 return 0;
79 switch (type) {
80 case isl_dim_cst: return 1;
81 case isl_dim_param:
82 case isl_dim_in:
83 case isl_dim_out: return isl_space_dim(bmap->dim, type);
84 case isl_dim_div: return bmap->n_div;
85 case isl_dim_all: return isl_basic_map_total_dim(bmap);
86 default: return 0;
90 /* Return the space of "map".
92 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
94 return map ? map->dim : NULL;
97 /* Return the space of "set".
99 __isl_keep isl_space *isl_set_peek_space(__isl_keep isl_set *set)
101 return isl_map_peek_space(set_to_map(set));
104 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
106 return map ? n(map->dim, type) : 0;
109 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
111 return set ? n(set->dim, type) : 0;
114 unsigned isl_basic_map_offset(__isl_keep isl_basic_map *bmap,
115 enum isl_dim_type type)
117 isl_space *space;
119 if (!bmap)
120 return 0;
122 space = bmap->dim;
123 switch (type) {
124 case isl_dim_cst: return 0;
125 case isl_dim_param: return 1;
126 case isl_dim_in: return 1 + space->nparam;
127 case isl_dim_out: return 1 + space->nparam + space->n_in;
128 case isl_dim_div: return 1 + space->nparam + space->n_in +
129 space->n_out;
130 default: return 0;
134 unsigned isl_basic_set_offset(__isl_keep isl_basic_set *bset,
135 enum isl_dim_type type)
137 return isl_basic_map_offset(bset, type);
140 static unsigned map_offset(__isl_keep isl_map *map, enum isl_dim_type type)
142 return pos(map->dim, type);
145 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
146 enum isl_dim_type type)
148 return isl_basic_map_dim(bset, type);
151 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
153 return isl_basic_set_dim(bset, isl_dim_set);
156 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
158 return isl_basic_set_dim(bset, isl_dim_param);
161 unsigned isl_basic_set_total_dim(__isl_keep const isl_basic_set *bset)
163 if (!bset)
164 return 0;
165 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
168 unsigned isl_set_n_dim(__isl_keep isl_set *set)
170 return isl_set_dim(set, isl_dim_set);
173 unsigned isl_set_n_param(__isl_keep isl_set *set)
175 return isl_set_dim(set, isl_dim_param);
178 unsigned isl_basic_map_n_in(__isl_keep const isl_basic_map *bmap)
180 return bmap ? bmap->dim->n_in : 0;
183 unsigned isl_basic_map_n_out(__isl_keep const isl_basic_map *bmap)
185 return bmap ? bmap->dim->n_out : 0;
188 unsigned isl_basic_map_n_param(__isl_keep const isl_basic_map *bmap)
190 return bmap ? bmap->dim->nparam : 0;
193 unsigned isl_basic_map_n_div(__isl_keep const isl_basic_map *bmap)
195 return bmap ? bmap->n_div : 0;
198 unsigned isl_basic_map_total_dim(__isl_keep const isl_basic_map *bmap)
200 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
203 unsigned isl_map_n_in(__isl_keep const isl_map *map)
205 return map ? map->dim->n_in : 0;
208 unsigned isl_map_n_out(__isl_keep const isl_map *map)
210 return map ? map->dim->n_out : 0;
213 unsigned isl_map_n_param(__isl_keep const isl_map *map)
215 return map ? map->dim->nparam : 0;
218 /* Return the number of equality constraints in the description of "bmap".
219 * Return -1 on error.
221 int isl_basic_map_n_equality(__isl_keep isl_basic_map *bmap)
223 if (!bmap)
224 return -1;
225 return bmap->n_eq;
228 /* Return the number of equality constraints in the description of "bset".
229 * Return -1 on error.
231 int isl_basic_set_n_equality(__isl_keep isl_basic_set *bset)
233 return isl_basic_map_n_equality(bset_to_bmap(bset));
236 /* Return the number of inequality constraints in the description of "bmap".
237 * Return -1 on error.
239 int isl_basic_map_n_inequality(__isl_keep isl_basic_map *bmap)
241 if (!bmap)
242 return -1;
243 return bmap->n_ineq;
246 /* Return the number of inequality constraints in the description of "bset".
247 * Return -1 on error.
249 int isl_basic_set_n_inequality(__isl_keep isl_basic_set *bset)
251 return isl_basic_map_n_inequality(bset_to_bmap(bset));
254 /* Do "bmap1" and "bmap2" have the same parameters?
256 static isl_bool isl_basic_map_has_equal_params(__isl_keep isl_basic_map *bmap1,
257 __isl_keep isl_basic_map *bmap2)
259 isl_space *space1, *space2;
261 space1 = isl_basic_map_peek_space(bmap1);
262 space2 = isl_basic_map_peek_space(bmap2);
263 return isl_space_has_equal_params(space1, space2);
266 /* Do "map1" and "map2" have the same parameters?
268 isl_bool isl_map_has_equal_params(__isl_keep isl_map *map1,
269 __isl_keep isl_map *map2)
271 isl_space *space1, *space2;
273 space1 = isl_map_peek_space(map1);
274 space2 = isl_map_peek_space(map2);
275 return isl_space_has_equal_params(space1, space2);
278 /* Do "map" and "set" have the same parameters?
280 static isl_bool isl_map_set_has_equal_params(__isl_keep isl_map *map,
281 __isl_keep isl_set *set)
283 return isl_map_has_equal_params(map, set_to_map(set));
286 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
287 __isl_keep isl_set *set)
289 isl_bool m;
290 if (!map || !set)
291 return isl_bool_error;
292 m = isl_map_has_equal_params(map, set_to_map(set));
293 if (m < 0 || !m)
294 return m;
295 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
296 set->dim, isl_dim_set);
299 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
300 __isl_keep isl_basic_set *bset)
302 isl_bool m;
303 if (!bmap || !bset)
304 return isl_bool_error;
305 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
306 if (m < 0 || !m)
307 return m;
308 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
309 bset->dim, isl_dim_set);
312 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
313 __isl_keep isl_set *set)
315 isl_bool m;
316 if (!map || !set)
317 return isl_bool_error;
318 m = isl_map_has_equal_params(map, set_to_map(set));
319 if (m < 0 || !m)
320 return m;
321 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
322 set->dim, isl_dim_set);
325 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
326 __isl_keep isl_basic_set *bset)
328 isl_bool m;
329 if (!bmap || !bset)
330 return isl_bool_error;
331 m = isl_basic_map_has_equal_params(bmap, bset_to_bmap(bset));
332 if (m < 0 || !m)
333 return m;
334 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
335 bset->dim, isl_dim_set);
338 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
340 return bmap ? bmap->ctx : NULL;
343 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
345 return bset ? bset->ctx : NULL;
348 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
350 return map ? map->ctx : NULL;
353 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
355 return set ? set->ctx : NULL;
358 /* Return the space of "bmap".
360 __isl_keep isl_space *isl_basic_map_peek_space(
361 __isl_keep const isl_basic_map *bmap)
363 return bmap ? bmap->dim : NULL;
366 /* Return the space of "bset".
368 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
370 return isl_basic_map_peek_space(bset_to_bmap(bset));
373 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
375 return isl_space_copy(isl_basic_map_peek_space(bmap));
378 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
380 return isl_basic_map_get_space(bset_to_bmap(bset));
383 /* Extract the divs in "bmap" as a matrix.
385 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
387 int i;
388 isl_ctx *ctx;
389 isl_mat *div;
390 unsigned total;
391 unsigned cols;
393 if (!bmap)
394 return NULL;
396 ctx = isl_basic_map_get_ctx(bmap);
397 total = isl_space_dim(bmap->dim, isl_dim_all);
398 cols = 1 + 1 + total + bmap->n_div;
399 div = isl_mat_alloc(ctx, bmap->n_div, cols);
400 if (!div)
401 return NULL;
403 for (i = 0; i < bmap->n_div; ++i)
404 isl_seq_cpy(div->row[i], bmap->div[i], cols);
406 return div;
409 /* Extract the divs in "bset" as a matrix.
411 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
413 return isl_basic_map_get_divs(bset);
416 __isl_give isl_local_space *isl_basic_map_get_local_space(
417 __isl_keep isl_basic_map *bmap)
419 isl_mat *div;
421 if (!bmap)
422 return NULL;
424 div = isl_basic_map_get_divs(bmap);
425 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
428 __isl_give isl_local_space *isl_basic_set_get_local_space(
429 __isl_keep isl_basic_set *bset)
431 return isl_basic_map_get_local_space(bset);
434 /* For each known div d = floor(f/m), add the constraints
436 * f - m d >= 0
437 * -(f-(m-1)) + m d >= 0
439 * Do not finalize the result.
441 static __isl_give isl_basic_map *add_known_div_constraints(
442 __isl_take isl_basic_map *bmap)
444 int i;
445 unsigned n_div;
447 if (!bmap)
448 return NULL;
449 n_div = isl_basic_map_dim(bmap, isl_dim_div);
450 if (n_div == 0)
451 return bmap;
452 bmap = isl_basic_map_cow(bmap);
453 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
454 if (!bmap)
455 return NULL;
456 for (i = 0; i < n_div; ++i) {
457 if (isl_int_is_zero(bmap->div[i][0]))
458 continue;
459 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
460 return isl_basic_map_free(bmap);
463 return bmap;
466 __isl_give isl_basic_map *isl_basic_map_from_local_space(
467 __isl_take isl_local_space *ls)
469 int i;
470 int n_div;
471 isl_basic_map *bmap;
473 if (!ls)
474 return NULL;
476 n_div = isl_local_space_dim(ls, isl_dim_div);
477 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
478 n_div, 0, 2 * n_div);
480 for (i = 0; i < n_div; ++i)
481 if (isl_basic_map_alloc_div(bmap) < 0)
482 goto error;
484 for (i = 0; i < n_div; ++i)
485 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
486 bmap = add_known_div_constraints(bmap);
488 isl_local_space_free(ls);
489 return bmap;
490 error:
491 isl_local_space_free(ls);
492 isl_basic_map_free(bmap);
493 return NULL;
496 __isl_give isl_basic_set *isl_basic_set_from_local_space(
497 __isl_take isl_local_space *ls)
499 return isl_basic_map_from_local_space(ls);
502 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
504 return isl_space_copy(isl_map_peek_space(map));
507 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
509 if (!set)
510 return NULL;
511 return isl_space_copy(set->dim);
514 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
515 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
517 bmap = isl_basic_map_cow(bmap);
518 if (!bmap)
519 return NULL;
520 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
521 if (!bmap->dim)
522 goto error;
523 bmap = isl_basic_map_finalize(bmap);
524 return bmap;
525 error:
526 isl_basic_map_free(bmap);
527 return NULL;
530 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
531 __isl_take isl_basic_set *bset, const char *s)
533 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
536 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
537 enum isl_dim_type type)
539 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
542 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
543 enum isl_dim_type type, const char *s)
545 int i;
547 map = isl_map_cow(map);
548 if (!map)
549 return NULL;
551 map->dim = isl_space_set_tuple_name(map->dim, type, s);
552 if (!map->dim)
553 goto error;
555 for (i = 0; i < map->n; ++i) {
556 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
557 if (!map->p[i])
558 goto error;
561 return map;
562 error:
563 isl_map_free(map);
564 return NULL;
567 /* Replace the identifier of the tuple of type "type" by "id".
569 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
570 __isl_take isl_basic_map *bmap,
571 enum isl_dim_type type, __isl_take isl_id *id)
573 bmap = isl_basic_map_cow(bmap);
574 if (!bmap)
575 goto error;
576 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
577 if (!bmap->dim)
578 return isl_basic_map_free(bmap);
579 bmap = isl_basic_map_finalize(bmap);
580 return bmap;
581 error:
582 isl_id_free(id);
583 return NULL;
586 /* Replace the identifier of the tuple by "id".
588 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
589 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
591 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
594 /* Does the input or output tuple have a name?
596 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
598 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
601 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
602 enum isl_dim_type type)
604 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
607 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
608 const char *s)
610 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
611 isl_dim_set, s));
614 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
615 enum isl_dim_type type, __isl_take isl_id *id)
617 map = isl_map_cow(map);
618 if (!map)
619 goto error;
621 map->dim = isl_space_set_tuple_id(map->dim, type, id);
623 return isl_map_reset_space(map, isl_space_copy(map->dim));
624 error:
625 isl_id_free(id);
626 return NULL;
629 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
630 __isl_take isl_id *id)
632 return isl_map_set_tuple_id(set, isl_dim_set, id);
635 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
636 enum isl_dim_type type)
638 map = isl_map_cow(map);
639 if (!map)
640 return NULL;
642 map->dim = isl_space_reset_tuple_id(map->dim, type);
644 return isl_map_reset_space(map, isl_space_copy(map->dim));
647 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
649 return isl_map_reset_tuple_id(set, isl_dim_set);
652 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
654 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
657 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
658 enum isl_dim_type type)
660 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
663 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
665 return isl_map_has_tuple_id(set, isl_dim_set);
668 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
670 return isl_map_get_tuple_id(set, isl_dim_set);
673 /* Does the set tuple have a name?
675 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
677 if (!set)
678 return isl_bool_error;
679 return isl_space_has_tuple_name(set->dim, isl_dim_set);
683 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
685 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
688 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
690 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
693 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
694 enum isl_dim_type type, unsigned pos)
696 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
699 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
700 enum isl_dim_type type, unsigned pos)
702 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
705 /* Does the given dimension have a name?
707 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
708 enum isl_dim_type type, unsigned pos)
710 if (!map)
711 return isl_bool_error;
712 return isl_space_has_dim_name(map->dim, type, pos);
715 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
716 enum isl_dim_type type, unsigned pos)
718 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
721 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
722 enum isl_dim_type type, unsigned pos)
724 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
727 /* Does the given dimension have a name?
729 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
730 enum isl_dim_type type, unsigned pos)
732 if (!set)
733 return isl_bool_error;
734 return isl_space_has_dim_name(set->dim, type, pos);
737 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
738 __isl_take isl_basic_map *bmap,
739 enum isl_dim_type type, unsigned pos, const char *s)
741 bmap = isl_basic_map_cow(bmap);
742 if (!bmap)
743 return NULL;
744 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
745 if (!bmap->dim)
746 goto error;
747 return isl_basic_map_finalize(bmap);
748 error:
749 isl_basic_map_free(bmap);
750 return NULL;
753 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
754 enum isl_dim_type type, unsigned pos, const char *s)
756 int i;
758 map = isl_map_cow(map);
759 if (!map)
760 return NULL;
762 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
763 if (!map->dim)
764 goto error;
766 for (i = 0; i < map->n; ++i) {
767 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
768 if (!map->p[i])
769 goto error;
772 return map;
773 error:
774 isl_map_free(map);
775 return NULL;
778 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
779 __isl_take isl_basic_set *bset,
780 enum isl_dim_type type, unsigned pos, const char *s)
782 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
783 type, pos, s));
786 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
787 enum isl_dim_type type, unsigned pos, const char *s)
789 return set_from_map(isl_map_set_dim_name(set_to_map(set),
790 type, pos, s));
793 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
794 enum isl_dim_type type, unsigned pos)
796 if (!bmap)
797 return isl_bool_error;
798 return isl_space_has_dim_id(bmap->dim, type, pos);
801 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
802 enum isl_dim_type type, unsigned pos)
804 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
807 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
808 enum isl_dim_type type, unsigned pos)
810 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
813 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
814 enum isl_dim_type type, unsigned pos)
816 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
819 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
820 enum isl_dim_type type, unsigned pos)
822 return isl_map_has_dim_id(set, type, pos);
825 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
826 enum isl_dim_type type, unsigned pos)
828 return isl_map_get_dim_id(set, type, pos);
831 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
832 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
834 map = isl_map_cow(map);
835 if (!map)
836 goto error;
838 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
840 return isl_map_reset_space(map, isl_space_copy(map->dim));
841 error:
842 isl_id_free(id);
843 return NULL;
846 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
847 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
849 return isl_map_set_dim_id(set, type, pos, id);
852 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
853 __isl_keep isl_id *id)
855 if (!map)
856 return -1;
857 return isl_space_find_dim_by_id(map->dim, type, id);
860 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
861 __isl_keep isl_id *id)
863 return isl_map_find_dim_by_id(set, type, id);
866 /* Return the position of the dimension of the given type and name
867 * in "bmap".
868 * Return -1 if no such dimension can be found.
870 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
871 enum isl_dim_type type, const char *name)
873 if (!bmap)
874 return -1;
875 return isl_space_find_dim_by_name(bmap->dim, type, name);
878 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
879 const char *name)
881 if (!map)
882 return -1;
883 return isl_space_find_dim_by_name(map->dim, type, name);
886 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
887 const char *name)
889 return isl_map_find_dim_by_name(set, type, name);
892 /* Check whether equality i of bset is a pure stride constraint
893 * on a single dimension, i.e., of the form
895 * v = k e
897 * with k a constant and e an existentially quantified variable.
899 isl_bool isl_basic_set_eq_is_stride(__isl_keep isl_basic_set *bset, int i)
901 unsigned nparam;
902 unsigned d;
903 unsigned n_div;
904 int pos1;
905 int pos2;
907 if (!bset)
908 return isl_bool_error;
910 if (!isl_int_is_zero(bset->eq[i][0]))
911 return isl_bool_false;
913 nparam = isl_basic_set_dim(bset, isl_dim_param);
914 d = isl_basic_set_dim(bset, isl_dim_set);
915 n_div = isl_basic_set_dim(bset, isl_dim_div);
917 if (isl_seq_first_non_zero(bset->eq[i] + 1, nparam) != -1)
918 return isl_bool_false;
919 pos1 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam, d);
920 if (pos1 == -1)
921 return isl_bool_false;
922 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + pos1 + 1,
923 d - pos1 - 1) != -1)
924 return isl_bool_false;
926 pos2 = isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d, n_div);
927 if (pos2 == -1)
928 return isl_bool_false;
929 if (isl_seq_first_non_zero(bset->eq[i] + 1 + nparam + d + pos2 + 1,
930 n_div - pos2 - 1) != -1)
931 return isl_bool_false;
932 if (!isl_int_is_one(bset->eq[i][1 + nparam + pos1]) &&
933 !isl_int_is_negone(bset->eq[i][1 + nparam + pos1]))
934 return isl_bool_false;
936 return isl_bool_true;
939 /* Reset the user pointer on all identifiers of parameters and tuples
940 * of the space of "map".
942 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
944 isl_space *space;
946 space = isl_map_get_space(map);
947 space = isl_space_reset_user(space);
948 map = isl_map_reset_space(map, space);
950 return map;
953 /* Reset the user pointer on all identifiers of parameters and tuples
954 * of the space of "set".
956 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
958 return isl_map_reset_user(set);
961 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
963 if (!bmap)
964 return isl_bool_error;
965 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
968 /* Has "map" been marked as a rational map?
969 * In particular, have all basic maps in "map" been marked this way?
970 * An empty map is not considered to be rational.
971 * Maps where only some of the basic maps are marked rational
972 * are not allowed.
974 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
976 int i;
977 isl_bool rational;
979 if (!map)
980 return isl_bool_error;
981 if (map->n == 0)
982 return isl_bool_false;
983 rational = isl_basic_map_is_rational(map->p[0]);
984 if (rational < 0)
985 return rational;
986 for (i = 1; i < map->n; ++i) {
987 isl_bool rational_i;
989 rational_i = isl_basic_map_is_rational(map->p[i]);
990 if (rational_i < 0)
991 return rational_i;
992 if (rational != rational_i)
993 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
994 "mixed rational and integer basic maps "
995 "not supported", return isl_bool_error);
998 return rational;
1001 /* Has "set" been marked as a rational set?
1002 * In particular, have all basic set in "set" been marked this way?
1003 * An empty set is not considered to be rational.
1004 * Sets where only some of the basic sets are marked rational
1005 * are not allowed.
1007 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
1009 return isl_map_is_rational(set);
1012 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
1014 return isl_basic_map_is_rational(bset);
1017 /* Does "bmap" contain any rational points?
1019 * If "bmap" has an equality for each dimension, equating the dimension
1020 * to an integer constant, then it has no rational points, even if it
1021 * is marked as rational.
1023 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
1025 isl_bool has_rational = isl_bool_true;
1026 unsigned total;
1028 if (!bmap)
1029 return isl_bool_error;
1030 if (isl_basic_map_plain_is_empty(bmap))
1031 return isl_bool_false;
1032 if (!isl_basic_map_is_rational(bmap))
1033 return isl_bool_false;
1034 bmap = isl_basic_map_copy(bmap);
1035 bmap = isl_basic_map_implicit_equalities(bmap);
1036 if (!bmap)
1037 return isl_bool_error;
1038 total = isl_basic_map_total_dim(bmap);
1039 if (bmap->n_eq == total) {
1040 int i, j;
1041 for (i = 0; i < bmap->n_eq; ++i) {
1042 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
1043 if (j < 0)
1044 break;
1045 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
1046 !isl_int_is_negone(bmap->eq[i][1 + j]))
1047 break;
1048 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
1049 total - j - 1);
1050 if (j >= 0)
1051 break;
1053 if (i == bmap->n_eq)
1054 has_rational = isl_bool_false;
1056 isl_basic_map_free(bmap);
1058 return has_rational;
1061 /* Does "map" contain any rational points?
1063 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
1065 int i;
1066 isl_bool has_rational;
1068 if (!map)
1069 return isl_bool_error;
1070 for (i = 0; i < map->n; ++i) {
1071 has_rational = isl_basic_map_has_rational(map->p[i]);
1072 if (has_rational < 0 || has_rational)
1073 return has_rational;
1075 return isl_bool_false;
1078 /* Does "set" contain any rational points?
1080 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
1082 return isl_map_has_rational(set);
1085 /* Is this basic set a parameter domain?
1087 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
1089 if (!bset)
1090 return isl_bool_error;
1091 return isl_space_is_params(bset->dim);
1094 /* Is this set a parameter domain?
1096 isl_bool isl_set_is_params(__isl_keep isl_set *set)
1098 if (!set)
1099 return isl_bool_error;
1100 return isl_space_is_params(set->dim);
1103 /* Is this map actually a parameter domain?
1104 * Users should never call this function. Outside of isl,
1105 * a map can never be a parameter domain.
1107 isl_bool isl_map_is_params(__isl_keep isl_map *map)
1109 if (!map)
1110 return isl_bool_error;
1111 return isl_space_is_params(map->dim);
1114 static __isl_give isl_basic_map *basic_map_init(isl_ctx *ctx,
1115 __isl_take isl_basic_map *bmap, unsigned extra,
1116 unsigned n_eq, unsigned n_ineq)
1118 int i;
1119 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1121 bmap->ctx = ctx;
1122 isl_ctx_ref(ctx);
1124 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1125 if (isl_blk_is_error(bmap->block))
1126 goto error;
1128 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1129 if ((n_ineq + n_eq) && !bmap->ineq)
1130 goto error;
1132 if (extra == 0) {
1133 bmap->block2 = isl_blk_empty();
1134 bmap->div = NULL;
1135 } else {
1136 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1137 if (isl_blk_is_error(bmap->block2))
1138 goto error;
1140 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1141 if (!bmap->div)
1142 goto error;
1145 for (i = 0; i < n_ineq + n_eq; ++i)
1146 bmap->ineq[i] = bmap->block.data + i * row_size;
1148 for (i = 0; i < extra; ++i)
1149 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1151 bmap->ref = 1;
1152 bmap->flags = 0;
1153 bmap->c_size = n_eq + n_ineq;
1154 bmap->eq = bmap->ineq + n_ineq;
1155 bmap->extra = extra;
1156 bmap->n_eq = 0;
1157 bmap->n_ineq = 0;
1158 bmap->n_div = 0;
1159 bmap->sample = NULL;
1161 return bmap;
1162 error:
1163 isl_basic_map_free(bmap);
1164 return NULL;
1167 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1168 unsigned nparam, unsigned dim, unsigned extra,
1169 unsigned n_eq, unsigned n_ineq)
1171 struct isl_basic_map *bmap;
1172 isl_space *space;
1174 space = isl_space_set_alloc(ctx, nparam, dim);
1175 if (!space)
1176 return NULL;
1178 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1179 return bset_from_bmap(bmap);
1182 __isl_give isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1183 unsigned extra, unsigned n_eq, unsigned n_ineq)
1185 struct isl_basic_map *bmap;
1186 if (!dim)
1187 return NULL;
1188 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1189 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1190 return bset_from_bmap(bmap);
1191 error:
1192 isl_space_free(dim);
1193 return NULL;
1196 __isl_give isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *space,
1197 unsigned extra, unsigned n_eq, unsigned n_ineq)
1199 struct isl_basic_map *bmap;
1201 if (!space)
1202 return NULL;
1203 bmap = isl_calloc_type(space->ctx, struct isl_basic_map);
1204 if (!bmap)
1205 goto error;
1206 bmap->dim = space;
1208 return basic_map_init(space->ctx, bmap, extra, n_eq, n_ineq);
1209 error:
1210 isl_space_free(space);
1211 return NULL;
1214 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1215 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1216 unsigned n_eq, unsigned n_ineq)
1218 struct isl_basic_map *bmap;
1219 isl_space *dim;
1221 dim = isl_space_alloc(ctx, nparam, in, out);
1222 if (!dim)
1223 return NULL;
1225 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1226 return bmap;
1229 static __isl_give isl_basic_map *dup_constraints(__isl_take isl_basic_map *dst,
1230 __isl_keep isl_basic_map *src)
1232 int i;
1233 unsigned total = isl_basic_map_total_dim(src);
1235 if (!dst)
1236 return NULL;
1238 for (i = 0; i < src->n_eq; ++i) {
1239 int j = isl_basic_map_alloc_equality(dst);
1240 if (j < 0)
1241 return isl_basic_map_free(dst);
1242 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1245 for (i = 0; i < src->n_ineq; ++i) {
1246 int j = isl_basic_map_alloc_inequality(dst);
1247 if (j < 0)
1248 return isl_basic_map_free(dst);
1249 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1252 for (i = 0; i < src->n_div; ++i) {
1253 int j = isl_basic_map_alloc_div(dst);
1254 if (j < 0)
1255 return isl_basic_map_free(dst);
1256 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1258 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1259 return dst;
1262 __isl_give isl_basic_map *isl_basic_map_dup(__isl_keep isl_basic_map *bmap)
1264 struct isl_basic_map *dup;
1266 if (!bmap)
1267 return NULL;
1268 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1269 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1270 dup = dup_constraints(dup, bmap);
1271 if (!dup)
1272 return NULL;
1273 dup->flags = bmap->flags;
1274 dup->sample = isl_vec_copy(bmap->sample);
1275 return dup;
1278 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1280 struct isl_basic_map *dup;
1282 dup = isl_basic_map_dup(bset_to_bmap(bset));
1283 return bset_from_bmap(dup);
1286 __isl_give isl_basic_set *isl_basic_set_copy(__isl_keep isl_basic_set *bset)
1288 if (!bset)
1289 return NULL;
1291 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1292 bset->ref++;
1293 return bset;
1295 return isl_basic_set_dup(bset);
1298 __isl_give isl_set *isl_set_copy(__isl_keep isl_set *set)
1300 if (!set)
1301 return NULL;
1303 set->ref++;
1304 return set;
1307 __isl_give isl_basic_map *isl_basic_map_copy(__isl_keep isl_basic_map *bmap)
1309 if (!bmap)
1310 return NULL;
1312 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1313 bmap->ref++;
1314 return bmap;
1316 bmap = isl_basic_map_dup(bmap);
1317 if (bmap)
1318 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1319 return bmap;
1322 __isl_give isl_map *isl_map_copy(__isl_keep isl_map *map)
1324 if (!map)
1325 return NULL;
1327 map->ref++;
1328 return map;
1331 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1333 if (!bmap)
1334 return NULL;
1336 if (--bmap->ref > 0)
1337 return NULL;
1339 isl_ctx_deref(bmap->ctx);
1340 free(bmap->div);
1341 isl_blk_free(bmap->ctx, bmap->block2);
1342 free(bmap->ineq);
1343 isl_blk_free(bmap->ctx, bmap->block);
1344 isl_vec_free(bmap->sample);
1345 isl_space_free(bmap->dim);
1346 free(bmap);
1348 return NULL;
1351 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1353 return isl_basic_map_free(bset_to_bmap(bset));
1356 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1358 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1361 /* Check that "map" has only named parameters, reporting an error
1362 * if it does not.
1364 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1366 return isl_space_check_named_params(isl_map_peek_space(map));
1369 /* Check that "bmap" has only named parameters, reporting an error
1370 * if it does not.
1372 static isl_stat isl_basic_map_check_named_params(__isl_keep isl_basic_map *bmap)
1374 return isl_space_check_named_params(isl_basic_map_peek_space(bmap));
1377 /* Check that "bmap1" and "bmap2" have the same parameters,
1378 * reporting an error if they do not.
1380 static isl_stat isl_basic_map_check_equal_params(
1381 __isl_keep isl_basic_map *bmap1, __isl_keep isl_basic_map *bmap2)
1383 isl_bool match;
1385 match = isl_basic_map_has_equal_params(bmap1, bmap2);
1386 if (match < 0)
1387 return isl_stat_error;
1388 if (!match)
1389 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
1390 "parameters don't match", return isl_stat_error);
1391 return isl_stat_ok;
1394 __isl_give isl_map *isl_map_align_params_map_map_and(
1395 __isl_take isl_map *map1, __isl_take isl_map *map2,
1396 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1397 __isl_take isl_map *map2))
1399 if (!map1 || !map2)
1400 goto error;
1401 if (isl_map_has_equal_params(map1, map2))
1402 return fn(map1, map2);
1403 if (isl_map_check_named_params(map1) < 0)
1404 goto error;
1405 if (isl_map_check_named_params(map2) < 0)
1406 goto error;
1407 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1408 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1409 return fn(map1, map2);
1410 error:
1411 isl_map_free(map1);
1412 isl_map_free(map2);
1413 return NULL;
1416 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1417 __isl_keep isl_map *map2,
1418 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1420 isl_bool r;
1422 if (!map1 || !map2)
1423 return isl_bool_error;
1424 if (isl_map_has_equal_params(map1, map2))
1425 return fn(map1, map2);
1426 if (isl_map_check_named_params(map1) < 0)
1427 return isl_bool_error;
1428 if (isl_map_check_named_params(map2) < 0)
1429 return isl_bool_error;
1430 map1 = isl_map_copy(map1);
1431 map2 = isl_map_copy(map2);
1432 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1433 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1434 r = fn(map1, map2);
1435 isl_map_free(map1);
1436 isl_map_free(map2);
1437 return r;
1440 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1442 struct isl_ctx *ctx;
1443 if (!bmap)
1444 return -1;
1445 ctx = bmap->ctx;
1446 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1447 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1448 return -1);
1449 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1450 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1451 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1452 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1453 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1454 isl_int *t;
1455 int j = isl_basic_map_alloc_inequality(bmap);
1456 if (j < 0)
1457 return -1;
1458 t = bmap->ineq[j];
1459 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1460 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1461 bmap->eq[-1] = t;
1462 bmap->n_eq++;
1463 bmap->n_ineq--;
1464 bmap->eq--;
1465 return 0;
1467 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1468 bmap->extra - bmap->n_div);
1469 return bmap->n_eq++;
1472 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1474 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1477 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1479 if (!bmap)
1480 return -1;
1481 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1482 bmap->n_eq -= n;
1483 return 0;
1486 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1488 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1491 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1493 isl_int *t;
1494 if (!bmap)
1495 return -1;
1496 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1498 if (pos != bmap->n_eq - 1) {
1499 t = bmap->eq[pos];
1500 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1501 bmap->eq[bmap->n_eq - 1] = t;
1503 bmap->n_eq--;
1504 return 0;
1507 /* Turn inequality "pos" of "bmap" into an equality.
1509 * In particular, we move the inequality in front of the equalities
1510 * and move the last inequality in the position of the moved inequality.
1511 * Note that isl_tab_make_equalities_explicit depends on this particular
1512 * change in the ordering of the constraints.
1514 void isl_basic_map_inequality_to_equality(
1515 struct isl_basic_map *bmap, unsigned pos)
1517 isl_int *t;
1519 t = bmap->ineq[pos];
1520 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1521 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1522 bmap->eq[-1] = t;
1523 bmap->n_eq++;
1524 bmap->n_ineq--;
1525 bmap->eq--;
1526 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1527 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1528 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1529 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1532 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1534 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1537 int isl_basic_map_alloc_inequality(__isl_keep isl_basic_map *bmap)
1539 struct isl_ctx *ctx;
1540 if (!bmap)
1541 return -1;
1542 ctx = bmap->ctx;
1543 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1544 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1545 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1546 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1547 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1548 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1549 1 + isl_basic_map_total_dim(bmap),
1550 bmap->extra - bmap->n_div);
1551 return bmap->n_ineq++;
1554 int isl_basic_set_alloc_inequality(__isl_keep isl_basic_set *bset)
1556 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1559 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1561 if (!bmap)
1562 return -1;
1563 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1564 bmap->n_ineq -= n;
1565 return 0;
1568 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1570 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1573 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1575 isl_int *t;
1576 if (!bmap)
1577 return -1;
1578 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1580 if (pos != bmap->n_ineq - 1) {
1581 t = bmap->ineq[pos];
1582 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1583 bmap->ineq[bmap->n_ineq - 1] = t;
1584 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
1586 bmap->n_ineq--;
1587 return 0;
1590 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1592 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1595 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1596 isl_int *eq)
1598 int k;
1600 bmap = isl_basic_map_cow(bmap);
1601 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1602 if (!bmap)
1603 return NULL;
1604 k = isl_basic_map_alloc_equality(bmap);
1605 if (k < 0)
1606 goto error;
1607 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1608 return bmap;
1609 error:
1610 isl_basic_map_free(bmap);
1611 return NULL;
1614 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1615 isl_int *eq)
1617 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1620 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1621 isl_int *ineq)
1623 int k;
1625 bmap = isl_basic_map_cow(bmap);
1626 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1627 if (!bmap)
1628 return NULL;
1629 k = isl_basic_map_alloc_inequality(bmap);
1630 if (k < 0)
1631 goto error;
1632 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1633 return bmap;
1634 error:
1635 isl_basic_map_free(bmap);
1636 return NULL;
1639 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1640 isl_int *ineq)
1642 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1645 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1647 if (!bmap)
1648 return -1;
1649 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1650 isl_seq_clr(bmap->div[bmap->n_div] +
1651 1 + 1 + isl_basic_map_total_dim(bmap),
1652 bmap->extra - bmap->n_div);
1653 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1654 return bmap->n_div++;
1657 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1659 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1662 /* Check that there are "n" dimensions of type "type" starting at "first"
1663 * in "bmap".
1665 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1666 enum isl_dim_type type, unsigned first, unsigned n)
1668 unsigned dim;
1670 if (!bmap)
1671 return isl_stat_error;
1672 dim = isl_basic_map_dim(bmap, type);
1673 if (first + n > dim || first + n < first)
1674 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1675 "position or range out of bounds",
1676 return isl_stat_error);
1677 return isl_stat_ok;
1680 /* Insert an extra integer division, prescribed by "div", to "bmap"
1681 * at (integer division) position "pos".
1683 * The integer division is first added at the end and then moved
1684 * into the right position.
1686 __isl_give isl_basic_map *isl_basic_map_insert_div(
1687 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1689 int i, k;
1691 bmap = isl_basic_map_cow(bmap);
1692 if (!bmap || !div)
1693 return isl_basic_map_free(bmap);
1695 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1696 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1697 "unexpected size", return isl_basic_map_free(bmap));
1698 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1699 return isl_basic_map_free(bmap);
1701 bmap = isl_basic_map_extend_space(bmap,
1702 isl_basic_map_get_space(bmap), 1, 0, 2);
1703 k = isl_basic_map_alloc_div(bmap);
1704 if (k < 0)
1705 return isl_basic_map_free(bmap);
1706 isl_seq_cpy(bmap->div[k], div->el, div->size);
1707 isl_int_set_si(bmap->div[k][div->size], 0);
1709 for (i = k; i > pos; --i)
1710 isl_basic_map_swap_div(bmap, i, i - 1);
1712 return bmap;
1715 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1717 if (!bmap)
1718 return isl_stat_error;
1719 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1720 bmap->n_div -= n;
1721 return isl_stat_ok;
1724 /* Copy constraint from src to dst, putting the vars of src at offset
1725 * dim_off in dst and the divs of src at offset div_off in dst.
1726 * If both sets are actually map, then dim_off applies to the input
1727 * variables.
1729 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1730 struct isl_basic_map *src_map, isl_int *src,
1731 unsigned in_off, unsigned out_off, unsigned div_off)
1733 unsigned src_nparam = isl_basic_map_dim(src_map, isl_dim_param);
1734 unsigned dst_nparam = isl_basic_map_dim(dst_map, isl_dim_param);
1735 unsigned src_in = isl_basic_map_dim(src_map, isl_dim_in);
1736 unsigned dst_in = isl_basic_map_dim(dst_map, isl_dim_in);
1737 unsigned src_out = isl_basic_map_dim(src_map, isl_dim_out);
1738 unsigned dst_out = isl_basic_map_dim(dst_map, isl_dim_out);
1739 isl_int_set(dst[0], src[0]);
1740 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1741 if (dst_nparam > src_nparam)
1742 isl_seq_clr(dst+1+src_nparam,
1743 dst_nparam - src_nparam);
1744 isl_seq_clr(dst+1+dst_nparam, in_off);
1745 isl_seq_cpy(dst+1+dst_nparam+in_off,
1746 src+1+src_nparam,
1747 isl_min(dst_in-in_off, src_in));
1748 if (dst_in-in_off > src_in)
1749 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1750 dst_in - in_off - src_in);
1751 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1752 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1753 src+1+src_nparam+src_in,
1754 isl_min(dst_out-out_off, src_out));
1755 if (dst_out-out_off > src_out)
1756 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1757 dst_out - out_off - src_out);
1758 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1759 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1760 src+1+src_nparam+src_in+src_out,
1761 isl_min(dst_map->extra-div_off, src_map->n_div));
1762 if (dst_map->n_div-div_off > src_map->n_div)
1763 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1764 div_off+src_map->n_div,
1765 dst_map->n_div - div_off - src_map->n_div);
1768 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1769 struct isl_basic_map *src_map, isl_int *src,
1770 unsigned in_off, unsigned out_off, unsigned div_off)
1772 isl_int_set(dst[0], src[0]);
1773 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1776 static __isl_give isl_basic_map *add_constraints(
1777 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2,
1778 unsigned i_pos, unsigned o_pos)
1780 int i;
1781 unsigned div_off;
1783 if (!bmap1 || !bmap2)
1784 goto error;
1786 div_off = bmap1->n_div;
1788 for (i = 0; i < bmap2->n_eq; ++i) {
1789 int i1 = isl_basic_map_alloc_equality(bmap1);
1790 if (i1 < 0)
1791 goto error;
1792 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1793 i_pos, o_pos, div_off);
1796 for (i = 0; i < bmap2->n_ineq; ++i) {
1797 int i1 = isl_basic_map_alloc_inequality(bmap1);
1798 if (i1 < 0)
1799 goto error;
1800 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1801 i_pos, o_pos, div_off);
1804 for (i = 0; i < bmap2->n_div; ++i) {
1805 int i1 = isl_basic_map_alloc_div(bmap1);
1806 if (i1 < 0)
1807 goto error;
1808 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1809 i_pos, o_pos, div_off);
1812 isl_basic_map_free(bmap2);
1814 return bmap1;
1816 error:
1817 isl_basic_map_free(bmap1);
1818 isl_basic_map_free(bmap2);
1819 return NULL;
1822 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1823 struct isl_basic_set *bset2, unsigned pos)
1825 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1826 bset_to_bmap(bset2), 0, pos));
1829 __isl_give isl_basic_map *isl_basic_map_extend_space(
1830 __isl_take isl_basic_map *base, __isl_take isl_space *space,
1831 unsigned extra, unsigned n_eq, unsigned n_ineq)
1833 struct isl_basic_map *ext;
1834 unsigned flags;
1835 int dims_ok;
1837 if (!space)
1838 goto error;
1840 if (!base)
1841 goto error;
1843 dims_ok = isl_space_is_equal(base->dim, space) &&
1844 base->extra >= base->n_div + extra;
1846 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1847 room_for_ineq(base, n_ineq)) {
1848 isl_space_free(space);
1849 return base;
1852 isl_assert(base->ctx, base->dim->nparam <= space->nparam, goto error);
1853 isl_assert(base->ctx, base->dim->n_in <= space->n_in, goto error);
1854 isl_assert(base->ctx, base->dim->n_out <= space->n_out, goto error);
1855 extra += base->extra;
1856 n_eq += base->n_eq;
1857 n_ineq += base->n_ineq;
1859 ext = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1860 space = NULL;
1861 if (!ext)
1862 goto error;
1864 if (dims_ok)
1865 ext->sample = isl_vec_copy(base->sample);
1866 flags = base->flags;
1867 ext = add_constraints(ext, base, 0, 0);
1868 if (ext) {
1869 ext->flags = flags;
1870 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1873 return ext;
1875 error:
1876 isl_space_free(space);
1877 isl_basic_map_free(base);
1878 return NULL;
1881 __isl_give isl_basic_set *isl_basic_set_extend_space(
1882 __isl_take isl_basic_set *base,
1883 __isl_take isl_space *dim, unsigned extra,
1884 unsigned n_eq, unsigned n_ineq)
1886 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1887 dim, extra, n_eq, n_ineq));
1890 struct isl_basic_map *isl_basic_map_extend_constraints(
1891 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1893 if (!base)
1894 return NULL;
1895 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1896 0, n_eq, n_ineq);
1899 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1900 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1901 unsigned n_eq, unsigned n_ineq)
1903 struct isl_basic_map *bmap;
1904 isl_space *dim;
1906 if (!base)
1907 return NULL;
1908 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1909 if (!dim)
1910 goto error;
1912 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1913 return bmap;
1914 error:
1915 isl_basic_map_free(base);
1916 return NULL;
1919 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1920 unsigned nparam, unsigned dim, unsigned extra,
1921 unsigned n_eq, unsigned n_ineq)
1923 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1924 nparam, 0, dim, extra, n_eq, n_ineq));
1927 struct isl_basic_set *isl_basic_set_extend_constraints(
1928 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1930 isl_basic_map *bmap = bset_to_bmap(base);
1931 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1932 return bset_from_bmap(bmap);
1935 __isl_give isl_basic_set *isl_basic_set_cow(__isl_take isl_basic_set *bset)
1937 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1940 __isl_give isl_basic_map *isl_basic_map_cow(__isl_take isl_basic_map *bmap)
1942 if (!bmap)
1943 return NULL;
1945 if (bmap->ref > 1) {
1946 bmap->ref--;
1947 bmap = isl_basic_map_dup(bmap);
1949 if (bmap) {
1950 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1951 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1953 return bmap;
1956 /* Clear all cached information in "map", either because it is about
1957 * to be modified or because it is being freed.
1958 * Always return the same pointer that is passed in.
1959 * This is needed for the use in isl_map_free.
1961 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1963 isl_basic_map_free(map->cached_simple_hull[0]);
1964 isl_basic_map_free(map->cached_simple_hull[1]);
1965 map->cached_simple_hull[0] = NULL;
1966 map->cached_simple_hull[1] = NULL;
1967 return map;
1970 __isl_give isl_set *isl_set_cow(__isl_take isl_set *set)
1972 return isl_map_cow(set);
1975 /* Return an isl_map that is equal to "map" and that has only
1976 * a single reference.
1978 * If the original input already has only one reference, then
1979 * simply return it, but clear all cached information, since
1980 * it may be rendered invalid by the operations that will be
1981 * performed on the result.
1983 * Otherwise, create a duplicate (without any cached information).
1985 __isl_give isl_map *isl_map_cow(__isl_take isl_map *map)
1987 if (!map)
1988 return NULL;
1990 if (map->ref == 1)
1991 return clear_caches(map);
1992 map->ref--;
1993 return isl_map_dup(map);
1996 static void swap_vars(struct isl_blk blk, isl_int *a,
1997 unsigned a_len, unsigned b_len)
1999 isl_seq_cpy(blk.data, a+a_len, b_len);
2000 isl_seq_cpy(blk.data+b_len, a, a_len);
2001 isl_seq_cpy(a, blk.data, b_len+a_len);
2004 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
2005 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
2007 int i;
2008 struct isl_blk blk;
2010 if (!bmap)
2011 goto error;
2013 isl_assert(bmap->ctx,
2014 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
2016 if (n1 == 0 || n2 == 0)
2017 return bmap;
2019 bmap = isl_basic_map_cow(bmap);
2020 if (!bmap)
2021 return NULL;
2023 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
2024 if (isl_blk_is_error(blk))
2025 goto error;
2027 for (i = 0; i < bmap->n_eq; ++i)
2028 swap_vars(blk,
2029 bmap->eq[i] + pos, n1, n2);
2031 for (i = 0; i < bmap->n_ineq; ++i)
2032 swap_vars(blk,
2033 bmap->ineq[i] + pos, n1, n2);
2035 for (i = 0; i < bmap->n_div; ++i)
2036 swap_vars(blk,
2037 bmap->div[i]+1 + pos, n1, n2);
2039 isl_blk_free(bmap->ctx, blk);
2041 ISL_F_CLR(bmap, ISL_BASIC_SET_SORTED);
2042 bmap = isl_basic_map_gauss(bmap, NULL);
2043 return isl_basic_map_finalize(bmap);
2044 error:
2045 isl_basic_map_free(bmap);
2046 return NULL;
2049 __isl_give isl_basic_map *isl_basic_map_set_to_empty(
2050 __isl_take isl_basic_map *bmap)
2052 int i = 0;
2053 unsigned total;
2054 if (!bmap)
2055 goto error;
2056 total = isl_basic_map_total_dim(bmap);
2057 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
2058 return isl_basic_map_free(bmap);
2059 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
2060 if (bmap->n_eq > 0)
2061 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
2062 else {
2063 i = isl_basic_map_alloc_equality(bmap);
2064 if (i < 0)
2065 goto error;
2067 isl_int_set_si(bmap->eq[i][0], 1);
2068 isl_seq_clr(bmap->eq[i]+1, total);
2069 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
2070 isl_vec_free(bmap->sample);
2071 bmap->sample = NULL;
2072 return isl_basic_map_finalize(bmap);
2073 error:
2074 isl_basic_map_free(bmap);
2075 return NULL;
2078 __isl_give isl_basic_set *isl_basic_set_set_to_empty(
2079 __isl_take isl_basic_set *bset)
2081 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
2084 __isl_give isl_basic_map *isl_basic_map_set_rational(
2085 __isl_take isl_basic_map *bmap)
2087 if (!bmap)
2088 return NULL;
2090 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2091 return bmap;
2093 bmap = isl_basic_map_cow(bmap);
2094 if (!bmap)
2095 return NULL;
2097 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
2099 return isl_basic_map_finalize(bmap);
2102 __isl_give isl_basic_set *isl_basic_set_set_rational(
2103 __isl_take isl_basic_set *bset)
2105 return isl_basic_map_set_rational(bset);
2108 __isl_give isl_basic_set *isl_basic_set_set_integral(
2109 __isl_take isl_basic_set *bset)
2111 if (!bset)
2112 return NULL;
2114 if (!ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
2115 return bset;
2117 bset = isl_basic_set_cow(bset);
2118 if (!bset)
2119 return NULL;
2121 ISL_F_CLR(bset, ISL_BASIC_MAP_RATIONAL);
2123 return isl_basic_set_finalize(bset);
2126 __isl_give isl_map *isl_map_set_rational(__isl_take isl_map *map)
2128 int i;
2130 map = isl_map_cow(map);
2131 if (!map)
2132 return NULL;
2133 for (i = 0; i < map->n; ++i) {
2134 map->p[i] = isl_basic_map_set_rational(map->p[i]);
2135 if (!map->p[i])
2136 goto error;
2138 return map;
2139 error:
2140 isl_map_free(map);
2141 return NULL;
2144 __isl_give isl_set *isl_set_set_rational(__isl_take isl_set *set)
2146 return isl_map_set_rational(set);
2149 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
2150 * of "bmap").
2152 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
2154 isl_int *t = bmap->div[a];
2155 bmap->div[a] = bmap->div[b];
2156 bmap->div[b] = t;
2159 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
2160 * div definitions accordingly.
2162 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
2164 int i;
2165 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
2167 swap_div(bmap, a, b);
2169 for (i = 0; i < bmap->n_eq; ++i)
2170 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
2172 for (i = 0; i < bmap->n_ineq; ++i)
2173 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
2175 for (i = 0; i < bmap->n_div; ++i)
2176 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
2177 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2180 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
2182 isl_seq_cpy(c, c + n, rem);
2183 isl_seq_clr(c + rem, n);
2186 /* Drop n dimensions starting at first.
2188 * In principle, this frees up some extra variables as the number
2189 * of columns remains constant, but we would have to extend
2190 * the div array too as the number of rows in this array is assumed
2191 * to be equal to extra.
2193 __isl_give isl_basic_set *isl_basic_set_drop_dims(
2194 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2196 return isl_basic_map_drop(bset_to_bmap(bset), isl_dim_set, first, n);
2199 /* Move "n" divs starting at "first" to the end of the list of divs.
2201 static struct isl_basic_map *move_divs_last(struct isl_basic_map *bmap,
2202 unsigned first, unsigned n)
2204 isl_int **div;
2205 int i;
2207 if (first + n == bmap->n_div)
2208 return bmap;
2210 div = isl_alloc_array(bmap->ctx, isl_int *, n);
2211 if (!div)
2212 goto error;
2213 for (i = 0; i < n; ++i)
2214 div[i] = bmap->div[first + i];
2215 for (i = 0; i < bmap->n_div - first - n; ++i)
2216 bmap->div[first + i] = bmap->div[first + n + i];
2217 for (i = 0; i < n; ++i)
2218 bmap->div[bmap->n_div - n + i] = div[i];
2219 free(div);
2220 return bmap;
2221 error:
2222 isl_basic_map_free(bmap);
2223 return NULL;
2226 /* Check that there are "n" dimensions of type "type" starting at "first"
2227 * in "map".
2229 static isl_stat isl_map_check_range(__isl_keep isl_map *map,
2230 enum isl_dim_type type, unsigned first, unsigned n)
2232 if (!map)
2233 return isl_stat_error;
2234 if (first + n > isl_map_dim(map, type) || first + n < first)
2235 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2236 "position or range out of bounds",
2237 return isl_stat_error);
2238 return isl_stat_ok;
2241 /* Drop "n" dimensions of type "type" starting at "first".
2243 * In principle, this frees up some extra variables as the number
2244 * of columns remains constant, but we would have to extend
2245 * the div array too as the number of rows in this array is assumed
2246 * to be equal to extra.
2248 __isl_give isl_basic_map *isl_basic_map_drop(__isl_take isl_basic_map *bmap,
2249 enum isl_dim_type type, unsigned first, unsigned n)
2251 int i;
2252 unsigned dim;
2253 unsigned offset;
2254 unsigned left;
2256 if (!bmap)
2257 goto error;
2259 dim = isl_basic_map_dim(bmap, type);
2260 isl_assert(bmap->ctx, first + n <= dim, goto error);
2262 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2263 return bmap;
2265 bmap = isl_basic_map_cow(bmap);
2266 if (!bmap)
2267 return NULL;
2269 offset = isl_basic_map_offset(bmap, type) + first;
2270 left = isl_basic_map_total_dim(bmap) - (offset - 1) - n;
2271 for (i = 0; i < bmap->n_eq; ++i)
2272 constraint_drop_vars(bmap->eq[i]+offset, n, left);
2274 for (i = 0; i < bmap->n_ineq; ++i)
2275 constraint_drop_vars(bmap->ineq[i]+offset, n, left);
2277 for (i = 0; i < bmap->n_div; ++i)
2278 constraint_drop_vars(bmap->div[i]+1+offset, n, left);
2280 if (type == isl_dim_div) {
2281 bmap = move_divs_last(bmap, first, n);
2282 if (!bmap)
2283 goto error;
2284 if (isl_basic_map_free_div(bmap, n) < 0)
2285 return isl_basic_map_free(bmap);
2286 } else
2287 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
2288 if (!bmap->dim)
2289 goto error;
2291 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2292 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2293 bmap = isl_basic_map_simplify(bmap);
2294 return isl_basic_map_finalize(bmap);
2295 error:
2296 isl_basic_map_free(bmap);
2297 return NULL;
2300 __isl_give isl_basic_set *isl_basic_set_drop(__isl_take isl_basic_set *bset,
2301 enum isl_dim_type type, unsigned first, unsigned n)
2303 return bset_from_bmap(isl_basic_map_drop(bset_to_bmap(bset),
2304 type, first, n));
2307 /* No longer consider "map" to be normalized.
2309 static __isl_give isl_map *isl_map_unmark_normalized(__isl_take isl_map *map)
2311 if (!map)
2312 return NULL;
2313 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2314 return map;
2317 __isl_give isl_map *isl_map_drop(__isl_take isl_map *map,
2318 enum isl_dim_type type, unsigned first, unsigned n)
2320 int i;
2322 if (isl_map_check_range(map, type, first, n) < 0)
2323 return isl_map_free(map);
2325 if (n == 0 && !isl_space_is_named_or_nested(map->dim, type))
2326 return map;
2327 map = isl_map_cow(map);
2328 if (!map)
2329 goto error;
2330 map->dim = isl_space_drop_dims(map->dim, type, first, n);
2331 if (!map->dim)
2332 goto error;
2334 for (i = 0; i < map->n; ++i) {
2335 map->p[i] = isl_basic_map_drop(map->p[i], type, first, n);
2336 if (!map->p[i])
2337 goto error;
2339 map = isl_map_unmark_normalized(map);
2341 return map;
2342 error:
2343 isl_map_free(map);
2344 return NULL;
2347 __isl_give isl_set *isl_set_drop(__isl_take isl_set *set,
2348 enum isl_dim_type type, unsigned first, unsigned n)
2350 return set_from_map(isl_map_drop(set_to_map(set), type, first, n));
2354 * We don't cow, as the div is assumed to be redundant.
2356 __isl_give isl_basic_map *isl_basic_map_drop_div(
2357 __isl_take isl_basic_map *bmap, unsigned div)
2359 int i;
2360 unsigned pos;
2362 if (!bmap)
2363 goto error;
2365 pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
2367 isl_assert(bmap->ctx, div < bmap->n_div, goto error);
2369 for (i = 0; i < bmap->n_eq; ++i)
2370 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
2372 for (i = 0; i < bmap->n_ineq; ++i) {
2373 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
2374 isl_basic_map_drop_inequality(bmap, i);
2375 --i;
2376 continue;
2378 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
2381 for (i = 0; i < bmap->n_div; ++i)
2382 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
2384 if (div != bmap->n_div - 1) {
2385 int j;
2386 isl_int *t = bmap->div[div];
2388 for (j = div; j < bmap->n_div - 1; ++j)
2389 bmap->div[j] = bmap->div[j+1];
2391 bmap->div[bmap->n_div - 1] = t;
2393 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
2394 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
2395 if (isl_basic_map_free_div(bmap, 1) < 0)
2396 return isl_basic_map_free(bmap);
2398 return bmap;
2399 error:
2400 isl_basic_map_free(bmap);
2401 return NULL;
2404 /* Eliminate the specified n dimensions starting at first from the
2405 * constraints, without removing the dimensions from the space.
2406 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2408 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
2409 enum isl_dim_type type, unsigned first, unsigned n)
2411 int i;
2413 if (n == 0)
2414 return map;
2416 if (isl_map_check_range(map, type, first, n) < 0)
2417 return isl_map_free(map);
2419 map = isl_map_cow(map);
2420 if (!map)
2421 return NULL;
2423 for (i = 0; i < map->n; ++i) {
2424 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
2425 if (!map->p[i])
2426 goto error;
2428 return map;
2429 error:
2430 isl_map_free(map);
2431 return NULL;
2434 /* Eliminate the specified n dimensions starting at first from the
2435 * constraints, without removing the dimensions from the space.
2436 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2438 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2439 enum isl_dim_type type, unsigned first, unsigned n)
2441 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2444 /* Eliminate the specified n dimensions starting at first from the
2445 * constraints, without removing the dimensions from the space.
2446 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2448 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2449 unsigned first, unsigned n)
2451 return isl_set_eliminate(set, isl_dim_set, first, n);
2454 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2455 __isl_take isl_basic_map *bmap)
2457 if (!bmap)
2458 return NULL;
2459 bmap = isl_basic_map_eliminate_vars(bmap,
2460 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2461 if (!bmap)
2462 return NULL;
2463 bmap->n_div = 0;
2464 return isl_basic_map_finalize(bmap);
2467 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2468 __isl_take isl_basic_set *bset)
2470 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2473 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2475 int i;
2477 if (!map)
2478 return NULL;
2479 if (map->n == 0)
2480 return map;
2482 map = isl_map_cow(map);
2483 if (!map)
2484 return NULL;
2486 for (i = 0; i < map->n; ++i) {
2487 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2488 if (!map->p[i])
2489 goto error;
2491 return map;
2492 error:
2493 isl_map_free(map);
2494 return NULL;
2497 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2499 return isl_map_remove_divs(set);
2502 __isl_give isl_basic_map *isl_basic_map_remove_dims(
2503 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
2504 unsigned first, unsigned n)
2506 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2507 return isl_basic_map_free(bmap);
2508 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2509 return bmap;
2510 bmap = isl_basic_map_eliminate_vars(bmap,
2511 isl_basic_map_offset(bmap, type) - 1 + first, n);
2512 if (!bmap)
2513 return bmap;
2514 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2515 return bmap;
2516 bmap = isl_basic_map_drop(bmap, type, first, n);
2517 return bmap;
2520 /* Return true if the definition of the given div (recursively) involves
2521 * any of the given variables.
2523 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2524 unsigned first, unsigned n)
2526 int i;
2527 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2529 if (isl_int_is_zero(bmap->div[div][0]))
2530 return isl_bool_false;
2531 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2532 return isl_bool_true;
2534 for (i = bmap->n_div - 1; i >= 0; --i) {
2535 isl_bool involves;
2537 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2538 continue;
2539 involves = div_involves_vars(bmap, i, first, n);
2540 if (involves < 0 || involves)
2541 return involves;
2544 return isl_bool_false;
2547 /* Try and add a lower and/or upper bound on "div" to "bmap"
2548 * based on inequality "i".
2549 * "total" is the total number of variables (excluding the divs).
2550 * "v" is a temporary object that can be used during the calculations.
2551 * If "lb" is set, then a lower bound should be constructed.
2552 * If "ub" is set, then an upper bound should be constructed.
2554 * The calling function has already checked that the inequality does not
2555 * reference "div", but we still need to check that the inequality is
2556 * of the right form. We'll consider the case where we want to construct
2557 * a lower bound. The construction of upper bounds is similar.
2559 * Let "div" be of the form
2561 * q = floor((a + f(x))/d)
2563 * We essentially check if constraint "i" is of the form
2565 * b + f(x) >= 0
2567 * so that we can use it to derive a lower bound on "div".
2568 * However, we allow a slightly more general form
2570 * b + g(x) >= 0
2572 * with the condition that the coefficients of g(x) - f(x) are all
2573 * divisible by d.
2574 * Rewriting this constraint as
2576 * 0 >= -b - g(x)
2578 * adding a + f(x) to both sides and dividing by d, we obtain
2580 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2582 * Taking the floor on both sides, we obtain
2584 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2586 * or
2588 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2590 * In the case of an upper bound, we construct the constraint
2592 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2595 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2596 __isl_take isl_basic_map *bmap, int div, int i,
2597 unsigned total, isl_int v, int lb, int ub)
2599 int j;
2601 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2602 if (lb) {
2603 isl_int_sub(v, bmap->ineq[i][1 + j],
2604 bmap->div[div][1 + 1 + j]);
2605 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2607 if (ub) {
2608 isl_int_add(v, bmap->ineq[i][1 + j],
2609 bmap->div[div][1 + 1 + j]);
2610 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2613 if (!lb && !ub)
2614 return bmap;
2616 bmap = isl_basic_map_cow(bmap);
2617 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2618 if (lb) {
2619 int k = isl_basic_map_alloc_inequality(bmap);
2620 if (k < 0)
2621 goto error;
2622 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2623 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2624 bmap->div[div][1 + j]);
2625 isl_int_cdiv_q(bmap->ineq[k][j],
2626 bmap->ineq[k][j], bmap->div[div][0]);
2628 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2630 if (ub) {
2631 int k = isl_basic_map_alloc_inequality(bmap);
2632 if (k < 0)
2633 goto error;
2634 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2635 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2636 bmap->div[div][1 + j]);
2637 isl_int_fdiv_q(bmap->ineq[k][j],
2638 bmap->ineq[k][j], bmap->div[div][0]);
2640 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2643 return bmap;
2644 error:
2645 isl_basic_map_free(bmap);
2646 return NULL;
2649 /* This function is called right before "div" is eliminated from "bmap"
2650 * using Fourier-Motzkin.
2651 * Look through the constraints of "bmap" for constraints on the argument
2652 * of the integer division and use them to construct constraints on the
2653 * integer division itself. These constraints can then be combined
2654 * during the Fourier-Motzkin elimination.
2655 * Note that it is only useful to introduce lower bounds on "div"
2656 * if "bmap" already contains upper bounds on "div" as the newly
2657 * introduce lower bounds can then be combined with the pre-existing
2658 * upper bounds. Similarly for upper bounds.
2659 * We therefore first check if "bmap" contains any lower and/or upper bounds
2660 * on "div".
2662 * It is interesting to note that the introduction of these constraints
2663 * can indeed lead to more accurate results, even when compared to
2664 * deriving constraints on the argument of "div" from constraints on "div".
2665 * Consider, for example, the set
2667 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2669 * The second constraint can be rewritten as
2671 * 2 * [(-i-2j+3)/4] + k >= 0
2673 * from which we can derive
2675 * -i - 2j + 3 >= -2k
2677 * or
2679 * i + 2j <= 3 + 2k
2681 * Combined with the first constraint, we obtain
2683 * -3 <= 3 + 2k or k >= -3
2685 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2686 * the first constraint, we obtain
2688 * [(i + 2j)/4] >= [-3/4] = -1
2690 * Combining this constraint with the second constraint, we obtain
2692 * k >= -2
2694 static __isl_give isl_basic_map *insert_bounds_on_div(
2695 __isl_take isl_basic_map *bmap, int div)
2697 int i;
2698 int check_lb, check_ub;
2699 isl_int v;
2700 unsigned total;
2702 if (!bmap)
2703 return NULL;
2705 if (isl_int_is_zero(bmap->div[div][0]))
2706 return bmap;
2708 total = isl_space_dim(bmap->dim, isl_dim_all);
2710 check_lb = 0;
2711 check_ub = 0;
2712 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2713 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2714 if (s > 0)
2715 check_ub = 1;
2716 if (s < 0)
2717 check_lb = 1;
2720 if (!check_lb && !check_ub)
2721 return bmap;
2723 isl_int_init(v);
2725 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2726 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2727 continue;
2729 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2730 check_lb, check_ub);
2733 isl_int_clear(v);
2735 return bmap;
2738 /* Remove all divs (recursively) involving any of the given dimensions
2739 * in their definitions.
2741 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2742 __isl_take isl_basic_map *bmap,
2743 enum isl_dim_type type, unsigned first, unsigned n)
2745 int i;
2747 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2748 return isl_basic_map_free(bmap);
2749 first += isl_basic_map_offset(bmap, type);
2751 for (i = bmap->n_div - 1; i >= 0; --i) {
2752 isl_bool involves;
2754 involves = div_involves_vars(bmap, i, first, n);
2755 if (involves < 0)
2756 return isl_basic_map_free(bmap);
2757 if (!involves)
2758 continue;
2759 bmap = insert_bounds_on_div(bmap, i);
2760 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2761 if (!bmap)
2762 return NULL;
2763 i = bmap->n_div;
2766 return bmap;
2769 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2770 __isl_take isl_basic_set *bset,
2771 enum isl_dim_type type, unsigned first, unsigned n)
2773 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2776 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2777 enum isl_dim_type type, unsigned first, unsigned n)
2779 int i;
2781 if (!map)
2782 return NULL;
2783 if (map->n == 0)
2784 return map;
2786 map = isl_map_cow(map);
2787 if (!map)
2788 return NULL;
2790 for (i = 0; i < map->n; ++i) {
2791 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2792 type, first, n);
2793 if (!map->p[i])
2794 goto error;
2796 return map;
2797 error:
2798 isl_map_free(map);
2799 return NULL;
2802 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2803 enum isl_dim_type type, unsigned first, unsigned n)
2805 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2806 type, first, n));
2809 /* Does the description of "bmap" depend on the specified dimensions?
2810 * We also check whether the dimensions appear in any of the div definitions.
2811 * In principle there is no need for this check. If the dimensions appear
2812 * in a div definition, they also appear in the defining constraints of that
2813 * div.
2815 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2816 enum isl_dim_type type, unsigned first, unsigned n)
2818 int i;
2820 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2821 return isl_bool_error;
2823 first += isl_basic_map_offset(bmap, type);
2824 for (i = 0; i < bmap->n_eq; ++i)
2825 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2826 return isl_bool_true;
2827 for (i = 0; i < bmap->n_ineq; ++i)
2828 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2829 return isl_bool_true;
2830 for (i = 0; i < bmap->n_div; ++i) {
2831 if (isl_int_is_zero(bmap->div[i][0]))
2832 continue;
2833 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2834 return isl_bool_true;
2837 return isl_bool_false;
2840 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2841 enum isl_dim_type type, unsigned first, unsigned n)
2843 int i;
2845 if (isl_map_check_range(map, type, first, n) < 0)
2846 return isl_bool_error;
2848 for (i = 0; i < map->n; ++i) {
2849 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2850 type, first, n);
2851 if (involves < 0 || involves)
2852 return involves;
2855 return isl_bool_false;
2858 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2859 enum isl_dim_type type, unsigned first, unsigned n)
2861 return isl_basic_map_involves_dims(bset, type, first, n);
2864 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2865 enum isl_dim_type type, unsigned first, unsigned n)
2867 return isl_map_involves_dims(set, type, first, n);
2870 /* Drop all constraints in bmap that involve any of the dimensions
2871 * first to first+n-1.
2873 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2874 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2876 int i;
2878 if (n == 0)
2879 return bmap;
2881 bmap = isl_basic_map_cow(bmap);
2883 if (!bmap)
2884 return NULL;
2886 for (i = bmap->n_eq - 1; i >= 0; --i) {
2887 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2888 continue;
2889 isl_basic_map_drop_equality(bmap, i);
2892 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2893 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2894 continue;
2895 isl_basic_map_drop_inequality(bmap, i);
2898 bmap = isl_basic_map_add_known_div_constraints(bmap);
2899 return bmap;
2902 /* Drop all constraints in bset that involve any of the dimensions
2903 * first to first+n-1.
2905 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2906 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2908 return isl_basic_map_drop_constraints_involving(bset, first, n);
2911 /* Drop all constraints in bmap that do not involve any of the dimensions
2912 * first to first + n - 1 of the given type.
2914 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2915 __isl_take isl_basic_map *bmap,
2916 enum isl_dim_type type, unsigned first, unsigned n)
2918 int i;
2920 if (n == 0) {
2921 isl_space *space = isl_basic_map_get_space(bmap);
2922 isl_basic_map_free(bmap);
2923 return isl_basic_map_universe(space);
2925 bmap = isl_basic_map_cow(bmap);
2926 if (!bmap)
2927 return NULL;
2929 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2930 return isl_basic_map_free(bmap);
2932 first += isl_basic_map_offset(bmap, type) - 1;
2934 for (i = bmap->n_eq - 1; i >= 0; --i) {
2935 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2936 continue;
2937 isl_basic_map_drop_equality(bmap, i);
2940 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2941 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2942 continue;
2943 isl_basic_map_drop_inequality(bmap, i);
2946 bmap = isl_basic_map_add_known_div_constraints(bmap);
2947 return bmap;
2950 /* Drop all constraints in bset that do not involve any of the dimensions
2951 * first to first + n - 1 of the given type.
2953 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2954 __isl_take isl_basic_set *bset,
2955 enum isl_dim_type type, unsigned first, unsigned n)
2957 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2958 type, first, n);
2961 /* Drop all constraints in bmap that involve any of the dimensions
2962 * first to first + n - 1 of the given type.
2964 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2965 __isl_take isl_basic_map *bmap,
2966 enum isl_dim_type type, unsigned first, unsigned n)
2968 if (!bmap)
2969 return NULL;
2970 if (n == 0)
2971 return bmap;
2973 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2974 return isl_basic_map_free(bmap);
2976 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2977 first += isl_basic_map_offset(bmap, type) - 1;
2978 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2981 /* Drop all constraints in bset that involve any of the dimensions
2982 * first to first + n - 1 of the given type.
2984 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2985 __isl_take isl_basic_set *bset,
2986 enum isl_dim_type type, unsigned first, unsigned n)
2988 return isl_basic_map_drop_constraints_involving_dims(bset,
2989 type, first, n);
2992 /* Drop constraints from "map" by applying "drop" to each basic map.
2994 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2995 enum isl_dim_type type, unsigned first, unsigned n,
2996 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2997 enum isl_dim_type type, unsigned first, unsigned n))
2999 int i;
3001 if (isl_map_check_range(map, type, first, n) < 0)
3002 return isl_map_free(map);
3004 map = isl_map_cow(map);
3005 if (!map)
3006 return NULL;
3008 for (i = 0; i < map->n; ++i) {
3009 map->p[i] = drop(map->p[i], type, first, n);
3010 if (!map->p[i])
3011 return isl_map_free(map);
3014 if (map->n > 1)
3015 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3017 return map;
3020 /* Drop all constraints in map that involve any of the dimensions
3021 * first to first + n - 1 of the given type.
3023 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
3024 __isl_take isl_map *map,
3025 enum isl_dim_type type, unsigned first, unsigned n)
3027 if (n == 0)
3028 return map;
3029 return drop_constraints(map, type, first, n,
3030 &isl_basic_map_drop_constraints_involving_dims);
3033 /* Drop all constraints in "map" that do not involve any of the dimensions
3034 * first to first + n - 1 of the given type.
3036 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
3037 __isl_take isl_map *map,
3038 enum isl_dim_type type, unsigned first, unsigned n)
3040 if (n == 0) {
3041 isl_space *space = isl_map_get_space(map);
3042 isl_map_free(map);
3043 return isl_map_universe(space);
3045 return drop_constraints(map, type, first, n,
3046 &isl_basic_map_drop_constraints_not_involving_dims);
3049 /* Drop all constraints in set that involve any of the dimensions
3050 * first to first + n - 1 of the given type.
3052 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
3053 __isl_take isl_set *set,
3054 enum isl_dim_type type, unsigned first, unsigned n)
3056 return isl_map_drop_constraints_involving_dims(set, type, first, n);
3059 /* Drop all constraints in "set" that do not involve any of the dimensions
3060 * first to first + n - 1 of the given type.
3062 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
3063 __isl_take isl_set *set,
3064 enum isl_dim_type type, unsigned first, unsigned n)
3066 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
3069 /* Does local variable "div" of "bmap" have a complete explicit representation?
3070 * Having a complete explicit representation requires not only
3071 * an explicit representation, but also that all local variables
3072 * that appear in this explicit representation in turn have
3073 * a complete explicit representation.
3075 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
3077 int i;
3078 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
3079 isl_bool marked;
3081 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
3082 if (marked < 0 || marked)
3083 return isl_bool_not(marked);
3085 for (i = bmap->n_div - 1; i >= 0; --i) {
3086 isl_bool known;
3088 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
3089 continue;
3090 known = isl_basic_map_div_is_known(bmap, i);
3091 if (known < 0 || !known)
3092 return known;
3095 return isl_bool_true;
3098 /* Remove all divs that are unknown or defined in terms of unknown divs.
3100 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
3101 __isl_take isl_basic_map *bmap)
3103 int i;
3105 if (!bmap)
3106 return NULL;
3108 for (i = bmap->n_div - 1; i >= 0; --i) {
3109 if (isl_basic_map_div_is_known(bmap, i))
3110 continue;
3111 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
3112 if (!bmap)
3113 return NULL;
3114 i = bmap->n_div;
3117 return bmap;
3120 /* Remove all divs that are unknown or defined in terms of unknown divs.
3122 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
3123 __isl_take isl_basic_set *bset)
3125 return isl_basic_map_remove_unknown_divs(bset);
3128 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
3130 int i;
3132 if (!map)
3133 return NULL;
3134 if (map->n == 0)
3135 return map;
3137 map = isl_map_cow(map);
3138 if (!map)
3139 return NULL;
3141 for (i = 0; i < map->n; ++i) {
3142 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
3143 if (!map->p[i])
3144 goto error;
3146 return map;
3147 error:
3148 isl_map_free(map);
3149 return NULL;
3152 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
3154 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
3157 __isl_give isl_basic_set *isl_basic_set_remove_dims(
3158 __isl_take isl_basic_set *bset,
3159 enum isl_dim_type type, unsigned first, unsigned n)
3161 isl_basic_map *bmap = bset_to_bmap(bset);
3162 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
3163 return bset_from_bmap(bmap);
3166 __isl_give isl_map *isl_map_remove_dims(__isl_take isl_map *map,
3167 enum isl_dim_type type, unsigned first, unsigned n)
3169 int i;
3171 if (n == 0)
3172 return map;
3174 map = isl_map_cow(map);
3175 if (isl_map_check_range(map, type, first, n) < 0)
3176 return isl_map_free(map);
3178 for (i = 0; i < map->n; ++i) {
3179 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
3180 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
3181 if (!map->p[i])
3182 goto error;
3184 map = isl_map_drop(map, type, first, n);
3185 return map;
3186 error:
3187 isl_map_free(map);
3188 return NULL;
3191 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
3192 enum isl_dim_type type, unsigned first, unsigned n)
3194 return set_from_map(isl_map_remove_dims(set_to_map(bset),
3195 type, first, n));
3198 /* Project out n inputs starting at first using Fourier-Motzkin */
3199 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
3200 unsigned first, unsigned n)
3202 return isl_map_remove_dims(map, isl_dim_in, first, n);
3205 static void dump_term(struct isl_basic_map *bmap,
3206 isl_int c, int pos, FILE *out)
3208 const char *name;
3209 unsigned in = isl_basic_map_dim(bmap, isl_dim_in);
3210 unsigned dim = in + isl_basic_map_dim(bmap, isl_dim_out);
3211 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
3212 if (!pos)
3213 isl_int_print(out, c, 0);
3214 else {
3215 if (!isl_int_is_one(c))
3216 isl_int_print(out, c, 0);
3217 if (pos < 1 + nparam) {
3218 name = isl_space_get_dim_name(bmap->dim,
3219 isl_dim_param, pos - 1);
3220 if (name)
3221 fprintf(out, "%s", name);
3222 else
3223 fprintf(out, "p%d", pos - 1);
3224 } else if (pos < 1 + nparam + in)
3225 fprintf(out, "i%d", pos - 1 - nparam);
3226 else if (pos < 1 + nparam + dim)
3227 fprintf(out, "o%d", pos - 1 - nparam - in);
3228 else
3229 fprintf(out, "e%d", pos - 1 - nparam - dim);
3233 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
3234 int sign, FILE *out)
3236 int i;
3237 int first;
3238 unsigned len = 1 + isl_basic_map_total_dim(bmap);
3239 isl_int v;
3241 isl_int_init(v);
3242 for (i = 0, first = 1; i < len; ++i) {
3243 if (isl_int_sgn(c[i]) * sign <= 0)
3244 continue;
3245 if (!first)
3246 fprintf(out, " + ");
3247 first = 0;
3248 isl_int_abs(v, c[i]);
3249 dump_term(bmap, v, i, out);
3251 isl_int_clear(v);
3252 if (first)
3253 fprintf(out, "0");
3256 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
3257 const char *op, FILE *out, int indent)
3259 int i;
3261 fprintf(out, "%*s", indent, "");
3263 dump_constraint_sign(bmap, c, 1, out);
3264 fprintf(out, " %s ", op);
3265 dump_constraint_sign(bmap, c, -1, out);
3267 fprintf(out, "\n");
3269 for (i = bmap->n_div; i < bmap->extra; ++i) {
3270 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
3271 continue;
3272 fprintf(out, "%*s", indent, "");
3273 fprintf(out, "ERROR: unused div coefficient not zero\n");
3274 abort();
3278 static void dump_constraints(struct isl_basic_map *bmap,
3279 isl_int **c, unsigned n,
3280 const char *op, FILE *out, int indent)
3282 int i;
3284 for (i = 0; i < n; ++i)
3285 dump_constraint(bmap, c[i], op, out, indent);
3288 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
3290 int j;
3291 int first = 1;
3292 unsigned total = isl_basic_map_total_dim(bmap);
3294 for (j = 0; j < 1 + total; ++j) {
3295 if (isl_int_is_zero(exp[j]))
3296 continue;
3297 if (!first && isl_int_is_pos(exp[j]))
3298 fprintf(out, "+");
3299 dump_term(bmap, exp[j], j, out);
3300 first = 0;
3304 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
3306 int i;
3308 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
3309 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
3311 for (i = 0; i < bmap->n_div; ++i) {
3312 fprintf(out, "%*s", indent, "");
3313 fprintf(out, "e%d = [(", i);
3314 dump_affine(bmap, bmap->div[i]+1, out);
3315 fprintf(out, ")/");
3316 isl_int_print(out, bmap->div[i][0], 0);
3317 fprintf(out, "]\n");
3321 void isl_basic_set_print_internal(struct isl_basic_set *bset,
3322 FILE *out, int indent)
3324 if (!bset) {
3325 fprintf(out, "null basic set\n");
3326 return;
3329 fprintf(out, "%*s", indent, "");
3330 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
3331 bset->ref, bset->dim->nparam, bset->dim->n_out,
3332 bset->extra, bset->flags);
3333 dump(bset_to_bmap(bset), out, indent);
3336 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
3337 FILE *out, int indent)
3339 if (!bmap) {
3340 fprintf(out, "null basic map\n");
3341 return;
3344 fprintf(out, "%*s", indent, "");
3345 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
3346 "flags: %x, n_name: %d\n",
3347 bmap->ref,
3348 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
3349 bmap->extra, bmap->flags, bmap->dim->n_id);
3350 dump(bmap, out, indent);
3353 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
3355 unsigned total;
3356 if (!bmap)
3357 return -1;
3358 total = isl_basic_map_total_dim(bmap);
3359 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
3360 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
3361 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
3362 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
3363 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
3364 return 0;
3367 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
3368 unsigned flags)
3370 if (isl_space_check_is_set(space) < 0)
3371 goto error;
3372 return isl_map_alloc_space(space, n, flags);
3373 error:
3374 isl_space_free(space);
3375 return NULL;
3378 /* Make sure "map" has room for at least "n" more basic maps.
3380 __isl_give isl_map *isl_map_grow(__isl_take isl_map *map, int n)
3382 int i;
3383 struct isl_map *grown = NULL;
3385 if (!map)
3386 return NULL;
3387 isl_assert(map->ctx, n >= 0, goto error);
3388 if (map->n + n <= map->size)
3389 return map;
3390 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
3391 if (!grown)
3392 goto error;
3393 for (i = 0; i < map->n; ++i) {
3394 grown->p[i] = isl_basic_map_copy(map->p[i]);
3395 if (!grown->p[i])
3396 goto error;
3397 grown->n++;
3399 isl_map_free(map);
3400 return grown;
3401 error:
3402 isl_map_free(grown);
3403 isl_map_free(map);
3404 return NULL;
3407 /* Make sure "set" has room for at least "n" more basic sets.
3409 struct isl_set *isl_set_grow(struct isl_set *set, int n)
3411 return set_from_map(isl_map_grow(set_to_map(set), n));
3414 __isl_give isl_set *isl_set_from_basic_set(__isl_take isl_basic_set *bset)
3416 return isl_map_from_basic_map(bset);
3419 __isl_give isl_map *isl_map_from_basic_map(__isl_take isl_basic_map *bmap)
3421 struct isl_map *map;
3423 if (!bmap)
3424 return NULL;
3426 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3427 return isl_map_add_basic_map(map, bmap);
3430 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3431 __isl_take isl_basic_set *bset)
3433 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3434 bset_to_bmap(bset)));
3437 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3439 return isl_map_free(set);
3442 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3444 int i;
3446 if (!set) {
3447 fprintf(out, "null set\n");
3448 return;
3451 fprintf(out, "%*s", indent, "");
3452 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3453 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3454 set->flags);
3455 for (i = 0; i < set->n; ++i) {
3456 fprintf(out, "%*s", indent, "");
3457 fprintf(out, "basic set %d:\n", i);
3458 isl_basic_set_print_internal(set->p[i], out, indent+4);
3462 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3464 int i;
3466 if (!map) {
3467 fprintf(out, "null map\n");
3468 return;
3471 fprintf(out, "%*s", indent, "");
3472 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3473 "flags: %x, n_name: %d\n",
3474 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3475 map->dim->n_out, map->flags, map->dim->n_id);
3476 for (i = 0; i < map->n; ++i) {
3477 fprintf(out, "%*s", indent, "");
3478 fprintf(out, "basic map %d:\n", i);
3479 isl_basic_map_print_internal(map->p[i], out, indent+4);
3483 __isl_give isl_basic_map *isl_basic_map_intersect_domain(
3484 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3486 struct isl_basic_map *bmap_domain;
3488 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3489 goto error;
3491 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3492 isl_assert(bset->ctx,
3493 isl_basic_map_compatible_domain(bmap, bset), goto error);
3495 bmap = isl_basic_map_cow(bmap);
3496 if (!bmap)
3497 goto error;
3498 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3499 bset->n_div, bset->n_eq, bset->n_ineq);
3500 bmap_domain = isl_basic_map_from_domain(bset);
3501 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3503 bmap = isl_basic_map_simplify(bmap);
3504 return isl_basic_map_finalize(bmap);
3505 error:
3506 isl_basic_map_free(bmap);
3507 isl_basic_set_free(bset);
3508 return NULL;
3511 /* Check that the space of "bset" is the same as that of the range of "bmap".
3513 static isl_stat isl_basic_map_check_compatible_range(
3514 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3516 isl_bool ok;
3518 ok = isl_basic_map_compatible_range(bmap, bset);
3519 if (ok < 0)
3520 return isl_stat_error;
3521 if (!ok)
3522 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3523 "incompatible spaces", return isl_stat_error);
3525 return isl_stat_ok;
3528 __isl_give isl_basic_map *isl_basic_map_intersect_range(
3529 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *bset)
3531 struct isl_basic_map *bmap_range;
3533 if (isl_basic_map_check_equal_params(bmap, bset_to_bmap(bset)) < 0)
3534 goto error;
3536 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3537 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3538 goto error;
3540 if (isl_basic_set_plain_is_universe(bset)) {
3541 isl_basic_set_free(bset);
3542 return bmap;
3545 bmap = isl_basic_map_cow(bmap);
3546 if (!bmap)
3547 goto error;
3548 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3549 bset->n_div, bset->n_eq, bset->n_ineq);
3550 bmap_range = bset_to_bmap(bset);
3551 bmap = add_constraints(bmap, bmap_range, 0, 0);
3553 bmap = isl_basic_map_simplify(bmap);
3554 return isl_basic_map_finalize(bmap);
3555 error:
3556 isl_basic_map_free(bmap);
3557 isl_basic_set_free(bset);
3558 return NULL;
3561 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3562 __isl_keep isl_vec *vec)
3564 int i;
3565 unsigned total;
3566 isl_int s;
3568 if (!bmap || !vec)
3569 return isl_bool_error;
3571 total = 1 + isl_basic_map_total_dim(bmap);
3572 if (total != vec->size)
3573 return isl_bool_false;
3575 isl_int_init(s);
3577 for (i = 0; i < bmap->n_eq; ++i) {
3578 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3579 if (!isl_int_is_zero(s)) {
3580 isl_int_clear(s);
3581 return isl_bool_false;
3585 for (i = 0; i < bmap->n_ineq; ++i) {
3586 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3587 if (isl_int_is_neg(s)) {
3588 isl_int_clear(s);
3589 return isl_bool_false;
3593 isl_int_clear(s);
3595 return isl_bool_true;
3598 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3599 __isl_keep isl_vec *vec)
3601 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3604 __isl_give isl_basic_map *isl_basic_map_intersect(
3605 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
3607 struct isl_vec *sample = NULL;
3609 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
3610 goto error;
3611 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3612 isl_space_dim(bmap1->dim, isl_dim_param) &&
3613 isl_space_dim(bmap2->dim, isl_dim_all) !=
3614 isl_space_dim(bmap2->dim, isl_dim_param))
3615 return isl_basic_map_intersect(bmap2, bmap1);
3617 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3618 isl_space_dim(bmap2->dim, isl_dim_param))
3619 isl_assert(bmap1->ctx,
3620 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3622 if (isl_basic_map_plain_is_empty(bmap1)) {
3623 isl_basic_map_free(bmap2);
3624 return bmap1;
3626 if (isl_basic_map_plain_is_empty(bmap2)) {
3627 isl_basic_map_free(bmap1);
3628 return bmap2;
3631 if (bmap1->sample &&
3632 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3633 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3634 sample = isl_vec_copy(bmap1->sample);
3635 else if (bmap2->sample &&
3636 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3637 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3638 sample = isl_vec_copy(bmap2->sample);
3640 bmap1 = isl_basic_map_cow(bmap1);
3641 if (!bmap1)
3642 goto error;
3643 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3644 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3645 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3647 if (!bmap1)
3648 isl_vec_free(sample);
3649 else if (sample) {
3650 isl_vec_free(bmap1->sample);
3651 bmap1->sample = sample;
3654 bmap1 = isl_basic_map_simplify(bmap1);
3655 return isl_basic_map_finalize(bmap1);
3656 error:
3657 if (sample)
3658 isl_vec_free(sample);
3659 isl_basic_map_free(bmap1);
3660 isl_basic_map_free(bmap2);
3661 return NULL;
3664 struct isl_basic_set *isl_basic_set_intersect(
3665 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3667 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3668 bset_to_bmap(bset2)));
3671 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3672 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3674 return isl_basic_set_intersect(bset1, bset2);
3677 /* Special case of isl_map_intersect, where both map1 and map2
3678 * are convex, without any divs and such that either map1 or map2
3679 * contains a single constraint. This constraint is then simply
3680 * added to the other map.
3682 static __isl_give isl_map *map_intersect_add_constraint(
3683 __isl_take isl_map *map1, __isl_take isl_map *map2)
3685 isl_assert(map1->ctx, map1->n == 1, goto error);
3686 isl_assert(map2->ctx, map1->n == 1, goto error);
3687 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3688 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3690 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3691 return isl_map_intersect(map2, map1);
3693 map1 = isl_map_cow(map1);
3694 if (!map1)
3695 goto error;
3696 if (isl_map_plain_is_empty(map1)) {
3697 isl_map_free(map2);
3698 return map1;
3700 if (map2->p[0]->n_eq == 1)
3701 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3702 else
3703 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3704 map2->p[0]->ineq[0]);
3706 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3707 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3708 if (!map1->p[0])
3709 goto error;
3711 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3712 isl_basic_map_free(map1->p[0]);
3713 map1->n = 0;
3716 isl_map_free(map2);
3718 map1 = isl_map_unmark_normalized(map1);
3719 return map1;
3720 error:
3721 isl_map_free(map1);
3722 isl_map_free(map2);
3723 return NULL;
3726 /* map2 may be either a parameter domain or a map living in the same
3727 * space as map1.
3729 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3730 __isl_take isl_map *map2)
3732 unsigned flags = 0;
3733 isl_bool equal;
3734 isl_map *result;
3735 int i, j;
3737 if (!map1 || !map2)
3738 goto error;
3740 if ((isl_map_plain_is_empty(map1) ||
3741 isl_map_plain_is_universe(map2)) &&
3742 isl_space_is_equal(map1->dim, map2->dim)) {
3743 isl_map_free(map2);
3744 return map1;
3746 if ((isl_map_plain_is_empty(map2) ||
3747 isl_map_plain_is_universe(map1)) &&
3748 isl_space_is_equal(map1->dim, map2->dim)) {
3749 isl_map_free(map1);
3750 return map2;
3753 if (map1->n == 1 && map2->n == 1 &&
3754 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3755 isl_space_is_equal(map1->dim, map2->dim) &&
3756 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3757 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3758 return map_intersect_add_constraint(map1, map2);
3760 equal = isl_map_plain_is_equal(map1, map2);
3761 if (equal < 0)
3762 goto error;
3763 if (equal) {
3764 isl_map_free(map2);
3765 return map1;
3768 if (isl_space_dim(map2->dim, isl_dim_all) !=
3769 isl_space_dim(map2->dim, isl_dim_param))
3770 isl_assert(map1->ctx,
3771 isl_space_is_equal(map1->dim, map2->dim), goto error);
3773 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3774 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3775 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3777 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3778 map1->n * map2->n, flags);
3779 if (!result)
3780 goto error;
3781 for (i = 0; i < map1->n; ++i)
3782 for (j = 0; j < map2->n; ++j) {
3783 struct isl_basic_map *part;
3784 part = isl_basic_map_intersect(
3785 isl_basic_map_copy(map1->p[i]),
3786 isl_basic_map_copy(map2->p[j]));
3787 if (isl_basic_map_is_empty(part) < 0)
3788 part = isl_basic_map_free(part);
3789 result = isl_map_add_basic_map(result, part);
3790 if (!result)
3791 goto error;
3793 isl_map_free(map1);
3794 isl_map_free(map2);
3795 return result;
3796 error:
3797 isl_map_free(map1);
3798 isl_map_free(map2);
3799 return NULL;
3802 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3803 __isl_take isl_map *map2)
3805 if (!map1 || !map2)
3806 goto error;
3807 if (!isl_space_is_equal(map1->dim, map2->dim))
3808 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3809 "spaces don't match", goto error);
3810 return map_intersect_internal(map1, map2);
3811 error:
3812 isl_map_free(map1);
3813 isl_map_free(map2);
3814 return NULL;
3817 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3818 __isl_take isl_map *map2)
3820 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3823 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3825 return set_from_map(isl_map_intersect(set_to_map(set1),
3826 set_to_map(set2)));
3829 /* map_intersect_internal accepts intersections
3830 * with parameter domains, so we can just call that function.
3832 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3833 __isl_take isl_set *params)
3835 return map_intersect_internal(map, params);
3838 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3839 __isl_take isl_map *map2)
3841 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3844 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3845 __isl_take isl_set *params)
3847 return isl_map_intersect_params(set, params);
3850 __isl_give isl_basic_map *isl_basic_map_reverse(__isl_take isl_basic_map *bmap)
3852 isl_space *space;
3853 unsigned pos, n1, n2;
3855 if (!bmap)
3856 return NULL;
3857 bmap = isl_basic_map_cow(bmap);
3858 if (!bmap)
3859 return NULL;
3860 space = isl_space_reverse(isl_space_copy(bmap->dim));
3861 pos = isl_basic_map_offset(bmap, isl_dim_in);
3862 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3863 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3864 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3865 return isl_basic_map_reset_space(bmap, space);
3868 static __isl_give isl_basic_map *basic_map_space_reset(
3869 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3871 isl_space *space;
3873 if (!bmap)
3874 return NULL;
3875 if (!isl_space_is_named_or_nested(bmap->dim, type))
3876 return bmap;
3878 space = isl_basic_map_get_space(bmap);
3879 space = isl_space_reset(space, type);
3880 bmap = isl_basic_map_reset_space(bmap, space);
3881 return bmap;
3884 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3885 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3886 unsigned pos, unsigned n)
3888 isl_bool rational;
3889 isl_space *res_space;
3890 struct isl_basic_map *res;
3891 struct isl_dim_map *dim_map;
3892 unsigned total, off;
3893 enum isl_dim_type t;
3895 if (n == 0)
3896 return basic_map_space_reset(bmap, type);
3898 res_space = isl_space_insert_dims(isl_basic_map_get_space(bmap),
3899 type, pos, n);
3900 if (!res_space)
3901 return isl_basic_map_free(bmap);
3903 total = isl_basic_map_total_dim(bmap) + n;
3904 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3905 off = 0;
3906 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3907 if (t != type) {
3908 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3909 } else {
3910 unsigned size = isl_basic_map_dim(bmap, t);
3911 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3912 0, pos, off);
3913 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3914 pos, size - pos, off + pos + n);
3916 off += isl_space_dim(res_space, t);
3918 isl_dim_map_div(dim_map, bmap, off);
3920 res = isl_basic_map_alloc_space(res_space,
3921 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3922 rational = isl_basic_map_is_rational(bmap);
3923 if (rational < 0)
3924 res = isl_basic_map_free(res);
3925 if (rational)
3926 res = isl_basic_map_set_rational(res);
3927 if (isl_basic_map_plain_is_empty(bmap)) {
3928 isl_basic_map_free(bmap);
3929 free(dim_map);
3930 return isl_basic_map_set_to_empty(res);
3932 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3933 return isl_basic_map_finalize(res);
3936 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3937 __isl_take isl_basic_set *bset,
3938 enum isl_dim_type type, unsigned pos, unsigned n)
3940 return isl_basic_map_insert_dims(bset, type, pos, n);
3943 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3944 enum isl_dim_type type, unsigned n)
3946 if (!bmap)
3947 return NULL;
3948 return isl_basic_map_insert_dims(bmap, type,
3949 isl_basic_map_dim(bmap, type), n);
3952 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3953 enum isl_dim_type type, unsigned n)
3955 if (!bset)
3956 return NULL;
3957 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3958 return isl_basic_map_add_dims(bset, type, n);
3959 error:
3960 isl_basic_set_free(bset);
3961 return NULL;
3964 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3965 enum isl_dim_type type)
3967 isl_space *space;
3969 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3970 return map;
3972 space = isl_map_get_space(map);
3973 space = isl_space_reset(space, type);
3974 map = isl_map_reset_space(map, space);
3975 return map;
3978 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3979 enum isl_dim_type type, unsigned pos, unsigned n)
3981 int i;
3983 if (n == 0)
3984 return map_space_reset(map, type);
3986 map = isl_map_cow(map);
3987 if (!map)
3988 return NULL;
3990 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3991 if (!map->dim)
3992 goto error;
3994 for (i = 0; i < map->n; ++i) {
3995 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3996 if (!map->p[i])
3997 goto error;
4000 return map;
4001 error:
4002 isl_map_free(map);
4003 return NULL;
4006 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
4007 enum isl_dim_type type, unsigned pos, unsigned n)
4009 return isl_map_insert_dims(set, type, pos, n);
4012 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
4013 enum isl_dim_type type, unsigned n)
4015 if (!map)
4016 return NULL;
4017 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
4020 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
4021 enum isl_dim_type type, unsigned n)
4023 if (!set)
4024 return NULL;
4025 isl_assert(set->ctx, type != isl_dim_in, goto error);
4026 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
4027 error:
4028 isl_set_free(set);
4029 return NULL;
4032 __isl_give isl_basic_map *isl_basic_map_move_dims(
4033 __isl_take isl_basic_map *bmap,
4034 enum isl_dim_type dst_type, unsigned dst_pos,
4035 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4037 struct isl_dim_map *dim_map;
4038 struct isl_basic_map *res;
4039 enum isl_dim_type t;
4040 unsigned total, off;
4042 if (!bmap)
4043 return NULL;
4044 if (n == 0) {
4045 bmap = isl_basic_map_reset(bmap, src_type);
4046 bmap = isl_basic_map_reset(bmap, dst_type);
4047 return bmap;
4050 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
4051 return isl_basic_map_free(bmap);
4053 if (dst_type == src_type && dst_pos == src_pos)
4054 return bmap;
4056 isl_assert(bmap->ctx, dst_type != src_type, goto error);
4058 if (pos(bmap->dim, dst_type) + dst_pos ==
4059 pos(bmap->dim, src_type) + src_pos +
4060 ((src_type < dst_type) ? n : 0)) {
4061 bmap = isl_basic_map_cow(bmap);
4062 if (!bmap)
4063 return NULL;
4065 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4066 src_type, src_pos, n);
4067 if (!bmap->dim)
4068 goto error;
4070 bmap = isl_basic_map_finalize(bmap);
4072 return bmap;
4075 total = isl_basic_map_total_dim(bmap);
4076 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4078 off = 0;
4079 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4080 unsigned size = isl_space_dim(bmap->dim, t);
4081 if (t == dst_type) {
4082 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4083 0, dst_pos, off);
4084 off += dst_pos;
4085 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
4086 src_pos, n, off);
4087 off += n;
4088 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4089 dst_pos, size - dst_pos, off);
4090 off += size - dst_pos;
4091 } else if (t == src_type) {
4092 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4093 0, src_pos, off);
4094 off += src_pos;
4095 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4096 src_pos + n, size - src_pos - n, off);
4097 off += size - src_pos - n;
4098 } else {
4099 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4100 off += size;
4103 isl_dim_map_div(dim_map, bmap, off);
4105 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4106 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4107 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4108 if (!bmap)
4109 goto error;
4111 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
4112 src_type, src_pos, n);
4113 if (!bmap->dim)
4114 goto error;
4116 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
4117 bmap = isl_basic_map_gauss(bmap, NULL);
4118 bmap = isl_basic_map_finalize(bmap);
4120 return bmap;
4121 error:
4122 isl_basic_map_free(bmap);
4123 return NULL;
4126 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
4127 enum isl_dim_type dst_type, unsigned dst_pos,
4128 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4130 isl_basic_map *bmap = bset_to_bmap(bset);
4131 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
4132 src_type, src_pos, n);
4133 return bset_from_bmap(bmap);
4136 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
4137 enum isl_dim_type dst_type, unsigned dst_pos,
4138 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4140 if (!set)
4141 return NULL;
4142 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
4143 return set_from_map(isl_map_move_dims(set_to_map(set),
4144 dst_type, dst_pos, src_type, src_pos, n));
4145 error:
4146 isl_set_free(set);
4147 return NULL;
4150 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
4151 enum isl_dim_type dst_type, unsigned dst_pos,
4152 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
4154 int i;
4156 if (n == 0) {
4157 map = isl_map_reset(map, src_type);
4158 map = isl_map_reset(map, dst_type);
4159 return map;
4162 if (isl_map_check_range(map, src_type, src_pos, n))
4163 return isl_map_free(map);
4165 if (dst_type == src_type && dst_pos == src_pos)
4166 return map;
4168 isl_assert(map->ctx, dst_type != src_type, goto error);
4170 map = isl_map_cow(map);
4171 if (!map)
4172 return NULL;
4174 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
4175 if (!map->dim)
4176 goto error;
4178 for (i = 0; i < map->n; ++i) {
4179 map->p[i] = isl_basic_map_move_dims(map->p[i],
4180 dst_type, dst_pos,
4181 src_type, src_pos, n);
4182 if (!map->p[i])
4183 goto error;
4186 return map;
4187 error:
4188 isl_map_free(map);
4189 return NULL;
4192 /* Move the specified dimensions to the last columns right before
4193 * the divs. Don't change the dimension specification of bmap.
4194 * That's the responsibility of the caller.
4196 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
4197 enum isl_dim_type type, unsigned first, unsigned n)
4199 struct isl_dim_map *dim_map;
4200 struct isl_basic_map *res;
4201 enum isl_dim_type t;
4202 unsigned total, off;
4204 if (!bmap)
4205 return NULL;
4206 if (pos(bmap->dim, type) + first + n ==
4207 1 + isl_space_dim(bmap->dim, isl_dim_all))
4208 return bmap;
4210 total = isl_basic_map_total_dim(bmap);
4211 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4213 off = 0;
4214 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
4215 unsigned size = isl_space_dim(bmap->dim, t);
4216 if (t == type) {
4217 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4218 0, first, off);
4219 off += first;
4220 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4221 first, n, total - bmap->n_div - n);
4222 isl_dim_map_dim_range(dim_map, bmap->dim, t,
4223 first + n, size - (first + n), off);
4224 off += size - (first + n);
4225 } else {
4226 isl_dim_map_dim(dim_map, bmap->dim, t, off);
4227 off += size;
4230 isl_dim_map_div(dim_map, bmap, off + n);
4232 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
4233 bmap->n_div, bmap->n_eq, bmap->n_ineq);
4234 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
4235 return res;
4238 /* Insert "n" rows in the divs of "bmap".
4240 * The number of columns is not changed, which means that the last
4241 * dimensions of "bmap" are being reintepreted as the new divs.
4242 * The space of "bmap" is not adjusted, however, which means
4243 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
4244 * from the space of "bmap" is the responsibility of the caller.
4246 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
4247 int n)
4249 int i;
4250 size_t row_size;
4251 isl_int **new_div;
4252 isl_int *old;
4254 bmap = isl_basic_map_cow(bmap);
4255 if (!bmap)
4256 return NULL;
4258 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
4259 old = bmap->block2.data;
4260 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
4261 (bmap->extra + n) * (1 + row_size));
4262 if (!bmap->block2.data)
4263 return isl_basic_map_free(bmap);
4264 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
4265 if (!new_div)
4266 return isl_basic_map_free(bmap);
4267 for (i = 0; i < n; ++i) {
4268 new_div[i] = bmap->block2.data +
4269 (bmap->extra + i) * (1 + row_size);
4270 isl_seq_clr(new_div[i], 1 + row_size);
4272 for (i = 0; i < bmap->extra; ++i)
4273 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
4274 free(bmap->div);
4275 bmap->div = new_div;
4276 bmap->n_div += n;
4277 bmap->extra += n;
4279 return bmap;
4282 /* Drop constraints from "bmap" that only involve the variables
4283 * of "type" in the range [first, first + n] that are not related
4284 * to any of the variables outside that interval.
4285 * These constraints cannot influence the values for the variables
4286 * outside the interval, except in case they cause "bmap" to be empty.
4287 * Only drop the constraints if "bmap" is known to be non-empty.
4289 static __isl_give isl_basic_map *drop_irrelevant_constraints(
4290 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
4291 unsigned first, unsigned n)
4293 int i;
4294 int *groups;
4295 unsigned dim, n_div;
4296 isl_bool non_empty;
4298 non_empty = isl_basic_map_plain_is_non_empty(bmap);
4299 if (non_empty < 0)
4300 return isl_basic_map_free(bmap);
4301 if (!non_empty)
4302 return bmap;
4304 dim = isl_basic_map_dim(bmap, isl_dim_all);
4305 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4306 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
4307 if (!groups)
4308 return isl_basic_map_free(bmap);
4309 first += isl_basic_map_offset(bmap, type) - 1;
4310 for (i = 0; i < first; ++i)
4311 groups[i] = -1;
4312 for (i = first + n; i < dim - n_div; ++i)
4313 groups[i] = -1;
4315 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
4317 return bmap;
4320 /* Turn the n dimensions of type type, starting at first
4321 * into existentially quantified variables.
4323 * If a subset of the projected out variables are unrelated
4324 * to any of the variables that remain, then the constraints
4325 * involving this subset are simply dropped first.
4327 __isl_give isl_basic_map *isl_basic_map_project_out(
4328 __isl_take isl_basic_map *bmap,
4329 enum isl_dim_type type, unsigned first, unsigned n)
4331 isl_bool empty;
4333 if (n == 0)
4334 return basic_map_space_reset(bmap, type);
4335 if (type == isl_dim_div)
4336 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4337 "cannot project out existentially quantified variables",
4338 return isl_basic_map_free(bmap));
4340 empty = isl_basic_map_plain_is_empty(bmap);
4341 if (empty < 0)
4342 return isl_basic_map_free(bmap);
4343 if (empty)
4344 bmap = isl_basic_map_set_to_empty(bmap);
4346 bmap = drop_irrelevant_constraints(bmap, type, first, n);
4347 if (!bmap)
4348 return NULL;
4350 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
4351 return isl_basic_map_remove_dims(bmap, type, first, n);
4353 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
4354 return isl_basic_map_free(bmap);
4356 bmap = move_last(bmap, type, first, n);
4357 bmap = isl_basic_map_cow(bmap);
4358 bmap = insert_div_rows(bmap, n);
4359 if (!bmap)
4360 return NULL;
4362 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
4363 if (!bmap->dim)
4364 goto error;
4365 bmap = isl_basic_map_simplify(bmap);
4366 bmap = isl_basic_map_drop_redundant_divs(bmap);
4367 return isl_basic_map_finalize(bmap);
4368 error:
4369 isl_basic_map_free(bmap);
4370 return NULL;
4373 /* Turn the n dimensions of type type, starting at first
4374 * into existentially quantified variables.
4376 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
4377 enum isl_dim_type type, unsigned first, unsigned n)
4379 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
4380 type, first, n));
4383 /* Turn the n dimensions of type type, starting at first
4384 * into existentially quantified variables.
4386 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
4387 enum isl_dim_type type, unsigned first, unsigned n)
4389 int i;
4391 if (n == 0)
4392 return map_space_reset(map, type);
4394 if (isl_map_check_range(map, type, first, n) < 0)
4395 return isl_map_free(map);
4397 map = isl_map_cow(map);
4398 if (!map)
4399 return NULL;
4401 map->dim = isl_space_drop_dims(map->dim, type, first, n);
4402 if (!map->dim)
4403 goto error;
4405 for (i = 0; i < map->n; ++i) {
4406 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
4407 if (!map->p[i])
4408 goto error;
4411 return map;
4412 error:
4413 isl_map_free(map);
4414 return NULL;
4417 /* Turn all the dimensions of type "type", except the "n" starting at "first"
4418 * into existentially quantified variables.
4420 __isl_give isl_map *isl_map_project_onto(__isl_take isl_map *map,
4421 enum isl_dim_type type, unsigned first, unsigned n)
4423 unsigned dim;
4425 if (isl_map_check_range(map, type, first, n) < 0)
4426 return isl_map_free(map);
4427 dim = isl_map_dim(map, type);
4428 map = isl_map_project_out(map, type, first + n, dim - (first + n));
4429 map = isl_map_project_out(map, type, 0, first);
4430 return map;
4433 /* Turn the n dimensions of type type, starting at first
4434 * into existentially quantified variables.
4436 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
4437 enum isl_dim_type type, unsigned first, unsigned n)
4439 return set_from_map(isl_map_project_out(set_to_map(set),
4440 type, first, n));
4443 /* Return a map that projects the elements in "set" onto their
4444 * "n" set dimensions starting at "first".
4445 * "type" should be equal to isl_dim_set.
4447 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
4448 enum isl_dim_type type, unsigned first, unsigned n)
4450 int i;
4451 int dim;
4452 isl_map *map;
4454 if (!set)
4455 return NULL;
4456 if (type != isl_dim_set)
4457 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4458 "only set dimensions can be projected out", goto error);
4459 dim = isl_set_dim(set, isl_dim_set);
4460 if (first + n > dim || first + n < first)
4461 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4462 "index out of bounds", goto error);
4464 map = isl_map_from_domain(set);
4465 map = isl_map_add_dims(map, isl_dim_out, n);
4466 for (i = 0; i < n; ++i)
4467 map = isl_map_equate(map, isl_dim_in, first + i,
4468 isl_dim_out, i);
4469 return map;
4470 error:
4471 isl_set_free(set);
4472 return NULL;
4475 static __isl_give isl_basic_map *add_divs(__isl_take isl_basic_map *bmap,
4476 unsigned n)
4478 int i, j;
4480 for (i = 0; i < n; ++i) {
4481 j = isl_basic_map_alloc_div(bmap);
4482 if (j < 0)
4483 goto error;
4484 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4486 return bmap;
4487 error:
4488 isl_basic_map_free(bmap);
4489 return NULL;
4492 struct isl_basic_map *isl_basic_map_apply_range(
4493 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4495 isl_space *space_result = NULL;
4496 struct isl_basic_map *bmap;
4497 unsigned n_in, n_out, n, nparam, total, pos;
4498 struct isl_dim_map *dim_map1, *dim_map2;
4500 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4501 goto error;
4502 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4503 bmap2->dim, isl_dim_in))
4504 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4505 "spaces don't match", goto error);
4507 space_result = isl_space_join(isl_space_copy(bmap1->dim),
4508 isl_space_copy(bmap2->dim));
4510 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4511 n_out = isl_basic_map_dim(bmap2, isl_dim_out);
4512 n = isl_basic_map_dim(bmap1, isl_dim_out);
4513 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4515 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4516 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4517 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4518 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4519 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4520 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4521 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4522 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4523 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4524 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4525 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4527 bmap = isl_basic_map_alloc_space(space_result,
4528 bmap1->n_div + bmap2->n_div + n,
4529 bmap1->n_eq + bmap2->n_eq,
4530 bmap1->n_ineq + bmap2->n_ineq);
4531 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4532 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4533 bmap = add_divs(bmap, n);
4534 bmap = isl_basic_map_simplify(bmap);
4535 bmap = isl_basic_map_drop_redundant_divs(bmap);
4536 return isl_basic_map_finalize(bmap);
4537 error:
4538 isl_basic_map_free(bmap1);
4539 isl_basic_map_free(bmap2);
4540 return NULL;
4543 struct isl_basic_set *isl_basic_set_apply(
4544 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4546 if (!bset || !bmap)
4547 goto error;
4549 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4550 goto error);
4552 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4553 bmap));
4554 error:
4555 isl_basic_set_free(bset);
4556 isl_basic_map_free(bmap);
4557 return NULL;
4560 struct isl_basic_map *isl_basic_map_apply_domain(
4561 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4563 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
4564 goto error;
4565 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4566 bmap2->dim, isl_dim_in))
4567 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4568 "spaces don't match", goto error);
4570 bmap1 = isl_basic_map_reverse(bmap1);
4571 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4572 return isl_basic_map_reverse(bmap1);
4573 error:
4574 isl_basic_map_free(bmap1);
4575 isl_basic_map_free(bmap2);
4576 return NULL;
4579 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4580 * A \cap B -> f(A) + f(B)
4582 __isl_give isl_basic_map *isl_basic_map_sum(__isl_take isl_basic_map *bmap1,
4583 __isl_take isl_basic_map *bmap2)
4585 unsigned n_in, n_out, nparam, total, pos;
4586 struct isl_basic_map *bmap = NULL;
4587 struct isl_dim_map *dim_map1, *dim_map2;
4588 int i;
4590 if (!bmap1 || !bmap2)
4591 goto error;
4593 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4594 goto error);
4596 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
4597 n_in = isl_basic_map_dim(bmap1, isl_dim_in);
4598 n_out = isl_basic_map_dim(bmap1, isl_dim_out);
4600 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4601 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4602 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4603 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4604 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4605 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4606 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4607 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4608 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4609 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4610 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4612 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4613 bmap1->n_div + bmap2->n_div + 2 * n_out,
4614 bmap1->n_eq + bmap2->n_eq + n_out,
4615 bmap1->n_ineq + bmap2->n_ineq);
4616 for (i = 0; i < n_out; ++i) {
4617 int j = isl_basic_map_alloc_equality(bmap);
4618 if (j < 0)
4619 goto error;
4620 isl_seq_clr(bmap->eq[j], 1+total);
4621 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4622 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4623 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4625 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4626 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4627 bmap = add_divs(bmap, 2 * n_out);
4629 bmap = isl_basic_map_simplify(bmap);
4630 return isl_basic_map_finalize(bmap);
4631 error:
4632 isl_basic_map_free(bmap);
4633 isl_basic_map_free(bmap1);
4634 isl_basic_map_free(bmap2);
4635 return NULL;
4638 /* Given two maps A -> f(A) and B -> g(B), construct a map
4639 * A \cap B -> f(A) + f(B)
4641 __isl_give isl_map *isl_map_sum(__isl_take isl_map *map1,
4642 __isl_take isl_map *map2)
4644 struct isl_map *result;
4645 int i, j;
4647 if (!map1 || !map2)
4648 goto error;
4650 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4652 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4653 map1->n * map2->n, 0);
4654 if (!result)
4655 goto error;
4656 for (i = 0; i < map1->n; ++i)
4657 for (j = 0; j < map2->n; ++j) {
4658 struct isl_basic_map *part;
4659 part = isl_basic_map_sum(
4660 isl_basic_map_copy(map1->p[i]),
4661 isl_basic_map_copy(map2->p[j]));
4662 if (isl_basic_map_is_empty(part))
4663 isl_basic_map_free(part);
4664 else
4665 result = isl_map_add_basic_map(result, part);
4666 if (!result)
4667 goto error;
4669 isl_map_free(map1);
4670 isl_map_free(map2);
4671 return result;
4672 error:
4673 isl_map_free(map1);
4674 isl_map_free(map2);
4675 return NULL;
4678 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4679 __isl_take isl_set *set2)
4681 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4684 /* Given a basic map A -> f(A), construct A -> -f(A).
4686 __isl_give isl_basic_map *isl_basic_map_neg(__isl_take isl_basic_map *bmap)
4688 int i, j;
4689 unsigned off, n;
4691 bmap = isl_basic_map_cow(bmap);
4692 if (!bmap)
4693 return NULL;
4695 n = isl_basic_map_dim(bmap, isl_dim_out);
4696 off = isl_basic_map_offset(bmap, isl_dim_out);
4697 for (i = 0; i < bmap->n_eq; ++i)
4698 for (j = 0; j < n; ++j)
4699 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4700 for (i = 0; i < bmap->n_ineq; ++i)
4701 for (j = 0; j < n; ++j)
4702 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4703 for (i = 0; i < bmap->n_div; ++i)
4704 for (j = 0; j < n; ++j)
4705 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4706 bmap = isl_basic_map_gauss(bmap, NULL);
4707 return isl_basic_map_finalize(bmap);
4710 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4712 return isl_basic_map_neg(bset);
4715 /* Given a map A -> f(A), construct A -> -f(A).
4717 __isl_give isl_map *isl_map_neg(__isl_take isl_map *map)
4719 int i;
4721 map = isl_map_cow(map);
4722 if (!map)
4723 return NULL;
4725 for (i = 0; i < map->n; ++i) {
4726 map->p[i] = isl_basic_map_neg(map->p[i]);
4727 if (!map->p[i])
4728 goto error;
4731 return map;
4732 error:
4733 isl_map_free(map);
4734 return NULL;
4737 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4739 return set_from_map(isl_map_neg(set_to_map(set)));
4742 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4743 * A -> floor(f(A)/d).
4745 __isl_give isl_basic_map *isl_basic_map_floordiv(__isl_take isl_basic_map *bmap,
4746 isl_int d)
4748 unsigned n_in, n_out, nparam, total, pos;
4749 struct isl_basic_map *result = NULL;
4750 struct isl_dim_map *dim_map;
4751 int i;
4753 if (!bmap)
4754 return NULL;
4756 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4757 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4758 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4760 total = nparam + n_in + n_out + bmap->n_div + n_out;
4761 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4762 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4763 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4764 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4765 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4767 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4768 bmap->n_div + n_out,
4769 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4770 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4771 result = add_divs(result, n_out);
4772 for (i = 0; i < n_out; ++i) {
4773 int j;
4774 j = isl_basic_map_alloc_inequality(result);
4775 if (j < 0)
4776 goto error;
4777 isl_seq_clr(result->ineq[j], 1+total);
4778 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4779 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4780 j = isl_basic_map_alloc_inequality(result);
4781 if (j < 0)
4782 goto error;
4783 isl_seq_clr(result->ineq[j], 1+total);
4784 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4785 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4786 isl_int_sub_ui(result->ineq[j][0], d, 1);
4789 result = isl_basic_map_simplify(result);
4790 return isl_basic_map_finalize(result);
4791 error:
4792 isl_basic_map_free(result);
4793 return NULL;
4796 /* Given a map A -> f(A) and an integer d, construct a map
4797 * A -> floor(f(A)/d).
4799 __isl_give isl_map *isl_map_floordiv(__isl_take isl_map *map, isl_int d)
4801 int i;
4803 map = isl_map_cow(map);
4804 if (!map)
4805 return NULL;
4807 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4808 for (i = 0; i < map->n; ++i) {
4809 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4810 if (!map->p[i])
4811 goto error;
4813 map = isl_map_unmark_normalized(map);
4815 return map;
4816 error:
4817 isl_map_free(map);
4818 return NULL;
4821 /* Given a map A -> f(A) and an integer d, construct a map
4822 * A -> floor(f(A)/d).
4824 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4825 __isl_take isl_val *d)
4827 if (!map || !d)
4828 goto error;
4829 if (!isl_val_is_int(d))
4830 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4831 "expecting integer denominator", goto error);
4832 map = isl_map_floordiv(map, d->n);
4833 isl_val_free(d);
4834 return map;
4835 error:
4836 isl_map_free(map);
4837 isl_val_free(d);
4838 return NULL;
4841 static __isl_give isl_basic_map *var_equal(__isl_take isl_basic_map *bmap,
4842 unsigned pos)
4844 int i;
4845 unsigned nparam;
4846 unsigned n_in;
4848 i = isl_basic_map_alloc_equality(bmap);
4849 if (i < 0)
4850 goto error;
4851 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4852 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4853 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4854 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4855 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4856 return isl_basic_map_finalize(bmap);
4857 error:
4858 isl_basic_map_free(bmap);
4859 return NULL;
4862 /* Add a constraint to "bmap" expressing i_pos < o_pos
4864 static __isl_give isl_basic_map *var_less(__isl_take isl_basic_map *bmap,
4865 unsigned pos)
4867 int i;
4868 unsigned nparam;
4869 unsigned n_in;
4871 i = isl_basic_map_alloc_inequality(bmap);
4872 if (i < 0)
4873 goto error;
4874 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4875 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4876 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4877 isl_int_set_si(bmap->ineq[i][0], -1);
4878 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4879 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4880 return isl_basic_map_finalize(bmap);
4881 error:
4882 isl_basic_map_free(bmap);
4883 return NULL;
4886 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4888 static __isl_give isl_basic_map *var_less_or_equal(
4889 __isl_take isl_basic_map *bmap, unsigned pos)
4891 int i;
4892 unsigned nparam;
4893 unsigned n_in;
4895 i = isl_basic_map_alloc_inequality(bmap);
4896 if (i < 0)
4897 goto error;
4898 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4899 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4900 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4901 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4902 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4903 return isl_basic_map_finalize(bmap);
4904 error:
4905 isl_basic_map_free(bmap);
4906 return NULL;
4909 /* Add a constraint to "bmap" expressing i_pos > o_pos
4911 static __isl_give isl_basic_map *var_more(__isl_take isl_basic_map *bmap,
4912 unsigned pos)
4914 int i;
4915 unsigned nparam;
4916 unsigned n_in;
4918 i = isl_basic_map_alloc_inequality(bmap);
4919 if (i < 0)
4920 goto error;
4921 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4922 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4923 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4924 isl_int_set_si(bmap->ineq[i][0], -1);
4925 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4926 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4927 return isl_basic_map_finalize(bmap);
4928 error:
4929 isl_basic_map_free(bmap);
4930 return NULL;
4933 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4935 static __isl_give isl_basic_map *var_more_or_equal(
4936 __isl_take isl_basic_map *bmap, unsigned pos)
4938 int i;
4939 unsigned nparam;
4940 unsigned n_in;
4942 i = isl_basic_map_alloc_inequality(bmap);
4943 if (i < 0)
4944 goto error;
4945 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4946 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4947 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4948 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4949 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4950 return isl_basic_map_finalize(bmap);
4951 error:
4952 isl_basic_map_free(bmap);
4953 return NULL;
4956 __isl_give isl_basic_map *isl_basic_map_equal(
4957 __isl_take isl_space *space, unsigned n_equal)
4959 int i;
4960 struct isl_basic_map *bmap;
4961 bmap = isl_basic_map_alloc_space(space, 0, n_equal, 0);
4962 if (!bmap)
4963 return NULL;
4964 for (i = 0; i < n_equal && bmap; ++i)
4965 bmap = var_equal(bmap, i);
4966 return isl_basic_map_finalize(bmap);
4969 /* Return a relation on of dimension "space" expressing i_[0..pos] << o_[0..pos]
4971 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *space,
4972 unsigned pos)
4974 int i;
4975 struct isl_basic_map *bmap;
4976 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4977 if (!bmap)
4978 return NULL;
4979 for (i = 0; i < pos && bmap; ++i)
4980 bmap = var_equal(bmap, i);
4981 if (bmap)
4982 bmap = var_less(bmap, pos);
4983 return isl_basic_map_finalize(bmap);
4986 /* Return a relation on "space" expressing i_[0..pos] <<= o_[0..pos]
4988 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4989 __isl_take isl_space *space, unsigned pos)
4991 int i;
4992 isl_basic_map *bmap;
4994 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
4995 for (i = 0; i < pos; ++i)
4996 bmap = var_equal(bmap, i);
4997 bmap = var_less_or_equal(bmap, pos);
4998 return isl_basic_map_finalize(bmap);
5001 /* Return a relation on "space" expressing i_pos > o_pos
5003 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *space,
5004 unsigned pos)
5006 int i;
5007 struct isl_basic_map *bmap;
5008 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5009 if (!bmap)
5010 return NULL;
5011 for (i = 0; i < pos && bmap; ++i)
5012 bmap = var_equal(bmap, i);
5013 if (bmap)
5014 bmap = var_more(bmap, pos);
5015 return isl_basic_map_finalize(bmap);
5018 /* Return a relation on "space" expressing i_[0..pos] >>= o_[0..pos]
5020 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
5021 __isl_take isl_space *space, unsigned pos)
5023 int i;
5024 isl_basic_map *bmap;
5026 bmap = isl_basic_map_alloc_space(space, 0, pos, 1);
5027 for (i = 0; i < pos; ++i)
5028 bmap = var_equal(bmap, i);
5029 bmap = var_more_or_equal(bmap, pos);
5030 return isl_basic_map_finalize(bmap);
5033 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *space,
5034 unsigned n, int equal)
5036 struct isl_map *map;
5037 int i;
5039 if (n == 0 && equal)
5040 return isl_map_universe(space);
5042 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5044 for (i = 0; i + 1 < n; ++i)
5045 map = isl_map_add_basic_map(map,
5046 isl_basic_map_less_at(isl_space_copy(space), i));
5047 if (n > 0) {
5048 if (equal)
5049 map = isl_map_add_basic_map(map,
5050 isl_basic_map_less_or_equal_at(space, n - 1));
5051 else
5052 map = isl_map_add_basic_map(map,
5053 isl_basic_map_less_at(space, n - 1));
5054 } else
5055 isl_space_free(space);
5057 return map;
5060 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *space, int equal)
5062 if (!space)
5063 return NULL;
5064 return map_lex_lte_first(space, space->n_out, equal);
5067 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
5069 return map_lex_lte_first(dim, n, 0);
5072 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
5074 return map_lex_lte_first(dim, n, 1);
5077 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
5079 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
5082 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
5084 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
5087 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *space,
5088 unsigned n, int equal)
5090 struct isl_map *map;
5091 int i;
5093 if (n == 0 && equal)
5094 return isl_map_universe(space);
5096 map = isl_map_alloc_space(isl_space_copy(space), n, ISL_MAP_DISJOINT);
5098 for (i = 0; i + 1 < n; ++i)
5099 map = isl_map_add_basic_map(map,
5100 isl_basic_map_more_at(isl_space_copy(space), i));
5101 if (n > 0) {
5102 if (equal)
5103 map = isl_map_add_basic_map(map,
5104 isl_basic_map_more_or_equal_at(space, n - 1));
5105 else
5106 map = isl_map_add_basic_map(map,
5107 isl_basic_map_more_at(space, n - 1));
5108 } else
5109 isl_space_free(space);
5111 return map;
5114 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *space, int equal)
5116 if (!space)
5117 return NULL;
5118 return map_lex_gte_first(space, space->n_out, equal);
5121 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
5123 return map_lex_gte_first(dim, n, 0);
5126 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
5128 return map_lex_gte_first(dim, n, 1);
5131 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
5133 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
5136 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
5138 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
5141 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
5142 __isl_take isl_set *set2)
5144 isl_map *map;
5145 map = isl_map_lex_le(isl_set_get_space(set1));
5146 map = isl_map_intersect_domain(map, set1);
5147 map = isl_map_intersect_range(map, set2);
5148 return map;
5151 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
5152 __isl_take isl_set *set2)
5154 isl_map *map;
5155 map = isl_map_lex_lt(isl_set_get_space(set1));
5156 map = isl_map_intersect_domain(map, set1);
5157 map = isl_map_intersect_range(map, set2);
5158 return map;
5161 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
5162 __isl_take isl_set *set2)
5164 isl_map *map;
5165 map = isl_map_lex_ge(isl_set_get_space(set1));
5166 map = isl_map_intersect_domain(map, set1);
5167 map = isl_map_intersect_range(map, set2);
5168 return map;
5171 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
5172 __isl_take isl_set *set2)
5174 isl_map *map;
5175 map = isl_map_lex_gt(isl_set_get_space(set1));
5176 map = isl_map_intersect_domain(map, set1);
5177 map = isl_map_intersect_range(map, set2);
5178 return map;
5181 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
5182 __isl_take isl_map *map2)
5184 isl_map *map;
5185 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
5186 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5187 map = isl_map_apply_range(map, isl_map_reverse(map2));
5188 return map;
5191 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
5192 __isl_take isl_map *map2)
5194 isl_map *map;
5195 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
5196 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5197 map = isl_map_apply_range(map, isl_map_reverse(map2));
5198 return map;
5201 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
5202 __isl_take isl_map *map2)
5204 isl_map *map;
5205 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
5206 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5207 map = isl_map_apply_range(map, isl_map_reverse(map2));
5208 return map;
5211 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
5212 __isl_take isl_map *map2)
5214 isl_map *map;
5215 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
5216 map = isl_map_apply_domain(map, isl_map_reverse(map1));
5217 map = isl_map_apply_range(map, isl_map_reverse(map2));
5218 return map;
5221 /* For a div d = floor(f/m), add the constraint
5223 * f - m d >= 0
5225 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
5226 unsigned pos, isl_int *div)
5228 int i;
5229 unsigned total = isl_basic_map_total_dim(bmap);
5231 i = isl_basic_map_alloc_inequality(bmap);
5232 if (i < 0)
5233 return isl_stat_error;
5234 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
5235 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
5237 return isl_stat_ok;
5240 /* For a div d = floor(f/m), add the constraint
5242 * -(f-(m-1)) + m d >= 0
5244 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
5245 unsigned pos, isl_int *div)
5247 int i;
5248 unsigned total = isl_basic_map_total_dim(bmap);
5250 i = isl_basic_map_alloc_inequality(bmap);
5251 if (i < 0)
5252 return isl_stat_error;
5253 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
5254 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
5255 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
5256 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
5258 return isl_stat_ok;
5261 /* For a div d = floor(f/m), add the constraints
5263 * f - m d >= 0
5264 * -(f-(m-1)) + m d >= 0
5266 * Note that the second constraint is the negation of
5268 * f - m d >= m
5270 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
5271 unsigned pos, isl_int *div)
5273 if (add_upper_div_constraint(bmap, pos, div) < 0)
5274 return -1;
5275 if (add_lower_div_constraint(bmap, pos, div) < 0)
5276 return -1;
5277 return 0;
5280 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
5281 unsigned pos, isl_int *div)
5283 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
5284 pos, div);
5287 int isl_basic_map_add_div_constraints(__isl_keep isl_basic_map *bmap,
5288 unsigned div)
5290 unsigned total = isl_basic_map_total_dim(bmap);
5291 unsigned div_pos = total - bmap->n_div + div;
5293 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
5294 bmap->div[div]);
5297 /* For each known div d = floor(f/m), add the constraints
5299 * f - m d >= 0
5300 * -(f-(m-1)) + m d >= 0
5302 * Remove duplicate constraints in case of some these div constraints
5303 * already appear in "bmap".
5305 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
5306 __isl_take isl_basic_map *bmap)
5308 unsigned n_div;
5310 if (!bmap)
5311 return NULL;
5312 n_div = isl_basic_map_dim(bmap, isl_dim_div);
5313 if (n_div == 0)
5314 return bmap;
5316 bmap = add_known_div_constraints(bmap);
5317 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
5318 bmap = isl_basic_map_finalize(bmap);
5319 return bmap;
5322 /* Add the div constraint of sign "sign" for div "div" of "bmap".
5324 * In particular, if this div is of the form d = floor(f/m),
5325 * then add the constraint
5327 * f - m d >= 0
5329 * if sign < 0 or the constraint
5331 * -(f-(m-1)) + m d >= 0
5333 * if sign > 0.
5335 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
5336 unsigned div, int sign)
5338 unsigned total;
5339 unsigned div_pos;
5341 if (!bmap)
5342 return -1;
5344 total = isl_basic_map_total_dim(bmap);
5345 div_pos = total - bmap->n_div + div;
5347 if (sign < 0)
5348 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
5349 else
5350 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
5353 __isl_give isl_basic_set *isl_basic_map_underlying_set(
5354 __isl_take isl_basic_map *bmap)
5356 if (!bmap)
5357 goto error;
5358 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
5359 bmap->n_div == 0 &&
5360 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
5361 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
5362 return bset_from_bmap(bmap);
5363 bmap = isl_basic_map_cow(bmap);
5364 if (!bmap)
5365 goto error;
5366 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
5367 if (!bmap->dim)
5368 goto error;
5369 bmap->extra -= bmap->n_div;
5370 bmap->n_div = 0;
5371 bmap = isl_basic_map_finalize(bmap);
5372 return bset_from_bmap(bmap);
5373 error:
5374 isl_basic_map_free(bmap);
5375 return NULL;
5378 __isl_give isl_basic_set *isl_basic_set_underlying_set(
5379 __isl_take isl_basic_set *bset)
5381 return isl_basic_map_underlying_set(bset_to_bmap(bset));
5384 /* Replace each element in "list" by the result of applying
5385 * isl_basic_map_underlying_set to the element.
5387 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
5388 __isl_take isl_basic_map_list *list)
5390 int i, n;
5392 if (!list)
5393 return NULL;
5395 n = isl_basic_map_list_n_basic_map(list);
5396 for (i = 0; i < n; ++i) {
5397 isl_basic_map *bmap;
5398 isl_basic_set *bset;
5400 bmap = isl_basic_map_list_get_basic_map(list, i);
5401 bset = isl_basic_set_underlying_set(bmap);
5402 list = isl_basic_set_list_set_basic_set(list, i, bset);
5405 return list;
5408 __isl_give isl_basic_map *isl_basic_map_overlying_set(
5409 __isl_take isl_basic_set *bset, __isl_take isl_basic_map *like)
5411 struct isl_basic_map *bmap;
5412 struct isl_ctx *ctx;
5413 unsigned total;
5414 int i;
5416 if (!bset || !like)
5417 goto error;
5418 ctx = bset->ctx;
5419 isl_assert(ctx, bset->n_div == 0, goto error);
5420 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
5421 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
5422 goto error);
5423 if (like->n_div == 0) {
5424 isl_space *space = isl_basic_map_get_space(like);
5425 isl_basic_map_free(like);
5426 return isl_basic_map_reset_space(bset, space);
5428 bset = isl_basic_set_cow(bset);
5429 if (!bset)
5430 goto error;
5431 total = bset->dim->n_out + bset->extra;
5432 bmap = bset_to_bmap(bset);
5433 isl_space_free(bmap->dim);
5434 bmap->dim = isl_space_copy(like->dim);
5435 if (!bmap->dim)
5436 goto error;
5437 bmap->n_div = like->n_div;
5438 bmap->extra += like->n_div;
5439 if (bmap->extra) {
5440 unsigned ltotal;
5441 isl_int **div;
5442 ltotal = total - bmap->extra + like->extra;
5443 if (ltotal > total)
5444 ltotal = total;
5445 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
5446 bmap->extra * (1 + 1 + total));
5447 if (isl_blk_is_error(bmap->block2))
5448 goto error;
5449 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5450 if (!div)
5451 goto error;
5452 bmap->div = div;
5453 for (i = 0; i < bmap->extra; ++i)
5454 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5455 for (i = 0; i < like->n_div; ++i) {
5456 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5457 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5459 bmap = isl_basic_map_add_known_div_constraints(bmap);
5461 isl_basic_map_free(like);
5462 bmap = isl_basic_map_simplify(bmap);
5463 bmap = isl_basic_map_finalize(bmap);
5464 return bmap;
5465 error:
5466 isl_basic_map_free(like);
5467 isl_basic_set_free(bset);
5468 return NULL;
5471 struct isl_basic_set *isl_basic_set_from_underlying_set(
5472 struct isl_basic_set *bset, struct isl_basic_set *like)
5474 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5475 bset_to_bmap(like)));
5478 __isl_give isl_set *isl_map_underlying_set(__isl_take isl_map *map)
5480 int i;
5482 map = isl_map_cow(map);
5483 if (!map)
5484 return NULL;
5485 map->dim = isl_space_cow(map->dim);
5486 if (!map->dim)
5487 goto error;
5489 for (i = 1; i < map->n; ++i)
5490 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5491 goto error);
5492 for (i = 0; i < map->n; ++i) {
5493 map->p[i] = bset_to_bmap(
5494 isl_basic_map_underlying_set(map->p[i]));
5495 if (!map->p[i])
5496 goto error;
5498 if (map->n == 0)
5499 map->dim = isl_space_underlying(map->dim, 0);
5500 else {
5501 isl_space_free(map->dim);
5502 map->dim = isl_space_copy(map->p[0]->dim);
5504 if (!map->dim)
5505 goto error;
5506 return set_from_map(map);
5507 error:
5508 isl_map_free(map);
5509 return NULL;
5512 /* Replace the space of "bmap" by "space".
5514 * If the space of "bmap" is identical to "space" (including the identifiers
5515 * of the input and output dimensions), then simply return the original input.
5517 __isl_give isl_basic_map *isl_basic_map_reset_space(
5518 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5520 isl_bool equal;
5521 isl_space *bmap_space;
5523 bmap_space = isl_basic_map_peek_space(bmap);
5524 equal = isl_space_is_equal(bmap_space, space);
5525 if (equal >= 0 && equal)
5526 equal = isl_space_has_equal_ids(bmap_space, space);
5527 if (equal < 0)
5528 goto error;
5529 if (equal) {
5530 isl_space_free(space);
5531 return bmap;
5533 bmap = isl_basic_map_cow(bmap);
5534 if (!bmap || !space)
5535 goto error;
5537 isl_space_free(bmap->dim);
5538 bmap->dim = space;
5540 bmap = isl_basic_map_finalize(bmap);
5542 return bmap;
5543 error:
5544 isl_basic_map_free(bmap);
5545 isl_space_free(space);
5546 return NULL;
5549 __isl_give isl_basic_set *isl_basic_set_reset_space(
5550 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5552 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5553 dim));
5556 /* Check that the total dimensions of "map" and "space" are the same.
5558 static isl_stat check_map_space_equal_total_dim(__isl_keep isl_map *map,
5559 __isl_keep isl_space *space)
5561 unsigned dim1, dim2;
5563 if (!map || !space)
5564 return isl_stat_error;
5565 dim1 = isl_map_dim(map, isl_dim_all);
5566 dim2 = isl_space_dim(space, isl_dim_all);
5567 if (dim1 == dim2)
5568 return isl_stat_ok;
5569 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5570 "total dimensions do not match", return isl_stat_error);
5573 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5574 __isl_take isl_space *space)
5576 int i;
5578 map = isl_map_cow(map);
5579 if (!map || !space)
5580 goto error;
5582 for (i = 0; i < map->n; ++i) {
5583 map->p[i] = isl_basic_map_reset_space(map->p[i],
5584 isl_space_copy(space));
5585 if (!map->p[i])
5586 goto error;
5588 isl_space_free(map->dim);
5589 map->dim = space;
5591 return map;
5592 error:
5593 isl_map_free(map);
5594 isl_space_free(space);
5595 return NULL;
5598 /* Replace the space of "map" by "space", without modifying
5599 * the dimension of "map".
5601 * If the space of "map" is identical to "space" (including the identifiers
5602 * of the input and output dimensions), then simply return the original input.
5604 __isl_give isl_map *isl_map_reset_equal_dim_space(__isl_take isl_map *map,
5605 __isl_take isl_space *space)
5607 isl_bool equal;
5608 isl_space *map_space;
5610 map_space = isl_map_peek_space(map);
5611 equal = isl_space_is_equal(map_space, space);
5612 if (equal >= 0 && equal)
5613 equal = isl_space_has_equal_ids(map_space, space);
5614 if (equal < 0)
5615 goto error;
5616 if (equal) {
5617 isl_space_free(space);
5618 return map;
5620 if (check_map_space_equal_total_dim(map, space) < 0)
5621 goto error;
5622 return isl_map_reset_space(map, space);
5623 error:
5624 isl_map_free(map);
5625 isl_space_free(space);
5626 return NULL;
5629 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5630 __isl_take isl_space *dim)
5632 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5635 /* Compute the parameter domain of the given basic set.
5637 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5639 isl_bool is_params;
5640 isl_space *space;
5641 unsigned n;
5643 is_params = isl_basic_set_is_params(bset);
5644 if (is_params < 0)
5645 return isl_basic_set_free(bset);
5646 if (is_params)
5647 return bset;
5649 n = isl_basic_set_dim(bset, isl_dim_set);
5650 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5651 space = isl_basic_set_get_space(bset);
5652 space = isl_space_params(space);
5653 bset = isl_basic_set_reset_space(bset, space);
5654 return bset;
5657 /* Construct a zero-dimensional basic set with the given parameter domain.
5659 __isl_give isl_basic_set *isl_basic_set_from_params(
5660 __isl_take isl_basic_set *bset)
5662 isl_space *space;
5663 space = isl_basic_set_get_space(bset);
5664 space = isl_space_set_from_params(space);
5665 bset = isl_basic_set_reset_space(bset, space);
5666 return bset;
5669 /* Compute the parameter domain of the given set.
5671 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5673 isl_space *space;
5674 unsigned n;
5676 if (isl_set_is_params(set))
5677 return set;
5679 n = isl_set_dim(set, isl_dim_set);
5680 set = isl_set_project_out(set, isl_dim_set, 0, n);
5681 space = isl_set_get_space(set);
5682 space = isl_space_params(space);
5683 set = isl_set_reset_space(set, space);
5684 return set;
5687 /* Construct a zero-dimensional set with the given parameter domain.
5689 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5691 isl_space *space;
5692 space = isl_set_get_space(set);
5693 space = isl_space_set_from_params(space);
5694 set = isl_set_reset_space(set, space);
5695 return set;
5698 /* Compute the parameter domain of the given map.
5700 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5702 isl_space *space;
5703 unsigned n;
5705 n = isl_map_dim(map, isl_dim_in);
5706 map = isl_map_project_out(map, isl_dim_in, 0, n);
5707 n = isl_map_dim(map, isl_dim_out);
5708 map = isl_map_project_out(map, isl_dim_out, 0, n);
5709 space = isl_map_get_space(map);
5710 space = isl_space_params(space);
5711 map = isl_map_reset_space(map, space);
5712 return map;
5715 __isl_give isl_basic_set *isl_basic_map_domain(__isl_take isl_basic_map *bmap)
5717 isl_space *space;
5718 unsigned n_out;
5720 if (!bmap)
5721 return NULL;
5722 space = isl_space_domain(isl_basic_map_get_space(bmap));
5724 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5725 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5727 return isl_basic_map_reset_space(bmap, space);
5730 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5732 if (!bmap)
5733 return isl_bool_error;
5734 return isl_space_may_be_set(bmap->dim);
5737 /* Is this basic map actually a set?
5738 * Users should never call this function. Outside of isl,
5739 * the type should indicate whether something is a set or a map.
5741 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5743 if (!bmap)
5744 return isl_bool_error;
5745 return isl_space_is_set(bmap->dim);
5748 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5750 isl_bool is_set;
5752 is_set = isl_basic_map_is_set(bmap);
5753 if (is_set < 0)
5754 goto error;
5755 if (is_set)
5756 return bmap;
5757 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5758 error:
5759 isl_basic_map_free(bmap);
5760 return NULL;
5763 __isl_give isl_basic_map *isl_basic_map_domain_map(
5764 __isl_take isl_basic_map *bmap)
5766 int i;
5767 isl_space *space;
5768 isl_basic_map *domain;
5769 int nparam, n_in, n_out;
5771 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5772 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5773 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5775 space = isl_basic_map_get_space(bmap);
5776 space = isl_space_from_range(isl_space_domain(space));
5777 domain = isl_basic_map_universe(space);
5779 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5780 bmap = isl_basic_map_apply_range(bmap, domain);
5781 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5783 for (i = 0; i < n_in; ++i)
5784 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5785 isl_dim_out, i);
5787 bmap = isl_basic_map_gauss(bmap, NULL);
5788 return isl_basic_map_finalize(bmap);
5791 __isl_give isl_basic_map *isl_basic_map_range_map(
5792 __isl_take isl_basic_map *bmap)
5794 int i;
5795 isl_space *space;
5796 isl_basic_map *range;
5797 int nparam, n_in, n_out;
5799 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5800 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5801 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5803 space = isl_basic_map_get_space(bmap);
5804 space = isl_space_from_range(isl_space_range(space));
5805 range = isl_basic_map_universe(space);
5807 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5808 bmap = isl_basic_map_apply_range(bmap, range);
5809 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5811 for (i = 0; i < n_out; ++i)
5812 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5813 isl_dim_out, i);
5815 bmap = isl_basic_map_gauss(bmap, NULL);
5816 return isl_basic_map_finalize(bmap);
5819 int isl_map_may_be_set(__isl_keep isl_map *map)
5821 if (!map)
5822 return -1;
5823 return isl_space_may_be_set(map->dim);
5826 /* Is this map actually a set?
5827 * Users should never call this function. Outside of isl,
5828 * the type should indicate whether something is a set or a map.
5830 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5832 if (!map)
5833 return isl_bool_error;
5834 return isl_space_is_set(map->dim);
5837 __isl_give isl_set *isl_map_range(__isl_take isl_map *map)
5839 int i;
5840 isl_bool is_set;
5841 struct isl_set *set;
5843 is_set = isl_map_is_set(map);
5844 if (is_set < 0)
5845 goto error;
5846 if (is_set)
5847 return set_from_map(map);
5849 map = isl_map_cow(map);
5850 if (!map)
5851 goto error;
5853 set = set_from_map(map);
5854 set->dim = isl_space_range(set->dim);
5855 if (!set->dim)
5856 goto error;
5857 for (i = 0; i < map->n; ++i) {
5858 set->p[i] = isl_basic_map_range(map->p[i]);
5859 if (!set->p[i])
5860 goto error;
5862 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5863 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5864 return set;
5865 error:
5866 isl_map_free(map);
5867 return NULL;
5870 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5872 int i;
5874 map = isl_map_cow(map);
5875 if (!map)
5876 return NULL;
5878 map->dim = isl_space_domain_map(map->dim);
5879 if (!map->dim)
5880 goto error;
5881 for (i = 0; i < map->n; ++i) {
5882 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5883 if (!map->p[i])
5884 goto error;
5886 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5887 map = isl_map_unmark_normalized(map);
5888 return map;
5889 error:
5890 isl_map_free(map);
5891 return NULL;
5894 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5896 int i;
5897 isl_space *range_dim;
5899 map = isl_map_cow(map);
5900 if (!map)
5901 return NULL;
5903 range_dim = isl_space_range(isl_map_get_space(map));
5904 range_dim = isl_space_from_range(range_dim);
5905 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5906 map->dim = isl_space_join(map->dim, range_dim);
5907 if (!map->dim)
5908 goto error;
5909 for (i = 0; i < map->n; ++i) {
5910 map->p[i] = isl_basic_map_range_map(map->p[i]);
5911 if (!map->p[i])
5912 goto error;
5914 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5915 map = isl_map_unmark_normalized(map);
5916 return map;
5917 error:
5918 isl_map_free(map);
5919 return NULL;
5922 /* Given a wrapped map of the form A[B -> C],
5923 * return the map A[B -> C] -> B.
5925 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5927 isl_id *id;
5928 isl_map *map;
5930 if (!set)
5931 return NULL;
5932 if (!isl_set_has_tuple_id(set))
5933 return isl_map_domain_map(isl_set_unwrap(set));
5935 id = isl_set_get_tuple_id(set);
5936 map = isl_map_domain_map(isl_set_unwrap(set));
5937 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5939 return map;
5942 __isl_give isl_basic_map *isl_basic_map_from_domain(
5943 __isl_take isl_basic_set *bset)
5945 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5948 __isl_give isl_basic_map *isl_basic_map_from_range(
5949 __isl_take isl_basic_set *bset)
5951 isl_space *space;
5952 space = isl_basic_set_get_space(bset);
5953 space = isl_space_from_range(space);
5954 bset = isl_basic_set_reset_space(bset, space);
5955 return bset_to_bmap(bset);
5958 /* Create a relation with the given set as range.
5959 * The domain of the created relation is a zero-dimensional
5960 * flat anonymous space.
5962 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5964 isl_space *space;
5965 space = isl_set_get_space(set);
5966 space = isl_space_from_range(space);
5967 set = isl_set_reset_space(set, space);
5968 return set_to_map(set);
5971 /* Create a relation with the given set as domain.
5972 * The range of the created relation is a zero-dimensional
5973 * flat anonymous space.
5975 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5977 return isl_map_reverse(isl_map_from_range(set));
5980 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5981 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5983 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5986 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5987 __isl_take isl_set *range)
5989 return isl_map_apply_range(isl_map_reverse(domain), range);
5992 /* Return a newly allocated isl_map with given space and flags and
5993 * room for "n" basic maps.
5994 * Make sure that all cached information is cleared.
5996 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5997 unsigned flags)
5999 struct isl_map *map;
6001 if (!space)
6002 return NULL;
6003 if (n < 0)
6004 isl_die(space->ctx, isl_error_internal,
6005 "negative number of basic maps", goto error);
6006 map = isl_calloc(space->ctx, struct isl_map,
6007 sizeof(struct isl_map) +
6008 (n - 1) * sizeof(struct isl_basic_map *));
6009 if (!map)
6010 goto error;
6012 map->ctx = space->ctx;
6013 isl_ctx_ref(map->ctx);
6014 map->ref = 1;
6015 map->size = n;
6016 map->n = 0;
6017 map->dim = space;
6018 map->flags = flags;
6019 return map;
6020 error:
6021 isl_space_free(space);
6022 return NULL;
6025 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *space)
6027 struct isl_basic_map *bmap;
6028 bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
6029 bmap = isl_basic_map_set_to_empty(bmap);
6030 return bmap;
6033 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *space)
6035 struct isl_basic_set *bset;
6036 bset = isl_basic_set_alloc_space(space, 0, 1, 0);
6037 bset = isl_basic_set_set_to_empty(bset);
6038 return bset;
6041 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *space)
6043 struct isl_basic_map *bmap;
6044 bmap = isl_basic_map_alloc_space(space, 0, 0, 0);
6045 bmap = isl_basic_map_finalize(bmap);
6046 return bmap;
6049 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *space)
6051 struct isl_basic_set *bset;
6052 bset = isl_basic_set_alloc_space(space, 0, 0, 0);
6053 bset = isl_basic_set_finalize(bset);
6054 return bset;
6057 __isl_give isl_basic_map *isl_basic_map_nat_universe(
6058 __isl_take isl_space *space)
6060 int i;
6061 unsigned total = isl_space_dim(space, isl_dim_all);
6062 isl_basic_map *bmap;
6064 bmap = isl_basic_map_alloc_space(space, 0, 0, total);
6065 for (i = 0; i < total; ++i) {
6066 int k = isl_basic_map_alloc_inequality(bmap);
6067 if (k < 0)
6068 goto error;
6069 isl_seq_clr(bmap->ineq[k], 1 + total);
6070 isl_int_set_si(bmap->ineq[k][1 + i], 1);
6072 return bmap;
6073 error:
6074 isl_basic_map_free(bmap);
6075 return NULL;
6078 __isl_give isl_basic_set *isl_basic_set_nat_universe(
6079 __isl_take isl_space *space)
6081 return isl_basic_map_nat_universe(space);
6084 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
6086 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
6089 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
6091 return isl_map_nat_universe(dim);
6094 __isl_give isl_map *isl_map_empty(__isl_take isl_space *space)
6096 return isl_map_alloc_space(space, 0, ISL_MAP_DISJOINT);
6099 __isl_give isl_set *isl_set_empty(__isl_take isl_space *space)
6101 return isl_set_alloc_space(space, 0, ISL_MAP_DISJOINT);
6104 __isl_give isl_map *isl_map_universe(__isl_take isl_space *space)
6106 struct isl_map *map;
6107 if (!space)
6108 return NULL;
6109 map = isl_map_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6110 map = isl_map_add_basic_map(map, isl_basic_map_universe(space));
6111 return map;
6114 __isl_give isl_set *isl_set_universe(__isl_take isl_space *space)
6116 struct isl_set *set;
6117 if (!space)
6118 return NULL;
6119 set = isl_set_alloc_space(isl_space_copy(space), 1, ISL_MAP_DISJOINT);
6120 set = isl_set_add_basic_set(set, isl_basic_set_universe(space));
6121 return set;
6124 struct isl_map *isl_map_dup(struct isl_map *map)
6126 int i;
6127 struct isl_map *dup;
6129 if (!map)
6130 return NULL;
6131 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
6132 for (i = 0; i < map->n; ++i)
6133 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
6134 return dup;
6137 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
6138 __isl_take isl_basic_map *bmap)
6140 if (!bmap || !map)
6141 goto error;
6142 if (isl_basic_map_plain_is_empty(bmap)) {
6143 isl_basic_map_free(bmap);
6144 return map;
6146 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
6147 isl_assert(map->ctx, map->n < map->size, goto error);
6148 map->p[map->n] = bmap;
6149 map->n++;
6150 map = isl_map_unmark_normalized(map);
6151 return map;
6152 error:
6153 if (map)
6154 isl_map_free(map);
6155 if (bmap)
6156 isl_basic_map_free(bmap);
6157 return NULL;
6160 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
6162 int i;
6164 if (!map)
6165 return NULL;
6167 if (--map->ref > 0)
6168 return NULL;
6170 clear_caches(map);
6171 isl_ctx_deref(map->ctx);
6172 for (i = 0; i < map->n; ++i)
6173 isl_basic_map_free(map->p[i]);
6174 isl_space_free(map->dim);
6175 free(map);
6177 return NULL;
6180 static __isl_give isl_basic_map *isl_basic_map_fix_pos_si(
6181 __isl_take isl_basic_map *bmap, unsigned pos, int value)
6183 int j;
6185 bmap = isl_basic_map_cow(bmap);
6186 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6187 j = isl_basic_map_alloc_equality(bmap);
6188 if (j < 0)
6189 goto error;
6190 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6191 isl_int_set_si(bmap->eq[j][pos], -1);
6192 isl_int_set_si(bmap->eq[j][0], value);
6193 bmap = isl_basic_map_simplify(bmap);
6194 return isl_basic_map_finalize(bmap);
6195 error:
6196 isl_basic_map_free(bmap);
6197 return NULL;
6200 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
6201 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
6203 int j;
6205 bmap = isl_basic_map_cow(bmap);
6206 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
6207 j = isl_basic_map_alloc_equality(bmap);
6208 if (j < 0)
6209 goto error;
6210 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
6211 isl_int_set_si(bmap->eq[j][pos], -1);
6212 isl_int_set(bmap->eq[j][0], value);
6213 bmap = isl_basic_map_simplify(bmap);
6214 return isl_basic_map_finalize(bmap);
6215 error:
6216 isl_basic_map_free(bmap);
6217 return NULL;
6220 __isl_give isl_basic_map *isl_basic_map_fix_si(__isl_take isl_basic_map *bmap,
6221 enum isl_dim_type type, unsigned pos, int value)
6223 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6224 return isl_basic_map_free(bmap);
6225 return isl_basic_map_fix_pos_si(bmap,
6226 isl_basic_map_offset(bmap, type) + pos, value);
6229 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
6230 enum isl_dim_type type, unsigned pos, isl_int value)
6232 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6233 return isl_basic_map_free(bmap);
6234 return isl_basic_map_fix_pos(bmap,
6235 isl_basic_map_offset(bmap, type) + pos, value);
6238 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
6239 * to be equal to "v".
6241 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
6242 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6244 if (!bmap || !v)
6245 goto error;
6246 if (!isl_val_is_int(v))
6247 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6248 "expecting integer value", goto error);
6249 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6250 goto error;
6251 pos += isl_basic_map_offset(bmap, type);
6252 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
6253 isl_val_free(v);
6254 return bmap;
6255 error:
6256 isl_basic_map_free(bmap);
6257 isl_val_free(v);
6258 return NULL;
6261 /* Fix the value of the variable at position "pos" of type "type" of "bset"
6262 * to be equal to "v".
6264 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
6265 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6267 return isl_basic_map_fix_val(bset, type, pos, v);
6270 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
6271 enum isl_dim_type type, unsigned pos, int value)
6273 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6274 type, pos, value));
6277 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
6278 enum isl_dim_type type, unsigned pos, isl_int value)
6280 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
6281 type, pos, value));
6284 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
6285 unsigned input, int value)
6287 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
6290 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
6291 unsigned dim, int value)
6293 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
6294 isl_dim_set, dim, value));
6297 /* Remove the basic map at position "i" from "map" if this basic map
6298 * is (obviously) empty.
6300 static __isl_give isl_map *remove_if_empty(__isl_take isl_map *map, int i)
6302 isl_bool empty;
6304 if (!map)
6305 return NULL;
6307 empty = isl_basic_map_plain_is_empty(map->p[i]);
6308 if (empty < 0)
6309 return isl_map_free(map);
6310 if (!empty)
6311 return map;
6313 isl_basic_map_free(map->p[i]);
6314 map->n--;
6315 if (i != map->n) {
6316 map->p[i] = map->p[map->n];
6317 map = isl_map_unmark_normalized(map);
6321 return map;
6324 /* Perform "fn" on each basic map of "map", where we may not be holding
6325 * the only reference to "map".
6326 * In particular, "fn" should be a semantics preserving operation
6327 * that we want to apply to all copies of "map". We therefore need
6328 * to be careful not to modify "map" in a way that breaks "map"
6329 * in case anything goes wrong.
6331 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
6332 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
6334 struct isl_basic_map *bmap;
6335 int i;
6337 if (!map)
6338 return NULL;
6340 for (i = map->n - 1; i >= 0; --i) {
6341 bmap = isl_basic_map_copy(map->p[i]);
6342 bmap = fn(bmap);
6343 if (!bmap)
6344 goto error;
6345 isl_basic_map_free(map->p[i]);
6346 map->p[i] = bmap;
6347 map = remove_if_empty(map, i);
6348 if (!map)
6349 return NULL;
6352 return map;
6353 error:
6354 isl_map_free(map);
6355 return NULL;
6358 __isl_give isl_map *isl_map_fix_si(__isl_take isl_map *map,
6359 enum isl_dim_type type, unsigned pos, int value)
6361 int i;
6363 map = isl_map_cow(map);
6364 if (!map)
6365 return NULL;
6367 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6368 for (i = map->n - 1; i >= 0; --i) {
6369 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
6370 map = remove_if_empty(map, i);
6371 if (!map)
6372 return NULL;
6374 map = isl_map_unmark_normalized(map);
6375 return map;
6376 error:
6377 isl_map_free(map);
6378 return NULL;
6381 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
6382 enum isl_dim_type type, unsigned pos, int value)
6384 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
6387 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
6388 enum isl_dim_type type, unsigned pos, isl_int value)
6390 int i;
6392 map = isl_map_cow(map);
6393 if (!map)
6394 return NULL;
6396 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6397 for (i = 0; i < map->n; ++i) {
6398 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
6399 if (!map->p[i])
6400 goto error;
6402 map = isl_map_unmark_normalized(map);
6403 return map;
6404 error:
6405 isl_map_free(map);
6406 return NULL;
6409 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
6410 enum isl_dim_type type, unsigned pos, isl_int value)
6412 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
6415 /* Fix the value of the variable at position "pos" of type "type" of "map"
6416 * to be equal to "v".
6418 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
6419 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6421 int i;
6423 map = isl_map_cow(map);
6424 if (!map || !v)
6425 goto error;
6427 if (!isl_val_is_int(v))
6428 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6429 "expecting integer value", goto error);
6430 if (pos >= isl_map_dim(map, type))
6431 isl_die(isl_map_get_ctx(map), isl_error_invalid,
6432 "index out of bounds", goto error);
6433 for (i = map->n - 1; i >= 0; --i) {
6434 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
6435 isl_val_copy(v));
6436 map = remove_if_empty(map, i);
6437 if (!map)
6438 goto error;
6440 map = isl_map_unmark_normalized(map);
6441 isl_val_free(v);
6442 return map;
6443 error:
6444 isl_map_free(map);
6445 isl_val_free(v);
6446 return NULL;
6449 /* Fix the value of the variable at position "pos" of type "type" of "set"
6450 * to be equal to "v".
6452 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
6453 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
6455 return isl_map_fix_val(set, type, pos, v);
6458 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
6459 unsigned input, int value)
6461 return isl_map_fix_si(map, isl_dim_in, input, value);
6464 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
6466 return set_from_map(isl_map_fix_si(set_to_map(set),
6467 isl_dim_set, dim, value));
6470 static __isl_give isl_basic_map *basic_map_bound_si(
6471 __isl_take isl_basic_map *bmap,
6472 enum isl_dim_type type, unsigned pos, int value, int upper)
6474 int j;
6476 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6477 return isl_basic_map_free(bmap);
6478 pos += isl_basic_map_offset(bmap, type);
6479 bmap = isl_basic_map_cow(bmap);
6480 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6481 j = isl_basic_map_alloc_inequality(bmap);
6482 if (j < 0)
6483 goto error;
6484 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6485 if (upper) {
6486 isl_int_set_si(bmap->ineq[j][pos], -1);
6487 isl_int_set_si(bmap->ineq[j][0], value);
6488 } else {
6489 isl_int_set_si(bmap->ineq[j][pos], 1);
6490 isl_int_set_si(bmap->ineq[j][0], -value);
6492 bmap = isl_basic_map_simplify(bmap);
6493 return isl_basic_map_finalize(bmap);
6494 error:
6495 isl_basic_map_free(bmap);
6496 return NULL;
6499 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
6500 __isl_take isl_basic_map *bmap,
6501 enum isl_dim_type type, unsigned pos, int value)
6503 return basic_map_bound_si(bmap, type, pos, value, 0);
6506 /* Constrain the values of the given dimension to be no greater than "value".
6508 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6509 __isl_take isl_basic_map *bmap,
6510 enum isl_dim_type type, unsigned pos, int value)
6512 return basic_map_bound_si(bmap, type, pos, value, 1);
6515 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6516 enum isl_dim_type type, unsigned pos, int value, int upper)
6518 int i;
6520 map = isl_map_cow(map);
6521 if (!map)
6522 return NULL;
6524 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6525 for (i = 0; i < map->n; ++i) {
6526 map->p[i] = basic_map_bound_si(map->p[i],
6527 type, pos, value, upper);
6528 if (!map->p[i])
6529 goto error;
6531 map = isl_map_unmark_normalized(map);
6532 return map;
6533 error:
6534 isl_map_free(map);
6535 return NULL;
6538 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6539 enum isl_dim_type type, unsigned pos, int value)
6541 return map_bound_si(map, type, pos, value, 0);
6544 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6545 enum isl_dim_type type, unsigned pos, int value)
6547 return map_bound_si(map, type, pos, value, 1);
6550 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6551 enum isl_dim_type type, unsigned pos, int value)
6553 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6554 type, pos, value));
6557 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6558 enum isl_dim_type type, unsigned pos, int value)
6560 return isl_map_upper_bound_si(set, type, pos, value);
6563 /* Bound the given variable of "bmap" from below (or above is "upper"
6564 * is set) to "value".
6566 static __isl_give isl_basic_map *basic_map_bound(
6567 __isl_take isl_basic_map *bmap,
6568 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6570 int j;
6572 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6573 return isl_basic_map_free(bmap);
6574 pos += isl_basic_map_offset(bmap, type);
6575 bmap = isl_basic_map_cow(bmap);
6576 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6577 j = isl_basic_map_alloc_inequality(bmap);
6578 if (j < 0)
6579 goto error;
6580 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6581 if (upper) {
6582 isl_int_set_si(bmap->ineq[j][pos], -1);
6583 isl_int_set(bmap->ineq[j][0], value);
6584 } else {
6585 isl_int_set_si(bmap->ineq[j][pos], 1);
6586 isl_int_neg(bmap->ineq[j][0], value);
6588 bmap = isl_basic_map_simplify(bmap);
6589 return isl_basic_map_finalize(bmap);
6590 error:
6591 isl_basic_map_free(bmap);
6592 return NULL;
6595 /* Bound the given variable of "map" from below (or above is "upper"
6596 * is set) to "value".
6598 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6599 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6601 int i;
6603 map = isl_map_cow(map);
6604 if (!map)
6605 return NULL;
6607 if (pos >= isl_map_dim(map, type))
6608 isl_die(map->ctx, isl_error_invalid,
6609 "index out of bounds", goto error);
6610 for (i = map->n - 1; i >= 0; --i) {
6611 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6612 map = remove_if_empty(map, i);
6613 if (!map)
6614 return NULL;
6616 map = isl_map_unmark_normalized(map);
6617 return map;
6618 error:
6619 isl_map_free(map);
6620 return NULL;
6623 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6624 enum isl_dim_type type, unsigned pos, isl_int value)
6626 return map_bound(map, type, pos, value, 0);
6629 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6630 enum isl_dim_type type, unsigned pos, isl_int value)
6632 return map_bound(map, type, pos, value, 1);
6635 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6636 enum isl_dim_type type, unsigned pos, isl_int value)
6638 return isl_map_lower_bound(set, type, pos, value);
6641 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6642 enum isl_dim_type type, unsigned pos, isl_int value)
6644 return isl_map_upper_bound(set, type, pos, value);
6647 /* Force the values of the variable at position "pos" of type "type" of "set"
6648 * to be no smaller than "value".
6650 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6651 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6653 if (!value)
6654 goto error;
6655 if (!isl_val_is_int(value))
6656 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6657 "expecting integer value", goto error);
6658 set = isl_set_lower_bound(set, type, pos, value->n);
6659 isl_val_free(value);
6660 return set;
6661 error:
6662 isl_val_free(value);
6663 isl_set_free(set);
6664 return NULL;
6667 /* Force the values of the variable at position "pos" of type "type" of "set"
6668 * to be no greater than "value".
6670 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6671 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6673 if (!value)
6674 goto error;
6675 if (!isl_val_is_int(value))
6676 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6677 "expecting integer value", goto error);
6678 set = isl_set_upper_bound(set, type, pos, value->n);
6679 isl_val_free(value);
6680 return set;
6681 error:
6682 isl_val_free(value);
6683 isl_set_free(set);
6684 return NULL;
6687 /* Bound the given variable of "bset" from below (or above is "upper"
6688 * is set) to "value".
6690 static __isl_give isl_basic_set *isl_basic_set_bound(
6691 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6692 isl_int value, int upper)
6694 return bset_from_bmap(basic_map_bound(bset_to_bmap(bset),
6695 type, pos, value, upper));
6698 /* Bound the given variable of "bset" from below (or above is "upper"
6699 * is set) to "value".
6701 static __isl_give isl_basic_set *isl_basic_set_bound_val(
6702 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6703 __isl_take isl_val *value, int upper)
6705 if (!value)
6706 goto error;
6707 if (!isl_val_is_int(value))
6708 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
6709 "expecting integer value", goto error);
6710 bset = isl_basic_set_bound(bset, type, pos, value->n, upper);
6711 isl_val_free(value);
6712 return bset;
6713 error:
6714 isl_val_free(value);
6715 isl_basic_set_free(bset);
6716 return NULL;
6719 /* Bound the given variable of "bset" from below to "value".
6721 __isl_give isl_basic_set *isl_basic_set_lower_bound_val(
6722 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6723 __isl_take isl_val *value)
6725 return isl_basic_set_bound_val(bset, type, pos, value, 0);
6728 /* Bound the given variable of "bset" from above to "value".
6730 __isl_give isl_basic_set *isl_basic_set_upper_bound_val(
6731 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned pos,
6732 __isl_take isl_val *value)
6734 return isl_basic_set_bound_val(bset, type, pos, value, 1);
6737 __isl_give isl_map *isl_map_reverse(__isl_take isl_map *map)
6739 int i;
6741 map = isl_map_cow(map);
6742 if (!map)
6743 return NULL;
6745 map->dim = isl_space_reverse(map->dim);
6746 if (!map->dim)
6747 goto error;
6748 for (i = 0; i < map->n; ++i) {
6749 map->p[i] = isl_basic_map_reverse(map->p[i]);
6750 if (!map->p[i])
6751 goto error;
6753 map = isl_map_unmark_normalized(map);
6754 return map;
6755 error:
6756 isl_map_free(map);
6757 return NULL;
6760 #undef TYPE
6761 #define TYPE isl_pw_multi_aff
6762 #undef SUFFIX
6763 #define SUFFIX _pw_multi_aff
6764 #undef EMPTY
6765 #define EMPTY isl_pw_multi_aff_empty
6766 #undef ADD
6767 #define ADD isl_pw_multi_aff_union_add
6768 #include "isl_map_lexopt_templ.c"
6770 /* Given a map "map", compute the lexicographically minimal
6771 * (or maximal) image element for each domain element in dom,
6772 * in the form of an isl_pw_multi_aff.
6773 * If "empty" is not NULL, then set *empty to those elements in dom that
6774 * do not have an image element.
6775 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6776 * should be computed over the domain of "map". "empty" is also NULL
6777 * in this case.
6779 * We first compute the lexicographically minimal or maximal element
6780 * in the first basic map. This results in a partial solution "res"
6781 * and a subset "todo" of dom that still need to be handled.
6782 * We then consider each of the remaining maps in "map" and successively
6783 * update both "res" and "todo".
6784 * If "empty" is NULL, then the todo sets are not needed and therefore
6785 * also not computed.
6787 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6788 __isl_take isl_map *map, __isl_take isl_set *dom,
6789 __isl_give isl_set **empty, unsigned flags)
6791 int i;
6792 int full;
6793 isl_pw_multi_aff *res;
6794 isl_set *todo;
6796 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6797 if (!map || (!full && !dom))
6798 goto error;
6800 if (isl_map_plain_is_empty(map)) {
6801 if (empty)
6802 *empty = dom;
6803 else
6804 isl_set_free(dom);
6805 return isl_pw_multi_aff_from_map(map);
6808 res = basic_map_partial_lexopt_pw_multi_aff(
6809 isl_basic_map_copy(map->p[0]),
6810 isl_set_copy(dom), empty, flags);
6812 if (empty)
6813 todo = *empty;
6814 for (i = 1; i < map->n; ++i) {
6815 isl_pw_multi_aff *res_i;
6817 res_i = basic_map_partial_lexopt_pw_multi_aff(
6818 isl_basic_map_copy(map->p[i]),
6819 isl_set_copy(dom), empty, flags);
6821 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6822 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6823 else
6824 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6826 if (empty)
6827 todo = isl_set_intersect(todo, *empty);
6830 isl_set_free(dom);
6831 isl_map_free(map);
6833 if (empty)
6834 *empty = todo;
6836 return res;
6837 error:
6838 if (empty)
6839 *empty = NULL;
6840 isl_set_free(dom);
6841 isl_map_free(map);
6842 return NULL;
6845 #undef TYPE
6846 #define TYPE isl_map
6847 #undef SUFFIX
6848 #define SUFFIX
6849 #undef EMPTY
6850 #define EMPTY isl_map_empty
6851 #undef ADD
6852 #define ADD isl_map_union_disjoint
6853 #include "isl_map_lexopt_templ.c"
6855 /* Given a map "map", compute the lexicographically minimal
6856 * (or maximal) image element for each domain element in "dom",
6857 * in the form of an isl_map.
6858 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6859 * do not have an image element.
6860 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6861 * should be computed over the domain of "map". "empty" is also NULL
6862 * in this case.
6864 * If the input consists of more than one disjunct, then first
6865 * compute the desired result in the form of an isl_pw_multi_aff and
6866 * then convert that into an isl_map.
6868 * This function used to have an explicit implementation in terms
6869 * of isl_maps, but it would continually intersect the domains of
6870 * partial results with the complement of the domain of the next
6871 * partial solution, potentially leading to an explosion in the number
6872 * of disjuncts if there are several disjuncts in the input.
6873 * An even earlier implementation of this function would look for
6874 * better results in the domain of the partial result and for extra
6875 * results in the complement of this domain, which would lead to
6876 * even more splintering.
6878 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6879 __isl_take isl_map *map, __isl_take isl_set *dom,
6880 __isl_give isl_set **empty, unsigned flags)
6882 int full;
6883 struct isl_map *res;
6884 isl_pw_multi_aff *pma;
6886 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6887 if (!map || (!full && !dom))
6888 goto error;
6890 if (isl_map_plain_is_empty(map)) {
6891 if (empty)
6892 *empty = dom;
6893 else
6894 isl_set_free(dom);
6895 return map;
6898 if (map->n == 1) {
6899 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6900 dom, empty, flags);
6901 isl_map_free(map);
6902 return res;
6905 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6906 flags);
6907 return isl_map_from_pw_multi_aff(pma);
6908 error:
6909 if (empty)
6910 *empty = NULL;
6911 isl_set_free(dom);
6912 isl_map_free(map);
6913 return NULL;
6916 __isl_give isl_map *isl_map_partial_lexmax(
6917 __isl_take isl_map *map, __isl_take isl_set *dom,
6918 __isl_give isl_set **empty)
6920 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6923 __isl_give isl_map *isl_map_partial_lexmin(
6924 __isl_take isl_map *map, __isl_take isl_set *dom,
6925 __isl_give isl_set **empty)
6927 return isl_map_partial_lexopt(map, dom, empty, 0);
6930 __isl_give isl_set *isl_set_partial_lexmin(
6931 __isl_take isl_set *set, __isl_take isl_set *dom,
6932 __isl_give isl_set **empty)
6934 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6935 dom, empty));
6938 __isl_give isl_set *isl_set_partial_lexmax(
6939 __isl_take isl_set *set, __isl_take isl_set *dom,
6940 __isl_give isl_set **empty)
6942 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6943 dom, empty));
6946 /* Compute the lexicographic minimum (or maximum if "flags" includes
6947 * ISL_OPT_MAX) of "bset" over its parametric domain.
6949 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6950 unsigned flags)
6952 return isl_basic_map_lexopt(bset, flags);
6955 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6957 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6960 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6962 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6965 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6967 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6970 /* Compute the lexicographic minimum of "bset" over its parametric domain
6971 * for the purpose of quantifier elimination.
6972 * That is, find an explicit representation for all the existentially
6973 * quantified variables in "bset" by computing their lexicographic
6974 * minimum.
6976 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6977 __isl_take isl_basic_set *bset)
6979 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6982 /* Given a basic map with one output dimension, compute the minimum or
6983 * maximum of that dimension as an isl_pw_aff.
6985 * Compute the optimum as a lexicographic optimum over the single
6986 * output dimension and extract the single isl_pw_aff from the result.
6988 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6989 int max)
6991 isl_pw_multi_aff *pma;
6992 isl_pw_aff *pwaff;
6994 bmap = isl_basic_map_copy(bmap);
6995 pma = isl_basic_map_lexopt_pw_multi_aff(bmap, max ? ISL_OPT_MAX : 0);
6996 pwaff = isl_pw_multi_aff_get_pw_aff(pma, 0);
6997 isl_pw_multi_aff_free(pma);
6999 return pwaff;
7002 /* Compute the minimum or maximum of the given output dimension
7003 * as a function of the parameters and the input dimensions,
7004 * but independently of the other output dimensions.
7006 * We first project out the other output dimension and then compute
7007 * the "lexicographic" maximum in each basic map, combining the results
7008 * using isl_pw_aff_union_max.
7010 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
7011 int max)
7013 int i;
7014 isl_pw_aff *pwaff;
7015 unsigned n_out;
7017 n_out = isl_map_dim(map, isl_dim_out);
7018 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
7019 map = isl_map_project_out(map, isl_dim_out, 0, pos);
7020 if (!map)
7021 return NULL;
7023 if (map->n == 0) {
7024 isl_space *space = isl_map_get_space(map);
7025 isl_map_free(map);
7026 return isl_pw_aff_empty(space);
7029 pwaff = basic_map_dim_opt(map->p[0], max);
7030 for (i = 1; i < map->n; ++i) {
7031 isl_pw_aff *pwaff_i;
7033 pwaff_i = basic_map_dim_opt(map->p[i], max);
7034 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
7037 isl_map_free(map);
7039 return pwaff;
7042 /* Compute the minimum of the given output dimension as a function of the
7043 * parameters and input dimensions, but independently of
7044 * the other output dimensions.
7046 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
7048 return map_dim_opt(map, pos, 0);
7051 /* Compute the maximum of the given output dimension as a function of the
7052 * parameters and input dimensions, but independently of
7053 * the other output dimensions.
7055 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
7057 return map_dim_opt(map, pos, 1);
7060 /* Compute the minimum or maximum of the given set dimension
7061 * as a function of the parameters,
7062 * but independently of the other set dimensions.
7064 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
7065 int max)
7067 return map_dim_opt(set, pos, max);
7070 /* Compute the maximum of the given set dimension as a function of the
7071 * parameters, but independently of the other set dimensions.
7073 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
7075 return set_dim_opt(set, pos, 1);
7078 /* Compute the minimum of the given set dimension as a function of the
7079 * parameters, but independently of the other set dimensions.
7081 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
7083 return set_dim_opt(set, pos, 0);
7086 /* Apply a preimage specified by "mat" on the parameters of "bset".
7087 * bset is assumed to have only parameters and divs.
7089 static __isl_give isl_basic_set *basic_set_parameter_preimage(
7090 __isl_take isl_basic_set *bset, __isl_take isl_mat *mat)
7092 unsigned nparam;
7094 if (!bset || !mat)
7095 goto error;
7097 bset->dim = isl_space_cow(bset->dim);
7098 if (!bset->dim)
7099 goto error;
7101 nparam = isl_basic_set_dim(bset, isl_dim_param);
7103 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
7105 bset->dim->nparam = 0;
7106 bset->dim->n_out = nparam;
7107 bset = isl_basic_set_preimage(bset, mat);
7108 if (bset) {
7109 bset->dim->nparam = bset->dim->n_out;
7110 bset->dim->n_out = 0;
7112 return bset;
7113 error:
7114 isl_mat_free(mat);
7115 isl_basic_set_free(bset);
7116 return NULL;
7119 /* Apply a preimage specified by "mat" on the parameters of "set".
7120 * set is assumed to have only parameters and divs.
7122 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
7123 __isl_take isl_mat *mat)
7125 isl_space *space;
7126 unsigned nparam;
7128 if (!set || !mat)
7129 goto error;
7131 nparam = isl_set_dim(set, isl_dim_param);
7133 if (mat->n_row != 1 + nparam)
7134 isl_die(isl_set_get_ctx(set), isl_error_internal,
7135 "unexpected number of rows", goto error);
7137 space = isl_set_get_space(set);
7138 space = isl_space_move_dims(space, isl_dim_set, 0,
7139 isl_dim_param, 0, nparam);
7140 set = isl_set_reset_space(set, space);
7141 set = isl_set_preimage(set, mat);
7142 nparam = isl_set_dim(set, isl_dim_out);
7143 space = isl_set_get_space(set);
7144 space = isl_space_move_dims(space, isl_dim_param, 0,
7145 isl_dim_out, 0, nparam);
7146 set = isl_set_reset_space(set, space);
7147 return set;
7148 error:
7149 isl_mat_free(mat);
7150 isl_set_free(set);
7151 return NULL;
7154 /* Intersect the basic set "bset" with the affine space specified by the
7155 * equalities in "eq".
7157 static __isl_give isl_basic_set *basic_set_append_equalities(
7158 __isl_take isl_basic_set *bset, __isl_take isl_mat *eq)
7160 int i, k;
7161 unsigned len;
7163 if (!bset || !eq)
7164 goto error;
7166 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
7167 eq->n_row, 0);
7168 if (!bset)
7169 goto error;
7171 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
7172 for (i = 0; i < eq->n_row; ++i) {
7173 k = isl_basic_set_alloc_equality(bset);
7174 if (k < 0)
7175 goto error;
7176 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
7177 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
7179 isl_mat_free(eq);
7181 bset = isl_basic_set_gauss(bset, NULL);
7182 bset = isl_basic_set_finalize(bset);
7184 return bset;
7185 error:
7186 isl_mat_free(eq);
7187 isl_basic_set_free(bset);
7188 return NULL;
7191 /* Intersect the set "set" with the affine space specified by the
7192 * equalities in "eq".
7194 static struct isl_set *set_append_equalities(struct isl_set *set,
7195 struct isl_mat *eq)
7197 int i;
7199 if (!set || !eq)
7200 goto error;
7202 for (i = 0; i < set->n; ++i) {
7203 set->p[i] = basic_set_append_equalities(set->p[i],
7204 isl_mat_copy(eq));
7205 if (!set->p[i])
7206 goto error;
7208 isl_mat_free(eq);
7209 return set;
7210 error:
7211 isl_mat_free(eq);
7212 isl_set_free(set);
7213 return NULL;
7216 /* Given a basic set "bset" that only involves parameters and existentially
7217 * quantified variables, return the index of the first equality
7218 * that only involves parameters. If there is no such equality then
7219 * return bset->n_eq.
7221 * This function assumes that isl_basic_set_gauss has been called on "bset".
7223 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
7225 int i, j;
7226 unsigned nparam, n_div;
7228 if (!bset)
7229 return -1;
7231 nparam = isl_basic_set_dim(bset, isl_dim_param);
7232 n_div = isl_basic_set_dim(bset, isl_dim_div);
7234 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
7235 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
7236 ++i;
7239 return i;
7242 /* Compute an explicit representation for the existentially quantified
7243 * variables in "bset" by computing the "minimal value" of the set
7244 * variables. Since there are no set variables, the computation of
7245 * the minimal value essentially computes an explicit representation
7246 * of the non-empty part(s) of "bset".
7248 * The input only involves parameters and existentially quantified variables.
7249 * All equalities among parameters have been removed.
7251 * Since the existentially quantified variables in the result are in general
7252 * going to be different from those in the input, we first replace
7253 * them by the minimal number of variables based on their equalities.
7254 * This should simplify the parametric integer programming.
7256 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
7258 isl_morph *morph1, *morph2;
7259 isl_set *set;
7260 unsigned n;
7262 if (!bset)
7263 return NULL;
7264 if (bset->n_eq == 0)
7265 return isl_basic_set_lexmin_compute_divs(bset);
7267 morph1 = isl_basic_set_parameter_compression(bset);
7268 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
7269 bset = isl_basic_set_lift(bset);
7270 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
7271 bset = isl_morph_basic_set(morph2, bset);
7272 n = isl_basic_set_dim(bset, isl_dim_set);
7273 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
7275 set = isl_basic_set_lexmin_compute_divs(bset);
7277 set = isl_morph_set(isl_morph_inverse(morph1), set);
7279 return set;
7282 /* Project the given basic set onto its parameter domain, possibly introducing
7283 * new, explicit, existential variables in the constraints.
7284 * The input has parameters and (possibly implicit) existential variables.
7285 * The output has the same parameters, but only
7286 * explicit existentially quantified variables.
7288 * The actual projection is performed by pip, but pip doesn't seem
7289 * to like equalities very much, so we first remove the equalities
7290 * among the parameters by performing a variable compression on
7291 * the parameters. Afterward, an inverse transformation is performed
7292 * and the equalities among the parameters are inserted back in.
7294 * The variable compression on the parameters may uncover additional
7295 * equalities that were only implicit before. We therefore check
7296 * if there are any new parameter equalities in the result and
7297 * if so recurse. The removal of parameter equalities is required
7298 * for the parameter compression performed by base_compute_divs.
7300 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
7302 int i;
7303 struct isl_mat *eq;
7304 struct isl_mat *T, *T2;
7305 struct isl_set *set;
7306 unsigned nparam;
7308 bset = isl_basic_set_cow(bset);
7309 if (!bset)
7310 return NULL;
7312 if (bset->n_eq == 0)
7313 return base_compute_divs(bset);
7315 bset = isl_basic_set_gauss(bset, NULL);
7316 if (!bset)
7317 return NULL;
7318 if (isl_basic_set_plain_is_empty(bset))
7319 return isl_set_from_basic_set(bset);
7321 i = first_parameter_equality(bset);
7322 if (i == bset->n_eq)
7323 return base_compute_divs(bset);
7325 nparam = isl_basic_set_dim(bset, isl_dim_param);
7326 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
7327 0, 1 + nparam);
7328 eq = isl_mat_cow(eq);
7329 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
7330 if (T && T->n_col == 0) {
7331 isl_mat_free(T);
7332 isl_mat_free(T2);
7333 isl_mat_free(eq);
7334 bset = isl_basic_set_set_to_empty(bset);
7335 return isl_set_from_basic_set(bset);
7337 bset = basic_set_parameter_preimage(bset, T);
7339 i = first_parameter_equality(bset);
7340 if (!bset)
7341 set = NULL;
7342 else if (i == bset->n_eq)
7343 set = base_compute_divs(bset);
7344 else
7345 set = parameter_compute_divs(bset);
7346 set = set_parameter_preimage(set, T2);
7347 set = set_append_equalities(set, eq);
7348 return set;
7351 /* Insert the divs from "ls" before those of "bmap".
7353 * The number of columns is not changed, which means that the last
7354 * dimensions of "bmap" are being reintepreted as the divs from "ls".
7355 * The caller is responsible for removing the same number of dimensions
7356 * from the space of "bmap".
7358 static __isl_give isl_basic_map *insert_divs_from_local_space(
7359 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
7361 int i;
7362 int n_div;
7363 int old_n_div;
7365 n_div = isl_local_space_dim(ls, isl_dim_div);
7366 if (n_div == 0)
7367 return bmap;
7369 old_n_div = bmap->n_div;
7370 bmap = insert_div_rows(bmap, n_div);
7371 if (!bmap)
7372 return NULL;
7374 for (i = 0; i < n_div; ++i) {
7375 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
7376 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
7379 return bmap;
7382 /* Replace the space of "bmap" by the space and divs of "ls".
7384 * If "ls" has any divs, then we simplify the result since we may
7385 * have discovered some additional equalities that could simplify
7386 * the div expressions.
7388 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
7389 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
7391 int n_div;
7393 bmap = isl_basic_map_cow(bmap);
7394 if (!bmap || !ls)
7395 goto error;
7397 n_div = isl_local_space_dim(ls, isl_dim_div);
7398 bmap = insert_divs_from_local_space(bmap, ls);
7399 if (!bmap)
7400 goto error;
7402 isl_space_free(bmap->dim);
7403 bmap->dim = isl_local_space_get_space(ls);
7404 if (!bmap->dim)
7405 goto error;
7407 isl_local_space_free(ls);
7408 if (n_div > 0)
7409 bmap = isl_basic_map_simplify(bmap);
7410 bmap = isl_basic_map_finalize(bmap);
7411 return bmap;
7412 error:
7413 isl_basic_map_free(bmap);
7414 isl_local_space_free(ls);
7415 return NULL;
7418 /* Replace the space of "map" by the space and divs of "ls".
7420 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
7421 __isl_take isl_local_space *ls)
7423 int i;
7425 map = isl_map_cow(map);
7426 if (!map || !ls)
7427 goto error;
7429 for (i = 0; i < map->n; ++i) {
7430 map->p[i] = basic_replace_space_by_local_space(map->p[i],
7431 isl_local_space_copy(ls));
7432 if (!map->p[i])
7433 goto error;
7435 isl_space_free(map->dim);
7436 map->dim = isl_local_space_get_space(ls);
7437 if (!map->dim)
7438 goto error;
7440 isl_local_space_free(ls);
7441 return map;
7442 error:
7443 isl_local_space_free(ls);
7444 isl_map_free(map);
7445 return NULL;
7448 /* Compute an explicit representation for the existentially
7449 * quantified variables for which do not know any explicit representation yet.
7451 * We first sort the existentially quantified variables so that the
7452 * existentially quantified variables for which we already have an explicit
7453 * representation are placed before those for which we do not.
7454 * The input dimensions, the output dimensions and the existentially
7455 * quantified variables for which we already have an explicit
7456 * representation are then turned into parameters.
7457 * compute_divs returns a map with the same parameters and
7458 * no input or output dimensions and the dimension specification
7459 * is reset to that of the input, including the existentially quantified
7460 * variables for which we already had an explicit representation.
7462 static __isl_give isl_map *compute_divs(__isl_take isl_basic_map *bmap)
7464 struct isl_basic_set *bset;
7465 struct isl_set *set;
7466 struct isl_map *map;
7467 isl_space *space;
7468 isl_local_space *ls;
7469 unsigned nparam;
7470 unsigned n_in;
7471 unsigned n_out;
7472 int n_known;
7473 int i;
7475 bmap = isl_basic_map_sort_divs(bmap);
7476 bmap = isl_basic_map_cow(bmap);
7477 if (!bmap)
7478 return NULL;
7480 n_known = isl_basic_map_first_unknown_div(bmap);
7481 if (n_known < 0)
7482 return isl_map_from_basic_map(isl_basic_map_free(bmap));
7484 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7485 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7486 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7487 space = isl_space_set_alloc(bmap->ctx,
7488 nparam + n_in + n_out + n_known, 0);
7489 if (!space)
7490 goto error;
7492 ls = isl_basic_map_get_local_space(bmap);
7493 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7494 n_known, bmap->n_div - n_known);
7495 if (n_known > 0) {
7496 for (i = n_known; i < bmap->n_div; ++i)
7497 swap_div(bmap, i - n_known, i);
7498 bmap->n_div -= n_known;
7499 bmap->extra -= n_known;
7501 bmap = isl_basic_map_reset_space(bmap, space);
7502 bset = bset_from_bmap(bmap);
7504 set = parameter_compute_divs(bset);
7505 map = set_to_map(set);
7506 map = replace_space_by_local_space(map, ls);
7508 return map;
7509 error:
7510 isl_basic_map_free(bmap);
7511 return NULL;
7514 /* Remove the explicit representation of local variable "div",
7515 * if there is any.
7517 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
7518 __isl_take isl_basic_map *bmap, int div)
7520 isl_bool unknown;
7522 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7523 if (unknown < 0)
7524 return isl_basic_map_free(bmap);
7525 if (unknown)
7526 return bmap;
7528 bmap = isl_basic_map_cow(bmap);
7529 if (!bmap)
7530 return NULL;
7531 isl_int_set_si(bmap->div[div][0], 0);
7532 return bmap;
7535 /* Is local variable "div" of "bmap" marked as not having an explicit
7536 * representation?
7537 * Note that even if "div" is not marked in this way and therefore
7538 * has an explicit representation, this representation may still
7539 * depend (indirectly) on other local variables that do not
7540 * have an explicit representation.
7542 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7543 int div)
7545 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7546 return isl_bool_error;
7547 return isl_int_is_zero(bmap->div[div][0]);
7550 /* Return the position of the first local variable that does not
7551 * have an explicit representation.
7552 * Return the total number of local variables if they all have
7553 * an explicit representation.
7554 * Return -1 on error.
7556 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7558 int i;
7560 if (!bmap)
7561 return -1;
7563 for (i = 0; i < bmap->n_div; ++i) {
7564 if (!isl_basic_map_div_is_known(bmap, i))
7565 return i;
7567 return bmap->n_div;
7570 /* Return the position of the first local variable that does not
7571 * have an explicit representation.
7572 * Return the total number of local variables if they all have
7573 * an explicit representation.
7574 * Return -1 on error.
7576 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7578 return isl_basic_map_first_unknown_div(bset);
7581 /* Does "bmap" have an explicit representation for all local variables?
7583 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7585 int first, n;
7587 n = isl_basic_map_dim(bmap, isl_dim_div);
7588 first = isl_basic_map_first_unknown_div(bmap);
7589 if (first < 0)
7590 return isl_bool_error;
7591 return first == n;
7594 /* Do all basic maps in "map" have an explicit representation
7595 * for all local variables?
7597 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7599 int i;
7601 if (!map)
7602 return isl_bool_error;
7604 for (i = 0; i < map->n; ++i) {
7605 int known = isl_basic_map_divs_known(map->p[i]);
7606 if (known <= 0)
7607 return known;
7610 return isl_bool_true;
7613 /* If bmap contains any unknown divs, then compute explicit
7614 * expressions for them. However, this computation may be
7615 * quite expensive, so first try to remove divs that aren't
7616 * strictly needed.
7618 __isl_give isl_map *isl_basic_map_compute_divs(__isl_take isl_basic_map *bmap)
7620 int known;
7621 struct isl_map *map;
7623 known = isl_basic_map_divs_known(bmap);
7624 if (known < 0)
7625 goto error;
7626 if (known)
7627 return isl_map_from_basic_map(bmap);
7629 bmap = isl_basic_map_drop_redundant_divs(bmap);
7631 known = isl_basic_map_divs_known(bmap);
7632 if (known < 0)
7633 goto error;
7634 if (known)
7635 return isl_map_from_basic_map(bmap);
7637 map = compute_divs(bmap);
7638 return map;
7639 error:
7640 isl_basic_map_free(bmap);
7641 return NULL;
7644 __isl_give isl_map *isl_map_compute_divs(__isl_take isl_map *map)
7646 int i;
7647 int known;
7648 struct isl_map *res;
7650 if (!map)
7651 return NULL;
7652 if (map->n == 0)
7653 return map;
7655 known = isl_map_divs_known(map);
7656 if (known < 0) {
7657 isl_map_free(map);
7658 return NULL;
7660 if (known)
7661 return map;
7663 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7664 for (i = 1 ; i < map->n; ++i) {
7665 struct isl_map *r2;
7666 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7667 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7668 res = isl_map_union_disjoint(res, r2);
7669 else
7670 res = isl_map_union(res, r2);
7672 isl_map_free(map);
7674 return res;
7677 __isl_give isl_set *isl_basic_set_compute_divs(__isl_take isl_basic_set *bset)
7679 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7682 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7684 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7687 __isl_give isl_set *isl_map_domain(__isl_take isl_map *map)
7689 int i;
7690 struct isl_set *set;
7692 if (!map)
7693 goto error;
7695 map = isl_map_cow(map);
7696 if (!map)
7697 return NULL;
7699 set = set_from_map(map);
7700 set->dim = isl_space_domain(set->dim);
7701 if (!set->dim)
7702 goto error;
7703 for (i = 0; i < map->n; ++i) {
7704 set->p[i] = isl_basic_map_domain(map->p[i]);
7705 if (!set->p[i])
7706 goto error;
7708 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7709 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7710 return set;
7711 error:
7712 isl_map_free(map);
7713 return NULL;
7716 /* Return the union of "map1" and "map2", where we assume for now that
7717 * "map1" and "map2" are disjoint. Note that the basic maps inside
7718 * "map1" or "map2" may not be disjoint from each other.
7719 * Also note that this function is also called from isl_map_union,
7720 * which takes care of handling the situation where "map1" and "map2"
7721 * may not be disjoint.
7723 * If one of the inputs is empty, we can simply return the other input.
7724 * Similarly, if one of the inputs is universal, then it is equal to the union.
7726 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7727 __isl_take isl_map *map2)
7729 int i;
7730 unsigned flags = 0;
7731 struct isl_map *map = NULL;
7732 int is_universe;
7734 if (!map1 || !map2)
7735 goto error;
7737 if (!isl_space_is_equal(map1->dim, map2->dim))
7738 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7739 "spaces don't match", goto error);
7741 if (map1->n == 0) {
7742 isl_map_free(map1);
7743 return map2;
7745 if (map2->n == 0) {
7746 isl_map_free(map2);
7747 return map1;
7750 is_universe = isl_map_plain_is_universe(map1);
7751 if (is_universe < 0)
7752 goto error;
7753 if (is_universe) {
7754 isl_map_free(map2);
7755 return map1;
7758 is_universe = isl_map_plain_is_universe(map2);
7759 if (is_universe < 0)
7760 goto error;
7761 if (is_universe) {
7762 isl_map_free(map1);
7763 return map2;
7766 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7767 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7768 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7770 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7771 map1->n + map2->n, flags);
7772 if (!map)
7773 goto error;
7774 for (i = 0; i < map1->n; ++i) {
7775 map = isl_map_add_basic_map(map,
7776 isl_basic_map_copy(map1->p[i]));
7777 if (!map)
7778 goto error;
7780 for (i = 0; i < map2->n; ++i) {
7781 map = isl_map_add_basic_map(map,
7782 isl_basic_map_copy(map2->p[i]));
7783 if (!map)
7784 goto error;
7786 isl_map_free(map1);
7787 isl_map_free(map2);
7788 return map;
7789 error:
7790 isl_map_free(map);
7791 isl_map_free(map1);
7792 isl_map_free(map2);
7793 return NULL;
7796 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7797 * guaranteed to be disjoint by the caller.
7799 * Note that this functions is called from within isl_map_make_disjoint,
7800 * so we have to be careful not to touch the constraints of the inputs
7801 * in any way.
7803 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7804 __isl_take isl_map *map2)
7806 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7809 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7810 * not be disjoint. The parameters are assumed to have been aligned.
7812 * We currently simply call map_union_disjoint, the internal operation
7813 * of which does not really depend on the inputs being disjoint.
7814 * If the result contains more than one basic map, then we clear
7815 * the disjoint flag since the result may contain basic maps from
7816 * both inputs and these are not guaranteed to be disjoint.
7818 * As a special case, if "map1" and "map2" are obviously equal,
7819 * then we simply return "map1".
7821 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7822 __isl_take isl_map *map2)
7824 int equal;
7826 if (!map1 || !map2)
7827 goto error;
7829 equal = isl_map_plain_is_equal(map1, map2);
7830 if (equal < 0)
7831 goto error;
7832 if (equal) {
7833 isl_map_free(map2);
7834 return map1;
7837 map1 = map_union_disjoint(map1, map2);
7838 if (!map1)
7839 return NULL;
7840 if (map1->n > 1)
7841 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7842 return map1;
7843 error:
7844 isl_map_free(map1);
7845 isl_map_free(map2);
7846 return NULL;
7849 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7850 * not be disjoint.
7852 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7853 __isl_take isl_map *map2)
7855 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7858 __isl_give isl_set *isl_set_union_disjoint(
7859 __isl_take isl_set *set1, __isl_take isl_set *set2)
7861 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7862 set_to_map(set2)));
7865 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7867 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7870 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7871 * the results.
7873 * "map" and "set" are assumed to be compatible and non-NULL.
7875 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7876 __isl_take isl_set *set,
7877 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7878 __isl_take isl_basic_set *bset))
7880 unsigned flags = 0;
7881 struct isl_map *result;
7882 int i, j;
7884 if (isl_set_plain_is_universe(set)) {
7885 isl_set_free(set);
7886 return map;
7889 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7890 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7891 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7893 result = isl_map_alloc_space(isl_space_copy(map->dim),
7894 map->n * set->n, flags);
7895 for (i = 0; result && i < map->n; ++i)
7896 for (j = 0; j < set->n; ++j) {
7897 result = isl_map_add_basic_map(result,
7898 fn(isl_basic_map_copy(map->p[i]),
7899 isl_basic_set_copy(set->p[j])));
7900 if (!result)
7901 break;
7904 isl_map_free(map);
7905 isl_set_free(set);
7906 return result;
7909 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7910 __isl_take isl_set *set)
7912 isl_bool ok;
7914 ok = isl_map_compatible_range(map, set);
7915 if (ok < 0)
7916 goto error;
7917 if (!ok)
7918 isl_die(set->ctx, isl_error_invalid,
7919 "incompatible spaces", goto error);
7921 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7922 error:
7923 isl_map_free(map);
7924 isl_set_free(set);
7925 return NULL;
7928 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7929 __isl_take isl_set *set)
7931 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7934 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7935 __isl_take isl_set *set)
7937 isl_bool ok;
7939 ok = isl_map_compatible_domain(map, set);
7940 if (ok < 0)
7941 goto error;
7942 if (!ok)
7943 isl_die(set->ctx, isl_error_invalid,
7944 "incompatible spaces", goto error);
7946 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7947 error:
7948 isl_map_free(map);
7949 isl_set_free(set);
7950 return NULL;
7953 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7954 __isl_take isl_set *set)
7956 return isl_map_align_params_map_map_and(map, set,
7957 &map_intersect_domain);
7960 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7961 * in the space B -> C, return the intersection.
7962 * The parameters are assumed to have been aligned.
7964 * The map "factor" is first extended to a map living in the space
7965 * [A -> B] -> C and then a regular intersection is computed.
7967 static __isl_give isl_map *map_intersect_domain_factor_range(
7968 __isl_take isl_map *map, __isl_take isl_map *factor)
7970 isl_space *space;
7971 isl_map *ext_factor;
7973 space = isl_space_domain_factor_domain(isl_map_get_space(map));
7974 ext_factor = isl_map_universe(space);
7975 ext_factor = isl_map_domain_product(ext_factor, factor);
7976 return map_intersect(map, ext_factor);
7979 /* Given a map "map" in a space [A -> B] -> C and a map "factor"
7980 * in the space B -> C, return the intersection.
7982 __isl_give isl_map *isl_map_intersect_domain_factor_range(
7983 __isl_take isl_map *map, __isl_take isl_map *factor)
7985 return isl_map_align_params_map_map_and(map, factor,
7986 &map_intersect_domain_factor_range);
7989 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
7990 * in the space A -> C, return the intersection.
7992 * The map "factor" is first extended to a map living in the space
7993 * A -> [B -> C] and then a regular intersection is computed.
7995 static __isl_give isl_map *map_intersect_range_factor_range(
7996 __isl_take isl_map *map, __isl_take isl_map *factor)
7998 isl_space *space;
7999 isl_map *ext_factor;
8001 space = isl_space_range_factor_domain(isl_map_get_space(map));
8002 ext_factor = isl_map_universe(space);
8003 ext_factor = isl_map_range_product(ext_factor, factor);
8004 return isl_map_intersect(map, ext_factor);
8007 /* Given a map "map" in a space A -> [B -> C] and a map "factor"
8008 * in the space A -> C, return the intersection.
8010 __isl_give isl_map *isl_map_intersect_range_factor_range(
8011 __isl_take isl_map *map, __isl_take isl_map *factor)
8013 return isl_map_align_params_map_map_and(map, factor,
8014 &map_intersect_range_factor_range);
8017 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
8018 __isl_take isl_map *map2)
8020 if (!map1 || !map2)
8021 goto error;
8022 map1 = isl_map_reverse(map1);
8023 map1 = isl_map_apply_range(map1, map2);
8024 return isl_map_reverse(map1);
8025 error:
8026 isl_map_free(map1);
8027 isl_map_free(map2);
8028 return NULL;
8031 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
8032 __isl_take isl_map *map2)
8034 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
8037 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
8038 __isl_take isl_map *map2)
8040 isl_space *space;
8041 struct isl_map *result;
8042 int i, j;
8044 if (!map1 || !map2)
8045 goto error;
8047 space = isl_space_join(isl_space_copy(map1->dim),
8048 isl_space_copy(map2->dim));
8050 result = isl_map_alloc_space(space, map1->n * map2->n, 0);
8051 if (!result)
8052 goto error;
8053 for (i = 0; i < map1->n; ++i)
8054 for (j = 0; j < map2->n; ++j) {
8055 result = isl_map_add_basic_map(result,
8056 isl_basic_map_apply_range(
8057 isl_basic_map_copy(map1->p[i]),
8058 isl_basic_map_copy(map2->p[j])));
8059 if (!result)
8060 goto error;
8062 isl_map_free(map1);
8063 isl_map_free(map2);
8064 if (result && result->n <= 1)
8065 ISL_F_SET(result, ISL_MAP_DISJOINT);
8066 return result;
8067 error:
8068 isl_map_free(map1);
8069 isl_map_free(map2);
8070 return NULL;
8073 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
8074 __isl_take isl_map *map2)
8076 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
8080 * returns range - domain
8082 __isl_give isl_basic_set *isl_basic_map_deltas(__isl_take isl_basic_map *bmap)
8084 isl_space *target_space;
8085 struct isl_basic_set *bset;
8086 unsigned dim;
8087 unsigned nparam;
8088 int i;
8090 if (!bmap)
8091 goto error;
8092 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8093 bmap->dim, isl_dim_out),
8094 goto error);
8095 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
8096 dim = isl_basic_map_dim(bmap, isl_dim_in);
8097 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8098 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
8099 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
8100 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
8101 for (i = 0; i < dim; ++i) {
8102 int j = isl_basic_map_alloc_equality(bmap);
8103 if (j < 0) {
8104 bmap = isl_basic_map_free(bmap);
8105 break;
8107 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
8108 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
8109 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
8110 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
8112 bset = isl_basic_map_domain(bmap);
8113 bset = isl_basic_set_reset_space(bset, target_space);
8114 return bset;
8115 error:
8116 isl_basic_map_free(bmap);
8117 return NULL;
8121 * returns range - domain
8123 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
8125 int i;
8126 isl_space *dim;
8127 struct isl_set *result;
8129 if (!map)
8130 return NULL;
8132 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
8133 map->dim, isl_dim_out),
8134 goto error);
8135 dim = isl_map_get_space(map);
8136 dim = isl_space_domain(dim);
8137 result = isl_set_alloc_space(dim, map->n, 0);
8138 if (!result)
8139 goto error;
8140 for (i = 0; i < map->n; ++i)
8141 result = isl_set_add_basic_set(result,
8142 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
8143 isl_map_free(map);
8144 return result;
8145 error:
8146 isl_map_free(map);
8147 return NULL;
8151 * returns [domain -> range] -> range - domain
8153 __isl_give isl_basic_map *isl_basic_map_deltas_map(
8154 __isl_take isl_basic_map *bmap)
8156 int i, k;
8157 isl_space *space;
8158 isl_basic_map *domain;
8159 int nparam, n;
8160 unsigned total;
8162 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
8163 bmap->dim, isl_dim_out))
8164 isl_die(bmap->ctx, isl_error_invalid,
8165 "domain and range don't match", goto error);
8167 nparam = isl_basic_map_dim(bmap, isl_dim_param);
8168 n = isl_basic_map_dim(bmap, isl_dim_in);
8170 space = isl_basic_map_get_space(bmap);
8171 space = isl_space_from_range(isl_space_domain(space));
8172 domain = isl_basic_map_universe(space);
8174 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
8175 bmap = isl_basic_map_apply_range(bmap, domain);
8176 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
8178 total = isl_basic_map_total_dim(bmap);
8180 for (i = 0; i < n; ++i) {
8181 k = isl_basic_map_alloc_equality(bmap);
8182 if (k < 0)
8183 goto error;
8184 isl_seq_clr(bmap->eq[k], 1 + total);
8185 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
8186 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
8187 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
8190 bmap = isl_basic_map_gauss(bmap, NULL);
8191 return isl_basic_map_finalize(bmap);
8192 error:
8193 isl_basic_map_free(bmap);
8194 return NULL;
8198 * returns [domain -> range] -> range - domain
8200 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
8202 int i;
8203 isl_space *domain_space;
8205 if (!map)
8206 return NULL;
8208 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
8209 map->dim, isl_dim_out))
8210 isl_die(map->ctx, isl_error_invalid,
8211 "domain and range don't match", goto error);
8213 map = isl_map_cow(map);
8214 if (!map)
8215 return NULL;
8217 domain_space = isl_space_domain(isl_map_get_space(map));
8218 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
8219 map->dim = isl_space_join(map->dim, isl_space_from_range(domain_space));
8220 if (!map->dim)
8221 goto error;
8222 for (i = 0; i < map->n; ++i) {
8223 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
8224 if (!map->p[i])
8225 goto error;
8227 map = isl_map_unmark_normalized(map);
8228 return map;
8229 error:
8230 isl_map_free(map);
8231 return NULL;
8234 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *space)
8236 unsigned n_in, n_out;
8238 if (!space)
8239 return NULL;
8240 n_in = isl_space_dim(space, isl_dim_in);
8241 n_out = isl_space_dim(space, isl_dim_out);
8242 if (n_in != n_out)
8243 isl_die(space->ctx, isl_error_invalid,
8244 "number of input and output dimensions needs to be "
8245 "the same", goto error);
8246 return isl_basic_map_equal(space, n_in);
8247 error:
8248 isl_space_free(space);
8249 return NULL;
8252 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
8254 return isl_map_from_basic_map(isl_basic_map_identity(dim));
8257 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
8259 isl_space *dim = isl_set_get_space(set);
8260 isl_map *id;
8261 id = isl_map_identity(isl_space_map_from_set(dim));
8262 return isl_map_intersect_range(id, set);
8265 /* Construct a basic set with all set dimensions having only non-negative
8266 * values.
8268 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
8269 __isl_take isl_space *space)
8271 int i;
8272 unsigned nparam;
8273 unsigned dim;
8274 struct isl_basic_set *bset;
8276 if (!space)
8277 return NULL;
8278 nparam = space->nparam;
8279 dim = space->n_out;
8280 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
8281 if (!bset)
8282 return NULL;
8283 for (i = 0; i < dim; ++i) {
8284 int k = isl_basic_set_alloc_inequality(bset);
8285 if (k < 0)
8286 goto error;
8287 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
8288 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
8290 return bset;
8291 error:
8292 isl_basic_set_free(bset);
8293 return NULL;
8296 /* Construct the half-space x_pos >= 0.
8298 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *space,
8299 int pos)
8301 int k;
8302 isl_basic_set *nonneg;
8304 nonneg = isl_basic_set_alloc_space(space, 0, 0, 1);
8305 k = isl_basic_set_alloc_inequality(nonneg);
8306 if (k < 0)
8307 goto error;
8308 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
8309 isl_int_set_si(nonneg->ineq[k][pos], 1);
8311 return isl_basic_set_finalize(nonneg);
8312 error:
8313 isl_basic_set_free(nonneg);
8314 return NULL;
8317 /* Construct the half-space x_pos <= -1.
8319 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *space,
8320 int pos)
8322 int k;
8323 isl_basic_set *neg;
8325 neg = isl_basic_set_alloc_space(space, 0, 0, 1);
8326 k = isl_basic_set_alloc_inequality(neg);
8327 if (k < 0)
8328 goto error;
8329 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
8330 isl_int_set_si(neg->ineq[k][0], -1);
8331 isl_int_set_si(neg->ineq[k][pos], -1);
8333 return isl_basic_set_finalize(neg);
8334 error:
8335 isl_basic_set_free(neg);
8336 return NULL;
8339 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
8340 enum isl_dim_type type, unsigned first, unsigned n)
8342 int i;
8343 unsigned offset;
8344 isl_basic_set *nonneg;
8345 isl_basic_set *neg;
8347 if (!set)
8348 return NULL;
8349 if (n == 0)
8350 return set;
8352 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
8354 offset = pos(set->dim, type);
8355 for (i = 0; i < n; ++i) {
8356 nonneg = nonneg_halfspace(isl_set_get_space(set),
8357 offset + first + i);
8358 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
8360 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
8363 return set;
8364 error:
8365 isl_set_free(set);
8366 return NULL;
8369 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
8370 int len,
8371 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8372 void *user)
8374 isl_set *half;
8376 if (!set)
8377 return isl_stat_error;
8378 if (isl_set_plain_is_empty(set)) {
8379 isl_set_free(set);
8380 return isl_stat_ok;
8382 if (first == len)
8383 return fn(set, signs, user);
8385 signs[first] = 1;
8386 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
8387 1 + first));
8388 half = isl_set_intersect(half, isl_set_copy(set));
8389 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
8390 goto error;
8392 signs[first] = -1;
8393 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
8394 1 + first));
8395 half = isl_set_intersect(half, set);
8396 return foreach_orthant(half, signs, first + 1, len, fn, user);
8397 error:
8398 isl_set_free(set);
8399 return isl_stat_error;
8402 /* Call "fn" on the intersections of "set" with each of the orthants
8403 * (except for obviously empty intersections). The orthant is identified
8404 * by the signs array, with each entry having value 1 or -1 according
8405 * to the sign of the corresponding variable.
8407 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
8408 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
8409 void *user)
8411 unsigned nparam;
8412 unsigned nvar;
8413 int *signs;
8414 isl_stat r;
8416 if (!set)
8417 return isl_stat_error;
8418 if (isl_set_plain_is_empty(set))
8419 return isl_stat_ok;
8421 nparam = isl_set_dim(set, isl_dim_param);
8422 nvar = isl_set_dim(set, isl_dim_set);
8424 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
8426 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
8427 fn, user);
8429 free(signs);
8431 return r;
8434 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8436 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
8439 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
8440 __isl_keep isl_basic_map *bmap2)
8442 isl_bool is_subset;
8443 struct isl_map *map1;
8444 struct isl_map *map2;
8446 if (!bmap1 || !bmap2)
8447 return isl_bool_error;
8449 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
8450 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
8452 is_subset = isl_map_is_subset(map1, map2);
8454 isl_map_free(map1);
8455 isl_map_free(map2);
8457 return is_subset;
8460 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
8461 __isl_keep isl_basic_set *bset2)
8463 return isl_basic_map_is_subset(bset1, bset2);
8466 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
8467 __isl_keep isl_basic_map *bmap2)
8469 isl_bool is_subset;
8471 if (!bmap1 || !bmap2)
8472 return isl_bool_error;
8473 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8474 if (is_subset != isl_bool_true)
8475 return is_subset;
8476 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8477 return is_subset;
8480 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
8481 __isl_keep isl_basic_set *bset2)
8483 return isl_basic_map_is_equal(
8484 bset_to_bmap(bset1), bset_to_bmap(bset2));
8487 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
8489 int i;
8490 int is_empty;
8492 if (!map)
8493 return isl_bool_error;
8494 for (i = 0; i < map->n; ++i) {
8495 is_empty = isl_basic_map_is_empty(map->p[i]);
8496 if (is_empty < 0)
8497 return isl_bool_error;
8498 if (!is_empty)
8499 return isl_bool_false;
8501 return isl_bool_true;
8504 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
8506 return map ? map->n == 0 : isl_bool_error;
8509 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
8511 return set ? set->n == 0 : isl_bool_error;
8514 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
8516 return isl_map_is_empty(set_to_map(set));
8519 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
8520 __isl_keep isl_map *map2)
8522 if (!map1 || !map2)
8523 return isl_bool_error;
8525 return isl_space_is_equal(map1->dim, map2->dim);
8528 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
8529 __isl_keep isl_set *set2)
8531 if (!set1 || !set2)
8532 return isl_bool_error;
8534 return isl_space_is_equal(set1->dim, set2->dim);
8537 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8539 isl_bool is_subset;
8541 if (!map1 || !map2)
8542 return isl_bool_error;
8543 is_subset = isl_map_is_subset(map1, map2);
8544 if (is_subset != isl_bool_true)
8545 return is_subset;
8546 is_subset = isl_map_is_subset(map2, map1);
8547 return is_subset;
8550 /* Is "map1" equal to "map2"?
8552 * First check if they are obviously equal.
8553 * If not, then perform a more detailed analysis.
8555 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8557 isl_bool equal;
8559 equal = isl_map_plain_is_equal(map1, map2);
8560 if (equal < 0 || equal)
8561 return equal;
8562 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8565 isl_bool isl_basic_map_is_strict_subset(
8566 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8568 isl_bool is_subset;
8570 if (!bmap1 || !bmap2)
8571 return isl_bool_error;
8572 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8573 if (is_subset != isl_bool_true)
8574 return is_subset;
8575 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8576 if (is_subset == isl_bool_error)
8577 return is_subset;
8578 return !is_subset;
8581 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8582 __isl_keep isl_map *map2)
8584 isl_bool is_subset;
8586 if (!map1 || !map2)
8587 return isl_bool_error;
8588 is_subset = isl_map_is_subset(map1, map2);
8589 if (is_subset != isl_bool_true)
8590 return is_subset;
8591 is_subset = isl_map_is_subset(map2, map1);
8592 if (is_subset == isl_bool_error)
8593 return is_subset;
8594 return !is_subset;
8597 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8598 __isl_keep isl_set *set2)
8600 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8603 /* Is "bmap" obviously equal to the universe with the same space?
8605 * That is, does it not have any constraints?
8607 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8609 if (!bmap)
8610 return isl_bool_error;
8611 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8614 /* Is "bset" obviously equal to the universe with the same space?
8616 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8618 return isl_basic_map_plain_is_universe(bset);
8621 /* If "c" does not involve any existentially quantified variables,
8622 * then set *univ to false and abort
8624 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8626 isl_bool *univ = user;
8627 unsigned n;
8629 n = isl_constraint_dim(c, isl_dim_div);
8630 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8631 isl_constraint_free(c);
8632 if (*univ < 0 || !*univ)
8633 return isl_stat_error;
8634 return isl_stat_ok;
8637 /* Is "bmap" equal to the universe with the same space?
8639 * First check if it is obviously equal to the universe.
8640 * If not and if there are any constraints not involving
8641 * existentially quantified variables, then it is certainly
8642 * not equal to the universe.
8643 * Otherwise, check if the universe is a subset of "bmap".
8645 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8647 isl_bool univ;
8648 isl_basic_map *test;
8650 univ = isl_basic_map_plain_is_universe(bmap);
8651 if (univ < 0 || univ)
8652 return univ;
8653 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8654 return isl_bool_false;
8655 univ = isl_bool_true;
8656 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8657 univ)
8658 return isl_bool_error;
8659 if (univ < 0 || !univ)
8660 return univ;
8661 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8662 univ = isl_basic_map_is_subset(test, bmap);
8663 isl_basic_map_free(test);
8664 return univ;
8667 /* Is "bset" equal to the universe with the same space?
8669 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8671 return isl_basic_map_is_universe(bset);
8674 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8676 int i;
8678 if (!map)
8679 return isl_bool_error;
8681 for (i = 0; i < map->n; ++i) {
8682 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8683 if (r < 0 || r)
8684 return r;
8687 return isl_bool_false;
8690 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8692 return isl_map_plain_is_universe(set_to_map(set));
8695 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8697 struct isl_basic_set *bset = NULL;
8698 struct isl_vec *sample = NULL;
8699 isl_bool empty, non_empty;
8701 if (!bmap)
8702 return isl_bool_error;
8704 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8705 return isl_bool_true;
8707 if (isl_basic_map_plain_is_universe(bmap))
8708 return isl_bool_false;
8710 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8711 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8712 copy = isl_basic_map_remove_redundancies(copy);
8713 empty = isl_basic_map_plain_is_empty(copy);
8714 isl_basic_map_free(copy);
8715 return empty;
8718 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8719 if (non_empty < 0)
8720 return isl_bool_error;
8721 if (non_empty)
8722 return isl_bool_false;
8723 isl_vec_free(bmap->sample);
8724 bmap->sample = NULL;
8725 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8726 if (!bset)
8727 return isl_bool_error;
8728 sample = isl_basic_set_sample_vec(bset);
8729 if (!sample)
8730 return isl_bool_error;
8731 empty = sample->size == 0;
8732 isl_vec_free(bmap->sample);
8733 bmap->sample = sample;
8734 if (empty)
8735 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8737 return empty;
8740 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8742 if (!bmap)
8743 return isl_bool_error;
8744 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8747 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8749 if (!bset)
8750 return isl_bool_error;
8751 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8754 /* Is "bmap" known to be non-empty?
8756 * That is, is the cached sample still valid?
8758 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8760 unsigned total;
8762 if (!bmap)
8763 return isl_bool_error;
8764 if (!bmap->sample)
8765 return isl_bool_false;
8766 total = 1 + isl_basic_map_total_dim(bmap);
8767 if (bmap->sample->size != total)
8768 return isl_bool_false;
8769 return isl_basic_map_contains(bmap, bmap->sample);
8772 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8774 return isl_basic_map_is_empty(bset_to_bmap(bset));
8777 __isl_give isl_map *isl_basic_map_union(__isl_take isl_basic_map *bmap1,
8778 __isl_take isl_basic_map *bmap2)
8780 struct isl_map *map;
8781 if (!bmap1 || !bmap2)
8782 goto error;
8784 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8786 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8787 if (!map)
8788 goto error;
8789 map = isl_map_add_basic_map(map, bmap1);
8790 map = isl_map_add_basic_map(map, bmap2);
8791 return map;
8792 error:
8793 isl_basic_map_free(bmap1);
8794 isl_basic_map_free(bmap2);
8795 return NULL;
8798 struct isl_set *isl_basic_set_union(
8799 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8801 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8802 bset_to_bmap(bset2)));
8805 /* Order divs such that any div only depends on previous divs */
8806 __isl_give isl_basic_map *isl_basic_map_order_divs(
8807 __isl_take isl_basic_map *bmap)
8809 int i;
8810 unsigned off;
8812 if (!bmap)
8813 return NULL;
8815 off = isl_space_dim(bmap->dim, isl_dim_all);
8817 for (i = 0; i < bmap->n_div; ++i) {
8818 int pos;
8819 if (isl_int_is_zero(bmap->div[i][0]))
8820 continue;
8821 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8822 bmap->n_div-i);
8823 if (pos == -1)
8824 continue;
8825 if (pos == 0)
8826 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8827 "integer division depends on itself",
8828 return isl_basic_map_free(bmap));
8829 isl_basic_map_swap_div(bmap, i, i + pos);
8830 --i;
8832 return bmap;
8835 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8837 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8840 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8842 int i;
8844 if (!map)
8845 return 0;
8847 for (i = 0; i < map->n; ++i) {
8848 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8849 if (!map->p[i])
8850 goto error;
8853 return map;
8854 error:
8855 isl_map_free(map);
8856 return NULL;
8859 /* Sort the local variables of "bset".
8861 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8862 __isl_take isl_basic_set *bset)
8864 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8867 /* Apply the expansion computed by isl_merge_divs.
8868 * The expansion itself is given by "exp" while the resulting
8869 * list of divs is given by "div".
8871 * Move the integer divisions of "bmap" into the right position
8872 * according to "exp" and then introduce the additional integer
8873 * divisions, adding div constraints.
8874 * The moving should be done first to avoid moving coefficients
8875 * in the definitions of the extra integer divisions.
8877 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8878 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8880 int i, j;
8881 int n_div;
8883 bmap = isl_basic_map_cow(bmap);
8884 if (!bmap || !div)
8885 goto error;
8887 if (div->n_row < bmap->n_div)
8888 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8889 "not an expansion", goto error);
8891 n_div = bmap->n_div;
8892 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8893 div->n_row - n_div, 0,
8894 2 * (div->n_row - n_div));
8896 for (i = n_div; i < div->n_row; ++i)
8897 if (isl_basic_map_alloc_div(bmap) < 0)
8898 goto error;
8900 for (j = n_div - 1; j >= 0; --j) {
8901 if (exp[j] == j)
8902 break;
8903 isl_basic_map_swap_div(bmap, j, exp[j]);
8905 j = 0;
8906 for (i = 0; i < div->n_row; ++i) {
8907 if (j < n_div && exp[j] == i) {
8908 j++;
8909 } else {
8910 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8911 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8912 continue;
8913 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8914 goto error;
8918 isl_mat_free(div);
8919 return bmap;
8920 error:
8921 isl_basic_map_free(bmap);
8922 isl_mat_free(div);
8923 return NULL;
8926 /* Apply the expansion computed by isl_merge_divs.
8927 * The expansion itself is given by "exp" while the resulting
8928 * list of divs is given by "div".
8930 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8931 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8933 return isl_basic_map_expand_divs(bset, div, exp);
8936 /* Look for a div in dst that corresponds to the div "div" in src.
8937 * The divs before "div" in src and dst are assumed to be the same.
8939 * Returns -1 if no corresponding div was found and the position
8940 * of the corresponding div in dst otherwise.
8942 static int find_div(__isl_keep isl_basic_map *dst,
8943 __isl_keep isl_basic_map *src, unsigned div)
8945 int i;
8947 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8949 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8950 for (i = div; i < dst->n_div; ++i)
8951 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8952 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8953 dst->n_div - div) == -1)
8954 return i;
8955 return -1;
8958 /* Align the divs of "dst" to those of "src", adding divs from "src"
8959 * if needed. That is, make sure that the first src->n_div divs
8960 * of the result are equal to those of src.
8962 * The result is not finalized as by design it will have redundant
8963 * divs if any divs from "src" were copied.
8965 __isl_give isl_basic_map *isl_basic_map_align_divs(
8966 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8968 int i;
8969 isl_bool known;
8970 int extended;
8971 unsigned total;
8973 if (!dst || !src)
8974 return isl_basic_map_free(dst);
8976 if (src->n_div == 0)
8977 return dst;
8979 known = isl_basic_map_divs_known(src);
8980 if (known < 0)
8981 return isl_basic_map_free(dst);
8982 if (!known)
8983 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8984 "some src divs are unknown",
8985 return isl_basic_map_free(dst));
8987 src = isl_basic_map_order_divs(isl_basic_map_copy(src));
8988 if (!src)
8989 return isl_basic_map_free(dst);
8991 extended = 0;
8992 total = isl_space_dim(src->dim, isl_dim_all);
8993 for (i = 0; i < src->n_div; ++i) {
8994 int j = find_div(dst, src, i);
8995 if (j < 0) {
8996 if (!extended) {
8997 int extra = src->n_div - i;
8998 dst = isl_basic_map_cow(dst);
8999 if (!dst)
9000 goto error;
9001 dst = isl_basic_map_extend_space(dst,
9002 isl_space_copy(dst->dim),
9003 extra, 0, 2 * extra);
9004 extended = 1;
9006 j = isl_basic_map_alloc_div(dst);
9007 if (j < 0)
9008 goto error;
9009 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
9010 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
9011 if (isl_basic_map_add_div_constraints(dst, j) < 0)
9012 goto error;
9014 if (j != i)
9015 isl_basic_map_swap_div(dst, i, j);
9017 isl_basic_map_free(src);
9018 return dst;
9019 error:
9020 isl_basic_map_free(src);
9021 isl_basic_map_free(dst);
9022 return NULL;
9025 __isl_give isl_map *isl_map_align_divs_internal(__isl_take isl_map *map)
9027 int i;
9029 if (!map)
9030 return NULL;
9031 if (map->n == 0)
9032 return map;
9033 map = isl_map_compute_divs(map);
9034 map = isl_map_cow(map);
9035 if (!map)
9036 return NULL;
9038 for (i = 1; i < map->n; ++i)
9039 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
9040 for (i = 1; i < map->n; ++i) {
9041 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
9042 if (!map->p[i])
9043 return isl_map_free(map);
9046 map = isl_map_unmark_normalized(map);
9047 return map;
9050 __isl_give isl_map *isl_map_align_divs(__isl_take isl_map *map)
9052 return isl_map_align_divs_internal(map);
9055 struct isl_set *isl_set_align_divs(struct isl_set *set)
9057 return set_from_map(isl_map_align_divs_internal(set_to_map(set)));
9060 /* Align the divs of the basic maps in "map" to those
9061 * of the basic maps in "list", as well as to the other basic maps in "map".
9062 * The elements in "list" are assumed to have known divs.
9064 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
9065 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
9067 int i, n;
9069 map = isl_map_compute_divs(map);
9070 map = isl_map_cow(map);
9071 if (!map || !list)
9072 return isl_map_free(map);
9073 if (map->n == 0)
9074 return map;
9076 n = isl_basic_map_list_n_basic_map(list);
9077 for (i = 0; i < n; ++i) {
9078 isl_basic_map *bmap;
9080 bmap = isl_basic_map_list_get_basic_map(list, i);
9081 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
9082 isl_basic_map_free(bmap);
9084 if (!map->p[0])
9085 return isl_map_free(map);
9087 return isl_map_align_divs_internal(map);
9090 /* Align the divs of each element of "list" to those of "bmap".
9091 * Both "bmap" and the elements of "list" are assumed to have known divs.
9093 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
9094 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
9096 int i, n;
9098 if (!list || !bmap)
9099 return isl_basic_map_list_free(list);
9101 n = isl_basic_map_list_n_basic_map(list);
9102 for (i = 0; i < n; ++i) {
9103 isl_basic_map *bmap_i;
9105 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9106 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
9107 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
9110 return list;
9113 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
9114 __isl_take isl_map *map)
9116 isl_bool ok;
9118 ok = isl_map_compatible_domain(map, set);
9119 if (ok < 0)
9120 goto error;
9121 if (!ok)
9122 isl_die(isl_set_get_ctx(set), isl_error_invalid,
9123 "incompatible spaces", goto error);
9124 map = isl_map_intersect_domain(map, set);
9125 set = isl_map_range(map);
9126 return set;
9127 error:
9128 isl_set_free(set);
9129 isl_map_free(map);
9130 return NULL;
9133 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
9134 __isl_take isl_map *map)
9136 return isl_map_align_params_map_map_and(set, map, &set_apply);
9139 /* There is no need to cow as removing empty parts doesn't change
9140 * the meaning of the set.
9142 __isl_give isl_map *isl_map_remove_empty_parts(__isl_take isl_map *map)
9144 int i;
9146 if (!map)
9147 return NULL;
9149 for (i = map->n - 1; i >= 0; --i)
9150 map = remove_if_empty(map, i);
9152 return map;
9155 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
9157 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
9160 /* Create a binary relation that maps the shared initial "pos" dimensions
9161 * of "bset1" and "bset2" to the remaining dimensions of "bset1" and "bset2".
9163 static __isl_give isl_basic_map *join_initial(__isl_keep isl_basic_set *bset1,
9164 __isl_keep isl_basic_set *bset2, int pos)
9166 isl_basic_map *bmap1;
9167 isl_basic_map *bmap2;
9169 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
9170 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
9171 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
9172 isl_dim_out, 0, pos);
9173 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
9174 isl_dim_out, 0, pos);
9175 return isl_basic_map_range_product(bmap1, bmap2);
9178 /* Given two basic sets bset1 and bset2, compute the maximal difference
9179 * between the values of dimension pos in bset1 and those in bset2
9180 * for any common value of the parameters and dimensions preceding pos.
9182 static enum isl_lp_result basic_set_maximal_difference_at(
9183 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
9184 int pos, isl_int *opt)
9186 isl_basic_map *bmap1;
9187 struct isl_ctx *ctx;
9188 struct isl_vec *obj;
9189 unsigned total;
9190 unsigned nparam;
9191 unsigned dim1;
9192 enum isl_lp_result res;
9194 if (!bset1 || !bset2)
9195 return isl_lp_error;
9197 nparam = isl_basic_set_n_param(bset1);
9198 dim1 = isl_basic_set_n_dim(bset1);
9200 bmap1 = join_initial(bset1, bset2, pos);
9201 if (!bmap1)
9202 return isl_lp_error;
9204 total = isl_basic_map_total_dim(bmap1);
9205 ctx = bmap1->ctx;
9206 obj = isl_vec_alloc(ctx, 1 + total);
9207 if (!obj)
9208 goto error;
9209 isl_seq_clr(obj->block.data, 1 + total);
9210 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
9211 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
9212 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
9213 opt, NULL, NULL);
9214 isl_basic_map_free(bmap1);
9215 isl_vec_free(obj);
9216 return res;
9217 error:
9218 isl_basic_map_free(bmap1);
9219 return isl_lp_error;
9222 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
9223 * for any common value of the parameters and dimensions preceding pos
9224 * in both basic sets, the values of dimension pos in bset1 are
9225 * smaller or larger than those in bset2.
9227 * Returns
9228 * 1 if bset1 follows bset2
9229 * -1 if bset1 precedes bset2
9230 * 0 if bset1 and bset2 are incomparable
9231 * -2 if some error occurred.
9233 int isl_basic_set_compare_at(__isl_keep isl_basic_set *bset1,
9234 __isl_keep isl_basic_set *bset2, int pos)
9236 isl_int opt;
9237 enum isl_lp_result res;
9238 int cmp;
9240 isl_int_init(opt);
9242 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
9244 if (res == isl_lp_empty)
9245 cmp = 0;
9246 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
9247 res == isl_lp_unbounded)
9248 cmp = 1;
9249 else if (res == isl_lp_ok && isl_int_is_neg(opt))
9250 cmp = -1;
9251 else
9252 cmp = -2;
9254 isl_int_clear(opt);
9255 return cmp;
9258 /* Given two basic sets bset1 and bset2, check whether
9259 * for any common value of the parameters and dimensions preceding pos
9260 * there is a value of dimension pos in bset1 that is larger
9261 * than a value of the same dimension in bset2.
9263 * Return
9264 * 1 if there exists such a pair
9265 * 0 if there is no such pair, but there is a pair of equal values
9266 * -1 otherwise
9267 * -2 if some error occurred.
9269 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
9270 __isl_keep isl_basic_set *bset2, int pos)
9272 isl_bool empty;
9273 isl_basic_map *bmap;
9274 unsigned dim1;
9276 dim1 = isl_basic_set_dim(bset1, isl_dim_set);
9277 bmap = join_initial(bset1, bset2, pos);
9278 bmap = isl_basic_map_order_ge(bmap, isl_dim_out, 0,
9279 isl_dim_out, dim1 - pos);
9280 empty = isl_basic_map_is_empty(bmap);
9281 if (empty < 0)
9282 goto error;
9283 if (empty) {
9284 isl_basic_map_free(bmap);
9285 return -1;
9287 bmap = isl_basic_map_order_gt(bmap, isl_dim_out, 0,
9288 isl_dim_out, dim1 - pos);
9289 empty = isl_basic_map_is_empty(bmap);
9290 if (empty < 0)
9291 goto error;
9292 isl_basic_map_free(bmap);
9293 if (empty)
9294 return 0;
9295 return 1;
9296 error:
9297 isl_basic_map_free(bmap);
9298 return -2;
9301 /* Given two sets set1 and set2, check whether
9302 * for any common value of the parameters and dimensions preceding pos
9303 * there is a value of dimension pos in set1 that is larger
9304 * than a value of the same dimension in set2.
9306 * Return
9307 * 1 if there exists such a pair
9308 * 0 if there is no such pair, but there is a pair of equal values
9309 * -1 otherwise
9310 * -2 if some error occurred.
9312 int isl_set_follows_at(__isl_keep isl_set *set1,
9313 __isl_keep isl_set *set2, int pos)
9315 int i, j;
9316 int follows = -1;
9318 if (!set1 || !set2)
9319 return -2;
9321 for (i = 0; i < set1->n; ++i)
9322 for (j = 0; j < set2->n; ++j) {
9323 int f;
9324 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
9325 if (f == 1 || f == -2)
9326 return f;
9327 if (f > follows)
9328 follows = f;
9331 return follows;
9334 static isl_bool isl_basic_map_plain_has_fixed_var(
9335 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
9337 int i;
9338 int d;
9339 unsigned total;
9341 if (!bmap)
9342 return isl_bool_error;
9343 total = isl_basic_map_total_dim(bmap);
9344 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
9345 for (; d+1 > pos; --d)
9346 if (!isl_int_is_zero(bmap->eq[i][1+d]))
9347 break;
9348 if (d != pos)
9349 continue;
9350 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
9351 return isl_bool_false;
9352 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
9353 return isl_bool_false;
9354 if (!isl_int_is_one(bmap->eq[i][1+d]))
9355 return isl_bool_false;
9356 if (val)
9357 isl_int_neg(*val, bmap->eq[i][0]);
9358 return isl_bool_true;
9360 return isl_bool_false;
9363 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
9364 unsigned pos, isl_int *val)
9366 int i;
9367 isl_int v;
9368 isl_int tmp;
9369 isl_bool fixed;
9371 if (!map)
9372 return isl_bool_error;
9373 if (map->n == 0)
9374 return isl_bool_false;
9375 if (map->n == 1)
9376 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
9377 isl_int_init(v);
9378 isl_int_init(tmp);
9379 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
9380 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
9381 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
9382 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
9383 fixed = isl_bool_false;
9385 if (val)
9386 isl_int_set(*val, v);
9387 isl_int_clear(tmp);
9388 isl_int_clear(v);
9389 return fixed;
9392 static isl_bool isl_basic_set_plain_has_fixed_var(
9393 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
9395 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
9396 pos, val);
9399 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
9400 enum isl_dim_type type, unsigned pos, isl_int *val)
9402 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
9403 return isl_bool_error;
9404 return isl_basic_map_plain_has_fixed_var(bmap,
9405 isl_basic_map_offset(bmap, type) - 1 + pos, val);
9408 /* If "bmap" obviously lies on a hyperplane where the given dimension
9409 * has a fixed value, then return that value.
9410 * Otherwise return NaN.
9412 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
9413 __isl_keep isl_basic_map *bmap,
9414 enum isl_dim_type type, unsigned pos)
9416 isl_ctx *ctx;
9417 isl_val *v;
9418 isl_bool fixed;
9420 if (!bmap)
9421 return NULL;
9422 ctx = isl_basic_map_get_ctx(bmap);
9423 v = isl_val_alloc(ctx);
9424 if (!v)
9425 return NULL;
9426 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
9427 if (fixed < 0)
9428 return isl_val_free(v);
9429 if (fixed) {
9430 isl_int_set_si(v->d, 1);
9431 return v;
9433 isl_val_free(v);
9434 return isl_val_nan(ctx);
9437 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
9438 enum isl_dim_type type, unsigned pos, isl_int *val)
9440 if (pos >= isl_map_dim(map, type))
9441 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9442 "position out of bounds", return isl_bool_error);
9443 return isl_map_plain_has_fixed_var(map,
9444 map_offset(map, type) - 1 + pos, val);
9447 /* If "map" obviously lies on a hyperplane where the given dimension
9448 * has a fixed value, then return that value.
9449 * Otherwise return NaN.
9451 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
9452 enum isl_dim_type type, unsigned pos)
9454 isl_ctx *ctx;
9455 isl_val *v;
9456 isl_bool fixed;
9458 if (!map)
9459 return NULL;
9460 ctx = isl_map_get_ctx(map);
9461 v = isl_val_alloc(ctx);
9462 if (!v)
9463 return NULL;
9464 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
9465 if (fixed < 0)
9466 return isl_val_free(v);
9467 if (fixed) {
9468 isl_int_set_si(v->d, 1);
9469 return v;
9471 isl_val_free(v);
9472 return isl_val_nan(ctx);
9475 /* If "set" obviously lies on a hyperplane where the given dimension
9476 * has a fixed value, then return that value.
9477 * Otherwise return NaN.
9479 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
9480 enum isl_dim_type type, unsigned pos)
9482 return isl_map_plain_get_val_if_fixed(set, type, pos);
9485 /* Check if dimension dim has fixed value and if so and if val is not NULL,
9486 * then return this fixed value in *val.
9488 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
9489 unsigned dim, isl_int *val)
9491 return isl_basic_set_plain_has_fixed_var(bset,
9492 isl_basic_set_n_param(bset) + dim, val);
9495 /* Return -1 if the constraint "c1" should be sorted before "c2"
9496 * and 1 if it should be sorted after "c2".
9497 * Return 0 if the two constraints are the same (up to the constant term).
9499 * In particular, if a constraint involves later variables than another
9500 * then it is sorted after this other constraint.
9501 * uset_gist depends on constraints without existentially quantified
9502 * variables sorting first.
9504 * For constraints that have the same latest variable, those
9505 * with the same coefficient for this latest variable (first in absolute value
9506 * and then in actual value) are grouped together.
9507 * This is useful for detecting pairs of constraints that can
9508 * be chained in their printed representation.
9510 * Finally, within a group, constraints are sorted according to
9511 * their coefficients (excluding the constant term).
9513 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9515 isl_int **c1 = (isl_int **) p1;
9516 isl_int **c2 = (isl_int **) p2;
9517 int l1, l2;
9518 unsigned size = *(unsigned *) arg;
9519 int cmp;
9521 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9522 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9524 if (l1 != l2)
9525 return l1 - l2;
9527 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9528 if (cmp != 0)
9529 return cmp;
9530 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9531 if (cmp != 0)
9532 return -cmp;
9534 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9537 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9538 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9539 * and 0 if the two constraints are the same (up to the constant term).
9541 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9542 isl_int *c1, isl_int *c2)
9544 unsigned total;
9546 if (!bmap)
9547 return -2;
9548 total = isl_basic_map_total_dim(bmap);
9549 return sort_constraint_cmp(&c1, &c2, &total);
9552 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9553 __isl_take isl_basic_map *bmap)
9555 unsigned total;
9557 if (!bmap)
9558 return NULL;
9559 if (bmap->n_ineq == 0)
9560 return bmap;
9561 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_SORTED))
9562 return bmap;
9563 total = isl_basic_map_total_dim(bmap);
9564 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9565 &sort_constraint_cmp, &total) < 0)
9566 return isl_basic_map_free(bmap);
9567 ISL_F_SET(bmap, ISL_BASIC_MAP_SORTED);
9568 return bmap;
9571 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9572 __isl_take isl_basic_set *bset)
9574 isl_basic_map *bmap = bset_to_bmap(bset);
9575 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
9578 __isl_give isl_basic_map *isl_basic_map_normalize(
9579 __isl_take isl_basic_map *bmap)
9581 bmap = isl_basic_map_remove_redundancies(bmap);
9582 bmap = isl_basic_map_sort_constraints(bmap);
9583 return bmap;
9585 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9586 __isl_keep isl_basic_map *bmap2)
9588 int i, cmp;
9589 unsigned total;
9590 isl_space *space1, *space2;
9592 if (!bmap1 || !bmap2)
9593 return -1;
9595 if (bmap1 == bmap2)
9596 return 0;
9597 space1 = isl_basic_map_peek_space(bmap1);
9598 space2 = isl_basic_map_peek_space(bmap2);
9599 cmp = isl_space_cmp(space1, space2);
9600 if (cmp)
9601 return cmp;
9602 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9603 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9604 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9605 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9606 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9607 return 0;
9608 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9609 return 1;
9610 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9611 return -1;
9612 if (bmap1->n_eq != bmap2->n_eq)
9613 return bmap1->n_eq - bmap2->n_eq;
9614 if (bmap1->n_ineq != bmap2->n_ineq)
9615 return bmap1->n_ineq - bmap2->n_ineq;
9616 if (bmap1->n_div != bmap2->n_div)
9617 return bmap1->n_div - bmap2->n_div;
9618 total = isl_basic_map_total_dim(bmap1);
9619 for (i = 0; i < bmap1->n_eq; ++i) {
9620 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9621 if (cmp)
9622 return cmp;
9624 for (i = 0; i < bmap1->n_ineq; ++i) {
9625 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9626 if (cmp)
9627 return cmp;
9629 for (i = 0; i < bmap1->n_div; ++i) {
9630 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9631 if (cmp)
9632 return cmp;
9634 return 0;
9637 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9638 __isl_keep isl_basic_set *bset2)
9640 return isl_basic_map_plain_cmp(bset1, bset2);
9643 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9645 int i, cmp;
9647 if (set1 == set2)
9648 return 0;
9649 if (set1->n != set2->n)
9650 return set1->n - set2->n;
9652 for (i = 0; i < set1->n; ++i) {
9653 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9654 if (cmp)
9655 return cmp;
9658 return 0;
9661 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9662 __isl_keep isl_basic_map *bmap2)
9664 if (!bmap1 || !bmap2)
9665 return isl_bool_error;
9666 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9669 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9670 __isl_keep isl_basic_set *bset2)
9672 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9673 bset_to_bmap(bset2));
9676 static int qsort_bmap_cmp(const void *p1, const void *p2)
9678 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9679 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9681 return isl_basic_map_plain_cmp(bmap1, bmap2);
9684 /* Sort the basic maps of "map" and remove duplicate basic maps.
9686 * While removing basic maps, we make sure that the basic maps remain
9687 * sorted because isl_map_normalize expects the basic maps of the result
9688 * to be sorted.
9690 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9692 int i, j;
9694 map = isl_map_remove_empty_parts(map);
9695 if (!map)
9696 return NULL;
9697 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9698 for (i = map->n - 1; i >= 1; --i) {
9699 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9700 continue;
9701 isl_basic_map_free(map->p[i-1]);
9702 for (j = i; j < map->n; ++j)
9703 map->p[j - 1] = map->p[j];
9704 map->n--;
9707 return map;
9710 /* Remove obvious duplicates among the basic maps of "map".
9712 * Unlike isl_map_normalize, this function does not remove redundant
9713 * constraints and only removes duplicates that have exactly the same
9714 * constraints in the input. It does sort the constraints and
9715 * the basic maps to ease the detection of duplicates.
9717 * If "map" has already been normalized or if the basic maps are
9718 * disjoint, then there can be no duplicates.
9720 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9722 int i;
9723 isl_basic_map *bmap;
9725 if (!map)
9726 return NULL;
9727 if (map->n <= 1)
9728 return map;
9729 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9730 return map;
9731 for (i = 0; i < map->n; ++i) {
9732 bmap = isl_basic_map_copy(map->p[i]);
9733 bmap = isl_basic_map_sort_constraints(bmap);
9734 if (!bmap)
9735 return isl_map_free(map);
9736 isl_basic_map_free(map->p[i]);
9737 map->p[i] = bmap;
9740 map = sort_and_remove_duplicates(map);
9741 return map;
9744 /* We normalize in place, but if anything goes wrong we need
9745 * to return NULL, so we need to make sure we don't change the
9746 * meaning of any possible other copies of map.
9748 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9750 int i;
9751 struct isl_basic_map *bmap;
9753 if (!map)
9754 return NULL;
9755 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9756 return map;
9757 for (i = 0; i < map->n; ++i) {
9758 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9759 if (!bmap)
9760 goto error;
9761 isl_basic_map_free(map->p[i]);
9762 map->p[i] = bmap;
9765 map = sort_and_remove_duplicates(map);
9766 if (map)
9767 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9768 return map;
9769 error:
9770 isl_map_free(map);
9771 return NULL;
9774 struct isl_set *isl_set_normalize(struct isl_set *set)
9776 return set_from_map(isl_map_normalize(set_to_map(set)));
9779 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9780 __isl_keep isl_map *map2)
9782 int i;
9783 isl_bool equal;
9785 if (!map1 || !map2)
9786 return isl_bool_error;
9788 if (map1 == map2)
9789 return isl_bool_true;
9790 if (!isl_space_is_equal(map1->dim, map2->dim))
9791 return isl_bool_false;
9793 map1 = isl_map_copy(map1);
9794 map2 = isl_map_copy(map2);
9795 map1 = isl_map_normalize(map1);
9796 map2 = isl_map_normalize(map2);
9797 if (!map1 || !map2)
9798 goto error;
9799 equal = map1->n == map2->n;
9800 for (i = 0; equal && i < map1->n; ++i) {
9801 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9802 if (equal < 0)
9803 goto error;
9805 isl_map_free(map1);
9806 isl_map_free(map2);
9807 return equal;
9808 error:
9809 isl_map_free(map1);
9810 isl_map_free(map2);
9811 return isl_bool_error;
9814 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9815 __isl_keep isl_set *set2)
9817 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9820 /* Return the basic maps in "map" as a list.
9822 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9823 __isl_keep isl_map *map)
9825 int i;
9826 isl_ctx *ctx;
9827 isl_basic_map_list *list;
9829 if (!map)
9830 return NULL;
9831 ctx = isl_map_get_ctx(map);
9832 list = isl_basic_map_list_alloc(ctx, map->n);
9834 for (i = 0; i < map->n; ++i) {
9835 isl_basic_map *bmap;
9837 bmap = isl_basic_map_copy(map->p[i]);
9838 list = isl_basic_map_list_add(list, bmap);
9841 return list;
9844 /* Return the intersection of the elements in the non-empty list "list".
9845 * All elements are assumed to live in the same space.
9847 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9848 __isl_take isl_basic_map_list *list)
9850 int i, n;
9851 isl_basic_map *bmap;
9853 if (!list)
9854 return NULL;
9855 n = isl_basic_map_list_n_basic_map(list);
9856 if (n < 1)
9857 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9858 "expecting non-empty list", goto error);
9860 bmap = isl_basic_map_list_get_basic_map(list, 0);
9861 for (i = 1; i < n; ++i) {
9862 isl_basic_map *bmap_i;
9864 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9865 bmap = isl_basic_map_intersect(bmap, bmap_i);
9868 isl_basic_map_list_free(list);
9869 return bmap;
9870 error:
9871 isl_basic_map_list_free(list);
9872 return NULL;
9875 /* Return the intersection of the elements in the non-empty list "list".
9876 * All elements are assumed to live in the same space.
9878 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9879 __isl_take isl_basic_set_list *list)
9881 return isl_basic_map_list_intersect(list);
9884 /* Return the union of the elements of "list".
9885 * The list is required to have at least one element.
9887 __isl_give isl_set *isl_basic_set_list_union(
9888 __isl_take isl_basic_set_list *list)
9890 int i, n;
9891 isl_space *space;
9892 isl_basic_set *bset;
9893 isl_set *set;
9895 if (!list)
9896 return NULL;
9897 n = isl_basic_set_list_n_basic_set(list);
9898 if (n < 1)
9899 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9900 "expecting non-empty list", goto error);
9902 bset = isl_basic_set_list_get_basic_set(list, 0);
9903 space = isl_basic_set_get_space(bset);
9904 isl_basic_set_free(bset);
9906 set = isl_set_alloc_space(space, n, 0);
9907 for (i = 0; i < n; ++i) {
9908 bset = isl_basic_set_list_get_basic_set(list, i);
9909 set = isl_set_add_basic_set(set, bset);
9912 isl_basic_set_list_free(list);
9913 return set;
9914 error:
9915 isl_basic_set_list_free(list);
9916 return NULL;
9919 /* Return the union of the elements in the non-empty list "list".
9920 * All elements are assumed to live in the same space.
9922 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9924 int i, n;
9925 isl_set *set;
9927 if (!list)
9928 return NULL;
9929 n = isl_set_list_n_set(list);
9930 if (n < 1)
9931 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9932 "expecting non-empty list", goto error);
9934 set = isl_set_list_get_set(list, 0);
9935 for (i = 1; i < n; ++i) {
9936 isl_set *set_i;
9938 set_i = isl_set_list_get_set(list, i);
9939 set = isl_set_union(set, set_i);
9942 isl_set_list_free(list);
9943 return set;
9944 error:
9945 isl_set_list_free(list);
9946 return NULL;
9949 __isl_give isl_basic_map *isl_basic_map_product(
9950 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9952 isl_space *space_result = NULL;
9953 struct isl_basic_map *bmap;
9954 unsigned in1, in2, out1, out2, nparam, total, pos;
9955 struct isl_dim_map *dim_map1, *dim_map2;
9957 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
9958 goto error;
9959 space_result = isl_space_product(isl_space_copy(bmap1->dim),
9960 isl_space_copy(bmap2->dim));
9962 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9963 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9964 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
9965 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
9966 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9968 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9969 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9970 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9971 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9972 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9973 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9974 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9975 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9976 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9977 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9978 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9980 bmap = isl_basic_map_alloc_space(space_result,
9981 bmap1->n_div + bmap2->n_div,
9982 bmap1->n_eq + bmap2->n_eq,
9983 bmap1->n_ineq + bmap2->n_ineq);
9984 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9985 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9986 bmap = isl_basic_map_simplify(bmap);
9987 return isl_basic_map_finalize(bmap);
9988 error:
9989 isl_basic_map_free(bmap1);
9990 isl_basic_map_free(bmap2);
9991 return NULL;
9994 __isl_give isl_basic_map *isl_basic_map_flat_product(
9995 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9997 isl_basic_map *prod;
9999 prod = isl_basic_map_product(bmap1, bmap2);
10000 prod = isl_basic_map_flatten(prod);
10001 return prod;
10004 __isl_give isl_basic_set *isl_basic_set_flat_product(
10005 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
10007 return isl_basic_map_flat_range_product(bset1, bset2);
10010 __isl_give isl_basic_map *isl_basic_map_domain_product(
10011 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10013 isl_space *space_result = NULL;
10014 isl_basic_map *bmap;
10015 unsigned in1, in2, out, nparam, total, pos;
10016 struct isl_dim_map *dim_map1, *dim_map2;
10018 if (!bmap1 || !bmap2)
10019 goto error;
10021 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
10022 isl_space_copy(bmap2->dim));
10024 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
10025 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
10026 out = isl_basic_map_dim(bmap1, isl_dim_out);
10027 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10029 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
10030 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10031 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10032 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10033 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10034 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10035 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
10036 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
10037 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
10038 isl_dim_map_div(dim_map1, bmap1, pos += out);
10039 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10041 bmap = isl_basic_map_alloc_space(space_result,
10042 bmap1->n_div + bmap2->n_div,
10043 bmap1->n_eq + bmap2->n_eq,
10044 bmap1->n_ineq + bmap2->n_ineq);
10045 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10046 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10047 bmap = isl_basic_map_simplify(bmap);
10048 return isl_basic_map_finalize(bmap);
10049 error:
10050 isl_basic_map_free(bmap1);
10051 isl_basic_map_free(bmap2);
10052 return NULL;
10055 __isl_give isl_basic_map *isl_basic_map_range_product(
10056 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10058 isl_bool rational;
10059 isl_space *space_result = NULL;
10060 isl_basic_map *bmap;
10061 unsigned in, out1, out2, nparam, total, pos;
10062 struct isl_dim_map *dim_map1, *dim_map2;
10064 rational = isl_basic_map_is_rational(bmap1);
10065 if (rational >= 0 && rational)
10066 rational = isl_basic_map_is_rational(bmap2);
10067 if (!bmap1 || !bmap2 || rational < 0)
10068 goto error;
10070 if (isl_basic_map_check_equal_params(bmap1, bmap2) < 0)
10071 goto error;
10073 space_result = isl_space_range_product(isl_space_copy(bmap1->dim),
10074 isl_space_copy(bmap2->dim));
10076 in = isl_basic_map_dim(bmap1, isl_dim_in);
10077 out1 = isl_basic_map_dim(bmap1, isl_dim_out);
10078 out2 = isl_basic_map_dim(bmap2, isl_dim_out);
10079 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
10081 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
10082 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
10083 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
10084 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
10085 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
10086 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
10087 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
10088 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
10089 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
10090 isl_dim_map_div(dim_map1, bmap1, pos += out2);
10091 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
10093 bmap = isl_basic_map_alloc_space(space_result,
10094 bmap1->n_div + bmap2->n_div,
10095 bmap1->n_eq + bmap2->n_eq,
10096 bmap1->n_ineq + bmap2->n_ineq);
10097 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
10098 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
10099 if (rational)
10100 bmap = isl_basic_map_set_rational(bmap);
10101 bmap = isl_basic_map_simplify(bmap);
10102 return isl_basic_map_finalize(bmap);
10103 error:
10104 isl_basic_map_free(bmap1);
10105 isl_basic_map_free(bmap2);
10106 return NULL;
10109 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
10110 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
10112 isl_basic_map *prod;
10114 prod = isl_basic_map_range_product(bmap1, bmap2);
10115 prod = isl_basic_map_flatten_range(prod);
10116 return prod;
10119 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
10120 * and collect the results.
10121 * The result live in the space obtained by calling "space_product"
10122 * on the spaces of "map1" and "map2".
10123 * If "remove_duplicates" is set then the result may contain duplicates
10124 * (even if the inputs do not) and so we try and remove the obvious
10125 * duplicates.
10127 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
10128 __isl_take isl_map *map2,
10129 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
10130 __isl_take isl_space *right),
10131 __isl_give isl_basic_map *(*basic_map_product)(
10132 __isl_take isl_basic_map *left,
10133 __isl_take isl_basic_map *right),
10134 int remove_duplicates)
10136 unsigned flags = 0;
10137 struct isl_map *result;
10138 int i, j;
10139 isl_bool m;
10141 m = isl_map_has_equal_params(map1, map2);
10142 if (m < 0)
10143 goto error;
10144 if (!m)
10145 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
10146 "parameters don't match", goto error);
10148 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
10149 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
10150 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
10152 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
10153 isl_space_copy(map2->dim)),
10154 map1->n * map2->n, flags);
10155 if (!result)
10156 goto error;
10157 for (i = 0; i < map1->n; ++i)
10158 for (j = 0; j < map2->n; ++j) {
10159 struct isl_basic_map *part;
10160 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
10161 isl_basic_map_copy(map2->p[j]));
10162 if (isl_basic_map_is_empty(part))
10163 isl_basic_map_free(part);
10164 else
10165 result = isl_map_add_basic_map(result, part);
10166 if (!result)
10167 goto error;
10169 if (remove_duplicates)
10170 result = isl_map_remove_obvious_duplicates(result);
10171 isl_map_free(map1);
10172 isl_map_free(map2);
10173 return result;
10174 error:
10175 isl_map_free(map1);
10176 isl_map_free(map2);
10177 return NULL;
10180 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
10182 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
10183 __isl_take isl_map *map2)
10185 return map_product(map1, map2, &isl_space_product,
10186 &isl_basic_map_product, 0);
10189 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
10190 __isl_take isl_map *map2)
10192 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
10195 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
10197 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
10198 __isl_take isl_map *map2)
10200 isl_map *prod;
10202 prod = isl_map_product(map1, map2);
10203 prod = isl_map_flatten(prod);
10204 return prod;
10207 /* Given two set A and B, construct its Cartesian product A x B.
10209 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
10211 return isl_map_range_product(set1, set2);
10214 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
10215 __isl_take isl_set *set2)
10217 return isl_map_flat_range_product(set1, set2);
10220 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
10222 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
10223 __isl_take isl_map *map2)
10225 return map_product(map1, map2, &isl_space_domain_product,
10226 &isl_basic_map_domain_product, 1);
10229 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
10231 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
10232 __isl_take isl_map *map2)
10234 return map_product(map1, map2, &isl_space_range_product,
10235 &isl_basic_map_range_product, 1);
10238 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
10239 __isl_take isl_map *map2)
10241 return isl_map_align_params_map_map_and(map1, map2,
10242 &map_domain_product_aligned);
10245 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
10246 __isl_take isl_map *map2)
10248 return isl_map_align_params_map_map_and(map1, map2,
10249 &map_range_product_aligned);
10252 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
10254 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
10256 isl_space *space;
10257 int total1, keep1, total2, keep2;
10259 if (!map)
10260 return NULL;
10261 if (!isl_space_domain_is_wrapping(map->dim) ||
10262 !isl_space_range_is_wrapping(map->dim))
10263 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10264 "not a product", return isl_map_free(map));
10266 space = isl_map_get_space(map);
10267 total1 = isl_space_dim(space, isl_dim_in);
10268 total2 = isl_space_dim(space, isl_dim_out);
10269 space = isl_space_factor_domain(space);
10270 keep1 = isl_space_dim(space, isl_dim_in);
10271 keep2 = isl_space_dim(space, isl_dim_out);
10272 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
10273 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
10274 map = isl_map_reset_space(map, space);
10276 return map;
10279 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
10281 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
10283 isl_space *space;
10284 int total1, keep1, total2, keep2;
10286 if (!map)
10287 return NULL;
10288 if (!isl_space_domain_is_wrapping(map->dim) ||
10289 !isl_space_range_is_wrapping(map->dim))
10290 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10291 "not a product", return isl_map_free(map));
10293 space = isl_map_get_space(map);
10294 total1 = isl_space_dim(space, isl_dim_in);
10295 total2 = isl_space_dim(space, isl_dim_out);
10296 space = isl_space_factor_range(space);
10297 keep1 = isl_space_dim(space, isl_dim_in);
10298 keep2 = isl_space_dim(space, isl_dim_out);
10299 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
10300 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
10301 map = isl_map_reset_space(map, space);
10303 return map;
10306 /* Given a map of the form [A -> B] -> C, return the map A -> C.
10308 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
10310 isl_space *space;
10311 int total, keep;
10313 if (!map)
10314 return NULL;
10315 if (!isl_space_domain_is_wrapping(map->dim))
10316 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10317 "domain is not a product", return isl_map_free(map));
10319 space = isl_map_get_space(map);
10320 total = isl_space_dim(space, isl_dim_in);
10321 space = isl_space_domain_factor_domain(space);
10322 keep = isl_space_dim(space, isl_dim_in);
10323 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
10324 map = isl_map_reset_space(map, space);
10326 return map;
10329 /* Given a map of the form [A -> B] -> C, return the map B -> C.
10331 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
10333 isl_space *space;
10334 int total, keep;
10336 if (!map)
10337 return NULL;
10338 if (!isl_space_domain_is_wrapping(map->dim))
10339 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10340 "domain is not a product", return isl_map_free(map));
10342 space = isl_map_get_space(map);
10343 total = isl_space_dim(space, isl_dim_in);
10344 space = isl_space_domain_factor_range(space);
10345 keep = isl_space_dim(space, isl_dim_in);
10346 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
10347 map = isl_map_reset_space(map, space);
10349 return map;
10352 /* Given a map A -> [B -> C], extract the map A -> B.
10354 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
10356 isl_space *space;
10357 int total, keep;
10359 if (!map)
10360 return NULL;
10361 if (!isl_space_range_is_wrapping(map->dim))
10362 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10363 "range is not a product", return isl_map_free(map));
10365 space = isl_map_get_space(map);
10366 total = isl_space_dim(space, isl_dim_out);
10367 space = isl_space_range_factor_domain(space);
10368 keep = isl_space_dim(space, isl_dim_out);
10369 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
10370 map = isl_map_reset_space(map, space);
10372 return map;
10375 /* Given a map A -> [B -> C], extract the map A -> C.
10377 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
10379 isl_space *space;
10380 int total, keep;
10382 if (!map)
10383 return NULL;
10384 if (!isl_space_range_is_wrapping(map->dim))
10385 isl_die(isl_map_get_ctx(map), isl_error_invalid,
10386 "range is not a product", return isl_map_free(map));
10388 space = isl_map_get_space(map);
10389 total = isl_space_dim(space, isl_dim_out);
10390 space = isl_space_range_factor_range(space);
10391 keep = isl_space_dim(space, isl_dim_out);
10392 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
10393 map = isl_map_reset_space(map, space);
10395 return map;
10398 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
10400 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
10401 __isl_take isl_map *map2)
10403 isl_map *prod;
10405 prod = isl_map_domain_product(map1, map2);
10406 prod = isl_map_flatten_domain(prod);
10407 return prod;
10410 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
10412 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
10413 __isl_take isl_map *map2)
10415 isl_map *prod;
10417 prod = isl_map_range_product(map1, map2);
10418 prod = isl_map_flatten_range(prod);
10419 return prod;
10422 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
10424 int i;
10425 uint32_t hash = isl_hash_init();
10426 unsigned total;
10428 if (!bmap)
10429 return 0;
10430 bmap = isl_basic_map_copy(bmap);
10431 bmap = isl_basic_map_normalize(bmap);
10432 if (!bmap)
10433 return 0;
10434 total = isl_basic_map_total_dim(bmap);
10435 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10436 for (i = 0; i < bmap->n_eq; ++i) {
10437 uint32_t c_hash;
10438 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10439 isl_hash_hash(hash, c_hash);
10441 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10442 for (i = 0; i < bmap->n_ineq; ++i) {
10443 uint32_t c_hash;
10444 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10445 isl_hash_hash(hash, c_hash);
10447 isl_hash_byte(hash, bmap->n_div & 0xFF);
10448 for (i = 0; i < bmap->n_div; ++i) {
10449 uint32_t c_hash;
10450 if (isl_int_is_zero(bmap->div[i][0]))
10451 continue;
10452 isl_hash_byte(hash, i & 0xFF);
10453 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10454 isl_hash_hash(hash, c_hash);
10456 isl_basic_map_free(bmap);
10457 return hash;
10460 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10462 return isl_basic_map_get_hash(bset_to_bmap(bset));
10465 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10467 int i;
10468 uint32_t hash;
10470 if (!map)
10471 return 0;
10472 map = isl_map_copy(map);
10473 map = isl_map_normalize(map);
10474 if (!map)
10475 return 0;
10477 hash = isl_hash_init();
10478 for (i = 0; i < map->n; ++i) {
10479 uint32_t bmap_hash;
10480 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10481 isl_hash_hash(hash, bmap_hash);
10484 isl_map_free(map);
10486 return hash;
10489 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10491 return isl_map_get_hash(set_to_map(set));
10494 /* Return the number of basic maps in the (current) representation of "map".
10496 int isl_map_n_basic_map(__isl_keep isl_map *map)
10498 return map ? map->n : 0;
10501 int isl_set_n_basic_set(__isl_keep isl_set *set)
10503 return set ? set->n : 0;
10506 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10507 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10509 int i;
10511 if (!map)
10512 return isl_stat_error;
10514 for (i = 0; i < map->n; ++i)
10515 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10516 return isl_stat_error;
10518 return isl_stat_ok;
10521 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10522 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10524 int i;
10526 if (!set)
10527 return isl_stat_error;
10529 for (i = 0; i < set->n; ++i)
10530 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10531 return isl_stat_error;
10533 return isl_stat_ok;
10536 /* Return a list of basic sets, the union of which is equal to "set".
10538 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10539 __isl_keep isl_set *set)
10541 int i;
10542 isl_basic_set_list *list;
10544 if (!set)
10545 return NULL;
10547 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10548 for (i = 0; i < set->n; ++i) {
10549 isl_basic_set *bset;
10551 bset = isl_basic_set_copy(set->p[i]);
10552 list = isl_basic_set_list_add(list, bset);
10555 return list;
10558 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10560 isl_space *space;
10562 if (!bset)
10563 return NULL;
10565 bset = isl_basic_set_cow(bset);
10566 if (!bset)
10567 return NULL;
10569 space = isl_basic_set_get_space(bset);
10570 space = isl_space_lift(space, bset->n_div);
10571 if (!space)
10572 goto error;
10573 isl_space_free(bset->dim);
10574 bset->dim = space;
10575 bset->extra -= bset->n_div;
10576 bset->n_div = 0;
10578 bset = isl_basic_set_finalize(bset);
10580 return bset;
10581 error:
10582 isl_basic_set_free(bset);
10583 return NULL;
10586 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10588 int i;
10589 isl_space *space;
10590 unsigned n_div;
10592 set = set_from_map(isl_map_align_divs_internal(set_to_map(set)));
10594 if (!set)
10595 return NULL;
10597 set = isl_set_cow(set);
10598 if (!set)
10599 return NULL;
10601 n_div = set->p[0]->n_div;
10602 space = isl_set_get_space(set);
10603 space = isl_space_lift(space, n_div);
10604 if (!space)
10605 goto error;
10606 isl_space_free(set->dim);
10607 set->dim = space;
10609 for (i = 0; i < set->n; ++i) {
10610 set->p[i] = isl_basic_set_lift(set->p[i]);
10611 if (!set->p[i])
10612 goto error;
10615 return set;
10616 error:
10617 isl_set_free(set);
10618 return NULL;
10621 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10623 unsigned dim;
10624 int size = 0;
10626 if (!bset)
10627 return -1;
10629 dim = isl_basic_set_total_dim(bset);
10630 size += bset->n_eq * (1 + dim);
10631 size += bset->n_ineq * (1 + dim);
10632 size += bset->n_div * (2 + dim);
10634 return size;
10637 int isl_set_size(__isl_keep isl_set *set)
10639 int i;
10640 int size = 0;
10642 if (!set)
10643 return -1;
10645 for (i = 0; i < set->n; ++i)
10646 size += isl_basic_set_size(set->p[i]);
10648 return size;
10651 /* Check if there is any lower bound (if lower == 0) and/or upper
10652 * bound (if upper == 0) on the specified dim.
10654 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10655 enum isl_dim_type type, unsigned pos, int lower, int upper)
10657 int i;
10659 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10660 return isl_bool_error;
10662 pos += isl_basic_map_offset(bmap, type);
10664 for (i = 0; i < bmap->n_div; ++i) {
10665 if (isl_int_is_zero(bmap->div[i][0]))
10666 continue;
10667 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10668 return isl_bool_true;
10671 for (i = 0; i < bmap->n_eq; ++i)
10672 if (!isl_int_is_zero(bmap->eq[i][pos]))
10673 return isl_bool_true;
10675 for (i = 0; i < bmap->n_ineq; ++i) {
10676 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10677 if (sgn > 0)
10678 lower = 1;
10679 if (sgn < 0)
10680 upper = 1;
10683 return lower && upper;
10686 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10687 enum isl_dim_type type, unsigned pos)
10689 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10692 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10693 enum isl_dim_type type, unsigned pos)
10695 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10698 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10699 enum isl_dim_type type, unsigned pos)
10701 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10704 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10705 enum isl_dim_type type, unsigned pos)
10707 int i;
10709 if (!map)
10710 return isl_bool_error;
10712 for (i = 0; i < map->n; ++i) {
10713 isl_bool bounded;
10714 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10715 if (bounded < 0 || !bounded)
10716 return bounded;
10719 return isl_bool_true;
10722 /* Return true if the specified dim is involved in both an upper bound
10723 * and a lower bound.
10725 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10726 enum isl_dim_type type, unsigned pos)
10728 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10731 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10733 static isl_bool has_any_bound(__isl_keep isl_map *map,
10734 enum isl_dim_type type, unsigned pos,
10735 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10736 enum isl_dim_type type, unsigned pos))
10738 int i;
10740 if (!map)
10741 return isl_bool_error;
10743 for (i = 0; i < map->n; ++i) {
10744 isl_bool bounded;
10745 bounded = fn(map->p[i], type, pos);
10746 if (bounded < 0 || bounded)
10747 return bounded;
10750 return isl_bool_false;
10753 /* Return 1 if the specified dim is involved in any lower bound.
10755 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10756 enum isl_dim_type type, unsigned pos)
10758 return has_any_bound(set, type, pos,
10759 &isl_basic_map_dim_has_lower_bound);
10762 /* Return 1 if the specified dim is involved in any upper bound.
10764 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10765 enum isl_dim_type type, unsigned pos)
10767 return has_any_bound(set, type, pos,
10768 &isl_basic_map_dim_has_upper_bound);
10771 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10773 static isl_bool has_bound(__isl_keep isl_map *map,
10774 enum isl_dim_type type, unsigned pos,
10775 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10776 enum isl_dim_type type, unsigned pos))
10778 int i;
10780 if (!map)
10781 return isl_bool_error;
10783 for (i = 0; i < map->n; ++i) {
10784 isl_bool bounded;
10785 bounded = fn(map->p[i], type, pos);
10786 if (bounded < 0 || !bounded)
10787 return bounded;
10790 return isl_bool_true;
10793 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10795 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10796 enum isl_dim_type type, unsigned pos)
10798 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10801 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10803 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10804 enum isl_dim_type type, unsigned pos)
10806 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10809 /* For each of the "n" variables starting at "first", determine
10810 * the sign of the variable and put the results in the first "n"
10811 * elements of the array "signs".
10812 * Sign
10813 * 1 means that the variable is non-negative
10814 * -1 means that the variable is non-positive
10815 * 0 means the variable attains both positive and negative values.
10817 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10818 unsigned first, unsigned n, int *signs)
10820 isl_vec *bound = NULL;
10821 struct isl_tab *tab = NULL;
10822 struct isl_tab_undo *snap;
10823 int i;
10825 if (!bset || !signs)
10826 return isl_stat_error;
10828 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10829 tab = isl_tab_from_basic_set(bset, 0);
10830 if (!bound || !tab)
10831 goto error;
10833 isl_seq_clr(bound->el, bound->size);
10834 isl_int_set_si(bound->el[0], -1);
10836 snap = isl_tab_snap(tab);
10837 for (i = 0; i < n; ++i) {
10838 int empty;
10840 isl_int_set_si(bound->el[1 + first + i], -1);
10841 if (isl_tab_add_ineq(tab, bound->el) < 0)
10842 goto error;
10843 empty = tab->empty;
10844 isl_int_set_si(bound->el[1 + first + i], 0);
10845 if (isl_tab_rollback(tab, snap) < 0)
10846 goto error;
10848 if (empty) {
10849 signs[i] = 1;
10850 continue;
10853 isl_int_set_si(bound->el[1 + first + i], 1);
10854 if (isl_tab_add_ineq(tab, bound->el) < 0)
10855 goto error;
10856 empty = tab->empty;
10857 isl_int_set_si(bound->el[1 + first + i], 0);
10858 if (isl_tab_rollback(tab, snap) < 0)
10859 goto error;
10861 signs[i] = empty ? -1 : 0;
10864 isl_tab_free(tab);
10865 isl_vec_free(bound);
10866 return isl_stat_ok;
10867 error:
10868 isl_tab_free(tab);
10869 isl_vec_free(bound);
10870 return isl_stat_error;
10873 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10874 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10876 if (!bset || !signs)
10877 return isl_stat_error;
10878 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10879 return isl_stat_error);
10881 first += pos(bset->dim, type) - 1;
10882 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10885 /* Is it possible for the integer division "div" to depend (possibly
10886 * indirectly) on any output dimensions?
10888 * If the div is undefined, then we conservatively assume that it
10889 * may depend on them.
10890 * Otherwise, we check if it actually depends on them or on any integer
10891 * divisions that may depend on them.
10893 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10895 int i;
10896 unsigned n_out, o_out;
10897 unsigned n_div, o_div;
10899 if (isl_int_is_zero(bmap->div[div][0]))
10900 return isl_bool_true;
10902 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10903 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10905 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10906 return isl_bool_true;
10908 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10909 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10911 for (i = 0; i < n_div; ++i) {
10912 isl_bool may_involve;
10914 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10915 continue;
10916 may_involve = div_may_involve_output(bmap, i);
10917 if (may_involve < 0 || may_involve)
10918 return may_involve;
10921 return isl_bool_false;
10924 /* Return the first integer division of "bmap" in the range
10925 * [first, first + n[ that may depend on any output dimensions and
10926 * that has a non-zero coefficient in "c" (where the first coefficient
10927 * in "c" corresponds to integer division "first").
10929 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10930 isl_int *c, int first, int n)
10932 int k;
10934 if (!bmap)
10935 return -1;
10937 for (k = first; k < first + n; ++k) {
10938 isl_bool may_involve;
10940 if (isl_int_is_zero(c[k]))
10941 continue;
10942 may_involve = div_may_involve_output(bmap, k);
10943 if (may_involve < 0)
10944 return -1;
10945 if (may_involve)
10946 return k;
10949 return first + n;
10952 /* Look for a pair of inequality constraints in "bmap" of the form
10954 * -l + i >= 0 or i >= l
10955 * and
10956 * n + l - i >= 0 or i <= l + n
10958 * with n < "m" and i the output dimension at position "pos".
10959 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10960 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10961 * and earlier output dimensions, as well as integer divisions that do
10962 * not involve any of the output dimensions.
10964 * Return the index of the first inequality constraint or bmap->n_ineq
10965 * if no such pair can be found.
10967 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10968 int pos, isl_int m)
10970 int i, j;
10971 isl_ctx *ctx;
10972 unsigned total;
10973 unsigned n_div, o_div;
10974 unsigned n_out, o_out;
10975 int less;
10977 if (!bmap)
10978 return -1;
10980 ctx = isl_basic_map_get_ctx(bmap);
10981 total = isl_basic_map_total_dim(bmap);
10982 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10983 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10984 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10985 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10986 for (i = 0; i < bmap->n_ineq; ++i) {
10987 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10988 continue;
10989 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10990 n_out - (pos + 1)) != -1)
10991 continue;
10992 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10993 0, n_div) < n_div)
10994 continue;
10995 for (j = i + 1; j < bmap->n_ineq; ++j) {
10996 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10997 ctx->one))
10998 continue;
10999 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
11000 bmap->ineq[j] + 1, total))
11001 continue;
11002 break;
11004 if (j >= bmap->n_ineq)
11005 continue;
11006 isl_int_add(bmap->ineq[i][0],
11007 bmap->ineq[i][0], bmap->ineq[j][0]);
11008 less = isl_int_abs_lt(bmap->ineq[i][0], m);
11009 isl_int_sub(bmap->ineq[i][0],
11010 bmap->ineq[i][0], bmap->ineq[j][0]);
11011 if (!less)
11012 continue;
11013 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
11014 return i;
11015 else
11016 return j;
11019 return bmap->n_ineq;
11022 /* Return the index of the equality of "bmap" that defines
11023 * the output dimension "pos" in terms of earlier dimensions.
11024 * The equality may also involve integer divisions, as long
11025 * as those integer divisions are defined in terms of
11026 * parameters or input dimensions.
11027 * In this case, *div is set to the number of integer divisions and
11028 * *ineq is set to the number of inequality constraints (provided
11029 * div and ineq are not NULL).
11031 * The equality may also involve a single integer division involving
11032 * the output dimensions (typically only output dimension "pos") as
11033 * long as the coefficient of output dimension "pos" is 1 or -1 and
11034 * there is a pair of constraints i >= l and i <= l + n, with i referring
11035 * to output dimension "pos", l an expression involving only earlier
11036 * dimensions and n smaller than the coefficient of the integer division
11037 * in the equality. In this case, the output dimension can be defined
11038 * in terms of a modulo expression that does not involve the integer division.
11039 * *div is then set to this single integer division and
11040 * *ineq is set to the index of constraint i >= l.
11042 * Return bmap->n_eq if there is no such equality.
11043 * Return -1 on error.
11045 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
11046 int pos, int *div, int *ineq)
11048 int j, k, l;
11049 unsigned n_out, o_out;
11050 unsigned n_div, o_div;
11052 if (!bmap)
11053 return -1;
11055 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11056 o_out = isl_basic_map_offset(bmap, isl_dim_out);
11057 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11058 o_div = isl_basic_map_offset(bmap, isl_dim_div);
11060 if (ineq)
11061 *ineq = bmap->n_ineq;
11062 if (div)
11063 *div = n_div;
11064 for (j = 0; j < bmap->n_eq; ++j) {
11065 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
11066 continue;
11067 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
11068 n_out - (pos + 1)) != -1)
11069 continue;
11070 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11071 0, n_div);
11072 if (k >= n_div)
11073 return j;
11074 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
11075 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
11076 continue;
11077 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
11078 k + 1, n_div - (k+1)) < n_div)
11079 continue;
11080 l = find_modulo_constraint_pair(bmap, pos,
11081 bmap->eq[j][o_div + k]);
11082 if (l < 0)
11083 return -1;
11084 if (l >= bmap->n_ineq)
11085 continue;
11086 if (div)
11087 *div = k;
11088 if (ineq)
11089 *ineq = l;
11090 return j;
11093 return bmap->n_eq;
11096 /* Check if the given basic map is obviously single-valued.
11097 * In particular, for each output dimension, check that there is
11098 * an equality that defines the output dimension in terms of
11099 * earlier dimensions.
11101 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
11103 int i;
11104 unsigned n_out;
11106 if (!bmap)
11107 return isl_bool_error;
11109 n_out = isl_basic_map_dim(bmap, isl_dim_out);
11111 for (i = 0; i < n_out; ++i) {
11112 int eq;
11114 eq = isl_basic_map_output_defining_equality(bmap, i,
11115 NULL, NULL);
11116 if (eq < 0)
11117 return isl_bool_error;
11118 if (eq >= bmap->n_eq)
11119 return isl_bool_false;
11122 return isl_bool_true;
11125 /* Check if the given basic map is single-valued.
11126 * We simply compute
11128 * M \circ M^-1
11130 * and check if the result is a subset of the identity mapping.
11132 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
11134 isl_space *space;
11135 isl_basic_map *test;
11136 isl_basic_map *id;
11137 isl_bool sv;
11139 sv = isl_basic_map_plain_is_single_valued(bmap);
11140 if (sv < 0 || sv)
11141 return sv;
11143 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
11144 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
11146 space = isl_basic_map_get_space(bmap);
11147 space = isl_space_map_from_set(isl_space_range(space));
11148 id = isl_basic_map_identity(space);
11150 sv = isl_basic_map_is_subset(test, id);
11152 isl_basic_map_free(test);
11153 isl_basic_map_free(id);
11155 return sv;
11158 /* Check if the given map is obviously single-valued.
11160 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
11162 if (!map)
11163 return isl_bool_error;
11164 if (map->n == 0)
11165 return isl_bool_true;
11166 if (map->n >= 2)
11167 return isl_bool_false;
11169 return isl_basic_map_plain_is_single_valued(map->p[0]);
11172 /* Check if the given map is single-valued.
11173 * We simply compute
11175 * M \circ M^-1
11177 * and check if the result is a subset of the identity mapping.
11179 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
11181 isl_space *dim;
11182 isl_map *test;
11183 isl_map *id;
11184 isl_bool sv;
11186 sv = isl_map_plain_is_single_valued(map);
11187 if (sv < 0 || sv)
11188 return sv;
11190 test = isl_map_reverse(isl_map_copy(map));
11191 test = isl_map_apply_range(test, isl_map_copy(map));
11193 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
11194 id = isl_map_identity(dim);
11196 sv = isl_map_is_subset(test, id);
11198 isl_map_free(test);
11199 isl_map_free(id);
11201 return sv;
11204 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
11206 isl_bool in;
11208 map = isl_map_copy(map);
11209 map = isl_map_reverse(map);
11210 in = isl_map_is_single_valued(map);
11211 isl_map_free(map);
11213 return in;
11216 /* Check if the given map is obviously injective.
11218 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
11220 isl_bool in;
11222 map = isl_map_copy(map);
11223 map = isl_map_reverse(map);
11224 in = isl_map_plain_is_single_valued(map);
11225 isl_map_free(map);
11227 return in;
11230 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
11232 isl_bool sv;
11234 sv = isl_map_is_single_valued(map);
11235 if (sv < 0 || !sv)
11236 return sv;
11238 return isl_map_is_injective(map);
11241 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
11243 return isl_map_is_single_valued(set_to_map(set));
11246 /* Does "map" only map elements to themselves?
11248 * If the domain and range spaces are different, then "map"
11249 * is considered not to be an identity relation, even if it is empty.
11250 * Otherwise, construct the maximal identity relation and
11251 * check whether "map" is a subset of this relation.
11253 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
11255 isl_space *space;
11256 isl_map *id;
11257 isl_bool equal, is_identity;
11259 space = isl_map_get_space(map);
11260 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
11261 isl_space_free(space);
11262 if (equal < 0 || !equal)
11263 return equal;
11265 id = isl_map_identity(isl_map_get_space(map));
11266 is_identity = isl_map_is_subset(map, id);
11267 isl_map_free(id);
11269 return is_identity;
11272 int isl_map_is_translation(__isl_keep isl_map *map)
11274 int ok;
11275 isl_set *delta;
11277 delta = isl_map_deltas(isl_map_copy(map));
11278 ok = isl_set_is_singleton(delta);
11279 isl_set_free(delta);
11281 return ok;
11284 static int unique(isl_int *p, unsigned pos, unsigned len)
11286 if (isl_seq_first_non_zero(p, pos) != -1)
11287 return 0;
11288 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
11289 return 0;
11290 return 1;
11293 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
11295 int i, j;
11296 unsigned nvar;
11297 unsigned ovar;
11299 if (!bset)
11300 return isl_bool_error;
11302 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
11303 return isl_bool_false;
11305 nvar = isl_basic_set_dim(bset, isl_dim_set);
11306 ovar = isl_space_offset(bset->dim, isl_dim_set);
11307 for (j = 0; j < nvar; ++j) {
11308 int lower = 0, upper = 0;
11309 for (i = 0; i < bset->n_eq; ++i) {
11310 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
11311 continue;
11312 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
11313 return isl_bool_false;
11314 break;
11316 if (i < bset->n_eq)
11317 continue;
11318 for (i = 0; i < bset->n_ineq; ++i) {
11319 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
11320 continue;
11321 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
11322 return isl_bool_false;
11323 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
11324 lower = 1;
11325 else
11326 upper = 1;
11328 if (!lower || !upper)
11329 return isl_bool_false;
11332 return isl_bool_true;
11335 isl_bool isl_set_is_box(__isl_keep isl_set *set)
11337 if (!set)
11338 return isl_bool_error;
11339 if (set->n != 1)
11340 return isl_bool_false;
11342 return isl_basic_set_is_box(set->p[0]);
11345 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11347 if (!bset)
11348 return isl_bool_error;
11350 return isl_space_is_wrapping(bset->dim);
11353 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11355 if (!set)
11356 return isl_bool_error;
11358 return isl_space_is_wrapping(set->dim);
11361 /* Modify the space of "map" through a call to "change".
11362 * If "can_change" is set (not NULL), then first call it to check
11363 * if the modification is allowed, printing the error message "cannot_change"
11364 * if it is not.
11366 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11367 isl_bool (*can_change)(__isl_keep isl_map *map),
11368 const char *cannot_change,
11369 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11371 isl_bool ok;
11372 isl_space *space;
11374 if (!map)
11375 return NULL;
11377 ok = can_change ? can_change(map) : isl_bool_true;
11378 if (ok < 0)
11379 return isl_map_free(map);
11380 if (!ok)
11381 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11382 return isl_map_free(map));
11384 space = change(isl_map_get_space(map));
11385 map = isl_map_reset_space(map, space);
11387 return map;
11390 /* Is the domain of "map" a wrapped relation?
11392 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11394 if (!map)
11395 return isl_bool_error;
11397 return isl_space_domain_is_wrapping(map->dim);
11400 /* Does "map" have a wrapped relation in both domain and range?
11402 isl_bool isl_map_is_product(__isl_keep isl_map *map)
11404 return isl_space_is_product(isl_map_peek_space(map));
11407 /* Is the range of "map" a wrapped relation?
11409 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11411 if (!map)
11412 return isl_bool_error;
11414 return isl_space_range_is_wrapping(map->dim);
11417 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11419 bmap = isl_basic_map_cow(bmap);
11420 if (!bmap)
11421 return NULL;
11423 bmap->dim = isl_space_wrap(bmap->dim);
11424 if (!bmap->dim)
11425 goto error;
11427 bmap = isl_basic_map_finalize(bmap);
11429 return bset_from_bmap(bmap);
11430 error:
11431 isl_basic_map_free(bmap);
11432 return NULL;
11435 /* Given a map A -> B, return the set (A -> B).
11437 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11439 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11442 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11444 bset = isl_basic_set_cow(bset);
11445 if (!bset)
11446 return NULL;
11448 bset->dim = isl_space_unwrap(bset->dim);
11449 if (!bset->dim)
11450 goto error;
11452 bset = isl_basic_set_finalize(bset);
11454 return bset_to_bmap(bset);
11455 error:
11456 isl_basic_set_free(bset);
11457 return NULL;
11460 /* Given a set (A -> B), return the map A -> B.
11461 * Error out if "set" is not of the form (A -> B).
11463 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11465 return isl_map_change_space(set, &isl_set_is_wrapping,
11466 "not a wrapping set", &isl_space_unwrap);
11469 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11470 enum isl_dim_type type)
11472 if (!bmap)
11473 return NULL;
11475 if (!isl_space_is_named_or_nested(bmap->dim, type))
11476 return bmap;
11478 bmap = isl_basic_map_cow(bmap);
11479 if (!bmap)
11480 return NULL;
11482 bmap->dim = isl_space_reset(bmap->dim, type);
11483 if (!bmap->dim)
11484 goto error;
11486 bmap = isl_basic_map_finalize(bmap);
11488 return bmap;
11489 error:
11490 isl_basic_map_free(bmap);
11491 return NULL;
11494 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11495 enum isl_dim_type type)
11497 int i;
11499 if (!map)
11500 return NULL;
11502 if (!isl_space_is_named_or_nested(map->dim, type))
11503 return map;
11505 map = isl_map_cow(map);
11506 if (!map)
11507 return NULL;
11509 for (i = 0; i < map->n; ++i) {
11510 map->p[i] = isl_basic_map_reset(map->p[i], type);
11511 if (!map->p[i])
11512 goto error;
11514 map->dim = isl_space_reset(map->dim, type);
11515 if (!map->dim)
11516 goto error;
11518 return map;
11519 error:
11520 isl_map_free(map);
11521 return NULL;
11524 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11526 if (!bmap)
11527 return NULL;
11529 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11530 return bmap;
11532 bmap = isl_basic_map_cow(bmap);
11533 if (!bmap)
11534 return NULL;
11536 bmap->dim = isl_space_flatten(bmap->dim);
11537 if (!bmap->dim)
11538 goto error;
11540 bmap = isl_basic_map_finalize(bmap);
11542 return bmap;
11543 error:
11544 isl_basic_map_free(bmap);
11545 return NULL;
11548 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11550 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
11553 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11554 __isl_take isl_basic_map *bmap)
11556 if (!bmap)
11557 return NULL;
11559 if (!bmap->dim->nested[0])
11560 return bmap;
11562 bmap = isl_basic_map_cow(bmap);
11563 if (!bmap)
11564 return NULL;
11566 bmap->dim = isl_space_flatten_domain(bmap->dim);
11567 if (!bmap->dim)
11568 goto error;
11570 bmap = isl_basic_map_finalize(bmap);
11572 return bmap;
11573 error:
11574 isl_basic_map_free(bmap);
11575 return NULL;
11578 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11579 __isl_take isl_basic_map *bmap)
11581 if (!bmap)
11582 return NULL;
11584 if (!bmap->dim->nested[1])
11585 return bmap;
11587 bmap = isl_basic_map_cow(bmap);
11588 if (!bmap)
11589 return NULL;
11591 bmap->dim = isl_space_flatten_range(bmap->dim);
11592 if (!bmap->dim)
11593 goto error;
11595 bmap = isl_basic_map_finalize(bmap);
11597 return bmap;
11598 error:
11599 isl_basic_map_free(bmap);
11600 return NULL;
11603 /* Remove any internal structure from the spaces of domain and range of "map".
11605 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11607 if (!map)
11608 return NULL;
11610 if (!map->dim->nested[0] && !map->dim->nested[1])
11611 return map;
11613 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11616 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11618 return set_from_map(isl_map_flatten(set_to_map(set)));
11621 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11623 isl_space *space, *flat_space;
11624 isl_map *map;
11626 space = isl_set_get_space(set);
11627 flat_space = isl_space_flatten(isl_space_copy(space));
11628 map = isl_map_identity(isl_space_join(isl_space_reverse(space),
11629 flat_space));
11630 map = isl_map_intersect_domain(map, set);
11632 return map;
11635 /* Remove any internal structure from the space of the domain of "map".
11637 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11639 if (!map)
11640 return NULL;
11642 if (!map->dim->nested[0])
11643 return map;
11645 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11648 /* Remove any internal structure from the space of the range of "map".
11650 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11652 if (!map)
11653 return NULL;
11655 if (!map->dim->nested[1])
11656 return map;
11658 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11661 /* Reorder the dimensions of "bmap" according to the given dim_map
11662 * and set the dimension specification to "space" and
11663 * perform Gaussian elimination on the result.
11665 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11666 __isl_take isl_space *space, __isl_take struct isl_dim_map *dim_map)
11668 isl_basic_map *res;
11669 unsigned flags;
11670 unsigned n_div;
11672 if (!bmap || !space || !dim_map)
11673 goto error;
11675 flags = bmap->flags;
11676 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11677 ISL_FL_CLR(flags, ISL_BASIC_MAP_SORTED);
11678 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11679 n_div = isl_basic_map_dim(bmap, isl_dim_div);
11680 res = isl_basic_map_alloc_space(space, n_div, bmap->n_eq, bmap->n_ineq);
11681 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11682 if (res)
11683 res->flags = flags;
11684 res = isl_basic_map_gauss(res, NULL);
11685 res = isl_basic_map_finalize(res);
11686 return res;
11687 error:
11688 free(dim_map);
11689 isl_basic_map_free(bmap);
11690 isl_space_free(space);
11691 return NULL;
11694 /* Reorder the dimensions of "map" according to given reordering.
11696 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11697 __isl_take isl_reordering *r)
11699 int i;
11700 struct isl_dim_map *dim_map;
11702 map = isl_map_cow(map);
11703 dim_map = isl_dim_map_from_reordering(r);
11704 if (!map || !r || !dim_map)
11705 goto error;
11707 for (i = 0; i < map->n; ++i) {
11708 struct isl_dim_map *dim_map_i;
11709 isl_space *space;
11711 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11713 space = isl_reordering_get_space(r);
11714 map->p[i] = isl_basic_map_realign(map->p[i], space, dim_map_i);
11716 if (!map->p[i])
11717 goto error;
11720 map = isl_map_reset_space(map, isl_reordering_get_space(r));
11721 map = isl_map_unmark_normalized(map);
11723 isl_reordering_free(r);
11724 free(dim_map);
11725 return map;
11726 error:
11727 free(dim_map);
11728 isl_map_free(map);
11729 isl_reordering_free(r);
11730 return NULL;
11733 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11734 __isl_take isl_reordering *r)
11736 return set_from_map(isl_map_realign(set_to_map(set), r));
11739 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11740 __isl_take isl_space *model)
11742 isl_ctx *ctx;
11743 isl_bool aligned;
11745 if (!map || !model)
11746 goto error;
11748 ctx = isl_space_get_ctx(model);
11749 if (!isl_space_has_named_params(model))
11750 isl_die(ctx, isl_error_invalid,
11751 "model has unnamed parameters", goto error);
11752 if (isl_map_check_named_params(map) < 0)
11753 goto error;
11754 aligned = isl_map_space_has_equal_params(map, model);
11755 if (aligned < 0)
11756 goto error;
11757 if (!aligned) {
11758 isl_reordering *exp;
11760 exp = isl_parameter_alignment_reordering(map->dim, model);
11761 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11762 map = isl_map_realign(map, exp);
11765 isl_space_free(model);
11766 return map;
11767 error:
11768 isl_space_free(model);
11769 isl_map_free(map);
11770 return NULL;
11773 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11774 __isl_take isl_space *model)
11776 return isl_map_align_params(set, model);
11779 /* Align the parameters of "bmap" to those of "model", introducing
11780 * additional parameters if needed.
11782 __isl_give isl_basic_map *isl_basic_map_align_params(
11783 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11785 isl_ctx *ctx;
11786 isl_bool equal_params;
11788 if (!bmap || !model)
11789 goto error;
11791 ctx = isl_space_get_ctx(model);
11792 if (!isl_space_has_named_params(model))
11793 isl_die(ctx, isl_error_invalid,
11794 "model has unnamed parameters", goto error);
11795 if (isl_basic_map_check_named_params(bmap) < 0)
11796 goto error;
11797 equal_params = isl_space_has_equal_params(bmap->dim, model);
11798 if (equal_params < 0)
11799 goto error;
11800 if (!equal_params) {
11801 isl_reordering *exp;
11802 struct isl_dim_map *dim_map;
11804 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11805 exp = isl_reordering_extend_space(exp,
11806 isl_basic_map_get_space(bmap));
11807 dim_map = isl_dim_map_from_reordering(exp);
11808 bmap = isl_basic_map_realign(bmap,
11809 isl_reordering_get_space(exp),
11810 isl_dim_map_extend(dim_map, bmap));
11811 isl_reordering_free(exp);
11812 free(dim_map);
11815 isl_space_free(model);
11816 return bmap;
11817 error:
11818 isl_space_free(model);
11819 isl_basic_map_free(bmap);
11820 return NULL;
11823 /* Do "bset" and "space" have the same parameters?
11825 isl_bool isl_basic_set_space_has_equal_params(__isl_keep isl_basic_set *bset,
11826 __isl_keep isl_space *space)
11828 isl_space *bset_space;
11830 bset_space = isl_basic_set_peek_space(bset);
11831 return isl_space_has_equal_params(bset_space, space);
11834 /* Do "map" and "space" have the same parameters?
11836 isl_bool isl_map_space_has_equal_params(__isl_keep isl_map *map,
11837 __isl_keep isl_space *space)
11839 isl_space *map_space;
11841 map_space = isl_map_peek_space(map);
11842 return isl_space_has_equal_params(map_space, space);
11845 /* Do "set" and "space" have the same parameters?
11847 isl_bool isl_set_space_has_equal_params(__isl_keep isl_set *set,
11848 __isl_keep isl_space *space)
11850 return isl_map_space_has_equal_params(set_to_map(set), space);
11853 /* Align the parameters of "bset" to those of "model", introducing
11854 * additional parameters if needed.
11856 __isl_give isl_basic_set *isl_basic_set_align_params(
11857 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11859 return isl_basic_map_align_params(bset, model);
11862 /* Drop all parameters not referenced by "map".
11864 __isl_give isl_map *isl_map_drop_unused_params(__isl_take isl_map *map)
11866 int i;
11868 if (isl_map_check_named_params(map) < 0)
11869 return isl_map_free(map);
11871 for (i = isl_map_dim(map, isl_dim_param) - 1; i >= 0; i--) {
11872 isl_bool involves;
11874 involves = isl_map_involves_dims(map, isl_dim_param, i, 1);
11875 if (involves < 0)
11876 return isl_map_free(map);
11877 if (!involves)
11878 map = isl_map_project_out(map, isl_dim_param, i, 1);
11881 return map;
11884 /* Drop all parameters not referenced by "set".
11886 __isl_give isl_set *isl_set_drop_unused_params(
11887 __isl_take isl_set *set)
11889 return set_from_map(isl_map_drop_unused_params(set_to_map(set)));
11892 /* Drop all parameters not referenced by "bmap".
11894 __isl_give isl_basic_map *isl_basic_map_drop_unused_params(
11895 __isl_take isl_basic_map *bmap)
11897 int i;
11899 if (isl_basic_map_check_named_params(bmap) < 0)
11900 return isl_basic_map_free(bmap);
11902 for (i = isl_basic_map_dim(bmap, isl_dim_param) - 1; i >= 0; i--) {
11903 isl_bool involves;
11905 involves = isl_basic_map_involves_dims(bmap,
11906 isl_dim_param, i, 1);
11907 if (involves < 0)
11908 return isl_basic_map_free(bmap);
11909 if (!involves)
11910 bmap = isl_basic_map_drop(bmap, isl_dim_param, i, 1);
11913 return bmap;
11916 /* Drop all parameters not referenced by "bset".
11918 __isl_give isl_basic_set *isl_basic_set_drop_unused_params(
11919 __isl_take isl_basic_set *bset)
11921 return bset_from_bmap(isl_basic_map_drop_unused_params(
11922 bset_to_bmap(bset)));
11925 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11926 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11927 enum isl_dim_type c2, enum isl_dim_type c3,
11928 enum isl_dim_type c4, enum isl_dim_type c5)
11930 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11931 struct isl_mat *mat;
11932 int i, j, k;
11933 int pos;
11935 if (!bmap)
11936 return NULL;
11937 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11938 isl_basic_map_total_dim(bmap) + 1);
11939 if (!mat)
11940 return NULL;
11941 for (i = 0; i < bmap->n_eq; ++i)
11942 for (j = 0, pos = 0; j < 5; ++j) {
11943 int off = isl_basic_map_offset(bmap, c[j]);
11944 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11945 isl_int_set(mat->row[i][pos],
11946 bmap->eq[i][off + k]);
11947 ++pos;
11951 return mat;
11954 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11955 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11956 enum isl_dim_type c2, enum isl_dim_type c3,
11957 enum isl_dim_type c4, enum isl_dim_type c5)
11959 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11960 struct isl_mat *mat;
11961 int i, j, k;
11962 int pos;
11964 if (!bmap)
11965 return NULL;
11966 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11967 isl_basic_map_total_dim(bmap) + 1);
11968 if (!mat)
11969 return NULL;
11970 for (i = 0; i < bmap->n_ineq; ++i)
11971 for (j = 0, pos = 0; j < 5; ++j) {
11972 int off = isl_basic_map_offset(bmap, c[j]);
11973 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11974 isl_int_set(mat->row[i][pos],
11975 bmap->ineq[i][off + k]);
11976 ++pos;
11980 return mat;
11983 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11984 __isl_take isl_space *space,
11985 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11986 enum isl_dim_type c2, enum isl_dim_type c3,
11987 enum isl_dim_type c4, enum isl_dim_type c5)
11989 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11990 isl_basic_map *bmap = NULL;
11991 unsigned total;
11992 unsigned extra;
11993 int i, j, k, l;
11994 int pos;
11996 if (!space || !eq || !ineq)
11997 goto error;
11999 if (eq->n_col != ineq->n_col)
12000 isl_die(space->ctx, isl_error_invalid,
12001 "equalities and inequalities matrices should have "
12002 "same number of columns", goto error);
12004 total = 1 + isl_space_dim(space, isl_dim_all);
12006 if (eq->n_col < total)
12007 isl_die(space->ctx, isl_error_invalid,
12008 "number of columns too small", goto error);
12010 extra = eq->n_col - total;
12012 bmap = isl_basic_map_alloc_space(isl_space_copy(space), extra,
12013 eq->n_row, ineq->n_row);
12014 if (!bmap)
12015 goto error;
12016 for (i = 0; i < extra; ++i) {
12017 k = isl_basic_map_alloc_div(bmap);
12018 if (k < 0)
12019 goto error;
12020 isl_int_set_si(bmap->div[k][0], 0);
12022 for (i = 0; i < eq->n_row; ++i) {
12023 l = isl_basic_map_alloc_equality(bmap);
12024 if (l < 0)
12025 goto error;
12026 for (j = 0, pos = 0; j < 5; ++j) {
12027 int off = isl_basic_map_offset(bmap, c[j]);
12028 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
12029 isl_int_set(bmap->eq[l][off + k],
12030 eq->row[i][pos]);
12031 ++pos;
12035 for (i = 0; i < ineq->n_row; ++i) {
12036 l = isl_basic_map_alloc_inequality(bmap);
12037 if (l < 0)
12038 goto error;
12039 for (j = 0, pos = 0; j < 5; ++j) {
12040 int off = isl_basic_map_offset(bmap, c[j]);
12041 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
12042 isl_int_set(bmap->ineq[l][off + k],
12043 ineq->row[i][pos]);
12044 ++pos;
12049 isl_space_free(space);
12050 isl_mat_free(eq);
12051 isl_mat_free(ineq);
12053 bmap = isl_basic_map_simplify(bmap);
12054 return isl_basic_map_finalize(bmap);
12055 error:
12056 isl_space_free(space);
12057 isl_mat_free(eq);
12058 isl_mat_free(ineq);
12059 isl_basic_map_free(bmap);
12060 return NULL;
12063 __isl_give isl_mat *isl_basic_set_equalities_matrix(
12064 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12065 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12067 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
12068 c1, c2, c3, c4, isl_dim_in);
12071 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
12072 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
12073 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12075 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
12076 c1, c2, c3, c4, isl_dim_in);
12079 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
12080 __isl_take isl_space *dim,
12081 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
12082 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
12084 isl_basic_map *bmap;
12085 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
12086 c1, c2, c3, c4, isl_dim_in);
12087 return bset_from_bmap(bmap);
12090 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
12092 if (!bmap)
12093 return isl_bool_error;
12095 return isl_space_can_zip(bmap->dim);
12098 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
12100 if (!map)
12101 return isl_bool_error;
12103 return isl_space_can_zip(map->dim);
12106 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
12107 * (A -> C) -> (B -> D).
12109 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
12111 unsigned pos;
12112 unsigned n1;
12113 unsigned n2;
12115 if (!bmap)
12116 return NULL;
12118 if (!isl_basic_map_can_zip(bmap))
12119 isl_die(bmap->ctx, isl_error_invalid,
12120 "basic map cannot be zipped", goto error);
12121 pos = isl_basic_map_offset(bmap, isl_dim_in) +
12122 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
12123 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
12124 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
12125 bmap = isl_basic_map_cow(bmap);
12126 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
12127 if (!bmap)
12128 return NULL;
12129 bmap->dim = isl_space_zip(bmap->dim);
12130 if (!bmap->dim)
12131 goto error;
12132 bmap = isl_basic_map_mark_final(bmap);
12133 return bmap;
12134 error:
12135 isl_basic_map_free(bmap);
12136 return NULL;
12139 /* Given a map (A -> B) -> (C -> D), return the corresponding map
12140 * (A -> C) -> (B -> D).
12142 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
12144 int i;
12146 if (!map)
12147 return NULL;
12149 if (!isl_map_can_zip(map))
12150 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
12151 goto error);
12153 map = isl_map_cow(map);
12154 if (!map)
12155 return NULL;
12157 for (i = 0; i < map->n; ++i) {
12158 map->p[i] = isl_basic_map_zip(map->p[i]);
12159 if (!map->p[i])
12160 goto error;
12163 map->dim = isl_space_zip(map->dim);
12164 if (!map->dim)
12165 goto error;
12167 return map;
12168 error:
12169 isl_map_free(map);
12170 return NULL;
12173 /* Can we apply isl_basic_map_curry to "bmap"?
12174 * That is, does it have a nested relation in its domain?
12176 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
12178 if (!bmap)
12179 return isl_bool_error;
12181 return isl_space_can_curry(bmap->dim);
12184 /* Can we apply isl_map_curry to "map"?
12185 * That is, does it have a nested relation in its domain?
12187 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
12189 if (!map)
12190 return isl_bool_error;
12192 return isl_space_can_curry(map->dim);
12195 /* Given a basic map (A -> B) -> C, return the corresponding basic map
12196 * A -> (B -> C).
12198 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
12201 if (!bmap)
12202 return NULL;
12204 if (!isl_basic_map_can_curry(bmap))
12205 isl_die(bmap->ctx, isl_error_invalid,
12206 "basic map cannot be curried", goto error);
12207 bmap = isl_basic_map_cow(bmap);
12208 if (!bmap)
12209 return NULL;
12210 bmap->dim = isl_space_curry(bmap->dim);
12211 if (!bmap->dim)
12212 goto error;
12213 bmap = isl_basic_map_mark_final(bmap);
12214 return bmap;
12215 error:
12216 isl_basic_map_free(bmap);
12217 return NULL;
12220 /* Given a map (A -> B) -> C, return the corresponding map
12221 * A -> (B -> C).
12223 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
12225 return isl_map_change_space(map, &isl_map_can_curry,
12226 "map cannot be curried", &isl_space_curry);
12229 /* Can isl_map_range_curry be applied to "map"?
12230 * That is, does it have a nested relation in its range,
12231 * the domain of which is itself a nested relation?
12233 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
12235 if (!map)
12236 return isl_bool_error;
12238 return isl_space_can_range_curry(map->dim);
12241 /* Given a map A -> ((B -> C) -> D), return the corresponding map
12242 * A -> (B -> (C -> D)).
12244 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
12246 return isl_map_change_space(map, &isl_map_can_range_curry,
12247 "map range cannot be curried",
12248 &isl_space_range_curry);
12251 /* Can we apply isl_basic_map_uncurry to "bmap"?
12252 * That is, does it have a nested relation in its domain?
12254 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
12256 if (!bmap)
12257 return isl_bool_error;
12259 return isl_space_can_uncurry(bmap->dim);
12262 /* Can we apply isl_map_uncurry to "map"?
12263 * That is, does it have a nested relation in its domain?
12265 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
12267 if (!map)
12268 return isl_bool_error;
12270 return isl_space_can_uncurry(map->dim);
12273 /* Given a basic map A -> (B -> C), return the corresponding basic map
12274 * (A -> B) -> C.
12276 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
12279 if (!bmap)
12280 return NULL;
12282 if (!isl_basic_map_can_uncurry(bmap))
12283 isl_die(bmap->ctx, isl_error_invalid,
12284 "basic map cannot be uncurried",
12285 return isl_basic_map_free(bmap));
12286 bmap = isl_basic_map_cow(bmap);
12287 if (!bmap)
12288 return NULL;
12289 bmap->dim = isl_space_uncurry(bmap->dim);
12290 if (!bmap->dim)
12291 return isl_basic_map_free(bmap);
12292 bmap = isl_basic_map_mark_final(bmap);
12293 return bmap;
12296 /* Given a map A -> (B -> C), return the corresponding map
12297 * (A -> B) -> C.
12299 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
12301 return isl_map_change_space(map, &isl_map_can_uncurry,
12302 "map cannot be uncurried", &isl_space_uncurry);
12305 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12306 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12308 return isl_map_equate(set, type1, pos1, type2, pos2);
12311 /* Construct a basic map where the given dimensions are equal to each other.
12313 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12314 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12316 isl_basic_map *bmap = NULL;
12317 int i;
12319 if (!space)
12320 return NULL;
12322 if (pos1 >= isl_space_dim(space, type1))
12323 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12324 "index out of bounds", goto error);
12325 if (pos2 >= isl_space_dim(space, type2))
12326 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12327 "index out of bounds", goto error);
12329 if (type1 == type2 && pos1 == pos2)
12330 return isl_basic_map_universe(space);
12332 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12333 i = isl_basic_map_alloc_equality(bmap);
12334 if (i < 0)
12335 goto error;
12336 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12337 pos1 += isl_basic_map_offset(bmap, type1);
12338 pos2 += isl_basic_map_offset(bmap, type2);
12339 isl_int_set_si(bmap->eq[i][pos1], -1);
12340 isl_int_set_si(bmap->eq[i][pos2], 1);
12341 bmap = isl_basic_map_finalize(bmap);
12342 isl_space_free(space);
12343 return bmap;
12344 error:
12345 isl_space_free(space);
12346 isl_basic_map_free(bmap);
12347 return NULL;
12350 /* Add a constraint imposing that the given two dimensions are equal.
12352 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12353 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12355 isl_basic_map *eq;
12357 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12359 bmap = isl_basic_map_intersect(bmap, eq);
12361 return bmap;
12364 /* Add a constraint imposing that the given two dimensions are equal.
12366 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12367 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12369 isl_basic_map *bmap;
12371 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12373 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12375 return map;
12378 /* Add a constraint imposing that the given two dimensions have opposite values.
12380 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12381 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12383 isl_basic_map *bmap = NULL;
12384 int i;
12386 if (!map)
12387 return NULL;
12389 if (pos1 >= isl_map_dim(map, type1))
12390 isl_die(map->ctx, isl_error_invalid,
12391 "index out of bounds", goto error);
12392 if (pos2 >= isl_map_dim(map, type2))
12393 isl_die(map->ctx, isl_error_invalid,
12394 "index out of bounds", goto error);
12396 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12397 i = isl_basic_map_alloc_equality(bmap);
12398 if (i < 0)
12399 goto error;
12400 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12401 pos1 += isl_basic_map_offset(bmap, type1);
12402 pos2 += isl_basic_map_offset(bmap, type2);
12403 isl_int_set_si(bmap->eq[i][pos1], 1);
12404 isl_int_set_si(bmap->eq[i][pos2], 1);
12405 bmap = isl_basic_map_finalize(bmap);
12407 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12409 return map;
12410 error:
12411 isl_basic_map_free(bmap);
12412 isl_map_free(map);
12413 return NULL;
12416 /* Construct a constraint imposing that the value of the first dimension is
12417 * greater than or equal to that of the second.
12419 static __isl_give isl_constraint *constraint_order_ge(
12420 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12421 enum isl_dim_type type2, int pos2)
12423 isl_constraint *c;
12425 if (!space)
12426 return NULL;
12428 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12430 if (pos1 >= isl_constraint_dim(c, type1))
12431 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12432 "index out of bounds", return isl_constraint_free(c));
12433 if (pos2 >= isl_constraint_dim(c, type2))
12434 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12435 "index out of bounds", return isl_constraint_free(c));
12437 if (type1 == type2 && pos1 == pos2)
12438 return c;
12440 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12441 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12443 return c;
12446 /* Add a constraint imposing that the value of the first dimension is
12447 * greater than or equal to that of the second.
12449 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12450 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12452 isl_constraint *c;
12453 isl_space *space;
12455 if (type1 == type2 && pos1 == pos2)
12456 return bmap;
12457 space = isl_basic_map_get_space(bmap);
12458 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12459 bmap = isl_basic_map_add_constraint(bmap, c);
12461 return bmap;
12464 /* Add a constraint imposing that the value of the first dimension is
12465 * greater than or equal to that of the second.
12467 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12468 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12470 isl_constraint *c;
12471 isl_space *space;
12473 if (type1 == type2 && pos1 == pos2)
12474 return map;
12475 space = isl_map_get_space(map);
12476 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12477 map = isl_map_add_constraint(map, c);
12479 return map;
12482 /* Add a constraint imposing that the value of the first dimension is
12483 * less than or equal to that of the second.
12485 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12486 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12488 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12491 /* Construct a basic map where the value of the first dimension is
12492 * greater than that of the second.
12494 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12495 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12497 isl_basic_map *bmap = NULL;
12498 int i;
12500 if (!space)
12501 return NULL;
12503 if (pos1 >= isl_space_dim(space, type1))
12504 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12505 "index out of bounds", goto error);
12506 if (pos2 >= isl_space_dim(space, type2))
12507 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12508 "index out of bounds", goto error);
12510 if (type1 == type2 && pos1 == pos2)
12511 return isl_basic_map_empty(space);
12513 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12514 i = isl_basic_map_alloc_inequality(bmap);
12515 if (i < 0)
12516 return isl_basic_map_free(bmap);
12517 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12518 pos1 += isl_basic_map_offset(bmap, type1);
12519 pos2 += isl_basic_map_offset(bmap, type2);
12520 isl_int_set_si(bmap->ineq[i][pos1], 1);
12521 isl_int_set_si(bmap->ineq[i][pos2], -1);
12522 isl_int_set_si(bmap->ineq[i][0], -1);
12523 bmap = isl_basic_map_finalize(bmap);
12525 return bmap;
12526 error:
12527 isl_space_free(space);
12528 isl_basic_map_free(bmap);
12529 return NULL;
12532 /* Add a constraint imposing that the value of the first dimension is
12533 * greater than that of the second.
12535 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12536 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12538 isl_basic_map *gt;
12540 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12542 bmap = isl_basic_map_intersect(bmap, gt);
12544 return bmap;
12547 /* Add a constraint imposing that the value of the first dimension is
12548 * greater than that of the second.
12550 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12551 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12553 isl_basic_map *bmap;
12555 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12557 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12559 return map;
12562 /* Add a constraint imposing that the value of the first dimension is
12563 * smaller than that of the second.
12565 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12566 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12568 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12571 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12572 int pos)
12574 isl_aff *div;
12575 isl_local_space *ls;
12577 if (!bmap)
12578 return NULL;
12580 if (!isl_basic_map_divs_known(bmap))
12581 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12582 "some divs are unknown", return NULL);
12584 ls = isl_basic_map_get_local_space(bmap);
12585 div = isl_local_space_get_div(ls, pos);
12586 isl_local_space_free(ls);
12588 return div;
12591 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12592 int pos)
12594 return isl_basic_map_get_div(bset, pos);
12597 /* Plug in "subs" for dimension "type", "pos" of "bset".
12599 * Let i be the dimension to replace and let "subs" be of the form
12601 * f/d
12603 * Any integer division with a non-zero coefficient for i,
12605 * floor((a i + g)/m)
12607 * is replaced by
12609 * floor((a f + d g)/(m d))
12611 * Constraints of the form
12613 * a i + g
12615 * are replaced by
12617 * a f + d g
12619 * We currently require that "subs" is an integral expression.
12620 * Handling rational expressions may require us to add stride constraints
12621 * as we do in isl_basic_set_preimage_multi_aff.
12623 __isl_give isl_basic_set *isl_basic_set_substitute(
12624 __isl_take isl_basic_set *bset,
12625 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12627 int i;
12628 isl_int v;
12629 isl_ctx *ctx;
12631 if (bset && isl_basic_set_plain_is_empty(bset))
12632 return bset;
12634 bset = isl_basic_set_cow(bset);
12635 if (!bset || !subs)
12636 goto error;
12638 ctx = isl_basic_set_get_ctx(bset);
12639 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12640 isl_die(ctx, isl_error_invalid,
12641 "spaces don't match", goto error);
12642 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12643 isl_die(ctx, isl_error_unsupported,
12644 "cannot handle divs yet", goto error);
12645 if (!isl_int_is_one(subs->v->el[0]))
12646 isl_die(ctx, isl_error_invalid,
12647 "can only substitute integer expressions", goto error);
12649 pos += isl_basic_set_offset(bset, type);
12651 isl_int_init(v);
12653 for (i = 0; i < bset->n_eq; ++i) {
12654 if (isl_int_is_zero(bset->eq[i][pos]))
12655 continue;
12656 isl_int_set(v, bset->eq[i][pos]);
12657 isl_int_set_si(bset->eq[i][pos], 0);
12658 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12659 v, subs->v->el + 1, subs->v->size - 1);
12662 for (i = 0; i < bset->n_ineq; ++i) {
12663 if (isl_int_is_zero(bset->ineq[i][pos]))
12664 continue;
12665 isl_int_set(v, bset->ineq[i][pos]);
12666 isl_int_set_si(bset->ineq[i][pos], 0);
12667 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12668 v, subs->v->el + 1, subs->v->size - 1);
12671 for (i = 0; i < bset->n_div; ++i) {
12672 if (isl_int_is_zero(bset->div[i][1 + pos]))
12673 continue;
12674 isl_int_set(v, bset->div[i][1 + pos]);
12675 isl_int_set_si(bset->div[i][1 + pos], 0);
12676 isl_seq_combine(bset->div[i] + 1,
12677 subs->v->el[0], bset->div[i] + 1,
12678 v, subs->v->el + 1, subs->v->size - 1);
12679 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12682 isl_int_clear(v);
12684 bset = isl_basic_set_simplify(bset);
12685 return isl_basic_set_finalize(bset);
12686 error:
12687 isl_basic_set_free(bset);
12688 return NULL;
12691 /* Plug in "subs" for dimension "type", "pos" of "set".
12693 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12694 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12696 int i;
12698 if (set && isl_set_plain_is_empty(set))
12699 return set;
12701 set = isl_set_cow(set);
12702 if (!set || !subs)
12703 goto error;
12705 for (i = set->n - 1; i >= 0; --i) {
12706 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12707 set = set_from_map(remove_if_empty(set_to_map(set), i));
12708 if (!set)
12709 return NULL;
12712 return set;
12713 error:
12714 isl_set_free(set);
12715 return NULL;
12718 /* Check if the range of "ma" is compatible with the domain or range
12719 * (depending on "type") of "bmap".
12721 static isl_stat check_basic_map_compatible_range_multi_aff(
12722 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12723 __isl_keep isl_multi_aff *ma)
12725 isl_bool m;
12726 isl_space *ma_space;
12728 ma_space = isl_multi_aff_get_space(ma);
12730 m = isl_space_has_equal_params(bmap->dim, ma_space);
12731 if (m < 0)
12732 goto error;
12733 if (!m)
12734 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12735 "parameters don't match", goto error);
12736 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12737 if (m < 0)
12738 goto error;
12739 if (!m)
12740 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12741 "spaces don't match", goto error);
12743 isl_space_free(ma_space);
12744 return isl_stat_ok;
12745 error:
12746 isl_space_free(ma_space);
12747 return isl_stat_error;
12750 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12751 * coefficients before the transformed range of dimensions,
12752 * the "n_after" coefficients after the transformed range of dimensions
12753 * and the coefficients of the other divs in "bmap".
12755 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12756 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12758 int i;
12759 int n_param;
12760 int n_set;
12761 isl_local_space *ls;
12763 if (n_div == 0)
12764 return 0;
12766 ls = isl_aff_get_domain_local_space(ma->u.p[0]);
12767 if (!ls)
12768 return -1;
12770 n_param = isl_local_space_dim(ls, isl_dim_param);
12771 n_set = isl_local_space_dim(ls, isl_dim_set);
12772 for (i = 0; i < n_div; ++i) {
12773 int o_bmap = 0, o_ls = 0;
12775 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12776 o_bmap += 1 + 1 + n_param;
12777 o_ls += 1 + 1 + n_param;
12778 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12779 o_bmap += n_before;
12780 isl_seq_cpy(bmap->div[i] + o_bmap,
12781 ls->div->row[i] + o_ls, n_set);
12782 o_bmap += n_set;
12783 o_ls += n_set;
12784 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12785 o_bmap += n_after;
12786 isl_seq_cpy(bmap->div[i] + o_bmap,
12787 ls->div->row[i] + o_ls, n_div);
12788 o_bmap += n_div;
12789 o_ls += n_div;
12790 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12791 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12792 goto error;
12795 isl_local_space_free(ls);
12796 return 0;
12797 error:
12798 isl_local_space_free(ls);
12799 return -1;
12802 /* How many stride constraints does "ma" enforce?
12803 * That is, how many of the affine expressions have a denominator
12804 * different from one?
12806 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12808 int i;
12809 int strides = 0;
12811 for (i = 0; i < ma->n; ++i)
12812 if (!isl_int_is_one(ma->u.p[i]->v->el[0]))
12813 strides++;
12815 return strides;
12818 /* For each affine expression in ma of the form
12820 * x_i = (f_i y + h_i)/m_i
12822 * with m_i different from one, add a constraint to "bmap"
12823 * of the form
12825 * f_i y + h_i = m_i alpha_i
12827 * with alpha_i an additional existentially quantified variable.
12829 * The input variables of "ma" correspond to a subset of the variables
12830 * of "bmap". There are "n_before" variables in "bmap" before this
12831 * subset and "n_after" variables after this subset.
12832 * The integer divisions of the affine expressions in "ma" are assumed
12833 * to have been aligned. There are "n_div_ma" of them and
12834 * they appear first in "bmap", straight after the "n_after" variables.
12836 static __isl_give isl_basic_map *add_ma_strides(
12837 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12838 int n_before, int n_after, int n_div_ma)
12840 int i, k;
12841 int div;
12842 int total;
12843 int n_param;
12844 int n_in;
12846 total = isl_basic_map_total_dim(bmap);
12847 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12848 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12849 for (i = 0; i < ma->n; ++i) {
12850 int o_bmap = 0, o_ma = 1;
12852 if (isl_int_is_one(ma->u.p[i]->v->el[0]))
12853 continue;
12854 div = isl_basic_map_alloc_div(bmap);
12855 k = isl_basic_map_alloc_equality(bmap);
12856 if (div < 0 || k < 0)
12857 goto error;
12858 isl_int_set_si(bmap->div[div][0], 0);
12859 isl_seq_cpy(bmap->eq[k] + o_bmap,
12860 ma->u.p[i]->v->el + o_ma, 1 + n_param);
12861 o_bmap += 1 + n_param;
12862 o_ma += 1 + n_param;
12863 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12864 o_bmap += n_before;
12865 isl_seq_cpy(bmap->eq[k] + o_bmap,
12866 ma->u.p[i]->v->el + o_ma, n_in);
12867 o_bmap += n_in;
12868 o_ma += n_in;
12869 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12870 o_bmap += n_after;
12871 isl_seq_cpy(bmap->eq[k] + o_bmap,
12872 ma->u.p[i]->v->el + o_ma, n_div_ma);
12873 o_bmap += n_div_ma;
12874 o_ma += n_div_ma;
12875 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12876 isl_int_neg(bmap->eq[k][1 + total], ma->u.p[i]->v->el[0]);
12877 total++;
12880 return bmap;
12881 error:
12882 isl_basic_map_free(bmap);
12883 return NULL;
12886 /* Replace the domain or range space (depending on "type) of "space" by "set".
12888 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12889 enum isl_dim_type type, __isl_take isl_space *set)
12891 if (type == isl_dim_in) {
12892 space = isl_space_range(space);
12893 space = isl_space_map_from_domain_and_range(set, space);
12894 } else {
12895 space = isl_space_domain(space);
12896 space = isl_space_map_from_domain_and_range(space, set);
12899 return space;
12902 /* Compute the preimage of the domain or range (depending on "type")
12903 * of "bmap" under the function represented by "ma".
12904 * In other words, plug in "ma" in the domain or range of "bmap".
12905 * The result is a basic map that lives in the same space as "bmap"
12906 * except that the domain or range has been replaced by
12907 * the domain space of "ma".
12909 * If bmap is represented by
12911 * A(p) + S u + B x + T v + C(divs) >= 0,
12913 * where u and x are input and output dimensions if type == isl_dim_out
12914 * while x and v are input and output dimensions if type == isl_dim_in,
12915 * and ma is represented by
12917 * x = D(p) + F(y) + G(divs')
12919 * then the result is
12921 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12923 * The divs in the input set are similarly adjusted.
12924 * In particular
12926 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12928 * becomes
12930 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12931 * B_i G(divs') + c_i(divs))/n_i)
12933 * If bmap is not a rational map and if F(y) involves any denominators
12935 * x_i = (f_i y + h_i)/m_i
12937 * then additional constraints are added to ensure that we only
12938 * map back integer points. That is we enforce
12940 * f_i y + h_i = m_i alpha_i
12942 * with alpha_i an additional existentially quantified variable.
12944 * We first copy over the divs from "ma".
12945 * Then we add the modified constraints and divs from "bmap".
12946 * Finally, we add the stride constraints, if needed.
12948 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12949 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12950 __isl_take isl_multi_aff *ma)
12952 int i, k;
12953 isl_space *space;
12954 isl_basic_map *res = NULL;
12955 int n_before, n_after, n_div_bmap, n_div_ma;
12956 isl_int f, c1, c2, g;
12957 isl_bool rational;
12958 int strides;
12960 isl_int_init(f);
12961 isl_int_init(c1);
12962 isl_int_init(c2);
12963 isl_int_init(g);
12965 ma = isl_multi_aff_align_divs(ma);
12966 if (!bmap || !ma)
12967 goto error;
12968 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12969 goto error;
12971 if (type == isl_dim_in) {
12972 n_before = 0;
12973 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12974 } else {
12975 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12976 n_after = 0;
12978 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12979 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
12981 space = isl_multi_aff_get_domain_space(ma);
12982 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12983 rational = isl_basic_map_is_rational(bmap);
12984 strides = rational ? 0 : multi_aff_strides(ma);
12985 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12986 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12987 if (rational)
12988 res = isl_basic_map_set_rational(res);
12990 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12991 if (isl_basic_map_alloc_div(res) < 0)
12992 goto error;
12994 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12995 goto error;
12997 for (i = 0; i < bmap->n_eq; ++i) {
12998 k = isl_basic_map_alloc_equality(res);
12999 if (k < 0)
13000 goto error;
13001 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
13002 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13005 for (i = 0; i < bmap->n_ineq; ++i) {
13006 k = isl_basic_map_alloc_inequality(res);
13007 if (k < 0)
13008 goto error;
13009 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
13010 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
13013 for (i = 0; i < bmap->n_div; ++i) {
13014 if (isl_int_is_zero(bmap->div[i][0])) {
13015 isl_int_set_si(res->div[n_div_ma + i][0], 0);
13016 continue;
13018 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
13019 n_before, n_after, n_div_ma, n_div_bmap,
13020 f, c1, c2, g, 1);
13023 if (strides)
13024 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
13026 isl_int_clear(f);
13027 isl_int_clear(c1);
13028 isl_int_clear(c2);
13029 isl_int_clear(g);
13030 isl_basic_map_free(bmap);
13031 isl_multi_aff_free(ma);
13032 res = isl_basic_map_simplify(res);
13033 return isl_basic_map_finalize(res);
13034 error:
13035 isl_int_clear(f);
13036 isl_int_clear(c1);
13037 isl_int_clear(c2);
13038 isl_int_clear(g);
13039 isl_basic_map_free(bmap);
13040 isl_multi_aff_free(ma);
13041 isl_basic_map_free(res);
13042 return NULL;
13045 /* Compute the preimage of "bset" under the function represented by "ma".
13046 * In other words, plug in "ma" in "bset". The result is a basic set
13047 * that lives in the domain space of "ma".
13049 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
13050 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
13052 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
13055 /* Compute the preimage of the domain of "bmap" under the function
13056 * represented by "ma".
13057 * In other words, plug in "ma" in the domain of "bmap".
13058 * The result is a basic map that lives in the same space as "bmap"
13059 * except that the domain has been replaced by the domain space of "ma".
13061 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
13062 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13064 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
13067 /* Compute the preimage of the range of "bmap" under the function
13068 * represented by "ma".
13069 * In other words, plug in "ma" in the range of "bmap".
13070 * The result is a basic map that lives in the same space as "bmap"
13071 * except that the range has been replaced by the domain space of "ma".
13073 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
13074 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
13076 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
13079 /* Check if the range of "ma" is compatible with the domain or range
13080 * (depending on "type") of "map".
13081 * Return isl_stat_error if anything is wrong.
13083 static isl_stat check_map_compatible_range_multi_aff(
13084 __isl_keep isl_map *map, enum isl_dim_type type,
13085 __isl_keep isl_multi_aff *ma)
13087 isl_bool m;
13088 isl_space *ma_space;
13090 ma_space = isl_multi_aff_get_space(ma);
13091 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
13092 isl_space_free(ma_space);
13093 if (m < 0)
13094 return isl_stat_error;
13095 if (!m)
13096 isl_die(isl_map_get_ctx(map), isl_error_invalid,
13097 "spaces don't match", return isl_stat_error);
13098 return isl_stat_ok;
13101 /* Compute the preimage of the domain or range (depending on "type")
13102 * of "map" under the function represented by "ma".
13103 * In other words, plug in "ma" in the domain or range of "map".
13104 * The result is a map that lives in the same space as "map"
13105 * except that the domain or range has been replaced by
13106 * the domain space of "ma".
13108 * The parameters are assumed to have been aligned.
13110 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
13111 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13113 int i;
13114 isl_space *space;
13116 map = isl_map_cow(map);
13117 ma = isl_multi_aff_align_divs(ma);
13118 if (!map || !ma)
13119 goto error;
13120 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
13121 goto error;
13123 for (i = 0; i < map->n; ++i) {
13124 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
13125 isl_multi_aff_copy(ma));
13126 if (!map->p[i])
13127 goto error;
13130 space = isl_multi_aff_get_domain_space(ma);
13131 space = isl_space_set(isl_map_get_space(map), type, space);
13133 isl_space_free(map->dim);
13134 map->dim = space;
13135 if (!map->dim)
13136 goto error;
13138 isl_multi_aff_free(ma);
13139 if (map->n > 1)
13140 ISL_F_CLR(map, ISL_MAP_DISJOINT);
13141 ISL_F_CLR(map, ISL_SET_NORMALIZED);
13142 return map;
13143 error:
13144 isl_multi_aff_free(ma);
13145 isl_map_free(map);
13146 return NULL;
13149 /* Compute the preimage of the domain or range (depending on "type")
13150 * of "map" under the function represented by "ma".
13151 * In other words, plug in "ma" in the domain or range of "map".
13152 * The result is a map that lives in the same space as "map"
13153 * except that the domain or range has been replaced by
13154 * the domain space of "ma".
13156 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
13157 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
13159 isl_bool aligned;
13161 if (!map || !ma)
13162 goto error;
13164 aligned = isl_map_space_has_equal_params(map, ma->space);
13165 if (aligned < 0)
13166 goto error;
13167 if (aligned)
13168 return map_preimage_multi_aff(map, type, ma);
13170 if (isl_map_check_named_params(map) < 0)
13171 goto error;
13172 if (!isl_space_has_named_params(ma->space))
13173 isl_die(map->ctx, isl_error_invalid,
13174 "unaligned unnamed parameters", goto error);
13175 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
13176 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
13178 return map_preimage_multi_aff(map, type, ma);
13179 error:
13180 isl_multi_aff_free(ma);
13181 return isl_map_free(map);
13184 /* Compute the preimage of "set" under the function represented by "ma".
13185 * In other words, plug in "ma" in "set". The result is a set
13186 * that lives in the domain space of "ma".
13188 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
13189 __isl_take isl_multi_aff *ma)
13191 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
13194 /* Compute the preimage of the domain of "map" under the function
13195 * represented by "ma".
13196 * In other words, plug in "ma" in the domain of "map".
13197 * The result is a map that lives in the same space as "map"
13198 * except that the domain has been replaced by the domain space of "ma".
13200 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
13201 __isl_take isl_multi_aff *ma)
13203 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
13206 /* Compute the preimage of the range of "map" under the function
13207 * represented by "ma".
13208 * In other words, plug in "ma" in the range of "map".
13209 * The result is a map that lives in the same space as "map"
13210 * except that the range has been replaced by the domain space of "ma".
13212 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
13213 __isl_take isl_multi_aff *ma)
13215 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
13218 /* Compute the preimage of "map" under the function represented by "pma".
13219 * In other words, plug in "pma" in the domain or range of "map".
13220 * The result is a map that lives in the same space as "map",
13221 * except that the space of type "type" has been replaced by
13222 * the domain space of "pma".
13224 * The parameters of "map" and "pma" are assumed to have been aligned.
13226 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
13227 __isl_take isl_map *map, enum isl_dim_type type,
13228 __isl_take isl_pw_multi_aff *pma)
13230 int i;
13231 isl_map *res;
13233 if (!pma)
13234 goto error;
13236 if (pma->n == 0) {
13237 isl_pw_multi_aff_free(pma);
13238 res = isl_map_empty(isl_map_get_space(map));
13239 isl_map_free(map);
13240 return res;
13243 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13244 isl_multi_aff_copy(pma->p[0].maff));
13245 if (type == isl_dim_in)
13246 res = isl_map_intersect_domain(res,
13247 isl_map_copy(pma->p[0].set));
13248 else
13249 res = isl_map_intersect_range(res,
13250 isl_map_copy(pma->p[0].set));
13252 for (i = 1; i < pma->n; ++i) {
13253 isl_map *res_i;
13255 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
13256 isl_multi_aff_copy(pma->p[i].maff));
13257 if (type == isl_dim_in)
13258 res_i = isl_map_intersect_domain(res_i,
13259 isl_map_copy(pma->p[i].set));
13260 else
13261 res_i = isl_map_intersect_range(res_i,
13262 isl_map_copy(pma->p[i].set));
13263 res = isl_map_union(res, res_i);
13266 isl_pw_multi_aff_free(pma);
13267 isl_map_free(map);
13268 return res;
13269 error:
13270 isl_pw_multi_aff_free(pma);
13271 isl_map_free(map);
13272 return NULL;
13275 /* Compute the preimage of "map" under the function represented by "pma".
13276 * In other words, plug in "pma" in the domain or range of "map".
13277 * The result is a map that lives in the same space as "map",
13278 * except that the space of type "type" has been replaced by
13279 * the domain space of "pma".
13281 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
13282 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
13284 isl_bool aligned;
13286 if (!map || !pma)
13287 goto error;
13289 aligned = isl_map_space_has_equal_params(map, pma->dim);
13290 if (aligned < 0)
13291 goto error;
13292 if (aligned)
13293 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13295 if (isl_map_check_named_params(map) < 0)
13296 goto error;
13297 if (isl_pw_multi_aff_check_named_params(pma) < 0)
13298 goto error;
13299 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
13300 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
13302 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
13303 error:
13304 isl_pw_multi_aff_free(pma);
13305 return isl_map_free(map);
13308 /* Compute the preimage of "set" under the function represented by "pma".
13309 * In other words, plug in "pma" in "set". The result is a set
13310 * that lives in the domain space of "pma".
13312 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13313 __isl_take isl_pw_multi_aff *pma)
13315 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13318 /* Compute the preimage of the domain of "map" under the function
13319 * represented by "pma".
13320 * In other words, plug in "pma" in the domain of "map".
13321 * The result is a map that lives in the same space as "map",
13322 * except that domain space has been replaced by the domain space of "pma".
13324 __isl_give isl_map *isl_map_preimage_domain_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_in, pma);
13330 /* Compute the preimage of the range of "map" under the function
13331 * represented by "pma".
13332 * In other words, plug in "pma" in the range of "map".
13333 * The result is a map that lives in the same space as "map",
13334 * except that range space has been replaced by the domain space of "pma".
13336 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13337 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13339 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13342 /* Compute the preimage of "map" under the function represented by "mpa".
13343 * In other words, plug in "mpa" in the domain or range of "map".
13344 * The result is a map that lives in the same space as "map",
13345 * except that the space of type "type" has been replaced by
13346 * the domain space of "mpa".
13348 * If the map does not involve any constraints that refer to the
13349 * dimensions of the substituted space, then the only possible
13350 * effect of "mpa" on the map is to map the space to a different space.
13351 * We create a separate isl_multi_aff to effectuate this change
13352 * in order to avoid spurious splitting of the map along the pieces
13353 * of "mpa".
13354 * If "mpa" has a non-trivial explicit domain, however,
13355 * then the full substitution should be performed.
13357 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13358 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13360 int n;
13361 isl_bool full;
13362 isl_pw_multi_aff *pma;
13364 if (!map || !mpa)
13365 goto error;
13367 n = isl_map_dim(map, type);
13368 full = isl_map_involves_dims(map, type, 0, n);
13369 if (full >= 0 && !full)
13370 full = isl_multi_pw_aff_has_non_trivial_domain(mpa);
13371 if (full < 0)
13372 goto error;
13373 if (!full) {
13374 isl_space *space;
13375 isl_multi_aff *ma;
13377 space = isl_multi_pw_aff_get_space(mpa);
13378 isl_multi_pw_aff_free(mpa);
13379 ma = isl_multi_aff_zero(space);
13380 return isl_map_preimage_multi_aff(map, type, ma);
13383 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13384 return isl_map_preimage_pw_multi_aff(map, type, pma);
13385 error:
13386 isl_map_free(map);
13387 isl_multi_pw_aff_free(mpa);
13388 return NULL;
13391 /* Compute the preimage of "map" under the function represented by "mpa".
13392 * In other words, plug in "mpa" in the domain "map".
13393 * The result is a map that lives in the same space as "map",
13394 * except that domain space has been replaced by the domain space of "mpa".
13396 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13397 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13399 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13402 /* Compute the preimage of "set" by the function represented by "mpa".
13403 * In other words, plug in "mpa" in "set".
13405 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13406 __isl_take isl_multi_pw_aff *mpa)
13408 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
13411 /* Return a copy of the equality constraints of "bset" as a matrix.
13413 __isl_give isl_mat *isl_basic_set_extract_equalities(
13414 __isl_keep isl_basic_set *bset)
13416 isl_ctx *ctx;
13417 unsigned total;
13419 if (!bset)
13420 return NULL;
13422 ctx = isl_basic_set_get_ctx(bset);
13423 total = 1 + isl_basic_set_dim(bset, isl_dim_all);
13424 return isl_mat_sub_alloc6(ctx, bset->eq, 0, bset->n_eq, 0, total);
13427 /* Are the "n" "coefficients" starting at "first" of the integer division
13428 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
13429 * to each other?
13430 * The "coefficient" at position 0 is the denominator.
13431 * The "coefficient" at position 1 is the constant term.
13433 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
13434 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
13435 unsigned first, unsigned n)
13437 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
13438 return isl_bool_error;
13439 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
13440 return isl_bool_error;
13441 return isl_seq_eq(bmap1->div[pos1] + first,
13442 bmap2->div[pos2] + first, n);
13445 /* Are the integer division expressions at position "pos1" in "bmap1" and
13446 * "pos2" in "bmap2" equal to each other, except that the constant terms
13447 * are different?
13449 isl_bool isl_basic_map_equal_div_expr_except_constant(
13450 __isl_keep isl_basic_map *bmap1, int pos1,
13451 __isl_keep isl_basic_map *bmap2, int pos2)
13453 isl_bool equal;
13454 unsigned total;
13456 if (!bmap1 || !bmap2)
13457 return isl_bool_error;
13458 total = isl_basic_map_total_dim(bmap1);
13459 if (total != isl_basic_map_total_dim(bmap2))
13460 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
13461 "incomparable div expressions", return isl_bool_error);
13462 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13463 0, 1);
13464 if (equal < 0 || !equal)
13465 return equal;
13466 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13467 1, 1);
13468 if (equal < 0 || equal)
13469 return isl_bool_not(equal);
13470 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
13471 2, total);
13474 /* Replace the numerator of the constant term of the integer division
13475 * expression at position "div" in "bmap" by "value".
13476 * The caller guarantees that this does not change the meaning
13477 * of the input.
13479 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
13480 __isl_take isl_basic_map *bmap, int div, int value)
13482 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
13483 return isl_basic_map_free(bmap);
13485 isl_int_set_si(bmap->div[div][1], value);
13487 return bmap;
13490 /* Is the point "inner" internal to inequality constraint "ineq"
13491 * of "bset"?
13492 * The point is considered to be internal to the inequality constraint,
13493 * if it strictly lies on the positive side of the inequality constraint,
13494 * or if it lies on the constraint and the constraint is lexico-positive.
13496 static isl_bool is_internal(__isl_keep isl_vec *inner,
13497 __isl_keep isl_basic_set *bset, int ineq)
13499 isl_ctx *ctx;
13500 int pos;
13501 unsigned total;
13503 if (!inner || !bset)
13504 return isl_bool_error;
13506 ctx = isl_basic_set_get_ctx(bset);
13507 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
13508 &ctx->normalize_gcd);
13509 if (!isl_int_is_zero(ctx->normalize_gcd))
13510 return isl_int_is_nonneg(ctx->normalize_gcd);
13512 total = isl_basic_set_dim(bset, isl_dim_all);
13513 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
13514 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
13517 /* Tighten the inequality constraints of "bset" that are outward with respect
13518 * to the point "vec".
13519 * That is, tighten the constraints that are not satisfied by "vec".
13521 * "vec" is a point internal to some superset S of "bset" that is used
13522 * to make the subsets of S disjoint, by tightening one half of the constraints
13523 * that separate two subsets. In particular, the constraints of S
13524 * are all satisfied by "vec" and should not be tightened.
13525 * Of the internal constraints, those that have "vec" on the outside
13526 * are tightened. The shared facet is included in the adjacent subset
13527 * with the opposite constraint.
13528 * For constraints that saturate "vec", this criterion cannot be used
13529 * to determine which of the two sides should be tightened.
13530 * Instead, the sign of the first non-zero coefficient is used
13531 * to make this choice. Note that this second criterion is never used
13532 * on the constraints of S since "vec" is interior to "S".
13534 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
13535 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
13537 int j;
13539 bset = isl_basic_set_cow(bset);
13540 if (!bset)
13541 return NULL;
13542 for (j = 0; j < bset->n_ineq; ++j) {
13543 isl_bool internal;
13545 internal = is_internal(vec, bset, j);
13546 if (internal < 0)
13547 return isl_basic_set_free(bset);
13548 if (internal)
13549 continue;
13550 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13553 return bset;
13556 /* Replace the variables x of type "type" starting at "first" in "bmap"
13557 * by x' with x = M x' with M the matrix trans.
13558 * That is, replace the corresponding coefficients c by c M.
13560 * The transformation matrix should be a square matrix.
13562 __isl_give isl_basic_map *isl_basic_map_transform_dims(
13563 __isl_take isl_basic_map *bmap, enum isl_dim_type type, unsigned first,
13564 __isl_take isl_mat *trans)
13566 unsigned pos;
13568 bmap = isl_basic_map_cow(bmap);
13569 if (!bmap || !trans)
13570 goto error;
13572 if (trans->n_row != trans->n_col)
13573 isl_die(trans->ctx, isl_error_invalid,
13574 "expecting square transformation matrix", goto error);
13575 if (first + trans->n_row > isl_basic_map_dim(bmap, type))
13576 isl_die(trans->ctx, isl_error_invalid,
13577 "oversized transformation matrix", goto error);
13579 pos = isl_basic_map_offset(bmap, type) + first;
13581 if (isl_mat_sub_transform(bmap->eq, bmap->n_eq, pos,
13582 isl_mat_copy(trans)) < 0)
13583 goto error;
13584 if (isl_mat_sub_transform(bmap->ineq, bmap->n_ineq, pos,
13585 isl_mat_copy(trans)) < 0)
13586 goto error;
13587 if (isl_mat_sub_transform(bmap->div, bmap->n_div, 1 + pos,
13588 isl_mat_copy(trans)) < 0)
13589 goto error;
13591 ISL_F_CLR(bmap, ISL_BASIC_MAP_SORTED);
13592 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
13594 isl_mat_free(trans);
13595 return bmap;
13596 error:
13597 isl_mat_free(trans);
13598 isl_basic_map_free(bmap);
13599 return NULL;
13602 /* Replace the variables x of type "type" starting at "first" in "bset"
13603 * by x' with x = M x' with M the matrix trans.
13604 * That is, replace the corresponding coefficients c by c M.
13606 * The transformation matrix should be a square matrix.
13608 __isl_give isl_basic_set *isl_basic_set_transform_dims(
13609 __isl_take isl_basic_set *bset, enum isl_dim_type type, unsigned first,
13610 __isl_take isl_mat *trans)
13612 return isl_basic_map_transform_dims(bset, type, first, trans);